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