]>
git.proxmox.com Git - aab.git/blob - PVE/AAB.pm
15 my @BASE_PACKAGES = qw(base openssh);
16 my @BASE_EXCLUDES = qw(e2fsprogs
27 my $PKGDIR = "/var/cache/pacman/pkg";
29 my ($aablibdir, $fake_init);
31 sub setup_defaults
($) {
34 $fake_init = "$aablibdir/scripts/init.bash";
37 setup_defaults
('/usr/lib/aab');
40 my ($data, $file, $perm) = @_;
42 die "no filename" if !$file;
45 my $fh = IO
::File-
>new ($file, O_WRONLY
| O_CREAT
, $perm) ||
46 die "unable to open file '$file'";
54 copy
($a, $b) or die "failed to copy $a => $b: $!";
59 rename($a, $b) or die "failed to rename $a => $b: $!";
64 symlink($a, $b) or die "failed to symlink $a => $b: $!";
75 my $fd = $self->{logfd
};
84 my $fh = IO
::File-
>new ("<$filename") || return $res;
87 while (defined (my $line = <$fh>)) {
88 next if $line =~ m/^\#/;
89 next if $line =~ m/^\s*$/;
99 if ($rec =~ s/^Description:\s*([^\n]*)(\n\s+.*)*$//si) {
100 $res->{headline
} = $1;
101 chomp $res->{headline
};
104 $res->{description
} = $long;
105 chomp $res->{description
};
106 } elsif ($rec =~ s/^([^:]+):\s*(.*\S)\s*\n//) {
107 my ($key, $value) = (lc ($1), $2);
108 if ($key eq 'source' || $key eq 'mirror') {
109 push @{$res->{$key}}, $value;
111 die "duplicate key '$key'\n" if defined ($res->{$key});
112 $res->{$key} = $value;
115 die "unable to parse config file: $rec";
119 die "unable to parse config file" if $rec;
121 $res->{architecture
} = 'amd64' if $res->{architecture
} eq 'x86_64';
127 my ($class, $config) = @_;
129 $config = read_config
('aab.conf') if !$config;
130 my $version = $config->{version
};
131 die "no 'version' specified\n" if !$version;
132 die "no 'section' specified\n" if !$config->{section
};
133 die "no 'description' specified\n" if !$config->{headline
};
134 die "no 'maintainer' specified\n" if !$config->{maintainer
};
136 my $name = $config->{name
} || die "no 'name' specified\n";
137 $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ ||
138 die "illegal characters in name '$name'\n";
141 if ($name =~ m/^archlinux/) {
142 $targetname = "${name}_${version}_$config->{architecture}";
144 $targetname = "archlinux-${name}_${version}_$config->{architecture}";
147 my $self = { logfile
=> 'logfile',
149 targetname
=> $targetname,
150 incl
=> [@BASE_PACKAGES],
151 excl
=> [@BASE_EXCLUDES],
154 $self->{logfd
} = IO
::File-
>new($self->{logfile
}, O_WRONLY
| O_APPEND
| O_CREAT
)
155 or die "unable to open log file";
159 $self->__allocate_ve();
164 sub __sample_config
{
167 my $arch = $self->{config
}->{architecture
};
171 lxc.include = /usr/share/lxc/config/archlinux.common.conf
172 lxc.uts.name = localhost
173 lxc.rootfs.path = $self->{rootfs}
174 lxc.mount.entry = $self->{pkgcache} $self->{pkgdir} none bind 0 0
182 if (my $fd = IO
::File-
>new(".veid")) {
189 $self->{working_dir
} = getcwd
;
190 $self->{veconffile
} = "$self->{working_dir}/config";
191 $self->{rootfs
} = "$self->{working_dir}/rootfs";
192 $self->{pkgdir
} = "$self->{working_dir}/rootfs/$PKGDIR";
193 $self->{pkgcache
} = "$self->{working_dir}/pkgcache";
194 $self->{'pacman.conf'} = "$self->{working_dir}/pacman.conf";
197 $self->{veid
} = $cid;
203 UUID
::generate
($uuid);
204 UUID
::unparse
($uuid, $uuid_str);
205 $self->{veid
} = $uuid_str;
207 my $fd = IO
::File-
>new (">.veid") ||
208 die "unable to write '.veid'\n";
209 print $fd "$self->{veid}\n";
211 $self->logmsg("allocated VE $self->{veid}\n");
217 my $config = $self->{config
};
219 $self->{logfd
} = IO
::File-
>new($self->{logfile
}, O_WRONLY
| O_TRUNC
| O_CREAT
)
220 or die "unable to open log file";
222 my $cdata = $self->__sample_config();
224 my $fh = IO
::File-
>new($self->{veconffile
}, O_WRONLY
|O_CREAT
|O_EXCL
) ||
225 die "unable to write lxc config file '$self->{veconffile}' - $!";
229 if (!$config->{source
} && !$config->{mirror
}) {
230 die "no sources/mirrors specified";
233 $config->{source
} //= [];
234 $config->{mirror
} //= [];
236 my $servers = "Server = "
237 . join("\nServer = ", @{$config->{source
}}, @{$config->{mirror
}})
240 $fh = IO
::File-
>new($self->{'pacman.conf'}, O_WRONLY
|O_CREAT
|O_EXCL
) ||
241 die "unable to write pacman config file $self->{'pacman.conf'} - $!";
242 my $arch = $config->{architecture
};
243 $arch = 'x86_64' if $arch eq 'amd64';
246 HoldPkg = pacman glibc
259 if ($config->{architecture
} eq 'x86_64') {
260 print $fh "[multilib]\n$servers\n";
263 mkdir $self->{rootfs
} || die "unable to create rootfs - $!";
265 $self->logmsg("configured VE $self->{veid}\n");
271 my $veid = $self->{veid
};
273 my $res = { running
=> 0 };
275 $res->{exist
} = 1 if -d
"$self->{rootfs}/usr";
277 my $filename = "/proc/net/unix";
279 # similar test is used by lcxcontainers.c: list_active_containers
280 my $fh = IO
::File-
>new ($filename, "r");
283 while (defined(my $line = <$fh>)) {
284 if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) {
286 if ($path =~ m!^@/\S+/$veid/command$!) {
299 my $veid = $self->{veid
}; # fixme
301 my $vestat = $self->ve_status();
302 if ($vestat->{running
}) {
303 $self->stop_container();
306 rmtree
$self->{rootfs
};
307 unlink $self->{veconffile
};
314 my $veid = $self->{veid
};
315 my $conffile = $self->{veconffile
};
317 $self->logmsg ("initialize VE $veid\n");
319 my $vestat = $self->ve_status();
320 if ($vestat->{running
}) {
321 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
324 rmtree
$self->{rootfs
};
325 mkpath
$self->{rootfs
};
329 my ($self, $cmd, $input) = @_;
331 my $veid = $self->{veid
};
332 my $conffile = $self->{veconffile
};
334 if (ref ($cmd) eq 'ARRAY') {
335 unshift @$cmd, 'lxc-attach', '-n', $veid, '--rcfile', $conffile,'--clear-env', '--';
336 $self->run_command ($cmd, $input);
338 $self->run_command ("lxc-attach -n $veid --rcfile $conffile --clear-env -- $cmd", $input);
343 my ($self, @cmd) = @_;
345 my $veid = $self->{veid
};
346 my $conffile = $self->{veconffile
};
349 my $pid = open2
($reader, "<&STDIN", 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--', @cmd)
350 or die "unable to exec command";
352 while (defined (my $line = <$reader>)) {
353 $self->logmsg ($line);
359 die "ve_exec failed - status $rc\n" if $rc != 0;
363 my ($self, $cmd, $input, $getoutput) = @_;
365 my $reader = IO
::File-
>new();
366 my $writer = IO
::File-
>new();
367 my $error = IO
::File-
>new();
371 my $cmdstr = ref ($cmd) eq 'ARRAY' ?
join (' ', @$cmd) : $cmd;
375 if (ref ($cmd) eq 'ARRAY') {
376 $pid = open3
($writer, $reader, $error, @$cmd) || die $!;
378 $pid = open3
($writer, $reader, $error, $cmdstr) || die $!;
385 if ($orig_pid != $$) {
386 $self->logmsg ("ERROR: command '$cmdstr' failed - fork failed\n");
393 print $writer $input if defined $input;
396 my $select = new IO
::Select
;
397 $select->add ($reader);
398 $select->add ($error);
401 my $logfd = $self->{logfd
};
403 while ($select->count) {
404 my @handles = $select->can_read ();
406 foreach my $h (@handles) {
408 my $count = sysread ($h, $buf, 4096);
409 if (!defined ($count)) {
411 die "command '$cmdstr' failed: $!";
413 $select->remove ($h) if !$count;
417 $res .= $buf if $getoutput;
424 die "command '$cmdstr' failed with exit code $ec\n" if $ec;
429 sub start_container
{
431 my $veid = $self->{veid
};
432 $self->run_command(['lxc-start', '-n', $veid, '-f', $self->{veconffile
}, '/usr/bin/aab_fake_init']);
437 my $veid = $self->{veid
};
438 my $conffile = $self->{veconffile
};
439 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
444 my $root = $self->{rootfs
};
445 return ('/usr/bin/pacman',
447 '--config', $self->{'pacman.conf'},
448 '--cachedir', $self->{pkgcache
},
453 my ($self, $packages) = @_;
454 my $root = $self->{rootfs
};
456 my @pacman = $self->pacman_command();
457 $self->run_command([@pacman, '-Sw', '--', @$packages]);
461 my ($self, $include, $exclude) = @_;
462 my $root = $self->{rootfs
};
464 my @pacman = $self->pacman_command();
466 print "Fetching package database...\n";
467 mkpath
$self->{pkgcache
};
468 mkpath
$self->{pkgdir
};
469 mkpath
"$root/var/lib/pacman";
470 $self->run_command([@pacman, '-Syy']);
472 print "Figuring out what to install...\n";
473 my $incl = { map { $_ => 1 } @{$self->{incl
}} };
474 my $excl = { map { $_ => 1 } @{$self->{excl
}} };
476 foreach my $addinc (@$include) {
477 $incl->{$addinc} = 1;
478 delete $excl->{$addinc};
480 foreach my $addexc (@$exclude) {
481 $excl->{$addexc} = 1;
482 delete $incl->{$addexc};
487 foreach my $inc (keys %$lst) {
489 eval { $group = $self->run_command([@pacman, '-Sqg', $inc], undef, 1); };
493 $lst->{$_} = 1 foreach split(/\s+/, $group);
501 my $packages = [ grep { !$excl->{$_} } keys %$incl ];
503 print "Setting up basic environment...\n";
505 mkpath
"$root/usr/bin";
507 my $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
508 write_file
($data, "$root/etc/fstab", 0644);
510 write_file
("", "$root/etc/resolv.conf", 0644);
511 write_file
("localhost\n", "$root/etc/hostname", 0644);
512 $self->run_command(['install', '-m0755', $fake_init, "$root/usr/bin/aab_fake_init"]);
514 unlink "$root/etc/localtime";
515 symln
'/usr/share/zoneinfo/UTC', "$root/etc/localtime";
517 print "Caching packages...\n";
518 $self->cache_packages($packages);
519 #$self->copy_packages();
521 print "Installing package manager and essentials...\n";
522 # inetutils for 'hostname' for our init
523 $self->run_command([@pacman, '-S', 'pacman', 'inetutils', 'archlinux-keyring']);
525 print "Setting up pacman for installation from cache...\n";
526 my $file = "$root/etc/pacman.d/mirrorlist";
527 my $backup = "${file}.aab_orig";
529 rename_file
($file, $backup);
530 write_file
("Server = file://$PKGDIR\n", $file);
533 print "Populating keyring...\n";
534 $self->populate_keyring();
536 print "Starting container...\n";
537 $self->start_container();
539 print "Installing packages...\n";
540 $self->ve_command(['pacman', '-S', '--needed', '--noconfirm', '--', @$packages]);
543 sub populate_keyring
{
545 my $root = $self->{rootfs
};
547 # devices needed for gnupg to function:
549 '/dev/null' => ['c', '1', '3'],
550 '/dev/random' => ['c', '1', '9'], # fake /dev/random (really urandom)
551 '/dev/urandom' => ['c', '1', '9'],
552 '/dev/tty' => ['c', '5', '0'],
555 my $cleanup_dev = sub {
556 # remove temporary device files
557 unlink "${root}$_" foreach keys %$devs;
559 local $SIG{INT
} = $SIG{TERM
} = $cleanup_dev;
561 # at least /dev/null exists as regular file after installing the filesystem package,
562 # and we want to replace /dev/random, so delete devices first
565 foreach my $dev (keys %$devs) {
566 my ($type, $major, $minor) = @{$devs->{$dev}};
567 system('mknod', "${root}${dev}", $type, $major, $minor);
570 # generate weak master key and populate the keyring
571 system('unshare', '--fork', '--pid', 'chroot', "$root", 'pacman-key', '--init') == 0
572 or die "failed to initialize keyring: $?";
573 system('unshare', '--fork', '--pid', 'chroot', "$root", 'pacman-key', '--populate') == 0
574 or die "failed to populate keyring: $?";
577 # reset to original state
578 system('touch', "$root/dev/null");
582 my ($self, $pkglist) = @_;
584 $self->cache_packages($pkglist);
585 $self->ve_command(['pacman', '-S', '--needed', '--noconfirm', '--', @$pkglist]);
589 my ($self, $filename, $size) = @_;
591 my $config = $self->{config
};
595 $data .= "Name: $config->{name}\n";
596 $data .= "Version: $config->{version}\n";
597 $data .= "Type: lxc\n";
598 $data .= "OS: archlinux\n";
599 $data .= "Section: $config->{section}\n";
600 $data .= "Maintainer: $config->{maintainer}\n";
601 $data .= "Architecture: $config->{architecture}\n";
602 $data .= "Infopage: https://www.archlinux.org\n";
603 $data .= "Installed-Size: $size\n";
606 $data .= "Infopage: $config->{infopage}\n" if $config->{infopage
};
607 $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl
};
608 $data .= "Certified: $config->{certified}\n" if $config->{certified
};
611 $data .= "Description: $config->{headline}\n";
612 $data .= "$config->{description}\n" if $config->{description
};
614 write_file
($data, $filename, 0644);
619 my $rootdir = $self->{rootfs
};
621 print "Stopping container...\n";
622 $self->stop_container();
624 print "Rolling back mirrorlist changes...\n";
625 my $file = "$rootdir/etc/pacman.d/mirrorlist";
627 rename_file
($file.'.aab_orig', $file);
629 print "Removing weak temporary pacman keyring...\n";
630 rmtree
("$rootdir/etc/pacman.d/gnupg");
632 my $sizestr = $self->run_command("du -sm $rootdir", undef, 1);
634 if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) {
637 die "unable to detect size\n";
639 $self->logmsg ("$size MB\n");
641 $self->write_config ("$rootdir/etc/appliance.info", $size);
643 $self->logmsg ("creating final appliance archive\n");
645 my $target = "$self->{targetname}.tar";
649 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
650 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
651 $self->run_command ("gzip $target");
656 my $veid = $self->{veid
};
657 my $conffile = $self->{veconffile
};
659 my $vestat = $self->ve_status();
660 if (!$vestat->{exist
}) {
661 $self->logmsg ("Please create the appliance first (bootstrap)");
665 if (!$vestat->{running
}) {
666 $self->start_container();
669 system ("lxc-attach -n $veid --rcfile $conffile --clear-env");
673 my ($self, $all) = @_;
675 unlink $self->{logfile
};
676 unlink $self->{'pacman.conf'};
679 unlink $self->{veconffile
};
681 rmtree
$self->{pkgcache
} if $all;