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