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