~ubuntu-branches/ubuntu/karmic/sbuild/karmic-proposed

« back to all changes in this revision

Viewing changes to bin/sbuild-createchroot

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2009-05-09 16:06:44 UTC
  • mfrom: (8.1.6 upstream) (3.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090509160644-9k0fgp6c2ajcu54h
Tags: 0.58.2-1ubuntu1
* Merge from debian unstable, remaining changes:
  - bin/sbuild, lib/Sbuild/{Base,Conf,Options}.pm: add --setup-hook
    to allow pre-build modifications to underlying chroots (needed
    to adjust pockets and components in sources.list).  (debian bug
    500746).

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
use POSIX;
28
28
use Getopt::Long qw(:config no_ignore_case auto_abbrev gnu_getopt);
29
 
use Sbuild::Sysconfig qw($arch $hostname);
30
 
use Sbuild qw(dump_file help_text version_text usage_error);
 
29
use Sbuild qw(dump_file help_text version_text usage_error check_packages);
 
30
use Sbuild::Conf;
 
31
use Sbuild::ChrootPlain;
 
32
use Sbuild::Sysconfig;
 
33
use File::Path qw(mkpath);
31
34
use File::Temp ();
32
35
 
33
36
package main;
34
37
 
35
38
sub add_items ($@);
36
 
sub dump_file ($);
 
39
sub makedir ($$);
 
40
 
 
41
my $conf = Sbuild::Conf->new();
 
42
my $hostname = $conf->get('HOSTNAME');
37
43
 
38
44
# Add items to the start of a comma-separated list, and remove the
39
45
# items from later in the list if they were already in the list.
60
66
    return $ret;
61
67
}
62
68
 
63
 
our $bootstrap_arch = $arch;
 
69
sub makedir ($$) {
 
70
    my $dir = shift;
 
71
    my $perms = shift;
 
72
 
 
73
    mkpath($dir,
 
74
           { mode => $perms,
 
75
             verbose => 1,
 
76
             error => \my $error
 
77
           });
 
78
 
 
79
    for my $diag (@$error) {
 
80
        my ($file, $message) = each %$diag;
 
81
        print "E: Can't make directory $file: $message\n";
 
82
    }
 
83
}
 
84
 
 
85
our $bootstrap_arch = $conf->get('HOST_ARCH');
64
86
our $foreign = 0;
65
87
our $include = '';
66
88
our $exclude = '';
158
180
# Write out schroot chroot configuration.
159
181
my $chrootname = "${suite}-${bootstrap_arch}-sbuild";
160
182
 
161
 
# TODO: Don't hardcode path
162
 
my $SCHROOT_CONF =
163
 
    new File::Temp( TEMPLATE => "$chrootname.XXXXXX",
164
 
                    DIR => "/etc/schroot/chroot.d",
165
 
                    UNLINK => 0)
166
 
    or die "Can't open schroot configuration file: $!\n";
 
183
if (-d "/etc/schroot/chroot.d") {
 
184
    # TODO: Don't hardcode path
 
185
    my $SCHROOT_CONF =
 
186
        new File::Temp( TEMPLATE => "$chrootname.XXXXXX",
 
187
                        DIR => "/etc/schroot/chroot.d",
 
188
                        UNLINK => 0)
 
189
        or die "Can't open schroot configuration file: $!\n";
167
190
 
168
 
print $SCHROOT_CONF <<"EOF";
 
191
    print $SCHROOT_CONF <<"EOF";
169
192
[$chrootname]
170
193
type=directory
171
194
description=Debian $suite/$bootstrap_arch autobuilder
183
206
print "I: schroot chroot configuration written to $SCHROOT_CONF.\n";
184
207
dump_file("$SCHROOT_CONF");
185
208
print "I: Please rename and modify this file as required.\n";
 
209
}
 
210
 
 
211
if (! -d "$Sbuild::Sysconfig::paths{'SBUILD_SYSCONF_DIR'}/chroot") {
 
212
    makedir("$Sbuild::Sysconfig::paths{'SBUILD_SYSCONF_DIR'}/chroot", 0775);
 
213
}
 
