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