]> git.proxmox.com Git - pve-installer.git/blob - proxinstall
rename Proxmox::Install::Setup to Proxmox::Install::Env
[pve-installer.git] / proxinstall
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 $ENV{DEBIAN_FRONTEND} = 'noninteractive';
7 $ENV{LC_ALL} = 'C';
8
9 use Getopt::Long;
10 use IPC::Open2;
11 use IO::File;
12 use Cwd 'abs_path';
13 use Glib;
14 use Gtk3 '-init';
15 use Gtk3::WebKit2;
16 use Encode;
17 use File::Basename;
18 use File::Path;
19 use Time::HiRes;
20 use POSIX ":sys_wait_h";
21
22 use Proxmox::Install::Env;
23 use Proxmox::Log;
24 use Proxmox::Sys::Command qw(run_command syscmd);
25 use Proxmox::Sys::File qw(file_read_firstline file_read_all file_write_all);
26 use Proxmox::Sys::Net qw(parse_ip_address parse_ip_mask);
27
28 if (!$ENV{G_SLICE} || $ENV{G_SLICE} ne "always-malloc") {
29 die "do not use slice allocator (run with 'G_SLICE=always-malloc ./proxinstall ...')\n";
30 }
31
32 my $test_image;
33 GetOptions(
34 'test-image|t=s' => \$test_image
35 ) or die "usage error\n";
36
37 Proxmox::Install::Env::enable_test_mode() if $test_image;
38
39 $ENV{'LVM_SUPPRESS_FD_WARNINGS'} = '1';
40
41 my ($setup, $cd_info) = Proxmox::Install::Env::setup();
42
43 my $zfstestpool = "test_rpool";
44 my $zfspoolname = is_test_mode() ? $zfstestpool : 'rpool';
45 my $zfsrootvolname = "$setup->{product}-1";
46
47 my $storage_cfg_zfs = <<__EOD__;
48 dir: local
49 path /var/lib/vz
50 content iso,vztmpl,backup
51
52 zfspool: local-zfs
53 pool $zfspoolname/data
54 sparse
55 content images,rootdir
56 __EOD__
57
58 my $storage_cfg_btrfs = <<__EOD__;
59 dir: local
60 path /var/lib/vz
61 content iso,vztmpl,backup
62 disable
63
64 btrfs: local-btrfs
65 path /var/lib/pve/local-btrfs
66 content iso,vztmpl,backup,images,rootdir
67 __EOD__
68
69 my $storage_cfg_lvmthin = <<__EOD__;
70 dir: local
71 path /var/lib/vz
72 content iso,vztmpl,backup
73
74 lvmthin: local-lvm
75 thinpool data
76 vgname pve
77 content rootdir,images
78 __EOD__
79
80 my $storage_cfg_local = <<__EOD__;
81 dir: local
82 path /var/lib/vz
83 content iso,vztmpl,backup,rootdir,images
84 __EOD__
85
86 Proxmox::Log::init("/tmp/install.log");
87
88 my $proxmox_libdir = is_test_mode()
89 ? Cwd::cwd() . "/testdir/var/lib/proxmox-installer"
90 : "/var/lib/proxmox-installer"
91 ;
92 my $proxmox_cddir = is_test_mode() ? "../pve-cd-builder/tmp/data-gz/" : "/cdrom";
93 my $proxmox_pkgdir = "${proxmox_cddir}/proxmox/packages/";
94
95 my $boot_type = -d '/sys/firmware/efi' ? 'efi' : 'bios';
96
97 my $step_number = 0; # Init number for global function list
98
99 my @steps = (
100 {
101 step => 'intro',
102 html => 'license.htm',
103 next_button => 'I a_gree',
104 function => \&create_intro_view,
105 },
106 {
107 step => 'intro',
108 html => 'page1.htm',
109 function => \&create_hdsel_view,
110 },
111 {
112 step => 'country',
113 html => 'country.htm',
114 function => \&create_country_view,
115 },
116 {
117 step => 'password',
118 html => 'passwd.htm',
119 function => \&create_password_view,
120 },
121 {
122 step => 'ipconf',
123 html => 'ipconf.htm',
124 function => \&create_ipconf_view,
125 },
126 {
127 step => 'ack',
128 html => 'ack.htm',
129 next_button => '_Install',
130 function => \&create_ack_view,
131 },
132 {
133 step => 'extract',
134 next_button => '_Reboot',
135 function => \&create_extract_view,
136 },
137 );
138
139 # GUI global variables
140 my ($window, $cmdbox, $inbox, $htmlview);
141 my $prev_btn;
142 my ($next, $next_fctn, $target_hd);
143 my ($progress, $progress_status);
144
145 my ($ipversion, $ipaddress, $cidr, $ipconf_entry_addr);
146 my ($netmask, $ipconf_entry_mask);
147 my ($gateway, $ipconf_entry_gw);
148 my ($dnsserver, $ipconf_entry_dns);
149 my $hostname = 'proxmox';
150 my $domain = 'domain.tld';
151 my $cmdline = file_read_firstline("/proc/cmdline");
152 my $ipconf;
153 my $country;
154 my $timezone = 'Europe/Vienna';
155 my $keymap = 'en-us';
156 my $password;
157 my $mailto = 'mail@example.invalid';
158 my $cmap;
159 my $autoreboot_seconds = 5;
160
161 my $config = {
162 # TODO: add all the user-provided options for previous button
163 country => $country,
164 timezone => $timezone,
165 keymap => $keymap,
166
167 password => $password,
168 mailto => $mailto,
169
170 mngmt_nic => undef,
171 hostname => $hostname,
172 fqdn => undef,
173 ipaddress => undef,
174 netmask => undef,
175 gateway => undef,
176 };
177
178 # parse command line args
179
180 my $config_options = {
181 autoreboot => 1,
182 };
183
184 if ($cmdline =~ m/\s(ext4|xfs)(\s.*)?$/) {
185 $config_options->{filesys} = $1;
186 } else {
187 $config_options->{filesys} = 'ext4';
188 }
189
190 if ($cmdline =~ m/hdsize=(\d+(\.\d+)?)[\s\n]/i) {
191 $config_options->{hdsize} = $1;
192 }
193
194 if ($cmdline =~ m/swapsize=(\d+(\.\d+)?)[\s\n]/i) {
195 $config_options->{swapsize} = $1;
196 }
197
198 if ($cmdline =~ m/maxroot=(\d+(\.\d+)?)[\s\n]/i) {
199 $config_options->{maxroot} = $1;
200 }
201
202 if ($cmdline =~ m/minfree=(\d+(\.\d+)?)[\s\n]/i) {
203 $config_options->{minfree} = $1;
204 }
205
206 if ($setup->{product} eq 'pve') {
207 if ($cmdline =~ m/maxvz=(\d+(\.\d+)?)[\s\n]/i) {
208 $config_options->{maxvz} = $1;
209 }
210 }
211
212 my $postfix_main_cf = <<_EOD;
213 # See /usr/share/postfix/main.cf.dist for a commented, more complete version
214
215 myhostname=__FQDN__
216
217 smtpd_banner = \$myhostname ESMTP \$mail_name (Debian/GNU)
218 biff = no
219
220 # appending .domain is the MUA's job.
221 append_dot_mydomain = no
222
223 # Uncomment the next line to generate "delayed mail" warnings
224 #delay_warning_time = 4h
225
226 alias_maps = hash:/etc/aliases
227 alias_database = hash:/etc/aliases
228 mydestination = \$myhostname, localhost.\$mydomain, localhost
229 relayhost =
230 mynetworks = 127.0.0.0/8
231 inet_interfaces = loopback-only
232 recipient_delimiter = +
233
234 compatibility_level = 2
235
236 _EOD
237
238
239 sub detect_country {
240
241 print "trying to detect country...\n";
242 my $cpid = open2(\*TMP, undef, "traceroute -N 1 -q 1 -n 8.8.8.8");
243 return undef if !$cpid;
244
245 my $country;
246
247 my $previous_alarm = alarm (10);
248 eval {
249 local $SIG{ALRM} = sub { die "timed out!\n" };
250 my $line;
251 while (defined ($line = <TMP>)) {
252 log_debug("DC TRACEROUTE: $line");
253 if ($line =~ m/\s*\d+\s+(\d+\.\d+\.\d+\.\d+)\s/) {
254 my $geoip = `geoiplookup $1`;
255 log_debug("DC GEOIP: $geoip");
256 if ($geoip =~ m/GeoIP Country Edition:\s*([A-Z]+),/) {
257 $country = lc ($1);
258 log_info("DC FOUND: $country\n");
259 last;
260 }
261 }
262 }
263 };
264
265 my $err = $@;
266
267 alarm ($previous_alarm);
268
269 close (TMP);
270
271 if ($err) {
272 print "unable to detect country - $err\n";
273 } elsif ($country) {
274 print "detected country: " . uc($country) . "\n";
275 } else {
276 print "unable to detect country\n";
277 }
278
279 return $country;
280 }
281
282 sub get_memtotal {
283
284 open (my $MEMINFO, '<', '/proc/meminfo');
285
286 my $res = 512; # default to 512 if something goes wrong
287 while (my $line = <$MEMINFO>) {
288 if ($line =~ m/^MemTotal:\s+(\d+)\s*kB/i) {
289 $res = int ($1 / 1024);
290 }
291 }
292
293 close($MEMINFO);
294
295 return $res;
296 }
297
298 my $total_memory = get_memtotal();
299
300 sub is_same_file {
301 my ($a, $b) = @_;
302
303 my ($dev_a ,$ino_a) = stat($a);
304 my ($dev_b, $ino_b) = stat($b);
305
306 return 0 if !($dev_a && $dev_b && $ino_a && $ino_b);
307
308 return $ino_a == $ino_b && $dev_a == $dev_b;
309 }
310
311 sub find_stable_path {
312 my ($stabledir, $bdev) = @_;
313
314 foreach my $path (<$stabledir/*>) {
315 if (is_same_file($path, $bdev)) {
316 return wantarray ? ($path, basename($path)) : $path;
317 }
318 }
319 }
320
321 sub find_dev_by_uuid {
322 my $bdev = shift;
323
324 my ($full_path, $name) = find_stable_path("/dev/disk/by-uuid", $bdev);
325
326 return $name;
327 }
328
329 sub hd_list {
330
331 my $res = ();
332
333 if (is_test_mode()) {
334 my @disks = split /,/, $test_image;
335
336 for my $disk (@disks) {
337 push @$res, [-1, $disk, int((-s $disk)/512), "TESTDISK", 512];
338 }
339 return $res;
340 }
341
342 my $count = 0;
343
344 foreach my $bd (</sys/block/*>) {
345 next if $bd =~ m|^/sys/block/ram\d+$|;
346 next if $bd =~ m|^/sys/block/loop\d+$|;
347 next if $bd =~ m|^/sys/block/md\d+$|;
348 next if $bd =~ m|^/sys/block/dm-.*$|;
349 next if $bd =~ m|^/sys/block/fd\d+$|;
350 next if $bd =~ m|^/sys/block/sr\d+$|;
351
352 my $dev = file_read_firstline("$bd/dev");
353 chomp $dev;
354
355 next if !$dev;
356
357 my $info = `udevadm info --path $bd --query all`;
358 next if !$info;
359
360 next if $info !~ m/^E: DEVTYPE=disk$/m;
361
362 next if $info =~ m/^E: ID_CDROM/m;
363 next if $info =~ m/^E: ID_FS_TYPE=iso9660/m;
364
365 my ($name) = $info =~ m/^N: (\S+)$/m;
366
367 if ($name) {
368 my $real_name = "/dev/$name";
369
370 my $size = file_read_firstline("$bd/size");
371 chomp $size;
372 $size = undef if !($size && $size =~ m/^\d+$/);
373
374 my $model = file_read_firstline("$bd/device/model") || '';
375 $model =~ s/^\s+//;
376 $model =~ s/\s+$//;
377 if (length ($model) > 30) {
378 $model = substr ($model, 0, 30);
379 }
380
381 my $logical_bsize = file_read_firstline("$bd/queue/logical_block_size") // '';
382 chomp $logical_bsize;
383 $logical_bsize = undef if !($logical_bsize && $logical_bsize =~ m/^\d+$/);
384
385 push @$res, [$count++, $real_name, $size, $model, $logical_bsize] if $size;
386 } else {
387 print STDERR "ERROR: unable to map device $dev ($bd)\n";
388 }
389 }
390
391 return $res;
392 }
393
394 sub read_cmap {
395 my $countryfn = "${proxmox_libdir}/country.dat";
396 open (my $TMP, "<:encoding(utf8)", "$countryfn") || die "unable to open '$countryfn' - $!\n";
397 my $line;
398 my $country = {};
399 my $countryhash = {};
400 my $kmap = {};
401 my $kmaphash = {};
402 while (defined ($line = <$TMP>)) {
403 if ($line =~ m|^map:([^\s:]+):([^:]+):([^:]+):([^:]+):([^:]+):([^:]*):$|) {
404 $kmap->{$1} = {
405 name => $2,
406 kvm => $3,
407 console => $4,
408 x11 => $5,
409 x11var => $6,
410 };
411 $kmaphash->{$2} = $1;
412 } elsif ($line =~ m|^([a-z]{2}):([^:]+):([^:]*):([^:]*):$|) {
413 $country->{$1} = {
414 name => $2,
415 kmap => $3,
416 mirror => $4,
417 };
418 $countryhash->{lc($2)} = $1;
419 } else {
420 warn "unable to parse 'country.dat' line: $line";
421 }
422 }
423 close ($TMP);
424 $TMP = undef;
425
426 my $zones = {};
427 my $cczones = {};
428 my $zonefn = "/usr/share/zoneinfo/zone.tab";
429 open ($TMP, '<', "$zonefn") || die "unable to open '$zonefn' - $!\n";
430 while (defined ($line = <$TMP>)) {
431 next if $line =~ m/^\#/;
432 next if $line =~ m/^\s*$/;
433 if ($line =~ m|^([A-Z][A-Z])\s+\S+\s+(([^/]+)/\S+)\s|) {
434 my $cc = lc($1);
435 $cczones->{$cc}->{$2} = 1;
436 $country->{$cc}->{zone} = $2 if !defined ($country->{$cc}->{zone});
437 $zones->{$2} = 1;
438
439 }
440 }
441 close ($TMP);
442
443 return {
444 zones => $zones,
445 cczones => $cczones,
446 country => $country,
447 countryhash => $countryhash,
448 kmap => $kmap,
449 kmaphash => $kmaphash,
450 }
451 }
452
453 # search for Harddisks
454 my $hds = hd_list();
455
456 sub hd_size {
457 my ($dev) = @_;
458
459 foreach my $hd (@$hds) {
460 my ($disk, $devname, $size, $model, $logical_bsize) = @$hd;
461 # size is always (also for 4kn disks) in 512B "sectors"! convert to KB
462 return int($size/2) if $devname eq $dev;
463 }
464
465 die "no such device '$dev'\n";
466 }
467
468 sub logical_blocksize {
469 my ($dev) = @_;
470
471 foreach my $hd (@$hds) {
472 my ($disk, $devname, $size, $model, $logical_bsize) = @$hd;
473 return $logical_bsize if $devname eq $dev;
474 }
475
476 die "no such device '$dev'\n";
477 }
478
479 sub get_partition_dev {
480 my ($dev, $partnum) = @_;
481
482 if ($dev =~ m|^/dev/sd([a-h]?[a-z]\|i[a-v])$|) {
483 return "${dev}$partnum";
484 } elsif ($dev =~ m|^/dev/xvd[a-z]$|) {
485 # Citrix Hypervisor blockdev
486 return "${dev}$partnum";
487 } elsif ($dev =~ m|^/dev/[hxev]d[a-z]$|) {
488 return "${dev}$partnum";
489 } elsif ($dev =~ m|^/dev/[^/]+/c\d+d\d+$|) {
490 return "${dev}p$partnum";
491 } elsif ($dev =~ m|^/dev/[^/]+/d\d+$|) {
492 return "${dev}p$partnum";
493 } elsif ($dev =~ m|^/dev/[^/]+/hd[a-z]$|) {
494 return "${dev}$partnum";
495 } elsif ($dev =~ m|^/dev/nvme\d+n\d+$|) {
496 return "${dev}p$partnum";
497 } else {
498 die "unable to get device for partition $partnum on device $dev\n";
499 }
500
501 }
502
503 sub update_progress {
504 my ($frac, $start, $end, $text) = @_;
505
506 my $part = $end - $start;
507 my $res = $start + $frac * $part;
508
509 $progress->set_fraction ($res);
510 $progress->set_text (sprintf ("%d%%", int ($res*100)));
511 $progress_status->set_text ($text) if defined ($text);
512
513 display_info() if $res < 0.9;
514
515 Gtk3::main_iteration() while Gtk3::events_pending();
516 }
517
518 my $fssetup = {
519 ext4 => {
520 mkfs => 'mkfs.ext4 -F',
521 mkfs_root_opt => '',
522 mkfs_data_opt => '-m 0',
523 root_mountopt => 'errors=remount-ro',
524 },
525 xfs => {
526 mkfs => 'mkfs.xfs -f',
527 mkfs_root_opt => '',
528 mkfs_data_opt => '',
529 root_mountopt => '',
530 },
531 };
532
533 sub create_filesystem {
534 my ($dev, $name, $type, $start, $end, $fs, $fe) = @_;
535
536 my $range = $end - $start;
537 my $rs = $start + $range*$fs;
538 my $re = $start + $range*$fe;
539 my $max = 0;
540
541 my $fsdata = $fssetup->{$type} || die "internal error - unknown file system '$type'";
542 my $opts = $name eq 'root' ? $fsdata->{mkfs_root_opt} : $fsdata->{mkfs_data_opt};
543
544 update_progress(0, $rs, $re, "creating $name filesystem");
545
546 run_command("$fsdata->{mkfs} $opts $dev", sub {
547 my $line = shift;
548
549 if ($line =~ m/Writing inode tables:\s+(\d+)\/(\d+)/) {
550 $max = $2;
551 } elsif ($max && $line =~ m/(\d+)\/$max/) {
552 update_progress(($1/$max)*0.9, $rs, $re);
553 } elsif ($line =~ m/Creating journal.*done/) {
554 update_progress(0.95, $rs, $re);
555 } elsif ($line =~ m/Writing superblocks and filesystem.*done/) {
556 update_progress(1, $rs, $re);
557 }
558 });
559 }
560
561 sub debconfig_set {
562 my ($targetdir, $dcdata) = @_;
563
564 my $cfgfile = "/tmp/debconf.txt";
565 file_write_all("$targetdir/$cfgfile", $dcdata);
566 syscmd("chroot $targetdir debconf-set-selections $cfgfile");
567 unlink "$targetdir/$cfgfile";
568 }
569
570 sub diversion_add {
571 my ($targetdir, $cmd, $new_cmd) = @_;
572
573 syscmd("chroot $targetdir dpkg-divert --package proxmox --add --rename $cmd") == 0
574 || die "unable to exec dpkg-divert\n";
575
576 syscmd("ln -sf ${new_cmd} $targetdir/$cmd") == 0
577 || die "unable to link diversion to ${new_cmd}\n";
578 }
579
580 sub diversion_remove {
581 my ($targetdir, $cmd) = @_;
582
583 syscmd("mv $targetdir/${cmd}.distrib $targetdir/${cmd};") == 0
584 || die "unable to remove $cmd diversion\n";
585
586 syscmd("chroot $targetdir dpkg-divert --remove $cmd") == 0
587 || die "unable to remove $cmd diversion\n";
588 }
589
590 sub btrfs_create {
591 my ($partitions, $mode) = @_;
592
593 die "unknown btrfs mode '$mode'"
594 if !($mode eq 'single' || $mode eq 'raid0' || $mode eq 'raid1' || $mode eq 'raid10');
595
596 my $cmd = ['mkfs.btrfs', '-f'];
597
598 push @$cmd, '-d', $mode, '-m', $mode;
599
600 push @$cmd, @$partitions;
601
602 syscmd($cmd);
603 }
604
605 sub zfs_create_rpool {
606 my ($vdev) = @_;
607
608 my $cmd = "zpool create -f -o cachefile=none";
609
610 $cmd .= " -o ashift=$config_options->{ashift}"
611 if defined($config_options->{ashift});
612
613 syscmd("$cmd $zfspoolname $vdev") == 0 ||
614 die "unable to create zfs root pool\n";
615
616 syscmd("zfs create $zfspoolname/ROOT") == 0 ||
617 die "unable to create zfs $zfspoolname/ROOT volume\n";
618
619 if ($setup->{product} eq 'pve') {
620 syscmd("zfs create $zfspoolname/data") == 0 ||
621 die "unable to create zfs $zfspoolname/data volume\n";
622 }
623
624 syscmd("zfs create $zfspoolname/ROOT/$zfsrootvolname") == 0 ||
625 die "unable to create zfs $zfspoolname/ROOT/$zfsrootvolname volume\n";
626
627 # default to `relatime` on, fast enough for the installer and production
628 syscmd("zfs set atime=on relatime=on $zfspoolname") == 0 ||
629 die "unable to set zfs properties\n";
630
631 my $value = $config_options->{compress};
632 syscmd("zfs set compression=$value $zfspoolname")
633 if defined($value) && $value ne 'off';
634
635 $value = $config_options->{checksum};
636 syscmd("zfs set checksum=$value $zfspoolname")
637 if defined($value) && $value ne 'on';
638
639 $value = $config_options->{copies};
640 syscmd("zfs set copies=$value $zfspoolname")
641 if defined($value) && $value != 1;
642 }
643
644 my $udevadm_trigger_block = sub {
645 my ($nowait) = @_;
646
647 sleep(1) if !$nowait; # give kernel time to reread part table
648
649 # trigger udev to create /dev/disk/by-uuid
650 syscmd("udevadm trigger --subsystem-match block");
651 syscmd("udevadm settle --timeout 10");
652 };
653
654 my $clean_disk = sub {
655 my ($disk) = @_;
656
657 # sort longest first as we need to cleanup depth-first
658 my @partitions = sort { length($b) <=> length($a) }
659 split("\n", `lsblk --output kname --noheadings --path --list $disk`);
660
661 for my $part (@partitions) {
662 next if $part eq $disk;
663 next if $part !~ /^\Q$disk\E/;
664 eval { syscmd("pvremove -ff -y $part"); };
665 eval { syscmd("zpool labelclear -f $part"); };
666 eval { syscmd("dd if=/dev/zero of=$part bs=1M count=16"); };
667 }
668 eval { syscmd("wipefs -a " . cmd2string(\@partitions)) };
669 warn "$@" if $@;
670 };
671
672 sub partition_bootable_disk {
673 my ($target_dev, $maxhdsizegb, $ptype) = @_;
674
675 die "too dangerous" if is_test_mode();
676
677 die "unknown partition type '$ptype'"
678 if !($ptype eq '8E00' || $ptype eq '8300' || $ptype eq 'BF01');
679
680 my $hdsize = hd_size($target_dev); # size in KB (1024 bytes)
681
682 # For bigger disks default to generous ESP size to allow users having multiple kernels/UKI's
683 my $esp_size = $hdsize > 100 * 1024 * 1024 ? 1024 : 512; # MB
684 my $esp_end = $esp_size + 1;
685
686 my $restricted_hdsize_mb = 0; # 0 ==> end of partition
687 if ($maxhdsizegb) {
688 my $maxhdsize = $maxhdsizegb * 1024 * 1024;
689 if ($maxhdsize < $hdsize) {
690 $hdsize = $maxhdsize;
691 $restricted_hdsize_mb = int($hdsize/1024) . 'M';
692 }
693 }
694
695 my $hdgb = int($hdsize/(1024*1024));
696
697 my ($hard_limit, $soft_limit) = (2, 8);
698
699 die "root disk '$target_dev' too small (${hdgb} GB < $hard_limit GB)\n" if $hdgb < $hard_limit;
700 if ($hdgb < $soft_limit) {
701 my $response = display_prompt(
702 "Root disk space ${hdgb} GB is below recommended minimum space of $soft_limit GB,"
703 ." installation might not be successful! Continue?"
704 );
705 die "root disk '$target_dev' too small (${hdgb} GB < $soft_limit GB), and warning not accepted.\n"
706 if $response ne 'ok';
707 }
708
709
710 syscmd("sgdisk -Z ${target_dev}");
711
712 # 1 - BIOS boot partition (Grub Stage2): first free 1 MB
713 # 2 - EFI ESP: next free 512 or 1024 MB
714 # 3 - OS/Data partition: rest, up to $maxhdsize in MB
715
716 my $grubbootdev = get_partition_dev($target_dev, 1);
717 my $efibootdev = get_partition_dev($target_dev, 2);
718 my $osdev = get_partition_dev ($target_dev, 3);
719
720 my $pcmd = ['sgdisk'];
721
722 my $pnum = 2;
723 push @$pcmd, "-n${pnum}:1M:+${esp_size}M", "-t$pnum:EF00";
724
725 $pnum = 3;
726 push @$pcmd, "-n${pnum}:${esp_end}M:${restricted_hdsize_mb}", "-t$pnum:$ptype";
727
728 push @$pcmd, $target_dev;
729
730 my $os_size = $hdsize - $esp_end * 1024; # efi + 1M bios_boot + 1M alignment
731
732 syscmd($pcmd) == 0 ||
733 die "unable to partition harddisk '${target_dev}'\n";
734
735 my $blocksize = logical_blocksize($target_dev);
736
737 if ($blocksize != 4096) {
738 $pnum = 1;
739 $pcmd = ['sgdisk', '-a1', "-n$pnum:34:2047", "-t$pnum:EF02" , $target_dev];
740
741 syscmd($pcmd) == 0 ||
742 die "unable to create bios_boot partition '${target_dev}'\n";
743 }
744
745 &$udevadm_trigger_block();
746
747 foreach my $part ($efibootdev, $osdev) {
748 syscmd("dd if=/dev/zero of=$part bs=1M count=256") if -b $part;
749 }
750
751 return ($os_size, $osdev, $efibootdev);
752 }
753
754 sub get_pv_list_from_vgname {
755 my ($vgname) = @_;
756
757 my $res;
758
759 my $parser = sub {
760 my $line = shift;
761 $line =~ s/^\s+//;
762 $line =~ s/\s+$//;
763 return if !$line;
764 my ($pv, $vg_uuid) = split(/\s+/, $line);
765
766 if (!defined($res->{$vg_uuid}->{pvs})) {
767 $res->{$vg_uuid}->{pvs} = "$pv";
768 } else {
769 $res->{$vg_uuid}->{pvs} .= ", $pv";
770 }
771 };
772 run_command("pvs --noheadings -o pv_name,vg_uuid -S vg_name='$vgname'", $parser, undef, 1);
773
774 return $res;
775 }
776
777 sub ask_existing_vg_rename_or_abort {
778 my ($vgname) = @_;
779
780 # this normally only happens if one put a disk with a PVE installation in
781 # this server and that disk is not the installation target.
782 my $duplicate_vgs = get_pv_list_from_vgname($vgname);
783 return if !$duplicate_vgs;
784
785 my $message = "Detected existing '$vgname' Volume Group(s)! Do you want to:\n";
786
787 for my $vg_uuid (keys %$duplicate_vgs) {
788 my $vg = $duplicate_vgs->{$vg_uuid};
789
790 # no high randomnes properties, but this is only for the cases where
791 # we either have multiple "$vgname" vgs from multiple old PVE disks, or
792 # we have a disk with both a "$vgname" and "$vgname-old"...
793 my $short_uid = sprintf "%08X", rand(0xffffffff);
794 $vg->{new_vgname} = "$vgname-OLD-$short_uid";
795
796 $message .= "rename VG backed by PV '$vg->{pvs}' to '$vg->{new_vgname}'\n";
797 }
798 $message .= "or cancel the installation?";
799
800 my $response = display_prompt($message);
801
802 if ($response eq 'ok') {
803 for my $vg_uuid (keys %$duplicate_vgs) {
804 my $vg = $duplicate_vgs->{$vg_uuid};
805 my $new_vgname = $vg->{new_vgname};
806
807 syscmd("vgrename $vg_uuid $new_vgname") == 0 ||
808 die "could not rename VG from '$vg->{pvs}' ($vg_uuid) to '$new_vgname'!\n";
809 }
810 } else {
811 set_next("_Reboot", sub { exit (0); } );
812 display_html("fail.htm");
813 die "Cancled installation by user, due to already existing volume group '$vgname'\n";
814 }
815 }
816
817 sub create_lvm_volumes {
818 my ($lvmdev, $os_size, $swap_size) = @_;
819
820 my $vgname = $setup->{product};
821
822 ask_existing_vg_rename_or_abort($vgname);
823
824 my $rootdev = "/dev/$vgname/root";
825 my $datadev = "/dev/$vgname/data";
826 my $swapfile;
827
828 # we use --metadatasize 250k, which results in "pe_start = 512"
829 # so pe_start is aligned on a 128k boundary (advantage for SSDs)
830 syscmd("/sbin/pvcreate --metadatasize 250k -y -ff $lvmdev") == 0 ||
831 die "unable to initialize physical volume $lvmdev\n";
832 syscmd("/sbin/vgcreate $vgname $lvmdev") == 0 ||
833 die "unable to create volume group '$vgname'\n";
834
835 my $hdgb = int($os_size / (1024 * 1024));
836
837 # always leave some space at the end to avoid roudning issues with LVM's physical extent (PE)
838 # size of 4 MB.
839 my $space = $hdgb <= 32 ? 4 * 1024 : (($hdgb > 128 ? 16 : $hdgb / 8) * 1024 * 1024);
840
841 my $rootsize;
842 my $datasize = 0;
843
844 if ($setup->{product} eq 'pve') {
845
846 my $maxroot_mb;
847 if ($config_options->{maxroot}) {
848 $maxroot_mb = $config_options->{maxroot} * 1024;
849 } else {
850 $maxroot_mb = 96 * 1024;
851 }
852
853 my $rest = $os_size - $swap_size;
854 my $rest_mb = int($rest / 1024);
855
856 my $rootsize_mb;
857 if ($rest_mb < 12 * 1024) {
858 # no point in wasting space, try to get us actually installed and align down to 4 MB
859 $rootsize_mb = ($rest_mb - 0.1) & ~3;
860 } elsif ($rest_mb < 48 * 1024) {
861 my $masked = int($rest_mb / 2) & ~3; # align down to 4 MB
862 $rootsize_mb = $masked;
863 } else {
864 $rootsize_mb = $rest_mb / 4 + 12 * 1024;
865 }
866
867 $rootsize_mb = $maxroot_mb if $rootsize_mb > $maxroot_mb;
868 $rootsize = int($rootsize_mb * 1024);
869
870 $rest -= $rootsize; # in KB
871
872 my $minfree = $space;
873 if (defined(my $cfg_minfree = $config_options->{minfree})) {
874 $minfree = $cfg_minfree * 1024 * 1024 >= $rest ? $space : $cfg_minfree * 1024 * 1024;
875 }
876
877 $rest = int($rest - $minfree) & ~0xFFF; # align down to 4 MB boundaries
878
879 if (defined(my $maxvz = $config_options->{maxvz})) {
880 $rest = $maxvz * 1024 * 1024 <= $rest ? $maxvz * 1024 * 1024 : $rest;
881 }
882
883 $datasize = $rest;
884
885 } else {
886 my $minfree = defined($config_options->{minfree}) ? $config_options->{minfree}*1024*1024 : $space;
887 $rootsize = int($os_size - $minfree - $swap_size); # in KB
888 $rootsize &= ~0xFFF; # align down to 4 MB boundaries
889 }
890
891 if ($swap_size) {
892 syscmd("/sbin/lvcreate -Wy --yes -L${swap_size}K -nswap $vgname") == 0 ||
893 die "unable to create swap volume\n";
894
895 $swapfile = "/dev/$vgname/swap";
896 }
897
898 syscmd("/sbin/lvcreate -Wy --yes -L${rootsize}K -nroot $vgname") == 0 ||
899 die "unable to create root volume\n";
900
901 if ($datasize > 4 * 1024 * 1024) {
902 my $metadatasize = $datasize/100; # default 1% of data
903 $metadatasize = 1024*1024 if $metadatasize < 1024*1024; # but at least 1G
904 $metadatasize = 16*1024*1024 if $metadatasize > 16*1024*1024; # but at most 16G
905
906 # otherwise the metadata is taken out of $minfree
907 $datasize -= 2 * $metadatasize;
908
909 # 1 4MB PE to allow for rounding
910 $datasize -= 4 * 1024;
911
912 syscmd("/sbin/lvcreate -Wy --yes -L${datasize}K -ndata $vgname") == 0 ||
913 die "unable to create data volume\n";
914
915 syscmd("/sbin/lvconvert --yes --type thin-pool --poolmetadatasize ${metadatasize}K $vgname/data") == 0 ||
916 die "unable to create data thin-pool\n";
917 } else {
918 if ($setup->{product} eq 'pve' && !defined($config_options->{maxvz})) {
919 display_message("Skipping auto-creation of LVM thinpool for guest data due to low space.");
920 }
921 $datadev = undef;
922 }
923
924 syscmd("/sbin/vgchange -a y $vgname") == 0 ||
925 die "unable to activate volume group\n";
926
927 return ($rootdev, $swapfile, $datadev);
928 }
929
930 sub compute_swapsize {
931 my ($hdsize) = @_;
932
933 my $hdgb = int($hdsize/(1024*1024));
934
935 my $swapsize_kb;
936 if (defined($config_options->{swapsize})) {
937 $swapsize_kb = $config_options->{swapsize} * 1024 * 1024;
938 } else {
939 my $ss = int($total_memory);
940 $ss = 4096 if $ss < 4096 && $hdgb >= 64;
941 $ss = 2048 if $ss < 2048 && $hdgb >= 32;
942 $ss = 1024 if $ss >= 2048 && $hdgb <= 16;
943 $ss = 512 if $ss < 512;
944 $ss = int($hdgb * 128) if $ss > $hdgb * 128;
945 $ss = 8192 if $ss > 8192;
946 $swapsize_kb = int($ss * 1024) & ~0xFFF; # align to 4 MB to avoid all to odd SWAP size
947 }
948
949 return $swapsize_kb;
950 }
951
952 my sub chroot_chown {
953 my ($root, $path, %param) = @_;
954
955 my $recursive = $param{recursive} ? ' -R' : '';
956 my $user = $param{user};
957 die "can not chown without user parameter\n" if !defined($user);
958 my $group = $param{group} // $user;
959
960 syscmd("chroot $root /bin/chown $user:$group $recursive $path") == 0 ||
961 die "chroot: unable to change owner for '$path'\n";
962 }
963
964 my sub chroot_chmod {
965 my ($root, $path, %param) = @_;
966
967 my $recursive = $param{recursive} ? ' -R' : '';
968 my $mode = $param{mode};
969 die "can not chmod without mode parameter\n" if !defined($mode);
970
971 syscmd("chroot $root /bin/chmod $mode $recursive $path") == 0 ||
972 die "chroot: unable to change permission mode for '$path'\n";
973 }
974
975 sub prepare_proxmox_boot_esp {
976 my ($espdev, $targetdir) = @_;
977
978 syscmd("chroot $targetdir proxmox-boot-tool init $espdev") == 0 ||
979 die "unable to init ESP and install proxmox-boot loader on '$espdev'\n";
980 }
981
982 sub prepare_grub_efi_boot_esp {
983 my ($dev, $espdev, $targetdir) = @_;
984
985 syscmd("mount -n $espdev -t vfat $targetdir/boot/efi") == 0 ||
986 die "unable to mount $espdev\n";
987
988 eval {
989 my $rc = syscmd("chroot $targetdir /usr/sbin/grub-install --target x86_64-efi --no-floppy --bootloader-id='proxmox' $dev");
990 if ($rc != 0) {
991 if ($boot_type eq 'efi') {
992 die "unable to install the EFI boot loader on '$dev'\n";
993 } else {
994 warn "unable to install the EFI boot loader on '$dev', ignoring (not booted using UEFI)\n";
995 }
996 }
997 # also install fallback boot file (OVMF does not boot without)
998 mkdir("$targetdir/boot/efi/EFI/BOOT");
999 syscmd("cp $targetdir/boot/efi/EFI/proxmox/grubx64.efi $targetdir/boot/efi/EFI/BOOT/BOOTx64.EFI") == 0 ||
1000 die "unable to copy efi boot loader\n";
1001 };
1002 my $err = $@;
1003
1004 eval {
1005 syscmd("umount $targetdir/boot/efi") == 0 ||
1006 die "unable to umount $targetdir/boot/efi\n";
1007 };
1008 warn $@ if $@;
1009
1010 die "failed to prepare EFI boot using Grub on '$espdev': $err" if $err;
1011 }
1012
1013 sub extract_data {
1014 my ($basefile, $targetdir) = @_;
1015
1016 die "target '$targetdir' does not exist\n" if ! -d $targetdir;
1017
1018 my $starttime = [Time::HiRes::gettimeofday];
1019
1020 my $bootdevinfo = [];
1021
1022 my $swapfile;
1023 my $rootdev;
1024 my $datadev;
1025
1026 my $use_zfs = 0;
1027 my $use_btrfs = 0;
1028
1029 my $filesys = $config_options->{filesys};
1030
1031 if ($filesys =~ m/zfs/) {
1032 $target_hd = undef; # do not use this config
1033 $use_zfs = 1;
1034 $targetdir = "/$zfspoolname/ROOT/$zfsrootvolname";
1035 } elsif ($filesys =~ m/btrfs/) {
1036 $target_hd = undef; # do not use this config
1037 $use_btrfs = 1;
1038 }
1039
1040 if ($use_zfs) {
1041 my $i;
1042 for ($i = 5; $i > 0; $i--) {
1043 syscmd("modprobe zfs");
1044 last if -c "/dev/zfs";
1045 sleep(1);
1046 }
1047
1048 die "unable to load zfs kernel module\n" if !$i;
1049 }
1050
1051 my $bootloader_err;
1052
1053 eval {
1054 my $maxper = 0.25;
1055
1056 update_progress(0, 0, $maxper, "cleanup root-disks");
1057
1058 syscmd("vgchange -an") if !is_test_mode(); # deactivate all detected VGs
1059
1060 if (is_test_mode()) {
1061
1062 $rootdev = abs_path($test_image);
1063 syscmd("umount $rootdev");
1064
1065 if ($use_btrfs) {
1066
1067 die "unsupported btrfs mode (for testing environment)\n"
1068 if $filesys ne 'btrfs (RAID0)';
1069
1070 btrfs_create([$rootdev], 'single');
1071
1072 } elsif ($use_zfs) {
1073
1074 die "unsupported zfs mode (for testing environment)\n"
1075 if $filesys ne 'zfs (RAID0)';
1076
1077 syscmd("zpool destroy $zfstestpool");
1078
1079 zfs_create_rpool($rootdev);
1080
1081 } else {
1082
1083 # nothing to do
1084 }
1085
1086 } elsif ($use_btrfs) {
1087
1088 my ($devlist, $btrfs_mode) = get_btrfs_raid_setup();
1089
1090 foreach my $hd (@$devlist) {
1091 $clean_disk->(@$hd[1]);
1092 }
1093
1094 update_progress(0, 0.02, $maxper, "create partitions");
1095
1096 my $btrfs_partitions = [];
1097 foreach my $hd (@$devlist) {
1098 my $devname = @$hd[1];
1099 my $logical_bsize = @$hd[4];
1100
1101 my ($size, $osdev, $efidev) =
1102 partition_bootable_disk($devname, $config_options->{hdsize}, '8300');
1103 $rootdev = $osdev if !defined($rootdev); # simply point to first disk
1104 my $by_id = find_stable_path("/dev/disk/by-id", $devname);
1105 push @$bootdevinfo, {
1106 esp => $efidev,
1107 devname => $devname,
1108 osdev => $osdev,
1109 by_id => $by_id,
1110 logical_bsize => $logical_bsize,
1111 };
1112 push @$btrfs_partitions, $osdev;
1113 }
1114
1115 $udevadm_trigger_block->();
1116
1117 update_progress(0, 0.03, $maxper, "create btrfs");
1118
1119 btrfs_create($btrfs_partitions, $btrfs_mode);
1120
1121 } elsif ($use_zfs) {
1122
1123 my ($devlist, $vdev) = get_zfs_raid_setup();
1124
1125 foreach my $hd (@$devlist) {
1126 $clean_disk->(@$hd[1]);
1127 }
1128
1129 update_progress(0, 0.02, $maxper, "create partitions");
1130
1131 # install esp/boot part on all, we can only win!
1132 for my $hd (@$devlist) {
1133 my $devname = @$hd[1];
1134 my $logical_bsize = @$hd[4];
1135
1136 my ($size, $osdev, $efidev) =
1137 partition_bootable_disk($devname, $config_options->{hdsize}, 'BF01');
1138
1139 push @$bootdevinfo, {
1140 esp => $efidev,
1141 devname => $devname,
1142 osdev => $osdev,
1143 logical_bsize => $logical_bsize,
1144 };
1145 }
1146
1147 $udevadm_trigger_block->();
1148
1149 foreach my $di (@$bootdevinfo) {
1150 my $devname = $di->{devname};
1151 $di->{by_id} = find_stable_path ("/dev/disk/by-id", $devname);
1152
1153 my $osdev = find_stable_path ("/dev/disk/by-id", $di->{osdev}) || $di->{osdev};
1154
1155 $vdev =~ s/ $devname/ $osdev/;
1156 }
1157
1158 foreach my $hd (@$devlist) {
1159 my $devname = @$hd[1];
1160 my $by_id = find_stable_path ("/dev/disk/by-id", $devname);
1161
1162 $vdev =~ s/ $devname/ $by_id/ if $by_id;
1163 }
1164
1165 update_progress(0, 0.03, $maxper, "create rpool");
1166
1167 zfs_create_rpool($vdev);
1168
1169 } else {
1170
1171 die "target '$target_hd' is not a valid block device\n" if ! -b $target_hd;
1172
1173 $clean_disk->($target_hd);
1174
1175 update_progress(0, 0.02, $maxper, "create partitions");
1176
1177 my $logical_bsize = logical_blocksize($target_hd);
1178
1179 my ($os_size, $osdev, $efidev) =
1180 partition_bootable_disk($target_hd, $config_options->{hdsize}, '8E00');
1181
1182 &$udevadm_trigger_block();
1183
1184 my $by_id = find_stable_path ("/dev/disk/by-id", $target_hd);
1185 push @$bootdevinfo, {
1186 esp => $efidev,
1187 devname => $target_hd,
1188 osdev => $osdev,
1189 by_id => $by_id,
1190 logical_bsize => $logical_bsize,
1191 };
1192
1193 update_progress(0, 0.03, $maxper, "create LVs");
1194
1195 my $swap_size = compute_swapsize($os_size);
1196 ($rootdev, $swapfile, $datadev) =
1197 create_lvm_volumes($osdev, $os_size, $swap_size);
1198
1199 # trigger udev to create /dev/disk/by-uuid
1200 &$udevadm_trigger_block(1);
1201 }
1202
1203 if ($use_zfs) {
1204 # to be fast during installation
1205 syscmd("zfs set sync=disabled $zfspoolname") == 0 ||
1206 die "unable to set zfs properties\n";
1207 }
1208
1209 update_progress(0.04, 0, $maxper, "create swap space");
1210 if ($swapfile) {
1211 syscmd("mkswap -f $swapfile") == 0 ||
1212 die "unable to create swap space\n";
1213 }
1214
1215 update_progress(0.045, 0, $maxper, "creating root filesystems");
1216
1217 foreach my $di (@$bootdevinfo) {
1218 next if !$di->{esp};
1219 # FIXME remove '-s1' once https://github.com/dosfstools/dosfstools/issues/111 is fixed
1220 my $vfat_extra_opts = ($di->{logical_bsize} == 4096) ? '-s1' : '';
1221 syscmd("mkfs.vfat $vfat_extra_opts -F32 $di->{esp}") == 0 ||
1222 die "unable to initialize EFI ESP on device $di->{esp}\n";
1223 }
1224
1225 if ($use_zfs) {
1226 # do nothing
1227 } elsif ($use_btrfs) {
1228 # do nothing
1229 } else {
1230 create_filesystem($rootdev, 'root', $filesys, 0.05, $maxper, 0, 1);
1231 }
1232
1233 update_progress(1, 0.05, $maxper, "mounting target $rootdev");
1234
1235 if ($use_zfs) {
1236 # do nothing
1237 } else {
1238 my $mount_opts = 'noatime';
1239 $mount_opts .= ',nobarrier'
1240 if $use_btrfs || $filesys =~ /^ext\d$/;
1241
1242 syscmd("mount -n $rootdev -o $mount_opts $targetdir") == 0 ||
1243 die "unable to mount $rootdev\n";
1244 }
1245
1246 mkdir "$targetdir/boot";
1247 mkdir "$targetdir/boot/efi";
1248
1249 mkdir "$targetdir/var";
1250 mkdir "$targetdir/var/lib";
1251
1252 if ($setup->{product} eq 'pve') {
1253 mkdir "$targetdir/var/lib/vz";
1254 mkdir "$targetdir/var/lib/pve";
1255
1256 if ($use_btrfs) {
1257 syscmd("btrfs subvolume create $targetdir/var/lib/pve/local-btrfs") == 0 ||
1258 die "unable to create btrfs subvolume\n";
1259 }
1260 }
1261
1262 mkdir "$targetdir/mnt";
1263 mkdir "$targetdir/mnt/hostrun";
1264 syscmd("mount --bind /run $targetdir/mnt/hostrun") == 0 ||
1265 die "unable to bindmount run on $targetdir/mnt/hostrun\n";
1266
1267 update_progress(1, 0.05, $maxper, "extracting base system");
1268
1269 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size) = stat ($basefile);
1270 $ino || die "unable to open file '$basefile' - $!\n";
1271
1272 my $files = file_read_firstline("${proxmox_cddir}/proxmox/$setup->{product}-base.cnt") ||
1273 die "unable to read base file count\n";
1274
1275 my $per = 0;
1276 my $count = 0;
1277
1278 run_command("unsquashfs -f -dest $targetdir -i $basefile", sub {
1279 my $line = shift;
1280 return if $line !~ m/^$targetdir/;
1281 $count++;
1282 my $nper = int (($count *100)/$files);
1283 if ($nper != $per) {
1284 $per = $nper;
1285 my $frac = $per > 100 ? 1 : $per/100;
1286 update_progress($frac, $maxper, 0.5);
1287 }
1288 });
1289
1290 syscmd("mount -n -t tmpfs tmpfs $targetdir/tmp") == 0 || die "unable to mount tmpfs on $targetdir/tmp\n";
1291
1292 mkdir "$targetdir/tmp/pkg";
1293 syscmd("mount -n --bind '$proxmox_pkgdir' '$targetdir/tmp/pkg'") == 0
1294 || die "unable to bind-mount packages on $targetdir/tmp/pkg\n";
1295 syscmd("mount -n -t proc proc $targetdir/proc") == 0 || die "unable to mount proc on $targetdir/proc\n";
1296 syscmd("mount -n -t sysfs sysfs $targetdir/sys") == 0 || die "unable to mount sysfs on $targetdir/sys\n";
1297 if ($boot_type eq 'efi') {
1298 syscmd("mount -n -t efivarfs efivarfs $targetdir/sys/firmware/efi/efivars") == 0 ||
1299 die "unable to mount efivarfs on $targetdir/sys/firmware/efi/efivars: $!\n";
1300 }
1301 syscmd("chroot $targetdir mount --bind /mnt/hostrun /run") == 0 ||
1302 die "unable to re-bindmount hostrun on /run in chroot\n";
1303
1304 update_progress(1, $maxper, 0.5, "configuring base system");
1305
1306 # configure hosts
1307
1308 my $hosts =
1309 "127.0.0.1 localhost.localdomain localhost\n" .
1310 "$ipaddress $hostname.$domain $hostname\n\n" .
1311 "# The following lines are desirable for IPv6 capable hosts\n\n" .
1312 "::1 ip6-localhost ip6-loopback\n" .
1313 "fe00::0 ip6-localnet\n" .
1314 "ff00::0 ip6-mcastprefix\n" .
1315 "ff02::1 ip6-allnodes\n" .
1316 "ff02::2 ip6-allrouters\n" .
1317 "ff02::3 ip6-allhosts\n";
1318
1319 file_write_all("$targetdir/etc/hosts", $hosts);
1320
1321 file_write_all("$targetdir/etc/hostname", "$hostname\n");
1322
1323 syscmd("/bin/hostname $hostname") if !is_test_mode();
1324
1325 # configure interfaces
1326
1327 my $ifaces = "auto lo\niface lo inet loopback\n\n";
1328
1329 my $ntype = $ipversion == 4 ? 'inet' : 'inet6';
1330
1331 my $ethdev = $ipconf->{ifaces}->{$ipconf->{selected}}->{name};
1332
1333 if ($setup->{bridged_network}) {
1334 $ifaces .= "iface $ethdev $ntype manual\n";
1335
1336 $ifaces .=
1337 "\nauto vmbr0\niface vmbr0 $ntype static\n" .
1338 "\taddress $cidr\n" .
1339 "\tgateway $gateway\n" .
1340 "\tbridge-ports $ethdev\n" .
1341 "\tbridge-stp off\n" .
1342 "\tbridge-fd 0\n";
1343 } else {
1344 $ifaces .= "auto $ethdev\n" .
1345 "iface $ethdev $ntype static\n" .
1346 "\taddress $cidr\n" .
1347 "\tgateway $gateway\n";
1348 }
1349
1350 foreach my $iface (sort keys %{$ipconf->{ifaces}}) {
1351 my $name = $ipconf->{ifaces}->{$iface}->{name};
1352 next if $name eq $ethdev;
1353
1354 $ifaces .= "\niface $name $ntype manual\n";
1355 }
1356
1357 file_write_all("$targetdir/etc/network/interfaces", $ifaces);
1358
1359 # configure dns
1360
1361 my $resolvconf = "search $domain\nnameserver $dnsserver\n";
1362 file_write_all("$targetdir/etc/resolv.conf", $resolvconf);
1363
1364 # configure fstab
1365
1366 my $fstab = "# <file system> <mount point> <type> <options> <dump> <pass>\n";
1367
1368 if ($use_zfs) {
1369 # do nothing
1370 } elsif ($use_btrfs) {
1371 my $fsuuid;
1372 my $cmd = "blkid -u filesystem -t TYPE=btrfs -o export $rootdev";
1373 run_command($cmd, sub {
1374 my $line = shift;
1375
1376 if ($line =~ m/^UUID=([A-Fa-f0-9\-]+)$/) {
1377 $fsuuid = $1;
1378 }
1379 });
1380
1381 die "unable to detect FS UUID" if !defined($fsuuid);
1382
1383 $fstab .= "UUID=$fsuuid / btrfs defaults 0 1\n";
1384 } else {
1385 my $root_mountopt = $fssetup->{$filesys}->{root_mountopt} || 'defaults';
1386 $fstab .= "$rootdev / $filesys ${root_mountopt} 0 1\n";
1387 }
1388
1389 # mount /boot/efi
1390 # Note: this is required by current grub, but really dangerous, because
1391 # vfat does not have journaling, so it triggers manual fsck after each crash
1392 # so we only mount /boot/efi if really required (efi systems).
1393 if ($boot_type eq 'efi' && !$use_zfs) {
1394 if (scalar(@$bootdevinfo)) {
1395 my $di = @$bootdevinfo[0]; # simply use first disk
1396
1397 if ($di->{esp}) {
1398 my $efi_boot_uuid = $di->{esp};
1399 if (my $uuid = find_dev_by_uuid ($di->{esp})) {
1400 $efi_boot_uuid = "UUID=$uuid";
1401 }
1402
1403 $fstab .= "${efi_boot_uuid} /boot/efi vfat defaults 0 1\n";
1404 }
1405 }
1406 }
1407
1408
1409 $fstab .= "$swapfile none swap sw 0 0\n" if $swapfile;
1410
1411 $fstab .= "proc /proc proc defaults 0 0\n";
1412
1413 file_write_all("$targetdir/etc/fstab", $fstab);
1414 file_write_all("$targetdir/etc/mtab", "");
1415
1416 syscmd("cp ${proxmox_libdir}/policy-disable-rc.d " .
1417 "$targetdir/usr/sbin/policy-rc.d") == 0 ||
1418 die "unable to copy policy-rc.d\n";
1419 syscmd("cp ${proxmox_libdir}/fake-start-stop-daemon " .
1420 "$targetdir/sbin/") == 0 ||
1421 die "unable to copy start-stop-daemon\n";
1422
1423 diversion_add($targetdir, "/sbin/start-stop-daemon", "/sbin/fake-start-stop-daemon");
1424 diversion_add($targetdir, "/usr/sbin/update-grub", "/bin/true");
1425 diversion_add($targetdir, "/usr/sbin/update-initramfs", "/bin/true");
1426
1427 my $machine_id = run_command("systemd-id128 new");
1428 die "unable to create a new machine-id\n" if ! $machine_id;
1429 file_write_all("$targetdir/etc/machine-id", $machine_id);
1430
1431 syscmd("cp /etc/hostid $targetdir/etc/") == 0 ||
1432 die "unable to copy hostid\n";
1433
1434 syscmd("touch $targetdir/proxmox_install_mode");
1435
1436 my $grub_install_devices_txt = '';
1437 foreach my $di (@$bootdevinfo) {
1438 $grub_install_devices_txt .= ', ' if $grub_install_devices_txt;
1439 $grub_install_devices_txt .= $di->{by_id} || $di->{devname};
1440 }
1441
1442 # Note: keyboard-configuration/xbkb-keymap is used by console-setup
1443 my $xkmap = $cmap->{kmap}->{$keymap}->{x11} // 'us';
1444
1445 debconfig_set ($targetdir, <<_EOD);
1446 locales locales/default_environment_locale select en_US.UTF-8
1447 locales locales/locales_to_be_generated select en_US.UTF-8 UTF-8
1448 samba-common samba-common/dhcp boolean false
1449 samba-common samba-common/workgroup string WORKGROUP
1450 postfix postfix/main_mailer_type select No configuration
1451 keyboard-configuration keyboard-configuration/xkb-keymap select $xkmap
1452 d-i debian-installer/locale select en_US.UTF-8
1453 grub-pc grub-pc/install_devices select $grub_install_devices_txt
1454 _EOD
1455
1456 my $pkg_count = 0;
1457 while (<${proxmox_pkgdir}/*.deb>) { $pkg_count++ };
1458
1459 # btrfs/dpkg is extremely slow without --force-unsafe-io
1460 my $dpkg_opts = $use_btrfs ? "--force-unsafe-io" : "";
1461
1462 $count = 0;
1463 while (<${proxmox_pkgdir}/*.deb>) {
1464 chomp;
1465 my $path = $_;
1466 my ($deb) = $path =~ m/${proxmox_pkgdir}\/(.*\.deb)/;
1467 update_progress($count/$pkg_count, 0.5, 0.75, "extracting $deb");
1468 print "extracting: $deb\n";
1469 syscmd("chroot $targetdir dpkg $dpkg_opts --force-depends --no-triggers --unpack /tmp/pkg/$deb") == 0
1470 || die "installation of package $deb failed\n";
1471 update_progress((++$count)/$pkg_count, 0.5, 0.75);
1472 }
1473
1474 # needed for postfix postinst in case no other NIC is active
1475 syscmd("chroot $targetdir ifup lo");
1476
1477 my $cmd = "chroot $targetdir dpkg $dpkg_opts --force-confold --configure -a";
1478 $count = 0;
1479 run_command($cmd, sub {
1480 my $line = shift;
1481 if ($line =~ m/Setting up\s+(\S+)/) {
1482 update_progress((++$count)/$pkg_count, 0.75, 0.95, "configuring $1");
1483 }
1484 });
1485
1486 unlink "$targetdir/etc/mailname";
1487 $postfix_main_cf =~ s/__FQDN__/${hostname}.${domain}/;
1488 file_write_all("$targetdir/etc/postfix/main.cf", $postfix_main_cf);
1489
1490 # make sure we have all postfix directories
1491 syscmd("chroot $targetdir /usr/sbin/postfix check");
1492 # cleanup mail queue
1493 syscmd("chroot $targetdir /usr/sbin/postsuper -d ALL");
1494 # create /etc/aliases.db (/etc/aliases is shipped in the base squashfs)
1495 syscmd("chroot $targetdir /usr/bin/newaliases");
1496
1497 unlink "$targetdir/proxmox_install_mode";
1498
1499 # set timezone
1500 unlink ("$targetdir/etc/localtime");
1501 symlink ("/usr/share/zoneinfo/$timezone", "$targetdir/etc/localtime");
1502 file_write_all("$targetdir/etc/timezone", "$timezone\n");
1503
1504 # set apt mirror
1505 if (my $mirror = $cmap->{country}->{$country}->{mirror}) {
1506 my $fn = "$targetdir/etc/apt/sources.list";
1507 syscmd("sed -i 's/ftp\\.debian\\.org/$mirror/' '$fn'");
1508 }
1509
1510 # create extended_states for apt (avoid cron job warning if that
1511 # file does not exist)
1512 file_write_all("$targetdir/var/lib/apt/extended_states", '');
1513
1514 # allow ssh root login
1515 syscmd(['sed', '-i', 's/^#\?PermitRootLogin.*/PermitRootLogin yes/', "$targetdir/etc/ssh/sshd_config"]);
1516
1517 if ($setup->{product} eq 'pmg') {
1518 # install initial clamav DB
1519 my $srcdir = "${proxmox_cddir}/proxmox/clamav";
1520 foreach my $fn ("main.cvd", "bytecode.cvd", "daily.cvd", "safebrowsing.cvd") {
1521 syscmd("cp \"$srcdir/$fn\" \"$targetdir/var/lib/clamav\"") == 0 ||
1522 die "installation of clamav db file '$fn' failed\n";
1523 }
1524 syscmd("chroot $targetdir /bin/chown clamav:clamav -R /var/lib/clamav") == 0 ||
1525 die "unable to set owner for clamav database files\n";
1526 }
1527
1528 if ($setup->{product} eq 'pve') {
1529 # save installer settings
1530 my $ucc = uc ($country);
1531 debconfig_set($targetdir, "pve-manager pve-manager/country string $ucc\n");
1532 }
1533
1534 update_progress(0.8, 0.95, 1, "make system bootable");
1535
1536 if ($use_zfs) {
1537 # add ZFS options while preserving existing kernel cmdline
1538 my $zfs_snippet = "GRUB_CMDLINE_LINUX=\"\$GRUB_CMDLINE_LINUX root=ZFS=$zfspoolname/ROOT/$zfsrootvolname boot=zfs\"";
1539 file_write_all("$targetdir/etc/default/grub.d/zfs.cfg", $zfs_snippet);
1540
1541 file_write_all("$targetdir/etc/kernel/cmdline", "root=ZFS=$zfspoolname/ROOT/$zfsrootvolname boot=zfs\n");
1542
1543 }
1544
1545 diversion_remove($targetdir, "/usr/sbin/update-grub");
1546 diversion_remove($targetdir, "/usr/sbin/update-initramfs");
1547
1548 my $kapi;
1549 foreach my $fn (<$targetdir/lib/modules/*>) {
1550 if ($fn =~ m!/(\d+\.\d+\.\d+-\d+-pve)$!) {
1551 die "found multiple kernels\n" if defined($kapi);
1552 $kapi = $1;
1553 }
1554 }
1555 die "unable to detect kernel version\n" if !defined($kapi);
1556
1557 if (!is_test_mode()) {
1558
1559 unlink ("$targetdir/etc/mtab");
1560 symlink ("/proc/mounts", "$targetdir/etc/mtab");
1561 syscmd("mount -n --bind /dev $targetdir/dev");
1562
1563 my $bootloader_err_list = [];
1564 eval {
1565 syscmd("chroot $targetdir /usr/sbin/update-initramfs -c -k $kapi") == 0 ||
1566 die "unable to install initramfs\n";
1567
1568 my $native_4k_disk_bootable = 0;
1569 foreach my $di (@$bootdevinfo) {
1570 $native_4k_disk_bootable |= ($di->{logical_bsize} == 4096);
1571 }
1572
1573 foreach my $di (@$bootdevinfo) {
1574 my $dev = $di->{devname};
1575 if ($use_zfs) {
1576 prepare_proxmox_boot_esp($di->{esp}, $targetdir);
1577 } else {
1578 if (!$native_4k_disk_bootable) {
1579 eval {
1580 syscmd("chroot $targetdir /usr/sbin/grub-install --target i386-pc --no-floppy --bootloader-id='proxmox' $dev") == 0 ||
1581 die "unable to install the i386-pc boot loader on '$dev'\n";
1582 };
1583 push @$bootloader_err_list, $@ if $@;
1584 }
1585
1586 eval {
1587 if (my $esp = $di->{esp}) {
1588 prepare_grub_efi_boot_esp($dev, $esp, $targetdir);
1589 }
1590 }
1591 };
1592 push @$bootloader_err_list, $@ if $@;
1593 }
1594
1595 syscmd("chroot $targetdir /usr/sbin/update-grub") == 0 ||
1596 die "unable to update boot loader config\n";
1597 };
1598 push @$bootloader_err_list, $@ if $@;
1599
1600 if (scalar(@$bootloader_err_list) > 0) {
1601 $bootloader_err = "bootloader setup errors:\n";
1602 map { $bootloader_err .= "- $_" } @$bootloader_err_list;
1603 warn $bootloader_err;
1604 }
1605
1606 syscmd("umount $targetdir/dev");
1607 }
1608
1609 # cleanup
1610
1611 unlink "$targetdir/usr/sbin/policy-rc.d";
1612
1613 diversion_remove($targetdir, "/sbin/start-stop-daemon");
1614
1615 # set root password
1616 my $octets = encode("utf-8", $password);
1617 run_command("chroot $targetdir /usr/sbin/chpasswd", undef,
1618 "root:$octets\n");
1619
1620 if ($setup->{product} eq 'pmg') {
1621 # save admin email
1622 file_write_all("$targetdir/etc/pmg/pmg.conf", "section: admin\n\temail ${mailto}\n");
1623
1624 } elsif ($setup->{product} eq 'pve') {
1625
1626 # create pmxcfs DB
1627
1628 my $tmpdir = "$targetdir/tmp/pve";
1629 mkdir $tmpdir;
1630
1631 # write vnc keymap to datacenter.cfg
1632 my $vnckmap = $cmap->{kmap}->{$keymap}->{kvm} || 'en-us';
1633 file_write_all("$tmpdir/datacenter.cfg", "keyboard: $vnckmap\n");
1634
1635 # save admin email
1636 file_write_all("$tmpdir/user.cfg", "user:root\@pam:1:0:::${mailto}::\n");
1637
1638 # write storage.cfg
1639 my $storage_cfg;
1640 if ($use_zfs) {
1641 $storage_cfg = $storage_cfg_zfs;
1642 } elsif ($use_btrfs) {
1643 $storage_cfg = $storage_cfg_btrfs;
1644 } elsif ($datadev) {
1645 $storage_cfg = $storage_cfg_lvmthin;
1646 } else {
1647 $storage_cfg = $storage_cfg_local;
1648 }
1649 file_write_all("$tmpdir/storage.cfg", $storage_cfg);
1650
1651 run_command("chroot $targetdir /usr/bin/create_pmxcfs_db /tmp/pve /var/lib/pve-cluster/config.db");
1652
1653 syscmd("rm -rf $tmpdir");
1654 } elsif ($setup->{product} eq 'pbs') {
1655 my $base_cfg_path = "/etc/proxmox-backup";
1656 mkdir "$targetdir/$base_cfg_path";
1657
1658 chroot_chown($targetdir, $base_cfg_path, user => 'backup', recursive => 1);
1659 chroot_chmod($targetdir, $base_cfg_path, mode => '0700');
1660
1661 my $user_cfg_fn = "$base_cfg_path/user.cfg";
1662 file_write_all("$targetdir/$user_cfg_fn", "user: root\@pam\n\temail ${mailto}\n");
1663 chroot_chown($targetdir, $user_cfg_fn, user => 'root', group => 'backup');
1664 chroot_chmod($targetdir, $user_cfg_fn, mode => '0640');
1665 }
1666 };
1667
1668 my $err = $@;
1669
1670 update_progress(1, 0, 1, "");
1671
1672 print $err if $err;
1673
1674 if (is_test_mode()) {
1675 my $elapsed = Time::HiRes::tv_interval($starttime);
1676 print "Elapsed extract time: $elapsed\n";
1677
1678 syscmd("chroot $targetdir /usr/bin/dpkg-query -W --showformat='\${package}\n'> final.pkglist");
1679 }
1680
1681 syscmd("umount $targetdir/run");
1682 syscmd("umount $targetdir/mnt/hostrun");
1683 syscmd("umount $targetdir/tmp/pkg");
1684 syscmd("umount $targetdir/tmp");
1685 syscmd("umount $targetdir/proc");
1686 syscmd("umount $targetdir/sys/firmware/efi/efivars");
1687 syscmd("umount $targetdir/sys");
1688 rmdir("$targetdir/mnt/hostrun");
1689
1690 if ($use_zfs) {
1691 syscmd("zfs umount -a") == 0 ||
1692 die "unable to unmount zfs\n";
1693 } else {
1694 syscmd("umount -d $targetdir");
1695 }
1696
1697 if (!$err && $use_zfs) {
1698 syscmd("zfs set sync=standard $zfspoolname") == 0 ||
1699 die "unable to set zfs properties\n";
1700
1701 syscmd("zfs set mountpoint=/ $zfspoolname/ROOT/$zfsrootvolname") == 0 ||
1702 die "zfs set mountpoint failed\n";
1703
1704 syscmd("zpool set bootfs=$zfspoolname/ROOT/$zfsrootvolname $zfspoolname") == 0 ||
1705 die "zpool set bootfs failed\n";
1706 syscmd("zpool export $zfspoolname");
1707 }
1708
1709 if ($bootloader_err) {
1710 $err = $err ? "$err\n$bootloader_err" : $bootloader_err;
1711 }
1712
1713 die $err if $err;
1714 }
1715
1716 my $last_display_change = 0;
1717
1718 my $display_info_counter = 0;
1719
1720 my $display_info_items = [
1721 "extract1-license.htm",
1722 "extract2-rulesystem.htm",
1723 "extract3-spam.htm",
1724 "extract4-virus.htm",
1725 ];
1726
1727 sub display_info {
1728
1729 my $min_display_time = 15;
1730
1731 my $ctime = time();
1732
1733 return if ($ctime - $last_display_change) < $min_display_time;
1734
1735 my $page = $display_info_items->[$display_info_counter % scalar(@$display_info_items)];
1736
1737 $display_info_counter++;
1738
1739 display_html($page);
1740 }
1741
1742 sub display_html {
1743 my ($filename) = @_;
1744
1745 $filename = $steps[$step_number]->{html} if !$filename;
1746
1747 my $htmldir = "${proxmox_libdir}/html";
1748 my $path;
1749 if (-f "$htmldir/$setup->{product}/$filename") {
1750 $path = "$htmldir/$setup->{product}/$filename";
1751 } else {
1752 $path = "$htmldir/$filename";
1753 }
1754
1755 my $data = file_read_all($path);
1756
1757 if ($filename eq 'license.htm') {
1758 my $license = eval { decode('utf8', file_read_all("${proxmox_cddir}/EULA")) };
1759 if (my $err = $@) {
1760 die $err if !is_test_mode();
1761 $license = "TESTMODE: Ignore non existent EULA...\n";
1762 }
1763 my $title = "END USER LICENSE AGREEMENT (EULA)";
1764 $data =~ s/__LICENSE__/$license/;
1765 $data =~ s/__LICENSE_TITLE__/$title/;
1766 } elsif ($filename eq 'success.htm') {
1767 my $addr = $ipversion == 6 ? "[${ipaddress}]" : "$ipaddress";
1768 $data =~ s/__IPADDR__/$addr/g;
1769 $data =~ s/__PORT__/$setup->{port}/g;
1770
1771 my $autoreboot_msg = $config_options->{autoreboot}
1772 ? "Automatic reboot scheduled in $autoreboot_seconds seconds."
1773 : '';
1774 $data =~ s/__AUTOREBOOT_MSG__/$autoreboot_msg/;
1775 }
1776 $data =~ s/__FULL_PRODUCT_NAME__/$setup->{fullname}/g;
1777
1778 # always set base-path to common path, all resources are accesible from there.
1779 $htmlview->load_html($data, "file://$htmldir/");
1780
1781 $last_display_change = time();
1782 }
1783
1784 sub prev_function {
1785
1786 my ($text, $fctn) = @_;
1787
1788 $fctn = $step_number if !$fctn;
1789 $text = "_Previous" if !$text;
1790 $prev_btn->set_label ($text);
1791
1792 $step_number--;
1793 $steps[$step_number]->{function}();
1794
1795 $prev_btn->grab_focus();
1796 }
1797
1798 sub set_next {
1799 my ($text, $fctn) = @_;
1800
1801 $next_fctn = $fctn;
1802 my $step = $steps[$step_number];
1803 $text //= $steps[$step_number]->{next_button} // '_Next';
1804 $next->set_label($text);
1805
1806 $next->grab_focus();
1807 }
1808
1809 sub create_main_window {
1810
1811 $window = Gtk3::Window->new();
1812 $window->set_default_size(1024, 768);
1813 $window->set_has_resize_grip(0);
1814 $window->fullscreen() if !is_test_mode();
1815 $window->set_decorated(0) if !is_test_mode();
1816
1817 my $vbox = Gtk3::VBox->new(0, 0);
1818
1819 my $logofn = "$setup->{product}-banner.png";
1820 my $image = Gtk3::Image->new_from_file("${proxmox_libdir}/$logofn");
1821
1822 my $provider = Gtk3::CssProvider->new();
1823 my $theming = "* {\nbackground: #171717;\n}";
1824 $provider->load_from_data ([map ord, split //, $theming]);
1825 my $context = $image->get_style_context();
1826 $context->add_provider($provider, 600);
1827
1828 $vbox->pack_start($image, 0, 0, 0);
1829
1830 my $hbox = Gtk3::HBox->new(0, 0);
1831 $vbox->pack_start($hbox, 1, 1, 0);
1832
1833 # my $f1 = Gtk3::Frame->new ('test');
1834 # $f1->set_shadow_type ('none');
1835 # $hbox->pack_start ($f1, 1, 1, 0);
1836
1837 my $sep1 = Gtk3::HSeparator->new();
1838 $vbox->pack_start($sep1, 0, 0, 0);
1839
1840 $cmdbox = Gtk3::HBox->new();
1841 $vbox->pack_start($cmdbox, 0, 0, 10);
1842
1843 $next = Gtk3::Button->new('_Next');
1844 $next->signal_connect(clicked => sub { $last_display_change = 0; &$next_fctn (); });
1845 $cmdbox->pack_end($next, 0, 0, 10);
1846
1847
1848 $prev_btn = Gtk3::Button->new('_Previous');
1849 $prev_btn->signal_connect(clicked => sub { $last_display_change = 0; &prev_function (); });
1850 $cmdbox->pack_end($prev_btn, 0, 0, 10);
1851
1852
1853 my $abort = Gtk3::Button->new('_Abort');
1854 $abort->set_can_focus(0);
1855 $cmdbox->pack_start($abort, 0, 0, 10);
1856 $abort->signal_connect(clicked => sub { exit (-1); });
1857
1858 my $vbox2 = Gtk3::VBox->new(0, 0);
1859 $hbox->add($vbox2);
1860
1861 $htmlview = Gtk3::WebKit2::WebView->new();
1862 my $scrolls = Gtk3::ScrolledWindow->new();
1863 $scrolls->add($htmlview);
1864
1865 my $hbox2 = Gtk3::HBox->new(0, 0);
1866 $hbox2->pack_start($scrolls, 1, 1, 0);
1867
1868 $vbox2->pack_start($hbox2, 1, 1, 0);
1869
1870 my $vbox3 = Gtk3::VBox->new(0, 0);
1871 $vbox2->pack_start($vbox3, 0, 0, 0);
1872
1873 my $sep2 = Gtk3::HSeparator->new;
1874 $vbox3->pack_start($sep2, 0, 0, 0);
1875
1876 $inbox = Gtk3::HBox->new(0, 0);
1877 $vbox3->pack_start($inbox, 0, 0, 0);
1878
1879 $window->add($vbox);
1880
1881 $window->show_all;
1882 $window->realize();
1883 }
1884
1885 sub cleanup_view {
1886 $inbox->foreach(sub {
1887 my $child = shift;
1888 $inbox->remove ($child);
1889 });
1890 }
1891
1892 # fixme: newer GTK3 has special properties to handle numbers with Entry
1893 # only allow floating point numbers with Gtk3::Entry
1894
1895 sub check_float {
1896 my ($entry, $event) = @_;
1897
1898 return check_number($entry, $event, 1);
1899 }
1900
1901 sub check_int {
1902 my ($entry, $event) = @_;
1903
1904 return check_number($entry, $event, 0);
1905 }
1906
1907 sub check_number {
1908 my ($entry, $event, $float) = @_;
1909
1910 my $val = $event->get_keyval;
1911
1912 if (($float && $val == ord '.') ||
1913 $val == Gtk3::Gdk::KEY_ISO_Left_Tab ||
1914 $val == Gtk3::Gdk::KEY_Shift_L ||
1915 $val == Gtk3::Gdk::KEY_Tab ||
1916 $val == Gtk3::Gdk::KEY_Left ||
1917 $val == Gtk3::Gdk::KEY_Right ||
1918 $val == Gtk3::Gdk::KEY_BackSpace ||
1919 $val == Gtk3::Gdk::KEY_Delete ||
1920 ($val >= ord '0' && $val <= ord '9') ||
1921 ($val >= Gtk3::Gdk::KEY_KP_0 &&
1922 $val <= Gtk3::Gdk::KEY_KP_9)) {
1923 return undef;
1924 }
1925
1926 return 1;
1927 }
1928
1929 sub create_text_input {
1930 my ($default, $text) = @_;
1931
1932 my $hbox = Gtk3::Box->new('horizontal', 0);
1933
1934 my $label = Gtk3::Label->new($text);
1935 $label->set_size_request(150, -1);
1936 $label->set_alignment(1, 0.5);
1937 $hbox->pack_start($label, 0, 0, 10);
1938 my $e1 = Gtk3::Entry->new();
1939 $e1->set_width_chars(35);
1940 $hbox->pack_start($e1, 0, 0, 0);
1941 $e1->set_text($default);
1942
1943 return ($hbox, $e1);
1944 }
1945 sub create_cidr_inputs {
1946 my ($default_ip, $default_mask) = @_;
1947
1948 my $hbox = Gtk3::Box->new('horizontal', 0);
1949
1950 my $label = Gtk3::Label->new('IP Address (CIDR)');
1951 $label->set_size_request(150, -1);
1952 $label->set_alignment(1, 0.5);
1953 $hbox->pack_start($label, 0, 0, 10);
1954
1955 my $ip_el = Gtk3::Entry->new();
1956 $ip_el->set_width_chars(28);
1957 $hbox->pack_start($ip_el, 0, 0, 0);
1958 $ip_el->set_text($default_ip);
1959
1960 $label = Gtk3::Label->new('/');
1961 $label->set_size_request(10, -1);
1962 $label->set_alignment(0.5, 0.5);
1963 $hbox->pack_start($label, 0, 0, 2);
1964
1965 my $cidr_el = Gtk3::Entry->new();
1966 $cidr_el->set_width_chars(3);
1967 $hbox->pack_start($cidr_el, 0, 0, 0);
1968 $cidr_el->set_text($default_mask);
1969
1970 return ($hbox, $ip_el, $cidr_el);
1971 }
1972
1973 sub display_message {
1974 my ($msg) = @_;
1975
1976 my $dialog = Gtk3::MessageDialog->new($window, 'modal', 'info', 'ok', $msg);
1977 $dialog->run();
1978 $dialog->destroy();
1979 }
1980
1981 sub display_error {
1982 my ($msg) = @_;
1983
1984 my $dialog = Gtk3::MessageDialog->new($window, 'modal', 'error', 'ok', $msg);
1985 $dialog->run();
1986 $dialog->destroy();
1987 }
1988
1989 sub display_prompt {
1990 my ($query) = @_;
1991
1992 my $dialog = Gtk3::MessageDialog->new($window, 'modal', 'question', 'ok-cancel', $query);
1993 my $response = $dialog->run();
1994 $dialog->destroy();
1995
1996 return $response;
1997 }
1998
1999 my $ipconf_first_view = 1;
2000
2001 sub create_ipconf_view {
2002
2003 cleanup_view();
2004 display_html();
2005
2006 my $vcontainer = Gtk3::Box->new('vertical', 0);
2007 $inbox->pack_start($vcontainer, 1, 0, 0);
2008 my $hcontainer = Gtk3::Box->new('horizontal', 0);
2009 $vcontainer->pack_start($hcontainer, 0, 0, 10);
2010 my $vbox = Gtk3::Box->new('vertical', 0);
2011 $hcontainer->add($vbox);
2012
2013 my $ipaddr_text = $config->{ipaddress} // "192.168.100.2";
2014 my $netmask_text = $config->{netmask} // "24";
2015 my $cidr_box;
2016 ($cidr_box, $ipconf_entry_addr, $ipconf_entry_mask) =
2017 create_cidr_inputs($ipaddr_text, $netmask_text);
2018
2019 my $device_cb = Gtk3::ComboBoxText->new();
2020 $device_cb->set_active(0);
2021 $device_cb->set_visible(1);
2022
2023 my $get_device_desc = sub {
2024 my $iface = shift;
2025 return "$iface->{name} - $iface->{mac} ($iface->{driver})";
2026 };
2027
2028 my $device_active_map = {};
2029 my $device_active_reverse_map = {};
2030
2031 my $device_change_handler = sub {
2032 my $current = shift;
2033
2034 my $new = $device_active_map->{$current->get_active()};
2035 return if defined($ipconf->{selected}) && $new eq $ipconf->{selected};
2036
2037 $ipconf->{selected} = $new;
2038 my $iface = $ipconf->{ifaces}->{$ipconf->{selected}};
2039 $config->{mngmt_nic} = $iface->{name};
2040 $ipconf_entry_addr->set_text($iface->{inet}->{addr} || $iface->{inet6}->{addr})
2041 if $iface->{inet}->{addr} || $iface->{inet6}->{addr};
2042 $ipconf_entry_mask->set_text($iface->{inet}->{prefix} || $iface->{inet6}->{prefix})
2043 if $iface->{inet}->{prefix} || $iface->{inet6}->{prefix};
2044 };
2045
2046 my $i = 0;
2047 foreach my $index (sort keys %{$ipconf->{ifaces}}) {
2048 $device_cb->append_text(&$get_device_desc($ipconf->{ifaces}->{$index}));
2049 $device_active_map->{$i} = $index;
2050 $device_active_reverse_map->{$ipconf->{ifaces}->{$index}->{name}} = $i;
2051 if ($ipconf_first_view && $index == $ipconf->{default}) {
2052 $device_cb->set_active($i);
2053 &$device_change_handler($device_cb);
2054 $ipconf_first_view = 0;
2055 }
2056 $device_cb->signal_connect('changed' => $device_change_handler);
2057 $i++;
2058 }
2059
2060 if (my $nic = $config->{mngmt_nic}) {
2061 $device_cb->set_active($device_active_reverse_map->{$nic} // 0);
2062 } else {
2063 $device_cb->set_active(0);
2064 }
2065
2066 my $devicebox = Gtk3::HBox->new(0, 0);
2067 my $label = Gtk3::Label->new("Management Interface:");
2068 $label->set_size_request(150, -1);
2069 $label->set_alignment(1, 0.5);
2070 $devicebox->pack_start($label, 0, 0, 10);
2071 $devicebox->pack_start($device_cb, 0, 0, 0);
2072
2073 $vbox->pack_start($devicebox, 0, 0, 2);
2074
2075 my $hn = $config->{fqdn} // "$setup->{product}." . ($ipconf->{domain} // "example.invalid");
2076
2077 my ($hostbox, $hostentry) = create_text_input($hn, 'Hostname (FQDN):');
2078 $vbox->pack_start($hostbox, 0, 0, 2);
2079
2080 $vbox->pack_start($cidr_box, 0, 0, 2);
2081
2082 $gateway = $config->{gateway} // $ipconf->{gateway} || '192.168.100.1';
2083
2084 my $gwbox;
2085 ($gwbox, $ipconf_entry_gw) =
2086 create_text_input($gateway, 'Gateway:');
2087
2088 $vbox->pack_start($gwbox, 0, 0, 2);
2089
2090 $dnsserver = $config->{dnsserver} // $ipconf->{dnsserver} || $gateway;
2091
2092 my $dnsbox;
2093 ($dnsbox, $ipconf_entry_dns) =
2094 create_text_input($dnsserver, 'DNS Server:');
2095
2096 $vbox->pack_start($dnsbox, 0, 0, 0);
2097
2098 $inbox->show_all;
2099 set_next(undef, sub {
2100
2101 # verify hostname
2102
2103 my $text = $hostentry->get_text();
2104
2105 $text =~ s/^\s+//;
2106 $text =~ s/\s+$//;
2107
2108 $config->{fqdn} = $text;
2109
2110 my $namere = "([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)";
2111
2112 # Debian does not support purely numeric hostnames
2113 if ($text && $text =~ /^[0-9]+(?:\.|$)/) {
2114 display_message("Purely numeric hostnames are not allowed.");
2115 $hostentry->grab_focus();
2116 return;
2117 }
2118
2119 if ($text && $text =~ m/^(${namere}\.)*${namere}$/ && $text !~ m/.example.invalid$/ &&
2120 $text =~ m/^([^\.]+)\.(\S+)$/) {
2121 $hostname = $1;
2122 $domain = $2;
2123 } else {
2124 display_message("Hostname does not look like a fully qualified domain name.");
2125 $hostentry->grab_focus();
2126 return;
2127 }
2128
2129 # verify ip address
2130 $text = $ipconf_entry_addr->get_text();
2131 ($ipaddress, $ipversion) = parse_ip_address($text);
2132 if (!defined($ipaddress)) {
2133 display_message("IP address is not valid.");
2134 $ipconf_entry_addr->grab_focus();
2135 return;
2136 }
2137 $config->{ipaddress} = $ipaddress;
2138
2139 $text = $ipconf_entry_mask->get_text();
2140 $netmask = parse_ip_mask($text, $ipversion);
2141 if (!defined($netmask)) {
2142 display_message("Netmask is not valid.");
2143 $ipconf_entry_mask->grab_focus();
2144 return;
2145 }
2146 $cidr = "$ipaddress/$netmask";
2147 $config->{netmask} = $netmask;
2148
2149 $text = $ipconf_entry_gw->get_text();
2150 my ($gateway_ip, $gateway_ip_version) = parse_ip_address($text);
2151 if (!defined($gateway_ip) || $gateway_ip_version != $ipversion) {
2152 my $msg = defined($gateway_ip)
2153 ? "Gateway and host IP version must not differ (IPv$gateway_ip_version != IPv$ipversion)."
2154 : "Gateway is not valid.";
2155 display_message($msg);
2156 $ipconf_entry_gw->grab_focus();
2157 return;
2158 }
2159 $config->{gateway} = $gateway = $gateway_ip;
2160
2161 $text = $ipconf_entry_dns->get_text();
2162 my ($dns_ip, $dns_ip_version) = parse_ip_address($text);
2163 if (!defined($dns_ip) || $dns_ip_version != $ipversion) {
2164 my $msg = defined($gateway_ip)
2165 ? "DNS and host IP version must not differ (IPv$gateway_ip_version != IPv$ipversion)."
2166 : "DNS IP is not valid.";
2167 display_message($msg);
2168 $ipconf_entry_dns->grab_focus();
2169 return;
2170 }
2171 $config->{dnsserver} = $dnsserver = $dns_ip;
2172
2173 #print "TEST $ipaddress $netmask $gateway $dnsserver\n";
2174
2175 $step_number++;
2176 create_ack_view();
2177 });
2178
2179 $hostentry->grab_focus();
2180 }
2181
2182 sub create_ack_view {
2183
2184 cleanup_view();
2185
2186 my $vbox = Gtk3::VBox->new(0, 0);
2187 $inbox->pack_start($vbox, 1, 0, 0);
2188
2189 my $reboot_checkbox = Gtk3::CheckButton->new('Automatically reboot after successful installation');
2190 $reboot_checkbox->set_active(1);
2191 $reboot_checkbox->signal_connect ("toggled" => sub {
2192 my $cb = shift;
2193 $config_options->{autoreboot} = $cb->get_active();
2194 });
2195 $vbox->pack_start($reboot_checkbox, 0, 0, 2);
2196
2197 my $ack_template = "${proxmox_libdir}/html/ack_template.htm";
2198 my $ack_html = "${proxmox_libdir}/html/$setup->{product}/$steps[$step_number]->{html}";
2199 my $html_data = file_read_all($ack_template);
2200
2201 my %config_values = (
2202 __target_hd__ => join(' | ', @{$config_options->{target_hds}}),
2203 __target_fs__ => $config_options->{filesys},
2204 __country__ => $cmap->{country}->{$country}->{name},
2205 __timezone__ => $timezone,
2206 __keymap__ => $keymap,
2207 __mailto__ => $mailto,
2208 __interface__ => $ipconf->{ifaces}->{$ipconf->{selected}}->{name},
2209 __hostname__ => $hostname,
2210 __ip__ => $ipaddress,
2211 __cidr__ => $cidr,
2212 __netmask__ => $netmask,
2213 __gateway__ => $gateway,
2214 __dnsserver__ => $dnsserver,
2215 );
2216
2217 while (my ($k, $v) = each %config_values) {
2218 $html_data =~ s/$k/$v/g;
2219 }
2220
2221 file_write_all($ack_html, $html_data);
2222
2223 display_html();
2224
2225 $inbox->show_all;
2226
2227 set_next(undef, sub {
2228 $step_number++;
2229 create_extract_view();
2230 });
2231 }
2232
2233 sub get_device_desc {
2234 my ($devname, $size, $model) = @_;
2235
2236 if ($size && ($size > 0)) {
2237 $size = int($size/2048); # size in MiB, from 512B "sectors"
2238
2239 my $text = "$devname (";
2240 if ($size >= 1024) {
2241 $size = $size/1024; # size in GiB
2242 if ($size >= 1024) {
2243 $size = $size/1024; # size in TiB
2244 $text .= sprintf("%.2f", $size) . "TiB";
2245 } else {
2246 $text .= sprintf("%.2f", $size) . "GiB";
2247 }
2248 } else {
2249 $text .= "${size}MiB";
2250 }
2251
2252 $text .= ", $model" if $model;
2253 $text .= ")";
2254 return $text;
2255
2256 } else {
2257 return $devname;
2258 }
2259 }
2260
2261 my $last_layout;
2262 my $country_layout;
2263 sub update_layout {
2264 my ($cb, $kmap) = @_;
2265
2266 my $ind;
2267 my $def;
2268 my $i = 0;
2269 my $kmaphash = $cmap->{kmaphash};
2270 foreach my $layout (sort keys %$kmaphash) {
2271 $def = $i if $kmaphash->{$layout} eq 'en-us';
2272 $ind = $i if $kmap && $kmaphash->{$layout} eq $kmap;
2273 $i++;
2274 }
2275
2276 my $val = $ind || $def || 0;
2277
2278 if (!defined($kmap)) {
2279 $last_layout //= $val;
2280 } elsif (!defined($country_layout) || $country_layout != $val) {
2281 $last_layout = $country_layout = $val;
2282 }
2283 $cb->set_active($last_layout);
2284 }
2285
2286 my $lastzonecb;
2287 sub update_zonelist {
2288 my ($box, $cc) = @_;
2289
2290 my $cczones = $cmap->{cczones};
2291 my $zones = $cmap->{zones};
2292
2293 my $sel;
2294 if ($lastzonecb) {
2295 $sel = $lastzonecb->get_active_text();
2296 $box->remove ($lastzonecb);
2297 } else {
2298 $sel = $timezone; # used once to select default
2299 }
2300
2301 my $cb = $lastzonecb = Gtk3::ComboBoxText->new();
2302 $cb->set_size_request(200, -1);
2303
2304 $cb->signal_connect('changed' => sub {
2305 $timezone = $cb->get_active_text();
2306 });
2307
2308 my @za;
2309 if ($cc && defined ($cczones->{$cc})) {
2310 @za = keys %{$cczones->{$cc}};
2311 } else {
2312 @za = keys %$zones;
2313 }
2314 my $ind;
2315 my $i = 0;
2316 foreach my $zone (sort @za) {
2317 $ind = $i if $sel && $zone eq $sel;
2318 $cb->append_text($zone);
2319 $i++;
2320 }
2321
2322 # Append UTC here, so it is always the last item and never the default for any country.
2323 $cb->append_text('UTC');
2324
2325 $cb->set_active($ind || 0);
2326
2327 $cb->show;
2328 $box->pack_start($cb, 0, 0, 0);
2329 }
2330
2331 sub create_password_view {
2332
2333 cleanup_view();
2334
2335 my $vbox2 = Gtk3::VBox->new(0, 0);
2336 $inbox->pack_start($vbox2, 1, 0, 0);
2337 my $vbox = Gtk3::VBox->new(0, 0);
2338 $vbox2->pack_start($vbox, 0, 0, 10);
2339
2340 my $hbox1 = Gtk3::HBox->new(0, 0);
2341 my $label = Gtk3::Label->new("Password");
2342 $label->set_size_request(150, -1);
2343 $label->set_alignment(1, 0.5);
2344 $hbox1->pack_start($label, 0, 0, 10);
2345 my $pwe1 = Gtk3::Entry->new();
2346 $pwe1->set_visibility(0);
2347 $pwe1->set_text($password) if $password;
2348 $pwe1->set_size_request(200, -1);
2349 $hbox1->pack_start($pwe1, 0, 0, 0);
2350
2351 my $hbox2 = Gtk3::HBox->new(0, 0);
2352 $label = Gtk3::Label->new("Confirm");
2353 $label->set_size_request(150, -1);
2354 $label->set_alignment(1, 0.5);
2355 $hbox2->pack_start($label, 0, 0, 10);
2356 my $pwe2 = Gtk3::Entry->new();
2357 $pwe2->set_visibility(0);
2358 $pwe2->set_text($password) if $password;
2359 $pwe2->set_size_request(200, -1);
2360 $hbox2->pack_start($pwe2, 0, 0, 0);
2361
2362 my $hbox3 = Gtk3::HBox->new(0, 0);
2363 $label = Gtk3::Label->new("Email");
2364 $label->set_size_request(150, -1);
2365 $label->set_alignment(1, 0.5);
2366 $hbox3->pack_start($label, 0, 0, 10);
2367 my $eme = Gtk3::Entry->new();
2368 $eme->set_size_request(200, -1);
2369 $eme->set_text($mailto);
2370 $hbox3->pack_start($eme, 0, 0, 0);
2371
2372
2373 $vbox->pack_start($hbox1, 0, 0, 5);
2374 $vbox->pack_start($hbox2, 0, 0, 5);
2375 $vbox->pack_start($hbox3, 0, 0, 15);
2376
2377 $inbox->show_all;
2378
2379 display_html();
2380
2381 set_next (undef, sub {
2382
2383 my $t1 = $pwe1->get_text;
2384 my $t2 = $pwe2->get_text;
2385
2386 if (length ($t1) < 5) {
2387 display_message("Password is too short.");
2388 $pwe1->grab_focus();
2389 return;
2390 }
2391
2392 if ($t1 ne $t2) {
2393 display_message("Password does not match.");
2394 $pwe1->grab_focus();
2395 return;
2396 }
2397
2398 my $t3 = $eme->get_text;
2399 if ($t3 !~ m/^[\w\+\-\~]+(\.[\w\+\-\~]+)*@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]+)*$/) {
2400 display_message("Email does not look like a valid address" .
2401 " (user\@domain.tld)");
2402 $eme->grab_focus();
2403 return;
2404 }
2405
2406 if ($t3 eq 'mail@example.invalid') {
2407 display_message("Please enter a valid Email address");
2408 $eme->grab_focus();
2409 return;
2410 }
2411
2412 $password = $t1;
2413 $mailto = $t3;
2414
2415 $step_number++;
2416 create_ipconf_view();
2417 });
2418
2419 $pwe1->grab_focus();
2420
2421 }
2422
2423 my $installer_kmap;
2424 sub create_country_view {
2425
2426 cleanup_view();
2427
2428 my $countryhash = $cmap->{countryhash};
2429 my $ctr = $cmap->{country};
2430
2431 my $vbox2 = Gtk3::VBox->new(0, 0);
2432 $inbox->pack_start($vbox2, 1, 0, 0);
2433 my $vbox = Gtk3::VBox->new(0, 0);
2434 $vbox2->pack_start($vbox, 0, 0, 10);
2435
2436 my $w = Gtk3::Entry->new();
2437 $w->set_size_request(200, -1);
2438
2439 my $c = Gtk3::EntryCompletion->new();
2440 $c->set_text_column(0);
2441 $c->set_minimum_key_length(0);
2442 $c->set_popup_set_width(1);
2443 $c->set_inline_completion(1);
2444
2445 my $hbox2 = Gtk3::HBox->new(0, 0);
2446 my $label = Gtk3::Label->new("Time zone");
2447 $label->set_size_request(150, -1);
2448 $label->set_alignment(1, 0.5);
2449 $hbox2->pack_start($label, 0, 0, 10);
2450 update_zonelist ($hbox2);
2451
2452 my $hbox3 = Gtk3::HBox->new(0, 0);
2453 $label = Gtk3::Label->new("Keyboard Layout");
2454 $label->set_size_request(150, -1);
2455 $label->set_alignment(1, 0.5);
2456 $hbox3->pack_start($label, 0, 0, 10);
2457
2458 my $kmapcb = Gtk3::ComboBoxText->new();
2459 $kmapcb->set_size_request (200, -1);
2460 foreach my $layout (sort keys %{$cmap->{kmaphash}}) {
2461 $kmapcb->append_text ($layout);
2462 }
2463
2464 update_layout($kmapcb);
2465 $hbox3->pack_start ($kmapcb, 0, 0, 0);
2466
2467 $kmapcb->signal_connect ('changed' => sub {
2468 my $sel = $kmapcb->get_active_text();
2469 $last_layout = $kmapcb->get_active();
2470 if (my $kmap = $cmap->{kmaphash}->{$sel}) {
2471 my $xkmap = $cmap->{kmap}->{$kmap}->{x11};
2472 my $xvar = $cmap->{kmap}->{$kmap}->{x11var};
2473 $keymap = $kmap;
2474
2475 return if (defined($installer_kmap) && $installer_kmap eq $kmap);
2476
2477 $installer_kmap = $keymap;
2478
2479 if (!is_test_mode()) {
2480 syscmd ("setxkbmap $xkmap $xvar");
2481
2482 my $kbd_config = qq{
2483 XKBLAYOUT="$xkmap"
2484 XKBVARIANT="$xvar"
2485 BACKSPACE="guess"
2486 };
2487 $kbd_config =~ s/^\s+//gm;
2488
2489 Proxmox::Sys::Command::run_in_background(sub {
2490 file_write_all('/etc/default/keyboard', $kbd_config);
2491 system("setupcon");
2492 });
2493 }
2494 }
2495 });
2496
2497 $w->signal_connect ('changed' => sub {
2498 my ($entry, $event) = @_;
2499 my $text = $entry->get_text;
2500
2501 if (my $cc = $countryhash->{lc($text)}) {
2502 update_zonelist($hbox2, $cc);
2503 my $kmap = $ctr->{$cc}->{kmap} || 'en-us';
2504 update_layout($kmapcb, $kmap);
2505 }
2506 });
2507
2508 $w->signal_connect (key_press_event => sub {
2509 my ($entry, $event) = @_;
2510 my $text = $entry->get_text;
2511
2512 my $val = $event->get_keyval;
2513
2514 if ($val == Gtk3::Gdk::KEY_Tab) {
2515 my $cc = $countryhash->{lc($text)};
2516
2517 my $found = 0;
2518 my $compl;
2519
2520 if ($cc) {
2521 $found = 1;
2522 $compl = $ctr->{$cc}->{name};
2523 } else {
2524 foreach my $cc (keys %$ctr) {
2525 my $ct = $ctr->{$cc}->{name};
2526 if ($ct =~ m/^\Q$text\E.*$/i) {
2527 $found++;
2528 $compl = $ct;
2529 }
2530 last if $found > 1;
2531 }
2532 }
2533
2534 if ($found == 1) {
2535 $entry->set_text($compl);
2536 $c->complete();
2537 return undef;
2538 } else {
2539 #Gtk3::Gdk::beep();
2540 print chr(7); # beep ?
2541 }
2542
2543 $c->complete();
2544
2545 my $buf = $w->get_buffer();
2546 $buf->insert_text(-1, '', -1); # popup selection
2547
2548 return 1;
2549 }
2550
2551 return undef;
2552 });
2553
2554 my $ls = Gtk3::ListStore->new('Glib::String');
2555 foreach my $cc (sort {$ctr->{$a}->{name} cmp $ctr->{$b}->{name} } keys %$ctr) {
2556 my $iter = $ls->append();
2557 $ls->set ($iter, 0, $ctr->{$cc}->{name});
2558 }
2559 $c->set_model ($ls);
2560
2561 $w->set_completion ($c);
2562
2563 my $hbox = Gtk3::HBox->new(0, 0);
2564
2565 $label = Gtk3::Label->new("Country");
2566 $label->set_alignment(1, 0.5);
2567 $label->set_size_request(150, -1);
2568 $hbox->pack_start($label, 0, 0, 10);
2569 $hbox->pack_start($w, 0, 0, 0);
2570
2571 $vbox->pack_start($hbox, 0, 0, 5);
2572 $vbox->pack_start($hbox2, 0, 0, 5);
2573 $vbox->pack_start($hbox3, 0, 0, 5);
2574
2575 if ($country && $ctr->{$country}) {
2576 $w->set_text ($ctr->{$country}->{name});
2577 }
2578
2579 $inbox->show_all;
2580
2581 display_html();
2582 set_next (undef, sub {
2583
2584 my $text = $w->get_text;
2585
2586 if (my $cc = $countryhash->{lc($text)}) {
2587 $country = $cc;
2588 $step_number++;
2589 create_password_view();
2590 return;
2591 } else {
2592 display_message("Please select a country first.");
2593 $w->grab_focus();
2594 }
2595 });
2596
2597 $w->grab_focus();
2598 }
2599
2600 my $target_hd_combo;
2601 my $target_hd_label;
2602
2603 my $hdoption_first_setup = 1;
2604
2605 my $create_basic_grid = sub {
2606 my $grid = Gtk3::Grid->new();
2607 $grid->set_visible(1);
2608 $grid->set_column_spacing(10);
2609 $grid->set_row_spacing(10);
2610 $grid->set_hexpand(1);
2611
2612 $grid->set_margin_start(10);
2613 $grid->set_margin_end(20);
2614 $grid->set_margin_top(5);
2615 $grid->set_margin_bottom(5);
2616
2617 return $grid;
2618 };
2619
2620 my $create_label_widget_grid = sub {
2621 my ($labeled_widgets) = @_;
2622
2623 my $grid = &$create_basic_grid();
2624 my $row = 0;
2625
2626 for (my $i = 0; $i < @$labeled_widgets; $i += 2) {
2627 my $widget = @$labeled_widgets[$i+1];
2628 my $label = Gtk3::Label->new(@$labeled_widgets[$i]);
2629 $label->set_visible(1);
2630 $label->set_alignment (1, 0.5);
2631 $grid->attach($label, 0, $row, 1, 1);
2632 $widget->set_visible(1);
2633 $grid->attach($widget, 1, $row, 1, 1);
2634 $row++;
2635 }
2636
2637 return $grid;
2638 };
2639
2640 # only relevant for raid with its multipl diskX to diskY mappings.
2641 my $get_selected_hdsize = sub {
2642 my $hdsize = shift;
2643 return $hdsize if defined($hdsize);
2644
2645 # compute the smallest disk size of the actually selected disks
2646 my $hd_count = scalar(@$hds);
2647 for (my $i = 0; $i < $hd_count; $i++) {
2648 my $cur_hd = $config_options->{"disksel$i"} // next;
2649 my $disksize = int(@$cur_hd[2] / (2 * 1024 * 1024.0)); # size in GB
2650 $hdsize //= $disksize;
2651 $hdsize = $disksize if $disksize < $hdsize;
2652 }
2653
2654 if (my $cfg_hdsize = $config_options->{hdsize}) {
2655 # had the dialog open previously and set an even lower size than the disk selection allows
2656 $hdsize = $cfg_hdsize if $cfg_hdsize < $hdsize;
2657 }
2658 return $hdsize // 0; # fall back to zero, e.g., if none is selected hdsize cannot be any size
2659 };
2660
2661 my sub update_hdsize_adjustment {
2662 my ($adjustment, $hdsize) = @_;
2663
2664 $hdsize = $get_selected_hdsize->($hdsize);
2665 # expect that lower = 0 and step increments = 1 still are valid
2666 $adjustment->set_upper($hdsize + 1);
2667 $adjustment->set_value($hdsize);
2668 }
2669
2670 my sub create_hdsize_adjustment {
2671 my ($hdsize) = @_;
2672 $hdsize = $get_selected_hdsize->($hdsize);
2673 # params are: initial value, lower, upper, step increment, page increment, page size
2674 return Gtk3::Adjustment->new($config_options->{hdsize} || $hdsize, 0, $hdsize+1, 1, 1, 1);
2675 }
2676
2677 my sub get_hdsize_spin_button {
2678 my $hdsize = shift;
2679
2680 my $hdsize_entry_buffer = Gtk3::EntryBuffer->new(undef, 1);
2681 my $hdsize_size_adj = create_hdsize_adjustment($hdsize);
2682
2683 my $spinbutton_hdsize = Gtk3::SpinButton->new($hdsize_size_adj, 1, 1);
2684 $spinbutton_hdsize->set_buffer($hdsize_entry_buffer);
2685 $spinbutton_hdsize->set_adjustment($hdsize_size_adj);
2686 $spinbutton_hdsize->set_tooltip_text("only use specified size (GB) of the harddisk (rest left unpartitioned)");
2687 return $spinbutton_hdsize;
2688 };
2689
2690 my $create_raid_disk_grid = sub {
2691 my ($hdsize_buttons) = @_;
2692
2693 my $hd_count = scalar(@$hds);
2694 my $disk_labeled_widgets = [];
2695 for (my $i = 0; $i < $hd_count; $i++) {
2696 my $disk_selector = Gtk3::ComboBoxText->new();
2697 $disk_selector->append_text("-- do not use --");
2698 $disk_selector->set_active(0);
2699 $disk_selector->set_visible(1);
2700
2701 foreach my $hd (@$hds) {
2702 my ($disk, $devname, $size, $model, $logical_bsize) = @$hd;
2703 $disk_selector->append_text(get_device_desc ($devname, $size, $model));
2704 }
2705
2706 $disk_selector->{pve_disk_id} = $i;
2707 $disk_selector->signal_connect(changed => sub {
2708 my $w = shift;
2709 my $diskid = $w->{pve_disk_id};
2710 my $a = $w->get_active - 1;
2711 $config_options->{"disksel${diskid}"} = ($a >= 0) ? $hds->[$a] : undef;
2712 for my $btn (@$hdsize_buttons) {
2713 update_hdsize_adjustment($btn->get_adjustment());
2714 }
2715 });
2716
2717 if ($hdoption_first_setup) {
2718 $disk_selector->set_active ($i+1) if $hds->[$i];
2719 } else {
2720 my $hdind = 0;
2721 if (my $cur_hd = $config_options->{"disksel$i"}) {
2722 foreach my $hd (@$hds) {
2723 if (@$hd[1] eq @$cur_hd[1]) {
2724 $disk_selector->set_active($hdind+1);
2725 last;
2726 }
2727 $hdind++;
2728 }
2729 }
2730 }
2731
2732 push @$disk_labeled_widgets, "Harddisk $i", $disk_selector;
2733 }
2734
2735 my $clear_all_button = Gtk3::Button->new('_Deselect All');
2736 if ($hd_count > 3) {
2737 $clear_all_button->signal_connect('clicked', sub {
2738 my $is_widget = 0;
2739 for my $disk_selector (@$disk_labeled_widgets) {
2740 $disk_selector->set_active(0) if $is_widget;
2741 $is_widget ^= 1;
2742 }
2743 });
2744 $clear_all_button->set_visible(1);
2745 }
2746
2747 my $scrolled_window = Gtk3::ScrolledWindow->new();
2748 $scrolled_window->set_hexpand(1);
2749 $scrolled_window->set_propagate_natural_height(1) if $hd_count > 4;
2750
2751 my $diskgrid = $create_label_widget_grid->($disk_labeled_widgets);
2752
2753 $scrolled_window->add($diskgrid);
2754 $scrolled_window->set_policy('never', 'automatic');
2755 $scrolled_window->set_visible(1);
2756 $scrolled_window->set_min_content_height(190);
2757
2758 my $vbox = Gtk3::Box->new('vertical', 0);
2759 $vbox->pack_start($scrolled_window, 1, 1, 10);
2760
2761 my $hbox = Gtk3::Box->new('horizontal', 0);
2762 $hbox->pack_end($clear_all_button, 0, 0, 20);
2763 $hbox->set_visible(1);
2764 $vbox->pack_end($hbox, 0, 0, 0);
2765
2766 return $vbox;
2767 };
2768
2769 my $create_raid_advanced_grid = sub {
2770 my ($hdsize_btn) = @_;
2771 my $labeled_widgets = [];
2772 my $spinbutton_ashift = Gtk3::SpinButton->new_with_range(9, 13, 1);
2773 $spinbutton_ashift->set_tooltip_text("zpool ashift property (pool sector size, default 2^12)");
2774 $spinbutton_ashift->signal_connect ("value-changed" => sub {
2775 my $w = shift;
2776 $config_options->{ashift} = $w->get_value_as_int();
2777 });
2778 $config_options->{ashift} = 12 if ! defined($config_options->{ashift});
2779 $spinbutton_ashift->set_value($config_options->{ashift});
2780 push @$labeled_widgets, "ashift";
2781 push @$labeled_widgets, $spinbutton_ashift;
2782
2783 my $combo_compress = Gtk3::ComboBoxText->new();
2784 $combo_compress->set_tooltip_text("zfs compression algorithm for rpool dataset");
2785 my $comp_opts = ["on","off","lzjb","lz4", "zle", "gzip", "zstd"];
2786 foreach my $opt (@$comp_opts) {
2787 $combo_compress->append($opt, $opt);
2788 }
2789 $config_options->{compress} = "on" if !defined($config_options->{compress});
2790 $combo_compress->set_active_id($config_options->{compress});
2791 $combo_compress->signal_connect (changed => sub {
2792 my $w = shift;
2793 $config_options->{compress} = $w->get_active_text();
2794 });
2795 push @$labeled_widgets, "compress";
2796 push @$labeled_widgets, $combo_compress;
2797
2798 my $combo_checksum = Gtk3::ComboBoxText->new();
2799 $combo_checksum->set_tooltip_text("zfs checksum algorithm for rpool dataset");
2800 my $csum_opts = ["on", "off","fletcher2", "fletcher4", "sha256"];
2801 foreach my $opt (@$csum_opts) {
2802 $combo_checksum->append($opt, $opt);
2803 }
2804 $config_options->{checksum} = "on" if !($config_options->{checksum});
2805 $combo_checksum->set_active_id($config_options->{checksum});
2806 $combo_checksum->signal_connect (changed => sub {
2807 my $w = shift;
2808 $config_options->{checksum} = $w->get_active_text();
2809 });
2810 push @$labeled_widgets, "checksum";
2811 push @$labeled_widgets, $combo_checksum;
2812
2813 my $spinbutton_copies = Gtk3::SpinButton->new_with_range(1,3,1);
2814 $spinbutton_copies->set_tooltip_text("zfs copies property for rpool dataset (in addition to RAID redundancy!)");
2815 $spinbutton_copies->signal_connect ("value-changed" => sub {
2816 my $w = shift;
2817 $config_options->{copies} = $w->get_value_as_int();
2818 });
2819 $config_options->{copies} = 1 if !defined($config_options->{copies});
2820 $spinbutton_copies->set_value($config_options->{copies});
2821 push @$labeled_widgets, "copies", $spinbutton_copies;
2822
2823 push @$labeled_widgets, "hdsize", $hdsize_btn;
2824 return $create_label_widget_grid->($labeled_widgets);;
2825 };
2826
2827 my $create_btrfs_raid_advanced_grid = sub {
2828 my ($hdsize_btn) = @_;
2829 my $labeled_widgets = [];
2830 push @$labeled_widgets, "hdsize", $hdsize_btn;
2831 return $create_label_widget_grid->($labeled_widgets);;
2832 };
2833
2834 sub create_hdoption_view {
2835 my $dialog = Gtk3::Dialog->new();
2836
2837 $dialog->set_title("Harddisk options");
2838
2839 $dialog->add_button("_OK", 1);
2840
2841 my $contarea = $dialog->get_content_area();
2842
2843 my $hbox2 = Gtk3::Box->new('horizontal', 0);
2844 $contarea->pack_start($hbox2, 1, 1, 5);
2845
2846 my $grid = Gtk3::Grid->new();
2847 $grid->set_column_spacing(10);
2848 $grid->set_row_spacing(10);
2849
2850 $hbox2->pack_start($grid, 1, 0, 5);
2851
2852 my $row = 0;
2853
2854 # Filesystem type
2855 my $label0 = Gtk3::Label->new("Filesystem");
2856 $label0->set_alignment (1, 0.5);
2857 $grid->attach($label0, 0, $row, 1, 1);
2858
2859 my $fstypecb = Gtk3::ComboBoxText->new();
2860 my $fstype = [
2861 'ext4',
2862 'xfs',
2863 'zfs (RAID0)',
2864 'zfs (RAID1)',
2865 'zfs (RAID10)',
2866 'zfs (RAIDZ-1)',
2867 'zfs (RAIDZ-2)',
2868 'zfs (RAIDZ-3)',
2869 ];
2870 push @$fstype, 'btrfs (RAID0)', 'btrfs (RAID1)', 'btrfs (RAID10)'
2871 if $setup->{enable_btrfs};
2872
2873 my $tcount = 0;
2874 foreach my $tmp (@$fstype) {
2875 $fstypecb->append_text($tmp);
2876 $fstypecb->set_active ($tcount) if $config_options->{filesys} eq $tmp;
2877 $tcount++;
2878 }
2879
2880 $grid->attach($fstypecb, 1, $row, 1, 1);
2881
2882 $hbox2->show_all();
2883
2884 $row++;
2885
2886 my $sep = Gtk3::HSeparator->new();
2887 $sep->set_visible(1);
2888 $grid->attach($sep, 0, $row, 2, 1);
2889 $row++;
2890
2891 my $hw_raid_note = Gtk3::Label->new(""); # text will be set below, before making it visible
2892 $hw_raid_note->set_line_wrap(1);
2893 $hw_raid_note->set_max_width_chars(30);
2894 $hw_raid_note->set_visible(0);
2895 $grid->attach($hw_raid_note, 0, $row++, 2, 1);
2896
2897 my $hdsize_labeled_widgets = [];
2898
2899 # size compute
2900 my $hdsize = 0;
2901 if ( -b $target_hd) {
2902 $hdsize = int(hd_size ($target_hd) / (1024 * 1024.0)); # size in GB
2903 } elsif ($target_hd) {
2904 $hdsize = int((-s $target_hd) / (1024 * 1024 * 1024.0));
2905 }
2906
2907 my $spinbutton_hdsize_nonraid = get_hdsize_spin_button($hdsize);
2908 push @$hdsize_labeled_widgets, "hdsize", $spinbutton_hdsize_nonraid;
2909 my $spinbutton_hdsize = $spinbutton_hdsize_nonraid;
2910
2911 my $entry_swapsize = Gtk3::Entry->new();
2912 $entry_swapsize->set_tooltip_text("maximum SWAP size (GB)");
2913 $entry_swapsize->signal_connect (key_press_event => \&check_float);
2914 $entry_swapsize->set_text($config_options->{swapsize}) if defined($config_options->{swapsize});
2915 push @$hdsize_labeled_widgets, "swapsize", $entry_swapsize;
2916
2917 my $entry_maxroot = Gtk3::Entry->new();
2918 if ($setup->{product} eq 'pve') {
2919 $entry_maxroot->set_tooltip_text("maximum size (GB) for LVM root volume");
2920 $entry_maxroot->signal_connect (key_press_event => \&check_float);
2921 $entry_maxroot->set_text($config_options->{maxroot}) if $config_options->{maxroot};
2922 push @$hdsize_labeled_widgets, "maxroot", $entry_maxroot;
2923 }
2924
2925 my $entry_minfree = Gtk3::Entry->new();
2926 $entry_minfree->set_tooltip_text("minimum free LVM space (GB, required for LVM snapshots)");
2927 $entry_minfree->signal_connect (key_press_event => \&check_float);
2928 $entry_minfree->set_text($config_options->{minfree}) if defined($config_options->{minfree});
2929 push @$hdsize_labeled_widgets, "minfree", $entry_minfree;
2930
2931 my $entry_maxvz;
2932 if ($setup->{product} eq 'pve') {
2933 $entry_maxvz = Gtk3::Entry->new();
2934 $entry_maxvz->set_tooltip_text("maximum size (GB) for LVM data volume");
2935 $entry_maxvz->signal_connect (key_press_event => \&check_float);
2936 $entry_maxvz->set_text($config_options->{maxvz}) if defined($config_options->{maxvz});
2937 push @$hdsize_labeled_widgets, "maxvz", $entry_maxvz;
2938 }
2939
2940 my $spinbutton_hdsize_zfs = get_hdsize_spin_button($hdsize);
2941 my $spinbutton_hdsize_btrfs = get_hdsize_spin_button($hdsize);
2942 my $hdsize_buttons = [ $spinbutton_hdsize_zfs, $spinbutton_hdsize_btrfs ];
2943 my $options_stack = Gtk3::Stack->new();
2944 $options_stack->set_visible(1);
2945 $options_stack->set_hexpand(1);
2946 $options_stack->set_vexpand(1);
2947 $options_stack->add_titled(&$create_raid_disk_grid($hdsize_buttons), "raiddisk", "Disk Setup");
2948 $options_stack->add_titled(&$create_label_widget_grid($hdsize_labeled_widgets), "hdsize", "Size Options");
2949 $options_stack->add_titled(&$create_raid_advanced_grid($spinbutton_hdsize_zfs), "raidzfsadvanced", "Advanced Options");
2950 $options_stack->add_titled(&$create_btrfs_raid_advanced_grid($spinbutton_hdsize_btrfs), "raidbtrfsadvanced", "Advanced Options");
2951 $options_stack->set_visible_child_name("raiddisk");
2952 my $options_stack_switcher = Gtk3::StackSwitcher->new();
2953 $options_stack_switcher->set_halign('center');
2954 $options_stack_switcher->set_stack($options_stack);
2955 $grid->attach($options_stack_switcher, 0, $row, 2, 1);
2956 $row++;
2957 $grid->attach($options_stack, 0, $row, 2, 1);
2958 $row++;
2959
2960 $hdoption_first_setup = 0;
2961
2962 my $switch_view = sub {
2963 my $raid = $config_options->{filesys} =~ m/zfs|btrfs/;
2964 my $is_zfs = $config_options->{filesys} =~ m/zfs/;
2965
2966 $target_hd_combo->set_visible(!$raid);
2967 $options_stack->get_child_by_name("hdsize")->set_visible(!$raid);
2968 $options_stack->get_child_by_name("raiddisk")->set_visible($raid);
2969
2970 if ($raid) {
2971 my $msg = "<b>Note</b>: " . ($is_zfs
2972 ? "ZFS is not compatible with hardware RAID controllers, for details see the documentation."
2973 : "BTRFS integration in $setup->{fullname} is a technology preview!"
2974 );
2975 $hw_raid_note->set_markup($msg);
2976 }
2977 $hw_raid_note->set_visible($raid);
2978 $options_stack_switcher->set_visible($raid);
2979 $options_stack->get_child_by_name("raidzfsadvanced")->set_visible($is_zfs);
2980 $options_stack->get_child_by_name("raidbtrfsadvanced")->set_visible(!$is_zfs);
2981 if ($raid) {
2982 $target_hd_label->set_text("Target: $config_options->{filesys} ");
2983 $options_stack->set_visible_child_name("raiddisk");
2984 } else {
2985 $target_hd_label->set_text("Target Harddisk: ");
2986 }
2987
2988 if ($raid) {
2989 $spinbutton_hdsize = $is_zfs ? $spinbutton_hdsize_zfs : $spinbutton_hdsize_btrfs;
2990 } else {
2991 $spinbutton_hdsize = $spinbutton_hdsize_nonraid;
2992 }
2993
2994 my (undef, $pref_width) = $dialog->get_preferred_width();
2995 my (undef, $pref_height) = $dialog->get_preferred_height();
2996 $pref_height = 750 if $pref_height > 750;
2997 $dialog->resize($pref_width, $pref_height);
2998 };
2999
3000 &$switch_view();
3001
3002 $fstypecb->signal_connect (changed => sub {
3003 $config_options->{filesys} = $fstypecb->get_active_text();
3004 &$switch_view();
3005 });
3006
3007 my $sep2 = Gtk3::HSeparator->new();
3008 $sep2->set_visible(1);
3009 $contarea->pack_end($sep2, 1, 1, 10);
3010
3011 $dialog->show();
3012
3013 $dialog->run();
3014
3015 my $get_float = sub {
3016 my ($entry) = @_;
3017
3018 my $text = $entry->get_text();
3019 return undef if !defined($text);
3020
3021 $text =~ s/^\s+//;
3022 $text =~ s/\s+$//;
3023
3024 return undef if $text !~ m/^\d+(\.\d+)?$/;
3025
3026 return $text;
3027 };
3028
3029 my $tmp;
3030
3031 if (($tmp = &$get_float($spinbutton_hdsize)) && ($tmp != $hdsize)) {
3032 $config_options->{hdsize} = $tmp;
3033 } else {
3034 delete $config_options->{hdsize};
3035 }
3036
3037 if (defined($tmp = &$get_float($entry_swapsize))) {
3038 $config_options->{swapsize} = $tmp;
3039 } else {
3040 delete $config_options->{swapsize};
3041 }
3042
3043 if (defined($tmp = &$get_float($entry_maxroot))) {
3044 $config_options->{maxroot} = $tmp;
3045 } else {
3046 delete $config_options->{maxroot};
3047 }
3048
3049 if (defined($tmp = &$get_float($entry_minfree))) {
3050 $config_options->{minfree} = $tmp;
3051 } else {
3052 delete $config_options->{minfree};
3053 }
3054
3055 if ($entry_maxvz && defined($tmp = &$get_float($entry_maxvz))) {
3056 $config_options->{maxvz} = $tmp;
3057 } else {
3058 delete $config_options->{maxvz};
3059 }
3060
3061 $dialog->destroy();
3062 }
3063
3064 my $get_raid_devlist = sub {
3065
3066 my $dev_name_hash = {};
3067
3068 my $devlist = [];
3069 for (my $i = 0; $i < @$hds; $i++) {
3070 if (my $hd = $config_options->{"disksel$i"}) {
3071 my ($disk, $devname, $size, $model, $logical_bsize) = @$hd;
3072 die "device '$devname' is used more than once\n"
3073 if $dev_name_hash->{$devname};
3074 $dev_name_hash->{$devname} = $hd;
3075 push @$devlist, $hd;
3076 }
3077 }
3078
3079 return $devlist;
3080 };
3081
3082 sub zfs_mirror_size_check {
3083 my ($expected, $actual) = @_;
3084
3085 die "mirrored disks must have same size\n"
3086 if abs($expected - $actual) > $expected / 10;
3087 }
3088
3089 sub legacy_bios_4k_check {
3090 my ($lbs) = @_;
3091 die "Booting from 4kn drive in legacy BIOS mode is not supported.\n"
3092 if (($boot_type ne 'efi') && ($lbs == 4096));
3093 }
3094
3095 sub get_zfs_raid_setup {
3096 my $filesys = $config_options->{filesys};
3097
3098 my $devlist = &$get_raid_devlist();
3099
3100 my $diskcount = scalar(@$devlist);
3101 die "$filesys needs at least one device\n" if $diskcount < 1;
3102
3103 my $cmd= '';
3104 if ($filesys eq 'zfs (RAID0)') {
3105 foreach my $hd (@$devlist) {
3106 legacy_bios_4k_check(@$hd[4]);
3107 $cmd .= " @$hd[1]";
3108 }
3109 } elsif ($filesys eq 'zfs (RAID1)') {
3110 die "zfs (RAID1) needs at least 2 device\n" if $diskcount < 2;
3111 $cmd .= ' mirror ';
3112 my $hd = @$devlist[0];
3113 my $expected_size = @$hd[2]; # all disks need approximately same size
3114 foreach my $hd (@$devlist) {
3115 zfs_mirror_size_check($expected_size, @$hd[2]);
3116 legacy_bios_4k_check(@$hd[4]);
3117 $cmd .= " @$hd[1]";
3118 }
3119 } elsif ($filesys eq 'zfs (RAID10)') {
3120 die "zfs (RAID10) needs at least 4 device\n" if $diskcount < 4;
3121 die "zfs (RAID10) needs an even number of devices\n" if $diskcount & 1;
3122
3123 for (my $i = 0; $i < $diskcount; $i+=2) {
3124 my $hd1 = @$devlist[$i];
3125 my $hd2 = @$devlist[$i+1];
3126 zfs_mirror_size_check(@$hd1[2], @$hd2[2]); # pairs need approximately same size
3127 legacy_bios_4k_check(@$hd1[4]);
3128 legacy_bios_4k_check(@$hd2[4]);
3129 $cmd .= ' mirror ' . @$hd1[1] . ' ' . @$hd2[1];
3130 }
3131
3132 } elsif ($filesys =~ m/^zfs \(RAIDZ-([123])\)$/) {
3133 my $level = $1;
3134 my $mindisks = 2 + $level;
3135 die "zfs (RAIDZ-$level) needs at least $mindisks devices\n" if scalar(@$devlist) < $mindisks;
3136 my $hd = @$devlist[0];
3137 my $expected_size = @$hd[2]; # all disks need approximately same size
3138 $cmd .= " raidz$level";
3139 foreach my $hd (@$devlist) {
3140 zfs_mirror_size_check($expected_size, @$hd[2]);
3141 legacy_bios_4k_check(@$hd[4]);
3142 $cmd .= " @$hd[1]";
3143 }
3144 } else {
3145 die "unknown zfs mode '$filesys'\n";
3146 }
3147
3148 return ($devlist, $cmd);
3149 }
3150
3151 sub get_btrfs_raid_setup {
3152
3153 my $filesys = $config_options->{filesys};
3154
3155 my $devlist = &$get_raid_devlist();
3156
3157 my $diskcount = scalar(@$devlist);
3158 die "$filesys needs at least one device\n" if $diskcount < 1;
3159
3160 my $mode;
3161
3162 if ($diskcount == 1) {
3163 $mode = 'single';
3164 } else {
3165 if ($filesys eq 'btrfs (RAID0)') {
3166 $mode = 'raid0';
3167 } elsif ($filesys eq 'btrfs (RAID1)') {
3168 die "btrfs (RAID1) needs at least 2 device\n" if $diskcount < 2;
3169 $mode = 'raid1';
3170 } elsif ($filesys eq 'btrfs (RAID10)') {
3171 die "btrfs (RAID10) needs at least 4 device\n" if $diskcount < 4;
3172 $mode = 'raid10';
3173 } else {
3174 die "unknown btrfs mode '$filesys'\n";
3175 }
3176 }
3177
3178 return ($devlist, $mode);
3179 }
3180
3181 my $last_hd_selected = 0;
3182 sub create_hdsel_view {
3183
3184 $prev_btn->set_sensitive(1); # enable previous button at this point
3185
3186 cleanup_view();
3187
3188 my $vbox = Gtk3::VBox->new(0, 0);
3189 $inbox->pack_start($vbox, 1, 0, 0);
3190 my $hbox = Gtk3::HBox->new(0, 0);
3191 $vbox->pack_start($hbox, 0, 0, 10);
3192
3193 my ($disk, $devname, $size, $model, $logical_bsize) = @{@$hds[0]};
3194 $target_hd = $devname if !defined($target_hd);
3195
3196 $target_hd_label = Gtk3::Label->new("Target Harddisk: ");
3197 $hbox->pack_start($target_hd_label, 0, 0, 0);
3198
3199 $target_hd_combo = Gtk3::ComboBoxText->new();
3200
3201 foreach my $hd (@$hds) {
3202 ($disk, $devname, $size, $model, $logical_bsize) = @$hd;
3203 $target_hd_combo->append_text(get_device_desc($devname, $size, $model));
3204 }
3205
3206 my $raid = $config_options->{filesys} =~ m/zfs|btrfs/;
3207 if ($raid) {
3208 $target_hd_label->set_text("Target: $config_options->{filesys} ");
3209 $target_hd_combo->set_visible(0);
3210 $target_hd_combo->set_no_show_all(1);
3211 }
3212 $target_hd_combo->set_active($last_hd_selected);
3213 $target_hd_combo->signal_connect(changed => sub {
3214 $a = shift->get_active;
3215 my ($disk, $devname) = @{@$hds[$a]};
3216 $last_hd_selected = $a;
3217 $target_hd = $devname;
3218 });
3219
3220 $hbox->pack_start($target_hd_combo, 0, 0, 10);
3221
3222 my $options = Gtk3::Button->new('_Options');
3223 $options->signal_connect (clicked => \&create_hdoption_view);
3224 $hbox->pack_start ($options, 0, 0, 0);
3225
3226
3227 $inbox->show_all;
3228
3229 display_html();
3230
3231 set_next(undef, sub {
3232
3233 if ($config_options->{filesys} =~ m/zfs/) {
3234 my ($devlist) = eval { get_zfs_raid_setup() };
3235 if (my $err = $@) {
3236 display_message("Warning: $err\nPlease fix ZFS setup first.");
3237 return;
3238 }
3239 $config_options->{target_hds} = [ map { $_->[1] } @$devlist ];
3240 } elsif ($config_options->{filesys} =~ m/btrfs/) {
3241 my ($devlist) = eval { get_btrfs_raid_setup() };
3242 if (my $err = $@) {
3243 display_message("Warning: $err\nPlease fix BTRFS setup first.");
3244 return;
3245 }
3246 $config_options->{target_hds} = [ map { $_->[1] } @$devlist ];
3247 } else {
3248 eval { legacy_bios_4k_check(logical_blocksize($target_hd)) };
3249 if (my $err = $@) {
3250 display_message("Warning: $err\n");
3251 return;
3252 }
3253 $config_options->{target_hds} = [ $target_hd ];
3254 }
3255
3256 $step_number++;
3257 create_country_view();
3258 });
3259 }
3260
3261 sub create_extract_view {
3262
3263 cleanup_view();
3264
3265 display_info();
3266
3267 $next->set_sensitive(0);
3268 $prev_btn->set_sensitive(0);
3269 $prev_btn->hide();
3270
3271 my $vbox = Gtk3::VBox->new(0, 0);
3272 $inbox->pack_start ($vbox, 1, 0, 0);
3273 my $hbox = Gtk3::HBox->new(0, 0);
3274 $vbox->pack_start ($hbox, 0, 0, 10);
3275
3276 my $vbox2 = Gtk3::VBox->new(0, 0);
3277 $hbox->pack_start ($vbox2, 0, 0, 0);
3278
3279 $progress_status = Gtk3::Label->new('');
3280 $vbox2->pack_start ($progress_status, 1, 1, 0);
3281
3282 $progress = Gtk3::ProgressBar->new;
3283 $progress->set_show_text(1);
3284 $progress->set_size_request (600, -1);
3285
3286 $vbox2->pack_start($progress, 0, 0, 0);
3287
3288 $inbox->show_all();
3289
3290 my $tdir = is_test_mode() ? "target" : "/target";
3291 mkdir $tdir;
3292 my $base = "${proxmox_cddir}/$setup->{product}-base.squashfs";
3293
3294 eval { extract_data($base, $tdir); };
3295 my $err = $@;
3296
3297 $next->set_sensitive(1);
3298
3299 set_next("_Reboot", sub { exit (0); } );
3300
3301 if ($err) {
3302 display_html("fail.htm");
3303 display_error($err);
3304 } else {
3305 cleanup_view();
3306 display_html("success.htm");
3307
3308 if ($config_options->{autoreboot}) {
3309 Glib::Timeout->add(1000, sub {
3310 if ($autoreboot_seconds > 0) {
3311 $autoreboot_seconds--;
3312 display_html("success.htm");
3313 } else {
3314 exit(0);
3315 }
3316 });
3317 }
3318 }
3319 }
3320
3321 sub create_intro_view {
3322
3323 $prev_btn->set_sensitive(0);
3324
3325 cleanup_view();
3326
3327 if (int($total_memory) < 1024) {
3328 display_error("Less than 1 GiB of usable memory detected, installation will probably fail.\n\n".
3329 "See 'System Requirements' in the $setup->{fullname} documentation.");
3330 }
3331
3332 if ($setup->{product} eq 'pve') {
3333 my $cpuinfo = eval { file_read_all('/proc/cpuinfo') };
3334 if (!$cpuinfo || $cpuinfo !~ /^flags\s*:.*(vmx|svm)/m) {
3335 display_error(
3336 "No support for hardware-accelerated KVM virtualization detected.\n\n"
3337 ."Check BIOS settings for Intel VT / AMD-V / SVM."
3338 );
3339 }
3340 }
3341
3342 display_html();
3343
3344 $step_number++;
3345 set_next("I a_gree", \&create_hdsel_view);
3346 }
3347
3348 $ipconf = Proxmox::Sys::Net::get_ip_config();
3349
3350 $country = detect_country() if $ipconf->{default} || is_test_mode();
3351
3352 # read country, kmap and timezone infos
3353 $cmap = read_cmap();
3354
3355 if (!defined($cmap->{country}->{$country})) {
3356 log_warn("ignoring detected country '$country', invalid or unknown\n");
3357 $country = undef;
3358 }
3359
3360 create_main_window ();
3361
3362 my $initial_error = 0;
3363
3364 if (!defined ($hds) || (scalar (@$hds) <= 0)) {
3365 print "no harddisks found\n";
3366 $initial_error = 1;
3367 display_html("nohds.htm");
3368 set_next("Reboot", sub { exit(0); } );
3369 } else {
3370 foreach my $hd (@$hds) {
3371 my ($disk, $devname) = @$hd;
3372 next if $devname =~ m|^/dev/md\d+$|;
3373 print "found Disk$disk N:$devname\n";
3374 }
3375 }
3376
3377 if (!$initial_error && (scalar keys %{ $ipconf->{ifaces} } == 0)) {
3378 print "no network interfaces found\n";
3379 $initial_error = 1;
3380 display_html("nonics.htm");
3381 set_next("Reboot", sub { exit(0); } );
3382 }
3383
3384 create_intro_view () if !$initial_error;
3385
3386 Gtk3->main;
3387
3388 # reap left over zombie processes
3389 while ((my $child = waitpid(-1, POSIX::WNOHANG)) > 0) {
3390 print "reaped child $child\n";
3391 }
3392
3393 exit 0;