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