aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Iurt/Chroot.pm
blob: 77a146371944676411e58cd41b6c0d736a8c3bb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package Iurt::Chroot;

use strict;
use base qw(Exporter);
use MDV::Distribconf::Build;
use MDK::Common;
use Iurt::Process qw(clean sudo);
use Iurt::Config qw(dump_cache_par);
use Iurt::Util qw(plog);
use File::Temp 'mktemp';
use File::Path 'mkpath';

our @EXPORT = qw(
    clean_chroot_tmp
    clean_all_chroot_tmp
    clean_chroot
    update_chroot
    dump_rpmmacros
    add_local_user
    create_temp_chroot
    remove_chroot
    create_chroot
    create_build_chroot
    check_chroot
    check_build_chroot
);
    
my $sudo = '/usr/bin/sudo';

=head2 clean_chroot($chroot, $run, $only_clean)

Create or clean a chroot
I<$chroot> chroot path
I<$run> is the running environment
I<%only_clean> only clean the chroot, do not create a new one
Return true.

=cut

sub clean_chroot {
    my ($chroot, $chroot_tar, $run, $config, $o_only_clean, $o_only_tar) = @_;

    plog('DEBUG', "clean chroot");
    if (-d $chroot && !$o_only_tar) {
        sudo($run, $config, "--umount", "$chroot/proc");
        sudo($run, $config, "--umount", "$chroot/dev/pts");
	if ($run->{icecream}) {
            sudo($run, $config, "--umount", "$chroot/var/cache/icecream");
	}
	if (-d "$chroot/urpmi_medias/") {
            sudo($run, $config, "--umount", "$chroot/urpmi_medias");
	}

	# Do not run rm if there is something still mounted there
	open(my $FP, "/proc/mounts") or die $!;
	my @list = grep { /$chroot/ } <$FP>;
	close($FP);
	if ($#list >= 0) {
	    # Still referenced
	    return 1;
	}

	sudo($run, $config, '--rm', '-r', $chroot);
    }

    return 1 if $o_only_clean;

    mkdir $chroot;

    # various integrity checking
    if ($o_only_tar
	&& -f "$chroot/home/builder/.rpmmacros"
	&& -d "$chroot/home/builder"
	&& -d "$chroot/proc") {
	return 1;
    }
 
    sudo($run, $config, '--untar', $chroot_tar, $chroot);
    if (!create_build_chroot($chroot, $chroot_tar, $run, $config)) {
	plog('ERROR', "Failed to create chroot");
        return;
    }

    if (!dump_rpmmacros($run, $config, "$chroot/home/builder/.rpmmacros")) {
	plog('ERROR', "Failed to dump macros");
	return;
    }
    if (!sudo($run, $config, '--bindmount', "/proc", "$chroot/proc")) {
	plog('ERROR', "Failed to mount proc");
	return;
    }
    if (!sudo($run, $config, '--bindmount', "/dev/pts", "$chroot/dev/pts")) {
	plog('ERROR', "Failed to mount dev/pts");
	sudo($run, $config, "--umount", "$chroot/proc");
	return;
    }
    if ($run->{icecream}) {
	system("$sudo mkdir -p $chroot/var/cache/icecream");
	if (!sudo($run, $config, '--bindmount', "/var/cache/icecream", "$chroot/var/cache/icecream")) {
	    plog('ERROR', "Failed to mount var/cache/icecream");
	    sudo($run, $config, "--umount", "$chroot/proc");
	    sudo($run, $config, "--umount", "$chroot/dev/pts");
	    return;
	}
    }

    if ($run->{additional_media} && $run->{additional_media}{repository}) {
	my $rep = $run->{additional_media}{repository};
	if ($rep !~ m/^(http:|ftp:)/) {
	    my $mount_point = "$chroot/urpmi_medias";
	    my $url = $rep;
	    $url =~ s!^file://!!;
	    sudo($run, $config, '--mkdir', '-p', $mount_point);
	    if (!sudo($run, $config, '--bindmount', $url, $mount_point)) {
		plog('ERROR', "Failed to mount $url on $mount_point");
		sudo($run, $config, "--umount", "$chroot/proc");
		sudo($run, $config, "--umount", "$chroot/dev/pts");
		if ($run->{icecream}) {
		    sudo($run, $config, "--umount", "$chroot/var/cache/icecream");
		}
		return;
	    }
	}
    }
    1;
}  

=head2 update_chroot($chroot, $run, $only_clean)

