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