214
 
 
215
my $chrootlink = "$Sbuild::Sysconfig::paths{'SBUILD_SYSCONF_DIR'}/chroot/$chrootname";
 
216
if (-l $chrootlink) {
 
217
    unlink($chrootlink) or die "Can't unlink $chrootlink: $!";
 
218
} else {
 
219
    print STDERR "E: Can't remove $chrootlink: not a symbolic link\n";
 
220
}
 
221
 
 
222
if (! -f $chrootlink) {
 
223
    if (symlink($target, $chrootlink)) {
 
224
        print "I: sudo chroot configuration linked as $Sbuild::Sysconfig::paths{'SBUILD_SYSCONF_DIR'}/chroot/$chrootname.\n";
 
225
    } else {
 
226
        print STDERR "E: Failed to symlink $target to $chrootlink: $!\n";
 
227
    }
 
228
} else {
 
229
    print "W: Failed to symlink $target to $chrootlink: \n"
 
230
}
 
231
 
 
232
if (! -d "$target/build") {
 
233
    print "I: Creating build directory: /build\n";
 
234
    makedir("$target/build", 0775);
 
235
}
 
236
!system("chown", "root:sbuild", "$target/build")
 
237
    or die "E: Failed to set root:sbuild ownership on $target/build";
 
238
!system("chmod", "0775", "$target/build")
 
239
    or die "E: Failed to set 0755 permissions on $target/build";
 
240
 
 
241
if (! -d "$target/var/lib/sbuild") {
 
242
    print "I: Creating sbuild directory: /var/lib/sbuild\n";
 
243
    makedir("$target/var/lib/sbuild/srcdep", 02775);
 
244
}
 
245
if (! -d "$target/var/lib/sbuild/srcdep-lock") {
 
246
    print "I: Creating sbuild lock directory: /var/lib/sbuild/srcdep-lock\n";
 
247
    makedir("$target/var/lib/sbuild/srcdep-lock", 02775);
 
248
}
 
249
!system("chown", "-R", "root:sbuild", "$target/var/lib/sbuild")
 
250
    or die "E: Failed to set root:sbuild ownership on $target/var/lib/sbuild";
 
251
!system("chmod", "-R", "02775", "$target/var/lib/sbuild")
 
252
    or die "E: Failed to set 02755 permissions on $target/var/lib/sbuild";
 
253
 
 
254
my $session = Sbuild::ChrootPlain->new($conf, $target);
 
255
if (defined($session)) {
 
256
    $session->set('Log Stream', \*STDOUT);
 
257
 
 
258
    if (!$session->begin_session()) {
 
259
        print STDERR "E: Error creating chroot session: skipping apt update\n";
 
260
    } else {
 
261
        print "I: Setting reference package list.\n";
 
262
        check_packages($session, "set");
 
263
 
 
264
        print "I: Updating chroot.\n";
 
265
        $session->run_apt_command(
 
266
            { COMMAND => [$conf->get('APT_GET'), 'update'],
 
267
              ENV => {'DEBIAN_FRONTEND' => 'noninteractive'},
 
268
              USER => 'root',
 
269
              PRIORITY => 1,
 
270
              DIR => '/'});
 
271
        print "W: Failed to update APT package lists\n"
 
272
            if ($?);
 
273
 
 
274
 
 
275
        $session->run_apt_command(
 
276
            { COMMAND => [$conf->get('APT_GET'), '-uy', 'upgrade'],
 
277
              ENV => {'DEBIAN_FRONTEND' => 'noninteractive'},
 
278
              USER => 'root',
 
279
              PRIORITY => 1,
 
280
              DIR => '/' });
 
281
        print "W: Failed to upgrade chroot\n"
 
282
            if ($?);
 
283
        $session->end_session();
 
284
        $session = undef;
 
285
    }
 
286
}
 
287
 
186
288
print "I: Successfully set up $suite chroot.\n";
187
289
print "I: Run sbuild-adduser to add new sbuild users.\n";
188
290