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