]>
Commit | Line | Data |
---|---|---|
7b25f331 WB |
1 | package PVE::AAB; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use File::Path; | |
7 | use File::Copy; | |
8 | use IO::File; | |
9 | use IO::Select; | |
7c20fd82 | 10 | use IPC::Open2; |
7b25f331 WB |
11 | use IPC::Open3; |
12 | use UUID; | |
13 | use Cwd; | |
b36f9145 | 14 | my @BASE_PACKAGES = qw(base openssh vi nano); |
6bd339d3 TL |
15 | my @BASE_EXCLUDES = qw( |
16 | e2fsprogs | |
17 | jfsutils | |
18 | linux | |
19 | linux-firmware | |
20 | lvm2 | |
21 | mdadm | |
22 | netctl | |
23 | pcmciautils | |
24 | reiserfsprogs | |
25 | xfsprogs | |
26 | ); | |
7b25f331 WB |
27 | |
28 | my $PKGDIR = "/var/cache/pacman/pkg"; | |
29 | ||
30 | my ($aablibdir, $fake_init); | |
31 | ||
32 | sub setup_defaults($) { | |
33 | my ($dir) = @_; | |
34 | $aablibdir = $dir; | |
35 | $fake_init = "$aablibdir/scripts/init.bash"; | |
36 | } | |
37 | ||
38 | setup_defaults('/usr/lib/aab'); | |
39 | ||
40 | sub write_file { | |
41 | my ($data, $file, $perm) = @_; | |
42 | ||
43 | die "no filename" if !$file; | |
44 | unlink $file; | |
45 | ||
46 | my $fh = IO::File->new ($file, O_WRONLY | O_CREAT, $perm) || | |
47 | die "unable to open file '$file'"; | |
48 | ||
49 | print $fh $data; | |
50 | $fh->close; | |
51 | } | |
52 | ||
53 | sub copy_file { | |
54 | my ($a, $b) = @_; | |
55 | copy($a, $b) or die "failed to copy $a => $b: $!"; | |
56 | } | |
57 | ||
58 | sub rename_file { | |
59 | my ($a, $b) = @_; | |
60 | rename($a, $b) or die "failed to rename $a => $b: $!"; | |
61 | } | |
62 | ||
63 | sub symln { | |
64 | my ($a, $b) = @_; | |
65 | symlink($a, $b) or die "failed to symlink $a => $b: $!"; | |
66 | } | |
67 | ||
68 | sub logmsg { | |
69 | my $self = shift; | |
70 | print STDERR @_; | |
71 | $self->writelog (@_); | |
72 | } | |
73 | ||
74 | sub writelog { | |
75 | my $self = shift; | |
76 | my $fd = $self->{logfd}; | |
77 | print $fd @_; | |
78 | } | |
79 | ||
80 | sub read_config { | |
81 | my ($filename) = @_; | |
82 | ||
83 | my $res = {}; | |
84 | ||
85 | my $fh = IO::File->new ("<$filename") || return $res; | |
86 | my $rec = ''; | |
87 | ||
88 | while (defined (my $line = <$fh>)) { | |
89 | next if $line =~ m/^\#/; | |
90 | next if $line =~ m/^\s*$/; | |
91 | $rec .= $line; | |
92 | }; | |
93 | ||
94 | close ($fh); | |
95 | ||
96 | chomp $rec; | |
97 | $rec .= "\n"; | |
98 | ||
99 | while ($rec) { | |
100 | if ($rec =~ s/^Description:\s*([^\n]*)(\n\s+.*)*$//si) { | |
101 | $res->{headline} = $1; | |
102 | chomp $res->{headline}; | |
103 | my $long = $2; | |
104 | $long =~ s/^\s+/ /; | |
105 | $res->{description} = $long; | |
b8f914c1 | 106 | chomp $res->{description}; |
7b25f331 WB |
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; | |
111 | } else { | |
112 | die "duplicate key '$key'\n" if defined ($res->{$key}); | |
113 | $res->{$key} = $value; | |
114 | } | |
115 | } else { | |
116 | die "unable to parse config file: $rec"; | |
117 | } | |
118 | } | |
119 | ||
120 | die "unable to parse config file" if $rec; | |
121 | ||
0cff4ef1 WB |
122 | $res->{architecture} = 'amd64' if $res->{architecture} eq 'x86_64'; |
123 | ||
7b25f331 WB |
124 | return $res; |
125 | } | |
126 | ||
127 | sub new { | |
128 | my ($class, $config) = @_; | |
129 | ||
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}; | |
136 | ||
137 | my $name = $config->{name} || die "no 'name' specified\n"; | |
b8f914c1 | 138 | $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ || |
7b25f331 WB |
139 | die "illegal characters in name '$name'\n"; |
140 | ||
141 | my $targetname; | |
142 | if ($name =~ m/^archlinux/) { | |
143 | $targetname = "${name}_${version}_$config->{architecture}"; | |
144 | } else { | |
145 | $targetname = "archlinux-${name}_${version}_$config->{architecture}"; | |
146 | } | |
147 | ||
148 | my $self = { logfile => 'logfile', | |
149 | config => $config, | |
150 | targetname => $targetname, | |
151 | incl => [@BASE_PACKAGES], | |
152 | excl => [@BASE_EXCLUDES], | |
153 | }; | |
154 | ||
155 | $self->{logfd} = IO::File->new($self->{logfile}, O_WRONLY | O_APPEND | O_CREAT) | |
156 | or die "unable to open log file"; | |
157 | ||
158 | bless $self, $class; | |
159 | ||
160 | $self->__allocate_ve(); | |
161 | ||
162 | return $self; | |
163 | } | |
164 | ||
165 | sub __sample_config { | |
166 | my ($self) = @_; | |
167 | ||
168 | my $arch = $self->{config}->{architecture}; | |
169 | ||
170 | return <<"CFG"; | |
171 | lxc.arch = $arch | |
172 | lxc.include = /usr/share/lxc/config/archlinux.common.conf | |
87fbfb3a TL |
173 | lxc.uts.name = localhost |
174 | lxc.rootfs.path = $self->{rootfs} | |
7b25f331 WB |
175 | lxc.mount.entry = $self->{pkgcache} $self->{pkgdir} none bind 0 0 |
176 | CFG | |
177 | } | |
178 | ||
179 | sub __allocate_ve { | |
180 | my ($self) = @_; | |
181 | ||
182 | my $cid; | |
183 | if (my $fd = IO::File->new(".veid")) { | |
184 | $cid = <$fd>; | |
185 | chomp $cid; | |
186 | close ($fd); | |
187 | } | |
188 | ||
189 | ||
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"; | |
196 | ||
197 | if ($cid) { | |
198 | $self->{veid} = $cid; | |
199 | return $cid; | |
200 | } | |
201 | ||
202 | my $uuid; | |
203 | my $uuid_str; | |
204 | UUID::generate($uuid); | |
205 | UUID::unparse($uuid, $uuid_str); | |
206 | $self->{veid} = $uuid_str; | |
207 | ||
208 | my $fd = IO::File->new (">.veid") || | |
209 | die "unable to write '.veid'\n"; | |
210 | print $fd "$self->{veid}\n"; | |
211 | close ($fd); | |
212 | $self->logmsg("allocated VE $self->{veid}\n"); | |
213 | } | |
214 | ||
215 | sub initialize { | |
216 | my ($self) = @_; | |
217 | ||
218 | my $config = $self->{config}; | |
219 | ||
220 | $self->{logfd} = IO::File->new($self->{logfile}, O_WRONLY | O_TRUNC | O_CREAT) | |
221 | or die "unable to open log file"; | |
222 | ||
223 | my $cdata = $self->__sample_config(); | |
224 | ||
225 | my $fh = IO::File->new($self->{veconffile}, O_WRONLY|O_CREAT|O_EXCL) || | |
226 | die "unable to write lxc config file '$self->{veconffile}' - $!"; | |
227 | print $fh $cdata; | |
228 | close ($fh); | |
229 | ||
230 | if (!$config->{source} && !$config->{mirror}) { | |
231 | die "no sources/mirrors specified"; | |
232 | } | |
233 | ||
ae71b49f TL |
234 | $self->write_pacman_conf(); |
235 | ||
236 | $self->logmsg("configured VE $self->{veid}\n"); | |
237 | } | |
238 | ||
239 | sub write_pacman_conf { | |
240 | my ($self) = @_; | |
241 | ||
242 | my $config = $self->{config}; | |
243 | ||
7b25f331 WB |
244 | $config->{source} //= []; |
245 | $config->{mirror} //= []; | |
246 | ||
ae71b49f TL |
247 | my $servers = "Server = ".join("\nServer = ", @{$config->{source}}, @{$config->{mirror}}) ."\n"; |
248 | ||
249 | my $fh = IO::File->new($self->{'pacman.conf'}, O_WRONLY | O_CREAT | O_EXCL) | |
250 | or die "unable to write pacman config file $self->{'pacman.conf'} - $!"; | |
7b25f331 | 251 | |
0cff4ef1 WB |
252 | my $arch = $config->{architecture}; |
253 | $arch = 'x86_64' if $arch eq 'amd64'; | |
ae71b49f | 254 | |
7b25f331 WB |
255 | print $fh <<"EOF"; |
256 | [options] | |
257 | HoldPkg = pacman glibc | |
0cff4ef1 | 258 | Architecture = $arch |
7b25f331 WB |
259 | CheckSpace |
260 | SigLevel = Never | |
261 | ||
262 | [core] | |
263 | $servers | |
264 | [extra] | |
265 | $servers | |
266 | [community] | |
267 | $servers | |
7b25f331 WB |
268 | EOF |
269 | ||
ae71b49f | 270 | print $fh "[multilib]\n$servers\n" if $config->{architecture} eq 'x86_64'; |
4eaaed91 | 271 | |
ae71b49f | 272 | close($fh); |
7b25f331 WB |
273 | } |
274 | ||
275 | sub ve_status { | |
276 | my ($self) = @_; | |
277 | ||
278 | my $veid = $self->{veid}; | |
279 | ||
280 | my $res = { running => 0 }; | |
281 | ||
282 | $res->{exist} = 1 if -d "$self->{rootfs}/usr"; | |
283 | ||
284 | my $filename = "/proc/net/unix"; | |
285 | ||
286 | # similar test is used by lcxcontainers.c: list_active_containers | |
287 | my $fh = IO::File->new ($filename, "r"); | |
288 | return $res if !$fh; | |
289 | ||
290 | while (defined(my $line = <$fh>)) { | |
291 | if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) { | |
292 | my $path = $1; | |
293 | if ($path =~ m!^@/\S+/$veid/command$!) { | |
294 | $res->{running} = 1; | |
295 | } | |
296 | } | |
297 | } | |
298 | close($fh); | |
b8f914c1 | 299 | |
7b25f331 WB |
300 | return $res; |
301 | } | |
302 | ||
303 | sub ve_destroy { | |
304 | my ($self) = @_; | |
305 | ||
306 | my $veid = $self->{veid}; # fixme | |
307 | ||
308 | my $vestat = $self->ve_status(); | |
309 | if ($vestat->{running}) { | |
310 | $self->stop_container(); | |
311 | } | |
312 | ||
313 | rmtree $self->{rootfs}; | |
314 | unlink $self->{veconffile}; | |
315 | } | |
316 | ||
317 | sub ve_init { | |
318 | my ($self) = @_; | |
319 | ||
320 | ||
321 | my $veid = $self->{veid}; | |
d43d058d | 322 | my $conffile = $self->{veconffile}; |
7b25f331 WB |
323 | |
324 | $self->logmsg ("initialize VE $veid\n"); | |
325 | ||
326 | my $vestat = $self->ve_status(); | |
327 | if ($vestat->{running}) { | |
d43d058d | 328 | $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill"); |
b8f914c1 | 329 | } |
7b25f331 WB |
330 | |
331 | rmtree $self->{rootfs}; | |
01756eba | 332 | mkpath "$self->{rootfs}/dev"; |
7b25f331 WB |
333 | } |
334 | ||
335 | sub ve_command { | |
336 | my ($self, $cmd, $input) = @_; | |
337 | ||
338 | my $veid = $self->{veid}; | |
d43d058d | 339 | my $conffile = $self->{veconffile}; |
7b25f331 WB |
340 | |
341 | if (ref ($cmd) eq 'ARRAY') { | |
d43d058d | 342 | unshift @$cmd, 'lxc-attach', '-n', $veid, '--rcfile', $conffile,'--clear-env', '--'; |
7b25f331 WB |
343 | $self->run_command ($cmd, $input); |
344 | } else { | |
d43d058d | 345 | $self->run_command ("lxc-attach -n $veid --rcfile $conffile --clear-env -- $cmd", $input); |
7b25f331 WB |
346 | } |
347 | } | |
348 | ||
349 | sub ve_exec { | |
350 | my ($self, @cmd) = @_; | |
351 | ||
352 | my $veid = $self->{veid}; | |
d43d058d | 353 | my $conffile = $self->{veconffile}; |
7b25f331 WB |
354 | |
355 | my $reader; | |
d43d058d | 356 | my $pid = open2($reader, "<&STDIN", 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--', @cmd) |
7b25f331 WB |
357 | or die "unable to exec command"; |
358 | ||
359 | while (defined (my $line = <$reader>)) { | |
360 | $self->logmsg ($line); | |
361 | } | |
362 | ||
363 | waitpid ($pid, 0); | |
364 | my $rc = $? >> 8; | |
365 | ||
366 | die "ve_exec failed - status $rc\n" if $rc != 0; | |
367 | } | |
368 | ||
369 | sub run_command { | |
03483500 | 370 | my ($self, $cmd, $input, $getoutput, $noerr) = @_; |
7b25f331 WB |
371 | |
372 | my $reader = IO::File->new(); | |
373 | my $writer = IO::File->new(); | |
374 | my $error = IO::File->new(); | |
375 | ||
376 | my $orig_pid = $$; | |
377 | ||
378 | my $cmdstr = ref ($cmd) eq 'ARRAY' ? join (' ', @$cmd) : $cmd; | |
379 | ||
380 | my $pid; | |
381 | eval { | |
382 | if (ref ($cmd) eq 'ARRAY') { | |
383 | $pid = open3 ($writer, $reader, $error, @$cmd) || die $!; | |
384 | } else { | |
385 | $pid = open3 ($writer, $reader, $error, $cmdstr) || die $!; | |
386 | } | |
387 | }; | |
388 | ||
389 | my $err = $@; | |
390 | ||
391 | # catch exec errors | |
392 | if ($orig_pid != $$) { | |
393 | $self->logmsg ("ERROR: command '$cmdstr' failed - fork failed\n"); | |
b8f914c1 OB |
394 | POSIX::_exit (1); |
395 | kill ('KILL', $$); | |
7b25f331 WB |
396 | } |
397 | ||
398 | die $err if $err; | |
399 | ||
400 | print $writer $input if defined $input; | |
401 | close $writer; | |
402 | ||
403 | my $select = new IO::Select; | |
404 | $select->add ($reader); | |
405 | $select->add ($error); | |
406 | ||
407 | my $res = ''; | |
408 | my $logfd = $self->{logfd}; | |
409 | ||
410 | while ($select->count) { | |
411 | my @handles = $select->can_read (); | |
412 | ||
413 | foreach my $h (@handles) { | |
414 | my $buf = ''; | |
415 | my $count = sysread ($h, $buf, 4096); | |
416 | if (!defined ($count)) { | |
417 | waitpid ($pid, 0); | |
418 | die "command '$cmdstr' failed: $!"; | |
419 | } | |
420 | $select->remove ($h) if !$count; | |
421 | ||
422 | print $logfd $buf; | |
423 | ||
424 | $res .= $buf if $getoutput; | |
425 | } | |
426 | } | |
427 | ||
428 | waitpid ($pid, 0); | |
429 | my $ec = ($? >> 8); | |
430 | ||
03483500 | 431 | die "command '$cmdstr' failed with exit code $ec\n" if $ec && !$noerr; |
7b25f331 | 432 | |
03483500 | 433 | return wantarray ? ($res, $ec) : $res; |
7b25f331 WB |
434 | } |
435 | ||
436 | sub start_container { | |
437 | my ($self) = @_; | |
438 | my $veid = $self->{veid}; | |
439 | $self->run_command(['lxc-start', '-n', $veid, '-f', $self->{veconffile}, '/usr/bin/aab_fake_init']); | |
440 | } | |
441 | ||
442 | sub stop_container { | |
443 | my ($self) = @_; | |
444 | my $veid = $self->{veid}; | |
d43d058d WB |
445 | my $conffile = $self->{veconffile}; |
446 | $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill"); | |
7b25f331 WB |
447 | } |
448 | ||
449 | sub pacman_command { | |
450 | my ($self) = @_; | |
451 | my $root = $self->{rootfs}; | |
452 | return ('/usr/bin/pacman', | |
453 | '--root', $root, | |
5f96733f | 454 | '--config', $self->{'pacman.conf'}, |
7b25f331 WB |
455 | '--cachedir', $self->{pkgcache}, |
456 | '--noconfirm'); | |
457 | } | |
458 | ||
459 | sub cache_packages { | |
460 | my ($self, $packages) = @_; | |
461 | my $root = $self->{rootfs}; | |
462 | ||
463 | my @pacman = $self->pacman_command(); | |
464 | $self->run_command([@pacman, '-Sw', '--', @$packages]); | |
465 | } | |
466 | ||
2d2bee91 TL |
467 | sub mask_systemd_unit { |
468 | my ($self, $unit) = @_; | |
469 | my $root = $self->{rootfs}; | |
470 | symln '/dev/null', "$root/etc/systemd/system/$unit"; | |
471 | } | |
472 | ||
7b25f331 WB |
473 | sub bootstrap { |
474 | my ($self, $include, $exclude) = @_; | |
475 | my $root = $self->{rootfs}; | |
476 | ||
477 | my @pacman = $self->pacman_command(); | |
478 | ||
479 | print "Fetching package database...\n"; | |
480 | mkpath $self->{pkgcache}; | |
481 | mkpath $self->{pkgdir}; | |
482 | mkpath "$root/var/lib/pacman"; | |
b8f914c1 | 483 | $self->run_command([@pacman, '-Syy']); |
7b25f331 WB |
484 | |
485 | print "Figuring out what to install...\n"; | |
486 | my $incl = { map { $_ => 1 } @{$self->{incl}} }; | |
487 | my $excl = { map { $_ => 1 } @{$self->{excl}} }; | |
488 | ||
489 | foreach my $addinc (@$include) { | |
490 | $incl->{$addinc} = 1; | |
491 | delete $excl->{$addinc}; | |
492 | } | |
493 | foreach my $addexc (@$exclude) { | |
494 | $excl->{$addexc} = 1; | |
495 | delete $incl->{$addexc}; | |
496 | } | |
497 | ||
498 | my $expand = sub { | |
499 | my ($lst) = @_; | |
500 | foreach my $inc (keys %$lst) { | |
501 | my $group; | |
502 | eval { $group = $self->run_command([@pacman, '-Sqg', $inc], undef, 1); }; | |
503 | if ($group && !$@) { | |
504 | # add the group | |
505 | delete $lst->{$inc}; | |
506 | $lst->{$_} = 1 foreach split(/\s+/, $group); | |
507 | } | |
508 | } | |
509 | }; | |
510 | ||
511 | $expand->($incl); | |
512 | $expand->($excl); | |
513 | ||
514 | my $packages = [ grep { !$excl->{$_} } keys %$incl ]; | |
515 | ||
516 | print "Setting up basic environment...\n"; | |
517 | mkpath "$root/etc"; | |
518 | mkpath "$root/usr/bin"; | |
519 | ||
520 | my $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n"; | |
521 | write_file ($data, "$root/etc/fstab", 0644); | |
522 | ||
523 | write_file ("", "$root/etc/resolv.conf", 0644); | |
524 | write_file("localhost\n", "$root/etc/hostname", 0644); | |
525 | $self->run_command(['install', '-m0755', $fake_init, "$root/usr/bin/aab_fake_init"]); | |
526 | ||
527 | unlink "$root/etc/localtime"; | |
528 | symln '/usr/share/zoneinfo/UTC', "$root/etc/localtime"; | |
529 | ||
530 | print "Caching packages...\n"; | |
531 | $self->cache_packages($packages); | |
532 | #$self->copy_packages(); | |
533 | ||
01756eba SI |
534 | print "Creating device nodes for package manager...\n"; |
535 | $self->create_dev(); | |
536 | ||
7b25f331 WB |
537 | print "Installing package manager and essentials...\n"; |
538 | # inetutils for 'hostname' for our init | |
539 | $self->run_command([@pacman, '-S', 'pacman', 'inetutils', 'archlinux-keyring']); | |
540 | ||
541 | print "Setting up pacman for installation from cache...\n"; | |
542 | my $file = "$root/etc/pacman.d/mirrorlist"; | |
543 | my $backup = "${file}.aab_orig"; | |
544 | if (!-f $backup) { | |
545 | rename_file($file, $backup); | |
546 | write_file("Server = file://$PKGDIR\n", $file); | |
547 | } | |
548 | ||
549 | print "Populating keyring...\n"; | |
766f0fa3 | 550 | $self->populate_keyring(); |
7b25f331 | 551 | |
01756eba SI |
552 | print "Removing device nodes...\n"; |
553 | $self->cleanup_dev(); | |
554 | ||
7b25f331 WB |
555 | print "Starting container...\n"; |
556 | $self->start_container(); | |
557 | ||
558 | print "Installing packages...\n"; | |
559 | $self->ve_command(['pacman', '-S', '--needed', '--noconfirm', '--', @$packages]); | |
2d2bee91 TL |
560 | |
561 | print "Masking problematic systemd units...\n"; | |
562 | for my $unit (qw(sys-kernel-config.mount sys-kernel-debug.mount)) { | |
563 | $self->mask_systemd_unit($unit); | |
564 | } | |
7b25f331 WB |
565 | } |
566 | ||
01756eba SI |
567 | # devices needed for gnupg to function: |
568 | my $devs = { | |
569 | '/dev/null' => ['c', '1', '3'], | |
570 | '/dev/random' => ['c', '1', '9'], # fake /dev/random (really urandom) | |
571 | '/dev/urandom' => ['c', '1', '9'], | |
572 | '/dev/tty' => ['c', '5', '0'], | |
573 | }; | |
574 | ||
575 | sub cleanup_dev { | |
766f0fa3 WB |
576 | my ($self) = @_; |
577 | my $root = $self->{rootfs}; | |
578 | ||
01756eba SI |
579 | # remove temporary device files |
580 | unlink "${root}$_" foreach keys %$devs; | |
581 | } | |
766f0fa3 | 582 | |
01756eba SI |
583 | sub create_dev { |
584 | my ($self) = @_; | |
585 | my $root = $self->{rootfs}; | |
586 | ||
587 | local $SIG{INT} = $SIG{TERM} = sub { $self->cleanup_dev; }; | |
766f0fa3 | 588 | |
01756eba SI |
589 | # we want to replace /dev/random, so delete devices first |
590 | $self->cleanup_dev(); | |
766f0fa3 WB |
591 | |
592 | foreach my $dev (keys %$devs) { | |
593 | my ($type, $major, $minor) = @{$devs->{$dev}}; | |
594 | system('mknod', "${root}${dev}", $type, $major, $minor); | |
595 | } | |
01756eba SI |
596 | } |
597 | ||
598 | sub populate_keyring { | |
599 | my ($self) = @_; | |
600 | my $root = $self->{rootfs}; | |
766f0fa3 WB |
601 | |
602 | # generate weak master key and populate the keyring | |
603 | system('unshare', '--fork', '--pid', 'chroot', "$root", 'pacman-key', '--init') == 0 | |
604 | or die "failed to initialize keyring: $?"; | |
605 | system('unshare', '--fork', '--pid', 'chroot', "$root", 'pacman-key', '--populate') == 0 | |
606 | or die "failed to populate keyring: $?"; | |
607 | ||
766f0fa3 WB |
608 | } |
609 | ||
7b25f331 WB |
610 | sub install { |
611 | my ($self, $pkglist) = @_; | |
612 | ||
613 | $self->cache_packages($pkglist); | |
614 | $self->ve_command(['pacman', '-S', '--needed', '--noconfirm', '--', @$pkglist]); | |
615 | } | |
616 | ||
617 | sub write_config { | |
618 | my ($self, $filename, $size) = @_; | |
619 | ||
620 | my $config = $self->{config}; | |
621 | ||
622 | my $data = ''; | |
623 | ||
624 | $data .= "Name: $config->{name}\n"; | |
625 | $data .= "Version: $config->{version}\n"; | |
626 | $data .= "Type: lxc\n"; | |
627 | $data .= "OS: archlinux\n"; | |
628 | $data .= "Section: $config->{section}\n"; | |
629 | $data .= "Maintainer: $config->{maintainer}\n"; | |
630 | $data .= "Architecture: $config->{architecture}\n"; | |
b65bfe8c | 631 | $data .= "Infopage: https://www.archlinux.org\n"; |
7b25f331 WB |
632 | $data .= "Installed-Size: $size\n"; |
633 | ||
634 | # optional | |
635 | $data .= "Infopage: $config->{infopage}\n" if $config->{infopage}; | |
636 | $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl}; | |
637 | $data .= "Certified: $config->{certified}\n" if $config->{certified}; | |
638 | ||
639 | # description | |
640 | $data .= "Description: $config->{headline}\n"; | |
641 | $data .= "$config->{description}\n" if $config->{description}; | |
642 | ||
643 | write_file ($data, $filename, 0644); | |
644 | } | |
645 | ||
646 | sub finalize { | |
b867882a TL |
647 | my ($self, $compressor) = @_; |
648 | ||
649 | my $use_zstd = 1; | |
650 | if (defined($compressor)) { | |
651 | if ($compressor =~ /^\s*--zstd?\s*$/) { | |
652 | $use_zstd = 1; | |
653 | } elsif ($compressor =~ /^\s*--(?:gz|gzip)\s*$/) { | |
654 | $use_zstd = 0; # just boolean for now.. | |
655 | } else { | |
656 | die "unkown compressor '$compressor'!\n"; | |
657 | } | |
658 | } | |
659 | ||
7b25f331 WB |
660 | my $rootdir = $self->{rootfs}; |
661 | ||
662 | print "Stopping container...\n"; | |
663 | $self->stop_container(); | |
664 | ||
665 | print "Rolling back mirrorlist changes...\n"; | |
666 | my $file = "$rootdir/etc/pacman.d/mirrorlist"; | |
667 | unlink $file; | |
668 | rename_file($file.'.aab_orig', $file); | |
669 | ||
e61d6533 WB |
670 | print "Removing weak temporary pacman keyring...\n"; |
671 | rmtree("$rootdir/etc/pacman.d/gnupg"); | |
672 | ||
7b25f331 WB |
673 | my $sizestr = $self->run_command("du -sm $rootdir", undef, 1); |
674 | my $size; | |
675 | if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) { | |
676 | $size = $1; | |
677 | } else { | |
678 | die "unable to detect size\n"; | |
679 | } | |
18ef2f8c | 680 | $self->logmsg ("uncompressed size: $size MB\n"); |
7b25f331 WB |
681 | |
682 | $self->write_config ("$rootdir/etc/appliance.info", $size); | |
683 | ||
684 | $self->logmsg ("creating final appliance archive\n"); | |
685 | ||
b867882a TL |
686 | my $compressor_ext = $use_zstd ? 'zst' : 'gz'; |
687 | ||
7b25f331 WB |
688 | my $target = "$self->{targetname}.tar"; |
689 | unlink $target; | |
b867882a | 690 | unlink "$target.$compressor_ext"; |
7b25f331 WB |
691 | |
692 | $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info"); | |
693 | $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info ."); | |
18ef2f8c | 694 | |
b867882a TL |
695 | $self->logmsg ("compressing archive ($compressor_ext)\n"); |
696 | if ($use_zstd) { | |
697 | $self->run_command ("zstd -19 --rm $target"); | |
698 | } else { | |
699 | $self->run_command ("gzip -9 $target"); | |
700 | } | |
18ef2f8c | 701 | |
b867882a TL |
702 | my $target_size = int(-s "$target.$compressor_ext") >> 20; |
703 | $self->logmsg ("created '$target.$compressor_ext' with size: $target_size MB\n"); | |
7b25f331 WB |
704 | } |
705 | ||
706 | sub enter { | |
707 | my ($self) = @_; | |
708 | my $veid = $self->{veid}; | |
d43d058d | 709 | my $conffile = $self->{veconffile}; |
7b25f331 WB |
710 | |
711 | my $vestat = $self->ve_status(); | |
712 | if (!$vestat->{exist}) { | |
713 | $self->logmsg ("Please create the appliance first (bootstrap)"); | |
714 | return; | |
715 | } | |
716 | ||
717 | if (!$vestat->{running}) { | |
718 | $self->start_container(); | |
719 | } | |
720 | ||
d43d058d | 721 | system ("lxc-attach -n $veid --rcfile $conffile --clear-env"); |
7b25f331 WB |
722 | } |
723 | ||
724 | sub clean { | |
725 | my ($self, $all) = @_; | |
726 | ||
727 | unlink $self->{logfile}; | |
728 | unlink $self->{'pacman.conf'}; | |
729 | $self->ve_destroy(); | |
730 | unlink '.veid'; | |
dfb4fbaa SI |
731 | unlink $self->{veconffile}; |
732 | ||
7b25f331 WB |
733 | rmtree $self->{pkgcache} if $all; |
734 | } | |
735 | ||
736 | 1; |