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