]> git.proxmox.com Git - pve-installer.git/blob - proxinstall
update layout, use new logo
[pve-installer.git] / proxinstall
1 #!/usr/bin/perl -w
2
3 $ENV{DEBIAN_FRONTEND} = 'noninteractive';
4 $ENV{LC_ALL} = 'C';
5
6 use strict;
7 use Getopt::Long;
8 use IPC::Open2;
9 use IPC::Open3;
10 use IO::File;
11 use IO::Select;
12 use Cwd 'abs_path';
13 use Gtk3 '-init';
14 use Gtk3::WebKit;
15 use Encode;
16 use String::ShellQuote;
17 use Data::Dumper;
18 use File::Basename;
19
20 my $release = '4.2';
21
22 my $kapi = `uname -r`;
23 chomp $kapi;
24
25 my $opt_testmode;
26
27 if (!$ENV{G_SLICE} || $ENV{G_SLICE} ne "always-malloc") {
28 die "do not use slice allocator (run with 'G_SLICE=always-malloc ./proxinstall ...')\n";
29 }
30
31 if (!GetOptions ('testmode=s' => \$opt_testmode)) {
32 die "usage error\n";
33 exit (-1);
34 }
35
36 my $zfspoolname = $opt_testmode ? "test_rpool" : 'rpool';
37
38 my $storage_cfg_zfs = <<__EOD__;
39 dir: local
40 path /var/lib/vz
41 content iso,vztmpl,backup
42
43 zfspool: local-zfs
44 pool $zfspoolname/data
45 sparse
46 content images,rootdir
47 __EOD__
48
49 my $storage_cfg_lvmthin = <<__EOD__;
50 dir: local
51 path /var/lib/vz
52 content iso,vztmpl,backup
53
54 lvmthin: local-lvm
55 thinpool data
56 vgname pve
57 content rootdir,images
58 __EOD__
59
60
61 sub file_read_firstline {
62 my ($filename) = @_;
63
64 my $fh = IO::File->new ($filename, "r");
65 return undef if !$fh;
66 my $res = <$fh>;
67 chomp $res if $res;
68 $fh->close;
69 return $res;
70 }
71
72 my $logfd = IO::File->new (">/tmp/install.log");
73
74 my $proxmox_dir = $opt_testmode ? Cwd::cwd() : "/var/lib/pve-installer";
75
76 my $grub_plattform = "pc"; # pc, efi-amd64 or efi-ia32
77
78 $grub_plattform = "efi-amd64" if -d "/sys/firmware/efi";
79
80 my $IPV4OCTET = "(?:25[0-5]|(?:2[0-4]|1[0-9]|[1-9])?[0-9])";
81 my $IPV4RE = "(?:(?:$IPV4OCTET\\.){3}$IPV4OCTET)";
82 my $IPV6H16 = "(?:[0-9a-fA-F]{1,4})";
83 my $IPV6LS32 = "(?:(?:$IPV4RE|$IPV6H16:$IPV6H16))";
84
85 my $IPV6RE = "(?:" .
86 "(?:(?:" . "(?:$IPV6H16:){6})$IPV6LS32)|" .
87 "(?:(?:" . "::(?:$IPV6H16:){5})$IPV6LS32)|" .
88 "(?:(?:(?:" . "$IPV6H16)?::(?:$IPV6H16:){4})$IPV6LS32)|" .
89 "(?:(?:(?:(?:$IPV6H16:){0,1}$IPV6H16)?::(?:$IPV6H16:){3})$IPV6LS32)|" .
90 "(?:(?:(?:(?:$IPV6H16:){0,2}$IPV6H16)?::(?:$IPV6H16:){2})$IPV6LS32)|" .
91 "(?:(?:(?:(?:$IPV6H16:){0,3}$IPV6H16)?::(?:$IPV6H16:){1})$IPV6LS32)|" .
92 "(?:(?:(?:(?:$IPV6H16:){0,4}$IPV6H16)?::" . ")$IPV6LS32)|" .
93 "(?:(?:(?:(?:$IPV6H16:){0,5}$IPV6H16)?::" . ")$IPV6H16)|" .
94 "(?:(?:(?:(?:$IPV6H16:){0,6}$IPV6H16)?::" . ")))";
95
96 my $IPRE = "(?:$IPV4RE|$IPV6RE)";
97
98
99 my $ipv4_mask_hash = {
100 '128.0.0.0' => 1,
101 '192.0.0.0' => 2,
102 '224.0.0.0' => 3,
103 '240.0.0.0' => 4,
104 '248.0.0.0' => 5,
105 '252.0.0.0' => 6,
106 '254.0.0.0' => 7,
107 '255.0.0.0' => 8,
108 '255.128.0.0' => 9,
109 '255.192.0.0' => 10,
110 '255.224.0.0' => 11,
111 '255.240.0.0' => 12,
112 '255.248.0.0' => 13,
113 '255.252.0.0' => 14,
114 '255.254.0.0' => 15,
115 '255.255.0.0' => 16,
116 '255.255.128.0' => 17,
117 '255.255.192.0' => 18,
118 '255.255.224.0' => 19,
119 '255.255.240.0' => 20,
120 '255.255.248.0' => 21,
121 '255.255.252.0' => 22,
122 '255.255.254.0' => 23,
123 '255.255.255.0' => 24,
124 '255.255.255.128' => 25,
125 '255.255.255.192' => 26,
126 '255.255.255.224' => 27,
127 '255.255.255.240' => 28,
128 '255.255.255.248' => 29,
129 '255.255.255.252' => 30
130 };
131
132 my ($window, $cmdbox, $inbox, $htmlview);
133 my ($next, $next_fctn, $target_hd);
134 my ($progress, $progress_status);
135 my ($ipversion, $ipaddress, $ipconf_entry_addr);
136 my ($netmask, $ipconf_entry_mask);
137 my ($gateway, $ipconf_entry_gw);
138 my ($dnsserver, $ipconf_entry_dns);
139 my $hostname = 'proxmox';
140 my $domain = 'domain.tld';
141 my $cmdline = file_read_firstline("/proc/cmdline");
142 my $ipconf;
143 my $country;
144 my $timezone = 'Europe/Vienna';
145 my $password;
146 my $mailto;
147 my $keymap = 'en-us';
148 my $cmap;
149
150 # parse command line args
151
152 my $config_options = {};
153
154 if ($cmdline =~ m/\s(ext3|ext4|xfs|zfs)(\s.*)?$/) {
155 $config_options->{filesys} = $1;
156 } else {
157 $config_options->{filesys} = 'ext4';
158 }
159
160 if ($cmdline =~ m/hdsize=(\d+(\.\d+)?)[\s\n]/i) {
161 $config_options->{hdsize} = $1;
162 }
163
164 if ($cmdline =~ m/swapsize=(\d+(\.\d+)?)[\s\n]/i) {
165 $config_options->{swapsize} = $1;
166 }
167
168 if ($cmdline =~ m/maxroot=(\d+(\.\d+)?)[\s\n]/i) {
169 $config_options->{maxroot} = $1;
170 }
171
172 if ($cmdline =~ m/minfree=(\d+(\.\d+)?)[\s\n]/i) {
173 $config_options->{minfree} = $1;
174 }
175
176 if ($cmdline =~ m/maxvz=(\d+(\.\d+)?)[\s\n]/i) {
177 $config_options->{maxvz} = $1;
178 }
179
180 my $postfix_main_cf = <<_EOD;
181 # See /usr/share/postfix/main.cf.dist for a commented, more complete version
182
183 myhostname=__FQDN__
184
185 smtpd_banner = \$myhostname ESMTP \$mail_name (Debian/GNU)
186 biff = no
187
188 # appending .domain is the MUA's job.
189 append_dot_mydomain = no
190
191 # Uncomment the next line to generate "delayed mail" warnings
192 #delay_warning_time = 4h
193
194 alias_maps = hash:/etc/aliases
195 alias_database = hash:/etc/aliases
196 mydestination = \$myhostname, localhost.\$mydomain, localhost
197 relayhost =
198 mynetworks = 127.0.0.0/8
199 inet_interfaces = loopback-only
200 recipient_delimiter = +
201
202 _EOD
203
204 sub shellquote {
205 my $str = shift;
206
207 return String::ShellQuote::shell_quote($str);
208 }
209
210 sub cmd2string {
211 my ($cmd) = @_;
212
213 die "no arguments" if !$cmd;
214
215 return $cmd if !ref($cmd);
216
217 my @qa = ();
218 foreach my $arg (@$cmd) { push @qa, shellquote($arg); }
219
220 return join (' ', @qa);
221 }
222
223 sub syscmd {
224 my ($cmd) = @_;
225
226 return run_command ($cmd, undef, undef, 1);
227 }
228
229 sub run_command {
230 my ($cmd, $func, $input, $noout) = @_;
231
232 my $cmdstr;
233 if (!ref($cmd)) {
234 $cmdstr = $cmd;
235 if ($cmd =~ m/|/) {
236 # see 'man bash' for option pipefail
237 $cmd = [ '/bin/bash', '-c', "set -o pipefail && $cmd" ];
238 } else {
239 $cmd = [ $cmd ];
240 }
241 } else {
242 $cmdstr = cmd2string($cmd);
243 }
244
245 my $cmdtxt;
246 if ($input && ($cmdstr !~ m/chpasswd/)) {
247 $cmdtxt = "# $cmdstr <<EOD\n$input";
248 chomp $cmdtxt;
249 $cmdtxt .= "\nEOD\n";
250 } else {
251 $cmdtxt = "# $cmdstr\n";
252 }
253
254 if ($opt_testmode) {
255 print $cmdtxt;
256 STDOUT->flush();
257 }
258
259 print $logfd $cmdtxt;
260
261 my $reader = IO::File->new();
262 my $writer = IO::File->new();
263 my $error = IO::File->new();
264
265 my $orig_pid = $$;
266
267 my $pid;
268 eval {
269 $pid = open3 ($writer, $reader, $error, @$cmd) || die $!;
270 };
271
272 my $err = $@;
273
274 # catch exec errors
275 if ($orig_pid != $$) {
276 POSIX::_exit (1);
277 kill ('KILL', $$);
278 }
279
280 die $err if $err;
281
282 print $writer $input if defined $input;
283 close $writer;
284
285 my $select = new IO::Select;
286 $select->add ($reader);
287 $select->add ($error);
288
289 my ($ostream, $logout) = ('', '', '');
290
291 while ($select->count) {
292 my @handles = $select->can_read (0.2);
293
294 Gtk3::main_iteration() while Gtk3::events_pending();
295
296 next if !scalar (@handles); # timeout
297
298 foreach my $h (@handles) {
299 my $buf = '';
300 my $count = sysread ($h, $buf, 4096);
301 if (!defined ($count)) {
302 my $err = $!;
303 kill (9, $pid);
304 waitpid ($pid, 0);
305 die "command '$cmd' failed: $err";
306 }
307 $select->remove ($h) if !$count;
308 if ($h eq $reader) {
309 $ostream .= $buf if !($noout || $func);
310 $logout .= $buf;
311 while ($logout =~ s/^([^\010\r\n]*)(\r|\n|(\010)+|\r\n)//s) {
312 my $line = $1;
313 &$func($line) if $func;
314 }
315
316 } elsif ($h eq $error) {
317 $ostream .= $buf if !($noout || $func);
318 }
319 print $buf;
320 STDOUT->flush();
321 print $logfd $buf;
322 }
323 }
324
325 &$func($logout) if $func;
326
327 my $rv = waitpid ($pid, 0);
328
329 return $? if $noout; # behave like standard system();
330
331 my $ec = ($? >> 8);
332
333 if ($ec) {
334 die "command '$cmdstr' failed with exit code $ec";
335 }
336
337 return $ostream;
338 }
339
340 sub detect_country {
341
342 print "trying to detect country...\n";
343 my $cpid = open2(\*TMP, undef, "traceroute -N 1 -q 1 -n 8.8.8.8");
344 return undef if !$cpid;
345
346 my $country;
347
348 my $previous_alarm = alarm (10);
349 eval {
350 local $SIG{ALRM} = sub { die "timed out!\n" };
351 my $line;
352 while (defined ($line = <TMP>)) {
353 print $logfd "DC TRACEROUTE: $line";
354 if ($line =~ m/\s*\d+\s+(\d+\.\d+\.\d+\.\d+)\s/) {
355 my $geoip = `geoiplookup $1`;
356 print $logfd "DC GEOIP: $geoip";
357 if ($geoip =~ m/GeoIP Country Edition:\s*([A-Z]+),/) {
358 $country = lc ($1);
359 print $logfd "DC FOUND: $country\n";
360 last;
361 }
362 }
363 }
364 };
365
366 my $err = $@;
367
368 alarm ($previous_alarm);
369
370 close (TMP);
371
372 if ($err) {
373 print "unable to detect country - $err\n";
374 } elsif ($country) {
375 print "detected country: " . uc($country) . "\n";
376 } else {
377 print "unable to detect country\n";
378 }
379
380 return $country;
381 }
382
383 sub get_memtotal {
384
385 open (MEMINFO, "/proc/meminfo");
386
387 my $res = 512; # default to 512 if something goes wrong
388 while (my $line = <MEMINFO>) {
389 if ($line =~ m/^MemTotal:\s+(\d+)\s*kB/i) {
390 $res = int ($1 / 1024);
391 }
392 }
393
394 close (MEMINFO);
395
396 return $res;
397 }
398
399 my $total_memory = get_memtotal();
400
401 sub link_points_to {
402 my ($src, $dest) = @_;
403
404 my ($dev1,$ino1) = stat ($src);
405 my ($dev2,$ino2) = stat ($dest);
406
407 return 0 if !($dev1 && $dev2 && $ino1 && $ino2);
408
409 return $ino1 == $ino2 && $dev1 == $dev2;
410 }
411
412 sub find_stable_path {
413 my ($stabledir, $bdev) = @_;
414
415 foreach my $path (<$stabledir/*>) {
416 if (link_points_to ($path, $bdev)) {
417 return wantarray ? ($path, basename($path)) : $path;
418 }
419 }
420 }
421
422 sub find_dev_by_uuid {
423 my $bdev = shift;
424
425 my ($full_path, $name) = find_stable_path ("/dev/disk/by-uuid", $bdev);
426
427 return $name;
428 }
429
430 sub hd_list {
431
432 my $res = ();
433
434 if ($opt_testmode) {
435 push @$res, [-1, $opt_testmode, int((-s $opt_testmode)/512), "TESTDISK"];
436 }
437
438 my $count = 0;
439
440 foreach my $bd (</sys/block/*>) {
441 next if $bd =~ m|^/sys/block/ram\d+$|;
442 next if $bd =~ m|^/sys/block/loop\d+$|;
443 next if $bd =~ m|^/sys/block/md\d+$|;
444 next if $bd =~ m|^/sys/block/dm-.*$|;
445 next if $bd =~ m|^/sys/block/fd\d+$|;
446 next if $bd =~ m|^/sys/block/sr\d+$|;
447
448 my $dev = file_read_firstline("$bd/dev");
449 chomp $dev;
450
451 next if !$dev;
452
453 my $info = `udevadm info --path $bd --query all`;
454 next if !$info;
455
456 next if $info !~ m/^E: DEVTYPE=disk$/m;
457
458 next if $info =~ m/^E: ID_CDROM/m;
459
460 my ($name) = $info =~ m/^N: (\S+)$/m;
461
462 if ($name) {
463 my $real_name = "/dev/$name";
464
465 my $size = file_read_firstline("$bd/size");
466 chomp $size;
467 $size = undef if !($size && $size =~ m/^\d+$/);
468
469 my $model = file_read_firstline("$bd/device/model") || '';
470 $model =~ s/^\s+//;
471 $model =~ s/\s+$//;
472 if (length ($model) > 30) {
473 $model = substr ($model, 0, 30);
474 }
475 push @$res, [$count++, $real_name, $size, $model] if $size;
476 } else {
477 print STDERR "ERROR: unable to map device $dev ($bd)\n";
478 }
479 }
480
481 return $res;
482 }
483
484 sub read_cmap {
485 my $countryfn = $opt_testmode ? "/usr/share/pve-manager/country.dat" :
486 "/proxmox/country.dat";
487 open (TMP, "<$countryfn") || die "unable to open '$countryfn' - $!\n";
488 my $line;
489 my $country = {};
490 my $countryhash = {};
491 my $kmap = {};
492 my $kmaphash = {};
493 while (defined ($line = <TMP>)) {
494 if ($line =~ m|^map:([^\s:]+):([^:]+):([^:]+):([^:]+):([^:]+):([^:]*):$|) {
495 $kmap->{$1} = {
496 name => $2,
497 kvm => $3,
498 console => $4,
499 x11 => $5,
500 x11var => $6,
501 };
502 $kmaphash->{$2} = $1;
503 } elsif ($line =~ m|^([a-z]{2}):([^:]+):([^:]*):([^:]*):$|) {
504 $country->{$1} = {
505 name => $2,
506 kmap => $3,
507 mirror => $4,
508 };
509 $countryhash->{lc($2)} = $1;
510 } else {
511 warn "unable to parse 'country.dat' line: $line";
512 }
513 }
514 close (TMP);
515
516 my $zones = {};
517 my $cczones = {};
518 my $zonefn = "/usr/share/zoneinfo/zone.tab";
519 open (TMP, "<$zonefn") || die "unable to open '$zonefn' - $!\n";
520 while (defined ($line = <TMP>)) {
521 next if $line =~ m/^\#/;
522 next if $line =~ m/^\s*$/;
523 if ($line =~ m|^([A-Z][A-Z])\s+\S+\s+(([^/]+)/\S+)\s|) {
524 my $cc = lc($1);
525 $cczones->{$cc}->{$2} = 1;
526 $country->{$cc}->{zone} = $2 if !defined ($country->{$cc}->{zone});
527 $zones->{$2} = 1;
528
529 }
530 }
531 close (TMP);
532
533 return {
534 zones => $zones,
535 cczones => $cczones,
536 country => $country,
537 countryhash => $countryhash,
538 kmap => $kmap,
539 kmaphash => $kmaphash,
540 }
541 }
542
543 # search for Harddisks
544 my $hds = hd_list ();
545
546 sub hd_size {
547 my ($dev) = @_;
548
549 foreach my $hd (@$hds) {
550 my ($disk, $devname, $size, $model) = @$hd;
551 return int($size/2) if $devname eq $dev;
552 }
553
554 die "no such device '$dev'\n";
555 }
556
557 sub get_partition_dev {
558 my ($dev, $partnum) = @_;
559
560 if ($dev =~ m|^/dev/[hxsev]d[a-z]$|) {
561 return "${dev}$partnum";
562 } elsif ($dev =~ m|^/dev/[^/]+/c\d+d\d+$|) {
563 return "${dev}p$partnum";
564 } elsif ($dev =~ m|^/dev/[^/]+/d\d+$|) {
565 return "${dev}p$partnum";
566 } elsif ($dev =~ m|^/dev/[^/]+/hd[a-z]$|) {
567 return "${dev}$partnum";
568 } elsif ($dev =~ m|^/dev/nvme\d+n\d+$|) {
569 return "${dev}p$partnum";
570 } else {
571 die "unable to get device for partition $partnum on device $dev\n";
572 }
573
574 }
575
576 sub file_get_contents {
577 my ($filename, $max) = @_;
578
579 my $fh = IO::File->new($filename, "r") ||
580 die "can't open '$filename' - $!\n";
581
582 local $/; # slurp mode
583
584 my $content = <$fh>;
585
586 close $fh;
587
588 return $content;
589 }
590
591 sub write_config {
592 my ($text, $filename) = @_;
593
594 my $fd = IO::File->new (">$filename") ||
595 die "unable to open file '$filename' - $!\n";
596 print $fd $text;
597 $fd->close();
598 }
599
600 sub update_progress {
601 my ($frac, $start, $end, $text) = @_;
602
603 my $part = $end - $start;
604 my $res = $start + $frac*$part;
605
606 $progress->set_fraction ($res);
607 $progress->set_text (sprintf ("%d%%", int ($res*100)));
608 $progress_status->set_text ($text) if defined ($text);
609
610 Gtk3::main_iteration() while Gtk3::events_pending();
611 }
612
613 my $fssetup = {
614 ext3 => {
615 mkfs => 'mkfs.ext3 -F',
616 mkfs_root_opt => '',
617 mkfs_data_opt => '-m 0',
618 root_mountopt => 'errors=remount-ro',
619 },
620 ext4 => {
621 mkfs => 'mkfs.ext4 -F',
622 mkfs_root_opt => '',
623 mkfs_data_opt => '-m 0',
624 root_mountopt => 'errors=remount-ro',
625 },
626 xfs => {
627 mkfs => 'mkfs.xfs -f',
628 mkfs_root_opt => '',
629 mkfs_data_opt => '',
630 root_mountopt => '',
631 },
632 };
633
634 sub create_filesystem {
635 my ($dev, $name, $type, $start, $end, $fs, $fe) = @_;
636
637 my $range = $end - $start;
638 my $rs = $start + $range*$fs;
639 my $re = $start + $range*$fe;
640 my $max = 0;
641
642 my $fsdata = $fssetup->{$type} || die "internal error - unknown file system '$type'";
643 my $opts = $name eq 'root' ? $fsdata->{mkfs_root_opt} : $fsdata->{mkfs_data_opt};
644
645 update_progress (0, $rs, $re, "creating $name filesystem");
646
647 run_command ("$fsdata->{mkfs} $opts $dev", sub {
648 my $line = shift;
649
650 if ($line =~ m/Writing inode tables:\s+(\d+)\/(\d+)/) {
651 $max = $2;
652 } elsif ($max && $line =~ m/(\d+)\/$max/) {
653 update_progress (($1/$max)*0.9, $rs, $re);
654 } elsif ($line =~ m/Creating journal.*done/) {
655 update_progress (0.95, $rs, $re);
656 } elsif ($line =~ m/Writing superblocks and filesystem.*done/) {
657 update_progress (1, $rs, $re);
658 }
659 });
660 }
661
662 sub debconfig_set {
663 my ($targetdir, $dcdata) = @_;
664
665 my $cfgfile = "/tmp/debconf.txt";
666 write_config ($dcdata, "$targetdir/$cfgfile");
667 syscmd ("chroot $targetdir debconf-set-selections $cfgfile");
668 unlink "$targetdir/$cfgfile";
669 }
670
671 sub diversion_add {
672 my ($targetdir, $cmd, $new_cmd) = @_;
673
674 syscmd ("chroot $targetdir dpkg-divert --package proxmox " .
675 "--add --rename $cmd") == 0 ||
676 die "unable to exec dpkg-divert\n";
677
678 syscmd ("ln -sf ${new_cmd} $targetdir/$cmd") == 0 ||
679 die "unable to link diversion to ${new_cmd}\n";
680 }
681
682 sub diversion_remove {
683 my ($targetdir, $cmd) = @_;
684
685 syscmd ("mv $targetdir/${cmd}.distrib $targetdir/${cmd};") == 0 ||
686 die "unable to remove $cmd diversion\n";
687
688 syscmd ("chroot $targetdir dpkg-divert --remove $cmd") == 0 ||
689 die "unable to remove $cmd diversion\n";
690 }
691
692 sub zfs_create_rpool {
693 my ($vdev) = @_;
694
695 syscmd ("zpool create -f -o ashift=12 -o cachefile=none $zfspoolname $vdev") == 0 ||
696 die "unable to create zfs root pool\n";
697
698 syscmd ("zfs create $zfspoolname/ROOT") == 0 ||
699 die "unable to create zfs $zfspoolname/ROOT volume\n";
700
701 syscmd ("zfs create $zfspoolname/data") == 0 ||
702 die "unable to create zfs $zfspoolname/data volume\n";
703
704 syscmd ("zfs create $zfspoolname/ROOT/pve-1") == 0 ||
705 die "unable to create zfs $zfspoolname/ROOT/pve-1 volume\n";
706
707 # disable atime during insatll
708 syscmd ("zfs set atime=off $zfspoolname") == 0 ||
709 die "unable to set zfs properties\n";
710 syscmd ("zfs set compression=lz4 $zfspoolname") == 0 ||
711 die "unable to set zfs properties\n";
712 }
713
714 sub zfs_create_swap {
715 my ($swapsize) = @_;
716
717 syscmd ("zfs create -V ${swapsize}K -b 4K $zfspoolname/swap") == 0 ||
718 die "unable to create zfs swap device\n";
719
720 syscmd ("zfs set com.sun:auto-snapshot=false $zfspoolname/swap") == 0 ||
721 die "unable to set zfs properties\n";
722
723 # reduces memory pressure
724 syscmd ("zfs set sync=always $zfspoolname/swap") == 0 ||
725 die "unable to set zfs properties\n";
726
727 return "/dev/zvol/$zfspoolname/swap";
728 }
729
730 sub partition_bootable_disk {
731 my ($target_dev, $maxhdsize, $use_zfs) = @_;
732
733 die "too dangerous" if $opt_testmode;
734
735 syscmd("sgdisk -Z ${target_dev}");
736 my $hdsize = hd_size($target_dev); # size in blocks (1024 bytes)
737
738 my $restricted_hdsize_mb = 0; # 0 ==> end of partition
739 if ($maxhdsize && ($maxhdsize < $hdsize)) {
740 $hdsize = $maxhdsize;
741 $restricted_hdsize_mb = int($hdsize/1024) . 'M';
742 }
743
744 my $hdgb = int($hdsize/(1024*1024));
745 die "hardisk '$target_dev' too small (${hdsize}GB)\n" if $hdgb < 8;
746
747 # 1 - GRUB boot partition: 1M
748 # 2 - EFI ESP: 128M
749 # 3 - OS/Data partition
750
751 my $grubbootdev = get_partition_dev($target_dev, 1);
752 my $efibootdev = get_partition_dev($target_dev, 2);
753 my $osdev = get_partition_dev ($target_dev, 3);
754
755 my $pcmd = ['sgdisk', '-a1'];
756
757 my $pnum = 1;
758 push @$pcmd, "-n$pnum:34:2047", "-t$pnum:EF02";
759
760 $pnum = 2;
761 # Note: from sector 2048 to 128M
762 my $endsec = 128*1024*2 - 1;
763 push @$pcmd, "-n$pnum:2048:$endsec", "-t$pnum:EF00";
764
765 $pnum = 3;
766 # Note: from 128M to end
767 push @$pcmd, "-n$pnum:128M:${restricted_hdsize_mb}", "-t$pnum:8E00";
768
769 push @$pcmd, $target_dev;
770
771 my $os_size = $hdsize - 128*1024;
772
773 syscmd($pcmd) == 0 ||
774 die "unable to partition harddisk '${target_dev}'\n";
775
776 return ($os_size, $osdev, $efibootdev);
777 }
778
779 # ZFS has this use_whole_disk concept, so we try to partition the same
780 # way as zfs does by default. There is room at start of disk to insert
781 # a grub boot partition. But adding a EFI ESP is not possible.
782 #
783 # Note: zfs people think this is just a waste of space an not
784 # required. Instead, you should put the ESP on another disk (log,
785 # ..).
786
787 sub partition_bootable_zfs_disk {
788 my ($target_dev) = @_;
789
790 die "too dangerous" if $opt_testmode;
791
792 syscmd("sgdisk -Z ${target_dev}");
793 my $hdsize = hd_size($target_dev); # size in blocks (1024 bytes)
794
795 my $hdgb = int($hdsize/(1024*1024));
796 die "hardisk '$target_dev' too small (${hdsize}GB)\n" if $hdgb < 8;
797
798 # 1 - GRUB boot partition: 1M
799 # 2 - OS/Data partition
800 # 9 - ZFS reserved partition
801
802 my $grubbootdev = get_partition_dev($target_dev, 1);
803 my $osdev = get_partition_dev ($target_dev, 2);
804
805 my $pcmd = ['sgdisk', '-a1'];
806
807 my $pnum = 1;
808 push @$pcmd, "-n$pnum:34:2047", "-t$pnum:EF02";
809
810 $pnum = 9;
811 push @$pcmd, "-n$pnum:-8M:0", "-t$pnum:BF07";
812
813 $pnum = 2;
814 push @$pcmd, "-n$pnum:2048:0", "-t$pnum:BF01", '-c', "$pnum:zfs";
815
816 push @$pcmd, $target_dev;
817
818 my $os_size = $hdsize - 1024 - 1024*8;
819
820 syscmd($pcmd) == 0 ||
821 die "unable to partition harddisk '${target_dev}'\n";
822
823 return ($os_size, $osdev);
824 }
825
826 sub create_lvm_volumes {
827 my ($lvmdev, $os_size, $swap_size) = @_;
828
829 my $rootdev = '/dev/pve/root';
830 my $datadev = '/dev/pve/data';
831 my $swapfile = '/dev/pve/swap';
832
833 # we use --metadatasize 250k, which reseults in "pe_start = 512"
834 # so pe_start is aligned on a 128k boundary (advantage for SSDs)
835 syscmd ("/sbin/pvcreate --metadatasize 250k -y -ff $lvmdev") == 0 ||
836 die "unable to initialize physical volume $lvmdev\n";
837 syscmd ("/sbin/vgcreate pve $lvmdev") == 0 ||
838 die "unable to create volume group 'pve'\n";
839
840 my $hdgb = int($os_size/(1024*1024));
841 my $space = (($hdgb > 128) ? 16 : ($hdgb/8))*1024*1024;
842
843 my $maxroot;
844 if ($config_options->{maxroot}) {
845 $maxroot = $config_options->{maxroot};
846 } else {
847 $maxroot = 96;
848 }
849
850 my $rootsize = (($hdgb > ($maxroot*4)) ? $maxroot : $hdgb/4)*1024*1024;
851
852 my $rest = $os_size - $swap_size - $rootsize; # in KB
853
854 my $minfree;
855 if ($config_options->{minfree}) {
856 $minfree = (($config_options->{minfree}*1024*1024) >= $rest ) ? $space :
857 $config_options->{minfree}*1024*1024 ;
858 } else {
859 $minfree = $space;
860 }
861
862 $rest = $rest - $minfree;
863
864 if ($config_options->{maxvz}) {
865 $rest = (($config_options->{maxvz}*1024*1024) <= $rest) ?
866 $config_options->{maxvz}*1024*1024 : $rest;
867 }
868
869 syscmd ("/sbin/lvcreate -L${swap_size}K -nswap pve") == 0 ||
870 die "unable to create swap volume\n";
871
872 syscmd ("/sbin/lvcreate -L${rootsize}K -nroot pve") == 0 ||
873 die "unable to create root volume\n";
874
875 syscmd ("/sbin/lvcreate -L${rest}K -ndata pve") == 0 ||
876 die "unable to create data volume\n";
877
878 syscmd ("/sbin/lvconvert --yes --type thin-pool pve/data") == 0 ||
879 die "unable to create data thin-pool\n";
880
881 syscmd ("/sbin/vgchange -a y pve") == 0 ||
882 die "unable to activate volume group\n";
883
884 return ($rootdev, $datadev, $swapfile);
885 }
886
887 sub compute_swapsize {
888 my ($hdsize) = @_;
889
890 my $hdgb = int($hdsize/(1024*1024));
891
892 my $swapsize;
893 if ($config_options->{swapsize}) {
894 $swapsize = $config_options->{swapsize}*1024*1024;
895 } else {
896 my $ss = int ($total_memory / 1024);
897 $ss = 4 if $ss < 4;
898 $ss = ($hdgb/8) if $ss > ($hdgb/8);
899 $ss = 8 if $ss > 8;
900 $swapsize = $ss*1024*1024;
901 }
902 }
903
904 sub extract_data {
905 my ($tgzfile, $targetdir) = @_;
906
907 die "target '$targetdir' does not exist\n" if ! -d $targetdir;
908
909 my $bootdevinfo = [];
910
911 my $datadev;
912 my $swapfile;
913 my $rootdev;
914
915 my $use_zfs;
916
917 my $filesys = $config_options->{filesys};
918
919 if ($filesys =~ m/zfs/) {
920 $target_hd = undef; # do not use this config
921 $use_zfs = 1;
922 $targetdir = "/$zfspoolname/ROOT/pve-1";
923 } else {
924 $use_zfs = 0;
925 }
926
927 if ($use_zfs) {
928 my $i;
929 for ($i = 5; $i > 0; $i--) {
930 syscmd("modprobe zfs");
931 last if -c "/dev/zfs";
932 sleep(1);
933 }
934
935 die "unable to load zfs kernel module\n" if !$i;
936 }
937
938 eval {
939
940
941 my $maxper = 0.25;
942
943 update_progress (0, 0, $maxper, "create partitions");
944
945 if ($opt_testmode) {
946
947 $rootdev = abs_path($target_hd);
948 syscmd ("umount $rootdev");
949
950 if ($use_zfs) {
951
952 die "unsupported zfs mode (for testing environment)\n"
953 if $filesys ne 'zfs (RAID0)';
954
955 syscmd ("zpool destroy $zfspoolname") if $opt_testmode;
956
957 zfs_create_rpool($rootdev);
958 }
959
960 } elsif ($use_zfs) {
961
962 my ($devlist, $bootdevlist, $vdev) = get_zfs_raid_setup();
963
964 my $disksize;
965 foreach my $hd (@$bootdevlist) {
966 my $devname = @$hd[1];
967 my ($size, $osdev) =
968 partition_bootable_zfs_disk($devname);
969 die "unable to mirror disks with different sizes!\n"
970 if $disksize && ($size != $disksize);
971 push @$bootdevinfo, { devname => $devname, osdev => $osdev};
972 $disksize = $size;
973 }
974
975 sleep(1); # give kernel time to reread part table
976
977 # trigger udev to create /dev/disk/by-uuid
978 syscmd ("udevadm trigger --subsystem-match block");
979 syscmd ("udevadm settle --timeout 10");
980
981 foreach my $di (@$bootdevinfo) {
982 my $devname = $di->{devname};
983 $di->{by_id} = find_stable_path ("/dev/disk/by-id", $devname);
984
985 # Note: using /dev/disk/by-id/ does not work for unknown reason, we get
986 # cannot create 'rpool': no such pool or dataset
987 #my $osdev = find_stable_path ("/dev/disk/by-id", $di->{osdev}) || $di->{osdev};
988
989 my $osdev = $di->{osdev};
990 $vdev =~ s/ $devname/ $osdev/;
991 }
992
993 zfs_create_rpool($vdev);
994
995 my $swap_size = compute_swapsize($disksize);
996 $swapfile = zfs_create_swap($swap_size);
997
998 } else {
999
1000 die "target '$target_hd' is not a valid block device\n" if ! -b $target_hd;
1001
1002 my $maxhdsize;
1003 if ($config_options->{hdsize}) {
1004 # max hdsize passed on cmdline (GB)
1005 $maxhdsize = $config_options->{hdsize}*1024*1024;
1006 }
1007
1008 my ($os_size, $osdev, $efidev);
1009 ($os_size, $osdev, $efidev) =
1010 partition_bootable_disk($target_hd, $maxhdsize);
1011
1012 sleep(1); # give kernel time to reread part table
1013
1014 # trigger udev to create /dev/disk/by-uuid
1015 syscmd ("udevadm trigger --subsystem-match block");
1016 syscmd ("udevadm settle --timeout 10");
1017
1018 my $by_id = find_stable_path ("/dev/disk/by-id", $target_hd);
1019 push @$bootdevinfo, { esp => $efidev, devname => $target_hd,
1020 osdev => $osdev, by_id => $by_id };
1021
1022 my $swap_size = compute_swapsize($os_size);
1023 ($rootdev, $datadev, $swapfile) =
1024 create_lvm_volumes($osdev, $os_size, $swap_size);
1025
1026 # trigger udev to create /dev/disk/by-uuid
1027 syscmd ("udevadm trigger --subsystem-match block");
1028 syscmd ("udevadm settle --timeout 10");
1029 }
1030
1031 if ($use_zfs) {
1032 # to be fast during installation
1033 syscmd ("zfs set sync=disabled $zfspoolname") == 0 ||
1034 die "unable to set zfs properties\n";
1035 }
1036
1037 update_progress (0.03, 0, $maxper, "create swap space");
1038 if ($swapfile) {
1039 syscmd ("mkswap -f $swapfile") == 0 ||
1040 die "unable to create swap space\n";
1041 }
1042
1043 update_progress (0.05, 0, $maxper, "creating filesystems");
1044
1045 foreach my $di (@$bootdevinfo) {
1046 next if !$di->{esp};
1047 syscmd ("mkfs.vfat -F32 $di->{esp}") == 0 ||
1048 die "unable to initialize EFI ESP on device $di->{esp}\n";
1049 }
1050
1051 if (!$use_zfs) {
1052
1053 if ( -b $target_hd) {
1054
1055 create_filesystem ($rootdev, 'root', $filesys, 0.05, $maxper, 0, 1);
1056
1057 } else {
1058
1059 create_filesystem ($rootdev, 'root', $filesys, 0.05, $maxper, 0, 1);
1060 }
1061 }
1062
1063 update_progress (1, 0.05, $maxper, "mounting target $rootdev");
1064
1065 if (!$use_zfs) {
1066 syscmd ("mount -n $rootdev -o noatime,barrier=0 $targetdir") == 0 ||
1067 die "unable to mount $rootdev\n";
1068 }
1069
1070 mkdir "$targetdir/boot";
1071 mkdir "$targetdir/boot/efi";
1072
1073 mkdir "$targetdir/var";
1074 mkdir "$targetdir/var/lib";
1075 mkdir "$targetdir/var/lib/vz";
1076
1077 display_html ("extract2-rulesystem.htm");
1078 update_progress (1, 0.05, $maxper, "extracting base system");
1079
1080 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size) = stat ($tgzfile);
1081 $ino || die "unable to open file '$tgzfile' - $!\n";
1082
1083 my $files;
1084 if ($opt_testmode) {
1085 $files = file_read_firstline("/pve/$release/install/pve-base.cnt");
1086 } else {
1087 $files = file_read_firstline("/proxmox/pve-base.cnt");
1088 }
1089
1090 my $per = 0;
1091 my $count = 0;
1092
1093 run_command ("tar xvf $tgzfile -C $targetdir", sub {
1094 my $line = shift;
1095 $count++;
1096 my $nper = int (($count *100)/$files);
1097 if ($nper != $per) {
1098 $per = $nper;
1099 my $frac = $per > 100 ? 100 : $per/100;
1100 update_progress ($frac, $maxper, 0.5);
1101 }
1102 });
1103
1104 syscmd ("mount -n -t tmpfs tmpfs $targetdir/tmp") == 0 ||
1105 die "unable to mount tmpfs on $targetdir/tmp\n";
1106 syscmd ("mount -n -t proc proc $targetdir/proc") == 0 ||
1107 die "unable to mount proc on $targetdir/proc\n";
1108 syscmd ("mount -n -t sysfs sysfs $targetdir/sys") == 0 ||
1109 die "unable to mount sysfs on $targetdir/sys\n";
1110
1111 display_html ("extract3-spam.htm");
1112 update_progress (1, $maxper, 0.5, "configuring base system");
1113
1114 # configure hosts
1115
1116 my $hosts =
1117 "127.0.0.1 localhost.localdomain localhost\n" .
1118 "$ipaddress $hostname.$domain $hostname pvelocalhost\n\n" .
1119 "# The following lines are desirable for IPv6 capable hosts\n\n" .
1120 "::1 ip6-localhost ip6-loopback\n" .
1121 "fe00::0 ip6-localnet\n" .
1122 "ff00::0 ip6-mcastprefix\n" .
1123 "ff02::1 ip6-allnodes\n" .
1124 "ff02::2 ip6-allrouters\n" .
1125 "ff02::3 ip6-allhosts\n";
1126
1127 write_config ($hosts, "$targetdir/etc/hosts");
1128
1129 write_config ("$hostname\n", "$targetdir/etc/hostname");
1130
1131 syscmd ("/bin/hostname $hostname") if !$opt_testmode;
1132
1133 # configure interfaces
1134
1135 my $ifaces = "auto lo\niface lo inet loopback\n\n";
1136
1137 my $ntype = $ipversion == 4 ? 'inet' : 'inet6';
1138
1139 $ifaces .=
1140 "auto vmbr0\niface vmbr0 $ntype static\n" .
1141 "\taddress $ipaddress\n" .
1142 "\tnetmask $netmask\n" .
1143 "\tgateway $gateway\n" .
1144 "\tbridge_ports eth0\n" .
1145 "\tbridge_stp off\n" .
1146 "\tbridge_fd 0\n";
1147
1148 write_config ($ifaces, "$targetdir/etc/network/interfaces");
1149
1150 # configure dns
1151
1152 my $resolfconf = "search $domain\nnameserver $dnsserver\n";
1153 write_config ($resolfconf, "$targetdir/etc/resolv.conf");
1154
1155 # configure fstab
1156
1157 my $fstab = "# <file system> <mount point> <type> <options> <dump> <pass>\n";
1158
1159 if (!$use_zfs) {
1160 my $root_mountopt = $fssetup->{$filesys}->{root_mountopt} || 'defaults';
1161 $fstab .= "$rootdev / $filesys ${root_mountopt} 0 1\n";
1162 }
1163
1164 # mount /boot/efi
1165 # Note: this is required by current grub, but really dangerous, because
1166 # vfat does not have journaling, so it triggers manual fsck after each crash
1167 # so we only mount /boot/efi if really required (efi systems).
1168 if ($grub_plattform =~ m/^efi-/) {
1169 if (scalar(@$bootdevinfo)) {
1170 my $di = @$bootdevinfo[0]; # simply use first disk
1171 if ($di->{esp}) {
1172 my $efi_boot_uuid = $di->{esp};
1173 if (my $uuid = find_dev_by_uuid ($di->{esp})) {
1174 $efi_boot_uuid = "UUID=$uuid";
1175 }
1176
1177 $fstab .= "${efi_boot_uuid} /boot/efi vfat defaults 0 1\n";
1178 }
1179 }
1180 }
1181
1182
1183 $fstab .= "$swapfile none swap sw 0 0\n" if $swapfile;
1184
1185 $fstab .= "proc /proc proc defaults 0 0\n";
1186
1187 write_config ($fstab, "$targetdir/etc/fstab");
1188 write_config ("", "$targetdir/etc/mtab");
1189
1190 syscmd ("cp ${proxmox_dir}/policy-disable-rc.d " .
1191 "$targetdir/usr/sbin/policy-rc.d") == 0 ||
1192 die "unable to copy policy-rc.d\n";
1193 syscmd ("cp ${proxmox_dir}/fake-start-stop-daemon " .
1194 "$targetdir/sbin/") == 0 ||
1195 die "unable to copy start-stop-daemon\n";
1196
1197 diversion_add ($targetdir, "/sbin/start-stop-daemon", "/sbin/fake-start-stop-daemon");
1198 diversion_add ($targetdir, "/usr/sbin/update-grub", "/bin/true");
1199 diversion_add ($targetdir, "/usr/sbin/update-initramfs", "/bin/true");
1200
1201 syscmd ("touch $targetdir/proxmox_install_mode");
1202
1203 my $grub_install_devices_txt = '';
1204 foreach my $di (@$bootdevinfo) {
1205 $grub_install_devices_txt .= ', ' if $grub_install_devices_txt;
1206 $grub_install_devices_txt .= $di->{by_id} || $di->{devname};
1207 }
1208
1209 # Note: debian-installer/locale is used by console-setup
1210
1211 debconfig_set ($targetdir, <<_EOD);
1212 locales locales/default_environment_locale select en_US.UTF-8
1213 locales locales/locales_to_be_generated select en_US.UTF-8 UTF-8
1214 samba-common samba-common/dhcp boolean false
1215 samba-common samba-common/workgroup string WORKGROUP
1216 postfix postfix/main_mailer_type select Local only
1217 keyboard-configuration keyboard-configuration/xkb-keymap select $keymap
1218 d-i debian-installer/locale select en_US.UTF-8
1219 grub-pc grub-pc/install_devices select $grub_install_devices_txt
1220 _EOD
1221
1222 my $pkgdir = $opt_testmode ? "packages" : "/proxmox/packages";
1223 my $pkg_count = 0;
1224 while (<$pkgdir/*.deb>) { $pkg_count++ };
1225
1226 $count = 0;
1227 while (<$pkgdir/*.deb>) {
1228 chomp;
1229 my $path = $_;
1230 my ($deb) = $path =~ m/$pkgdir\/(.*\.deb)/;
1231 # if ($deb =~ m/^grub-efi-/ && $deb !~ m/^grub-${grub_plattform}/) {
1232 # $count++;
1233 # next;
1234 # }
1235 update_progress ($count/$pkg_count, 0.5, 0.75, "extracting $deb");
1236 print "extracting: $deb\n";
1237 syscmd ("cp $path $targetdir/tmp/$deb") == 0 ||
1238 die "installation of package $deb failed\n";
1239 syscmd ("chroot $targetdir dpkg --force-depends --no-triggers --unpack /tmp/$deb") == 0 ||
1240 die "installation of package $deb failed\n";
1241 update_progress ((++$count)/$pkg_count, 0.5, 0.75);
1242 }
1243
1244 display_html ("extract4-virus.htm");
1245
1246 my $cmd = "chroot $targetdir dpkg --force-confold --configure -a";
1247 $count = 0;
1248 run_command ($cmd, sub {
1249 my $line = shift;
1250 if ($line =~ m/Setting up\s+(\S+)/) {
1251 update_progress ((++$count)/$pkg_count, 0.75, 0.95,
1252 "configuring $1");
1253 }
1254 });
1255
1256 debconfig_set ($targetdir, <<_EOD);
1257 postfix postfix/main_mailer_type select No configuration
1258 _EOD
1259
1260 unlink "$targetdir/etc/mailname";
1261 $postfix_main_cf =~ s/__FQDN__/${hostname}.${domain}/;
1262 write_config ($postfix_main_cf, "$targetdir/etc/postfix/main.cf");
1263
1264 # make sure we have all postfix directories
1265 syscmd ("chroot $targetdir /usr/sbin/postfix check");
1266 # cleanup mail queue
1267 syscmd ("chroot $targetdir /usr/sbin/postsuper -d ALL");
1268
1269 # enable NTP (timedatectl set-ntp true does not work without DBUS)
1270 syscmd ("chroot $targetdir /bin/systemctl enable systemd-timesyncd.service");
1271
1272 unlink "$targetdir/proxmox_install_mode";
1273
1274 # set timezone
1275 unlink ("$targetdir/etc/localtime");
1276 symlink ("/usr/share/zoneinfo/$timezone", "$targetdir/etc/localtime");
1277 write_config ("$timezone\n", "$targetdir/etc/timezone");
1278
1279 # set apt mirror
1280 if (my $mirror = $cmap->{country}->{$country}->{mirror}) {
1281 my $fn = "$targetdir/etc/apt/sources.list";
1282 syscmd ("sed -i 's/ftp\\.debian\\.org/$mirror/' '$fn'");
1283 }
1284
1285 # create extended_states for apt (avoid cron job warning if that
1286 # file does not exist)
1287 write_config ('', "$targetdir/var/lib/apt/extended_states");
1288
1289 # allow ssh root login
1290 syscmd ("sed -i 's/^PermitRootLogin.*/PermitRootLogin yes/' '$targetdir/etc/ssh/sshd_config'");
1291
1292 # save installer settings
1293 my $ucc = uc ($country);
1294 debconfig_set ($targetdir, <<_EOD);
1295 pve-manager pve-manager/country string $ucc
1296 _EOD
1297
1298 update_progress (0.8, 0.95, 1, "make system bootable");
1299
1300 if ($use_zfs) {
1301 syscmd ("sed -i -e 's/^GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX=\"root=ZFS=$zfspoolname\\/ROOT\\/pve-1 boot=zfs\"/' $targetdir/etc/default/grub") == 0 ||
1302 die "unable to update /etc/default/grub\n";
1303
1304 }
1305
1306 diversion_remove ($targetdir, "/usr/sbin/update-grub");
1307 diversion_remove ($targetdir, "/usr/sbin/update-initramfs");
1308
1309 if (!$opt_testmode) {
1310
1311 unlink ("$targetdir/etc/mtab");
1312 symlink ("/proc/mounts", "$targetdir/etc/mtab");
1313 syscmd ("mount -n --bind /dev $targetdir/dev");
1314
1315 syscmd ("chroot $targetdir /usr/sbin/update-initramfs -c -k $kapi") == 0 ||
1316 die "unable to install initramfs\n";
1317
1318 foreach my $di (@$bootdevinfo) {
1319 my $dev = $di->{devname};
1320 syscmd ("chroot $targetdir /usr/sbin/grub-install --target i386-pc --no-floppy --bootloader-id='proxmox' $dev") == 0 ||
1321 die "unable to install the i386-pc boot loader on '$dev'\n";
1322
1323 if ($di->{esp}) {
1324 syscmd ("mount -n $di->{esp} $targetdir/boot/efi") == 0 ||
1325 die "unable to mount $di->{esp}\n";
1326 syscmd ("chroot $targetdir /usr/sbin/grub-install --target x86_64-efi --no-floppy --bootloader-id='proxmox' $dev") == 0 ||
1327 die "unable to install the EFI boot loader on '$dev'\n";
1328 # also install fallback boot file (OVMF does not boot without)
1329 mkdir("$targetdir/boot/efi/EFI/BOOT");
1330 syscmd("cp $targetdir/boot/efi/EFI/proxmox/grubx64.efi $targetdir/boot/efi/EFI/BOOT/BOOTx64.EFI") == 0 ||
1331 die "unable to copy efi boot loader\n";
1332
1333 syscmd ("umount $targetdir/boot/efi") == 0 ||
1334 die "unable to umount $targetdir/boot/efi\n";
1335 }
1336 }
1337
1338 syscmd ("chroot $targetdir /usr/sbin/update-grub") == 0 ||
1339 die "unable to update boot loader config\n";
1340
1341 syscmd ("umount $targetdir/dev");
1342 }
1343
1344 # cleanup
1345
1346 # hack: remove dead.letter from sshd installation
1347 syscmd ("rm -rf $targetdir/dead.letter");
1348
1349 unlink "$targetdir/usr/sbin/policy-rc.d";
1350
1351 diversion_remove ($targetdir, "/sbin/start-stop-daemon");
1352
1353 # set root password
1354 my $octets = encode("utf-8", $password);
1355 run_command ("chroot $targetdir /usr/sbin/chpasswd", undef,
1356 "root:$octets\n");
1357
1358 # create pmxcfs DB
1359
1360 my $tmpdir = "$targetdir/tmp/pve";
1361 mkdir $tmpdir;
1362
1363 # write vnc keymap to datacenter.cfg
1364 my $vnckmap = $cmap->{kmap}->{$keymap}->{kvm} || 'en-us';
1365 write_config ("keyboard: $vnckmap\n",
1366 "$tmpdir/datacenter.cfg");
1367
1368 # save admin email
1369 write_config ("user:root\@pam:1:0:::${mailto}::\n",
1370 "$tmpdir/user.cfg");
1371
1372 # write storage.cfg
1373 my $strorage_cfg_fn = "$tmpdir/storage.cfg";
1374 if ($use_zfs) {
1375 write_config ($storage_cfg_zfs, $strorage_cfg_fn);
1376 } else {
1377 write_config ($storage_cfg_lvmthin, $strorage_cfg_fn);
1378 }
1379
1380 run_command("chroot $targetdir /usr/bin/create_pmxcfs_db /tmp/pve /var/lib/pve-cluster/config.db");
1381
1382 syscmd ("rm -rf $tmpdir");
1383 };
1384
1385 my $err = $@;
1386
1387 update_progress (1, 0, 1, "");
1388
1389 print $err if $err;
1390
1391 if ($opt_testmode) {
1392 syscmd ("chroot $targetdir /usr/bin/dpkg-query -W --showformat='\${package}\n'> pve-final.pkglist");
1393 }
1394
1395 syscmd ("umount $targetdir/var/lib/vz");
1396 syscmd ("umount $targetdir/tmp");
1397 syscmd ("umount $targetdir/proc");
1398 syscmd ("umount $targetdir/sys");
1399
1400 if ($use_zfs) {
1401 syscmd ("zfs umount -a") == 0 ||
1402 die "unable to unmount zfs\n";
1403 } else {
1404 syscmd ("umount -d $targetdir");
1405 }
1406
1407 if (!$err && $use_zfs) {
1408 syscmd ("zfs set sync=standard $zfspoolname") == 0 ||
1409 die "unable to set zfs properties\n";
1410
1411 syscmd ("zfs set mountpoint=/ $zfspoolname/ROOT/pve-1") == 0 ||
1412 die "zfs set mountpoint failed\n";
1413
1414 syscmd ("zpool set bootfs=$zfspoolname/ROOT/pve-1 $zfspoolname") == 0 ||
1415 die "zfs set bootfs failed\n";
1416 }
1417
1418 die $err if $err;
1419 }
1420
1421 sub display_html {
1422 my ($filename) = @_;
1423
1424 my $path = "${proxmox_dir}/html/$filename";
1425 my $url = "file://$path";
1426
1427 my $data = file_get_contents($path);
1428
1429 if ($filename eq 'license.htm') {
1430 my $licensefn = -f "/EULA" ? "/EULA" :
1431 $opt_testmode ? "./copyright" :
1432 "/usr/share/doc/pve-installer/copyright";
1433 my $license = decode('utf8', file_get_contents($licensefn));
1434 $license =~ m/^\s+(.+\n)/;
1435 my $title = $licensefn eq '/EULA' ? "END USER LICENSE AGREEMENT (EULA)": $1;
1436 $data =~ s/__LICENSE__/$license/;
1437 $data =~ s/__LICENSE_TITLE__/$title/;
1438 }
1439
1440 $htmlview->load_html_string($data, $url);
1441 }
1442
1443 sub set_next {
1444 my ($text, $fctn) = @_;
1445
1446 $next_fctn = $fctn;
1447 $text = "_Next" if !$text;
1448 $next->set_label ($text);
1449
1450 $next->grab_focus ();
1451 }
1452
1453 sub url_requested {
1454 my ($doc, $url, $stream) = @_;
1455
1456 $stream->set_cancel_func (sub {}); # hack: avoid warning
1457
1458 my $path = "${proxmox_dir}/html/$url";
1459
1460 if (-f $path) {
1461 open (HTMLTMP, $path) ||
1462 die "unable to open file '$path' - $!\n";
1463 my $buf;
1464 while (my $i = read (HTMLTMP, $buf, 4096)) {
1465 $stream->write ($buf);
1466 Gtk3::main_iteration() while Gtk3::events_pending();
1467 }
1468 close (HTMLTMP);
1469 }
1470
1471 #$stream->close(); # hack: dont close - avoid crash
1472 }
1473
1474 sub create_main_window {
1475
1476 $window = Gtk3::Window->new ();
1477 $window->set_default_size (1024, 768);
1478 $window->set_has_resize_grip(0);
1479 $window->set_decorated (0) if !$opt_testmode;
1480
1481 my $vbox = Gtk3::VBox->new (0, 0);
1482
1483 my $image = Gtk3::Image->new_from_file ("${proxmox_dir}/proxlogo.png");
1484 $vbox->pack_start ($image, 0, 0, 0);
1485
1486 my $hbox = Gtk3::HBox->new (0, 0);
1487 $vbox->pack_start ($hbox, 1, 1, 0);
1488
1489 # my $f1 = Gtk3::Frame->new ('test');
1490 # $f1->set_shadow_type ('none');
1491 # $hbox->pack_start ($f1, 1, 1, 0);
1492
1493 my $sep1 = Gtk3::HSeparator->new;
1494 $vbox->pack_start ($sep1, 0, 0, 0);
1495
1496 $cmdbox = Gtk3::HBox->new ();
1497 $vbox->pack_start ($cmdbox, 0, 0, 10);
1498
1499 $next = Gtk3::Button->new ('_Next');
1500 $next->signal_connect (clicked => sub { &$next_fctn (); });
1501 $cmdbox->pack_end ($next, 0, 0, 10);
1502 my $abort = Gtk3::Button->new ('_Abort');
1503 $abort->set_can_focus (0);
1504 $cmdbox->pack_start ($abort, 0, 0, 10);
1505 $abort->signal_connect (clicked => sub { exit (-1); });
1506
1507 my $vbox2 = Gtk3::VBox->new (0, 0);
1508 $hbox->add ($vbox2);
1509
1510 $htmlview = Gtk3::WebKit::WebView->new();
1511 my $scrolls = Gtk3::ScrolledWindow->new();
1512 $scrolls->add($htmlview);
1513
1514 my $hbox2 = Gtk3::HBox->new (0, 0);
1515 $hbox2->pack_start ($scrolls, 1, 1, 0);
1516
1517 $vbox2->pack_start ($hbox2, 1, 1, 0);
1518
1519 my $vbox3 = Gtk3::VBox->new (0, 0);
1520 $vbox2->pack_start ($vbox3, 0, 0, 0);
1521
1522 my $sep2 = Gtk3::HSeparator->new;
1523 $vbox3->pack_start ($sep2, 0, 0, 0);
1524
1525 $inbox = Gtk3::HBox->new (0, 0);
1526 $vbox3->pack_start ($inbox, 0, 0, 0);
1527
1528 $window->add ($vbox);
1529
1530 $window->show_all;
1531 $window->realize ();
1532 }
1533
1534 sub cleanup_view {
1535 $inbox->foreach(sub {
1536 my $child = shift;
1537 $inbox->remove ($child);
1538 });
1539 }
1540
1541 # fixme: newer GTK3 has special properties to handle numbers with Entry
1542 # only allow floating point numbers with Gtk3::Entry
1543 sub check_float {
1544 my ($entry, $event) = @_;
1545
1546
1547 my $val = $event->get_keyval;
1548
1549 if ($val == ord '.' ||
1550 $val == Gtk3::Gdk::KEY_ISO_Left_Tab ||
1551 $val == Gtk3::Gdk::KEY_Shift_L ||
1552 $val == Gtk3::Gdk::KEY_Tab ||
1553 $val == Gtk3::Gdk::KEY_Left ||
1554 $val == Gtk3::Gdk::KEY_Right ||
1555 $val == Gtk3::Gdk::KEY_BackSpace ||
1556 $val == Gtk3::Gdk::KEY_Delete ||
1557 ($val >= ord '0' && $val <= ord '9') ||
1558 ($val >= Gtk3::Gdk::KEY_KP_0 &&
1559 $val <= Gtk3::Gdk::KEY_KP_9)) {
1560 return undef;
1561 }
1562
1563 return 1;
1564 }
1565
1566 sub create_text_input {
1567 my ($default, $text) = @_;
1568
1569 my $hbox = Gtk3::HBox->new (0, 0);
1570
1571 my $label = Gtk3::Label->new ($text);
1572 $label->set_size_request (150, -1);
1573 $label->set_alignment (1, 0.5);
1574 $hbox->pack_start ($label, 0, 0, 10);
1575 my $e1 = Gtk3::Entry->new ();
1576 $e1->set_width_chars (30);
1577 $hbox->pack_start ($e1, 0, 0, 0);
1578 $e1->set_text ($default);
1579
1580 return ($hbox, $e1);
1581 }
1582
1583 sub get_ip_config {
1584
1585 my $ifconfig = `ifconfig eth0`;
1586
1587 my ($addr) = $ifconfig =~ m/inet addr:(\S*)/m;
1588 my ($mask) = $ifconfig =~ m/Mask:(\S*)/m;
1589
1590 my $route = `route -n`;
1591 my ($gateway) = $route =~ m/^0\.0\.0\.0\s+(\d+\.\d+\.\d+\.\d+)\s+/m;
1592
1593 my $resolvconf = `cat /etc/resolv.conf`;
1594 my ($dnsserver) = $resolvconf =~ m/^nameserver\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/m;
1595 my ($domain) = $resolvconf =~ m/^domain\s+(\S+)$/m;
1596
1597 return {
1598 addr => $addr,
1599 mask => $mask,
1600 gateway => $gateway,
1601 dnsserver => $dnsserver,
1602 domain => $domain,
1603 }
1604 }
1605
1606 sub display_message {
1607 my ($msg) = @_;
1608
1609 my $dialog = Gtk3::MessageDialog->new ($window, 'modal',
1610 'info', 'ok', $msg);
1611 $dialog->run();
1612 $dialog->destroy();
1613 }
1614
1615 sub display_error {
1616 my ($msg) = @_;
1617
1618 my $dialog = Gtk3::MessageDialog->new ($window, 'modal',
1619 'error', 'ok', $msg);
1620 $dialog->run();
1621 $dialog->destroy();
1622 }
1623
1624 sub create_ipconf_view {
1625
1626 cleanup_view ();
1627 display_html ("ipconf.htm");
1628
1629 my $vbox = Gtk3::VBox->new (0, 0);
1630 $inbox->pack_start ($vbox, 1, 0, 0);
1631 my $hbox = Gtk3::HBox->new (0, 0);
1632 $vbox->pack_start ($hbox, 0, 0, 10);
1633 my $vbox2 = Gtk3::VBox->new (0, 0);
1634 $hbox->add ($vbox2);
1635
1636 my $addr = $ipconf->{addr} || '192.168.100.2';
1637 my $mask = $ipconf->{mask} || '255.255.255.0';
1638
1639 my $hn = $ipconf->{domain} ? "pve.$ipconf->{domain}" : 'pve.domain.tld';
1640
1641 my ($hostbox, $hostentry) =
1642 create_text_input ($hn, 'Hostname (FQDN):');
1643 $vbox2->pack_start ($hostbox, 0, 0, 2);
1644
1645 my $ipbox;
1646 ($ipbox, $ipconf_entry_addr) =
1647 create_text_input ($addr, 'IP Address:');
1648 $vbox2->pack_start ($ipbox, 0, 0, 2);
1649
1650 my $maskbox;
1651 ($maskbox, $ipconf_entry_mask) =
1652 create_text_input ($mask, 'Netmask:');
1653 $vbox2->pack_start ($maskbox, 0, 0, 2);
1654
1655 $gateway = $ipconf->{gateway} || '192.168.100.1';
1656
1657 my $gwbox;
1658 ($gwbox, $ipconf_entry_gw) =
1659 create_text_input ($gateway, 'Gateway:');
1660
1661 $vbox2->pack_start ($gwbox, 0, 0, 2);
1662
1663 $dnsserver = $ipconf->{dnsserver} || $gateway;
1664
1665 my $dnsbox;
1666 ($dnsbox, $ipconf_entry_dns) =
1667 create_text_input ($dnsserver, 'DNS Server:');
1668
1669 $vbox2->pack_start ($dnsbox, 0, 0, 0);
1670
1671 $inbox->show_all;
1672 set_next (undef, sub {
1673
1674 # verify hostname
1675
1676 my $text = $hostentry->get_text();
1677
1678 $text =~ s/^\s+//;
1679 $text =~ s/\s+$//;
1680
1681 my $namere = "([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)";
1682
1683 if ($text && $text =~ m/^(${namere}\.)*${namere}$/ && $text !~ m/.domain.tld$/ &&
1684 $text =~ m/^([^\.]+)\.(\S+)$/) {
1685 $hostname = $1;
1686 $domain = $2;
1687 } else {
1688 display_message ("Hostname does not look like a fully qualified domain name.");
1689 $hostentry->grab_focus();
1690 return;
1691 }
1692
1693 # verify ip address
1694
1695 $text = $ipconf_entry_addr->get_text();
1696 $text =~ s/^\s+//;
1697 $text =~ s/\s+$//;
1698 if ($text =~ m!^($IPV4RE)$!) {
1699 $ipaddress = $text;
1700 $ipversion = 4;
1701 } elsif ($text =~ m!^($IPV6RE)$!) {
1702 $ipaddress = $text;
1703 $ipversion = 6;
1704 } else {
1705 display_message ("IP address is not valid.");
1706 $ipconf_entry_addr->grab_focus();
1707 return;
1708 }
1709
1710 $text = $ipconf_entry_mask->get_text();
1711 $text =~ s/^\s+//;
1712 $text =~ s/\s+$//;
1713 if (($ipversion == 6) && ($text =~ m/^(\d+)$/) && ($1 >= 8) && ($1 <= 126)) {
1714 $netmask = $text;
1715 } elsif (($ipversion == 4) && defined($ipv4_mask_hash->{$text})) {
1716 $netmask = $text;
1717 } else {
1718 display_message ("Netmask is not valid.");
1719 $ipconf_entry_mask->grab_focus();
1720 return;
1721 }
1722
1723 $text = $ipconf_entry_gw->get_text();
1724 $text =~ s/^\s+//;
1725 $text =~ s/\s+$//;
1726 if (($ipversion == 4) && ($text =~ m!^($IPV4RE)$!)) {
1727 $gateway = $text;
1728 } elsif (($ipversion == 6) && ($text =~ m!^($IPV6RE)$!)) {
1729 $gateway = $text;
1730 } else {
1731 display_message ("Gateway is not valid.");
1732 $ipconf_entry_gw->grab_focus();
1733 return;
1734 }
1735
1736 $text = $ipconf_entry_dns->get_text();
1737 $text =~ s/^\s+//;
1738 $text =~ s/\s+$//;
1739 if (($ipversion == 4) && ($text =~ m!^($IPV4RE)$!)) {
1740 $dnsserver = $text;
1741 } elsif (($ipversion == 6) && ($text =~ m!^($IPV6RE)$!)) {
1742 $dnsserver = $text;
1743 } else {
1744 display_message ("DNS server is not valid.");
1745 $ipconf_entry_dns->grab_focus();
1746 return;
1747 }
1748
1749 #print "TEST $ipaddress $netmask $gateway $dnsserver\n";
1750
1751 create_extract_view ();
1752 });
1753
1754 $hostentry->grab_focus();
1755 }
1756
1757 sub get_device_desc {
1758 my ($devname, $size, $model) = @_;
1759
1760 if ($size && ($size > 0)) {
1761 $size = int($size/2048); # size in MB
1762
1763 my $text = "$devname (";
1764 if ($size >= 1024) {
1765 $size = int($size/1024); # size in GB
1766 $text .= "${size}GB";
1767 } else {
1768 $text .= "${size}MB";
1769 }
1770
1771 $text .= ", $model" if $model;
1772 $text .= ")";
1773
1774 } else {
1775 return $devname;
1776 }
1777 }
1778
1779 sub update_layout {
1780 my ($cb, $kmap) = @_;
1781
1782 my $ind;
1783 my $def;
1784 my $i = 0;
1785 my $kmaphash = $cmap->{kmaphash};
1786 foreach my $layout (sort keys %$kmaphash) {
1787 $def = $i if $kmaphash->{$layout} eq 'en-us';
1788 $ind = $i if $kmap && $kmaphash->{$layout} eq $kmap;
1789 $i++;
1790 }
1791
1792 $cb->set_active ($ind || $def || 0);
1793 }
1794
1795 my $lastzonecb;
1796 sub update_zonelist {
1797 my ($box, $cc) = @_;
1798
1799 my $cczones = $cmap->{cczones};
1800 my $zones = $cmap->{zones};
1801
1802 my $sel;
1803 if ($lastzonecb) {
1804 $sel = $lastzonecb->get_active_text();
1805 $box->remove ($lastzonecb);
1806 } else {
1807 $sel = $timezone; # used once to select default
1808 }
1809
1810 my $cb = $lastzonecb = Gtk3::ComboBoxText->new();
1811 $cb->set_size_request (200, -1);
1812
1813 $cb->signal_connect ('changed' => sub {
1814 $timezone = $cb->get_active_text();
1815 });
1816
1817 my @za;
1818 if ($cc && defined ($cczones->{$cc})) {
1819 @za = keys %{$cczones->{$cc}};
1820 } else {
1821 @za = keys %$zones;
1822 }
1823 my $ind;
1824 my $i = 0;
1825 foreach my $zone (sort @za) {
1826 $ind = $i if $sel && $zone eq $sel;
1827 $cb->append_text ($zone);
1828 $i++;
1829 }
1830
1831 $cb->set_active ($ind || 0);
1832
1833 $cb->show;
1834 $box->pack_start ($cb, 0, 0, 0);
1835 }
1836
1837 sub create_password_view {
1838
1839 cleanup_view ();
1840
1841 my $vbox2 = Gtk3::VBox->new (0, 0);
1842 $inbox->pack_start ($vbox2, 1, 0, 0);
1843 my $vbox = Gtk3::VBox->new (0, 0);
1844 $vbox2->pack_start ($vbox, 0, 0, 10);
1845
1846 my $hbox1 = Gtk3::HBox->new (0, 0);
1847 my $label = Gtk3::Label->new ("Password");
1848 $label->set_size_request (150, -1);
1849 $label->set_alignment (1, 0.5);
1850 $hbox1->pack_start ($label, 0, 0, 10);
1851 my $pwe1 = Gtk3::Entry->new ();
1852 $pwe1->set_visibility (0);
1853 $pwe1->set_size_request (200, -1);
1854 $hbox1->pack_start ($pwe1, 0, 0, 0);
1855
1856 my $hbox2 = Gtk3::HBox->new (0, 0);
1857 $label = Gtk3::Label->new ("Confirm");
1858 $label->set_size_request (150, -1);
1859 $label->set_alignment (1, 0.5);
1860 $hbox2->pack_start ($label, 0, 0, 10);
1861 my $pwe2 = Gtk3::Entry->new ();
1862 $pwe2->set_visibility (0);
1863 $pwe2->set_size_request (200, -1);
1864 $hbox2->pack_start ($pwe2, 0, 0, 0);
1865
1866 my $hbox3 = Gtk3::HBox->new (0, 0);
1867 $label = Gtk3::Label->new ("E-Mail");
1868 $label->set_size_request (150, -1);
1869 $label->set_alignment (1, 0.5);
1870 $hbox3->pack_start ($label, 0, 0, 10);
1871 my $eme = Gtk3::Entry->new ();
1872 $eme->set_size_request (200, -1);
1873 $hbox3->pack_start ($eme, 0, 0, 0);
1874
1875
1876 $vbox->pack_start ($hbox1, 0, 0, 5);
1877 $vbox->pack_start ($hbox2, 0, 0, 5);
1878 $vbox->pack_start ($hbox3, 0, 0, 15);
1879
1880 $inbox->show_all;
1881
1882 display_html ("passwd.htm");
1883
1884 set_next (undef, sub {
1885
1886 my $t1 = $pwe1->get_text;
1887 my $t2 = $pwe2->get_text;
1888
1889 if (length ($t1) < 5) {
1890 display_message ("Password is too short.");
1891 $pwe1->grab_focus();
1892 return;
1893 }
1894
1895 if ($t1 ne $t2) {
1896 display_message ("Password does not match.");
1897 $pwe1->grab_focus();
1898 return;
1899 }
1900
1901 my $t3 = $eme->get_text;
1902 if ($t3 !~ m/^\S+\@\S+\.\S+$/) {
1903 display_message ("E-Mail does not look like a vaild address" .
1904 " (user\@domain.tld)");
1905 $eme->grab_focus();
1906 return;
1907
1908 }
1909
1910 $password = $t1;
1911 $mailto = $t3;
1912
1913 create_ipconf_view();
1914 });
1915
1916 $pwe1->grab_focus();
1917
1918 }
1919
1920 sub create_country_view {
1921
1922 cleanup_view ();
1923
1924 my $countryhash = $cmap->{countryhash};
1925 my $ctr = $cmap->{country};
1926
1927 my $vbox2 = Gtk3::VBox->new (0, 0);
1928 $inbox->pack_start ($vbox2, 1, 0, 0);
1929 my $vbox = Gtk3::VBox->new (0, 0);
1930 $vbox2->pack_start ($vbox, 0, 0, 10);
1931
1932 my $w = Gtk3::Entry->new ();
1933 $w->set_size_request (200, -1);
1934
1935 my $c = Gtk3::EntryCompletion->new ();
1936 $c->set_text_column (0);
1937 $c->set_minimum_key_length(0);
1938 $c->set_popup_set_width (1);
1939 $c->set_inline_completion (1);
1940
1941 my $hbox2 = Gtk3::HBox->new (0, 0);
1942 my $label = Gtk3::Label->new ("Time zone");
1943 $label->set_size_request (150, -1);
1944 $label->set_alignment (1, 0.5);
1945 $hbox2->pack_start ($label, 0, 0, 10);
1946 update_zonelist ($hbox2);
1947
1948 my $hbox3 = Gtk3::HBox->new (0, 0);
1949 $label = Gtk3::Label->new ("Keyboard Layout");
1950 $label->set_size_request (150, -1);
1951 $label->set_alignment (1, 0.5);
1952 $hbox3->pack_start ($label, 0, 0, 10);
1953
1954 my $kmapcb = Gtk3::ComboBoxText->new();
1955 $kmapcb->set_size_request (200, -1);
1956 foreach my $layout (sort keys %{$cmap->{kmaphash}}) {
1957 $kmapcb->append_text ($layout);
1958 }
1959
1960 update_layout ($kmapcb);
1961 $hbox3->pack_start ($kmapcb, 0, 0, 0);
1962
1963 $kmapcb->signal_connect ('changed' => sub {
1964 my $sel = $kmapcb->get_active_text();
1965 if (my $kmap = $cmap->{kmaphash}->{$sel}) {
1966 my $xkmap = $cmap->{kmap}->{$kmap}->{x11};
1967 my $xvar = $cmap->{kmap}->{$kmap}->{x11var};
1968 syscmd ("setxkbmap $xkmap $xvar") if !$opt_testmode;
1969 $keymap = $kmap;
1970 }
1971 });
1972
1973 $w->signal_connect ('changed' => sub {
1974 my ($entry, $event) = @_;
1975 my $text = $entry->get_text;
1976
1977 if (my $cc = $countryhash->{lc($text)}) {
1978 update_zonelist ($hbox2, $cc);
1979 my $kmap = $ctr->{$cc}->{kmap} || 'en-us';
1980 update_layout ($kmapcb, $kmap);
1981 }
1982 });
1983
1984 $w->signal_connect (key_press_event => sub {
1985 my ($entry, $event) = @_;
1986 my $text = $entry->get_text;
1987
1988 my $val = $event->get_keyval;
1989
1990 if ($val == Gtk3::Gdk::KEY_Tab) {
1991 my $cc = $countryhash->{lc($text)};
1992
1993 my $found = 0;
1994 my $compl;
1995
1996 if ($cc) {
1997 $found = 1;
1998 $compl = $ctr->{$cc}->{name};
1999 } else {
2000 foreach my $cc (keys %$ctr) {
2001 my $ct = $ctr->{$cc}->{name};
2002 if ($ct =~ m/^\Q$text\E.*$/i) {
2003 $found++;
2004 $compl = $ct;
2005 }
2006 last if $found > 1;
2007 }
2008 }
2009
2010 if ($found == 1) {
2011 $entry->set_text($compl);
2012 $c->complete();
2013 return undef;
2014 } else {
2015 #Gtk3::Gdk::beep();
2016 print chr(7); # beep ?
2017 }
2018
2019 $c->complete();
2020
2021 my $buf = $w->get_buffer();
2022 $buf->insert_text(-1, '', -1); # popup selection
2023
2024 return 1;
2025 }
2026
2027 return undef;
2028 });
2029
2030 my $ls = Gtk3::ListStore->new('Glib::String');
2031 foreach my $cc (sort {$ctr->{$a}->{name} cmp $ctr->{$b}->{name} } keys %$ctr) {
2032 my $iter = $ls->append();
2033 $ls->set ($iter, 0, $ctr->{$cc}->{name});
2034 }
2035 $c->set_model ($ls);
2036
2037 $w->set_completion ($c);
2038
2039 my $hbox = Gtk3::HBox->new (0, 0);
2040
2041 $label = Gtk3::Label->new ("Country");
2042 $label->set_alignment (1, 0.5);
2043 $label->set_size_request (150, -1);
2044 $hbox->pack_start ($label, 0, 0, 10);
2045 $hbox->pack_start ($w, 0, 0, 0);
2046
2047 $vbox->pack_start ($hbox, 0, 0, 5);
2048 $vbox->pack_start ($hbox2, 0, 0, 5);
2049 $vbox->pack_start ($hbox3, 0, 0, 5);
2050
2051 if ($country) {
2052 $w->set_text ($ctr->{$country}->{name});
2053 }
2054
2055 $inbox->show_all;
2056
2057 display_html ("country.htm");
2058 set_next (undef, sub {
2059
2060 my $text = $w->get_text;
2061
2062 if (my $cc = $countryhash->{lc($text)}) {
2063 $country = $cc;
2064 create_password_view();
2065 return;
2066 } else {
2067 display_message ("Please select a country first.");
2068 $w->grab_focus();
2069 }
2070 });
2071
2072 $w->grab_focus();
2073 }
2074
2075 my $target_hd_combo;
2076 my $target_hd_label;
2077
2078 my $hdopion_first_setup = 1;
2079
2080 sub create_hdoption_view {
2081
2082 my $dialog = Gtk3::Dialog->new();
2083
2084 $dialog->set_title("Harddisk options");
2085
2086 $dialog->add_button("_OK", 1);
2087
2088 my $contarea = $dialog->get_content_area();
2089
2090 my $hbox2 = Gtk3::Box->new('horizontal', 0);
2091 $contarea->pack_start($hbox2, 1, 1, 10);
2092
2093 my $grid = Gtk3::Grid->new();
2094 $grid->set_column_spacing(10);
2095 $grid->set_row_spacing(10);
2096
2097 $hbox2->pack_start($grid, 1, 0, 10);
2098
2099 my $row = 0;
2100
2101 # Filesystem type
2102
2103 my $label0 = Gtk3::Label->new ("Filesystem");
2104 $label0->set_alignment (1, 0.5);
2105 $grid->attach($label0, 0, $row, 1, 1);
2106
2107 my $fstypecb = Gtk3::ComboBoxText->new();
2108
2109 my $fstype = ['ext3', 'ext4', 'xfs', 'zfs (RAID0)', 'zfs (RAID1)',
2110 'zfs (RAID10)', 'zfs (RAIDZ-1)',
2111 'zfs (RAIDZ-2)', 'zfs (RAIDZ-3)'];
2112
2113 my $tcount = 0;
2114 foreach my $tmp (@$fstype) {
2115 $fstypecb->append_text($tmp);
2116 $fstypecb->set_active ($tcount)
2117 if $config_options->{filesys} eq $tmp;
2118 $tcount++;
2119 }
2120
2121 $grid->attach($fstypecb, 1, $row, 1, 1);
2122
2123 $hbox2->show_all();
2124
2125 $row++;
2126
2127 my @disk_label;
2128 my @disk_selector;
2129 for (my $i = 0; $i < 8; $i++) {
2130 $disk_label[$i] = Gtk3::Label->new ("Hardisk $i");
2131 $disk_label[$i]->set_alignment (1, 0.5);
2132 $grid->attach($disk_label[$i], 0, $row, 1, 1);
2133 $disk_selector[$i] = Gtk3::ComboBoxText->new();
2134 $disk_selector[$i]->append_text("-- do not use --");
2135 $disk_selector[$i]->set_active(0);
2136 foreach my $hd (@$hds) {
2137 my ($disk, $devname, $size, $model) = @$hd;
2138 $disk_selector[$i]->append_text(get_device_desc ($devname, $size, $model));
2139 $disk_selector[$i]->{pve_disk_id} = $i;
2140 $disk_selector[$i]->signal_connect (changed => sub {
2141 my $w = shift;
2142 my $diskid = $w->{pve_disk_id};
2143 my $a = $w->get_active - 1;
2144 $config_options->{"disksel${diskid}"} = ($a >= 0) ? $hds->[$a] : undef;
2145 });
2146 }
2147
2148 if ($hdopion_first_setup) {
2149 $disk_selector[$i]->set_active ($i+1) if $hds->[$i];
2150 } else {
2151 my $hdind = 0;
2152 if (my $cur_hd = $config_options->{"disksel$i"}) {
2153 foreach my $hd (@$hds) {
2154 if (@$hd[1] eq @$cur_hd[1]) {
2155 $disk_selector[$i]->set_active($hdind+1);
2156 last;
2157 }
2158 $hdind++;
2159 }
2160
2161 }
2162 }
2163
2164 $grid->attach($disk_selector[$i], 1, $row, 1, 1);
2165
2166 $row++;
2167 }
2168
2169 $hdopion_first_setup = 0;
2170
2171 my $label_hdsize = Gtk3::Label->new ("hdsize");
2172 $label_hdsize->set_alignment (1, 0.5);
2173 $grid->attach($label_hdsize, 0, $row, 1, 1);
2174
2175 # size compute
2176 my $hdsize = 0;
2177 if ( -b $target_hd) {
2178 $hdsize = int(hd_size ($target_hd) / (1024*1024.0)); # size in GB
2179 } elsif ($target_hd) {
2180 $hdsize = int((-s $target_hd) / (1024*1024*1024.0));
2181 }
2182
2183 my $hdsize_size_adj = Gtk3::Adjustment->new($config_options->{hdsize} || $hdsize, 0, $hdsize+1, 1, 1, 1);
2184 my $spinbutton_hdsize = Gtk3::SpinButton->new($hdsize_size_adj, 1, 1);
2185 $spinbutton_hdsize->set_tooltip_text("only use specified size (GB) of the harddisk (rest left unpartitioned)");
2186 $grid->attach($spinbutton_hdsize, 1, $row, 1, 1);
2187 $row++;
2188
2189 my $label_swapsize = Gtk3::Label->new ("swapsize");
2190 $label_swapsize->set_alignment (1, 0.5);
2191 $grid->attach($label_swapsize, 0, $row, 1, 1);
2192
2193 my $entry_swapsize = Gtk3::Entry->new();
2194 $entry_swapsize->set_tooltip_text("maximum SWAP size (GB)");
2195 $entry_swapsize->signal_connect (key_press_event => \&check_float);
2196 $entry_swapsize->set_text($config_options->{swapsize}) if $config_options->{swapsize};
2197 $grid->attach($entry_swapsize, 1, $row, 1, 1);
2198 $row++;
2199
2200 my $label_maxroot = Gtk3::Label->new ("maxroot");
2201 $label_maxroot->set_alignment (1, 0.5);
2202 $grid->attach($label_maxroot, 0, $row, 1, 1);
2203
2204 my $entry_maxroot = Gtk3::Entry->new();
2205 $entry_maxroot->set_tooltip_text("maximum size (GB) for LVM root volume");
2206 $entry_maxroot->signal_connect (key_press_event => \&check_float);
2207 $entry_maxroot->set_text($config_options->{maxroot}) if $config_options->{maxroot};
2208 $grid->attach($entry_maxroot, 1, $row, 1, 1);
2209 $row++;
2210
2211 my $label_minfree = Gtk3::Label->new ("minfree");
2212 $label_minfree->set_alignment (1, 0.5);
2213 $grid->attach($label_minfree, 0, $row, 1, 1);
2214
2215 my $entry_minfree = Gtk3::Entry->new();
2216 $entry_minfree->set_tooltip_text("minumum free LVM space (GB, required for LVM snapshots)");
2217 $entry_minfree->signal_connect (key_press_event => \&check_float);
2218 $entry_minfree->set_text($config_options->{minfree}) if $config_options->{minfree};
2219 $grid->attach($entry_minfree, 1, $row, 1, 1);
2220 $row++;
2221
2222 my $label_maxvz = Gtk3::Label->new ("maxvz");
2223 $label_maxvz->set_alignment (1, 0.5);
2224 $grid->attach($label_maxvz, 0, $row, 1, 1);
2225
2226 my $entry_maxvz = Gtk3::Entry->new();
2227 $entry_maxvz->set_tooltip_text("maximum size (GB) for LVM data volume");
2228 $entry_maxvz->signal_connect (key_press_event => \&check_float);
2229 $entry_maxvz->set_text($config_options->{maxvz}) if $config_options->{maxvz};
2230 $grid->attach($entry_maxvz, 1, $row, 1, 1);
2231 $row++;
2232
2233 my $set_sensitive_flag = sub {
2234 my $enable_size_hints = $config_options->{filesys} !~ m/zfs/;
2235
2236 if ($config_options->{filesys} =~ m/zfs/) {
2237 $target_hd_combo->set_visible(0);
2238 $target_hd_label->set_text("Target: $config_options->{filesys} ");
2239 foreach my $w (@disk_selector) { $w->set_visible(1); }
2240 foreach my $w (@disk_label) { $w->set_visible(1); }
2241 } else {
2242 $target_hd_combo->set_visible(1);
2243 foreach my $w (@disk_selector) { $w->set_visible(0); }
2244 foreach my $w (@disk_label) { $w->set_visible(0); }
2245 $target_hd_label->set_text("Target Harddisk: ");
2246 }
2247
2248 $spinbutton_hdsize->set_visible($enable_size_hints);
2249 $label_hdsize->set_visible($enable_size_hints);
2250 $entry_swapsize->set_visible($enable_size_hints);
2251 $label_swapsize->set_visible($enable_size_hints);
2252 $entry_maxroot->set_visible($enable_size_hints);
2253 $label_maxroot->set_visible($enable_size_hints);
2254 $entry_minfree->set_visible($enable_size_hints);
2255 $label_minfree->set_visible($enable_size_hints);
2256 $entry_maxvz->set_visible($enable_size_hints);
2257 $label_maxvz->set_visible($enable_size_hints);
2258 };
2259
2260 &$set_sensitive_flag();
2261
2262 $fstypecb->signal_connect (changed => sub {
2263 $config_options->{filesys} = $fstypecb->get_active_text();
2264 &$set_sensitive_flag();
2265 });
2266
2267 $dialog->show();
2268
2269 $dialog->run();
2270
2271 my $get_float = sub {
2272 my ($entry) = @_;
2273
2274 my $text = $entry->get_text();
2275 return undef if !defined($text);
2276
2277 $text =~ s/^\s+//;
2278 $text =~ s/\s+$//;
2279
2280 return undef if $text !~ m/^\d+(\.\d+)?$/;
2281
2282 return $text;
2283 };
2284
2285 my $tmp;
2286
2287 if (($tmp = &$get_float($spinbutton_hdsize)) && ($tmp != $hdsize)) {
2288 $config_options->{hdsize} = $tmp;
2289 } else {
2290 delete $config_options->{hdsize};
2291 }
2292
2293 if (defined($tmp = &$get_float($entry_swapsize))) {
2294 $config_options->{swapsize} = $tmp;
2295 } else {
2296 delete $config_options->{swapsize};
2297 }
2298
2299 if (defined($tmp = &$get_float($entry_maxroot))) {
2300 $config_options->{maxroot} = $tmp;
2301 } else {
2302 delete $config_options->{maxroot};
2303 }
2304
2305 if (defined($tmp = &$get_float($entry_minfree))) {
2306 $config_options->{minfree} = $tmp;
2307 } else {
2308 delete $config_options->{minfree};
2309 }
2310
2311 if (defined($tmp = &$get_float($entry_maxvz))) {
2312 $config_options->{maxvz} = $tmp;
2313 } else {
2314 delete $config_options->{maxvz};
2315 }
2316
2317 $dialog->destroy();
2318 }
2319
2320 sub get_zfs_raid_setup {
2321
2322 my $filesys = $config_options->{filesys};
2323
2324 my $dev_name_hash = {};
2325
2326 my $devlist = [];
2327 my $bootdevlist = [];
2328 for (my $i = 0; $i < 8; $i++) {
2329 if (my $hd = $config_options->{"disksel$i"}) {
2330 my ($disk, $devname, $size, $model) = @$hd;
2331 die "device '$devname' is used more than once\n"
2332 if $dev_name_hash->{$devname};
2333 $dev_name_hash->{$devname} = $hd;
2334 push @$devlist, $hd;
2335 }
2336 }
2337
2338 my $diskcount = scalar(@$devlist);
2339 die "$filesys need at least one device\n" if $diskcount < 1;
2340
2341 my $cmd= '';
2342 if ($filesys eq 'zfs (RAID0)') {
2343 push @$bootdevlist, @$devlist[0];
2344 foreach my $hd (@$devlist) {
2345 $cmd .= " @$hd[1]";
2346 }
2347 } elsif ($filesys eq 'zfs (RAID1)') {
2348 die "zfs (RAID1) need at least 2 device\n" if $diskcount < 2;
2349 $cmd .= ' mirror ';
2350 my $hd = @$devlist[0];
2351 my $expected_size = @$hd[2]; # all disks needs same size
2352 foreach $hd (@$devlist) {
2353 die "mirrored disks must have same size\n" if @$hd[2] != $expected_size;
2354 $cmd .= " @$hd[1]";
2355 push @$bootdevlist, $hd;
2356 }
2357 } elsif ($filesys eq 'zfs (RAID10)') {
2358 my $half = int($diskcount/2);
2359 die "zfs (RAID10) need at least 4 device\n" if $diskcount < 4;
2360 die "zfs (RAID10) need at an even numver of devices\n" if $half*2 != $diskcount;
2361
2362 push @$bootdevlist, @$devlist[0], @$devlist[1];
2363
2364 for (my $i = 0; $i < $diskcount; $i+=2) {
2365 my $hd1 = @$devlist[$i];
2366 my $hd2 = @$devlist[$i+1];
2367 die "mirrored disks must have same size\n" if @$hd1[2] != @$hd2[2];
2368 $cmd .= ' mirror ' . @$hd1[1] . ' ' . @$hd2[1];
2369 }
2370
2371 } elsif ($filesys =~ m/^zfs \(RAIDZ-([123])\)$/) {
2372 my $level = $1;
2373 my $mindisks = 2 + $level;
2374 die "zfs (RAIDZ-$level) need at least $mindisks devices\n" if scalar(@$devlist) < $mindisks;
2375 my $hd = @$devlist[0];
2376 my $expected_size = @$hd[2]; # all disks needs same size
2377 $cmd .= " raidz$level";
2378 foreach $hd (@$devlist) {
2379 die "mirrored disks must have same size\n" if @$hd[2] != $expected_size;
2380 $cmd .= " @$hd[1]";
2381 push @$bootdevlist, $hd;
2382 }
2383 } else {
2384 die "unknown zfs mode '$filesys'\n";
2385 }
2386
2387 return ($devlist, $bootdevlist, $cmd);
2388 }
2389
2390 sub create_hdsel_view {
2391
2392 cleanup_view ();
2393
2394 my $vbox = Gtk3::VBox->new (0, 0);
2395 $inbox->pack_start ($vbox, 1, 0, 0);
2396 my $hbox = Gtk3::HBox->new (0, 0);
2397 $vbox->pack_start ($hbox, 0, 0, 10);
2398
2399 my ($disk, $devname, $size, $model) = @{@$hds[0]};
2400 $target_hd = $devname;
2401
2402 $target_hd_label = Gtk3::Label->new ("Target Harddisk: ");
2403 $hbox->pack_start ($target_hd_label, 0, 0, 0);
2404
2405 $target_hd_combo = Gtk3::ComboBoxText->new();
2406
2407 foreach my $hd (@$hds) {
2408 ($disk, $devname, $size, $model) = @$hd;
2409 $target_hd_combo->append_text (get_device_desc ($devname, $size, $model));
2410 }
2411
2412 $target_hd_combo->set_active (0);
2413 $target_hd_combo->signal_connect (changed => sub {
2414 $a = shift->get_active;
2415 my ($disk, $devname) = @{@$hds[$a]};
2416 $target_hd = $devname;
2417 });
2418
2419 $hbox->pack_start ($target_hd_combo, 0, 0, 10);
2420
2421 my $options = Gtk3::Button->new ('_Options');
2422 $options->signal_connect (clicked => \&create_hdoption_view);
2423 $hbox->pack_start ($options, 0, 0, 0);
2424
2425
2426 $inbox->show_all;
2427
2428 display_html ("page1.htm");
2429
2430 set_next (undef, sub {
2431
2432 if ($config_options->{filesys} =~ m/zfs/) {
2433 eval { get_zfs_raid_setup(); };
2434 if (my $err = $@) {
2435 display_message ("Warning: $err\n" .
2436 "Please fix ZFS setup first.");
2437 } else {
2438 create_country_view();
2439 }
2440 } else {
2441 create_country_view();
2442 }
2443 });
2444 }
2445
2446 sub create_extract_view {
2447
2448 cleanup_view ();
2449
2450 display_html ("extract1-license.htm");
2451 $next->set_sensitive (0);
2452
2453 my $vbox = Gtk3::VBox->new (0, 0);
2454 $inbox->pack_start ($vbox, 1, 0, 0);
2455 my $hbox = Gtk3::HBox->new (0, 0);
2456 $vbox->pack_start ($hbox, 0, 0, 10);
2457
2458 my $vbox2 = Gtk3::VBox->new (0, 0);
2459 $hbox->pack_start ($vbox2, 0, 0, 0);
2460
2461 $progress_status = Gtk3::Label->new ('');
2462 $vbox2->pack_start ($progress_status, 1, 1, 0);
2463
2464 $progress = Gtk3::ProgressBar->new;
2465 $progress->set_show_text(1);
2466 $progress->set_size_request (600, -1);
2467
2468 $vbox2->pack_start ($progress, 0, 0, 0);
2469
2470 $inbox->show_all;
2471
2472 my $tdir = $opt_testmode ? "target" : "/target";
2473 mkdir $tdir;
2474 my $base = $opt_testmode ? "/pve/$release/install/pve-base.tar" : "/proxmox/pve-base.tar";
2475
2476 eval { extract_data ($base, $tdir); };
2477 my $err = $@;
2478
2479 $next->set_sensitive (1);
2480
2481 set_next ("_Reboot", sub { exit (0); } );
2482
2483 if ($err) {
2484 display_html ("fail.htm");
2485 display_error ($err);
2486 } else {
2487 cleanup_view ();
2488 display_html ("success.htm");
2489 }
2490 }
2491
2492 sub create_intro_view {
2493
2494 cleanup_view ();
2495
2496 display_html ("license.htm");
2497
2498 set_next ("I a_gree", \&create_hdsel_view);
2499 }
2500
2501 $ipconf = get_ip_config ();
2502
2503 $country = detect_country() if $ipconf->{addr} || $opt_testmode;;
2504
2505 # read country, kmap and timezone infos
2506 $cmap = read_cmap ();
2507
2508 create_main_window ();
2509
2510 if (!defined ($hds) || (scalar (@$hds) <= 0)) {
2511 print "no hardisks found\n";
2512 display_html ("nohds.htm");
2513 set_next ("Reboot", sub { exit (0); } );
2514 } else {
2515
2516 foreach my $hd (@$hds) {
2517 my ($disk, $devname) = @$hd;
2518 next if $devname =~ m|^/dev/md\d+$|;
2519 print "found Disk$disk N:$devname\n";
2520 }
2521
2522 create_intro_view ();
2523 }
2524
2525 Gtk3->main;
2526
2527 exit 0;