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