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