]>
git.proxmox.com Git - aab.git/blob - PVE/AAB.pm
14 my @BASE_PACKAGES = qw(base openssh vi nano);
15 my @BASE_EXCLUDES = qw(
28 my $PKGDIR = "/var/cache/pacman/pkg";
30 my ($aablibdir, $fake_init);
32 sub setup_defaults
($) {
35 $fake_init = "$aablibdir/scripts/init.bash";
38 setup_defaults
('/usr/lib/aab');
41 my ($data, $file, $perm) = @_;
43 die "no filename" if !$file;
46 my $fh = IO
::File-
>new ($file, O_WRONLY
| O_CREAT
, $perm) ||
47 die "unable to open file '$file'";
55 copy
($a, $b) or die "failed to copy $a => $b: $!";
60 rename($a, $b) or die "failed to rename $a => $b: $!";
65 symlink($a, $b) or die "failed to symlink $a => $b: $!";
76 my $fd = $self->{logfd
};
85 my $fh = IO
::File-
>new ("<$filename") || return $res;
88 while (defined (my $line = <$fh>)) {
89 next if $line =~ m/^\#/;
90 next if $line =~ m/^\s*$/;
100 if ($rec =~ s/^Description:\s*([^\n]*)(\n\s+.*)*$//si) {
101 $res->{headline
} = $1;
102 chomp $res->{headline
};
105 $res->{description
} = $long;
106 chomp $res->{description
};
107 } elsif ($rec =~ s/^([^:]+):\s*(.*\S)\s*\n//) {
108 my ($key, $value) = (lc ($1), $2);
109 if ($key eq 'source' || $key eq 'mirror') {
110 push @{$res->{$key}}, $value;
112 die "duplicate key '$key'\n" if defined ($res->{$key});
113 $res->{$key} = $value;
116 die "unable to parse config file: $rec";
120 die "unable to parse config file" if $rec;
122 $res->{architecture
} = 'amd64' if $res->{architecture
} eq 'x86_64';
128 my ($class, $config) = @_;
130 $config = read_config
('aab.conf') if !$config;
131 my $version = $config->{version
};
132 die "no 'version' specified\n" if !$version;
133 die "no 'section' specified\n" if !$config->{section
};
134 die "no 'description' specified\n" if !$config->{headline
};
135 die "no 'maintainer' specified\n" if !$config->{maintainer
};
137 my $name = $config->{name
} || die "no 'name' specified\n";
138 $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ ||
139 die "illegal characters in name '$name'\n";
142 if ($name =~ m/^archlinux/) {
143 $targetname = "${name}_${version}_$config->{architecture}";
145 $targetname = "archlinux-${name}_${version}_$config->{architecture}";
148 my $self = { logfile
=> 'logfile',
150 targetname
=> $targetname,
151 incl
=> [@BASE_PACKAGES],
152 excl
=> [@BASE_EXCLUDES],
155 $self->{logfd
} = IO
::File-
>new($self->{logfile
}, O_WRONLY
| O_APPEND
| O_CREAT
)
156 or die "unable to open log file";
160 $self->__allocate_ve();
165 sub __sample_config
{
168 my $arch = $self->{config
}->{architecture
};
172 lxc.include = /usr/share/lxc/config/archlinux.common.conf
173 lxc.uts.name = localhost
174 lxc.rootfs.path = $self->{rootfs}
175 lxc.mount.entry = $self->{pkgcache} $self->{pkgdir} none bind 0 0
183 if (my $fd = IO
::File-
>new(".veid")) {
190 $self->{working_dir
} = getcwd
;
191 $self->{veconffile
} = "$self->{working_dir}/config";
192 $self->{rootfs
} = "$self->{working_dir}/rootfs";
193 $self->{pkgdir
} = "$self->{working_dir}/rootfs/$PKGDIR";
194 $self->{pkgcache
} = "$self->{working_dir}/pkgcache";
195 $self->{'pacman.conf'} = "$self->{working_dir}/pacman.conf";
198 $self->{veid
} = $cid;
204 UUID
::generate
($uuid);
205 UUID
::unparse
($uuid, $uuid_str);
206 $self->{veid
} = $uuid_str;
208 my $fd = IO
::File-
>new (">.veid") ||
209 die "unable to write '.veid'\n";
210 print $fd "$self->{veid}\n";
212 $self->logmsg("allocated VE $self->{veid}\n");
218 my $config = $self->{config
};
220 $self->{logfd
} = IO
::File-
>new($self->{logfile
}, O_WRONLY
| O_TRUNC
| O_CREAT
)
221 or die "unable to open log file";
223 my $cdata = $self->__sample_config();
225 my $fh = IO
::File-
>new($self->{veconffile
}, O_WRONLY
|O_CREAT
|O_EXCL
) ||
226 die "unable to write lxc config file '$self->{veconffile}' - $!";
230 if (!$config->{source
} && !$config->{mirror
}) {
231 die "no sources/mirrors specified";
234 $self->write_pacman_conf();
236 $self->logmsg("configured VE $self->{veid}\n");
239 sub write_pacman_conf
{
240 my ($self, $config_fn, $siglevel) = @_;
242 my $config = $self->{config
};
244 $config->{source
} //= [];
245 $config->{mirror
} //= [];
247 $siglevel ||= "Never";
248 $config_fn ||= $self->{'pacman.conf'};
250 my $servers = "Server = ".join("\nServer = ", @{$config->{source
}}, @{$config->{mirror
}}) ."\n";
252 my $fh = IO
::File-
>new($config_fn, O_WRONLY
| O_CREAT
| O_EXCL
)
253 or die "unable to write pacman config file $self->{'pacman.conf'} - $!";
255 my $arch = $config->{architecture
};
256 $arch = 'x86_64' if $arch eq 'amd64';
260 HoldPkg = pacman glibc
273 print $fh "[multilib]\n$servers\n" if $config->{architecture
} eq 'x86_64';
281 my $veid = $self->{veid
};
283 my $res = { running
=> 0 };
285 $res->{exist
} = 1 if -d
"$self->{rootfs}/usr";
287 my $filename = "/proc/net/unix";
289 # similar test is used by lcxcontainers.c: list_active_containers
290 my $fh = IO
::File-
>new ($filename, "r");
293 while (defined(my $line = <$fh>)) {
294 if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) {
296 if ($path =~ m!^@/\S+/$veid/command$!) {
309 my $veid = $self->{veid
}; # fixme
311 my $vestat = $self->ve_status();
312 if ($vestat->{running
}) {
313 $self->stop_container();
316 rmtree
$self->{rootfs
};
317 unlink $self->{veconffile
};
324 my $veid = $self->{veid
};
325 my $conffile = $self->{veconffile
};
327 $self->logmsg ("initialize VE $veid\n");
329 my $vestat = $self->ve_status();
330 if ($vestat->{running
}) {
331 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
334 rmtree
$self->{rootfs
};
335 mkpath
"$self->{rootfs}/dev";
339 my ($self, $cmd, $input) = @_;
341 my $veid = $self->{veid
};
342 my $conffile = $self->{veconffile
};
344 if (ref ($cmd) eq 'ARRAY') {
345 unshift @$cmd, 'lxc-attach', '-n', $veid, '--rcfile', $conffile,'--clear-env', '--';
346 $self->run_command ($cmd, $input);
348 $self->run_command ("lxc-attach -n $veid --rcfile $conffile --clear-env -- $cmd", $input);
353 my ($self, @cmd) = @_;
355 my $veid = $self->{veid
};
356 my $conffile = $self->{veconffile
};
359 my $pid = open2
($reader, "<&STDIN", 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--', @cmd)
360 or die "unable to exec command";
362 while (defined (my $line = <$reader>)) {
363 $self->logmsg ($line);
369 die "ve_exec failed - status $rc\n" if $rc != 0;
373 my ($self, $cmd, $input, $getoutput, $noerr) = @_;
375 my $reader = IO
::File-
>new();
376 my $writer = IO
::File-
>new();
377 my $error = IO
::File-
>new();
381 my $cmdstr = ref ($cmd) eq 'ARRAY' ?
join (' ', @$cmd) : $cmd;
385 if (ref ($cmd) eq 'ARRAY') {
386 $pid = open3
($writer, $reader, $error, @$cmd) || die $!;
388 $pid = open3
($writer, $reader, $error, $cmdstr) || die $!;
395 if ($orig_pid != $$) {
396 $self->logmsg ("ERROR: command '$cmdstr' failed - fork failed\n");
403 print $writer $input if defined $input;
406 my $select = new IO
::Select
;
407 $select->add ($reader);
408 $select->add ($error);
411 my $logfd = $self->{logfd
};
413 while ($select->count) {
414 my @handles = $select->can_read ();
416 foreach my $h (@handles) {
418 my $count = sysread ($h, $buf, 4096);
419 if (!defined ($count)) {
421 die "command '$cmdstr' failed: $!";
423 $select->remove ($h) if !$count;
427 $res .= $buf if $getoutput;
434 die "command '$cmdstr' failed with exit code $ec\n" if $ec && !$noerr;
436 return wantarray ?
($res, $ec) : $res;
439 sub start_container
{
441 my $veid = $self->{veid
};
442 $self->run_command(['lxc-start', '-n', $veid, '-f', $self->{veconffile
}, '/usr/bin/aab_fake_init']);
447 my $veid = $self->{veid
};
448 my $conffile = $self->{veconffile
};
449 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
453 my ($self, $config_fn) = @_;
454 my $root = $self->{rootfs
};
458 '--config', $config_fn || $self->{'pacman.conf'},
459 '--cachedir', $self->{pkgcache
},
465 my ($self, $packages) = @_;
466 my $root = $self->{rootfs
};
468 $self->write_pacman_conf('pacman.caching.conf', "Optional");
469 my @pacman = $self->pacman_command('pacman.caching.conf');
470 my ($_res, $ec) = $self->run_command([@pacman, '-Sw', '--', @$packages], undef, undef, 1);
471 $self->logmsg("ignore bad exit $ec due to unavailable keyring, the CT will verify that later.\n")
475 sub mask_systemd_unit
{
476 my ($self, $unit) = @_;
477 my $root = $self->{rootfs
};
478 symln
'/dev/null', "$root/etc/systemd/system/$unit";
482 my ($self, $include, $exclude) = @_;
483 my $root = $self->{rootfs
};
485 my @pacman = $self->pacman_command();
487 print "Fetching package database...\n";
488 mkpath
$self->{pkgcache
};
489 mkpath
$self->{pkgdir
};
490 mkpath
"$root/var/lib/pacman";
491 $self->run_command([@pacman, '-Syy']);
493 print "Figuring out what to install...\n";
494 my $incl = { map { $_ => 1 } @{$self->{incl
}} };
495 my $excl = { map { $_ => 1 } @{$self->{excl
}} };
497 foreach my $addinc (@$include) {
498 $incl->{$addinc} = 1;
499 delete $excl->{$addinc};
501 foreach my $addexc (@$exclude) {
502 $excl->{$addexc} = 1;
503 delete $incl->{$addexc};
508 foreach my $inc (keys %$lst) {
510 eval { $group = $self->run_command([@pacman, '-Sqg', $inc], undef, 1); };
514 $lst->{$_} = 1 foreach split(/\s+/, $group);
522 my $packages = [ grep { !$excl->{$_} } keys %$incl ];
524 print "Setting up basic environment...\n";
526 mkpath
"$root/usr/bin";
528 my $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
529 write_file
($data, "$root/etc/fstab", 0644);
531 write_file
("", "$root/etc/resolv.conf", 0644);
532 write_file
("localhost\n", "$root/etc/hostname", 0644);
533 $self->run_command(['install', '-m0755', $fake_init, "$root/usr/bin/aab_fake_init"]);
535 unlink "$root/etc/localtime";
536 symln
'/usr/share/zoneinfo/UTC', "$root/etc/localtime";
538 print "Caching packages...\n";
539 $self->cache_packages($packages);
540 #$self->copy_packages();
542 print "Creating device nodes for package manager...\n";
545 print "Installing package manager and essentials...\n";
546 # inetutils for 'hostname' for our init
547 $self->run_command([@pacman, '-S', 'pacman', 'inetutils', 'archlinux-keyring']);
549 print "Setting up pacman for installation from cache...\n";
550 my $file = "$root/etc/pacman.d/mirrorlist";
551 my $backup = "${file}.aab_orig";
553 rename_file
($file, $backup);
554 write_file
("Server = file://$PKGDIR\n", $file);
557 print "Populating keyring...\n";
558 $self->populate_keyring();
560 print "Removing device nodes...\n";
561 $self->cleanup_dev();
563 print "Starting container...\n";
564 $self->start_container();
566 print "Installing packages...\n";
567 $self->ve_command(['pacman', '-S', '--needed', '--noconfirm', '--', @$packages]);
569 print "Masking problematic systemd units...\n";
570 for my $unit (qw(sys-kernel-config.mount sys-kernel-debug.mount)) {
571 $self->mask_systemd_unit($unit);
575 # devices needed for gnupg to function:
577 '/dev/null' => ['c', '1', '3'],
578 '/dev/random' => ['c', '1', '9'], # fake /dev/random (really urandom)
579 '/dev/urandom' => ['c', '1', '9'],
580 '/dev/tty' => ['c', '5', '0'],
585 my $root = $self->{rootfs
};
587 # remove temporary device files
588 unlink "${root}$_" foreach keys %$devs;
593 my $root = $self->{rootfs
};
595 local $SIG{INT
} = $SIG{TERM
} = sub { $self->cleanup_dev; };
597 # we want to replace /dev/random, so delete devices first
598 $self->cleanup_dev();
600 foreach my $dev (keys %$devs) {
601 my ($type, $major, $minor) = @{$devs->{$dev}};
602 system('mknod', "${root}${dev}", $type, $major, $minor);
606 sub populate_keyring
{
608 my $root = $self->{rootfs
};
610 # generate weak master key and populate the keyring
611 system('unshare', '--fork', '--pid', 'chroot', "$root", 'pacman-key', '--init') == 0
612 or die "failed to initialize keyring: $?";
613 system('unshare', '--fork', '--pid', 'chroot', "$root", 'pacman-key', '--populate') == 0
614 or die "failed to populate keyring: $?";
619 my ($self, $pkglist) = @_;
621 $self->cache_packages($pkglist);
622 $self->ve_command(['pacman', '-S', '--needed', '--noconfirm', '--', @$pkglist]);
626 my ($self, $filename, $size) = @_;
628 my $config = $self->{config
};
632 $data .= "Name: $config->{name}\n";
633 $data .= "Version: $config->{version}\n";
634 $data .= "Type: lxc\n";
635 $data .= "OS: archlinux\n";
636 $data .= "Section: $config->{section}\n";
637 $data .= "Maintainer: $config->{maintainer}\n";
638 $data .= "Architecture: $config->{architecture}\n";
639 $data .= "Infopage: https://www.archlinux.org\n";
640 $data .= "Installed-Size: $size\n";
643 $data .= "Infopage: $config->{infopage}\n" if $config->{infopage
};
644 $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl
};
645 $data .= "Certified: $config->{certified}\n" if $config->{certified
};
648 $data .= "Description: $config->{headline}\n";
649 $data .= "$config->{description}\n" if $config->{description
};
651 write_file
($data, $filename, 0644);
655 my ($self, $compressor) = @_;
658 if (defined($compressor)) {
659 if ($compressor =~ /^\s*--zstd?\s*$/) {
661 } elsif ($compressor =~ /^\s*--(?:gz|gzip)\s*$/) {
662 $use_zstd = 0; # just boolean for now..
664 die "unkown compressor '$compressor'!\n";
668 my $rootdir = $self->{rootfs
};
670 print "Stopping container...\n";
671 $self->stop_container();
673 print "Rolling back mirrorlist changes...\n";
674 my $file = "$rootdir/etc/pacman.d/mirrorlist";
676 rename_file
($file.'.aab_orig', $file);
678 print "Removing weak temporary pacman keyring...\n";
679 rmtree
("$rootdir/etc/pacman.d/gnupg");
681 my $sizestr = $self->run_command("du -sm $rootdir", undef, 1);
683 if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) {
686 die "unable to detect size\n";
688 $self->logmsg ("uncompressed size: $size MB\n");
690 $self->write_config ("$rootdir/etc/appliance.info", $size);
692 $self->logmsg ("creating final appliance archive\n");
694 my $compressor_ext = $use_zstd ?
'zst' : 'gz';
696 my $target = "$self->{targetname}.tar";
698 unlink "$target.$compressor_ext";
700 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
701 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
703 $self->logmsg ("compressing archive ($compressor_ext)\n");
705 $self->run_command ("zstd -19 --rm $target");
707 $self->run_command ("gzip -9 $target");
710 my $target_size = int(-s
"$target.$compressor_ext") >> 20;
711 $self->logmsg ("created '$target.$compressor_ext' with size: $target_size MB\n");
716 my $veid = $self->{veid
};
717 my $conffile = $self->{veconffile
};
719 my $vestat = $self->ve_status();
720 if (!$vestat->{exist
}) {
721 $self->logmsg ("Please create the appliance first (bootstrap)");
725 if (!$vestat->{running
}) {
726 $self->start_container();
729 system ("lxc-attach -n $veid --rcfile $conffile --clear-env");
733 my ($self, $all) = @_;
735 unlink $self->{logfile
};
736 unlink $self->{'pacman.conf'}, 'pacman.caching.conf';
739 unlink $self->{veconffile
};
741 rmtree
$self->{pkgcache
} if $all;