Updates chroot
I<$chroot> chroot path
I<$run> is the running environment
I<%only_clean> only clean the chroot, do not create a new one
Return true.

=cut

sub update_chroot {
    my ($_chroot, $_chroot_tar, $_run, $_config, $_only_clean, $_only_tar) = @_;

    plog('DEBUG', "update chroot");

#    my $urpmi = $run->{urpmi};
#    $urpmi->auto_select($chroot);

}

sub dump_rpmmacros {
    my ($run, $config, $file) = @_;
    my $f;

    #plog("adding rpmmacros to $file");

    my $tmpfile = "/tmp/rpmmacros";
    if (!open $f, ">$tmpfile") {
	plog("ERROR: could not open $tmpfile ($!)");
	return 0;
    }
    my $packager = $run->{packager} || $config->{packager};

    print $f qq(\%_topdir                \%(echo \$HOME)/rpm
\%_tmppath               \%(echo \$HOME)/rpm/tmp/
\%distribution           $config->{distribution}
\%vendor                 $config->{vendor}
\%packager               $packager
);
    print $f join "\n", @{$run->{rpmmacros}} if defined $run->{rpmmacros};
    close $f;

    my $ret = sudo($run, $config, '--cp', $tmpfile, $file);
    unlink $tmpfile;

    if (!$ret) {
	plog("ERROR: could not write $file ($!)");
	return 0;
    }

    1;
}

sub add_local_user {
    my ($chroot_tmp, $run, $config, $luser, $uid) = @_;
    my $program_name = $run->{program_name};

    # change the builder user to the local user id
    # FIXME it seems that unionfs does not handle well the change of the
    #       uid of files
    # if (system(qq|sudo chroot $chroot_tmp usermod -u $run->{uid} builder|)) {
    
    if ($uid) {
	if (!sudo($run, $config, "--useradd", $chroot_tmp, $luser, $uid)) {
	    plog('ERROR', "ERROR: setting userid $uid to $luser in " .
		"$chroot_tmp failed, checking the chroot");
	    check_build_chroot($run->{chroot_path}, $run->{chroot_tar}, $run,
		$config) or return;
	}
    } else {
	# the program has been launch as root, setting the home to /home/root for compatibility
	system($sudo, 'chroot', $chroot_tmp, 'usermod', '-d', "/home/$luser", '-u', $uid, '-o', '-l', $luser, 'root');
    }

    dump_rpmmacros($run, $config, "$chroot_tmp/home/$luser/.rpmmacros") or return;

    1;
}

sub create_temp_chroot {
    my ($run, $config, $cache, $chroot_tmp, $chroot_tar, $o_srpm) = @_;

    my $home = $config->{local_home};
    my $debug_tag = $run->{debug_tag};

    plog("Install new chroot");
    plog('DEBUG', "... in $chroot_tmp");
    clean_chroot($chroot_tmp, $chroot_tar, $run, $config) or return;
    update_chroot($chroot_tmp, $run, $config);

    $chroot_tmp;
}

sub remove_chroot {
    my ($run, $dir, $func, $prefix) = @_;

    plog("Remove existing chroot");
    plog('DEBUG', "... dir $dir all $run->{clean_all} prefix $prefix");

    if ($run->{clean_all}) {
	opendir(my $chroot_dir, $dir);
	foreach (readdir $chroot_dir) {
	    next if !-d "$dir/$_" || /\.{1,2}/;
	    plog("cleaning old chroot for $_ in $dir");
	    $func->($run, "$dir/$_", $prefix);
	}
    } else {
	foreach my $user (@{$run->{clean}}) {
	    plog("cleaning old chroot for $user in $dir");
	    $func->($run, "$dir/$user", $prefix);
	}
    }
} 

sub clean_mnt {
    my ($run, $mount_point, $verbose) = @_;
    return clean($run, $mount_point, "/sbin/fuser", "$sudo /sbin/fuser -k", $verbose);
}

sub clean_all_chroot_tmp {
    my ($run, $chroot_dir, $prefix) = @_;

    plog(1, "cleaning all old chroot remaining dir in $chroot_dir");

    my $dir;
    if (!opendir $dir, $chroot_dir) { 
	plog("ERROR: can't open $chroot_dir ($!)");
	return;
    }
    foreach (readdir($dir)) {
	/$prefix/ or next;
	clean_chroot_tmp($run, $chroot_dir, $_);
    }
    closedir $dir;
}

