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