]>
git.proxmox.com Git - aab.git/blob - PVE/AAB.pm
697c6644cf315c4630bce30183a26482d1f387b3
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'";
56 my $fh = IO
::File-
>new ("<$filename") or die "failed to read $filename - $!\n";
58 while (defined (my $line = <$fh>)) {
66 copy
($a, $b) or die "failed to copy $a => $b: $!";
71 rename($a, $b) or die "failed to rename $a => $b: $!";
76 symlink($a, $b) or die "failed to symlink $a => $b: $!";
87 my $fd = $self->{logfd
};
96 my $fh = IO
::File-
>new ("<$filename") || return $res;
99 while (defined (my $line = <$fh>)) {
100 next if $line =~ m/^\#/;
101 next if $line =~ m/^\s*$/;
111 if ($rec =~ s/^Description:\s*([^\n]*)(\n\s+.*)*$//si) {
112 $res->{headline
} = $1;
113 chomp $res->{headline
};
116 $res->{description
} = $long;
117 chomp $res->{description
};
118 } elsif ($rec =~ s/^([^:]+):\s*(.*\S)\s*\n//) {
119 my ($key, $value) = (lc ($1), $2);
120 if ($key eq 'source' || $key eq 'mirror') {
121 push @{$res->{$key}}, $value;
123 die "duplicate key '$key'\n" if defined ($res->{$key});
124 $res->{$key} = $value;
127 die "unable to parse config file: $rec";
131 die "unable to parse config file" if $rec;
133 $res->{architecture
} = 'amd64' if $res->{architecture
} eq 'x86_64';
139 my ($class, $config) = @_;
141 $config = read_config
('aab.conf') if !$config;
142 my $version = $config->{version
};
143 die "no 'version' specified\n" if !$version;
144 die "no 'section' specified\n" if !$config->{section
};
145 die "no 'description' specified\n" if !$config->{headline
};
146 die "no 'maintainer' specified\n" if !$config->{maintainer
};
148 my $name = $config->{name
} || die "no 'name' specified\n";
149 $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ ||
150 die "illegal characters in name '$name'\n";
153 if ($name =~ m/^archlinux/) {
154 $targetname = "${name}_${version}_$config->{architecture}";
156 $targetname = "archlinux-${name}_${version}_$config->{architecture}";
159 my $self = { logfile
=> 'logfile',
161 targetname
=> $targetname,
162 incl
=> [@BASE_PACKAGES],
163 excl
=> [@BASE_EXCLUDES],
166 $self->{logfd
} = IO
::File-
>new($self->{logfile
}, O_WRONLY
| O_APPEND
| O_CREAT
)
167 or die "unable to open log file";
171 $self->__allocate_ve();
176 sub __sample_config
{
179 my $arch = $self->{config
}->{architecture
};
183 lxc.include = /usr/share/lxc/config/archlinux.common.conf
184 lxc.uts.name = localhost
185 lxc.rootfs.path = $self->{rootfs}
186 lxc.mount.entry = $self->{pkgcache} $self->{pkgdir} none bind 0 0
194 if (my $fd = IO
::File-
>new(".veid")) {
201 $self->{working_dir
} = getcwd
;
202 $self->{veconffile
} = "$self->{working_dir}/config";
203 $self->{rootfs
} = "$self->{working_dir}/rootfs";
204 $self->{pkgdir
} = "$self->{working_dir}/rootfs/$PKGDIR";
205 $self->{pkgcache
} = "$self->{working_dir}/pkgcache";
206 $self->{'pacman.conf'} = "$self->{working_dir}/pacman.conf";
209 $self->{veid
} = $cid;
215 UUID
::generate
($uuid);
216 UUID
::unparse
($uuid, $uuid_str);
217 $self->{veid
} = $uuid_str;
219 my $fd = IO
::File-
>new (">.veid") ||
220 die "unable to write '.veid'\n";
221 print $fd "$self->{veid}\n";
223 $self->logmsg("allocated VE $self->{veid}\n");
229 my $config = $self->{config
};
231 $self->{logfd
} = IO
::File-
>new($self->{logfile
}, O_WRONLY
| O_TRUNC
| O_CREAT
)
232 or die "unable to open log file";
234 my $cdata = $self->__sample_config();
236 my $fh = IO
::File-
>new($self->{veconffile
}, O_WRONLY
|O_CREAT
|O_EXCL
) ||
237 die "unable to write lxc config file '$self->{veconffile}' - $!";
241 if (!$config->{source
} && !$config->{mirror
}) {
242 die "no sources/mirrors specified";
245 $self->write_pacman_conf();
247 $self->logmsg("configured VE $self->{veid}\n");
250 sub write_pacman_conf
{
251 my ($self, $config_fn, $siglevel) = @_;
253 my $config = $self->{config
};
255 $config->{source
} //= [];
256 $config->{mirror
} //= [];
258 $siglevel ||= "Never";
259 $config_fn ||= $self->{'pacman.conf'};
261 my $servers = "Server = ".join("\nServer = ", @{$config->{source
}}, @{$config->{mirror
}}) ."\n";
263 my $fh = IO
::File-
>new($config_fn, O_WRONLY
| O_CREAT
| O_EXCL
)
264 or die "unable to write pacman config file $self->{'pacman.conf'} - $!";
266 my $arch = $config->{architecture
};
267 $arch = 'x86_64' if $arch eq 'amd64';
271 HoldPkg = pacman glibc
284 print $fh "[multilib]\n$servers\n" if $config->{architecture
} eq 'x86_64';
292 my $veid = $self->{veid
};
294 my $res = { running
=> 0 };
296 $res->{exist
} = 1 if -d
"$self->{rootfs}/usr";
298 my $filename = "/proc/net/unix";
300 # similar test is used by lcxcontainers.c: list_active_containers
301 my $fh = IO
::File-
>new ($filename, "r");
304 while (defined(my $line = <$fh>)) {
305 if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) {
307 if ($path =~ m!^@/\S+/$veid/command$!) {
320 my $veid = $self->{veid
}; # fixme
322 my $vestat = $self->ve_status();
323 if ($vestat->{running
}) {
324 $self->stop_container();
327 rmtree
$self->{rootfs
};
328 unlink $self->{veconffile
};
335 my $veid = $self->{veid
};
336 my $conffile = $self->{veconffile
};
338 $self->logmsg ("initialize VE $veid\n");
340 my $vestat = $self->ve_status();
341 if ($vestat->{running
}) {
342 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
345 rmtree
$self->{rootfs
};
346 mkpath
"$self->{rootfs}/dev";
350 my ($self, $cmd, $input) = @_;
352 my $veid = $self->{veid
};
353 my $conffile = $self->{veconffile
};
355 if (ref ($cmd) eq 'ARRAY') {
356 unshift @$cmd, 'lxc-attach', '-n', $veid, '--rcfile', $conffile,'--clear-env', '--';
357 $self->run_command ($cmd, $input);
359 $self->run_command ("lxc-attach -n $veid --rcfile $conffile --clear-env -- $cmd", $input);
364 my ($self, @cmd) = @_;
366 my $veid = $self->{veid
};
367 my $conffile = $self->{veconffile
};
370 my $pid = open2
($reader, "<&STDIN", 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--', @cmd)
371 or die "unable to exec command";
373 while (defined (my $line = <$reader>)) {
374 $self->logmsg ($line);
380 die "ve_exec failed - status $rc\n" if $rc != 0;
384 my ($self, $cmd, $input, $getoutput, $noerr) = @_;
386 my $reader = IO
::File-
>new();
387 my $writer = IO
::File-
>new();
388 my $error = IO
::File-
>new();
392 my $cmdstr = ref ($cmd) eq 'ARRAY' ?
join (' ', @$cmd) : $cmd;
396 if (ref ($cmd) eq 'ARRAY') {
397 $pid = open3
($writer, $reader, $error, @$cmd) || die $!;
399 $pid = open3
($writer, $reader, $error, $cmdstr) || die $!;
406 if ($orig_pid != $$) {
407 $self->logmsg ("ERROR: command '$cmdstr' failed - fork failed\n");
414 print $writer $input if defined $input;
417 my $select = new IO
::Select
;
418 $select->add ($reader);
419 $select->add ($error);
422 my $logfd = $self->{logfd
};
424 while ($select->count) {
425 my @handles = $select->can_read ();
427 foreach my $h (@handles) {
429 my $count = sysread ($h, $buf, 4096);
430 if (!defined ($count)) {
432 die "command '$cmdstr' failed: $!";
434 $select->remove ($h) if !$count;
438 $res .= $buf if $getoutput;
445 die "command '$cmdstr' failed with exit code $ec\n" if $ec && !$noerr;
447 return wantarray ?
($res, $ec) : $res;
450 sub start_container
{
452 my $veid = $self->{veid
};
453 $self->run_command(['lxc-start', '-n', $veid, '-f', $self->{veconffile
}, '/usr/bin/aab_fake_init']);
458 my $veid = $self->{veid
};
459 my $conffile = $self->{veconffile
};
460 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
464 my ($self, $config_fn) = @_;
465 my $root = $self->{rootfs
};
469 '--config', $config_fn || $self->{'pacman.conf'},
470 '--cachedir', $self->{pkgcache
},
476 my ($self, $packages) = @_;
477 my $root = $self->{rootfs
};
479 $self->write_pacman_conf('pacman.caching.conf', "Optional");
480 my @pacman = $self->pacman_command('pacman.caching.conf');
481 my ($_res, $ec) = $self->run_command([@pacman, '-Sw', '--', @$packages], undef, undef, 1);
482 $self->logmsg("ignore bad exit $ec due to unavailable keyring, the CT will verify that later.\n")
486 sub mask_systemd_unit
{
487 my ($self, $unit) = @_;
488 my $root = $self->{rootfs
};
489 symln
'/dev/null', "$root/etc/systemd/system/$unit";
493 my ($self, $include, $exclude) = @_;
494 my $root = $self->{rootfs
};
496 my @pacman = $self->pacman_command();
498 print "Fetching package database...\n";
499 mkpath
$self->{pkgcache
};
500 mkpath
$self->{pkgdir
};
501 mkpath
"$root/var/lib/pacman";
502 $self->run_command([@pacman, '-Syy']);
504 print "Figuring out what to install...\n";
505 my $incl = { map { $_ => 1 } @{$self->{incl
}} };
506 my $excl = { map { $_ => 1 } @{$self->{excl
}} };
508 foreach my $addinc (@$include) {
509 $incl->{$addinc} = 1;
510 delete $excl->{$addinc};
512 foreach my $addexc (@$exclude) {
513 $excl->{$addexc} = 1;
514 delete $incl->{$addexc};
519 foreach my $inc (keys %$lst) {
521 eval { $group = $self->run_command([@pacman, '-Sqg', $inc], undef, 1); };
525 $lst->{$_} = 1 foreach split(/\s+/, $group);
533 my $packages = [ grep { !$excl->{$_} } keys %$incl ];
535 print "Setting up basic environment...\n";
537 mkpath
"$root/usr/bin";
539 my $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
540 write_file
($data, "$root/etc/fstab", 0644);
542 write_file
("", "$root/etc/resolv.conf", 0644);
543 write_file
("localhost\n", "$root/etc/hostname", 0644);
544 $self->run_command(['install', '-m0755', $fake_init, "$root/usr/bin/aab_fake_init"]);
546 unlink "$root/etc/localtime";
547 symln
'/usr/share/zoneinfo/UTC', "$root/etc/localtime";
549 print "Caching packages...\n";
550 $self->cache_packages($packages);
551 #$self->copy_packages();
553 print "Creating device nodes for package manager...\n";
556 print "Installing package manager and essentials...\n";
557 # inetutils for 'hostname' for our init
558 $self->run_command([@pacman, '-S', 'pacman', 'inetutils', 'archlinux-keyring']);
560 print "Setting up pacman for installation from cache...\n";
561 my $file = "$root/etc/pacman.d/mirrorlist";
562 my $backup = "${file}.aab_orig";
564 rename_file
($file, $backup);
565 write_file
("Server = file://$PKGDIR\n", $file);
568 print "Populating keyring...\n";
569 $self->populate_keyring();
571 print "Removing device nodes...\n";
572 $self->cleanup_dev();
574 print "Starting container...\n";
575 $self->start_container();
577 print "Installing packages...\n";
578 $self->ve_command(['pacman', '-S', '--needed', '--noconfirm', '--', @$packages]);
580 print "Masking problematic systemd units...\n";
581 for my $unit (qw(sys-kernel-config.mount sys-kernel-debug.mount)) {
582 $self->mask_systemd_unit($unit);
586 # devices needed for gnupg to function:
588 '/dev/null' => ['c', '1', '3'],
589 '/dev/random' => ['c', '1', '9'], # fake /dev/random (really urandom)
590 '/dev/urandom' => ['c', '1', '9'],
591 '/dev/tty' => ['c', '5', '0'],
596 my $root = $self->{rootfs
};
598 # remove temporary device files
599 unlink "${root}$_" foreach keys %$devs;
604 my $root = $self->{rootfs
};
606 local $SIG{INT
} = $SIG{TERM
} = sub { $self->cleanup_dev; };
608 # we want to replace /dev/random, so delete devices first
609 $self->cleanup_dev();
611 foreach my $dev (keys %$devs) {
612 my ($type, $major, $minor) = @{$devs->{$dev}};
613 system('mknod', "${root}${dev}", $type, $major, $minor);
617 sub populate_keyring
{
619 my $root = $self->{rootfs
};
621 # generate weak master key and populate the keyring
622 system('unshare', '--fork', '--pid', 'chroot', "$root", 'pacman-key', '--init') == 0
623 or die "failed to initialize keyring: $?";
624 system('unshare', '--fork', '--pid', 'chroot', "$root", 'pacman-key', '--populate') == 0
625 or die "failed to populate keyring: $?";
630 my ($self, $pkglist) = @_;
632 $self->cache_packages($pkglist);
633 $self->ve_command(['pacman', '-S', '--needed', '--noconfirm', '--', @$pkglist]);
637 my ($self, $filename, $size) = @_;
639 my $config = $self->{config
};
643 $data .= "Name: $config->{name}\n";
644 $data .= "Version: $config->{version}\n";
645 $data .= "Type: lxc\n";
646 $data .= "OS: archlinux\n";
647 $data .= "Section: $config->{section}\n";
648 $data .= "Maintainer: $config->{maintainer}\n";
649 $data .= "Architecture: $config->{architecture}\n";
650 $data .= "Infopage: https://www.archlinux.org\n";
651 $data .= "Installed-Size: $size\n";
654 $data .= "Infopage: $config->{infopage}\n" if $config->{infopage
};
655 $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl
};
656 $data .= "Certified: $config->{certified}\n" if $config->{certified
};
659 $data .= "Description: $config->{headline}\n";
660 $data .= "$config->{description}\n" if $config->{description
};
662 write_file
($data, $filename, 0644);
666 my ($self, $compressor) = @_;
669 if (defined($compressor)) {
670 if ($compressor =~ /^\s*--zstd?\s*$/) {
672 } elsif ($compressor =~ /^\s*--(?:gz|gzip)\s*$/) {
673 $use_zstd = 0; # just boolean for now..
675 die "finalize: unknown compressor '$compressor'!\n";
679 my $rootdir = $self->{rootfs
};
681 print "Stopping container...\n";
682 $self->stop_container();
684 print "Rolling back mirrorlist changes...\n";
685 my $file = "$rootdir/etc/pacman.d/mirrorlist";
687 rename_file
($file.'.aab_orig', $file);
689 # experienced user can change it anytime and others do well to start out with an updatable system..
690 my $mirrors = eval { read_file
($file) } // '';
691 $mirrors = "\nServer = https://geo.mirror.pkgbuild.com/\$repo/os/\$arch\n\n" . $mirrors;
692 write_file
($mirrors, $file, 0644);
694 print "Removing weak temporary pacman keyring...\n";
695 rmtree
("$rootdir/etc/pacman.d/gnupg");
697 my $sizestr = $self->run_command("du -sm $rootdir", undef, 1);
699 if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) {
702 die "unable to detect size\n";
704 $self->logmsg ("uncompressed size: $size MB\n");
706 $self->write_config ("$rootdir/etc/appliance.info", $size);
708 $self->logmsg ("creating final appliance archive\n");
710 my $compressor_ext = $use_zstd ?
'zst' : 'gz';
712 my $target = "$self->{targetname}.tar";
714 unlink "$target.$compressor_ext";
716 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
717 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
719 $self->logmsg ("compressing archive ($compressor_ext)\n");
721 $self->run_command ("zstd -19 --rm $target");
723 $self->run_command ("gzip -9 $target");
726 my $target_size = int(-s
"$target.$compressor_ext") >> 20;
727 $self->logmsg ("created '$target.$compressor_ext' with size: $target_size MB\n");
732 my $veid = $self->{veid
};
733 my $conffile = $self->{veconffile
};
735 my $vestat = $self->ve_status();
736 if (!$vestat->{exist
}) {
737 $self->logmsg ("Please create the appliance first (bootstrap)");
741 if (!$vestat->{running
}) {
742 $self->start_container();
745 system ("lxc-attach -n $veid --rcfile $conffile --clear-env");
749 my ($self, $all) = @_;
751 unlink $self->{logfile
};
752 unlink $self->{'pacman.conf'}, 'pacman.caching.conf';
755 unlink $self->{veconffile
};
757 rmtree
$self->{pkgcache
} if $all;