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