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