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