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