sub clean_chroot_tmp {
    my ($run, $chroot_dir, $dir) = @_;
    my $d = "$chroot_dir/$dir";

    foreach my $m ('proc', 'dev/pts', 'urpmi_medias', 'var/cache/icecream') {
	if (system("$sudo umount $d/$m &>/dev/null") && $run->{verbose} > 1) { 
	    plog("ERROR: could not umount /$m in $d/");
	    # FIXME: <mrl> We can't go on, otherelse we will remove something
	    # that we shouldn't. But for that, we should:
	    #  a) Check for all mount-points inside the chroot
	    #  b) Try to unmount only the needed ones, otherelse the errors
	    # can be misleading.
	}
    }

    plog(1, "cleaning $d");
    system("$sudo /sbin/fuser -k $d &> /dev/null");
    plog(1, "removing $d");
    system($sudo, 'rm', '-rf', $d);
}

sub check_mounted {
    my ($mount_point, $type) = @_;

    my $mount;
    if (!open $mount, '/proc/mounts') {
	plog("ERROR: could not open /proc/mounts");
	return;
    }
    $mount_point =~ s,//+,/,g;
    local $_;
    while (<$mount>) {
	return 1 if /^\w+ $mount_point $type /;
    }
    0;
}

sub create_build_chroot {
    my ($chroot, $chroot_tar, $run, $config) = @_;
    create_chroot($chroot, $chroot_tar, $run, $config,
			{ packages => $config->{basesystem_packages} });
}

sub create_chroot {
    my ($chroot, $chroot_tar, $run, $config, $opt) = @_;
    my $tmp_tar = mktemp("$chroot_tar.tmp.XXXXXX");
    my $tmp_chroot = mktemp("$chroot.tmp.XXXXXX");
    my $rebuild;
    my $clean = sub {
	plog("Remove temporary chroot tarball");
	sudo($run, $config, '--rm', '-r', $tmp_chroot, $tmp_tar);
    };

    plog('NOTIFY', "creating chroot");
    plog('DEBUG', "... with packages " . join(', ', @{$opt->{packages}}));

    mkdir_p($tmp_chroot);
    if (!-f $chroot_tar) {
        plog("rebuild chroot tarball");
        $rebuild = 1;
    } else {
        link $chroot_tar, $tmp_tar or die "FATAL: could not initialize chroot ($!)\n";

        plog('DEBUG', "decompressing /var/log/qa from $chroot_tar in $tmp_chroot");
        sudo($run, $config, '--untar', $chroot_tar, $tmp_chroot, "./var/log/qa");

        my $tmp_urpmi = mktemp("$chroot.tmp.XXXXXX");
        my @installed_pkgs = grep(!/^gpg-pubkey/, chomp_(cat_("$tmp_chroot/var/log/qa")));
        my @available_pkgs = chomp_(`urpmq --urpmi-root $tmp_urpmi --use-distrib $run->{urpmi}{distrib_url} --list -f 2>/dev/null`);
        my @removed_pkgs = difference2(\@installed_pkgs, \@available_pkgs);
        rm_rf($tmp_urpmi);

        if (@installed_pkgs) {
            if (@removed_pkgs) {
                plog('DEBUG', "changed packages: @removed_pkgs");
                plog('NOTIFY', "Rebuilding chroot tarball");
                $rebuild = 1;
            } else {
                plog('NOTIFY', "chroot tarball is already up-to-date");
                link $tmp_tar, $chroot_tar;
            }
        } else {
            plog('DEBUG', "can't open $tmp_chroot/var/log/qa");
            plog('ERROR', "can't check chroot, recreating");
            $rebuild = 1;
        }
    }

    if ($rebuild) {
	    if (!build_chroot($run, $config, $tmp_chroot, $chroot_tar, $opt)) {
		plog('NOTIFY', "creating chroot failed.");
		$clean->();
		sudo($run, $config, '--rm', '-r', $chroot, $chroot_tar);
		return;
	    }
    }

    if (!-d $chroot || $rebuild) {
	plog('DEBUG', "recreate chroot $chroot");
	plog('NOTIFY', "recreate chroot");
	my $urpmi = $run->{urpmi};
	$urpmi->clean_urpmi_process($chroot);
	sudo($run, $config, '--rm', '-r', $chroot, $tmp_tar);
	mkdir_p $chroot;
	sudo($run, $config, '--untar', $chroot_tar, $chroot);
	plog('NOTIFY', "chroot recreated in $chroot_tar (live in $chroot)");
    }
    
    $clean->();

    1;
}

