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