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