sub build_chroot {
    my ($run, $config, $tmp_chroot, $chroot_tar, $opt) = @_;

    plog('DEBUG', "building the chroot with "
			. join(', ', @{$opt->{packages}}));

    sudo($run, $config, "--mkdir", "-p", "$tmp_chroot/dev/pts",
		"$tmp_chroot/etc/sysconfig", "$tmp_chroot/proc",
	        "$tmp_chroot/var/lib/rpm");

    #system(qq($sudo sh -c "echo 127.0.0.1 localhost > $tmp_chroot/etc/hosts"));
    # warly some program perform a gethostbyname(hostname) and in the cluster the 
    # name are not resolved via DNS but via /etc/hosts
    sudo($run, $config, '--cp', "/etc/hosts", "$tmp_chroot/etc/");
    sudo($run, $config, '--cp', "/etc/resolv.conf", "$tmp_chroot/etc/");

    # install chroot
    my $urpmi = $run->{urpmi};

    if ($urpmi->{use__urpmi_root}) {
	if (!$urpmi->add_media__urpmi_root($tmp_chroot, $config->{base_media})) {
	    plog('ERROR', "urpmi.addmedia --urpmi-root failed");
	    return 0;
	}
    }
    $urpmi->set_command($tmp_chroot);

    # (blino) install meta-task first for prefer.vendor.list to be used
    foreach my $packages ([ 'meta-task' ], $opt->{packages}) {
        if (!$urpmi->install_packages(
            "chroot",
            $tmp_chroot,
            $run->{local_spool},
            {},
            'initialize',
            "[ADMIN] creation of initial chroot failed on $run->{my_arch}",
            { maintainer => $config->{admin} },
            @$packages
        )) {
            plog('ERROR', "Failed to install initial packages during chroot creation.");
            return 0;
        }
    }

    # <mrl> URPMI saying ok or not, we check this anyway. So that's why
    # it's outside the else.
    if (! -f "$tmp_chroot/usr/bin/rpmbuild") {
	plog(1, "ERROR: rpm-build is missing!");
	return 0;
    }

    # remove files used by --urpmi-root
    sudo($run, $config, "--rm", "$tmp_chroot/etc/urpmi/urpmi.cfg");
    sudo($run, $config, "--rm", "$tmp_chroot/var/lib/urpmi/*");

    system("rpm -qa --root $tmp_chroot --qf '\%{NAME}-\%{VERSION}-\%{RELEASE}.\%{ARCH}\n' | sort > $tmp_chroot/tmp/qa");
    sudo($run, $config, "--cp", "$tmp_chroot/tmp/qa", "$tmp_chroot/var/log/qa");
    unlink("$tmp_chroot/tmp/qa");

    sudo($run, $config, "--mkdir", "$tmp_chroot/etc/skel/rpm/$_")
      foreach "", qw(RPMS BUILD SPECS SRPMS SOURCES tmp);

    #
    # CM: Choose a sub-500 uid to prevent collison with $luser
    #
    sudo($run, $config, "--useradd", $tmp_chroot, 'builder', 499);

    # FIXME: <mrl> Be careful! Damn ugly hack right below!
    sudo($run, $config, "--rm", "$tmp_chroot/var/lib/rpm/__db*");
    sudo($run, $config, "--umount", "$tmp_chroot/proc");
    sudo($run, $config, "--umount", "$tmp_chroot/dev/pts");
    if ($run->{icecream}) {
	sudo($run, $config, "--umount", "$tmp_chroot/var/cache/icecream");
    }
    if (-d "$tmp_chroot/urpmi_medias/") {
	sudo($run, $config, "--umount", "$tmp_chroot/urpmi_medias");
    }
    return sudo($run, $config, "--tar", $chroot_tar, $tmp_chroot);
}

sub check_build_chroot {
    my ($chroot, $chroot_tar, $run, $config) = @_;

    check_chroot($chroot, $chroot_tar, $run, $config,
		{ packages => $config->{basesystem_packages} });
}

sub check_chroot {
    my ($chroot, $chroot_tar, $run, $config, $opt) = @_;

    plog('DEBUG', "checking basesystem tar");
    
    my (@stat) = stat $chroot_tar;

    if (time -$stat[9] > 604800) {
	plog('WARN', "chroot tarball too old, force rebuild");
	sudo($run, $config, '--rm', '-r', $chroot, $chroot_tar);
    }
    create_chroot($chroot, $chroot_tar, $run, $config, $opt);
}

1;