]>
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"; | |
380 | if ($suite eq 'squeeze') { | |
381 | $config->{ostype} = "debian-6.0"; | |
382 | } elsif ($suite eq 'lenny') { | |
383 | $config->{ostype} = "debian-5.0"; | |
384 | } elsif ($suite eq 'etch') { | |
385 | $config->{ostype} = "debian-4.0"; | |
386 | } elsif ($suite eq 'hardy') { | |
387 | $config->{ostype} = "ubuntu-8.04"; | |
388 | } elsif ($suite eq 'intrepid') { | |
389 | $config->{ostype} = "ubuntu-8.10"; | |
390 | } elsif ($suite eq 'jaunty') { | |
391 | $config->{ostype} = "ubuntu-9.04"; | |
392 | } else { | |
393 | die "unsupported debian suite '$suite'\n"; | |
394 | } | |
395 | ||
396 | my $name = $config->{name} || die "no 'name' specified\n"; | |
397 | ||
398 | $name =~ m/^[a-z][0-9a-z\-\*\.]+$/ || | |
399 | die "illegal characters in name '$name'\n"; | |
400 | ||
401 | my $version = $config->{version}; | |
402 | die "no 'version' specified\n" if !$version; | |
403 | die "no 'section' specified\n" if !$config->{section}; | |
404 | die "no 'description' specified\n" if !$config->{headline}; | |
405 | die "no 'maintainer' specified\n" if !$config->{maintainer}; | |
406 | ||
407 | if ($name =~ m/^$config->{ostype}/) { | |
408 | $self->{targetname} = "${name}_${version}_$config->{architecture}"; | |
409 | } else { | |
410 | $self->{targetname} = "$config->{ostype}-${name}_" . | |
411 | "${version}_$config->{architecture}"; | |
412 | } | |
413 | ||
414 | if (!$config->{source}) { | |
415 | if ($suite eq 'etch' || $suite eq 'lenny' || $suite eq 'squeeze') { | |
416 | push @{$config->{source}}, "http://ftp.debian.org/debian SUITE main contrib"; | |
417 | push @{$config->{source}}, "http://ftp.debian.org/debian SUITE-updates main contrib" | |
418 | if ($suite eq 'squeeze'); | |
419 | push @{$config->{source}}, "http://security.debian.org SUITE/updates main contrib"; | |
420 | } elsif ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') { | |
421 | my $comp = "main restricted universe multiverse"; | |
422 | push @{$config->{source}}, "http://archive.ubuntu.com/ubuntu SUITE $comp"; | |
423 | push @{$config->{source}}, "http://archive.ubuntu.com/ubuntu SUITE-updates $comp"; | |
424 | push @{$config->{source}}, "http://archive.ubuntu.com/ubuntu SUITE-security $comp"; | |
425 | } | |
426 | } | |
427 | ||
428 | my $sources = undef; | |
429 | ||
430 | foreach my $s (@{$config->{source}}) { | |
431 | if ($s =~ m@^\s*((http|ftp)://\S+)\s+(\S+)((\s+(\S+))+)$@) { | |
432 | my ($url, $su, $components) = ($1, $3, $4); | |
433 | $su =~ s/SUITE/$suite/; | |
434 | $components =~ s/^\s+//; | |
435 | $components =~ s/\s+$//; | |
436 | my $ca; | |
437 | foreach my $co (split (/\s+/, $components)) { | |
438 | push @$ca, $co; | |
439 | } | |
440 | $ca = ['main'] if !$ca; | |
441 | ||
442 | push @$sources, { | |
443 | source => $url, | |
444 | comp => $ca, | |
445 | suite => $su, | |
446 | }; | |
447 | } else { | |
448 | die "syntax error in source spezification '$s'\n"; | |
449 | } | |
450 | } | |
451 | ||
452 | foreach my $m (@{$config->{mirror}}) { | |
453 | if ($m =~ m@^\s*((http|ftp)://\S+)\s*=>\s*((http|ftp)://\S+)\s*$@) { | |
454 | my ($ms, $md) = ($1, $3); | |
455 | my $found; | |
456 | foreach my $ss (@$sources) { | |
457 | if ($ss->{source} eq $ms) { | |
458 | $found = 1; | |
459 | $ss->{mirror} = $md; | |
460 | last; | |
461 | } | |
462 | } | |
463 | die "unusable mirror $ms\n" if !$found; | |
464 | } else { | |
465 | die "syntax error in mirror spezification '$m'\n"; | |
466 | } | |
467 | } | |
468 | ||
469 | $self->{sources} = $sources; | |
470 | ||
471 | $self->{infodir} = "info"; | |
472 | ||
473 | $self->__allocate_ve (); | |
474 | ||
475 | $self->{cachedir} = ($config->{cachedir} || 'cache') . "/$suite";; | |
476 | ||
477 | my $incl = [qw (less ssh openssh-server logrotate)]; | |
478 | ||
479 | my $excl = [qw (modutils reiserfsprogs ppp pppconfig pppoe | |
480 | pppoeconf nfs-common mtools ntp)]; | |
481 | ||
482 | # ubuntu has too many dependencies on udev, so | |
483 | # we cannot exclude it (instead we disable udevd) | |
484 | if ($suite eq 'hardy') { | |
485 | push @$excl, qw(kbd); | |
486 | push @$excl, qw(apparmor apparmor-utils ntfs-3g | |
487 | friendly-recovery); | |
488 | } elsif($suite eq 'intrepid' || $suite eq 'jaunty') { | |
489 | push @$excl, qw(apparmor apparmor-utils libapparmor1 libapparmor-perl | |
490 | libntfs-3g28 ntfs-3g friendly-recovery); | |
491 | } else { | |
492 | push @$excl, qw(udev module-init-tools pciutils hdparm | |
493 | memtest86+ parted); | |
494 | } | |
495 | ||
496 | $self->{incl} = $incl; | |
497 | $self->{excl} = $excl; | |
498 | ||
499 | return $self; | |
500 | } | |
501 | ||
502 | sub initialize { | |
503 | my ($self) = @_; | |
504 | ||
505 | my $infodir = $self->{infodir}; | |
506 | my $arch = $self->{config}->{architecture}; | |
507 | ||
508 | rmtree $infodir; | |
509 | mkpath $infodir; | |
510 | ||
511 | # truncate log | |
512 | my $logfd = $self->{logfd} = IO::File->new (">$self->{logfile}") || | |
513 | die "unable to open log file"; | |
514 | ||
515 | foreach my $ss (@{$self->{sources}}) { | |
516 | my $src = $ss->{mirror} || $ss->{source}; | |
517 | my $path = "dists/$ss->{suite}/Release"; | |
518 | my $url = "$src/$path"; | |
519 | my $target = __url_to_filename ("$ss->{source}/$path"); | |
520 | eval { | |
521 | $self->download ($url, "$infodir/$target"); | |
522 | $self->download ("$url.gpg", "$infodir/$target.gpg"); | |
523 | # fixme: impl. verify (needs --keyring option) | |
524 | }; | |
525 | if (my $err = $@) { | |
526 | print $logfd $@; | |
527 | warn "Release info ignored\n"; | |
528 | }; | |
529 | foreach my $comp (@{$ss->{comp}}) { | |
530 | $path = "dists/$ss->{suite}/$comp/binary-$arch/Packages.gz"; | |
531 | $target = "$infodir/" . __url_to_filename ("$ss->{source}/$path"); | |
532 | my $pkgsrc = "$src/$path"; | |
533 | $self->download ($pkgsrc, $target); | |
534 | $self->run_command ("gzip -d '$target'"); | |
535 | } | |
536 | } | |
537 | } | |
538 | ||
539 | sub write_config { | |
540 | my ($self, $filename, $size) = @_; | |
541 | ||
542 | my $config = $self->{config}; | |
543 | ||
544 | my $data = ''; | |
545 | ||
546 | $data .= "Name: $config->{name}\n"; | |
547 | $data .= "Version: $config->{version}\n"; | |
548 | $data .= "Type: openvz\n"; | |
549 | $data .= "OS: $config->{ostype}\n"; | |
550 | $data .= "Section: $config->{section}\n"; | |
551 | $data .= "Maintainer: $config->{maintainer}\n"; | |
552 | $data .= "Architecture: $config->{architecture}\n"; | |
553 | $data .= "Installed-Size: $size\n"; | |
554 | ||
555 | # optional | |
556 | $data .= "Infopage: $config->{infopage}\n" if $config->{infopage}; | |
557 | $data .= "ManageUrl: $config->{manageurl}\n" if $config->{manageurl}; | |
558 | $data .= "Certified: $config->{certified}\n" if $config->{certified}; | |
559 | ||
560 | # description | |
561 | $data .= "Description: $config->{headline}\n"; | |
562 | $data .= "$config->{description}\n" if $config->{description}; | |
563 | ||
564 | write_file ($data, $filename, 0644); | |
565 | } | |
566 | ||
567 | sub finalize { | |
568 | my ($self, $opts) = @_; | |
569 | ||
570 | my $suite = $self->{config}->{suite}; | |
571 | my $infodir = $self->{infodir}; | |
572 | my $arch = $self->{config}->{architecture}; | |
573 | ||
574 | my $instpkgs = $self->read_installed (); | |
575 | my $pkginfo = $self->pkginfo(); | |
576 | my $veid = $self->{veid}; | |
577 | my $rootdir = $self->vz_root_dir(); | |
578 | ||
579 | my $vestat = $self->ve_status(); | |
580 | die "ve not running - unable to finalize\n" if !$vestat->{running}; | |
581 | ||
582 | # cleanup mysqld | |
583 | if (-f "$rootdir/etc/init.d/mysql") { | |
584 | $self->ve_command ("/etc/init.d/mysql stop"); | |
585 | } | |
586 | ||
587 | if (!($opts->{keepmycnf} || (-f "$rootdir/etc/init.d/mysql_randompw"))) { | |
588 | unlink "$rootdir/root/.my.cnf"; | |
589 | } | |
590 | ||
591 | if ($suite eq 'etch') { | |
592 | # enable apache2 startup | |
593 | if ($instpkgs->{apache2}) { | |
594 | write_file ("NO_START=0\n", "$rootdir/etc/default/apache2"); | |
595 | } else { | |
596 | unlink "$rootdir/etc/default/apache2"; | |
597 | } | |
598 | } | |
599 | $self->logmsg ("cleanup package status\n"); | |
600 | # prevent auto selection of all standard, required or important | |
601 | # packages which are not installed | |
602 | foreach my $pkg (keys %$pkginfo) { | |
603 | my $pri = $pkginfo->{$pkg}->{priority}; | |
604 | if ($pri && ($pri eq 'required' || $pri eq 'important' | |
605 | || $pri eq 'standard')) { | |
606 | if (!$instpkgs->{$pkg}) { | |
607 | $self->ve_dpkg_set_selection ($pkg, 'purge'); | |
608 | } | |
609 | } | |
610 | } | |
611 | ||
612 | $self->ve_command ("apt-get clean"); | |
613 | ||
614 | $self->logmsg ("update available package list\n"); | |
615 | ||
616 | $self->ve_command ("dpkg --clear-avail"); | |
617 | foreach my $ss (@{$self->{sources}}) { | |
618 | my $relsrc = __url_to_filename ("$ss->{source}/dists/$ss->{suite}/Release"); | |
619 | if (-f "$infodir/$relsrc" && -f "$infodir/$relsrc.gpg") { | |
620 | $self->run_command ("cp '$infodir/$relsrc' '$rootdir/var/lib/apt/lists/$relsrc'"); | |
621 | $self->run_command ("cp '$infodir/$relsrc.gpg' '$rootdir/var/lib/apt/lists/$relsrc.gpg'"); | |
622 | } | |
623 | foreach my $comp (@{$ss->{comp}}) { | |
624 | my $src = __url_to_filename ("$ss->{source}/dists/$ss->{suite}/" . | |
625 | "$comp/binary-$arch/Packages"); | |
626 | my $target = "/var/lib/apt/lists/$src"; | |
627 | $self->run_command ("cp '$infodir/$src' '$rootdir/$target'"); | |
628 | $self->ve_command ("dpkg --merge-avail '$target'"); | |
629 | } | |
630 | } | |
631 | ||
632 | # set dselect default method | |
633 | write_file ("apt apt\n", "$rootdir/var/lib/dpkg/cmethopt"); | |
634 | ||
635 | $self->ve_divert_remove ("/usr/sbin/policy-rc.d"); | |
636 | ||
637 | $self->ve_divert_remove ("/sbin/start-stop-daemon"); | |
638 | ||
639 | $self->ve_divert_remove ("/sbin/init"); | |
640 | ||
641 | # finally stop the VE | |
642 | $self->run_command ("vzctl stop $veid --fast"); | |
643 | $rootdir = $self->vz_priv_dir(); | |
644 | ||
645 | unlink "$rootdir/sbin/defenv"; | |
646 | ||
647 | unlink <$rootdir/root/dead.letter*>; | |
648 | ||
649 | unlink "$rootdir/var/log/init.log"; | |
650 | ||
651 | unlink "$rootdir/aquota.group"; | |
652 | ||
653 | unlink "$rootdir/aquota.user"; | |
654 | ||
655 | write_file ("", "$rootdir/var/log/syslog"); | |
656 | ||
657 | $self->logmsg ("detecting final size: "); | |
658 | ||
659 | my $sizestr = $self->run_command ("du -sm $rootdir", undef, 1); | |
660 | my $size; | |
661 | if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) { | |
662 | $size = $1; | |
663 | } else { | |
664 | die "unable to detect size\n"; | |
665 | } | |
666 | $self->logmsg ("$size MB\n"); | |
667 | ||
668 | $self->write_config ("$rootdir/etc/appliance.info", $size); | |
669 | ||
670 | $self->logmsg ("creating final appliance archive\n"); | |
671 | ||
672 | my $target = "$self->{targetname}.tar"; | |
673 | unlink $target; | |
674 | unlink "$target.gz"; | |
675 | ||
676 | $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info"); | |
677 | $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info ."); | |
678 | $self->run_command ("gzip $target"); | |
679 | } | |
680 | ||
681 | sub read_installed { | |
682 | my ($self) = @_; | |
683 | ||
684 | my $rootdir = $self->vz_priv_dir(); | |
685 | ||
686 | my $pkgfilelist = "$rootdir/var/lib/dpkg/status"; | |
687 | local $/ = ''; | |
688 | open (PKGLST, "<$pkgfilelist") || | |
689 | die "unable to open '$pkgfilelist'"; | |
690 | ||
691 | my $pkglist = {}; | |
692 | ||
693 | while (my $rec = <PKGLST>) { | |
694 | chomp $rec; | |
695 | $rec =~ s/\n\s+/ /g; | |
696 | $rec .= "\n"; | |
697 | my $res = {}; | |
698 | ||
699 | while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) { | |
700 | $res->{lc $1} = $2; | |
701 | } | |
702 | ||
703 | my $pkg = $res->{'package'}; | |
704 | if (my $status = $res->{status}) { | |
705 | my @sa = split (/\s+/, $status); | |
706 | my $stat = $sa[0]; | |
707 | if ($stat && ($stat ne 'purge')) { | |
708 | $pkglist->{$pkg} = $res; | |
709 | } | |
710 | } | |
711 | } | |
712 | ||
713 | close (PKGLST); | |
714 | ||
715 | return $pkglist; | |
716 | } | |
717 | ||
718 | sub vz_root_dir { | |
719 | my ($self) = @_; | |
720 | ||
721 | my $veid = $self->{veid}; | |
722 | ||
723 | return "/var/lib/vz/root/$veid"; | |
724 | } | |
725 | ||
726 | sub vz_priv_dir { | |
727 | my ($self) = @_; | |
728 | ||
729 | my $veid = $self->{veid}; | |
730 | ||
731 | return "/var/lib/vz/private/$veid"; | |
732 | } | |
733 | ||
734 | sub ve_status { | |
735 | my ($self) = @_; | |
736 | ||
737 | my $veid = $self->{veid}; | |
738 | ||
739 | my $res = $self->run_command ("vzctl status $veid", undef, 1); | |
740 | chomp $res; | |
741 | ||
742 | if ($res =~ m/^CTID\s+$veid\s+(exist|deleted)\s+(mounted|unmounted)\s+(running|down)$/) { | |
743 | return { | |
744 | exist => $1 eq 'exist', | |
745 | mounted => $2 eq 'mounted', | |
746 | running => $3 eq 'running', | |
747 | }; | |
748 | } else { | |
749 | die "unable to parse ve status"; | |
750 | } | |
751 | } | |
752 | ||
753 | sub ve_command { | |
754 | my ($self, $cmd, $input) = @_; | |
755 | ||
756 | my $veid = $self->{veid}; | |
757 | ||
758 | if (ref ($cmd) eq 'ARRAY') { | |
759 | unshift @$cmd, 'vzctl', 'exec2', $veid, 'defenv'; | |
760 | $self->run_command ($cmd, $input); | |
761 | } else { | |
762 | $self->run_command ("vzctl exec2 $veid defenv $cmd", $input); | |
763 | } | |
764 | } | |
765 | ||
766 | # like ve_command, but pipes stdin correctly | |
767 | sub ve_exec { | |
768 | my ($self, @cmd) = @_; | |
769 | ||
770 | my $veid = $self->{veid}; | |
771 | ||
772 | my $reader; | |
773 | my $pid = open2($reader, "<&STDIN", 'vzctl', 'exec2', $veid, | |
774 | 'defenv', @cmd) || die "unable to exec command"; | |
775 | ||
776 | while (defined (my $line = <$reader>)) { | |
777 | $self->logmsg ($line); | |
778 | } | |
779 | ||
780 | waitpid ($pid, 0); | |
781 | my $rc = $? >> 8; | |
782 | ||
783 | die "ve_exec failed - status $rc\n" if $rc != 0; | |
784 | } | |
785 | ||
786 | sub ve_divert_add { | |
787 | my ($self, $filename) = @_; | |
788 | ||
789 | $self->ve_command ("dpkg-divert --add --divert '$filename.distrib' " . | |
790 | "--rename '$filename'"); | |
791 | } | |
792 | sub ve_divert_remove { | |
793 | my ($self, $filename) = @_; | |
794 | ||
795 | my $rootdir = $self->vz_root_dir(); | |
796 | ||
797 | unlink "$rootdir/$filename"; | |
798 | $self->ve_command ("dpkg-divert --remove --rename '$filename'"); | |
799 | } | |
800 | ||
801 | sub ve_debconfig_set { | |
802 | my ($self, $dcdata) = @_; | |
803 | ||
804 | my $rootdir = $self->vz_root_dir(); | |
805 | my $cfgfile = "/tmp/debconf.txt"; | |
806 | write_file ($dcdata, "$rootdir/$cfgfile"); | |
807 | $self->ve_command ("debconf-set-selections $cfgfile"); | |
808 | unlink "$rootdir/$cfgfile"; | |
809 | } | |
810 | ||
811 | sub ve_dpkg_set_selection { | |
812 | my ($self, $pkg, $status) = @_; | |
813 | ||
814 | $self->ve_command ("dpkg --set-selections", "$pkg $status"); | |
815 | } | |
816 | ||
817 | sub ve_dpkg { | |
818 | my ($self, $cmd, @pkglist) = @_; | |
819 | ||
820 | return if !scalar (@pkglist); | |
821 | ||
822 | my $pkginfo = $self->pkginfo(); | |
823 | ||
824 | my $rootdir = $self->vz_root_dir(); | |
825 | my $cachedir = $self->{cachedir}; | |
826 | ||
827 | my @files; | |
828 | ||
829 | foreach my $pkg (@pkglist) { | |
830 | my $filename = $self->getpkgfile ($pkg); | |
831 | $self->run_command ("cp '$cachedir/$filename' '$rootdir/$filename'"); | |
832 | push @files, "/$filename"; | |
833 | $self->logmsg ("$cmd: $pkg\n"); | |
834 | } | |
835 | ||
836 | my $fl = join (' ', @files); | |
837 | ||
838 | if ($cmd eq 'install') { | |
839 | $self->ve_command ("dpkg --force-depends --force-confold --install $fl"); | |
840 | } elsif ($cmd eq 'unpack') { | |
841 | $self->ve_command ("dpkg --force-depends --unpack $fl"); | |
842 | } else { | |
843 | die "internal error"; | |
844 | } | |
845 | ||
846 | foreach my $fn (@files) { unlink "$rootdir$fn"; } | |
847 | } | |
848 | ||
849 | sub ve_destroy { | |
850 | my ($self) = @_; | |
851 | ||
852 | my $veid = $self->{veid}; # fixme | |
853 | ||
854 | my $vestat = $self->ve_status(); | |
855 | if ($vestat->{running}) { | |
856 | $self->run_command ("vzctl stop $veid --fast"); | |
857 | } elsif ($vestat->{mounted}) { | |
858 | $self->run_command ("vzctl umount $veid"); | |
859 | } | |
860 | if ($vestat->{exist}) { | |
861 | $self->run_command ("vzctl destroy $veid"); | |
862 | } else { | |
863 | unlink $self->{veconffile}; | |
864 | } | |
865 | } | |
866 | ||
867 | sub ve_init { | |
868 | my ($self) = @_; | |
869 | ||
870 | my $root = $self->vz_root_dir(); | |
871 | my $priv = $self->vz_priv_dir(); | |
872 | ||
873 | my $veid = $self->{veid}; # fixme | |
874 | ||
875 | $self->logmsg ("initialize VE $veid\n"); | |
876 | ||
877 | while (1) { | |
878 | my $vestat = $self->ve_status(); | |
879 | if ($vestat->{running}) { | |
880 | $self->run_command ("vzctl stop $veid --fast"); | |
881 | } elsif ($vestat->{mounted}) { | |
882 | $self->run_command ("vzctl umount $veid"); | |
883 | } else { | |
884 | last; | |
885 | } | |
886 | sleep (1); | |
887 | } | |
888 | ||
889 | rmtree $root; | |
890 | rmtree $priv; | |
891 | mkpath $root; | |
892 | mkpath $priv; | |
893 | ||
894 | $self->run_command ("vzctl mount $veid"); | |
895 | } | |
896 | ||
897 | sub __deb_version_cmp { | |
898 | my ($cur, $op, $new) = @_; | |
899 | ||
900 | if (system("dpkg", "--compare-versions", $cur, $op, $new) == 0) { | |
901 | return 1; | |
902 | } | |
903 | ||
904 | return 0; | |
905 | } | |
906 | ||
907 | sub __parse_packages { | |
908 | my ($pkginfo, $filename, $src) = @_; | |
909 | ||
910 | local $/ = ''; | |
911 | open (PKGLST, "<$filename") || | |
912 | die "unable to open '$filename'"; | |
913 | ||
914 | while (my $rec = <PKGLST>) { | |
915 | $rec =~ s/\n\s+/ /g; | |
916 | chomp $rec; | |
917 | $rec .= "\n"; | |
918 | ||
919 | my $res = {}; | |
920 | ||
921 | while ($rec =~ s/^([^:]+):\s+(.*)\s*\n//) { | |
922 | $res->{lc $1} = $2; | |
923 | } | |
924 | ||
925 | my $pkg = $res->{'package'}; | |
926 | if ($pkg && $res->{'filename'}) { | |
927 | my $cur; | |
928 | if (my $info = $pkginfo->{$pkg}) { | |
929 | $cur = $info->{version}; | |
930 | } | |
931 | my $new = $res->{version}; | |
932 | if (!$cur || __deb_version_cmp ($cur, 'lt', $new)) { | |
933 | if ($src) { | |
934 | $res->{url} = "$src/$res->{'filename'}"; | |
935 | } else { | |
936 | die "no url for package '$pkg'" if !$res->{url}; | |
937 | } | |
938 | $pkginfo->{$pkg} = $res; | |
939 | } | |
940 | } | |
941 | } | |
942 | ||
943 | close (PKGLST); | |
944 | } | |
945 | ||
946 | sub pkginfo { | |
947 | my ($self) = @_; | |
948 | ||
949 | return $self->{pkginfo} if $self->{pkginfo}; | |
950 | ||
951 | my $infodir = $self->{infodir}; | |
952 | my $arch = $self->{config}->{architecture}; | |
953 | ||
954 | my $availfn = "$infodir/available"; | |
955 | ||
956 | my $pkginfo = {}; | |
957 | my $pkgcount = 0; | |
958 | ||
959 | # reading 'available' is faster, because it only contains latest version | |
960 | # (no need to do slow version compares) | |
961 | if (-f $availfn) { | |
962 | __parse_packages ($pkginfo, $availfn); | |
963 | $self->{pkginfo} = $pkginfo; | |
964 | return $pkginfo; | |
965 | } | |
966 | ||
967 | $self->logmsg ("generating available package list\n"); | |
968 | ||
969 | foreach my $ss (@{$self->{sources}}) { | |
970 | foreach my $comp (@{$ss->{comp}}) { | |
971 | my $url = "$ss->{source}/dists/$ss->{suite}/$comp/binary-$arch/Packages"; | |
972 | my $pkgfilelist = "$infodir/" . __url_to_filename ($url); | |
973 | ||
974 | my $src = $ss->{mirror} || $ss->{source}; | |
975 | ||
976 | __parse_packages ($pkginfo, $pkgfilelist, $src); | |
977 | } | |
978 | } | |
979 | ||
980 | if (my $dep = $self->{config}->{depends}) { | |
981 | foreach my $d (split (/,/, $dep)) { | |
982 | if ($d =~ m/^\s*(\S+)\s*(\((\S+)\s+(\S+)\)\s*)?$/) { | |
983 | my ($pkg, $op, $rver) = ($1, $3, $4); | |
984 | $self->logmsg ("checking dependencies: $d\n"); | |
985 | my $info = $pkginfo->{$pkg}; | |
986 | die "package '$pkg' not available\n" if !$info; | |
987 | if ($op) { | |
988 | my $cver = $info->{version}; | |
989 | if (!__deb_version_cmp ($cver, $op, $rver)) { | |
990 | die "detected wrong version '$cver'\n"; | |
991 | } | |
992 | } | |
993 | } else { | |
994 | die "syntax error in depends field"; | |
995 | } | |
996 | } | |
997 | } | |
998 | ||
999 | $self->{pkginfo} = $pkginfo; | |
1000 | ||
1001 | my $tmpfn = "$availfn.tmp$$"; | |
1002 | my $fd = IO::File->new (">$tmpfn"); | |
1003 | foreach my $pkg (sort keys %$pkginfo) { | |
1004 | my $info = $pkginfo->{$pkg}; | |
1005 | print $fd "package: $pkg\n"; | |
1006 | foreach my $k (sort keys %$info) { | |
1007 | next if $k eq 'description'; | |
1008 | next if $k eq 'package'; | |
1009 | my $v = $info->{$k}; | |
1010 | print $fd "$k: $v\n" if $v; | |
1011 | } | |
1012 | print $fd "description: $info->{description}\n" if $info->{description}; | |
1013 | print $fd "\n"; | |
1014 | } | |
1015 | close ($fd); | |
1016 | ||
1017 | rename ($tmpfn, $availfn); | |
1018 | ||
1019 | return $pkginfo; | |
1020 | } | |
1021 | ||
1022 | sub __record_provides { | |
1023 | my ($pkginfo, $closure, $list, $skipself) = @_; | |
1024 | ||
1025 | foreach my $pname (@$list) { | |
1026 | my $info = $pkginfo->{$pname}; | |
1027 | # fixme: if someone install packages directly using dpkg, there | |
1028 | # is no entry in 'available', only in 'status'. In that case, we | |
1029 | # should extract info from $instpkgs | |
1030 | if (!$info) { | |
1031 | warn "hint: ignoring provides for '$pname' - package not in 'available' list.\n"; | |
1032 | next; | |
1033 | } | |
1034 | if (my $prov = $info->{provides}) { | |
1035 | my @pl = split (',', $prov); | |
1036 | foreach my $p (@pl) { | |
1037 | $p =~ m/\s*(\S+)/; | |
1038 | if (!($skipself && (grep { $1 eq $_ } @$list))) { | |
1039 | $closure->{$1} = 1; | |
1040 | } | |
1041 | } | |
1042 | } | |
1043 | $closure->{$pname} = 1 if !$skipself; | |
1044 | } | |
1045 | } | |
1046 | ||
1047 | sub closure { | |
1048 | my ($self, $closure, $list) = @_; | |
1049 | ||
1050 | my $pkginfo = $self->pkginfo(); | |
1051 | ||
1052 | # first, record provided packages | |
1053 | __record_provides ($pkginfo, $closure, $list, 1); | |
1054 | ||
1055 | my $pkgs = {}; | |
1056 | ||
1057 | # then resolve dependencies | |
1058 | foreach my $pname (@$list) { | |
1059 | __closure_single ($pkginfo, $closure, $pkgs, $pname, $self->{excl}); | |
1060 | } | |
1061 | ||
1062 | return [ keys %$pkgs ]; | |
1063 | } | |
1064 | ||
1065 | sub __closure_single { | |
1066 | my ($pkginfo, $closure, $pkgs, $pname, $excl) = @_; | |
1067 | ||
1068 | $pname =~ s/^\s+//; | |
1069 | $pname =~ s/\s+$//; | |
1070 | ||
1071 | return if $closure->{$pname}; | |
1072 | ||
1073 | my $info = $pkginfo->{$pname} || die "no such package '$pname'"; | |
1074 | ||
1075 | my $dep = $info->{depends}; | |
1076 | my $predep = $info->{'pre-depends'}; | |
1077 | ||
1078 | my $size = $info->{size}; | |
1079 | my $url = $info->{url}; | |
1080 | ||
1081 | $url || die "$pname: no url for package '$pname'"; | |
1082 | ||
1083 | $pkgs->{$pname} = 1; | |
1084 | ||
1085 | __record_provides ($pkginfo, $closure, [$pname]) if $info->{provides}; | |
1086 | ||
1087 | $closure->{$pname} = 1; | |
1088 | ||
1089 | #print "$url\n"; | |
1090 | ||
1091 | my @l; | |
1092 | ||
1093 | push @l, split (/,/, $predep) if $predep; | |
1094 | push @l, split (/,/, $dep) if $dep; | |
1095 | ||
1096 | DEPEND: foreach my $p (@l) { | |
1097 | my @l1 = split (/\|/, $p); | |
1098 | foreach my $p1 (@l1) { | |
1099 | if ($p1 =~ m/^\s*(\S+).*/) { | |
1100 | #printf (STDERR "$pname: $p --> $1\n"); | |
1101 | if ($closure->{$1}) { | |
1102 | next DEPEND; # dependency already met | |
1103 | } | |
1104 | } | |
1105 | } | |
1106 | # search for non-excluded alternative | |
1107 | my $found; | |
1108 | foreach my $p1 (@l1) { | |
1109 | if ($p1 =~ m/^\s*(\S+).*/) { | |
1110 | next if grep { $1 eq $_ } @$excl; | |
1111 | $found = $1; | |
1112 | last; | |
1113 | } | |
1114 | } | |
1115 | die "package '$pname' depends on exclusion '$p'\n" if !$found; | |
1116 | ||
1117 | #printf (STDERR "$pname: $p --> $found\n"); | |
1118 | ||
1119 | __closure_single ($pkginfo, $closure, $pkgs, $found, $excl); | |
1120 | } | |
1121 | } | |
1122 | ||
1123 | sub cache_packages { | |
1124 | my ($self, $pkglist) = @_; | |
1125 | ||
1126 | foreach my $pkg (@$pkglist) { | |
1127 | $self->getpkgfile ($pkg); | |
1128 | } | |
1129 | } | |
1130 | ||
1131 | sub getpkgfile { | |
1132 | my ($self, $pkg) = @_; | |
1133 | ||
1134 | my $pkginfo = $self->pkginfo(); | |
1135 | my $info = $pkginfo->{$pkg} || die "no such package '$pkg'"; | |
1136 | my $cachedir = $self->{cachedir}; | |
1137 | ||
1138 | my $url = $info->{url}; | |
1139 | ||
1140 | my $filename; | |
1141 | if ($url =~ m|/([^/]+.deb)$|) { | |
1142 | $filename = $1; | |
1143 | } else { | |
1144 | die "internal error"; | |
1145 | } | |
1146 | ||
1147 | return $filename if -f "$cachedir/$filename"; | |
1148 | ||
1149 | mkpath $cachedir; | |
1150 | ||
1151 | $self->download ($url, "$cachedir/$filename"); | |
1152 | ||
1153 | return $filename; | |
1154 | } | |
1155 | ||
1156 | sub install_init_script { | |
1157 | my ($self, $script, $runlevel, $prio) = @_; | |
1158 | ||
1159 | my $suite = $self->{config}->{suite}; | |
1160 | my $rootdir = $self->vz_root_dir(); | |
1161 | ||
1162 | my $base = basename ($script); | |
1163 | my $target = "$rootdir/etc/init.d/$base"; | |
1164 | ||
1165 | $self->run_command ("install -m 0755 '$script' '$target'"); | |
1166 | if ($suite eq 'etch' || $suite eq 'lenny') { | |
1167 | $self->ve_command ("update-rc.d $base start $prio $runlevel ."); | |
1168 | } else { | |
1169 | $self->ve_command ("insserv $base"); | |
1170 | } | |
1171 | ||
1172 | return $target; | |
1173 | } | |
1174 | ||
1175 | sub bootstrap { | |
1176 | my ($self, $opts) = @_; | |
1177 | ||
1178 | my $pkginfo = $self->pkginfo(); | |
1179 | my $veid = $self->{veid}; | |
1180 | my $suite = $self->{config}->{suite}; | |
1181 | ||
1182 | my $important = [ @{$self->{incl}} ]; | |
1183 | my $required; | |
1184 | my $standard; | |
1185 | ||
1186 | my $mta = $opts->{exim} ? 'exim' : 'postfix'; | |
1187 | ||
1188 | if ($mta eq 'postfix') { | |
1189 | push @$important, "postfix"; | |
1190 | } | |
1191 | ||
1192 | foreach my $p (keys %$pkginfo) { | |
1193 | next if grep { $p eq $_ } @{$self->{excl}}; | |
1194 | my $pri = $pkginfo->{$p}->{priority}; | |
1195 | next if !$pri; | |
1196 | next if $mta ne 'exim' && $p =~ m/exim/; | |
1197 | next if $p =~ m/(selinux|semanage|policycoreutils)/; | |
1198 | ||
1199 | push @$required, $p if $pri eq 'required'; | |
1200 | push @$important, $p if $pri eq 'important'; | |
1201 | push @$standard, $p if $pri eq 'standard' && !$opts->{minimal}; | |
1202 | } | |
1203 | ||
1204 | my $closure = {}; | |
1205 | $required = $self->closure ($closure, $required); | |
1206 | $important = $self->closure ($closure, $important); | |
1207 | ||
1208 | if (!$opts->{minimal}) { | |
1209 | push @$standard, 'xbase-clients'; | |
1210 | $standard = $self->closure ($closure, $standard); | |
1211 | } | |
1212 | ||
1213 | # test if we have all 'ubuntu-minimal' and 'ubuntu-standard' packages | |
1214 | # except those explicitly excluded | |
1215 | if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') { | |
1216 | my $mdeps = $pkginfo->{'ubuntu-minimal'}->{depends}; | |
1217 | foreach my $d (split (/,/, $mdeps)) { | |
1218 | if ($d =~ m/^\s*(\S+)$/) { | |
1219 | my $pkg = $1; | |
1220 | next if $closure->{$pkg}; | |
1221 | next if grep { $pkg eq $_ } @{$self->{excl}}; | |
1222 | die "missing ubuntu-minimal package '$pkg'\n"; | |
1223 | } | |
1224 | } | |
1225 | if (!$opts->{minimal}) { | |
1226 | $mdeps = $pkginfo->{'ubuntu-standard'}->{depends}; | |
1227 | foreach my $d (split (/,/, $mdeps)) { | |
1228 | if ($d =~ m/^\s*(\S+)$/) { | |
1229 | my $pkg = $1; | |
1230 | next if $closure->{$pkg}; | |
1231 | next if grep { $pkg eq $_ } @{$self->{excl}}; | |
1232 | die "missing ubuntu-standard package '$pkg'\n"; | |
1233 | } | |
1234 | } | |
1235 | } | |
1236 | } | |
1237 | ||
1238 | # download/cache all files first | |
1239 | $self->cache_packages ($required); | |
1240 | $self->cache_packages ($important); | |
1241 | $self->cache_packages ($standard); | |
1242 | ||
1243 | my $rootdir = $self->vz_priv_dir(); | |
1244 | ||
1245 | # extract required packages first | |
1246 | $self->logmsg ("create basic environment\n"); | |
1247 | foreach my $p (@$required) { | |
1248 | my $filename = $self->getpkgfile ($p); | |
1249 | $self->run_command ("ar -p '$self->{cachedir}/$filename' data.tar.gz | zcat | tar -C '$rootdir' -xf -"); | |
1250 | } | |
1251 | ||
1252 | # fake dpkg status | |
1253 | my $data = "Package: dpkg\n" . | |
1254 | "Version: $pkginfo->{dpkg}->{version}\n" . | |
1255 | "Status: install ok installed\n"; | |
1256 | ||
1257 | write_file ($data, "$rootdir/var/lib/dpkg/status"); | |
1258 | write_file ("", "$rootdir/var/lib/dpkg/info/dpkg.list"); | |
1259 | write_file ("", "$rootdir/var/lib/dpkg/available"); | |
1260 | ||
1261 | $data = ''; | |
1262 | foreach my $ss (@{$self->{sources}}) { | |
1263 | my $url = $ss->{source}; | |
1264 | my $comp = join (' ', @{$ss->{comp}}); | |
1265 | $data .= "deb $url $ss->{suite} $comp\n\n"; | |
1266 | } | |
1267 | ||
1268 | write_file ($data, "$rootdir/etc/apt/sources.list"); | |
1269 | ||
1270 | $data = "# UNCONFIGURED FSTAB FOR BASE SYSTEM\n"; | |
1271 | write_file ($data, "$rootdir/etc/fstab", 0644); | |
1272 | ||
1273 | write_file ("localhost\n", "$rootdir/etc/hostname", 0644); | |
1274 | ||
1275 | # avoid warnings about non-existent resolv.conf | |
1276 | write_file ("", "$rootdir/etc/resolv.conf", 0644); | |
1277 | ||
1278 | $data = "auto lo\niface lo inet loopback\n"; | |
1279 | write_file ($data, "$rootdir/etc/network/interfaces", 0644); | |
1280 | ||
1281 | # setup devices | |
1282 | $self->run_command ("tar xzf '$devicetar' -C '$rootdir'"); | |
1283 | ||
1284 | # avoid warnings about missing default locale | |
1285 | write_file ("LANG=\"C\"\n", "$rootdir/etc/default/locale", 0644); | |
1286 | ||
1287 | # fake init | |
1288 | rename ("$rootdir/sbin/init", "$rootdir/sbin/init.org"); | |
1289 | $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'"); | |
1290 | ||
1291 | $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'"); | |
1292 | ||
1293 | $self->run_command ("vzctl start $veid"); | |
1294 | $rootdir = $self->vz_root_dir(); | |
1295 | ||
1296 | $self->logmsg ("initialize ld cache\n"); | |
1297 | $self->ve_command ("/sbin/ldconfig"); | |
1298 | $self->run_command ("ln -sf mawk '$rootdir/usr/bin/awk'"); | |
1299 | ||
1300 | $self->logmsg ("installing packages\n"); | |
1301 | ||
1302 | $self->ve_dpkg ('install', 'base-files', 'base-passwd'); | |
1303 | ||
1304 | $self->ve_dpkg ('install', 'dpkg'); | |
1305 | ||
1306 | $self->run_command ("ln -sf /usr/share/zoneinfo/UTC '$rootdir/etc/localtime'"); | |
1307 | ||
1308 | $self->ve_dpkg ('install', 'libc6'); | |
1309 | $self->ve_dpkg ('install', 'perl-base'); | |
1310 | ||
1311 | unlink "$rootdir/usr/bin/awk"; | |
1312 | ||
1313 | $self->ve_dpkg ('install', 'mawk'); | |
1314 | $self->ve_dpkg ('install', 'debconf'); | |
1315 | ||
1316 | # unpack required packages | |
1317 | foreach my $p (@$required) { | |
1318 | $self->ve_dpkg ('unpack', $p); | |
1319 | } | |
1320 | ||
1321 | rename ("$rootdir/sbin/init.org", "$rootdir/sbin/init"); | |
1322 | $self->ve_divert_add ("/sbin/init"); | |
1323 | $self->run_command ("cp '$fake_init' '$rootdir/sbin/init'"); | |
1324 | ||
1325 | # disable service activation | |
1326 | $self->ve_divert_add ("/usr/sbin/policy-rc.d"); | |
1327 | $data = "#!/bin/sh\nexit 101\n"; | |
1328 | write_file ($data, "$rootdir/usr/sbin/policy-rc.d", 755); | |
1329 | ||
1330 | # disable start-stop-daemon | |
1331 | $self->ve_divert_add ("/sbin/start-stop-daemon"); | |
1332 | $data = <<EOD; | |
1333 | #!/bin/sh | |
1334 | echo | |
1335 | echo \"Warning: Fake start-stop-daemon called, doing nothing\" | |
1336 | EOD | |
1337 | write_file ($data, "$rootdir/sbin/start-stop-daemon", 0755); | |
1338 | ||
1339 | # disable udevd | |
1340 | $self->ve_divert_add ("/sbin/udevd"); | |
1341 | ||
1342 | if ($suite eq 'etch') { | |
1343 | # disable apache2 startup | |
1344 | write_file ("NO_START=1\n", "$rootdir/etc/default/apache2"); | |
1345 | } | |
1346 | ||
1347 | $self->logmsg ("configure required packages\n"); | |
1348 | $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a"); | |
1349 | ||
1350 | # set postfix defaults | |
1351 | if ($mta eq 'postfix') { | |
1352 | $data = "postfix postfix/main_mailer_type select Local only\n"; | |
1353 | $self->ve_debconfig_set ($data); | |
1354 | ||
1355 | $data = "postmaster: root\nwebmaster: root\n"; | |
1356 | write_file ($data, "$rootdir/etc/aliases"); | |
1357 | } | |
1358 | ||
1359 | if ($suite eq 'jaunty') { | |
1360 | # jaunty does not create /var/run/network, so network startup fails. | |
1361 | # so we do not use tmpfs for /var/run and /var/lock | |
1362 | $self->run_command ("sed -e 's/RAMRUN=yes/RAMRUN=no/' -e 's/RAMLOCK=yes/RAMLOCK=no/' -i $rootdir/etc/default/rcS"); | |
1363 | # and create the directory here | |
1364 | $self->run_command ("mkdir $rootdir/var/run/network"); | |
1365 | } | |
1366 | ||
1367 | # unpack base packages | |
1368 | foreach my $p (@$important) { | |
1369 | $self->ve_dpkg ('unpack', $p); | |
1370 | } | |
1371 | ||
1372 | # start loopback | |
1373 | $self->ve_command ("ifconfig lo up"); | |
1374 | ||
1375 | $self->logmsg ("configure important packages\n"); | |
1376 | $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a"); | |
1377 | ||
1378 | if (-d "$rootdir/etc/event.d") { | |
1379 | unlink <$rootdir/etc/event.d/tty*>; | |
1380 | } | |
1381 | ||
1382 | if (-f "$rootdir/etc/inittab") { | |
1383 | $self->run_command ("sed -i -e '/getty/d' '$rootdir/etc/inittab'"); | |
1384 | } | |
1385 | ||
1386 | # Link /etc/mtab to /proc/mounts, so df and friends will work: | |
1387 | unlink "$rootdir/etc/mtab"; | |
1388 | $self->ve_command ("ln -s /proc/mounts /etc/mtab"); | |
1389 | ||
1390 | # reset password | |
1391 | $self->ve_command ("usermod -L root"); | |
1392 | ||
1393 | # regenerate sshd host keys | |
1394 | $self->install_init_script ($script_ssh_init, 2, 14); | |
1395 | ||
1396 | if ($mta eq 'postfix') { | |
1397 | $data = "postfix postfix/main_mailer_type select No configuration\n"; | |
1398 | $self->ve_debconfig_set ($data); | |
1399 | ||
1400 | unlink "$rootdir/etc/mailname"; | |
1401 | write_file ($postfix_main_cf, "$rootdir/etc/postfix/main.cf"); | |
1402 | } | |
1403 | ||
1404 | if (!$opts->{minimal}) { | |
1405 | # unpack standard packages | |
1406 | foreach my $p (@$standard) { | |
1407 | $self->ve_dpkg ('unpack', $p); | |
1408 | } | |
1409 | ||
1410 | $self->logmsg ("configure standard packages\n"); | |
1411 | $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a"); | |
1412 | } | |
1413 | ||
1414 | # disable HWCLOCK access | |
1415 | $self->run_command ("echo 'HWCLOCKACCESS=no' >> '$rootdir/etc/default/rcS'"); | |
1416 | ||
1417 | # disable hald | |
1418 | $self->ve_divert_add ("/usr/sbin/hald"); | |
1419 | ||
1420 | # disable /dev/urandom init | |
1421 | $self->run_command ("install -m 0755 '$script_init_urandom' '$rootdir/etc/init.d/urandom'"); | |
1422 | ||
1423 | if ($suite eq 'etch' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') { | |
1424 | # avoid klogd start | |
1425 | $self->ve_divert_add ("/sbin/klogd"); | |
1426 | } | |
1427 | ||
1428 | # remove unnecessays sysctl entries to avoid warnings | |
1429 | my $cmd = 'sed'; | |
1430 | $cmd .= ' -e \'s/^\(kernel\.printk.*\)/#\1/\''; | |
1431 | $cmd .= ' -e \'s/^\(kernel\.maps_protect.*\)/#\1/\''; | |
1432 | $cmd .= ' -e \'s/^\(fs\.inotify\.max_user_watches.*\)/#\1/\''; | |
1433 | $cmd .= ' -e \'s/^\(vm\.mmap_min_addr.*\)/#\1/\''; | |
1434 | $cmd .= " -i '$rootdir/etc/sysctl.conf'"; | |
1435 | $self->run_command ($cmd); | |
1436 | ||
1437 | my $bindv6only = "$rootdir/etc/sysctl.d/bindv6only.conf"; | |
1438 | if (-f $bindv6only) { | |
1439 | $cmd = 'sed'; | |
1440 | $cmd .= ' -e \'s/^\(net\.ipv6\.bindv6only.*\)/#\1/\''; | |
1441 | $cmd .= " -i '$bindv6only'"; | |
1442 | $self->run_command ($cmd); | |
1443 | } | |
1444 | ||
1445 | if ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') { | |
1446 | # disable tty init (console-setup) | |
1447 | my $cmd = 'sed'; | |
1448 | $cmd .= ' -e \'s/^\(ACTIVE_CONSOLES=.*\)/ACTIVE_CONSOLES=/\''; | |
1449 | $cmd .= " -i '$rootdir/etc/default/console-setup'"; | |
1450 | $self->run_command ($cmd); | |
1451 | } | |
1452 | ||
1453 | if ($suite eq 'intrepid') { | |
1454 | # remove sysctl setup (avoid warnings at startup) | |
1455 | my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf"; | |
1456 | $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf"; | |
1457 | $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf"; | |
1458 | $self->run_command ("rm $filelist"); | |
1459 | } | |
1460 | if ($suite eq 'jaunty') { | |
1461 | # remove sysctl setup (avoid warnings at startup) | |
1462 | my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf"; | |
1463 | $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf"; | |
1464 | $self->run_command ("rm $filelist"); | |
1465 | } | |
1466 | } | |
1467 | ||
1468 | sub enter { | |
1469 | my ($self) = @_; | |
1470 | ||
1471 | my $veid = $self->{veid}; | |
1472 | ||
1473 | my $vestat = $self->ve_status(); | |
1474 | ||
1475 | if (!$vestat->{exist}) { | |
1476 | $self->logmsg ("Please create the appliance first (bootstrap)"); | |
1477 | return; | |
1478 | } | |
1479 | ||
1480 | if (!$vestat->{running}) { | |
1481 | $self->run_command ("vzctl start $veid"); | |
1482 | } | |
1483 | ||
1484 | system ("vzctl enter $veid"); | |
1485 | } | |
1486 | ||
1487 | sub ve_mysql_command { | |
1488 | my ($self, $sql, $password) = @_; | |
1489 | ||
1490 | #my $bootstrap = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " . | |
1491 | #"--skip-bdb --skip-innodb --skip-ndbcluster"; | |
1492 | ||
1493 | $self->ve_command ("mysql", $sql); | |
1494 | } | |
1495 | ||
1496 | sub ve_mysql_bootstrap { | |
1497 | my ($self, $sql, $password) = @_; | |
1498 | ||
1499 | my $cmd; | |
1500 | ||
1501 | my $suite = $self->{config}->{suite}; | |
1502 | ||
1503 | if ($suite eq 'squeeze') { | |
1504 | $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables"; | |
1505 | ||
1506 | } else { | |
1507 | $cmd = "/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables " . | |
1508 | "--skip-bdb --skip-innodb --skip-ndbcluster"; | |
1509 | } | |
1510 | ||
1511 | $self->ve_command ($cmd, $sql); | |
1512 | } | |
1513 | ||
1514 | sub compute_required { | |
1515 | my ($self, $pkglist) = @_; | |
1516 | ||
1517 | my $pkginfo = $self->pkginfo(); | |
1518 | my $instpkgs = $self->read_installed (); | |
1519 | ||
1520 | my $closure = {}; | |
1521 | __record_provides ($pkginfo, $closure, [keys %$instpkgs]); | |
1522 | ||
1523 | return $self->closure ($closure, $pkglist); | |
1524 | } | |
1525 | ||
1526 | sub task_postgres { | |
1527 | my ($self, $opts) = @_; | |
1528 | ||
1529 | my @supp = ('7.4', '8.1'); | |
1530 | my $pgversion = '8.1'; | |
1531 | ||
1532 | my $suite = $self->{config}->{suite}; | |
1533 | ||
1534 | if ($suite eq 'lenny' || $suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') { | |
1535 | @supp = ('8.3'); | |
1536 | $pgversion = '8.3'; | |
1537 | } elsif ($suite eq 'squeeze') { | |
1538 | @supp = ('8.4'); | |
1539 | $pgversion = '8.4'; | |
1540 | } | |
1541 | ||
1542 | $pgversion = $opts->{version} if $opts->{version}; | |
1543 | ||
1544 | die "unsupported postgres version '$pgversion'\n" | |
1545 | if !grep { $pgversion eq $_; } @supp; | |
1546 | ||
1547 | my $rootdir = $self->vz_root_dir(); | |
1548 | ||
1549 | my $required = $self->compute_required (["postgresql-$pgversion"]); | |
1550 | ||
1551 | $self->cache_packages ($required); | |
1552 | ||
1553 | $self->ve_dpkg ('install', @$required); | |
1554 | ||
1555 | my $iscript = $suite eq 'squeeze' ? 'postgresql' : "postgresql-$pgversion"; | |
1556 | ||
1557 | $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start}; | |
1558 | } | |
1559 | ||
1560 | sub task_mysql { | |
1561 | my ($self, $opts) = @_; | |
1562 | ||
1563 | my $password = $opts->{password}; | |
1564 | my $rootdir = $self->vz_root_dir(); | |
1565 | ||
1566 | my $suite = $self->{config}->{suite}; | |
1567 | ||
1568 | my $ver = $suite eq 'squeeze' ? '5.1' : '5.0'; | |
1569 | ||
1570 | my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]); | |
1571 | ||
1572 | $self->cache_packages ($required); | |
1573 | ||
1574 | $self->ve_dpkg ('install', @$required); | |
1575 | ||
1576 | # fix security (see /usr/bin/mysql_secure_installation) | |
1577 | my $sql = "DELETE FROM mysql.user WHERE User='';\n" . | |
1578 | "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';\n" . | |
1579 | "FLUSH PRIVILEGES;\n"; | |
1580 | $self->ve_mysql_bootstrap ($sql); | |
1581 | ||
1582 | if ($password) { | |
1583 | ||
1584 | my $rpw = $password eq 'random' ? 'admin' : $password; | |
1585 | ||
1586 | my $sql = "USE mysql;\n" . | |
1587 | "UPDATE user SET password=PASSWORD(\"$rpw\") WHERE user='root';\n" . | |
1588 | "FLUSH PRIVILEGES;\n"; | |
1589 | $self->ve_mysql_bootstrap ($sql); | |
1590 | ||
1591 | write_file ("[client]\nuser=root\npassword=\"$rpw\"\n", "$rootdir/root/.my.cnf", 0600); | |
1592 | if ($password eq 'random') { | |
1593 | $self->install_init_script ($script_mysql_randompw, 2, 20); | |
1594 | } | |
1595 | } | |
1596 | ||
1597 | $self->ve_command ("/etc/init.d/mysql start") if $opts->{start}; | |
1598 | } | |
1599 | ||
1600 | sub task_php { | |
1601 | my ($self, $opts) = @_; | |
1602 | ||
1603 | my $memlimit = $opts->{memlimit}; | |
1604 | my $rootdir = $self->vz_root_dir(); | |
1605 | ||
1606 | my $required = $self->compute_required ([qw (php5 php5-cli libapache2-mod-php5 php5-gd)]); | |
1607 | ||
1608 | $self->cache_packages ($required); | |
1609 | ||
1610 | $self->ve_dpkg ('install', @$required); | |
1611 | ||
1612 | if ($memlimit) { | |
1613 | $self->run_command ("sed -e 's/^\\s*memory_limit\\s*=.*;/memory_limit = ${memlimit}M;/' -i $rootdir/etc/php5/apache2/php.ini"); | |
1614 | } | |
1615 | } | |
1616 | ||
1617 | sub install { | |
1618 | my ($self, $pkglist, $unpack) = @_; | |
1619 | ||
1620 | my $required = $self->compute_required ($pkglist); | |
1621 | ||
1622 | $self->cache_packages ($required); | |
1623 | ||
1624 | $self->ve_dpkg ($unpack ? 'unpack' : 'install', @$required); | |
1625 | } | |
1626 | ||
1627 | sub cleanup { | |
1628 | my ($self, $distclean) = @_; | |
1629 | ||
1630 | unlink $self->{logfile}; | |
1631 | unlink "$self->{targetname}.tar"; | |
1632 | unlink "$self->{targetname}.tar.gz"; | |
1633 | ||
1634 | $self->ve_destroy (); | |
1635 | unlink ".veid"; | |
1636 | ||
1637 | rmtree $self->{cachedir} if $distclean && !$self->{config}->{cachedir}; | |
1638 | ||
1639 | rmtree $self->{infodir}; | |
1640 | ||
1641 | } | |
1642 | ||
1643 | 1; |