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