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