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