]> git.proxmox.com Git - dab.git/blame - DAB.pm
add experimental support for upcomming Debian Bullseye
[dab.git] / DAB.pm
CommitLineData
8ab34b87
DM
1package PVE::DAB;
2
3use strict;
4use warnings;
5use IO::File;
6use File::Path;
7use File::Basename;
8use IO::Select;
9use IPC::Open2;
10use IPC::Open3;
11use POSIX qw (LONG_MAX);
f0134ed2
DM
12use UUID;
13use Cwd;
8ab34b87
DM
14
15# fixme: lock container ?
16
17my $dablibdir = "/usr/lib/dab";
18my $devicetar = "$dablibdir/devices.tar.gz";
19my $default_env = "$dablibdir/scripts/defenv";
20my $fake_init = "$dablibdir/scripts/init.pl";
21my $script_ssh_init = "$dablibdir/scripts/ssh_gen_host_keys";
22my $script_mysql_randompw = "$dablibdir/scripts/mysql_randompw";
23my $script_init_urandom = "$dablibdir/scripts/init_urandom";
24
25my $postfix_main_cf = <<EOD;
26# See /usr/share/postfix/main.cf.dist for a commented, more complete version
27
28smtpd_banner = \$myhostname ESMTP \$mail_name (Debian/GNU)
29biff = no
30
31# appending .domain is the MUA's job.
32append_dot_mydomain = no
33
34# Uncomment the next line to generate "delayed mail" warnings
35#delay_warning_time = 4h
36
37alias_maps = hash:/etc/aliases
38alias_database = hash:/etc/aliases
39mydestination = \$myhostname, localhost.\$mydomain, localhost
40relayhost =
41mynetworks = 127.0.0.0/8
42inet_interfaces = loopback-only
43recipient_delimiter = +
44
76ec8d4b
SI
45compatibility_level = 2
46
8ab34b87
DM
47EOD
48
49# produce apt compatible filenames (/var/lib/apt/lists)
50sub __url_to_filename {
51 my $url = shift;
52
53 $url =~ s|^\S+://||;
54 $url =~ s|_|%5f|g;
55 $url =~ s|/|_|g;
56
57 return $url;
58}
59
a603a512
TL
60# defaults:
61# origin: debian
62my $supported_suites = {
58ef618c
TL
63 'bullseye' => {
64 ostype => "debian-11.0",
65 },
a603a512
TL
66 'buster' => {
67 ostype => "debian-10.0",
68 },
69 'stretch' => {
70 ostype => "debian-9.0",
71 },
72 'jessie' => {
73 ostype => "debian-8.0",
74 },
75 'wheezy' => {
76 ostype => "debian-7.0",
77 },
78 'squeeze' => {
79 ostype => "debian-6.0",
80 },
81 'lenny' => {
82 ostype => "debian-5.0",
83 },
84 'etch' => {
85 ostype => "debian-4.0",
86 },
87
88# DEVUAN
89 'devuan-jessie' => {
90 suite => 'jessie',
91 ostype => "devuan-1.0",
92 },
93 'devuan-ascii' => {
94 suite => 'ascii',
95 ostype => "devuan-2.0",
96 },
97 'ascii' => {
98 ostype => "devuan-2.0",
99 },
100
101# UBUNTU
102 'hardy' => {
103 ostype => "ubuntu-8.04",
104 origin => 'ubuntu',
105 },
106 'intrepid' => {
107 ostype => "ubuntu-8.10",
108 origin => 'ubuntu',
109 },
110 'jaunty' => {
111 ostype => "ubuntu-9.04",
112 origin => 'ubuntu',
113 },
114 'precise' => {
115 ostype => "ubuntu-12.04",
116 origin => 'ubuntu',
117 },
118 'trusty' => {
119 ostype => "ubuntu-14.04",
120 origin => 'ubuntu',
121 },
122 'vivid' => {
123 ostype => "ubuntu-15.04",
124 origin => 'ubuntu',
125 },
126 'wily' => {
127 ostype => "ubuntu-15.10",
128 origin => 'ubuntu',
129 },
130 'xenial' => {
131 ostype => "ubuntu-16.04",
132 origin => 'ubuntu',
133 },
134 'yakkety' => {
135 ostype => "ubuntu-16.10",
136 origin => 'ubuntu',
137 },
138 'zesty' => {
139 ostype => "ubuntu-17.04",
140 origin => 'ubuntu',
141 },
142 'artful' => {
143 ostype => "ubuntu-17.10",
144 origin => 'ubuntu',
145 },
146 'bionic' => {
147 ostype => "ubuntu-18.04",
148 origin => 'ubuntu',
149 },
150 'cosmic' => {
151 ostype => "ubuntu-18.10",
152 origin => 'ubuntu',
153 },
154 'disco' => {
155 ostype => "ubuntu-19.04",
156 origin => 'ubuntu',
157 },
158 'eoan' => {
159 ostype => "ubuntu-19.10",
160 origin => 'ubuntu',
161 },
162};
163
164sub get_suite_info {
165 my ($suite) = @_;
166
167 my $suiteinfo = $supported_suites->{$suite} || die "unsupported suite '$suite'!\n";
168
169 # set defaults
170 $suiteinfo->{origin} //= 'debian';
171 $suiteinfo->{suite} //= $suite;
172
173 return $suiteinfo;
174}
175
8ab34b87
DM
176sub download {
177 my ($self, $url, $path) = @_;
178
179 $self->logmsg ("download: $url\n");
180 my $tmpfn = "$path.tmp$$";
181 eval {
182 $self->run_command ("wget -q '$url' -O '$tmpfn'");
183 };
184
185 my $err = $@;
186 if ($err) {
187 unlink $tmpfn;
188 die $err;
189 }
190
191 rename ($tmpfn, $path);
192}
193
194sub write_file {
195 my ($data, $file, $perm) = @_;
196
197 die "no filename" if !$file;
198
199 unlink $file;
200
201 my $fh = IO::File->new ($file, O_WRONLY | O_CREAT, $perm) ||
202 die "unable to open file '$file'";
203
204 print $fh $data;
205
206 $fh->close;
207}
208
209sub read_file {
210 my ($file) = @_;
211
212 die "no filename" if !$file;
213
214 my $fh = IO::File->new ($file) ||
215 die "unable to open file '$file'";
216
217 local $/; # slurp mode
218
219 my $data = <$fh>;
220
221 $fh->close;
222
223 return $data;
224}
225
226sub read_config {
227 my ($filename) = @_;
228
229 my $res = {};
230
231 my $fh = IO::File->new ("<$filename") || return $res;
232 my $rec = '';
233
234 while (defined (my $line = <$fh>)) {
235 next if $line =~ m/^\#/;
236 next if $line =~ m/^\s*$/;
237 $rec .= $line;
238 };
239
240 close ($fh);
241
242 chomp $rec;
243 $rec .= "\n";
244
245 while ($rec) {
246 if ($rec =~ s/^Description:\s*([^\n]*)(\n\s+.*)*$//si) {
247 $res->{headline} = $1;
248 chomp $res->{headline};
249 my $long = $2;
250 $long =~ s/^\s+/ /;
251 $res->{description} = $long;
252 chomp $res->{description};
253 } elsif ($rec =~ s/^([^:]+):\s*(.*\S)\s*\n//) {
254 my ($key, $value) = (lc ($1), $2);
255 if ($key eq 'source' || $key eq 'mirror') {
256 push @{$res->{$key}}, $value;
257 } else {
258 die "duplicate key '$key'\n" if defined ($res->{$key});
259 $res->{$key} = $value;
260 }
261 } else {
262 die "unable to parse config file: $rec";
263 }
264 }
265
266 die "unable to parse config file" if $rec;
267
268 return $res;
269}
270
271sub run_command {
272 my ($self, $cmd, $input, $getoutput) = @_;
273
274 my $reader = IO::File->new();
275 my $writer = IO::File->new();
276 my $error = IO::File->new();
277
278 my $orig_pid = $$;
279
280 my $cmdstr = ref ($cmd) eq 'ARRAY' ? join (' ', @$cmd) : $cmd;
281
282 my $pid;
283 eval {
284 if (ref ($cmd) eq 'ARRAY') {
285 $pid = open3 ($writer, $reader, $error, @$cmd) || die $!;
286 } else {
287 $pid = open3 ($writer, $reader, $error, $cmdstr) || die $!;
288 }
289 };
290
291 my $err = $@;
292
293 # catch exec errors
294 if ($orig_pid != $$) {
295 $self->logmsg ("ERROR: command '$cmdstr' failed - fork failed\n");
296 POSIX::_exit (1);
297 kill ('KILL', $$);
298 }
299
300 die $err if $err;
301
302 print $writer $input if defined $input;
303 close $writer;
304
305 my $select = new IO::Select;
306 $select->add ($reader);
307 $select->add ($error);
308
309 my $res = '';
310 my $logfd = $self->{logfd};
311
312 while ($select->count) {
313 my @handles = $select->can_read ();
314
315 foreach my $h (@handles) {
316 my $buf = '';
317 my $count = sysread ($h, $buf, 4096);
318 if (!defined ($count)) {
319 waitpid ($pid, 0);
320 die "command '$cmdstr' failed: $!";
321 }
322 $select->remove ($h) if !$count;
323
324 print $logfd $buf;
325
326 $res .= $buf if $getoutput;
327 }
328 }
329
330 waitpid ($pid, 0);
331 my $ec = ($? >> 8);
332
333 die "command '$cmdstr' failed with exit code $ec\n" if $ec;
334
335 return $res;
336}
337
338sub logmsg {
339 my $self = shift;
340 print STDERR @_;
341 $self->writelog (@_);
342}
343
344sub writelog {
345 my $self = shift;
346 my $fd = $self->{logfd};
347 print $fd @_;
348}
349
350sub __sample_config {
f0134ed2 351 my ($self) = @_;
8ab34b87
DM
352
353 my $data = '';
f0134ed2 354 my $arch = $self->{config}->{architecture};
8ab34b87 355
f0134ed2 356 my $ostype = $self->{config}->{ostype};
8ab34b87 357
b5583827 358 if ($ostype =~ m/^de(bi|vu)an-/) {
f0134ed2
DM
359 $data .= "lxc.include = /usr/share/lxc/config/debian.common.conf\n";
360 } elsif ($ostype =~ m/^ubuntu-/) {
361 $data .= "lxc.include = /usr/share/lxc/config/ubuntu.common.conf\n";
362 } else {
363 die "unknown os type '$ostype'\n";
364 }
9313f84a
TL
365 $data .= "lxc.uts.name = localhost\n";
366 $data .= "lxc.rootfs.path = $self->{rootfs}\n";
8ab34b87
DM
367
368 return $data;
369}
370
371sub __allocate_ve {
372 my ($self) = @_;
373
374 my $cid;
375 if (my $fd = IO::File->new (".veid")) {
376 $cid = <$fd>;
377 chomp $cid;
378 close ($fd);
379 }
380
f0134ed2
DM
381
382 $self->{working_dir} = getcwd;
383 $self->{veconffile} = "$self->{working_dir}/config";
384 $self->{rootfs} = "$self->{working_dir}/rootfs";
8ab34b87
DM
385
386 if ($cid) {
387 $self->{veid} = $cid;
8ab34b87
DM
388 return $cid;
389 }
390
f0134ed2
DM
391 my $uuid;
392 my $uuid_str;
393 UUID::generate($uuid);
394 UUID::unparse($uuid, $uuid_str);
395 $self->{veid} = $uuid_str;
8ab34b87
DM
396
397 my $fd = IO::File->new (">.veid") ||
398 die "unable to write '.veid'\n";
f0134ed2 399 print $fd "$self->{veid}\n";
8ab34b87
DM
400 close ($fd);
401
f0134ed2
DM
402 my $cdata = $self->__sample_config();
403
404 my $fh = IO::File->new ($self->{veconffile}, O_WRONLY|O_CREAT|O_EXCL) ||
405 die "unable to write lxc config file '$self->{veconffile}' - $!";
406 print $fh $cdata;
407 close ($fh);
408
409 mkdir $self->{rootfs} || die "unable to create rootfs - $!";
8ab34b87 410
f0134ed2 411 $self->logmsg ("allocated VE $self->{veid}\n");
8ab34b87 412
f0134ed2 413 return $self->{veid};
8ab34b87
DM
414}
415
416sub new {
417 my ($class, $config) = @_;
418
419 $class = ref ($class) || $class;
420
421 my $self = {};
422
423 $config = read_config ('dab.conf') if !$config;
424
425 $self->{config} = $config;
426
427 bless $self, $class;
428
429 $self->{logfile} = "logfile";
430 $self->{logfd} = IO::File->new (">>$self->{logfile}") ||
431 die "unable to open log file";
432
433 my $arch = $config->{architecture};
434 die "no 'architecture' specified\n" if !$arch;
435
436 die "unsupported architecture '$arch'\n"
437 if $arch !~ m/^(i386|amd64)$/;
438
439 my $suite = $config->{suite} || die "no 'suite' specified\n";
a603a512
TL
440
441 my $suiteinfo = get_suite_info($suite);
442 $suite = $suiteinfo->{suite};
443 $config->{ostype} = $suiteinfo->{ostype};
8ab34b87
DM
444
445 my $name = $config->{name} || die "no 'name' specified\n";
446
447 $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ ||
448 die "illegal characters in name '$name'\n";
449
450 my $version = $config->{version};
451 die "no 'version' specified\n" if !$version;
452 die "no 'section' specified\n" if !$config->{section};
453 die "no 'description' specified\n" if !$config->{headline};
454 die "no 'maintainer' specified\n" if !$config->{maintainer};
455
456 if ($name =~ m/^$config->{ostype}/) {
457 $self->{targetname} = "${name}_${version}_$config->{architecture}";
458 } else {
459 $self->{targetname} = "$config->{ostype}-${name}_" .
460 "${version}_$config->{architecture}";
461 }
462
463 if (!$config->{source}) {
a603a512
TL
464 if (lc($suiteinfo->{origin}) eq 'debian') {
465 if ($suite eq 'etch' || $suite eq 'lenny') {
466 push @{$config->{source}}, (
467 'http://ftp.debian.org/debian SUITE main contrib',
468 'http://security.debian.org SUITE/updates main contrib',
469 );
470 } else {
471 push @{$config->{source}}, (
472 "http://ftp.debian.org/debian SUITE main contrib",
473 "http://ftp.debian.org/debian SUITE-updates main contrib",
474 "http://security.debian.org SUITE/updates main contrib",
475 );
476 }
477 } elsif (lc($suiteinfo->{origin}) eq 'ubuntu') {
8ab34b87 478 my $comp = "main restricted universe multiverse";
a603a512
TL
479 push @{$config->{source}}, (
480 "http://archive.ubuntu.com/ubuntu SUITE $comp",
481 "http://archive.ubuntu.com/ubuntu SUITE-updates $comp",
482 "http://archive.ubuntu.com/ubuntu SUITE-security $comp",
483 );
e6b5b5ac
DM
484 } else {
485 die "implement me";
8ab34b87
DM
486 }
487 }
488
489 my $sources = undef;
490
491 foreach my $s (@{$config->{source}}) {
492 if ($s =~ m@^\s*((http|ftp)://\S+)\s+(\S+)((\s+(\S+))+)$@) {
493 my ($url, $su, $components) = ($1, $3, $4);
494 $su =~ s/SUITE/$suite/;
495 $components =~ s/^\s+//;
496 $components =~ s/\s+$//;
497 my $ca;
498 foreach my $co (split (/\s+/, $components)) {
499 push @$ca, $co;
500 }
501 $ca = ['main'] if !$ca;
502
503 push @$sources, {
504 source => $url,
505 comp => $ca,
506 suite => $su,
507 };
508 } else {
509 die "syntax error in source spezification '$s'\n";
510 }
511 }
512
513 foreach my $m (@{$config->{mirror}}) {
514 if ($m =~ m@^\s*((http|ftp)://\S+)\s*=>\s*((http|ftp)://\S+)\s*$@) {
515 my ($ms, $md) = ($1, $3);
516 my $found;
517 foreach my $ss (@$sources) {
518 if ($ss->{source} eq $ms) {
519 $found = 1;
520 $ss->{mirror} = $md;
521 last;
522 }
523 }
524 die "unusable mirror $ms\n" if !$found;
525 } else {
526 die "syntax error in mirror spezification '$m'\n";
527 }
528 }
529
530 $self->{sources} = $sources;
531
532 $self->{infodir} = "info";
533
534 $self->__allocate_ve ();
535
536 $self->{cachedir} = ($config->{cachedir} || 'cache') . "/$suite";;
537
538 my $incl = [qw (less ssh openssh-server logrotate)];
539
540 my $excl = [qw (modutils reiserfsprogs ppp pppconfig pppoe
541 pppoeconf nfs-common mtools ntp)];
542
543 # ubuntu has too many dependencies on udev, so
544 # we cannot exclude it (instead we disable udevd)
e6b5b5ac 545
ef852234 546 if ($suite eq 'vivid' || $suite eq 'wily' || $suite eq 'xenial' ||
e8fed6ff 547 $suite eq 'yakkety' || $suite eq 'zesty' || $suite eq 'artful' ||
230e4471
TL
548 $suite eq 'bionic' || $suite eq 'cosmic' || $suite eq 'disco' ||
549 $suite eq 'eoan') {
306c21dc 550 push @$incl, 'isc-dhcp-client';
881f3099 551 push @$excl, qw(libmodule-build-perl);
e6b5b5ac
DM
552 } elsif ($suite eq 'trusty') {
553 push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+);
91be0a6a
DM
554 } elsif ($suite eq 'precise') {
555 push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+ ubuntu-standard);
e6b5b5ac 556 } elsif ($suite eq 'hardy') {
8ab34b87
DM
557 push @$excl, qw(kbd);
558 push @$excl, qw(apparmor apparmor-utils ntfs-3g
559 friendly-recovery);
5d0f537e 560 } elsif ($suite eq 'intrepid' || $suite eq 'jaunty') {
8ab34b87
DM
561 push @$excl, qw(apparmor apparmor-utils libapparmor1 libapparmor-perl
562 libntfs-3g28 ntfs-3g friendly-recovery);
5d0f537e
DM
563 } elsif ($suite eq 'jessie') {
564 push @$incl, 'sysvinit-core'; # avoid systemd and udev
565 push @$incl, 'libperl4-corelibs-perl'; # to make lsof happy
566 push @$excl, qw(systemd systemd-sysv udev module-init-tools pciutils hdparm
567 memtest86+ parted);
58ef618c 568 } elsif ($suite eq 'stretch' || $suite eq 'buster' || $suite eq 'bullseye') {
b6a1907e
DM
569 push @$excl, qw(module-init-tools pciutils hdparm
570 memtest86+ parted);
5d0f537e 571 } else {
8ab34b87
DM
572 push @$excl, qw(udev module-init-tools pciutils hdparm
573 memtest86+ parted);
574 }
575
576 $self->{incl} = $incl;
577 $self->{excl} = $excl;
578
579 return $self;
580}
581
582sub initialize {
583 my ($self) = @_;
584
585 my $infodir = $self->{infodir};
586 my $arch = $self->{config}->{architecture};
587
588 rmtree $infodir;
589 mkpath $infodir;
590
591 # truncate log
592 my $logfd = $self->{logfd} = IO::File->new (">$self->{logfile}") ||
593 die "unable to open log file";
594
595 foreach my $ss (@{$self->{sources}}) {
596 my $src = $ss->{mirror} || $ss->{source};
597 my $path = "dists/$ss->{suite}/Release";
598 my $url = "$src/$path";
599 my $target = __url_to_filename ("$ss->{source}/$path");
600 eval {
601 $self->download ($url, "$infodir/$target");
602 $self->download ("$url.gpg", "$infodir/$target.gpg");
603 # fixme: impl. verify (needs --keyring option)
604 };
605 if (my $err = $@) {
606 print $logfd $@;
607 warn "Release info ignored\n";
608 };
609 foreach my $comp (@{$ss->{comp}}) {
610 $path = "dists/$ss->{suite}/$comp/binary-$arch/Packages.gz";
611 $target = "$infodir/" . __url_to_filename ("$ss->{source}/$path");
612 my $pkgsrc = "$src/$path";
613 $self->download ($pkgsrc, $target);
614 $self->run_command ("gzip -d '$target'");
615 }
616 }
617}
618
619sub write_config {
620 my ($self, $filename, $size) = @_;
621
622 my $config = $self->{config};
623
624 my $data = '';
625
626 $data .= "Name: $config->{name}\n";
627 $data .= "Version: $config->{version}\n";
f0134ed2 628 $data .= "Type: lxc\n";
8ab34b87
DM
629 $data .= "OS: $config->{ostype}\n";
630 $data .= "Section: $config->{section}\n";
631 $data .= "Maintainer: $config->{maintainer}\n";
632 $data .= "Architecture: $config->{architecture}\n";
633 $data .= "Installed-Size: $size\n";
634
635 # optional
636 $data .= "Infopage: $config->{infopage}\n" if $config->{infopage};
637 $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl};
638 $data .= "Certified: $config->{certified}\n" if $config->{certified};
639
640 # description
641 $data .= "Description: $config->{headline}\n";
642 $data .= "$config->{description}\n" if $config->{description};
643
644 write_file ($data, $filename, 0644);
645}
646
647sub finalize {
648 my ($self, $opts) = @_;
649
650 my $suite = $self->{config}->{suite};
651 my $infodir = $self->{infodir};
652 my $arch = $self->{config}->{architecture};
653
654 my $instpkgs = $self->read_installed ();
655 my $pkginfo = $self->pkginfo();
656 my $veid = $self->{veid};
6fe8088f 657 my $conffile = $self->{veconffile};
f0134ed2 658 my $rootdir = $self->{rootfs};
8ab34b87
DM
659
660 my $vestat = $self->ve_status();
661 die "ve not running - unable to finalize\n" if !$vestat->{running};
662
663 # cleanup mysqld
664 if (-f "$rootdir/etc/init.d/mysql") {
665 $self->ve_command ("/etc/init.d/mysql stop");
666 }
667
668 if (!($opts->{keepmycnf} || (-f "$rootdir/etc/init.d/mysql_randompw"))) {
669 unlink "$rootdir/root/.my.cnf";
670 }
671
672 if ($suite eq 'etch') {
673 # enable apache2 startup
674 if ($instpkgs->{apache2}) {
675 write_file ("NO_START=0\n", "$rootdir/etc/default/apache2");
676 } else {
677 unlink "$rootdir/etc/default/apache2";
678 }
679 }
680 $self->logmsg ("cleanup package status\n");
681 # prevent auto selection of all standard, required or important
682 # packages which are not installed
683 foreach my $pkg (keys %$pkginfo) {
684 my $pri = $pkginfo->{$pkg}->{priority};
685 if ($pri && ($pri eq 'required' || $pri eq 'important'
686 || $pri eq 'standard')) {
687 if (!$instpkgs->{$pkg}) {
688 $self->ve_dpkg_set_selection ($pkg, 'purge');
689 }
690 }
691 }
692
693 $self->ve_command ("apt-get clean");
694
695 $self->logmsg ("update available package list\n");
696
697 $self->ve_command ("dpkg --clear-avail");
698 foreach my $ss (@{$self->{sources}}) {
699 my $relsrc = __url_to_filename ("$ss->{source}/dists/$ss->{suite}/Release");
700 if (-f "$infodir/$relsrc" && -f "$infodir/$relsrc.gpg") {
701 $self->run_command ("cp '$infodir/$relsrc' '$rootdir/var/lib/apt/lists/$relsrc'");
702 $self->run_command ("cp '$infodir/$relsrc.gpg' '$rootdir/var/lib/apt/lists/$relsrc.gpg'");
703 }
704 foreach my $comp (@{$ss->{comp}}) {
705 my $src = __url_to_filename ("$ss->{source}/dists/$ss->{suite}/" .
706 "$comp/binary-$arch/Packages");
707 my $target = "/var/lib/apt/lists/$src";
708 $self->run_command ("cp '$infodir/$src' '$rootdir/$target'");
709 $self->ve_command ("dpkg --merge-avail '$target'");
710 }
711 }
712
713 # set dselect default method
714 write_file ("apt apt\n", "$rootdir/var/lib/dpkg/cmethopt");
715
716 $self->ve_divert_remove ("/usr/sbin/policy-rc.d");
717
718 $self->ve_divert_remove ("/sbin/start-stop-daemon");
719
720 $self->ve_divert_remove ("/sbin/init");
721
722 # finally stop the VE
6fe8088f 723 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
8ab34b87
DM
724
725 unlink "$rootdir/sbin/defenv";
726
727 unlink <$rootdir/root/dead.letter*>;
728
729 unlink "$rootdir/var/log/init.log";
730
731 unlink "$rootdir/aquota.group";
732
733 unlink "$rootdir/aquota.user";
734
735 write_file ("", "$rootdir/var/log/syslog");
736
737 $self->logmsg ("detecting final size: ");
738
739 my $sizestr = $self->run_command ("du -sm $rootdir", undef, 1);
740 my $size;
741 if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) {
742 $size = $1;
743 } else {
744 die "unable to detect size\n";
745 }
746 $self->logmsg ("$size MB\n");
747
748 $self->write_config ("$rootdir/etc/appliance.info", $size);
749
750 $self->logmsg ("creating final appliance archive\n");
751
752 my $target = "$self->{targetname}.tar";
753 unlink $target;
754 unlink "$target.gz";
755
756 $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
757 $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
758 $self->run_command ("gzip $target");
759}
760
761sub read_installed {
762 my ($self) = @_;
763
f0134ed2 764 my $rootdir = $self->{rootfs};
8ab34b87
DM
765
766 my $pkgfilelist = "$rootdir/var/lib/dpkg/status";
767 local $/ = '';
768 open (PKGLST, "<$pkgfilelist") ||
769 die "unable to open '$pkgfilelist'";
770
771 my $pkglist = {};
772
773 while (my $rec = <PKGLST>) {
774 chomp $rec;
775 $rec =~ s/\n\s+/ /g;
776 $rec .= "\n";
777 my $res = {};
778
779 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
780 $res->{lc $1} = $2;
781 }
782
783 my $pkg = $res->{'package'};
784 if (my $status = $res->{status}) {
785 my @sa = split (/\s+/, $status);
786 my $stat = $sa[0];
787 if ($stat && ($stat ne 'purge')) {
788 $pkglist->{$pkg} = $res;
789 }
790 }
791 }
792
793 close (PKGLST);
794
795 return $pkglist;
796}
797
f0134ed2 798sub ve_status {
8ab34b87
DM
799 my ($self) = @_;
800
801 my $veid = $self->{veid};
802
f0134ed2 803 my $res = { running => 0 };
8ab34b87 804
f0134ed2 805 $res->{exist} = 1 if -d "$self->{rootfs}/usr";
8ab34b87 806
f0134ed2 807 my $filename = "/proc/net/unix";
8ab34b87 808
f0134ed2
DM
809 # similar test is used by lcxcontainers.c: list_active_containers
810 my $fh = IO::File->new ($filename, "r");
811 return $res if !$fh;
8ab34b87 812
f0134ed2
DM
813 while (defined(my $line = <$fh>)) {
814 if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) {
815 my $path = $1;
816 if ($path =~ m!^@/\S+/$veid/command$!) {
817 $res->{running} = 1;
818 }
819 }
8ab34b87 820 }
f0134ed2
DM
821 close($fh);
822
823 return $res;
8ab34b87
DM
824}
825
826sub ve_command {
827 my ($self, $cmd, $input) = @_;
828
829 my $veid = $self->{veid};
6fe8088f 830 my $conffile = $self->{veconffile};
8ab34b87
DM
831
832 if (ref ($cmd) eq 'ARRAY') {
6fe8088f 833 unshift @$cmd, 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--clear-env', '--', 'defenv';
8ab34b87
DM
834 $self->run_command ($cmd, $input);
835 } else {
6fe8088f 836 $self->run_command ("lxc-attach -n $veid --rcfile $conffile --clear-env -- defenv $cmd", $input);
8ab34b87
DM
837 }
838}
839
840# like ve_command, but pipes stdin correctly
841sub ve_exec {
842 my ($self, @cmd) = @_;
843
844 my $veid = $self->{veid};
6fe8088f 845 my $conffile = $self->{veconffile};
8ab34b87
DM
846
847 my $reader;
6fe8088f 848 my $pid = open2($reader, "<&STDIN", 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--',
8ab34b87
DM
849 'defenv', @cmd) || die "unable to exec command";
850
851 while (defined (my $line = <$reader>)) {
852 $self->logmsg ($line);
853 }
854
855 waitpid ($pid, 0);
856 my $rc = $? >> 8;
857
858 die "ve_exec failed - status $rc\n" if $rc != 0;
859}
860
861sub ve_divert_add {
862 my ($self, $filename) = @_;
863
864 $self->ve_command ("dpkg-divert --add --divert '$filename.distrib' " .
865 "--rename '$filename'");
866}
867sub ve_divert_remove {
868 my ($self, $filename) = @_;
869
f0134ed2 870 my $rootdir = $self->{rootfs};
8ab34b87
DM
871
872 unlink "$rootdir/$filename";
873 $self->ve_command ("dpkg-divert --remove --rename '$filename'");
874}
875
876sub ve_debconfig_set {
877 my ($self, $dcdata) = @_;
878
f0134ed2 879 my $rootdir = $self->{rootfs};
8ab34b87
DM
880 my $cfgfile = "/tmp/debconf.txt";
881 write_file ($dcdata, "$rootdir/$cfgfile");
882 $self->ve_command ("debconf-set-selections $cfgfile");
883 unlink "$rootdir/$cfgfile";
884}
885
886sub ve_dpkg_set_selection {
887 my ($self, $pkg, $status) = @_;
888
889 $self->ve_command ("dpkg --set-selections", "$pkg $status");
890}
891
892sub ve_dpkg {
893 my ($self, $cmd, @pkglist) = @_;
894
895 return if !scalar (@pkglist);
896
897 my $pkginfo = $self->pkginfo();
898
f0134ed2 899 my $rootdir = $self->{rootfs};
8ab34b87
DM
900 my $cachedir = $self->{cachedir};
901
902 my @files;
903
904 foreach my $pkg (@pkglist) {
905 my $filename = $self->getpkgfile ($pkg);
906 $self->run_command ("cp '$cachedir/$filename' '$rootdir/$filename'");
907 push @files, "/$filename";
908 $self->logmsg ("$cmd: $pkg\n");
909 }
910
911 my $fl = join (' ', @files);
912
913 if ($cmd eq 'install') {
914 $self->ve_command ("dpkg --force-depends --force-confold --install $fl");
915 } elsif ($cmd eq 'unpack') {
916 $self->ve_command ("dpkg --force-depends --unpack $fl");
917 } else {
918 die "internal error";
919 }
920
921 foreach my $fn (@files) { unlink "$rootdir$fn"; }
922}
923
924sub ve_destroy {
925 my ($self) = @_;
926
927 my $veid = $self->{veid}; # fixme
6fe8088f 928 my $conffile = $self->{veconffile};
8ab34b87
DM
929
930 my $vestat = $self->ve_status();
931 if ($vestat->{running}) {
6fe8088f 932 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
8ab34b87 933 }
f0134ed2
DM
934
935 rmtree $self->{rootfs};
936 unlink $self->{veconffile};
8ab34b87
DM
937}
938
939sub ve_init {
940 my ($self) = @_;
941
f0134ed2 942 my $veid = $self->{veid};
6fe8088f 943 my $conffile = $self->{veconffile};
8ab34b87
DM
944
945 $self->logmsg ("initialize VE $veid\n");
946
f0134ed2
DM
947 my $vestat = $self->ve_status();
948 if ($vestat->{running}) {
6fe8088f 949 $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
f0134ed2 950 }
8ab34b87 951
f0134ed2
DM
952 rmtree $self->{rootfs};
953 mkpath $self->{rootfs};
8ab34b87
DM
954}
955
956sub __deb_version_cmp {
957 my ($cur, $op, $new) = @_;
958
959 if (system("dpkg", "--compare-versions", $cur, $op, $new) == 0) {
960 return 1;
961 }
962
963 return 0;
964}
965
966sub __parse_packages {
967 my ($pkginfo, $filename, $src) = @_;
968
969 local $/ = '';
970 open (PKGLST, "<$filename") ||
971 die "unable to open '$filename'";
972
973 while (my $rec = <PKGLST>) {
974 $rec =~ s/\n\s+/ /g;
975 chomp $rec;
976 $rec .= "\n";
977
978 my $res = {};
979
980 while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) {
981 $res->{lc $1} = $2;
982 }
983
984 my $pkg = $res->{'package'};
985 if ($pkg && $res->{'filename'}) {
986 my $cur;
987 if (my $info = $pkginfo->{$pkg}) {
988 $cur = $info->{version};
989 }
990 my $new = $res->{version};
991 if (!$cur || __deb_version_cmp ($cur, 'lt', $new)) {
992 if ($src) {
993 $res->{url} = "$src/$res->{'filename'}";
994 } else {
995 die "no url for package '$pkg'" if !$res->{url};
996 }
997 $pkginfo->{$pkg} = $res;
998 }
999 }
1000 }
1001
1002 close (PKGLST);
1003}
1004
1005sub pkginfo {
1006 my ($self) = @_;
1007
1008 return $self->{pkginfo} if $self->{pkginfo};
1009
1010 my $infodir = $self->{infodir};
1011 my $arch = $self->{config}->{architecture};
1012
1013 my $availfn = "$infodir/available";
1014
1015 my $pkginfo = {};
1016 my $pkgcount = 0;
1017
1018 # reading 'available' is faster, because it only contains latest version
1019 # (no need to do slow version compares)
1020 if (-f $availfn) {
1021 __parse_packages ($pkginfo, $availfn);
1022 $self->{pkginfo} = $pkginfo;
1023 return $pkginfo;
1024 }
1025
1026 $self->logmsg ("generating available package list\n");
1027
1028 foreach my $ss (@{$self->{sources}}) {
1029 foreach my $comp (@{$ss->{comp}}) {
1030 my $url = "$ss->{source}/dists/$ss->{suite}/$comp/binary-$arch/Packages";
1031 my $pkgfilelist = "$infodir/" . __url_to_filename ($url);
1032
1033 my $src = $ss->{mirror} || $ss->{source};
1034
1035 __parse_packages ($pkginfo, $pkgfilelist, $src);
1036 }
1037 }
1038
1039 if (my $dep = $self->{config}->{depends}) {
1040 foreach my $d (split (/,/, $dep)) {
1041 if ($d =~ m/^\s*(\S+)\s*(\((\S+)\s+(\S+)\)\s*)?$/) {
1042 my ($pkg, $op, $rver) = ($1, $3, $4);
1043 $self->logmsg ("checking dependencies: $d\n");
1044 my $info = $pkginfo->{$pkg};
1045 die "package '$pkg' not available\n" if !$info;
1046 if ($op) {
1047 my $cver = $info->{version};
1048 if (!__deb_version_cmp ($cver, $op, $rver)) {
1049 die "detected wrong version '$cver'\n";
1050 }
1051 }
1052 } else {
1053 die "syntax error in depends field";
1054 }
1055 }
1056 }
1057
1058 $self->{pkginfo} = $pkginfo;
1059
1060 my $tmpfn = "$availfn.tmp$$";
1061 my $fd = IO::File->new (">$tmpfn");
1062 foreach my $pkg (sort keys %$pkginfo) {
1063 my $info = $pkginfo->{$pkg};
1064 print $fd "package: $pkg\n";
1065 foreach my $k (sort keys %$info) {
1066 next if $k eq 'description';
1067 next if $k eq 'package';
1068 my $v = $info->{$k};
1069 print $fd "$k: $v\n" if $v;
1070 }
1071 print $fd "description: $info->{description}\n" if $info->{description};
1072 print $fd "\n";
1073 }
1074 close ($fd);
1075
1076 rename ($tmpfn, $availfn);
1077
1078 return $pkginfo;
1079}
1080
1081sub __record_provides {
1082 my ($pkginfo, $closure, $list, $skipself) = @_;
1083
1084 foreach my $pname (@$list) {
1085 my $info = $pkginfo->{$pname};
1086 # fixme: if someone install packages directly using dpkg, there
1087 # is no entry in 'available', only in 'status'. In that case, we
1088 # should extract info from $instpkgs
1089 if (!$info) {
1090 warn "hint: ignoring provides for '$pname' - package not in 'available' list.\n";
1091 next;
1092 }
1093 if (my $prov = $info->{provides}) {
1094 my @pl = split (',', $prov);
1095 foreach my $p (@pl) {
1096 $p =~ m/\s*(\S+)/;
1097 if (!($skipself && (grep { $1 eq $_ } @$list))) {
1098 $closure->{$1} = 1;
1099 }
1100 }
1101 }
1102 $closure->{$pname} = 1 if !$skipself;
1103 }
1104}
1105
1106sub closure {
1107 my ($self, $closure, $list) = @_;
1108
1109 my $pkginfo = $self->pkginfo();
1110
1111 # first, record provided packages
1112 __record_provides ($pkginfo, $closure, $list, 1);
1113
e30a1928
DM
1114 my $pkghash = {};
1115 my $pkglist = [];
8ab34b87
DM
1116
1117 # then resolve dependencies
1118 foreach my $pname (@$list) {
e30a1928 1119 __closure_single ($pkginfo, $closure, $pkghash, $pkglist, $pname, $self->{excl});
8ab34b87
DM
1120 }
1121
e30a1928 1122 return $pkglist;
8ab34b87
DM
1123}
1124
1125sub __closure_single {
e30a1928 1126 my ($pkginfo, $closure, $pkghash, $pkglist, $pname, $excl) = @_;
8ab34b87
DM
1127
1128 $pname =~ s/^\s+//;
1129 $pname =~ s/\s+$//;
5d0f537e 1130 $pname =~ s/:any$//;
8ab34b87
DM
1131
1132 return if $closure->{$pname};
1133
1134 my $info = $pkginfo->{$pname} || die "no such package '$pname'";
1135
1136 my $dep = $info->{depends};
1137 my $predep = $info->{'pre-depends'};
1138
1139 my $size = $info->{size};
1140 my $url = $info->{url};
1141
1142 $url || die "$pname: no url for package '$pname'";
e30a1928
DM
1143
1144 if (!$pkghash->{$pname}) {
1145 unshift @$pkglist, $pname;
1146 $pkghash->{$pname} = 1;
1147 }
8ab34b87
DM
1148
1149 __record_provides ($pkginfo, $closure, [$pname]) if $info->{provides};
1150
1151 $closure->{$pname} = 1;
1152
1153 #print "$url\n";
1154
1155 my @l;
1156
1157 push @l, split (/,/, $predep) if $predep;
1158 push @l, split (/,/, $dep) if $dep;
1159
1160 DEPEND: foreach my $p (@l) {
1161 my @l1 = split (/\|/, $p);
1162 foreach my $p1 (@l1) {
1163 if ($p1 =~ m/^\s*(\S+).*/) {
1164 #printf (STDERR "$pname: $p --> $1\n");
1165 if ($closure->{$1}) {
1166 next DEPEND; # dependency already met
1167 }
1168 }
1169 }
1170 # search for non-excluded alternative
1171 my $found;
1172 foreach my $p1 (@l1) {
1173 if ($p1 =~ m/^\s*(\S+).*/) {
1174 next if grep { $1 eq $_ } @$excl;
1175 $found = $1;
1176 last;
1177 }
1178 }
1179 die "package '$pname' depends on exclusion '$p'\n" if !$found;
1180
1181 #printf (STDERR "$pname: $p --> $found\n");
1182
e30a1928 1183 __closure_single ($pkginfo, $closure, $pkghash, $pkglist, $found, $excl);
8ab34b87
DM
1184 }
1185}
1186
1187sub cache_packages {
1188 my ($self, $pkglist) = @_;
1189
1190 foreach my $pkg (@$pkglist) {
1191 $self->getpkgfile ($pkg);
1192 }
1193}
1194
1195sub getpkgfile {
1196 my ($self, $pkg) = @_;
1197
1198 my $pkginfo = $self->pkginfo();
1199 my $info = $pkginfo->{$pkg} || die "no such package '$pkg'";
1200 my $cachedir = $self->{cachedir};
1201
1202 my $url = $info->{url};
1203
1204 my $filename;
1205 if ($url =~ m|/([^/]+.deb)$|) {
1206 $filename = $1;
1207 } else {
1208 die "internal error";
1209 }
1210
1211 return $filename if -f "$cachedir/$filename";
1212
1213 mkpath $cachedir;
1214
1215 $self->download ($url, "$cachedir/$filename");
1216
1217 return $filename;
1218}
1219
1220sub install_init_script {
1221 my ($self, $script, $runlevel, $prio) = @_;
1222
1223 my $suite = $self->{config}->{suite};
f0134ed2 1224 my $rootdir = $self->{rootfs};
8ab34b87
DM
1225
1226 my $base = basename ($script);
1227 my $target = "$rootdir/etc/init.d/$base";
1228
1229 $self->run_command ("install -m 0755 '$script' '$target'");
1230 if ($suite eq 'etch' || $suite eq 'lenny') {
1231 $self->ve_command ("update-rc.d $base start $prio $runlevel .");
ef852234 1232 } elsif ($suite eq 'xenial' || $suite eq 'wily' || $suite eq 'vivid' ||
e8fed6ff 1233 $suite eq 'yakkety' || $suite eq 'zesty' || $suite eq 'artful' ||
230e4471
TL
1234 $suite eq 'bionic' || $suite eq 'cosmic' || $suite eq 'disco' ||
1235 $suite eq 'eoan') {
bb6602fa 1236 die "unable to install init script (system uses systemd)\n";
91be0a6a 1237 } elsif ($suite eq 'trusty' || $suite eq 'precise') {
bb6602fa 1238 die "unable to install init script (system uses upstart)\n";
8ab34b87
DM
1239 } else {
1240 $self->ve_command ("insserv $base");
1241 }
1242
1243 return $target;
1244}
1245
1246sub bootstrap {
1247 my ($self, $opts) = @_;
1248
1249 my $pkginfo = $self->pkginfo();
1250 my $veid = $self->{veid};
1251 my $suite = $self->{config}->{suite};
1252
1253 my $important = [ @{$self->{incl}} ];
1254 my $required;
1255 my $standard;
1256
1257 my $mta = $opts->{exim} ? 'exim' : 'postfix';
1258
1259 if ($mta eq 'postfix') {
1260 push @$important, "postfix";
1261 }
1262
e30a1928 1263 foreach my $p (sort keys %$pkginfo) {
8ab34b87
DM
1264 next if grep { $p eq $_ } @{$self->{excl}};
1265 my $pri = $pkginfo->{$p}->{priority};
1266 next if !$pri;
1267 next if $mta ne 'exim' && $p =~ m/exim/;
1268 next if $p =~ m/(selinux|semanage|policycoreutils)/;
1269
1270 push @$required, $p if $pri eq 'required';
1271 push @$important, $p if $pri eq 'important';
1272 push @$standard, $p if $pri eq 'standard' && !$opts->{minimal};
1273 }
1274
1275 my $closure = {};
1276 $required = $self->closure ($closure, $required);
1277 $important = $self->closure ($closure, $important);
1278
1279 if (!$opts->{minimal}) {
1280 push @$standard, 'xbase-clients';
1281 $standard = $self->closure ($closure, $standard);
1282 }
1283
1284 # test if we have all 'ubuntu-minimal' and 'ubuntu-standard' packages
1285 # except those explicitly excluded
1286 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1287 my $mdeps = $pkginfo->{'ubuntu-minimal'}->{depends};
1288 foreach my $d (split (/,/, $mdeps)) {
1289 if ($d =~ m/^\s*(\S+)$/) {
1290 my $pkg = $1;
1291 next if $closure->{$pkg};
1292 next if grep { $pkg eq $_ } @{$self->{excl}};
1293 die "missing ubuntu-minimal package '$pkg'\n";
1294 }
1295 }
1296 if (!$opts->{minimal}) {
1297 $mdeps = $pkginfo->{'ubuntu-standard'}->{depends};
1298 foreach my $d (split (/,/, $mdeps)) {
1299 if ($d =~ m/^\s*(\S+)$/) {
1300 my $pkg = $1;
1301 next if $closure->{$pkg};
1302 next if grep { $pkg eq $_ } @{$self->{excl}};
1303 die "missing ubuntu-standard package '$pkg'\n";
1304 }
1305 }
1306 }
1307 }
1308
1309 # download/cache all files first
1310 $self->cache_packages ($required);
1311 $self->cache_packages ($important);
1312 $self->cache_packages ($standard);
1313
f0134ed2 1314 my $rootdir = $self->{rootfs};
8ab34b87
DM
1315
1316 # extract required packages first
1317 $self->logmsg ("create basic environment\n");
1318 foreach my $p (@$required) {
1319 my $filename = $self->getpkgfile ($p);
5d0f537e
DM
1320 my $content = $self->run_command("ar -t '$self->{cachedir}/$filename'", undef, 1);
1321 if ($content =~ m/^data.tar.xz$/m) {
1322 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.xz | tar -C '$rootdir' -xJf -");
1323 } else {
1324 $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.gz | tar -C '$rootdir' -xzf -");
1325 }
8ab34b87
DM
1326 }
1327
1328 # fake dpkg status
1329 my $data = "Package: dpkg\n" .
1330 "Version: $pkginfo->{dpkg}->{version}\n" .
1331 "Status: install ok installed\n";
1332
1333 write_file ($data, "$rootdir/var/lib/dpkg/status");
1334 write_file ("", "$rootdir/var/lib/dpkg/info/dpkg.list");
1335 write_file ("", "$rootdir/var/lib/dpkg/available");
1336
1337 $data = '';
1338 foreach my $ss (@{$self->{sources}}) {
1339 my $url = $ss->{source};
1340 my $comp = join (' ', @{$ss->{comp}});
1341 $data .= "deb $url $ss->{suite} $comp\n\n";
1342 }
1343
1344 write_file ($data, "$rootdir/etc/apt/sources.list");
1345
1346 $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n";
1347 write_file ($data, "$rootdir/etc/fstab", 0644);
1348
1349 write_file ("localhost\n", "$rootdir/etc/hostname", 0644);
1350
1351 # avoid warnings about non-existent resolv.conf
1352 write_file ("", "$rootdir/etc/resolv.conf", 0644);
1353
230e4471
TL
1354 if ($suite eq 'eoan' || $suite eq 'disco' || $suite eq 'cosmic' ||
1355 $suite eq 'bionic' || $suite eq 'artful' ||
40c4a19f
TL
1356 $suite eq 'zesty' || $suite eq 'yakkety' || $suite eq 'xenial' ||
1357 $suite eq 'wily') {
ae2443bc
DM
1358 # no need to configure loopback device
1359 } else {
1360 $data = "auto lo\niface lo inet loopback\n";
b6a1907e 1361 mkdir "$rootdir/etc/network";
ae2443bc
DM
1362 write_file ($data, "$rootdir/etc/network/interfaces", 0644);
1363 }
8ab34b87
DM
1364
1365 # setup devices
1366 $self->run_command ("tar xzf '$devicetar' -C '$rootdir'");
1367
1368 # avoid warnings about missing default locale
1369 write_file ("LANG=\"C\"\n", "$rootdir/etc/default/locale", 0644);
1370
1371 # fake init
1372 rename ("$rootdir/sbin/init", "$rootdir/sbin/init.org");
1373 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1374
1375 $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'");
1376
f0134ed2 1377 $self->run_command ("lxc-start -n $veid -f $self->{veconffile}");
8ab34b87
DM
1378
1379 $self->logmsg ("initialize ld cache\n");
1380 $self->ve_command ("/sbin/ldconfig");
1381 $self->run_command ("ln -sf mawk '$rootdir/usr/bin/awk'");
1382
1383 $self->logmsg ("installing packages\n");
1384
1385 $self->ve_dpkg ('install', 'base-files', 'base-passwd');
1386
1387 $self->ve_dpkg ('install', 'dpkg');
1388
1389 $self->run_command ("ln -sf /usr/share/zoneinfo/UTC '$rootdir/etc/localtime'");
756b991f
DM
1390
1391 $self->run_command ("ln -sf bash '$rootdir/bin/sh'");
8ab34b87
DM
1392
1393 $self->ve_dpkg ('install', 'libc6');
1394 $self->ve_dpkg ('install', 'perl-base');
1395
1396 unlink "$rootdir/usr/bin/awk";
1397
1398 $self->ve_dpkg ('install', 'mawk');
1399 $self->ve_dpkg ('install', 'debconf');
e30a1928 1400
8ab34b87
DM
1401 # unpack required packages
1402 foreach my $p (@$required) {
1403 $self->ve_dpkg ('unpack', $p);
1404 }
1405
1406 rename ("$rootdir/sbin/init.org", "$rootdir/sbin/init");
1407 $self->ve_divert_add ("/sbin/init");
1408 $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'");
1409
1410 # disable service activation
1411 $self->ve_divert_add ("/usr/sbin/policy-rc.d");
1412 $data = "#!/bin/sh\nexit 101\n";
1413 write_file ($data, "$rootdir/usr/sbin/policy-rc.d", 755);
1414
1415 # disable start-stop-daemon
1416 $self->ve_divert_add ("/sbin/start-stop-daemon");
1417 $data = <<EOD;
1418#!/bin/sh
1419echo
1420echo \"Warning: Fake start-stop-daemon called, doing nothing\"
1421EOD
1422 write_file ($data, "$rootdir/sbin/start-stop-daemon", 0755);
1423
1424 # disable udevd
1425 $self->ve_divert_add ("/sbin/udevd");
1426
1427 if ($suite eq 'etch') {
1428 # disable apache2 startup
1429 write_file ("NO_START=1\n", "$rootdir/etc/default/apache2");
1430 }
1431
1432 $self->logmsg ("configure required packages\n");
1433 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1434
1435 # set postfix defaults
1436 if ($mta eq 'postfix') {
1437 $data = "postfix postfix/main_mailer_type select Local only\n";
1438 $self->ve_debconfig_set ($data);
1439
1440 $data = "postmaster: root\nwebmaster: root\n";
1441 write_file ($data, "$rootdir/etc/aliases");
1442 }
1443
1444 if ($suite eq 'jaunty') {
1445 # jaunty does not create /var/run/network, so network startup fails.
1446 # so we do not use tmpfs for /var/run and /var/lock
1447 $self->run_command ("sed -e 's/RAMRUN=yes/RAMRUN=no/' -e 's/RAMLOCK=yes/RAMLOCK=no/' -i $rootdir/etc/default/rcS");
1448 # and create the directory here
1449 $self->run_command ("mkdir $rootdir/var/run/network");
1450 }
1451
1452 # unpack base packages
1453 foreach my $p (@$important) {
1454 $self->ve_dpkg ('unpack', $p);
1455 }
1456
1457 # start loopback
b6a1907e
DM
1458 if (-x "$rootdir/sbin/ifconfig") {
1459 $self->ve_command ("ifconfig lo up");
1460 } else {
1461 $self->ve_command ("ip link set lo up");
1462 }
8ab34b87
DM
1463
1464 $self->logmsg ("configure important packages\n");
1465 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1466
1467 if (-d "$rootdir/etc/event.d") {
1468 unlink <$rootdir/etc/event.d/tty*>;
1469 }
1470
1471 if (-f "$rootdir/etc/inittab") {
756b991f 1472 $self->run_command ("sed -i -e '/getty\\s38400\\stty[23456]/d' '$rootdir/etc/inittab'");
8ab34b87
DM
1473 }
1474
1475 # Link /etc/mtab to /proc/mounts, so df and friends will work:
1476 unlink "$rootdir/etc/mtab";
1477 $self->ve_command ("ln -s /proc/mounts /etc/mtab");
1478
1479 # reset password
1480 $self->ve_command ("usermod -L root");
1481
8ab34b87
DM
1482 if ($mta eq 'postfix') {
1483 $data = "postfix postfix/main_mailer_type select No configuration\n";
1484 $self->ve_debconfig_set ($data);
1485
1486 unlink "$rootdir/etc/mailname";
1487 write_file ($postfix_main_cf, "$rootdir/etc/postfix/main.cf");
1488 }
1489
1490 if (!$opts->{minimal}) {
1491 # unpack standard packages
1492 foreach my $p (@$standard) {
1493 $self->ve_dpkg ('unpack', $p);
1494 }
1495
1496 $self->logmsg ("configure standard packages\n");
1497 $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
1498 }
1499
1500 # disable HWCLOCK access
1501 $self->run_command ("echo 'HWCLOCKACCESS=no' >> '$rootdir/etc/default/rcS'");
1502
1503 # disable hald
1504 $self->ve_divert_add ("/usr/sbin/hald");
1505
1506 # disable /dev/urandom init
1507 $self->run_command ("install -m 0755 '$script_init_urandom' '$rootdir/etc/init.d/urandom'");
1508
1509 if ($suite eq 'etch' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1510 # avoid klogd start
1511 $self->ve_divert_add ("/sbin/klogd");
1512 }
1513
1514 # remove unnecessays sysctl entries to avoid warnings
1515 my $cmd = 'sed';
1516 $cmd .= ' -e \'s/^\(kernel\.printk.*\)/#\1/\'';
1517 $cmd .= ' -e \'s/^\(kernel\.maps_protect.*\)/#\1/\'';
1518 $cmd .= ' -e \'s/^\(fs\.inotify\.max_user_watches.*\)/#\1/\'';
1519 $cmd .= ' -e \'s/^\(vm\.mmap_min_addr.*\)/#\1/\'';
1520 $cmd .= " -i '$rootdir/etc/sysctl.conf'";
1521 $self->run_command ($cmd);
1522
1523 my $bindv6only = "$rootdir/etc/sysctl.d/bindv6only.conf";
1524 if (-f $bindv6only) {
1525 $cmd = 'sed';
1526 $cmd .= ' -e \'s/^\(net\.ipv6\.bindv6only.*\)/#\1/\'';
1527 $cmd .= " -i '$bindv6only'";
1528 $self->run_command ($cmd);
1529 }
1530
1531 if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1532 # disable tty init (console-setup)
1533 my $cmd = 'sed';
1534 $cmd .= ' -e \'s/^\(ACTIVE_CONSOLES=.*\)/ACTIVE_CONSOLES=/\'';
1535 $cmd .= " -i '$rootdir/etc/default/console-setup'";
1536 $self->run_command ($cmd);
1537 }
1538
1539 if ($suite eq 'intrepid') {
1540 # remove sysctl setup (avoid warnings at startup)
1541 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1542 $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf";
1543 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1544 $self->run_command ("rm $filelist");
1545 }
1546 if ($suite eq 'jaunty') {
1547 # remove sysctl setup (avoid warnings at startup)
1548 my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
1549 $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
1550 $self->run_command ("rm $filelist");
1551 }
1552}
1553
1554sub enter {
1555 my ($self) = @_;
1556
1557 my $veid = $self->{veid};
6fe8088f 1558 my $conffile = $self->{veconffile};
8ab34b87
DM
1559
1560 my $vestat = $self->ve_status();
1561
1562 if (!$vestat->{exist}) {
1563 $self->logmsg ("Please create the appliance first (bootstrap)");
1564 return;
1565 }
1566
1567 if (!$vestat->{running}) {
6fe8088f 1568 $self->run_command ("lxc-start -n $veid -f $conffile");
8ab34b87
DM
1569 }
1570
6fe8088f 1571 system ("lxc-attach -n $veid --rcfile $conffile --clear-env");
8ab34b87
DM
1572}
1573
1574sub ve_mysql_command {
1575 my ($self, $sql, $password) = @_;
1576
1577 #my $bootstrap = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1578 #"--skip-bdb --skip-innodb --skip-ndbcluster";
1579
1580 $self->ve_command ("mysql", $sql);
1581}
1582
1583sub ve_mysql_bootstrap {
1584 my ($self, $sql, $password) = @_;
1585
1586 my $cmd;
1587
1588 my $suite = $self->{config}->{suite};
dcc99599
DM
1589
1590 if ($suite eq 'jessie') {
f0134ed2 1591 my $rootdir = $self->{rootfs};
dcc99599
DM
1592 $self->run_command ("sed -e 's/^key_buffer\\s*=/key_buffer_size =/' -i $rootdir/etc/mysql/my.cnf");
1593 }
1594
12152e08 1595 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
8ab34b87
DM
1596 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables";
1597
1598 } else {
1599 $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " .
1600 "--skip-bdb --skip-innodb --skip-ndbcluster";
1601 }
1602
1603 $self->ve_command ($cmd, $sql);
1604}
1605
1606sub compute_required {
1607 my ($self, $pkglist) = @_;
1608
1609 my $pkginfo = $self->pkginfo();
1610 my $instpkgs = $self->read_installed ();
1611
1612 my $closure = {};
1613 __record_provides ($pkginfo, $closure, [keys %$instpkgs]);
1614
1615 return $self->closure ($closure, $pkglist);
1616}
1617
1618sub task_postgres {
1619 my ($self, $opts) = @_;
1620
1621 my @supp = ('7.4', '8.1');
1622 my $pgversion = '8.1';
1623
1624 my $suite = $self->{config}->{suite};
1625
1626 if ($suite eq 'lenny' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
1627 @supp = ('8.3');
1628 $pgversion = '8.3';
1629 } elsif ($suite eq 'squeeze') {
1630 @supp = ('8.4');
1631 $pgversion = '8.4';
632ea034
DM
1632 } elsif ($suite eq 'wheezy') {
1633 @supp = ('9.1');
1634 $pgversion = '9.1';
5d0f537e
DM
1635 } elsif ($suite eq 'jessie') {
1636 @supp = ('9.4');
1637 $pgversion = '9.4';
b6a1907e
DM
1638 } elsif ($suite eq 'stretch') {
1639 @supp = ('9.6');
1640 $pgversion = '9.6';
139be064
TL
1641 } elsif ($suite eq 'buster') {
1642 @supp = ('11');
1643 $pgversion = '11';
58ef618c
TL
1644 } elsif ($suite eq 'bullseye') {
1645 # FIXME update on freeze!
1646 @supp = ('12');
1647 $pgversion = '12';
8ab34b87
DM
1648 }
1649
1650 $pgversion = $opts->{version} if $opts->{version};
1651
1652 die "unsupported postgres version '$pgversion'\n"
1653 if !grep { $pgversion eq $_; } @supp;
1654
f0134ed2 1655 my $rootdir = $self->{rootfs};
8ab34b87
DM
1656
1657 my $required = $self->compute_required (["postgresql-$pgversion"]);
1658
1659 $self->cache_packages ($required);
1660
1661 $self->ve_dpkg ('install', @$required);
1662
632ea034 1663 my $iscript = "postgresql-$pgversion";
b6a1907e
DM
1664 if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie' ||
1665 $suite eq 'stretch') {
1666 $iscript = 'postgresql';
632ea034 1667 }
8ab34b87
DM
1668
1669 $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start};
1670}
1671
1672sub task_mysql {
1673 my ($self, $opts) = @_;
1674
1675 my $password = $opts->{password};
f0134ed2 1676 my $rootdir = $self->{rootfs};
8ab34b87
DM
1677
1678 my $suite = $self->{config}->{suite};
1679
632ea034
DM
1680 my $ver = '5.0';
1681 if ($suite eq 'squeeze') {
1682 $ver = '5.1';
5d0f537e 1683 } elsif ($suite eq 'wheezy' || $suite eq 'jessie') {
632ea034 1684 $ver = '5.5';
b6a1907e
DM
1685 } else {
1686 die "task_mysql: unsupported suite '$suite'";
632ea034 1687 }
8ab34b87
DM
1688
1689 my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]);
1690
1691 $self->cache_packages ($required);
1692
1693 $self->ve_dpkg ('install', @$required);
1694
1695 # fix security (see /usr/bin/mysql_secure_installation)
1696 my $sql = "DELETE FROM mysql.user WHERE User='';\n" .
1697 "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';\n" .
1698 "FLUSH PRIVILEGES;\n";
1699 $self->ve_mysql_bootstrap ($sql);
1700
1701 if ($password) {
1702
1703 my $rpw = $password eq 'random' ? 'admin' : $password;
1704
1705 my $sql = "USE mysql;\n" .
1706 "UPDATE user SET password=PASSWORD(\"$rpw\") WHERE user='root';\n" .
1707 "FLUSH PRIVILEGES;\n";
1708 $self->ve_mysql_bootstrap ($sql);
1709
1710 write_file ("[client]\nuser=root\npassword=\"$rpw\"\n", "$rootdir/root/.my.cnf", 0600);
1711 if ($password eq 'random') {
1712 $self->install_init_script ($script_mysql_randompw, 2, 20);
1713 }
1714 }
1715
1716 $self->ve_command ("/etc/init.d/mysql start") if $opts->{start};
1717}
1718
1719sub task_php {
1720 my ($self, $opts) = @_;
1721
1722 my $memlimit = $opts->{memlimit};
f0134ed2 1723 my $rootdir = $self->{rootfs};
8ab34b87
DM
1724
1725 my $required = $self->compute_required ([qw (php5 php5-cli libapache2-mod-php5 php5-gd)]);
1726
1727 $self->cache_packages ($required);
1728
1729 $self->ve_dpkg ('install', @$required);
1730
1731 if ($memlimit) {
1732 $self->run_command ("sed -e 's/^\\s*memory_limit\\s*=.*;/memory_limit = ${memlimit}M;/' -i $rootdir/etc/php5/apache2/php.ini");
1733 }
1734}
1735
1736sub install {
1737 my ($self, $pkglist, $unpack) = @_;
1738
1739 my $required = $self->compute_required ($pkglist);
1740
1741 $self->cache_packages ($required);
1742
1743 $self->ve_dpkg ($unpack ? 'unpack' : 'install', @$required);
1744}
1745
1746sub cleanup {
1747 my ($self, $distclean) = @_;
1748
1749 unlink $self->{logfile};
1750 unlink "$self->{targetname}.tar";
1751 unlink "$self->{targetname}.tar.gz";
1752
1753 $self->ve_destroy ();
1754 unlink ".veid";
1755
1756 rmtree $self->{cachedir} if $distclean && !$self->{config}->{cachedir};
1757
1758 rmtree $self->{infodir};
1759
1760}
1761
17621;