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