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