]>
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 $self->logmsg("configured VE $self->{veid}\n");
269 my $veid = $self->{veid
};
271 my $res = { running
=> 0 };
273 $res->{exist
} = 1 if -d
"$self->{rootfs}/usr";
275 my $filename = "/proc/net/unix";
277 # similar test is used by lcxcontainers.c: list_active_containers
278 my $fh = IO
::File-
>new ($filename, "r");
281 while (defined(my $line = <$fh>)) {
282 if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) {
284 if ($path =~ m!^@/\S+/$veid/command$!) {
297 my $veid = $self->{veid
}; # fixme
299 my $vestat = $self->ve_status();
300 if ($vestat->{running
}) {
301 $self->stop_container();
304 rmtree
$self->{rootfs
};
305 unlink $self->{veconffile
};
312 my $veid = $self->{veid
};
313 my $conffile = $self->{veconffile
};
315 $self->logmsg ("initialize VE $veid\n");
317 my $vestat = $self->ve_status();
318 if ($vestat->{running
}) {
319 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
322 rmtree
$self->{rootfs
};
323 mkpath
"$self->{rootfs}/dev";
327 my ($self, $cmd, $input) = @_;
329 my $veid = $self->{veid
};
330 my $conffile = $self->{veconffile
};
332 if (ref ($cmd) eq 'ARRAY') {
333 unshift @$cmd, 'lxc-attach', '-n', $veid, '--rcfile', $conffile,'--clear-env', '--';
334 $self->run_command ($cmd, $input);
336 $self->run_command ("lxc-attach -n $veid --rcfile $conffile --clear-env -- $cmd", $input);
341 my ($self, @cmd) = @_;
343 my $veid = $self->{veid
};
344 my $conffile = $self->{veconffile
};
347 my $pid = open2
($reader, "<&STDIN", 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--', @cmd)
348 or die "unable to exec command";
350 while (defined (my $line = <$reader>)) {
351 $self->logmsg ($line);
357 die "ve_exec failed - status $rc\n" if $rc != 0;
361 my ($self, $cmd, $input, $getoutput) = @_;
363 my $reader = IO
::File-
>new();
364 my $writer = IO
::File-
>new();
365 my $error = IO
::File-
>new();
369 my $cmdstr = ref ($cmd) eq 'ARRAY' ?
join (' ', @$cmd) : $cmd;
373 if (ref ($cmd) eq 'ARRAY') {
374 $pid = open3
($writer, $reader, $error, @$cmd) || die $!;
376 $pid = open3
($writer, $reader, $error, $cmdstr) || die $!;
383 if ($orig_pid != $$) {
384 $self->logmsg ("ERROR: command '$cmdstr' failed - fork failed\n");
391 print $writer $input if defined $input;
394 my $select = new IO
::Select
;
395 $select->add ($reader);
396 $select->add ($error);
399 my $logfd = $self->{logfd
};
401 while ($select->count) {
402 my @handles = $select->can_read ();
404 foreach my $h (@handles) {
406 my $count = sysread ($h, $buf, 4096);
407 if (!defined ($count)) {
409 die "command '$cmdstr' failed: $!";
411 $select->remove ($h) if !$count;
415 $res .= $buf if $getoutput;
422 die "command '$cmdstr' failed with exit code $ec\n" if $ec;
427 sub start_container
{
429 my $veid = $self->{veid
};
430 $self->run_command(['lxc-start', '-n', $veid, '-f', $self->{veconffile
}, '/usr/bin/aab_fake_init']);
435 my $veid = $self->{veid
};
436 my $conffile = $self->{veconffile
};
437 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
442 my $root = $self->{rootfs
};
443 return ('/usr/bin/pacman',
445 '--config', $self->{'pacman.conf'},
446 '--cachedir', $self->{pkgcache
},
451 my ($self, $packages) = @_;
452 my $root = $self->{rootfs
};
454 my @pacman = $self->pacman_command();
455 $self->run_command([@pacman, '-Sw', '--', @$packages]);
459 my ($self, $include, $exclude) = @_;
460 my $root = $self->{rootfs
};
462 my @pacman = $self->pacman_command();
464 print "Fetching package database...\n";
465 mkpath
$self->{pkgcache
};
466 mkpath
$self->{pkgdir
};
467 mkpath
"$root/var/lib/pacman";
468 $self->run_command([@pacman, '-Syy']);
470 print "Figuring out what to install...\n";
471 my $incl = { map { $_ => 1 } @{$self->{incl
}} };
472 my $excl = { map { $_ => 1 } @{$self->{excl
}} };
474 foreach my $addinc (@$include) {
475 $incl->{$addinc} = 1;
476 delete $excl->{$addinc};
478 foreach my $addexc (@$exclude) {
479 $excl->{$addexc} = 1;
480 delete $incl->{$addexc};
485 foreach my $inc (keys %$lst) {
487 eval { $group = $self->run_command([@pacman, '-Sqg', $inc], undef, 1); };
491 $lst->{$_} = 1 foreach split(/\s+/, $group);
499 my $packages = [ grep { !$excl->{$_} } keys %$incl ];
501 print "Setting up basic environment...\n";
503 mkpath
"$root/usr/bin";
505 my $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
506 write_file
($data, "$root/etc/fstab", 0644);
508 write_file
("", "$root/etc/resolv.conf", 0644);
509 write_file
("localhost\n", "$root/etc/hostname", 0644);
510 $self->run_command(['install', '-m0755', $fake_init, "$root/usr/bin/aab_fake_init"]);
512 unlink "$root/etc/localtime";
513 symln
'/usr/share/zoneinfo/UTC', "$root/etc/localtime";
515 print "Caching packages...\n";
516 $self->cache_packages($packages);
517 #$self->copy_packages();
519 print "Creating device nodes for package manager...\n";
522 print "Installing package manager and essentials...\n";
523 # inetutils for 'hostname' for our init
524 $self->run_command([@pacman, '-S', 'pacman', 'inetutils', 'archlinux-keyring']);
526 print "Setting up pacman for installation from cache...\n";
527 my $file = "$root/etc/pacman.d/mirrorlist";
528 my $backup = "${file}.aab_orig";
530 rename_file
($file, $backup);
531 write_file
("Server = file://$PKGDIR\n", $file);
534 print "Populating keyring...\n";
535 $self->populate_keyring();
537 print "Removing device nodes...\n";
538 $self->cleanup_dev();
540 print "Starting container...\n";
541 $self->start_container();
543 print "Installing packages...\n";
544 $self->ve_command(['pacman', '-S', '--needed', '--noconfirm', '--', @$packages]);
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'],
557 my $root = $self->{rootfs
};
559 # remove temporary device files
560 unlink "${root}$_" foreach keys %$devs;
565 my $root = $self->{rootfs
};
567 local $SIG{INT
} = $SIG{TERM
} = sub { $self->cleanup_dev; };
569 # we want to replace /dev/random, so delete devices first
570 $self->cleanup_dev();
572 foreach my $dev (keys %$devs) {
573 my ($type, $major, $minor) = @{$devs->{$dev}};
574 system('mknod', "${root}${dev}", $type, $major, $minor);
578 sub populate_keyring
{
580 my $root = $self->{rootfs
};
582 # generate weak master key and populate the keyring
583 system('unshare', '--fork', '--pid', 'chroot', "$root", 'pacman-key', '--init') == 0
584 or die "failed to initialize keyring: $?";
585 system('unshare', '--fork', '--pid', 'chroot', "$root", 'pacman-key', '--populate') == 0
586 or die "failed to populate keyring: $?";
591 my ($self, $pkglist) = @_;
593 $self->cache_packages($pkglist);
594 $self->ve_command(['pacman', '-S', '--needed', '--noconfirm', '--', @$pkglist]);
598 my ($self, $filename, $size) = @_;
600 my $config = $self->{config
};
604 $data .= "Name: $config->{name}\n";
605 $data .= "Version: $config->{version}\n";
606 $data .= "Type: lxc\n";
607 $data .= "OS: archlinux\n";
608 $data .= "Section: $config->{section}\n";
609 $data .= "Maintainer: $config->{maintainer}\n";
610 $data .= "Architecture: $config->{architecture}\n";
611 $data .= "Infopage: https://www.archlinux.org\n";
612 $data .= "Installed-Size: $size\n";
615 $data .= "Infopage: $config->{infopage}\n" if $config->{infopage
};
616 $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl
};
617 $data .= "Certified: $config->{certified}\n" if $config->{certified
};
620 $data .= "Description: $config->{headline}\n";
621 $data .= "$config->{description}\n" if $config->{description
};
623 write_file
($data, $filename, 0644);
628 my $rootdir = $self->{rootfs
};
630 print "Stopping container...\n";
631 $self->stop_container();
633 print "Rolling back mirrorlist changes...\n";
634 my $file = "$rootdir/etc/pacman.d/mirrorlist";
636 rename_file
($file.'.aab_orig', $file);
638 print "Removing weak temporary pacman keyring...\n";
639 rmtree
("$rootdir/etc/pacman.d/gnupg");
641 my $sizestr = $self->run_command("du -sm $rootdir", undef, 1);
643 if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) {
646 die "unable to detect size\n";
648 $self->logmsg ("$size MB\n");
650 $self->write_config ("$rootdir/etc/appliance.info", $size);
652 $self->logmsg ("creating final appliance archive\n");
654 my $target = "$self->{targetname}.tar";
658 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
659 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
660 $self->run_command ("gzip $target");
665 my $veid = $self->{veid
};
666 my $conffile = $self->{veconffile
};
668 my $vestat = $self->ve_status();
669 if (!$vestat->{exist
}) {
670 $self->logmsg ("Please create the appliance first (bootstrap)");
674 if (!$vestat->{running
}) {
675 $self->start_container();
678 system ("lxc-attach -n $veid --rcfile $conffile --clear-env");
682 my ($self, $all) = @_;
684 unlink $self->{logfile
};
685 unlink $self->{'pacman.conf'};
688 unlink $self->{veconffile
};
690 rmtree
$self->{pkgcache
} if $all;