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