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