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