]>
git.proxmox.com Git - aab.git/blob - PVE/AAB.pm
15 my @BASE_PACKAGES = qw(base openssh);
16 my @BASE_EXCLUDES = qw(e2fsprogs
26 my $PKGDIR = "/var/cache/pacman/pkg";
28 my ($aablibdir, $fake_init);
30 sub setup_defaults
($) {
33 $fake_init = "$aablibdir/scripts/init.bash";
36 setup_defaults
('/usr/lib/aab');
39 my ($data, $file, $perm) = @_;
41 die "no filename" if !$file;
44 my $fh = IO
::File-
>new ($file, O_WRONLY
| O_CREAT
, $perm) ||
45 die "unable to open file '$file'";
53 copy
($a, $b) or die "failed to copy $a => $b: $!";
58 rename($a, $b) or die "failed to rename $a => $b: $!";
63 symlink($a, $b) or die "failed to symlink $a => $b: $!";
74 my $fd = $self->{logfd
};
83 my $fh = IO
::File-
>new ("<$filename") || return $res;
86 while (defined (my $line = <$fh>)) {
87 next if $line =~ m/^\#/;
88 next if $line =~ m/^\s*$/;
98 if ($rec =~ s/^Description:\s*([^\n]*)(\n\s+.*)*$//si) {
99 $res->{headline
} = $1;
100 chomp $res->{headline
};
103 $res->{description
} = $long;
104 chomp $res->{description
};
105 } elsif ($rec =~ s/^([^:]+):\s*(.*\S)\s*\n//) {
106 my ($key, $value) = (lc ($1), $2);
107 if ($key eq 'source' || $key eq 'mirror') {
108 push @{$res->{$key}}, $value;
110 die "duplicate key '$key'\n" if defined ($res->{$key});
111 $res->{$key} = $value;
114 die "unable to parse config file: $rec";
118 die "unable to parse config file" if $rec;
120 $res->{architecture
} = 'amd64' if $res->{architecture
} eq 'x86_64';
126 my ($class, $config) = @_;
128 $config = read_config
('aab.conf') if !$config;
129 my $version = $config->{version
};
130 die "no 'version' specified\n" if !$version;
131 die "no 'section' specified\n" if !$config->{section
};
132 die "no 'description' specified\n" if !$config->{headline
};
133 die "no 'maintainer' specified\n" if !$config->{maintainer
};
135 my $name = $config->{name
} || die "no 'name' specified\n";
136 $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ ||
137 die "illegal characters in name '$name'\n";
140 if ($name =~ m/^archlinux/) {
141 $targetname = "${name}_${version}_$config->{architecture}";
143 $targetname = "archlinux-${name}_${version}_$config->{architecture}";
146 my $self = { logfile
=> 'logfile',
148 targetname
=> $targetname,
149 incl
=> [@BASE_PACKAGES],
150 excl
=> [@BASE_EXCLUDES],
153 $self->{logfd
} = IO
::File-
>new($self->{logfile
}, O_WRONLY
| O_APPEND
| O_CREAT
)
154 or die "unable to open log file";
158 $self->__allocate_ve();
163 sub __sample_config
{
166 my $arch = $self->{config
}->{architecture
};
170 lxc.include = /usr/share/lxc/config/archlinux.common.conf
171 lxc.uts.name = localhost
172 lxc.rootfs.path = $self->{rootfs}
173 lxc.mount.entry = $self->{pkgcache} $self->{pkgdir} none bind 0 0
181 if (my $fd = IO
::File-
>new(".veid")) {
188 $self->{working_dir
} = getcwd
;
189 $self->{veconffile
} = "$self->{working_dir}/config";
190 $self->{rootfs
} = "$self->{working_dir}/rootfs";
191 $self->{pkgdir
} = "$self->{working_dir}/rootfs/$PKGDIR";
192 $self->{pkgcache
} = "$self->{working_dir}/pkgcache";
193 $self->{'pacman.conf'} = "$self->{working_dir}/pacman.conf";
196 $self->{veid
} = $cid;
202 UUID
::generate
($uuid);
203 UUID
::unparse
($uuid, $uuid_str);
204 $self->{veid
} = $uuid_str;
206 my $fd = IO
::File-
>new (">.veid") ||
207 die "unable to write '.veid'\n";
208 print $fd "$self->{veid}\n";
210 $self->logmsg("allocated VE $self->{veid}\n");
216 my $config = $self->{config
};
218 $self->{logfd
} = IO
::File-
>new($self->{logfile
}, O_WRONLY
| O_TRUNC
| O_CREAT
)
219 or die "unable to open log file";
221 my $cdata = $self->__sample_config();
223 my $fh = IO
::File-
>new($self->{veconffile
}, O_WRONLY
|O_CREAT
|O_EXCL
) ||
224 die "unable to write lxc config file '$self->{veconffile}' - $!";
228 if (!$config->{source
} && !$config->{mirror
}) {
229 die "no sources/mirrors specified";
232 $config->{source
} //= [];
233 $config->{mirror
} //= [];
235 my $servers = "Server = "
236 . join("\nServer = ", @{$config->{source
}}, @{$config->{mirror
}})
239 $fh = IO
::File-
>new($self->{'pacman.conf'}, O_WRONLY
|O_CREAT
|O_EXCL
) ||
240 die "unable to write pacman config file $self->{'pacman.conf'} - $!";
241 my $arch = $config->{architecture
};
242 $arch = 'x86_64' if $arch eq 'amd64';
245 HoldPkg = pacman glibc
258 if ($config->{architecture
} eq 'x86_64') {
259 print $fh "[multilib]\n$servers\n";
262 mkdir $self->{rootfs
} || die "unable to create rootfs - $!";
264 $self->logmsg("configured VE $self->{veid}\n");
270 my $veid = $self->{veid
};
272 my $res = { running
=> 0 };
274 $res->{exist
} = 1 if -d
"$self->{rootfs}/usr";
276 my $filename = "/proc/net/unix";
278 # similar test is used by lcxcontainers.c: list_active_containers
279 my $fh = IO
::File-
>new ($filename, "r");
282 while (defined(my $line = <$fh>)) {
283 if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) {
285 if ($path =~ m!^@/\S+/$veid/command$!) {
298 my $veid = $self->{veid
}; # fixme
300 my $vestat = $self->ve_status();
301 if ($vestat->{running
}) {
302 $self->stop_container();
305 rmtree
$self->{rootfs
};
306 unlink $self->{veconffile
};
313 my $veid = $self->{veid
};
314 my $conffile = $self->{veconffile
};
316 $self->logmsg ("initialize VE $veid\n");
318 my $vestat = $self->ve_status();
319 if ($vestat->{running
}) {
320 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
323 rmtree
$self->{rootfs
};
324 mkpath
$self->{rootfs
};
328 my ($self, $cmd, $input) = @_;
330 my $veid = $self->{veid
};
331 my $conffile = $self->{veconffile
};
333 if (ref ($cmd) eq 'ARRAY') {
334 unshift @$cmd, 'lxc-attach', '-n', $veid, '--rcfile', $conffile,'--clear-env', '--';
335 $self->run_command ($cmd, $input);
337 $self->run_command ("lxc-attach -n $veid --rcfile $conffile --clear-env -- $cmd", $input);
342 my ($self, @cmd) = @_;
344 my $veid = $self->{veid
};
345 my $conffile = $self->{veconffile
};
348 my $pid = open2
($reader, "<&STDIN", 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--', @cmd)
349 or die "unable to exec command";
351 while (defined (my $line = <$reader>)) {
352 $self->logmsg ($line);
358 die "ve_exec failed - status $rc\n" if $rc != 0;
362 my ($self, $cmd, $input, $getoutput) = @_;
364 my $reader = IO
::File-
>new();
365 my $writer = IO
::File-
>new();
366 my $error = IO
::File-
>new();
370 my $cmdstr = ref ($cmd) eq 'ARRAY' ?
join (' ', @$cmd) : $cmd;
374 if (ref ($cmd) eq 'ARRAY') {
375 $pid = open3
($writer, $reader, $error, @$cmd) || die $!;
377 $pid = open3
($writer, $reader, $error, $cmdstr) || die $!;
384 if ($orig_pid != $$) {
385 $self->logmsg ("ERROR: command '$cmdstr' failed - fork failed\n");
392 print $writer $input if defined $input;
395 my $select = new IO
::Select
;
396 $select->add ($reader);
397 $select->add ($error);
400 my $logfd = $self->{logfd
};
402 while ($select->count) {
403 my @handles = $select->can_read ();
405 foreach my $h (@handles) {
407 my $count = sysread ($h, $buf, 4096);
408 if (!defined ($count)) {
410 die "command '$cmdstr' failed: $!";
412 $select->remove ($h) if !$count;
416 $res .= $buf if $getoutput;
423 die "command '$cmdstr' failed with exit code $ec\n" if $ec;
428 sub start_container
{
430 my $veid = $self->{veid
};
431 $self->run_command(['lxc-start', '-n', $veid, '-f', $self->{veconffile
}, '/usr/bin/aab_fake_init']);
436 my $veid = $self->{veid
};
437 my $conffile = $self->{veconffile
};
438 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
443 my $root = $self->{rootfs
};
444 return ('/usr/bin/pacman',
446 '--config', $self->{'pacman.conf'},
447 '--cachedir', $self->{pkgcache
},
452 my ($self, $packages) = @_;
453 my $root = $self->{rootfs
};
455 my @pacman = $self->pacman_command();
456 $self->run_command([@pacman, '-Sw', '--', @$packages]);
460 my ($self, $include, $exclude) = @_;
461 my $root = $self->{rootfs
};
463 my @pacman = $self->pacman_command();
465 print "Fetching package database...\n";
466 mkpath
$self->{pkgcache
};
467 mkpath
$self->{pkgdir
};
468 mkpath
"$root/var/lib/pacman";
469 $self->run_command([@pacman, '-Syy']);
471 print "Figuring out what to install...\n";
472 my $incl = { map { $_ => 1 } @{$self->{incl
}} };
473 my $excl = { map { $_ => 1 } @{$self->{excl
}} };
475 foreach my $addinc (@$include) {
476 $incl->{$addinc} = 1;
477 delete $excl->{$addinc};
479 foreach my $addexc (@$exclude) {
480 $excl->{$addexc} = 1;
481 delete $incl->{$addexc};
486 foreach my $inc (keys %$lst) {
488 eval { $group = $self->run_command([@pacman, '-Sqg', $inc], undef, 1); };
492 $lst->{$_} = 1 foreach split(/\s+/, $group);
500 my $packages = [ grep { !$excl->{$_} } keys %$incl ];
502 print "Setting up basic environment...\n";
504 mkpath
"$root/usr/bin";
506 my $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
507 write_file
($data, "$root/etc/fstab", 0644);
509 write_file
("", "$root/etc/resolv.conf", 0644);
510 write_file
("localhost\n", "$root/etc/hostname", 0644);
511 $self->run_command(['install', '-m0755', $fake_init, "$root/usr/bin/aab_fake_init"]);
513 unlink "$root/etc/localtime";
514 symln
'/usr/share/zoneinfo/UTC', "$root/etc/localtime";
516 print "Caching packages...\n";
517 $self->cache_packages($packages);
518 #$self->copy_packages();
520 print "Installing package manager and essentials...\n";
521 # inetutils for 'hostname' for our init
522 $self->run_command([@pacman, '-S', 'pacman', 'inetutils', 'archlinux-keyring']);
524 print "Setting up pacman for installation from cache...\n";
525 my $file = "$root/etc/pacman.d/mirrorlist";
526 my $backup = "${file}.aab_orig";
528 rename_file
($file, $backup);
529 write_file
("Server = file://$PKGDIR\n", $file);
532 print "Populating keyring...\n";
533 $self->populate_keyring();
535 print "Starting container...\n";
536 $self->start_container();
538 print "Installing packages...\n";
539 $self->ve_command(['pacman', '-S', '--needed', '--noconfirm', '--', @$packages]);
542 sub populate_keyring
{
544 my $root = $self->{rootfs
};
546 # devices needed for gnupg to function:
548 '/dev/null' => ['c', '1', '3'],
549 '/dev/random' => ['c', '1', '9'], # fake /dev/random (really urandom)
550 '/dev/urandom' => ['c', '1', '9'],
551 '/dev/tty' => ['c', '5', '0'],
554 my $cleanup_dev = sub {
555 # remove temporary device files
556 unlink "${root}$_" foreach keys %$devs;
558 local $SIG{INT
} = $SIG{TERM
} = $cleanup_dev;
560 # at least /dev/null exists as regular file after installing the filesystem package,
561 # and we want to replace /dev/random, so delete devices first
564 foreach my $dev (keys %$devs) {
565 my ($type, $major, $minor) = @{$devs->{$dev}};
566 system('mknod', "${root}${dev}", $type, $major, $minor);
569 # generate weak master key and populate the keyring
570 system('unshare', '--fork', '--pid', 'chroot', "$root", 'pacman-key', '--init') == 0
571 or die "failed to initialize keyring: $?";
572 system('unshare', '--fork', '--pid', 'chroot', "$root", 'pacman-key', '--populate') == 0
573 or die "failed to populate keyring: $?";
576 # reset to original state
577 system('touch', "$root/dev/null");
581 my ($self, $pkglist) = @_;
583 $self->cache_packages($pkglist);
584 $self->ve_command(['pacman', '-S', '--needed', '--noconfirm', '--', @$pkglist]);
588 my ($self, $filename, $size) = @_;
590 my $config = $self->{config
};
594 $data .= "Name: $config->{name}\n";
595 $data .= "Version: $config->{version}\n";
596 $data .= "Type: lxc\n";
597 $data .= "OS: archlinux\n";
598 $data .= "Section: $config->{section}\n";
599 $data .= "Maintainer: $config->{maintainer}\n";
600 $data .= "Architecture: $config->{architecture}\n";
601 $data .= "Infopage: https://www.archlinux.org\n";
602 $data .= "Installed-Size: $size\n";
605 $data .= "Infopage: $config->{infopage}\n" if $config->{infopage
};
606 $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl
};
607 $data .= "Certified: $config->{certified}\n" if $config->{certified
};
610 $data .= "Description: $config->{headline}\n";
611 $data .= "$config->{description}\n" if $config->{description
};
613 write_file
($data, $filename, 0644);
618 my $rootdir = $self->{rootfs
};
620 print "Stopping container...\n";
621 $self->stop_container();
623 print "Rolling back mirrorlist changes...\n";
624 my $file = "$rootdir/etc/pacman.d/mirrorlist";
626 rename_file
($file.'.aab_orig', $file);
628 print "Removing weak temporary pacman keyring...\n";
629 rmtree
("$rootdir/etc/pacman.d/gnupg");
631 my $sizestr = $self->run_command("du -sm $rootdir", undef, 1);
633 if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) {
636 die "unable to detect size\n";
638 $self->logmsg ("$size MB\n");
640 $self->write_config ("$rootdir/etc/appliance.info", $size);
642 $self->logmsg ("creating final appliance archive\n");
644 my $target = "$self->{targetname}.tar";
648 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
649 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
650 $self->run_command ("gzip $target");
655 my $veid = $self->{veid
};
656 my $conffile = $self->{veconffile
};
658 my $vestat = $self->ve_status();
659 if (!$vestat->{exist
}) {
660 $self->logmsg ("Please create the appliance first (bootstrap)");
664 if (!$vestat->{running
}) {
665 $self->start_container();
668 system ("lxc-attach -n $veid --rcfile $conffile --clear-env");
672 my ($self, $all) = @_;
674 unlink $self->{logfile
};
675 unlink $self->{'pacman.conf'};
678 rmtree
$self->{pkgcache
} if $all;