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