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