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