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