2018-09-18 09:20:24 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# Copyright: 2018 Johannes Schauer <josch@mister-muffin.de>
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to
|
|
|
|
# deal in the Software without restriction, including without limitation the
|
|
|
|
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
# sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use English;
|
|
|
|
use Getopt::Long;
|
|
|
|
use Pod::Usage;
|
|
|
|
use File::Copy;
|
|
|
|
use File::Path qw(make_path remove_tree);
|
|
|
|
use File::Temp qw(tempfile tempdir);
|
|
|
|
use Cwd qw(abs_path);
|
|
|
|
require "syscall.ph";
|
2018-09-23 17:47:14 +00:00
|
|
|
use Fcntl qw(S_IFCHR S_IFBLK FD_CLOEXEC F_GETFD F_SETFD);
|
2018-09-23 17:36:07 +00:00
|
|
|
use List::Util qw(any none);
|
2018-09-18 09:20:24 +00:00
|
|
|
|
|
|
|
# from sched.h
|
|
|
|
use constant {
|
|
|
|
CLONE_NEWNS => 0x20000,
|
|
|
|
CLONE_NEWUTS => 0x4000000,
|
|
|
|
CLONE_NEWIPC => 0x8000000,
|
|
|
|
CLONE_NEWUSER => 0x10000000,
|
|
|
|
CLONE_NEWPID => 0x20000000,
|
|
|
|
CLONE_NEWNET => 0x40000000,
|
|
|
|
};
|
|
|
|
|
|
|
|
# type codes:
|
|
|
|
# 0 -> normal file
|
|
|
|
# 1 -> hardlink
|
|
|
|
# 2 -> symlink
|
|
|
|
# 3 -> character special
|
|
|
|
# 4 -> block special
|
|
|
|
# 5 -> directory
|
|
|
|
my @devfiles = (
|
|
|
|
# filename mode type link target major minor
|
|
|
|
[ "./dev/", 0755, 5, '', undef, undef ],
|
|
|
|
[ "./dev/console", 0666, 3, '', 5, 1 ],
|
|
|
|
[ "./dev/fd", 0777, 2, '/proc/self/fd', undef, undef ],
|
|
|
|
[ "./dev/full", 0666, 3, '', 1, 7 ],
|
|
|
|
[ "./dev/null", 0666, 3, '', 1, 3 ],
|
|
|
|
[ "./dev/ptmx", 0666, 3, '', 5, 2 ],
|
|
|
|
[ "./dev/pts/", 0755, 5, '', undef, undef ],
|
|
|
|
[ "./dev/random", 0666, 3, '', 1, 8 ],
|
|
|
|
[ "./dev/shm/", 0755, 5, '', undef, undef ],
|
|
|
|
[ "./dev/stderr", 0777, 2, '/proc/self/fd/2', undef, undef ],
|
|
|
|
[ "./dev/stdin", 0777, 2, '/proc/self/fd/0', undef, undef ],
|
|
|
|
[ "./dev/stdout", 0777, 2, '/proc/self/fd/1', undef, undef ],
|
|
|
|
[ "./dev/tty", 0666, 3, '', 5, 0 ],
|
|
|
|
[ "./dev/urandom", 0666, 3, '', 1, 9 ],
|
|
|
|
[ "./dev/zero", 0666, 3, '', 1, 5 ],
|
|
|
|
);
|
|
|
|
|
|
|
|
# tar cannot figure out the decompression program when receiving data on
|
|
|
|
# standard input, thus we do it ourselves. This is copied from tar's
|
|
|
|
# src/suffix.c
|
|
|
|
sub get_tar_compress_options($) {
|
|
|
|
my $filename = shift;
|
|
|
|
if ($filename =~ /\.(gz|tgz|taz)$/) {
|
|
|
|
return ('--gzip');
|
|
|
|
} elsif ($filename =~ /\.(Z|taZ)$/) {
|
|
|
|
return ('--compress');
|
|
|
|
} elsif ($filename =~ /\.(bz2|tbz|tbz2|tz2)$/) {
|
|
|
|
return ('--bzip2');
|
|
|
|
} elsif ($filename =~ /\.lz$/) {
|
|
|
|
return ('--lzip');
|
|
|
|
} elsif ($filename =~ /\.(lzma|tlz)$/) {
|
|
|
|
return ('--lzma');
|
|
|
|
} elsif ($filename =~ /\.lzo$/) {
|
|
|
|
return ('--lzop');
|
|
|
|
} elsif ($filename =~ /\.lz4$/) {
|
|
|
|
return ('--use-compress-program', 'lz4');
|
|
|
|
} elsif ($filename =~ /\.(xz|txz)$/) {
|
|
|
|
return ('--xz');
|
|
|
|
}
|
|
|
|
return ();
|
|
|
|
}
|
|
|
|
|
|
|
|
sub test_unshare() {
|
2018-09-24 18:07:46 +00:00
|
|
|
if ($EFFECTIVE_USER_ID == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-18 09:20:24 +00:00
|
|
|
# arguments to syscalls have to be stored in their own variable or
|
|
|
|
# otherwise we will get "Modification of a read-only value attempted"
|
|
|
|
my $unshare_flags = CLONE_NEWUSER;
|
|
|
|
# we spawn a new per process because if unshare succeeds, we would
|
|
|
|
# otherwise have unshared the sbuild process itself which we don't want
|
|
|
|
my $pid = fork() // die "fork() failed: $!";
|
|
|
|
if ($pid == 0) {
|
|
|
|
my $ret = syscall &SYS_unshare, $unshare_flags;
|
|
|
|
if (($ret >> 8) == 0) {
|
|
|
|
exit 0;
|
|
|
|
} else {
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
waitpid($pid, 0);
|
|
|
|
if (($? >> 8) != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub read_subuid_subgid() {
|
|
|
|
my $username = getpwuid $<;
|
|
|
|
my ($subid, $num_subid, $fh, $n);
|
|
|
|
my @result = ();
|
|
|
|
|
|
|
|
if (! -e "/etc/subuid") {
|
|
|
|
printf STDERR "/etc/subuid doesn't exist\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (! -r "/etc/subuid") {
|
|
|
|
printf STDERR "/etc/subuid is not readable\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
open $fh, "<", "/etc/subuid" or die "cannot open /etc/subuid for reading: $!";
|
|
|
|
while (my $line = <$fh>) {
|
|
|
|
($n, $subid, $num_subid) = split(/:/, $line, 3);
|
|
|
|
last if ($n eq $username);
|
|
|
|
}
|
|
|
|
close $fh;
|
|
|
|
push @result, ["u", 0, $subid, $num_subid];
|
|
|
|
|
|
|
|
if (scalar(@result) < 1) {
|
|
|
|
printf STDERR "/etc/subuid does not contain an entry for $username\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (scalar(@result) > 1) {
|
|
|
|
printf STDERR "/etc/subuid contains multiple entries for $username\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
open $fh, "<", "/etc/subgid" or die "cannot open /etc/subgid for reading: $!";
|
|
|
|
while (my $line = <$fh>) {
|
|
|
|
($n, $subid, $num_subid) = split(/:/, $line, 3);
|
|
|
|
last if ($n eq $username);
|
|
|
|
}
|
|
|
|
close $fh;
|
|
|
|
push @result, ["g", 0, $subid, $num_subid];
|
|
|
|
|
|
|
|
if (scalar(@result) < 2) {
|
|
|
|
printf STDERR "/etc/subgid does not contain an entry for $username\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (scalar(@result) > 2) {
|
|
|
|
printf STDERR "/etc/subgid contains multiple entries for $username\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return @result;
|
|
|
|
}
|
|
|
|
|
|
|
|
# This function spawns two child processes forming the following process tree
|
|
|
|
#
|
|
|
|
# A
|
|
|
|
# |
|
|
|
|
# fork()
|
|
|
|
# | \
|
|
|
|
# B C
|
|
|
|
# | |
|
|
|
|
# | fork()
|
|
|
|
# | | \
|
|
|
|
# | D E
|
|
|
|
# | | |
|
|
|
|
# |unshare()
|
|
|
|
# | close()
|
|
|
|
# | | |
|
|
|
|
# | | read()
|
|
|
|
# | | newuidmap(D)
|
|
|
|
# | | newgidmap(D)
|
|
|
|
# | | /
|
|
|
|
# | waitpid()
|
|
|
|
# | |
|
|
|
|
# | fork()
|
|
|
|
# | | \
|
|
|
|
# | F G
|
|
|
|
# | | |
|
|
|
|
# | | exec()
|
|
|
|
# | | /
|
|
|
|
# | waitpid()
|
|
|
|
# | /
|
|
|
|
# waitpid()
|
|
|
|
#
|
|
|
|
# To better refer to each individual part, we give each process a new
|
|
|
|
# identifier after calling fork(). Process A is the main process. After
|
|
|
|
# executing fork() we call the parent and child B and C, respectively. This
|
|
|
|
# first fork() is done because we do not want to modify A. B then remains
|
|
|
|
# waiting for its child C to finish. C calls fork() again, splitting into
|
|
|
|
# the parent D and its child E. In the parent D we call unshare() and close a
|
|
|
|
# pipe shared by D and E to signal to E that D is done with calling unshare().
|
|
|
|
# E notices this by using read() and follows up with executing the tools
|
|
|
|
# new[ug]idmap on D. E finishes and D continues with doing another fork().
|
|
|
|
# This is because when unsharing the PID namespace, we need a PID 1 to be kept
|
|
|
|
# alive or otherwise any child processes cannot fork() anymore themselves. So
|
|
|
|
# we keep F as PID 1 and finally call exec() in G.
|
|
|
|
sub get_unshare_cmd(&$) {
|
|
|
|
my $cmd = shift;
|
|
|
|
my $idmap = shift;
|
|
|
|
|
|
|
|
my $unshare_flags = CLONE_NEWUSER | CLONE_NEWNS | CLONE_NEWPID | CLONE_NEWUTS | CLONE_NEWIPC;
|
|
|
|
|
|
|
|
if (0) {
|
|
|
|
$unshare_flags |= CLONE_NEWNET;
|
|
|
|
}
|
|
|
|
|
|
|
|
# fork a new process and let the child get unshare()ed
|
|
|
|
# we don't want to unshare the parent process
|
|
|
|
my $gcpid = fork() // die "fork() failed: $!";
|
|
|
|
if ($gcpid == 0) {
|
|
|
|
# Create a pipe for the parent process to signal the child process that it is
|
|
|
|
# done with calling unshare() so that the child can go ahead setting up
|
|
|
|
# uid_map and gid_map.
|
|
|
|
pipe my $rfh, my $wfh;
|
|
|
|
|
|
|
|
# We have to do this dance with forking a process and then modifying the
|
|
|
|
# parent from the child because:
|
|
|
|
# - new[ug]idmap can only be called on a process id after that process has
|
|
|
|
# unshared the user namespace
|
|
|
|
# - a process looses its capabilities if it performs an execve() with nonzero
|
|
|
|
# user ids see the capabilities(7) man page for details.
|
|
|
|
# - a process that unshared the user namespace by default does not have the
|
|
|
|
# privileges to call new[ug]idmap on itself
|
|
|
|
#
|
|
|
|
# this also works the other way around (the child setting up a user namespace
|
|
|
|
# and being modified from the parent) but that way, the parent would have to
|
|
|
|
# stay around until the child exited (so a pid would be wasted). Additionally,
|
|
|
|
# that variant would require an additional pipe to let the parent signal the
|
|
|
|
# child that it is done with calling new[ug]idmap. The way it is done here,
|
|
|
|
# this signaling can instead be done by wait()-ing for the exit of the child.
|
|
|
|
my $ppid = $$;
|
|
|
|
my $cpid = fork() // die "fork() failed: $!";
|
|
|
|
if ($cpid == 0) {
|
|
|
|
# child
|
|
|
|
|
|
|
|
# Close the writing descriptor at our end of the pipe so that we
|
|
|
|
# see EOF when parent closes its descriptor.
|
|
|
|
close $wfh;
|
|
|
|
|
|
|
|
# Wait for the parent process to finish its unshare() call by
|
|
|
|
# waiting for an EOF.
|
|
|
|
0 == sysread $rfh, my $c, 1 or die "read() did not receive EOF";
|
|
|
|
|
|
|
|
# The program's new[ug]idmap have to be used because they are
|
|
|
|
# setuid root. These privileges are needed to map the ids from
|
|
|
|
# /etc/sub[ug]id to the user namespace set up by the parent.
|
|
|
|
# Without these privileges, only the id of the user itself can be
|
|
|
|
# mapped into the new namespace.
|
|
|
|
#
|
|
|
|
# Since new[ug]idmap is setuid root we also don't need to write
|
|
|
|
# "deny" to /proc/$$/setgroups beforehand (this is otherwise
|
|
|
|
# required for unprivileged processes trying to write to
|
|
|
|
# /proc/$$/gid_map since kernel version 3.19 for security reasons)
|
|
|
|
# and therefore the parent process keeps its ability to change its
|
|
|
|
# own group here.
|
|
|
|
#
|
|
|
|
# Since /proc/$ppid/[ug]id_map can only be written to once,
|
|
|
|
# respectively, instead of making multiple calls to new[ug]idmap,
|
|
|
|
# we assemble a command line that makes one call each.
|
|
|
|
my $uidmapcmd = "";
|
|
|
|
my $gidmapcmd = "";
|
|
|
|
foreach (@{$idmap}) {
|
|
|
|
my ($t, $hostid, $nsid, $range) = @{$_};
|
|
|
|
if ($t ne "u" and $t ne "g" and $t ne "b") {
|
|
|
|
die "invalid idmap type: $t";
|
|
|
|
}
|
|
|
|
if ($t eq "u" or $t eq "b") {
|
|
|
|
$uidmapcmd .= " $hostid $nsid $range";
|
|
|
|
}
|
|
|
|
if ($t eq "g" or $t eq "b") {
|
|
|
|
$gidmapcmd .= " $hostid $nsid $range";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
my $idmapcmd = '';
|
|
|
|
if ($uidmapcmd ne "") {
|
|
|
|
0 == system "newuidmap $ppid $uidmapcmd" or die "newuidmap failed: $!";
|
|
|
|
}
|
|
|
|
if ($gidmapcmd ne "") {
|
|
|
|
0 == system "newgidmap $ppid $gidmapcmd" or die "newgidmap failed: $!";
|
|
|
|
}
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
# parent
|
|
|
|
|
|
|
|
# After fork()-ing, the parent immediately calls unshare...
|
|
|
|
0 == syscall &SYS_unshare, $unshare_flags or die "unshare() failed: $!";
|
|
|
|
|
|
|
|
# .. and then signals the child process that we are done with the
|
|
|
|
# unshare() call by sending an EOF.
|
|
|
|
close $wfh;
|
|
|
|
|
|
|
|
# Wait for the child process to finish its setup by waiting for its
|
|
|
|
# exit.
|
|
|
|
$cpid == waitpid $cpid, 0 or die "waitpid() failed: $!";
|
|
|
|
my $exit = $? >> 8;
|
|
|
|
if ($exit != 0) {
|
|
|
|
die "child had a non-zero exit status: $exit";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Currently we are nobody (uid and gid are 65534). So we become root
|
|
|
|
# user and group instead.
|
|
|
|
#
|
|
|
|
# We are using direct syscalls instead of setting $(, $), $< and $>
|
|
|
|
# because then perl would do additional stuff which we don't need or
|
|
|
|
# want here, like checking /proc/sys/kernel/ngroups_max (which might
|
|
|
|
# not exist). It would also also call setgroups() in a way that makes
|
|
|
|
# the root user be part of the group unknown.
|
|
|
|
0 == syscall &SYS_setgid, 0 or die "setgid failed: $!";
|
|
|
|
0 == syscall &SYS_setuid, 0 or die "setuid failed: $!";
|
|
|
|
0 == syscall &SYS_setgroups, 0, 0 or die "setgroups failed: $!";
|
|
|
|
|
|
|
|
if (1) {
|
|
|
|
# When the pid namespace is also unshared, then processes expect a
|
|
|
|
# master pid to always be alive within the namespace. To achieve
|
|
|
|
# this, we fork() here instead of exec() to always have one dummy
|
|
|
|
# process running as pid 1 inside the namespace. This is also what
|
|
|
|
# the unshare tool does when used with the --fork option.
|
|
|
|
#
|
|
|
|
# Otherwise, without a pid 1, new processes cannot be forked
|
|
|
|
# anymore after pid 1 finished.
|
|
|
|
my $cpid = fork() // die "fork() failed: $!";
|
|
|
|
if ($cpid != 0) {
|
|
|
|
# The parent process will stay alive as pid 1 in this
|
|
|
|
# namespace until the child finishes executing. This is
|
|
|
|
# important because pid 1 must never die or otherwise nothing
|
|
|
|
# new can be forked.
|
|
|
|
$cpid == waitpid $cpid, 0 or die "waitpid() failed: $!";
|
|
|
|
exit ($? >> 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
&{$cmd}();
|
|
|
|
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
# parent
|
|
|
|
return $gcpid;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub havemknod($) {
|
|
|
|
my $root = shift;
|
|
|
|
my $havemknod = 0;
|
|
|
|
if (-e "$root/test-dev-null") {
|
|
|
|
die "/test-dev-null already exists";
|
|
|
|
}
|
|
|
|
while ($havemknod == 0) {
|
2018-09-23 13:26:47 +00:00
|
|
|
# we fork so that we can read STDERR
|
|
|
|
my $pid = open my $fh, '-|' // die "failed to fork(): $!";
|
|
|
|
if ($pid == 0) {
|
|
|
|
open(STDERR, '>&', STDOUT);
|
|
|
|
# we use mknod(1) instead of the system call because creating the
|
|
|
|
# right dev_t argument requires makedev(3)
|
|
|
|
exec 'mknod', "$root/test-dev-null", 'c', '1', '3';
|
|
|
|
}
|
|
|
|
chomp (my $content = do { local $/; <$fh> });
|
|
|
|
close $fh;
|
|
|
|
{
|
|
|
|
last unless $? == 0 and $content eq '';
|
|
|
|
last unless -c "$root/test-dev-null";
|
|
|
|
last unless open my $fh, '>', "$root/test-dev-null";
|
|
|
|
last unless print $fh 'test';
|
|
|
|
}
|
2018-09-18 09:20:24 +00:00
|
|
|
$havemknod = 1;
|
|
|
|
}
|
|
|
|
if (-e "$root/test-dev-null") {
|
|
|
|
unlink "$root/test-dev-null" or die "cannot unlink /test-dev-null";
|
|
|
|
}
|
|
|
|
return $havemknod;
|
|
|
|
}
|
|
|
|
|
2018-09-23 17:47:14 +00:00
|
|
|
sub print_progress {
|
|
|
|
my $perc = shift;
|
|
|
|
if (!-t STDERR) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($perc eq "done") {
|
|
|
|
# \e[2K clears everything on the current line (i.e. the progress bar)
|
|
|
|
print STDERR "\e[2Kdone\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($perc >= 100) {
|
|
|
|
$perc = 100;
|
|
|
|
}
|
|
|
|
my $width = 50;
|
|
|
|
my $num_x = int($perc*$width/100);
|
|
|
|
my $bar = '=' x $num_x;
|
|
|
|
if ($num_x != $width) {
|
|
|
|
$bar .= '>';
|
|
|
|
$bar .= ' ' x ($width - $num_x - 1);
|
|
|
|
}
|
|
|
|
printf STDERR "%6.2f [%s]\r", $perc, $bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub run_dpkg_progress {
|
|
|
|
my $debs = shift;
|
|
|
|
my @args = @_;
|
|
|
|
pipe my $rfh, my $wfh;
|
|
|
|
my $dpkgpid = open (my $pipe_dpkg, '-|') // die "failed to fork(): $!";
|
|
|
|
if ($dpkgpid == 0) {
|
|
|
|
close $rfh;
|
|
|
|
# Unset the close-on-exec flag, so that the file descriptor does not
|
|
|
|
# get closed when we exec dpkg
|
|
|
|
my $flags = fcntl( $wfh, F_GETFD, 0 ) or die "fcntl F_GETFD: $!";
|
|
|
|
fcntl($wfh, F_SETFD, $flags & ~FD_CLOEXEC ) or die "fcntl F_SETFD: $!";
|
|
|
|
my $fd = fileno $wfh;
|
|
|
|
# redirect dpkg's stderr to stdout so that we can capture it
|
|
|
|
open(STDERR, '>&', STDOUT);
|
|
|
|
exec { $args[0] } @args, "--status-fd=$fd", @{$debs};
|
|
|
|
die "cannot exec() dpkg";
|
|
|
|
}
|
|
|
|
close $wfh;
|
|
|
|
|
|
|
|
# spawn two processes:
|
|
|
|
# parent will parse stdout
|
|
|
|
# child will parse $rfh for the progress meter
|
|
|
|
my $pid = fork() // die "failed to fork(): $!";
|
|
|
|
if ($pid == 0) {
|
|
|
|
# child
|
|
|
|
print_progress 0.0;
|
|
|
|
my $num = 0;
|
|
|
|
# each package has one install and one configure step, thus the total
|
|
|
|
# number is twice the number of packages
|
|
|
|
my $total = (scalar @{$debs}) * 2;
|
|
|
|
while (my $line = <$rfh>) {
|
|
|
|
if ($line =~ /^processing: (install|configure): /) {
|
|
|
|
$num += 1;
|
|
|
|
}
|
|
|
|
my $perc = $num/$total*100;
|
|
|
|
print_progress $perc;
|
|
|
|
}
|
|
|
|
print_progress "done";
|
|
|
|
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
# parent
|
|
|
|
my $dpkg_output;
|
|
|
|
while (my $line = <$pipe_dpkg>) {
|
|
|
|
# forward captured apt output
|
|
|
|
#print STDERR $line;
|
|
|
|
$dpkg_output .= $line;
|
|
|
|
}
|
|
|
|
close $pipe_dpkg;
|
|
|
|
my $fail = 0;
|
|
|
|
if ($? != 0) {
|
|
|
|
$fail = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
waitpid $pid, 0;
|
|
|
|
$? == 0 or die "progress parsing failed";
|
|
|
|
|
|
|
|
# only print apt failure after progress output finished or otherwise it
|
|
|
|
# might interfere
|
|
|
|
if ($fail) {
|
|
|
|
print STDERR $dpkg_output;
|
|
|
|
die ((join ' ', @args) . ' failed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub run_apt_progress {
|
|
|
|
my @args = @_;
|
|
|
|
pipe my $rfh, my $wfh;
|
|
|
|
my $aptpid = open(my $pipe_apt, '-|') // die "failed to fork(): $!";
|
|
|
|
if ($aptpid == 0) {
|
|
|
|
close $rfh;
|
|
|
|
# Unset the close-on-exec flag, so that the file descriptor does not
|
|
|
|
# get closed when we exec apt-get
|
|
|
|
my $flags = fcntl( $wfh, F_GETFD, 0 ) or die "fcntl F_GETFD: $!";
|
|
|
|
fcntl($wfh, F_SETFD, $flags & ~FD_CLOEXEC ) or die "fcntl F_SETFD: $!";
|
|
|
|
my $fd = fileno $wfh;
|
|
|
|
# redirect apt's stderr to stdout so that we can capture it
|
|
|
|
open(STDERR, '>&', STDOUT);
|
|
|
|
exec { $args[0] } @args, "-oAPT::Status-Fd=$fd";
|
|
|
|
die "cannot exec apt-get update: $!";
|
|
|
|
}
|
|
|
|
close $wfh;
|
|
|
|
|
|
|
|
# spawn two processes:
|
|
|
|
# parent will parse stdout to look for errors
|
|
|
|
# child will parse $rfh for the progress meter
|
|
|
|
my $pid = fork() // die "failed to fork(): $!";
|
|
|
|
if ($pid == 0) {
|
|
|
|
# child
|
|
|
|
print_progress 0.0;
|
|
|
|
while (my $line = <$rfh>) {
|
|
|
|
if ($line =~ /(pmstatus|dlstatus):[^:]+:(\d+\.\d+):.*/) {
|
|
|
|
print_progress $2;
|
|
|
|
}
|
|
|
|
#print STDERR "apt: $line";
|
|
|
|
}
|
|
|
|
print_progress "done";
|
|
|
|
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
# parent
|
|
|
|
my $has_error = 0;
|
|
|
|
# apt-get doesn't report a non-zero exit if the update failed. Thus, we
|
|
|
|
# have to parse its output. See #778357, #776152, #696335 and #745735
|
|
|
|
my $apt_output = '';
|
|
|
|
while (my $line = <$pipe_apt>) {
|
|
|
|
if ($line =~ '^W: ') {
|
|
|
|
$has_error = 1;
|
|
|
|
} elsif ($line =~ '^Err:') {
|
|
|
|
$has_error = 1;
|
|
|
|
}
|
|
|
|
# forward captured apt output
|
|
|
|
#print STDERR $line;
|
|
|
|
$apt_output .= $line;
|
|
|
|
}
|
|
|
|
close($pipe_apt);
|
|
|
|
my $fail = 0;
|
|
|
|
if ($? != 0 or $has_error) {
|
|
|
|
$fail = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
waitpid $pid, 0;
|
|
|
|
$? == 0 or die "progress parsing failed";
|
|
|
|
|
|
|
|
# only print apt failure after progress output finished or otherwise it
|
|
|
|
# might interfere
|
|
|
|
if ($fail) {
|
|
|
|
print STDERR $apt_output;
|
|
|
|
die ((join ' ', @args) . ' failed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 09:20:24 +00:00
|
|
|
sub setup {
|
|
|
|
my $options = shift;
|
|
|
|
|
|
|
|
foreach my $key (sort keys %{$options}) {
|
|
|
|
my $value = $options->{$key};
|
|
|
|
if (!defined $value) {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if (ref $value eq '') {
|
|
|
|
print STDERR "I: $key: $options->{$key}\n";
|
|
|
|
} elsif (ref $value eq 'ARRAY') {
|
|
|
|
print STDERR "I: $key: [" . (join ', ', @{$value}) . "]\n";
|
|
|
|
} else {
|
|
|
|
die "unknown type";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($conf, $tmpfile) = tempfile(UNLINK => 1) or die "cannot open apt.conf: $!";
|
|
|
|
print $conf "Apt::Architecture \"$options->{nativearch}\";\n";
|
|
|
|
# the host system might have configured additional architectures
|
|
|
|
# force only the native architecture
|
|
|
|
if (scalar @{$options->{foreignarchs}} > 0) {
|
|
|
|
print $conf "Apt::Architectures \"$options->{nativearch} @{$options->{foreignarchs}}\";\n";
|
|
|
|
} else {
|
|
|
|
print $conf "Apt::Architectures \"$options->{nativearch}\";\n";
|
|
|
|
}
|
|
|
|
print $conf "Dir::Etc \"$options->{root}/etc/apt\";\n";
|
|
|
|
print $conf "Dir::State \"$options->{root}/var/lib/apt\";\n";
|
|
|
|
print $conf "Dir::Cache \"$options->{root}/var/cache/apt\";\n";
|
|
|
|
# for retrieving the essential packages set, only download
|
|
|
|
print $conf "Apt::Get::Download-Only true;\n";
|
|
|
|
# for authentication, use the keyrings from the host
|
|
|
|
print $conf "Dir::Etc::Trusted \"/etc/apt/trusted.gpg\";\n";
|
|
|
|
print $conf "Dir::Etc::TrustedParts \"/etc/apt/trusted.gpg.d\";\n";
|
|
|
|
close $conf;
|
|
|
|
|
|
|
|
foreach my $dir ('/etc/apt/apt.conf.d', '/etc/apt/sources.list.d',
|
|
|
|
'/etc/apt/preferences.d', '/var/cache/apt',
|
|
|
|
'/var/lib/apt/lists/partial', '/var/lib/dpkg',
|
|
|
|
'/etc/dpkg/dpkg.cfg.d/') {
|
|
|
|
make_path("$options->{root}/$dir") or die "failed to create $dir: $!";
|
|
|
|
}
|
|
|
|
|
2018-09-23 17:43:14 +00:00
|
|
|
# We put certain configuration items in their own configuration file
|
|
|
|
# because they have to be valid for apt invocation from outside as well as
|
|
|
|
# from inside the chroot.
|
|
|
|
# The config filename is chosen such that any settings in it will be
|
|
|
|
# overridden by what the user specified with --aptopt.
|
|
|
|
{
|
|
|
|
open my $fh, '>', "$options->{root}/etc/apt/apt.conf.d/00mmdebstrap" or die "cannot open /etc/apt/apt.conf.d/00mmdebstrap: $!";
|
|
|
|
print $fh "Apt::Install-Recommends false;\n";
|
|
|
|
print $fh "Acquire::Languages \"none\";\n";
|
|
|
|
close $fh;
|
|
|
|
}
|
|
|
|
|
2018-09-18 09:20:24 +00:00
|
|
|
{
|
|
|
|
open my $fh, '>', "$options->{root}/var/lib/dpkg/status" or die "failed to open(): $!";
|
|
|
|
close $fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scalar @{$options->{foreignarchs}} > 0) {
|
|
|
|
open my $fh, '>', "$options->{root}/var/lib/dpkg/arch" or die "cannot open /var/lib/dpkg/arch: $!";
|
|
|
|
print $fh "$options->{nativearch}\n";
|
|
|
|
foreach my $arch ($options->{foreignarchs}) {
|
|
|
|
print $fh "$arch\n";
|
|
|
|
}
|
|
|
|
close $fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scalar @{$options->{aptopts}} > 0) {
|
2018-09-23 17:43:14 +00:00
|
|
|
open my $fh, '>', "$options->{root}/etc/apt/apt.conf.d/99mmdebstrap" or die "cannot open /etc/apt/apt.conf.d/99mmdebstrap: $!";
|
2018-09-18 09:20:24 +00:00
|
|
|
foreach my $opt (@{$options->{aptopts}}) {
|
|
|
|
if (-r $opt) {
|
|
|
|
copy $opt, $fh or die "cannot copy $opt: $!";
|
|
|
|
} else {
|
|
|
|
print $fh $opt;
|
|
|
|
if ($opt !~ /;$/) {
|
|
|
|
print $fh ';';
|
|
|
|
}
|
|
|
|
if ($opt !~ /\n$/) {
|
|
|
|
print $fh "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close $fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scalar @{$options->{dpkgopts}} > 0) {
|
2018-09-23 17:43:14 +00:00
|
|
|
open my $fh, '>', "$options->{root}/etc/dpkg/dpkg.cfg.d/99mmdebstrap" or die "cannot open /etc/dpkg/dpkg.cfg.d/99mmdebstrap: $!";
|
2018-09-18 09:20:24 +00:00
|
|
|
foreach my $opt (@{$options->{dpkgopts}}) {
|
|
|
|
if (-r $opt) {
|
|
|
|
copy $opt, $fh or die "cannot copy $opt: $!";
|
|
|
|
} else {
|
|
|
|
print $fh $opt;
|
|
|
|
if ($opt !~ /\n$/) {
|
|
|
|
print $fh "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close $fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
# setup merged usr
|
|
|
|
my @amd64_dirs = ('lib32', 'lib64', 'libx32'); # only amd64 for now
|
|
|
|
foreach my $dir ("bin", "sbin", "lib", @amd64_dirs) {
|
|
|
|
symlink "usr/$dir", "$options->{root}/$dir" or die "cannot create symlink: $!";
|
|
|
|
make_path("$options->{root}/usr/$dir") or die "cannot create /usr/$dir: $!";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
open my $fh, '>', "$options->{root}/etc/fstab" or die "cannot open fstab: $!";
|
|
|
|
print $fh "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
|
|
|
|
close $fh;
|
|
|
|
chmod 0644, "$options->{root}/etc/fstab" or die "cannot chmod fstab: $!";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
my $archopt = '';
|
|
|
|
if (scalar @{$options->{foreignarchs}} > 0) {
|
2018-09-21 05:57:52 +00:00
|
|
|
$archopt = " [arch=$options->{nativearch}]";
|
2018-09-18 09:20:24 +00:00
|
|
|
}
|
|
|
|
open my $fh, '>', "$options->{root}/etc/apt/sources.list" or die "cannot open /etc/apt/sources.list: $!";
|
|
|
|
if (scalar(@{$options->{mirrors}}) > 0) {
|
2018-09-23 13:26:47 +00:00
|
|
|
if((grep /^-$/, @{$options->{mirrors}}) > 1 ) {
|
2018-09-18 09:20:24 +00:00
|
|
|
die "can only read from stdin once";
|
|
|
|
}
|
|
|
|
for my $arg (@{$options->{mirrors}}) {
|
|
|
|
if ($arg eq '-') {
|
|
|
|
# read from stdin
|
2018-09-18 11:10:09 +00:00
|
|
|
print STDERR "I: Reading sources.list from standard input...\n";
|
|
|
|
copy *STDIN, $fh or die "cannot copy stdin: $!";
|
2018-09-18 09:20:24 +00:00
|
|
|
} elsif ($arg =~ /:\/\//) {
|
2018-09-21 05:57:52 +00:00
|
|
|
print $fh "deb$archopt $arg $options->{suite} $options->{components}\n";
|
2018-09-18 09:20:24 +00:00
|
|
|
} elsif ($arg =~ /^deb(-src)? /) {
|
|
|
|
print $fh "$arg\n";
|
|
|
|
} elsif (-f $arg) {
|
|
|
|
copy($arg, $fh) or die "cannot copy $arg: $!";
|
|
|
|
} else {
|
|
|
|
die "invalid mirror: $arg";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print $fh "deb$archopt http://deb.debian.org/debian $options->{suite} $options->{components}\n";
|
2018-09-23 17:36:07 +00:00
|
|
|
if (any { $_ eq $options->{suite} } ('stable', 'oldstable', 'stretch') ) {
|
2018-09-18 09:20:24 +00:00
|
|
|
print $fh "deb$archopt http://deb.debian.org/debian $options->{suite}-updates $options->{components}\n";
|
|
|
|
print $fh "deb$archopt http://security.debian.org/debian-security $options->{suite}/updates $options->{components}\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close $fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
# we tell apt about the configuration via a config file passed via the
|
|
|
|
# APT_CONFIG environment variable instead of using the --option command
|
|
|
|
# line arguments because configuration settings like Dir::Etc have already
|
|
|
|
# been evaluated at the time that apt takes its command line arguments
|
|
|
|
# into account.
|
|
|
|
$ENV{"APT_CONFIG"} = "$tmpfile";
|
2018-09-21 06:04:40 +00:00
|
|
|
|
2018-09-23 20:27:49 +00:00
|
|
|
print STDERR "I: running apt-get update...\n";
|
2018-09-23 17:47:14 +00:00
|
|
|
run_apt_progress 'apt-get', 'update';
|
2018-09-21 06:04:40 +00:00
|
|
|
|
|
|
|
# check if anything was downloaded at all
|
|
|
|
{
|
|
|
|
open my $fh, '-|', 'apt-get', 'indextargets' // die "failed to fork(): $!";
|
|
|
|
chomp (my $indextargets = do { local $/; <$fh> });
|
|
|
|
close $fh;
|
|
|
|
if ($indextargets eq '') {
|
|
|
|
die "apt-get update didn't download anything";
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 09:20:24 +00:00
|
|
|
|
|
|
|
# setting PATH for chroot, ldconfig, start-stop-daemon...
|
|
|
|
$ENV{"PATH"} = "/usr/sbin:/usr/bin:/sbin:/bin";
|
|
|
|
|
|
|
|
my %pkgs_to_install;
|
|
|
|
if (defined $options->{include}) {
|
|
|
|
for my $pkg (split /,/, $options->{include}) {
|
|
|
|
$pkgs_to_install{$pkg} = ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($options->{variant} eq 'buildd') {
|
|
|
|
$pkgs_to_install{'build-essential'} = ();
|
|
|
|
}
|
|
|
|
# To figure out the right package set for the apt variant we can use:
|
|
|
|
# $ apt-get dist-upgrade -o dir::state::status=/dev/null
|
|
|
|
# This is because that variants only contain essential packages and
|
|
|
|
# apt and libapt treats apt as essential. If we want to install less
|
|
|
|
# (essential variant) then we have to compute the package set ourselves.
|
|
|
|
# Same if we want to install priority based variants.
|
|
|
|
if ($options->{variant} ne 'apt') {
|
|
|
|
my %ess_pkgs;
|
|
|
|
open(my $pipe_apt, '-|', 'apt-get', 'indextargets', '--format', '$(FILENAME)', 'Created-By: Packages', "Architecture: $options->{nativearch}") or die "cannot start apt-get indextargets: $!";
|
|
|
|
while (my $fname = <$pipe_apt>) {
|
|
|
|
chomp $fname;
|
|
|
|
open (my $pipe_cat, '-|', '/usr/lib/apt/apt-helper', 'cat-file', $fname) or die "cannot start apt-helper cat-file: $!";
|
|
|
|
|
2018-09-23 19:15:12 +00:00
|
|
|
my $pkgname;
|
|
|
|
my $ess = '';
|
|
|
|
my $prio = 'optional';
|
|
|
|
while (my $line = <$pipe_cat>) {
|
|
|
|
chomp $line;
|
|
|
|
# Dpkg::Index takes 10 seconds to parse a typical Packages
|
|
|
|
# file. Thus we instead use a simple parser that just retrieve
|
|
|
|
# the information we need.
|
|
|
|
if ($line ne "") {
|
|
|
|
if ($line =~ /^Package: (.*)/) {
|
|
|
|
$pkgname = $1;
|
|
|
|
} elsif ($line =~ /^Essential: yes$/) {
|
|
|
|
$ess = 'yes'
|
|
|
|
} elsif ($line =~ /^Priority: (.*)/) {
|
|
|
|
$prio = $1;
|
|
|
|
}
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
# the line is empty, thus a package stanza just finished
|
|
|
|
# processing and we can handle it now
|
2018-09-18 09:20:24 +00:00
|
|
|
if ($ess eq 'yes') {
|
|
|
|
$ess_pkgs{$pkgname} = ();
|
2018-09-23 19:15:12 +00:00
|
|
|
} elsif ($options->{variant} eq 'essential') {
|
2018-09-18 09:20:24 +00:00
|
|
|
# for this variant we are only interested in the
|
|
|
|
# essential packages
|
2018-09-23 17:36:07 +00:00
|
|
|
} elsif (any { $_ eq $options->{variant} } ('standard', 'important', 'required', 'buildd', 'minbase')) {
|
2018-09-18 09:20:24 +00:00
|
|
|
if ($prio eq 'optional' or $prio eq 'extra') {
|
2018-09-23 17:36:07 +00:00
|
|
|
# always ignore packages of priority optional and extra
|
2018-09-18 09:20:24 +00:00
|
|
|
} elsif ($prio eq 'standard') {
|
2018-09-23 19:15:12 +00:00
|
|
|
if (none { $_ eq $options->{variant} } ('important', 'required', 'buildd', 'minbase')) {
|
|
|
|
$pkgs_to_install{$pkgname} = ();
|
2018-09-18 09:20:24 +00:00
|
|
|
}
|
|
|
|
} elsif ($prio eq 'important') {
|
2018-09-23 19:15:12 +00:00
|
|
|
if (none { $_ eq $options->{variant} } ('required', 'buildd', 'minbase')) {
|
|
|
|
$pkgs_to_install{$pkgname} = ();
|
2018-09-18 09:20:24 +00:00
|
|
|
}
|
2018-09-23 17:36:07 +00:00
|
|
|
} elsif ($prio eq 'required') {
|
|
|
|
# required packages are part of all sets except
|
|
|
|
# essential and apt
|
2018-09-23 19:15:12 +00:00
|
|
|
$pkgs_to_install{$pkgname} = ();
|
2018-09-18 09:20:24 +00:00
|
|
|
} else {
|
|
|
|
die "unknown priority: $prio";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
die "unknown variant: $options->{variant}";
|
|
|
|
}
|
2018-09-23 19:15:12 +00:00
|
|
|
# reset values
|
|
|
|
undef $pkgname;
|
|
|
|
$ess = '';
|
|
|
|
$prio = 'optional';
|
2018-09-18 09:20:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close $pipe_cat;
|
|
|
|
$? == 0 or die "apt-helper cat-file failed: $?";
|
|
|
|
}
|
|
|
|
close $pipe_apt;
|
|
|
|
$? == 0 or die "apt-get indextargets failed: $?";
|
|
|
|
|
2018-09-23 20:27:49 +00:00
|
|
|
print STDERR "I: downloading packages with apt...\n";
|
2018-09-23 17:47:14 +00:00
|
|
|
run_apt_progress 'apt-get', '--yes', 'install', keys %ess_pkgs;
|
2018-09-18 09:20:24 +00:00
|
|
|
} else {
|
|
|
|
# if we just want to install Essential:yes packages, apt and their
|
|
|
|
# dependencies then we can make use of libapt treating apt as
|
|
|
|
# implicitly essential. An upgrade with the (currently) empty status
|
|
|
|
# file will trigger an installation of the essential packages plus apt.
|
|
|
|
#
|
|
|
|
# 2018-09-02, #debian-dpkg on OFTC, times in UTC+2
|
|
|
|
# 23:39 < josch> I'll just put it in my script and if it starts
|
|
|
|
# breaking some time I just say it's apt's fault. :P
|
|
|
|
# 23:42 < DonKult> that is how it usually works, so yes, do that :P (<-
|
|
|
|
# and please add that line next to it so you can
|
|
|
|
# remind me in 5+ years that I said that after I wrote
|
|
|
|
# in the bugreport: "Are you crazy?!? Nobody in his
|
|
|
|
# right mind would even suggest depending on it!")
|
2018-09-23 20:27:49 +00:00
|
|
|
print STDERR "I: downloading packages with apt...\n";
|
2018-09-23 17:47:14 +00:00
|
|
|
run_apt_progress 'apt-get', '--yes', 'dist-upgrade';
|
2018-09-18 09:20:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# extract the downloaded packages
|
2018-09-21 18:32:07 +00:00
|
|
|
my @essential_pkgs;
|
2018-09-21 20:10:14 +00:00
|
|
|
{
|
|
|
|
my $apt_archives = "/var/cache/apt/archives/";
|
|
|
|
opendir my $dh, "$options->{root}/$apt_archives" or die "cannot read $apt_archives";
|
|
|
|
while (my $deb = readdir $dh) {
|
|
|
|
if ($deb !~ /\.deb$/) {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
$deb = "$apt_archives/$deb";
|
|
|
|
if (!-f "$options->{root}/$deb") {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
push @essential_pkgs, $deb;
|
2018-09-21 18:32:07 +00:00
|
|
|
}
|
2018-09-21 20:10:14 +00:00
|
|
|
close $dh;
|
2018-09-21 18:32:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (scalar @essential_pkgs == 0) {
|
|
|
|
# check if a file:// URI was used
|
|
|
|
open(my $pipe_apt, '-|', 'apt-get', 'indextargets', '--format', '$(URI)', 'Created-By: Packages') or die "cannot start apt-get indextargets: $!";
|
|
|
|
while (my $uri = <$pipe_apt>) {
|
|
|
|
if ($uri =~ /^file:\/\//) {
|
|
|
|
die "nothing got downloaded -- use copy:// instead of file://";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
die "nothing got downloaded";
|
|
|
|
}
|
|
|
|
|
2018-09-23 20:27:49 +00:00
|
|
|
print STDERR "I: extracting archives...\n";
|
2018-09-23 17:47:14 +00:00
|
|
|
print_progress 0.0;
|
|
|
|
my $counter = 0;
|
|
|
|
my $total = scalar @essential_pkgs;
|
2018-09-21 18:32:07 +00:00
|
|
|
foreach my $deb (@essential_pkgs) {
|
2018-09-23 17:47:14 +00:00
|
|
|
$counter += 1;
|
2018-09-18 09:20:24 +00:00
|
|
|
# not using dpkg-deb --extract as that would replace the
|
|
|
|
# merged-usr symlinks with plain directories
|
|
|
|
pipe my $rfh, my $wfh;
|
|
|
|
my $pid1 = fork() // die "fork() failed: $!";
|
|
|
|
if ($pid1 == 0) {
|
|
|
|
open(STDOUT, '>&', $wfh);
|
2018-09-21 20:10:14 +00:00
|
|
|
exec 'dpkg-deb', '--fsys-tarfile', "$options->{root}/$deb";
|
2018-09-18 09:20:24 +00:00
|
|
|
}
|
|
|
|
my $pid2 = fork() // die "fork() failed: $!";
|
|
|
|
if ($pid2 == 0) {
|
|
|
|
open(STDIN, '<&', $rfh);
|
|
|
|
exec 'tar', '-C', $options->{root}, '--keep-directory-symlink', '--extract', '--file', '-';
|
|
|
|
}
|
|
|
|
waitpid($pid1, 0);
|
|
|
|
$? == 0 or die "dpkg-deb --fsys-tarfile failed: $?";
|
|
|
|
waitpid($pid2, 0);
|
|
|
|
$? == 0 or die "tar --extract failed: $?";
|
2018-09-23 17:47:14 +00:00
|
|
|
print_progress ($counter/$total*100);
|
2018-09-18 09:20:24 +00:00
|
|
|
}
|
2018-09-23 17:47:14 +00:00
|
|
|
print_progress "done";
|
2018-09-18 09:20:24 +00:00
|
|
|
|
|
|
|
if ($options->{mode} eq 'fakechroot') {
|
2018-09-18 14:59:02 +00:00
|
|
|
$ENV{FAKECHROOT_CMD_SUBST} = join ':', (
|
|
|
|
'/bin/mount=/bin/true',
|
|
|
|
'/usr/bin/env=/usr/bin/env.fakechroot',
|
|
|
|
'/usr/bin/ldd=/usr/bin/ldd.fakechroot',
|
|
|
|
'/usr/bin/mkfifo=/bin/true',
|
|
|
|
'/usr/sbin/ldconfig=/bin/true',
|
|
|
|
);
|
2018-09-18 09:20:24 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:11:48 +00:00
|
|
|
# make sure that APT_CONFIG is not set when executing anything inside the
|
|
|
|
# chroot
|
|
|
|
my @chrootcmd = ('env', '--unset=APT_CONFIG');
|
2018-09-18 09:20:24 +00:00
|
|
|
if ($options->{mode} eq 'proot') {
|
|
|
|
# FIXME: proot currently cannot install apt because of https://github.com/proot-me/PRoot/issues/147
|
2018-09-23 18:11:48 +00:00
|
|
|
push @chrootcmd, ('proot', '--root-id', '--bind=/dev', "--rootfs=$options->{root}", '--cwd=/');
|
|
|
|
} else {
|
|
|
|
push @chrootcmd, ('/usr/sbin/chroot', $options->{root});
|
2018-09-18 15:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# copy qemu-user-static binary into chroot or setup proot with --qemu
|
|
|
|
if (defined $options->{qemu}) {
|
|
|
|
if ($options->{mode} eq 'proot') {
|
2018-09-18 09:20:24 +00:00
|
|
|
push @chrootcmd, "--qemu=qemu-$options->{qemu}";
|
2018-09-18 15:11:02 +00:00
|
|
|
} elsif ($options->{mode} eq 'fakechroot') {
|
|
|
|
# The binfmt support on the outside is used, so qemu needs to know
|
|
|
|
# where it has to look for shared libraries
|
|
|
|
$ENV{QEMU_LD_PREFIX} = $options->{root};
|
|
|
|
# Make sure that the fakeroot and fakechroot shared libraries
|
|
|
|
# exist for the right architecture
|
|
|
|
open my $fh, '-|', 'dpkg-architecture', '-a', $options->{nativearch}, '-qDEB_HOST_MULTIARCH' // die "failed to fork(): $!";
|
|
|
|
chomp (my $deb_host_multiarch = do { local $/; <$fh> });
|
|
|
|
close $fh;
|
|
|
|
if ($? != 0 or !$deb_host_multiarch) {
|
|
|
|
die "dpkg-architecture failed: $?";
|
|
|
|
}
|
|
|
|
my $fakechrootdir = "/usr/lib/$deb_host_multiarch/fakechroot";
|
|
|
|
if (!-e "$fakechrootdir/libfakechroot.so") {
|
2018-09-21 05:57:52 +00:00
|
|
|
die "$fakechrootdir/libfakechroot.so doesn't exist. Install libfakechroot:$options->{nativearch} outside the chroot";
|
2018-09-18 15:11:02 +00:00
|
|
|
}
|
|
|
|
my $fakerootdir = "/usr/lib/$deb_host_multiarch/libfakeroot";
|
|
|
|
if (!-e "$fakerootdir/libfakeroot-sysv.so") {
|
2018-09-21 05:57:52 +00:00
|
|
|
die "$fakerootdir/libfakeroot-sysv.so doesn't exist. Install libfakeroot:$options->{nativearch} outside the chroot";
|
2018-09-18 15:11:02 +00:00
|
|
|
}
|
|
|
|
# fakechroot only fills LD_LIBRARY_PATH with the directories of
|
|
|
|
# the host's architecture. We append the directories of the chroot
|
|
|
|
# architecture.
|
|
|
|
$ENV{LD_LIBRARY_PATH} .= ":$fakechrootdir:$fakerootdir";
|
|
|
|
} else {
|
|
|
|
# other modes require a static qemu-user binary
|
|
|
|
my $qemubin = "/usr/bin/qemu-$options->{qemu}-static";
|
|
|
|
if (!-e $qemubin) {
|
|
|
|
die "cannot find $qemubin";
|
|
|
|
}
|
|
|
|
copy $qemubin, "$options->{root}/$qemubin";
|
2018-09-18 09:20:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($options->{havemknod}) {
|
|
|
|
foreach my $file (@devfiles) {
|
|
|
|
my ($fname, $mode, $type, $linkname, $devmajor, $devminor) = @{$file};
|
|
|
|
next if $fname eq './dev/';
|
|
|
|
if ($type == 0) { # normal file
|
|
|
|
die "type 0 not implemented";
|
|
|
|
} elsif ($type == 1) { # hardlink
|
|
|
|
die "type 1 not implemented";
|
|
|
|
} elsif ($type == 2) { # symlink
|
|
|
|
# nothing to do
|
|
|
|
} elsif ($type == 3) { # character special
|
|
|
|
0 == system('mknod', "$options->{root}/$fname", 'c', $devmajor, $devminor) or die "mknod failed: $?";
|
|
|
|
} elsif ($type == 4) { # block special
|
|
|
|
0 == system('mknod', "$options->{root}/$fname", 'b', $devmajor, $devminor) or die "mknod failed: $?";
|
|
|
|
} elsif ($type == 5) { # directory
|
|
|
|
make_path "$options->{root}/$fname", { error => \my $err };
|
|
|
|
if (@$err) {
|
|
|
|
die "cannot create $fname";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
die "unsupported type: $type";
|
|
|
|
}
|
|
|
|
chmod $mode, "$options->{root}/$fname";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# install the extracted packages properly
|
2018-09-23 17:47:14 +00:00
|
|
|
# we need --force-depends because dpkg does not take Pre-Depends into
|
|
|
|
# account and thus doesn't install them in the right order
|
2018-09-23 20:27:49 +00:00
|
|
|
print STDERR "I: installing packages...\n";
|
2018-09-23 17:47:14 +00:00
|
|
|
run_dpkg_progress [@essential_pkgs], @chrootcmd, 'dpkg', '--install', '--force-depends';
|
2018-09-18 09:20:24 +00:00
|
|
|
|
|
|
|
# if the path-excluded option was added to the dpkg config, reinstall all
|
|
|
|
# packages
|
2018-09-23 17:43:14 +00:00
|
|
|
if (-e "$options->{root}/etc/dpkg/dpkg.cfg.d/99mmdebstrap") {
|
|
|
|
open(my $fh, '<', "$options->{root}/etc/dpkg/dpkg.cfg.d/99mmdebstrap") or die "cannot open /etc/dpkg/dpkg.cfg.d/99mmdebstrap: $!";
|
2018-09-18 09:20:24 +00:00
|
|
|
my $num_matches = grep /^path-exclude=/, <$fh>;
|
|
|
|
close $fh;
|
|
|
|
if ($num_matches > 0) {
|
|
|
|
# without --skip-same-version, dpkg will install the given
|
|
|
|
# packages even though they are already installed
|
2018-09-23 20:27:49 +00:00
|
|
|
print STDERR "I: re-installing packages because of path-exclude...\n";
|
2018-09-23 17:47:14 +00:00
|
|
|
run_dpkg_progress [@essential_pkgs], @chrootcmd, 'dpkg', '--install';
|
2018-09-18 09:20:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 20:10:14 +00:00
|
|
|
foreach my $deb (@essential_pkgs) {
|
|
|
|
unlink "$options->{root}/$deb" or die "cannot unlink $deb";
|
|
|
|
}
|
|
|
|
|
2018-09-20 20:42:44 +00:00
|
|
|
if (%pkgs_to_install) {
|
|
|
|
# some packages have to be installed from the outside before anything
|
|
|
|
# can be installed from the inside.
|
|
|
|
my %pkgs_to_install_from_outside;
|
2018-09-18 09:20:24 +00:00
|
|
|
|
2018-09-20 20:42:44 +00:00
|
|
|
# install apt if necessary
|
|
|
|
if ($options->{variant} ne 'apt') {
|
|
|
|
$pkgs_to_install_from_outside{apt} = ();
|
|
|
|
}
|
|
|
|
|
|
|
|
# since apt will be run inside the chroot, make sure that
|
|
|
|
# apt-transport-https and ca-certificates gets installed first if any
|
|
|
|
# mirror is a https URI
|
|
|
|
open(my $pipe_apt, '-|', 'apt-get', 'indextargets', '--format', '$(URI)', 'Created-By: Packages') or die "cannot start apt-get indextargets: $!";
|
|
|
|
while (my $uri = <$pipe_apt>) {
|
|
|
|
if ($uri =~ /^https:\/\//) {
|
2018-09-21 06:05:39 +00:00
|
|
|
# FIXME: support for https is part of apt >= 1.5
|
2018-09-20 20:42:44 +00:00
|
|
|
$pkgs_to_install_from_outside{'apt-transport-https'} = ();
|
|
|
|
$pkgs_to_install_from_outside{'ca-certificates'} = ();
|
|
|
|
last;
|
2018-09-21 06:05:39 +00:00
|
|
|
} elsif ($uri =~ /^tor(\+[a-z]+)*:\/\//) {
|
|
|
|
# tor URIs can be tor+http://, tor+https:// or even
|
|
|
|
# tor+mirror+file://
|
|
|
|
$pkgs_to_install_from_outside{'apt-transport-tor'} = ();
|
|
|
|
last;
|
2018-09-20 20:42:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close $pipe_apt;
|
|
|
|
$? == 0 or die "apt-get indextargets failed";
|
|
|
|
|
|
|
|
if (%pkgs_to_install_from_outside) {
|
2018-09-23 20:27:49 +00:00
|
|
|
print STDERR 'I: downloading ' . (join ', ', keys %pkgs_to_install_from_outside) . "...\n";
|
2018-09-23 17:47:14 +00:00
|
|
|
run_apt_progress 'apt-get', '--yes', 'install', (keys %pkgs_to_install_from_outside);
|
2018-09-21 20:10:14 +00:00
|
|
|
my @debs_to_install;
|
|
|
|
my $apt_archives = "/var/cache/apt/archives/";
|
|
|
|
opendir my $dh, "$options->{root}/$apt_archives" or die "cannot read $apt_archives";
|
|
|
|
while (my $deb = readdir $dh) {
|
|
|
|
if ($deb !~ /\.deb$/) {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
$deb = "$apt_archives/$deb";
|
|
|
|
if (!-f "$options->{root}/$deb") {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
push @debs_to_install, $deb;
|
|
|
|
}
|
|
|
|
close $dh;
|
|
|
|
if (scalar @debs_to_install == 0) {
|
|
|
|
die "nothing got downloaded";
|
|
|
|
}
|
|
|
|
# we need --force-depends because dpkg does not take Pre-Depends
|
|
|
|
# into account and thus doesn't install them in the right order
|
2018-09-23 20:27:49 +00:00
|
|
|
print STDERR 'I: installing ' . (join ', ', keys %pkgs_to_install_from_outside) . "...\n";
|
2018-09-23 17:47:14 +00:00
|
|
|
run_dpkg_progress [@debs_to_install], @chrootcmd, 'dpkg', '--install', '--force-depends';
|
2018-09-21 20:10:14 +00:00
|
|
|
foreach my $deb (@debs_to_install) {
|
|
|
|
unlink "$options->{root}/$deb" or die "cannot unlink $deb";
|
|
|
|
}
|
2018-09-20 20:42:44 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 09:20:24 +00:00
|
|
|
# if more than essential should be installed, make the system look
|
|
|
|
# more like a real one by creating or bind-mounting the device nodes
|
|
|
|
foreach my $file (@devfiles) {
|
|
|
|
my ($fname, $mode, $type, $linkname, $devmajor, $devminor) = @{$file};
|
|
|
|
next if $fname eq './dev/';
|
|
|
|
if ($type == 0) { # normal file
|
|
|
|
die "type 0 not implemented";
|
|
|
|
} elsif ($type == 1) { # hardlink
|
|
|
|
die "type 1 not implemented";
|
|
|
|
} elsif ($type == 2) { # symlink
|
|
|
|
symlink $linkname, "$options->{root}/$fname";
|
|
|
|
} elsif ($type == 3 or $type == 4) { # character/block special
|
|
|
|
if (!$options->{havemknod}) {
|
|
|
|
open my $fh, '>', "$options->{root}/$fname" or die "cannot open $options->{root}/$fname: $!";
|
|
|
|
close $fh;
|
|
|
|
0 == system('mount', '-o', 'bind', "/$fname", "$options->{root}/$fname") or die "mount failed: $?";
|
|
|
|
}
|
|
|
|
} elsif ($type == 5) { # directory
|
|
|
|
if (!$options->{havemknod}) {
|
|
|
|
make_path "$options->{root}/$fname";
|
|
|
|
}
|
|
|
|
0 == system('mount', '-o', 'bind', "/$fname", "$options->{root}/$fname") or die "mount failed: $?";
|
|
|
|
} else {
|
|
|
|
die "unsupported type: $type";
|
|
|
|
}
|
|
|
|
chmod $mode, "$options->{root}/$fname";
|
|
|
|
}
|
|
|
|
# We can only mount /proc and /sys after extracting the essential
|
|
|
|
# set because if we mount it before, then base-files not be able
|
|
|
|
# to extract those
|
|
|
|
0 == system('mount', '-o', 'rbind', '/sys', "$options->{root}/sys") or die "mount failed: $?";
|
|
|
|
0 == system('mount', '-t', 'proc', 'proc', "$options->{root}/proc") or die "mount failed: $?";
|
|
|
|
|
|
|
|
# prevent daemons from starting
|
|
|
|
{
|
|
|
|
open my $fh, '>', "$options->{root}/usr/sbin/policy-rc.d" or die "cannot open policy-rc.d: $!";
|
|
|
|
print $fh "#!/bin/sh\n";
|
|
|
|
print $fh "exit 101\n";
|
|
|
|
close $fh;
|
|
|
|
chmod 0755, "$options->{root}/usr/sbin/policy-rc.d" or die "cannot chmod policy-rc.d: $!";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
move("$options->{root}/sbin/start-stop-daemon", "$options->{root}/sbin/start-stop-daemon.REAL") or die "cannot move start-stop-daemon";
|
|
|
|
open my $fh, '>', "$options->{root}/sbin/start-stop-daemon" or die "cannot open policy-rc.d: $!";
|
|
|
|
print $fh "#!/bin/sh\n";
|
|
|
|
print $fh "echo \"Warning: Fake start-stop-daemon called, doing nothing\">&2\n";
|
|
|
|
close $fh;
|
|
|
|
chmod 0755, "$options->{root}/sbin/start-stop-daemon" or die "cannot chmod start-stop-daemon: $!";
|
|
|
|
}
|
|
|
|
|
|
|
|
# allow network access from within
|
|
|
|
copy("/etc/resolv.conf", "$options->{root}/etc/resolv.conf");
|
|
|
|
copy("/etc/hostname", "$options->{root}/etc/hostname");
|
|
|
|
|
2018-09-23 20:27:49 +00:00
|
|
|
print STDERR "I: installing remaining packages inside the chroot...\n";
|
2018-09-23 17:47:14 +00:00
|
|
|
run_apt_progress @chrootcmd, 'apt-get', '--yes', 'install', keys %pkgs_to_install;
|
2018-09-18 09:20:24 +00:00
|
|
|
|
|
|
|
# cleanup
|
|
|
|
move("$options->{root}/sbin/start-stop-daemon.REAL", "$options->{root}/sbin/start-stop-daemon") or die "cannot move start-stop-daemon";
|
|
|
|
unlink "$options->{root}/usr/sbin/policy-rc.d" or die "cannot unlink policy-rc.d";
|
|
|
|
|
|
|
|
foreach my $file (@devfiles) {
|
|
|
|
my ($fname, undef, $type, $linkname, undef, undef) = @{$file};
|
|
|
|
next if $fname eq './dev/';
|
|
|
|
if ($type == 0) { # normal file
|
|
|
|
die "type 0 not implemented";
|
|
|
|
} elsif ($type == 1) { # hardlink
|
|
|
|
die "type 1 not implemented";
|
|
|
|
} elsif ($type == 2) { # symlink
|
|
|
|
unlink "$options->{root}/$fname" or die "cannot unlink $fname: $!";
|
|
|
|
} elsif ($type == 3 or $type == 4) { # character/block special
|
|
|
|
if (!$options->{havemknod}) {
|
|
|
|
0 == system('umount', '--no-mtab', "$options->{root}/$fname") or die "umount failed: $?";
|
|
|
|
unlink "$options->{root}/$fname";
|
|
|
|
}
|
|
|
|
} elsif ($type == 5) { # directory
|
|
|
|
0 == system('umount', '--no-mtab', "$options->{root}/$fname") or die "umount failed: $?";
|
|
|
|
if (!$options->{havemknod}) {
|
|
|
|
rmdir "$options->{root}/$fname" or die "cannot rmdir $fname: $!";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
die "unsupported type: $type";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# no need to umount if the mount namespace was unshared
|
|
|
|
if ($options->{mode} ne 'unshare') {
|
|
|
|
0 == system('umount', '--no-mtab', '--recursive', '--lazy', "$options->{root}/sys") or die "umount failed: $?";
|
|
|
|
0 == system('umount', '--no-mtab', "$options->{root}/proc") or die "umount failed: $?";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 17:43:14 +00:00
|
|
|
# clean up temporary configuration file
|
|
|
|
unlink "$options->{root}/etc/apt/apt.conf.d/00mmdebstrap" or die "failed to unlink /etc/apt/apt.conf.d/00mmdebstrap: $!";
|
|
|
|
|
2018-09-23 17:45:09 +00:00
|
|
|
# apt since 1.6 creates the auxfiles directory. If apt inside the chroot
|
|
|
|
# is older than that, then it will not know how to clean it.
|
|
|
|
rmdir "$options->{root}/var/lib/apt/lists/auxfiles" or die "cannot rmdir /var/lib/apt/lists/auxfiles: $!";
|
2018-09-18 09:20:24 +00:00
|
|
|
# if there is no apt inside the chroot, clean it from the outside
|
2018-09-23 20:27:49 +00:00
|
|
|
print STDERR "I: cleaning package lists and apt cache...\n";
|
2018-09-18 09:20:24 +00:00
|
|
|
if ($options->{variant} eq 'essential') {
|
2018-09-23 17:47:14 +00:00
|
|
|
run_apt_progress 'apt-get', '--option', 'Dir::Etc::SourceList=/dev/null', 'update';
|
|
|
|
run_apt_progress 'apt-get', 'clean';
|
2018-09-18 09:20:24 +00:00
|
|
|
} else {
|
2018-09-23 17:47:14 +00:00
|
|