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