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