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