]> git.proxmox.com Git - pve-installer.git/blobdiff - proxinstall
stop main loop when closing the main window
[pve-installer.git] / proxinstall
index 396e344c8c630945a98c499492926fd98a271a4e..21c7201eb388430cf957cc99b99346a8fad4c04e 100755 (executable)
@@ -8,40 +8,44 @@ $ENV{LC_ALL} = 'C';
 
 use Getopt::Long;
 use IPC::Open2;
-use IPC::Open3;
 use IO::File;
-use IO::Select;
 use Cwd 'abs_path';
 use Glib;
-use Gtk3 '-init';
+use Gtk3;
 use Gtk3::WebKit2;
 use Encode;
-use String::ShellQuote;
-use Data::Dumper;
 use File::Basename;
 use File::Path;
 use Time::HiRes;
 use POSIX ":sys_wait_h";
 
-use ProxmoxInstallerSetup;
+use Proxmox::Install::ISOEnv;
+use Proxmox::Log;
+use Proxmox::Sys::Block qw(get_cached_disks wipe_disk partition_bootable_disk);
+use Proxmox::Sys::Command qw(run_command syscmd);
+use Proxmox::Sys::File qw(file_read_firstline file_read_all file_write_all);
+use Proxmox::Sys::Net qw(parse_ip_address parse_ip_mask);
 
 if (!$ENV{G_SLICE} ||  $ENV{G_SLICE} ne "always-malloc") {
     die "do not use slice allocator (run with 'G_SLICE=always-malloc ./proxinstall ...')\n";
 }
 
-my $opt_testmode;
-if (!GetOptions('testmode=s' => \$opt_testmode)) {
-    die "usage error\n";
-    exit (-1);
+{
+    my $test_image;
+    GetOptions(
+       'test-image|t=s' => \$test_image
+    ) or die "usage error\n";
+
+    Proxmox::Install::ISOEnv::set_test_image($test_image) if $test_image;
 }
 
 $ENV{'LVM_SUPPRESS_FD_WARNINGS'} = '1';
 
-my ($setup, $cd_info) = ProxmoxInstallerSetup::setup();
+my $env = Proxmox::Install::ISOEnv::setup();
 
 my $zfstestpool = "test_rpool";
-my $zfspoolname = $opt_testmode ? $zfstestpool : 'rpool';
-my $zfsrootvolname = "$setup->{product}-1";
+my $zfspoolname = is_test_mode() ? $zfstestpool : 'rpool';
+my $zfsrootvolname = "$env->{product}-1";
 
 my $storage_cfg_zfs = <<__EOD__;
 dir: local
@@ -82,118 +86,14 @@ dir: local
        content iso,vztmpl,backup,rootdir,images
 __EOD__
 
-sub file_read_firstline {
-    my ($filename) = @_;
-
-    my $fh = IO::File->new ($filename, "r");
-    return undef if !$fh;
-    my $res = <$fh>;
-    chomp $res if $res;
-    $fh->close;
-    return $res;
-}
-
-my $logfd = IO::File->new(">/tmp/install.log");
+Proxmox::Log::init("/tmp/install.log");
 
-my $proxmox_libdir = $opt_testmode
-    ? Cwd::cwd() . "/testdir/var/lib/proxmox-installer"
-    : "/var/lib/proxmox-installer"
-    ;
-my $proxmox_cddir = $opt_testmode ? "../pve-cd-builder/tmp/data-gz/" : "/cdrom";
+my $proxmox_libdir = $env->{locations}->{lib};
+my $proxmox_cddir = $env->{locations}->{iso};
 my $proxmox_pkgdir = "${proxmox_cddir}/proxmox/packages/";
 
 my $boot_type = -d '/sys/firmware/efi' ? 'efi' : 'bios';
 
-my $IPV4OCTET = "(?:25[0-5]|(?:2[0-4]|1[0-9]|[1-9])?[0-9])";
-my $IPV4RE = "(?:(?:$IPV4OCTET\\.){3}$IPV4OCTET)";
-my $IPV6H16 = "(?:[0-9a-fA-F]{1,4})";
-my $IPV6LS32 = "(?:(?:$IPV4RE|$IPV6H16:$IPV6H16))";
-
-my $IPV6RE = "(?:" .
-    "(?:(?:" .                             "(?:$IPV6H16:){6})$IPV6LS32)|" .
-    "(?:(?:" .                           "::(?:$IPV6H16:){5})$IPV6LS32)|" .
-    "(?:(?:(?:" .              "$IPV6H16)?::(?:$IPV6H16:){4})$IPV6LS32)|" .
-    "(?:(?:(?:(?:$IPV6H16:){0,1}$IPV6H16)?::(?:$IPV6H16:){3})$IPV6LS32)|" .
-    "(?:(?:(?:(?:$IPV6H16:){0,2}$IPV6H16)?::(?:$IPV6H16:){2})$IPV6LS32)|" .
-    "(?:(?:(?:(?:$IPV6H16:){0,3}$IPV6H16)?::(?:$IPV6H16:){1})$IPV6LS32)|" .
-    "(?:(?:(?:(?:$IPV6H16:){0,4}$IPV6H16)?::" .           ")$IPV6LS32)|" .
-    "(?:(?:(?:(?:$IPV6H16:){0,5}$IPV6H16)?::" .            ")$IPV6H16)|" .
-    "(?:(?:(?:(?:$IPV6H16:){0,6}$IPV6H16)?::" .                    ")))";
-
-my $IPRE = "(?:$IPV4RE|$IPV6RE)";
-
-
-my $ipv4_mask_hash = {
-    '128.0.0.0' => 1,
-    '192.0.0.0' => 2,
-    '224.0.0.0' => 3,
-    '240.0.0.0' => 4,
-    '248.0.0.0' => 5,
-    '252.0.0.0' => 6,
-    '254.0.0.0' => 7,
-    '255.0.0.0' => 8,
-    '255.128.0.0' => 9,
-    '255.192.0.0' => 10,
-    '255.224.0.0' => 11,
-    '255.240.0.0' => 12,
-    '255.248.0.0' => 13,
-    '255.252.0.0' => 14,
-    '255.254.0.0' => 15,
-    '255.255.0.0' => 16,
-    '255.255.128.0' => 17,
-    '255.255.192.0' => 18,
-    '255.255.224.0' => 19,
-    '255.255.240.0' => 20,
-    '255.255.248.0' => 21,
-    '255.255.252.0' => 22,
-    '255.255.254.0' => 23,
-    '255.255.255.0' => 24,
-    '255.255.255.128' => 25,
-    '255.255.255.192' => 26,
-    '255.255.255.224' => 27,
-    '255.255.255.240' => 28,
-    '255.255.255.248' => 29,
-    '255.255.255.252' => 30,
-    '255.255.255.254' => 31,
-    '255.255.255.255' => 32
-};
-
-my $ipv4_reverse_mask = [
-    '0.0.0.0',
-    '128.0.0.0',
-    '192.0.0.0',
-    '224.0.0.0',
-    '240.0.0.0',
-    '248.0.0.0',
-    '252.0.0.0',
-    '254.0.0.0',
-    '255.0.0.0',
-    '255.128.0.0',
-    '255.192.0.0',
-    '255.224.0.0',
-    '255.240.0.0',
-    '255.248.0.0',
-    '255.252.0.0',
-    '255.254.0.0',
-    '255.255.0.0',
-    '255.255.128.0',
-    '255.255.192.0',
-    '255.255.224.0',
-    '255.255.240.0',
-    '255.255.248.0',
-    '255.255.252.0',
-    '255.255.254.0',
-    '255.255.255.0',
-    '255.255.255.128',
-    '255.255.255.192',
-    '255.255.255.224',
-    '255.255.255.240',
-    '255.255.255.248',
-    '255.255.255.252',
-    '255.255.255.254',
-    '255.255.255.255',
-];
-
 my $step_number = 0; # Init number for global function list
 
 my @steps = (
@@ -255,7 +155,6 @@ my $timezone = 'Europe/Vienna';
 my $keymap = 'en-us';
 my $password;
 my $mailto = 'mail@example.invalid';
-my $cmap;
 my $autoreboot_seconds = 5;
 
 my $config = {
@@ -303,7 +202,7 @@ if ($cmdline =~ m/minfree=(\d+(\.\d+)?)[\s\n]/i) {
     $config_options->{minfree} = $1;
 }
 
-if ($setup->{product} eq 'pve') {
+if ($env->{product} eq 'pve') {
     if ($cmdline =~ m/maxvz=(\d+(\.\d+)?)[\s\n]/i) {
        $config_options->{maxvz} = $1;
     }
@@ -335,161 +234,22 @@ compatibility_level = 2
 
 _EOD
 
-sub shellquote {
-    my $str = shift;
-
-    return String::ShellQuote::shell_quote($str);
-}
-
-sub cmd2string {
-    my ($cmd) = @_;
-
-    die "no arguments" if !$cmd;
-
-    return $cmd if !ref($cmd);
-
-    my @qa = ();
-    foreach my $arg (@$cmd) { push @qa, shellquote($arg); }
-
-    return join (' ', @qa);
-}
-
-sub syscmd {
-    my ($cmd) = @_;
-
-    return run_command($cmd, undef, undef, 1);
-}
-
-sub run_command {
-    my ($cmd, $func, $input, $noout) = @_;
-
-    my $cmdstr;
-    if (!ref($cmd)) {
-       $cmdstr = $cmd;
-       if ($cmd =~ m/|/) {
-           # see 'man bash' for option pipefail
-           $cmd = [ '/bin/bash', '-c', "set -o pipefail && $cmd" ];
-       } else {
-           $cmd = [ $cmd ];
-       }
-    } else {
-       $cmdstr = cmd2string($cmd);
-    }
-
-    my $cmdtxt;
-    if ($input && ($cmdstr !~ m/chpasswd/)) {
-       $cmdtxt = "# $cmdstr <<EOD\n$input";
-       chomp $cmdtxt;
-       $cmdtxt .= "\nEOD\n";
-    } else {
-       $cmdtxt = "# $cmdstr\n";
-    }
-
-    if ($opt_testmode) {
-       print $cmdtxt;
-       STDOUT->flush();
-    }
-
-    print $logfd $cmdtxt;
-
-    my $reader = IO::File->new();
-    my $writer = IO::File->new();
-    my $error  = IO::File->new();
-
-    my $orig_pid = $$;
-
-    my $pid = eval { open3($writer, $reader, $error, @$cmd) || die $!; };
-    my $err = $@;
-
-    # catch exec errors
-    if ($orig_pid != $$) {
-       POSIX::_exit (1);
-       kill ('KILL', $$);
-    }
-
-    die $err if $err;
-
-    print $writer $input if defined $input;
-    close $writer;
-
-    my $select = IO::Select->new();
-    $select->add($reader);
-    $select->add($error);
-
-    my ($ostream, $logout) = ('', '', '');
-
-    while ($select->count) {
-       my @handles = $select->can_read (0.2);
-
-       Gtk3::main_iteration() while Gtk3::events_pending();
-
-       next if !scalar (@handles); # timeout
-
-       foreach my $h (@handles) {
-           my $buf = '';
-           my $count = sysread ($h, $buf, 4096);
-           if (!defined ($count)) {
-               my $err = $!;
-               kill (9, $pid);
-               waitpid ($pid, 0);
-               die "command '$cmd' failed: $err";
-           }
-           $select->remove($h) if !$count;
-           if ($h eq $reader) {
-               $ostream .= $buf if !($noout || $func);
-               $logout .= $buf;
-               while ($logout =~ s/^([^\010\r\n]*)(\r|\n|(\010)+|\r\n)//s) {
-                   my $line = $1;
-                   &$func($line) if $func;
-               }
-
-           } elsif ($h eq $error) {
-               $ostream .= $buf if !($noout || $func);
-           }
-           print $buf;
-           STDOUT->flush();
-           print $logfd $buf;
-       }
-    }
-
-    &$func($logout) if $func;
-
-    my $rv = waitpid ($pid, 0);
 
-    return $? if $noout; # behave like standard system();
+sub app_quit {
+    my ($exit_code) = @_;
 
-    if ($? == -1) {
-       die "command '$cmdstr' failed to execute\n";
-    } elsif (my $sig = ($? & 127)) {
-       die "command '$cmdstr' failed - got signal $sig\n";
-    } elsif (my $exitcode = ($? >> 8)) {
-       die "command '$cmdstr' failed with exit code $exitcode";
-    }
-
-    return $ostream;
-}
-
-# forks and runs the provided coderef in the child
-# do not use syscmd or run_command as both confuse the GTK mainloop if
-# run from a child process
-sub run_in_background {
-    my ($cmd) = @_;
+    Gtk3->main_quit() if Gtk3->main_level() > 0;
 
-    my $pid = fork() // die "fork failed: $!\n";
-    if (!$pid) {
-       eval { $cmd->(); };
-       if (my $err = $@) {
-           warn "run_in_background error: $err\n";
-           POSIX::_exit(1);
-       }
-       POSIX::_exit(0);
+    # reap left over zombie processes
+    while ((my $child = waitpid(-1, POSIX::WNOHANG)) > 0) {
+       print "reaped child $child\n";
     }
+    exit($exit_code);
 }
 
 sub detect_country {
-
     print "trying to detect country...\n";
-    my $cpid = open2(\*TMP, undef, "traceroute -N 1 -q 1 -n 8.8.8.8");
+    my $cpid = open2(my $TRACEROUTE_FH, undef, "traceroute -N 1 -q 1 -n 8.8.8.8");
     return undef if !$cpid;
 
     my $country;
@@ -498,25 +258,23 @@ sub detect_country {
     eval  {
        local $SIG{ALRM} = sub { die "timed out!\n" };
        my $line;
-       while (defined ($line = <TMP>)) {
-           print $logfd "DC TRACEROUTE: $line";
+       while (defined ($line = <$TRACEROUTE_FH>)) {
+           log_debug("DC TRACEROUTE: $line");
            if ($line =~ m/\s*\d+\s+(\d+\.\d+\.\d+\.\d+)\s/) {
                my $geoip = `geoiplookup $1`;
-               print $logfd "DC GEOIP: $geoip";
+               log_debug("DC GEOIP: $geoip");
                if ($geoip =~ m/GeoIP Country Edition:\s*([A-Z]+),/) {
                    $country = lc ($1);
-                   print $logfd "DC FOUND: $country\n";
+                   log_info("DC FOUND: $country\n");
                    last;
                }
            }
        }
     };
-
     my $err = $@;
-
     alarm ($previous_alarm);
 
-    close (TMP);
+    close($TRACEROUTE_FH);
 
     if ($err) {
        print "unable to detect country - $err\n";
@@ -547,233 +305,6 @@ sub get_memtotal {
 
 my $total_memory = get_memtotal();
 
-sub link_points_to {
-    my ($src, $dest) = @_;
-
-    my ($dev1,$ino1) = stat ($src);
-    my ($dev2,$ino2) = stat ($dest);
-
-    return 0 if !($dev1 && $dev2 && $ino1 && $ino2);
-
-    return $ino1 == $ino2 && $dev1 == $dev2;
-}
-
-sub find_stable_path {
-    my ($stabledir, $bdev) = @_;
-
-    foreach my $path (<$stabledir/*>) {
-       if (link_points_to($path, $bdev)) {
-           return wantarray ? ($path, basename($path)) : $path;
-       }
-    }
-}
-
-sub find_dev_by_uuid {
-    my $bdev = shift;
-
-    my ($full_path, $name) = find_stable_path("/dev/disk/by-uuid", $bdev);
-
-    return $name;
-}
-
-sub hd_list {
-
-    my $res = ();
-
-    if ($opt_testmode) {
-       my @disks = split /,/, $opt_testmode;
-
-       for my $disk (@disks) {
-           push @$res, [-1, $disk, int((-s $disk)/512), "TESTDISK", 512];
-       }
-       return $res;
-    }
-
-    my $count = 0;
-
-    foreach my $bd (</sys/block/*>) {
-       next if $bd =~ m|^/sys/block/ram\d+$|;
-       next if $bd =~ m|^/sys/block/loop\d+$|;
-       next if $bd =~ m|^/sys/block/md\d+$|;
-       next if $bd =~ m|^/sys/block/dm-.*$|;
-       next if $bd =~ m|^/sys/block/fd\d+$|;
-       next if $bd =~ m|^/sys/block/sr\d+$|;
-
-       my $dev = file_read_firstline("$bd/dev");
-       chomp $dev;
-
-       next if !$dev;
-
-       my $info = `udevadm info --path $bd --query all`;
-       next if !$info;
-
-       next if $info !~ m/^E: DEVTYPE=disk$/m;
-
-       next if $info =~ m/^E: ID_CDROM/m;
-       next if $info =~ m/^E: ID_FS_TYPE=iso9660/m;
-
-       my ($name) = $info =~ m/^N: (\S+)$/m;
-
-       if ($name) {
-           my $real_name = "/dev/$name";
-
-           my $size = file_read_firstline("$bd/size");
-           chomp $size;
-           $size = undef if !($size && $size =~ m/^\d+$/);
-
-           my $model = file_read_firstline("$bd/device/model") || '';
-           $model =~ s/^\s+//;
-           $model =~ s/\s+$//;
-           if (length ($model) > 30) {
-               $model = substr ($model, 0, 30);
-           }
-
-           my $logical_bsize = file_read_firstline("$bd/queue/logical_block_size") // '';
-           chomp $logical_bsize;
-           $logical_bsize = undef if !($logical_bsize && $logical_bsize =~ m/^\d+$/);
-
-           push @$res, [$count++, $real_name, $size, $model, $logical_bsize] if $size;
-       } else {
-           print STDERR "ERROR: unable to map device $dev ($bd)\n";
-       }
-    }
-
-    return $res;
-}
-
-sub read_cmap {
-    my $countryfn = "${proxmox_libdir}/country.dat";
-    open (my $TMP, "<:encoding(utf8)", "$countryfn") || die "unable to open '$countryfn' - $!\n";
-    my $line;
-    my $country = {};
-    my $countryhash = {};
-    my $kmap = {};
-    my $kmaphash = {};
-    while (defined ($line = <$TMP>)) {
-       if ($line =~ m|^map:([^\s:]+):([^:]+):([^:]+):([^:]+):([^:]+):([^:]*):$|) {
-           $kmap->{$1} = {
-               name => $2,
-               kvm => $3,
-               console => $4,
-               x11 => $5,
-               x11var => $6,
-           };
-           $kmaphash->{$2} = $1;
-       } elsif ($line =~ m|^([a-z]{2}):([^:]+):([^:]*):([^:]*):$|) {
-           $country->{$1} = {
-               name => $2,
-               kmap => $3,
-               mirror => $4,
-           };
-           $countryhash->{lc($2)} = $1;
-       } else {
-           warn "unable to parse 'country.dat' line: $line";
-       }
-    }
-    close ($TMP);
-    $TMP = undef;
-
-    my $zones = {};
-    my $cczones = {};
-    my $zonefn = "/usr/share/zoneinfo/zone.tab";
-    open ($TMP, '<', "$zonefn") || die "unable to open '$zonefn' - $!\n";
-    while (defined ($line = <$TMP>)) {
-       next if $line =~ m/^\#/;
-       next if $line =~ m/^\s*$/;
-       if ($line =~ m|^([A-Z][A-Z])\s+\S+\s+(([^/]+)/\S+)\s|) {
-           my $cc = lc($1);
-           $cczones->{$cc}->{$2} = 1;
-           $country->{$cc}->{zone} = $2 if !defined ($country->{$cc}->{zone});
-           $zones->{$2} = 1;
-
-       }
-    }
-    close ($TMP);
-
-    return {
-       zones => $zones,
-       cczones => $cczones,
-       country => $country,
-       countryhash => $countryhash,
-       kmap => $kmap,
-       kmaphash => $kmaphash,
-    }
-}
-
-# search for Harddisks
-my $hds = hd_list();
-
-sub hd_size {
-    my ($dev) = @_;
-
-    foreach my $hd (@$hds) {
-       my ($disk, $devname, $size, $model, $logical_bsize) = @$hd;
-       # size is always (also for 4kn disks) in 512B "sectors"! convert to KB
-       return int($size/2) if $devname eq $dev;
-    }
-
-    die "no such device '$dev'\n";
-}
-
-sub logical_blocksize {
-    my ($dev) = @_;
-
-    foreach my $hd (@$hds) {
-       my ($disk, $devname, $size, $model, $logical_bsize) = @$hd;
-       return $logical_bsize if $devname eq $dev;
-    }
-
-    die "no such device '$dev'\n";
-}
-
-sub get_partition_dev {
-    my ($dev, $partnum) = @_;
-
-    if ($dev =~ m|^/dev/sd([a-h]?[a-z]\|i[a-v])$|) {
-       return "${dev}$partnum";
-    } elsif ($dev =~ m|^/dev/xvd[a-z]$|) {
-       # Citrix Hypervisor blockdev
-       return "${dev}$partnum";
-    } elsif ($dev =~ m|^/dev/[hxev]d[a-z]$|) {
-       return "${dev}$partnum";
-    } elsif ($dev =~ m|^/dev/[^/]+/c\d+d\d+$|) {
-       return "${dev}p$partnum";
-    } elsif ($dev =~ m|^/dev/[^/]+/d\d+$|) {
-       return "${dev}p$partnum";
-    } elsif ($dev =~ m|^/dev/[^/]+/hd[a-z]$|) {
-       return "${dev}$partnum";
-    } elsif ($dev =~ m|^/dev/nvme\d+n\d+$|) {
-       return "${dev}p$partnum";
-    } else {
-       die "unable to get device for partition $partnum on device $dev\n";
-    }
-
-}
-
-sub file_get_contents {
-    my ($filename, $max) = @_;
-
-    my $fh = IO::File->new($filename, "r") ||
-       die "can't open '$filename' - $!\n";
-
-    local $/; # slurp mode
-
-    my $content = <$fh>;
-
-    close $fh;
-
-    return $content;
-}
-
-sub write_config {
-    my ($text, $filename) = @_;
-
-    my $fd = IO::File->new(">$filename") ||
-       die "unable to open file '$filename' - $!\n";
-    print $fd $text;
-    $fd->close();
-}
-
 sub update_progress {
     my ($frac, $start, $end, $text) = @_;
 
@@ -836,7 +367,7 @@ sub debconfig_set {
     my ($targetdir, $dcdata) = @_;
 
     my $cfgfile = "/tmp/debconf.txt";
-    write_config($dcdata, "$targetdir/$cfgfile");
+    file_write_all("$targetdir/$cfgfile", $dcdata);
     syscmd("chroot $targetdir debconf-set-selections $cfgfile");
     unlink "$targetdir/$cfgfile";
 }
@@ -844,30 +375,28 @@ sub debconfig_set {
 sub diversion_add {
     my ($targetdir, $cmd, $new_cmd) = @_;
 
-    syscmd("chroot $targetdir dpkg-divert --package proxmox " .
-          "--add --rename $cmd") == 0 ||
-          die "unable to exec dpkg-divert\n";
+    syscmd("chroot $targetdir dpkg-divert --package proxmox --add --rename $cmd") == 0
+        || die "unable to exec dpkg-divert\n";
 
-    syscmd("ln -sf ${new_cmd} $targetdir/$cmd") == 0 ||
-       die "unable to link diversion to ${new_cmd}\n";
+    syscmd("ln -sf ${new_cmd} $targetdir/$cmd") == 0
+        || die "unable to link diversion to ${new_cmd}\n";
 }
 
 sub diversion_remove {
     my  ($targetdir, $cmd) = @_;
 
-    syscmd("mv $targetdir/${cmd}.distrib $targetdir/${cmd};") == 0 ||
-       die "unable to remove $cmd diversion\n";
+    syscmd("mv $targetdir/${cmd}.distrib $targetdir/${cmd};") == 0
+        || die "unable to remove $cmd diversion\n";
 
-    syscmd("chroot $targetdir dpkg-divert --remove $cmd") == 0 ||
-       die "unable to remove $cmd diversion\n";
+    syscmd("chroot $targetdir dpkg-divert --remove $cmd") == 0
+        || die "unable to remove $cmd diversion\n";
 }
 
 sub btrfs_create {
     my ($partitions, $mode) = @_;
 
     die "unknown btrfs mode '$mode'"
-       if !($mode eq 'single' || $mode eq 'raid0' ||
-            $mode eq 'raid1' || $mode eq 'raid10');
+       if !($mode eq 'single' || $mode eq 'raid0' || $mode eq 'raid1' || $mode eq 'raid10');
 
     my $cmd = ['mkfs.btrfs', '-f'];
 
@@ -892,7 +421,7 @@ sub zfs_create_rpool {
     syscmd("zfs create $zfspoolname/ROOT")  == 0 ||
         die "unable to create zfs $zfspoolname/ROOT volume\n";
 
-    if ($setup->{product} eq 'pve') {
+    if ($env->{product} eq 'pve') {
        syscmd("zfs create $zfspoolname/data")  == 0 ||
            die "unable to create zfs $zfspoolname/data volume\n";
     }
@@ -917,112 +446,6 @@ sub zfs_create_rpool {
         if defined($value) && $value != 1;
 }
 
-my $udevadm_trigger_block = sub {
-    my ($nowait) = @_;
-
-    sleep(1) if !$nowait; # give kernel time to reread part table
-
-    # trigger udev to create /dev/disk/by-uuid
-    syscmd("udevadm trigger --subsystem-match block");
-    syscmd("udevadm settle --timeout 10");
-};
-
-my $clean_disk = sub {
-    my ($disk) = @_;
-
-    # sort longest first as we need to cleanup depth-first
-    my @partitions = sort { length($b) <=> length($a) }
-       split("\n", `lsblk --output kname --noheadings --path --list $disk`);
-
-    for my $part (@partitions) {
-       next if $part eq $disk;
-       next if $part !~ /^\Q$disk\E/;
-       eval { syscmd("pvremove -ff -y $part"); };
-       eval { syscmd("zpool labelclear -f $part"); };
-       eval { syscmd("dd if=/dev/zero of=$part bs=1M count=16"); };
-    }
-    eval { syscmd("wipefs -a " . cmd2string(\@partitions)) };
-    warn "$@" if $@;
-};
-
-sub partition_bootable_disk {
-    my ($target_dev, $maxhdsizegb, $ptype) = @_;
-
-    die "too dangerous" if $opt_testmode;
-
-    die "unknown partition type '$ptype'"
-       if !($ptype eq '8E00' || $ptype eq '8300' || $ptype eq 'BF01');
-
-    my $hdsize = hd_size($target_dev); # size in KB (1024 bytes)
-
-    my $restricted_hdsize_mb = 0; # 0 ==> end of partition
-    if ($maxhdsizegb) {
-       my $maxhdsize = $maxhdsizegb * 1024 * 1024;
-       if ($maxhdsize < $hdsize) {
-           $hdsize = $maxhdsize;
-           $restricted_hdsize_mb = int($hdsize/1024) . 'M';
-       }
-    }
-
-    my $hdgb = int($hdsize/(1024*1024));
-
-    my ($hard_limit, $soft_limit) = (2, 8);
-
-    die "root disk '$target_dev' too small (${hdgb} GB < $hard_limit GB)\n" if $hdgb < $hard_limit;
-    if ($hdgb < $soft_limit) {
-       my $response = display_prompt(
-           "Root disk space ${hdgb} GB is below recommended minimum space of $soft_limit GB,"
-           ." installation might not be successful! Continue?"
-       );
-       die "root disk '$target_dev' too small (${hdgb} GB < $soft_limit GB), and warning not accepted.\n"
-           if $response ne 'ok';
-    }
-
-
-    syscmd("sgdisk -Z ${target_dev}");
-
-    # 1 - BIOS boot partition (Grub Stage2): first free 1M
-    # 2 - EFI ESP: next free 512M
-    # 3 - OS/Data partition: rest, up to $maxhdsize in MB
-
-    my $grubbootdev = get_partition_dev($target_dev, 1);
-    my $efibootdev = get_partition_dev($target_dev, 2);
-    my $osdev = get_partition_dev ($target_dev, 3);
-
-    my $pcmd = ['sgdisk'];
-
-    my $pnum = 2;
-    push @$pcmd, "-n${pnum}:1M:+512M", "-t$pnum:EF00";
-
-    $pnum = 3;
-    push @$pcmd, "-n${pnum}:513M:${restricted_hdsize_mb}", "-t$pnum:$ptype";
-
-    push @$pcmd, $target_dev;
-
-    my $os_size = $hdsize - 513*1024; # 512M efi + 1M bios_boot + 1M alignment
-
-    syscmd($pcmd) == 0 ||
-       die "unable to partition harddisk '${target_dev}'\n";
-
-    my $blocksize = logical_blocksize($target_dev);
-
-    if ($blocksize != 4096) {
-       $pnum = 1;
-       $pcmd = ['sgdisk', '-a1', "-n$pnum:34:2047", "-t$pnum:EF02" , $target_dev];
-
-       syscmd($pcmd) == 0 ||
-           die "unable to create bios_boot partition '${target_dev}'\n";
-    }
-
-    &$udevadm_trigger_block();
-
-    foreach my $part ($efibootdev, $osdev) {
-       syscmd("dd if=/dev/zero of=$part bs=1M count=256") if -b $part;
-    }
-
-    return ($os_size, $osdev, $efibootdev);
-}
-
 sub get_pv_list_from_vgname {
     my ($vgname) = @_;
 
@@ -1080,7 +503,7 @@ sub ask_existing_vg_rename_or_abort {
                die "could not rename VG from '$vg->{pvs}' ($vg_uuid) to '$new_vgname'!\n";
        }
     } else {
-       set_next("_Reboot", sub { exit (0); } );
+       set_next("_Reboot", sub { app_quit(0); } );
        display_html("fail.htm");
        die "Cancled installation by user, due to already existing volume group '$vgname'\n";
     }
@@ -1089,7 +512,7 @@ sub ask_existing_vg_rename_or_abort {
 sub create_lvm_volumes {
     my ($lvmdev, $os_size, $swap_size) = @_;
 
-    my $vgname = $setup->{product};
+    my $vgname = $env->{product};
 
     ask_existing_vg_rename_or_abort($vgname);
 
@@ -1105,13 +528,15 @@ sub create_lvm_volumes {
        die "unable to create volume group '$vgname'\n";
 
     my $hdgb = int($os_size / (1024 * 1024));
-    # FIXME: drop odd by-default unallocated space?
-    my $space = $hdgb <= 32 ? 0 : (($hdgb > 128 ? 16 : $hdgb / 8) * 1024 * 1024);
+
+    # always leave some space at the end to avoid roudning issues with LVM's physical extent (PE)
+    # size of 4 MB.
+    my $space = $hdgb <= 32 ? 4 * 1024 : (($hdgb > 128 ? 16 : $hdgb / 8) * 1024 * 1024);
 
     my $rootsize;
     my $datasize = 0;
 
-    if ($setup->{product} eq 'pve') {
+    if ($env->{product} eq 'pve') {
 
        my $maxroot_mb;
        if ($config_options->{maxroot}) {
@@ -1154,7 +579,7 @@ sub create_lvm_volumes {
 
     } else {
        my $minfree = defined($config_options->{minfree}) ? $config_options->{minfree}*1024*1024 : $space;
-       $rootsize = $os_size - $minfree - $swap_size; # in KB
+       $rootsize = int($os_size - $minfree - $swap_size); # in KB
        $rootsize &= ~0xFFF; # align down to 4 MB boundaries
     }
 
@@ -1185,7 +610,7 @@ sub create_lvm_volumes {
        syscmd("/sbin/lvconvert --yes --type thin-pool --poolmetadatasize ${metadatasize}K $vgname/data") == 0 ||
            die "unable to create data thin-pool\n";
     } else {
-       if  ($setup->{product} eq 'pve' && !defined($config_options->{maxvz})) {
+       if ($env->{product} eq 'pve' && !defined($config_options->{maxvz})) {
            display_message("Skipping auto-creation of LVM thinpool for guest data due to low space.");
        }
        $datadev = undef;
@@ -1202,9 +627,9 @@ sub compute_swapsize {
 
     my $hdgb = int($hdsize/(1024*1024));
 
-    my $swapsize;
+    my $swapsize_kb;
     if (defined($config_options->{swapsize})) {
-       $swapsize = $config_options->{swapsize} * 1024 * 1024;
+       $swapsize_kb = $config_options->{swapsize} * 1024 * 1024;
     } else {
        my $ss = int($total_memory);
        $ss = 4096 if $ss < 4096 && $hdgb >= 64;
@@ -1213,10 +638,10 @@ sub compute_swapsize {
        $ss = 512 if $ss < 512;
        $ss = int($hdgb * 128) if $ss > $hdgb * 128;
        $ss = 8192 if $ss > 8192;
-       $swapsize = $ss * 1024;
+       $swapsize_kb = int($ss * 1024) & ~0xFFF; # align to 4 MB to avoid all to odd SWAP size
     }
 
-    return $swapsize;
+    return $swapsize_kb;
 }
 
 my sub chroot_chown {
@@ -1289,12 +714,8 @@ sub extract_data {
 
     my $bootdevinfo = [];
 
-    my $swapfile;
-    my $rootdev;
-    my $datadev;
-
-    my $use_zfs = 0;
-    my $use_btrfs = 0;
+    my ($swapfile, $rootdev, $datadev);
+    my ($use_zfs, $use_btrfs) = (0, 0);
 
     my $filesys = $config_options->{filesys};
 
@@ -1325,11 +746,12 @@ sub extract_data {
 
        update_progress(0, 0, $maxper, "cleanup root-disks");
 
-       syscmd("vgchange -an") if !$opt_testmode; # deactivate all detected VGs
+       syscmd("vgchange -an") if !is_test_mode(); # deactivate all detected VGs
 
-       if ($opt_testmode) {
+       if (is_test_mode()) {
 
-           $rootdev = abs_path($opt_testmode);
+           my $test_images = Proxmox::Install::ISOEnv::get_test_images();
+           $rootdev = abs_path($test_images->[0]); # FIXME: use all selected for test too!
            syscmd("umount $rootdev");
 
            if ($use_btrfs) {
@@ -1358,21 +780,20 @@ sub extract_data {
            my ($devlist, $btrfs_mode) = get_btrfs_raid_setup();
 
            foreach my $hd (@$devlist) {
-               $clean_disk->(@$hd[1]);
+               wipe_disk(@$hd[1]);
            }
 
            update_progress(0, 0.02, $maxper, "create partitions");
 
            my $btrfs_partitions = [];
-           my $disksize;
            foreach my $hd (@$devlist) {
                my $devname = @$hd[1];
                my $logical_bsize = @$hd[4];
 
-               my ($size, $osdev, $efidev) =
-                   partition_bootable_disk($devname, $config_options->{hdsize}, '8300');
+               my ($size, $osdev, $efidev) = partition_bootable_disk(
+                   $devname, $config_options->{hdsize}, '8300');
                $rootdev = $osdev if !defined($rootdev); # simply point to first disk
-               my $by_id = find_stable_path("/dev/disk/by-id", $devname);
+               my $by_id = Proxmox::Sys::Block::get_disk_by_id_path($devname);
                push @$bootdevinfo, {
                    esp => $efidev,
                    devname => $devname,
@@ -1381,10 +802,9 @@ sub extract_data {
                    logical_bsize => $logical_bsize,
                };
                push @$btrfs_partitions, $osdev;
-               $disksize = $size;
            }
 
-           $udevadm_trigger_block->();
+           Proxmox::Sys::Block::udevadm_trigger_block();
 
            update_progress(0, 0.03, $maxper, "create btrfs");
 
@@ -1395,13 +815,12 @@ sub extract_data {
            my ($devlist, $vdev) = get_zfs_raid_setup();
 
            foreach my $hd (@$devlist) {
-               $clean_disk->(@$hd[1]);
+               wipe_disk(@$hd[1]);
            }
 
            update_progress(0, 0.02, $maxper, "create partitions");
 
            # install esp/boot part on all, we can only win!
-           my $disksize;
            for my $hd (@$devlist) {
                my $devname = @$hd[1];
                my $logical_bsize = @$hd[4];
@@ -1415,23 +834,22 @@ sub extract_data {
                    osdev => $osdev,
                    logical_bsize => $logical_bsize,
                };
-               $disksize = $size;
            }
 
-           $udevadm_trigger_block->();
+           Proxmox::Sys::Block::udevadm_trigger_block();
 
            foreach my $di (@$bootdevinfo) {
                my $devname = $di->{devname};
-               $di->{by_id} = find_stable_path ("/dev/disk/by-id", $devname);
+               $di->{by_id} = Proxmox::Sys::Block::get_disk_by_id_path($devname);
 
-               my $osdev = find_stable_path ("/dev/disk/by-id", $di->{osdev}) || $di->{osdev};
+               my $osdev = Proxmox::Sys::Block::get_disk_by_id_path($di->{osdev}) || $di->{osdev};
 
                $vdev =~ s/ $devname/ $osdev/;
            }
 
            foreach my $hd (@$devlist) {
                my $devname = @$hd[1];
-               my $by_id = find_stable_path ("/dev/disk/by-id", $devname);
+               my $by_id = Proxmox::Sys::Block::get_disk_by_id_path($devname);
 
                $vdev =~ s/ $devname/ $by_id/ if $by_id;
            }
@@ -1444,19 +862,18 @@ sub extract_data {
 
            die "target '$target_hd' is not a valid block device\n" if ! -b $target_hd;
 
-           $clean_disk->($target_hd);
+           wipe_disk($target_hd);
 
            update_progress(0, 0.02, $maxper, "create partitions");
 
-           my $logical_bsize = logical_blocksize($target_hd);
+           my $logical_bsize = Proxmox::Sys::Block::logical_blocksize($target_hd);
 
-           my ($os_size, $osdev, $efidev);
-           ($os_size, $osdev, $efidev) =
-               partition_bootable_disk($target_hd, $config_options->{hdsize}, '8E00');
+           my ($os_size, $osdev, $efidev) =
+               partition_bootable_disk($target_hd, $config_options->{hdsize}, '8E00');
 
-           &$udevadm_trigger_block();
+           Proxmox::Sys::Block::udevadm_trigger_block();
 
-           my $by_id = find_stable_path ("/dev/disk/by-id", $target_hd);
+           my $by_id = Proxmox::Sys::Block::get_disk_by_id_path($target_hd);
            push @$bootdevinfo, {
                esp => $efidev,
                devname => $target_hd,
@@ -1472,7 +889,7 @@ sub extract_data {
                create_lvm_volumes($osdev, $os_size, $swap_size);
 
            # trigger udev to create /dev/disk/by-uuid
-           &$udevadm_trigger_block(1);
+           Proxmox::Sys::Block::udevadm_trigger_block(1);
        }
 
        if ($use_zfs) {
@@ -1524,7 +941,7 @@ sub extract_data {
        mkdir "$targetdir/var";
        mkdir "$targetdir/var/lib";
 
-       if ($setup->{product} eq 'pve') {
+       if ($env->{product} eq 'pve') {
            mkdir "$targetdir/var/lib/vz";
            mkdir "$targetdir/var/lib/pve";
 
@@ -1544,7 +961,7 @@ sub extract_data {
        my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size) = stat ($basefile);
        $ino || die "unable to open file '$basefile' - $!\n";
 
-       my $files = file_read_firstline("${proxmox_cddir}/proxmox/$setup->{product}-base.cnt") ||
+       my $files = file_read_firstline("${proxmox_cddir}/proxmox/$env->{product}-base.cnt") ||
            die "unable to read base file count\n";
 
        my $per = 0;
@@ -1591,11 +1008,11 @@ sub extract_data {
            "ff02::2 ip6-allrouters\n" .
            "ff02::3 ip6-allhosts\n";
 
-       write_config($hosts, "$targetdir/etc/hosts");
+       file_write_all("$targetdir/etc/hosts", $hosts);
 
-       write_config("$hostname\n", "$targetdir/etc/hostname");
+       file_write_all("$targetdir/etc/hostname", "$hostname\n");
 
-       syscmd("/bin/hostname $hostname") if !$opt_testmode;
+       syscmd("/bin/hostname $hostname") if !is_test_mode();
 
        # configure interfaces
 
@@ -1605,7 +1022,7 @@ sub extract_data {
 
        my $ethdev = $ipconf->{ifaces}->{$ipconf->{selected}}->{name};
 
-       if ($setup->{bridged_network}) {
+       if ($env->{cfg}->{bridged_network}) {
            $ifaces .= "iface $ethdev $ntype manual\n";
 
            $ifaces .=
@@ -1629,12 +1046,12 @@ sub extract_data {
            $ifaces .= "\niface $name $ntype manual\n";
        }
 
-       write_config($ifaces, "$targetdir/etc/network/interfaces");
+       file_write_all("$targetdir/etc/network/interfaces", $ifaces);
 
        # configure dns
 
        my $resolvconf = "search $domain\nnameserver $dnsserver\n";
-       write_config($resolvconf, "$targetdir/etc/resolv.conf");
+       file_write_all("$targetdir/etc/resolv.conf", $resolvconf);
 
        # configure fstab
 
@@ -1671,7 +1088,7 @@ sub extract_data {
 
                if ($di->{esp}) {
                    my $efi_boot_uuid = $di->{esp};
-                   if (my $uuid = find_dev_by_uuid ($di->{esp})) {
+                   if (my $uuid = Proxmox::Sys::Block::get_dev_uuid($di->{esp})) {
                        $efi_boot_uuid = "UUID=$uuid";
                    }
 
@@ -1685,14 +1102,12 @@ sub extract_data {
 
        $fstab .= "proc /proc proc defaults 0 0\n";
 
-       write_config($fstab, "$targetdir/etc/fstab");
-       write_config("", "$targetdir/etc/mtab");
+       file_write_all("$targetdir/etc/fstab", $fstab);
+       file_write_all("$targetdir/etc/mtab", "");
 
-       syscmd("cp ${proxmox_libdir}/policy-disable-rc.d " .
-               "$targetdir/usr/sbin/policy-rc.d") == 0 ||
+       syscmd("cp ${proxmox_libdir}/policy-disable-rc.d $targetdir/usr/sbin/policy-rc.d") == 0 ||
                die "unable to copy policy-rc.d\n";
-       syscmd("cp ${proxmox_libdir}/fake-start-stop-daemon " .
-               "$targetdir/sbin/") == 0 ||
+       syscmd("cp ${proxmox_libdir}/fake-start-stop-daemon $targetdir/sbin/") == 0 ||
                die "unable to copy start-stop-daemon\n";
 
        diversion_add($targetdir, "/sbin/start-stop-daemon", "/sbin/fake-start-stop-daemon");
@@ -1701,7 +1116,7 @@ sub extract_data {
 
        my $machine_id = run_command("systemd-id128 new");
        die "unable to create a new machine-id\n" if ! $machine_id;
-       write_config($machine_id, "$targetdir/etc/machine-id");
+       file_write_all("$targetdir/etc/machine-id", $machine_id);
 
        syscmd("cp /etc/hostid $targetdir/etc/") == 0 ||
            die "unable to copy hostid\n";
@@ -1715,7 +1130,7 @@ sub extract_data {
        }
 
        # Note: keyboard-configuration/xbkb-keymap is used by console-setup
-       my $xkmap = $cmap->{kmap}->{$keymap}->{x11} // 'us';
+       my $xkmap = $env->{locales}->{kmap}->{$keymap}->{x11} // 'us';
 
        debconfig_set ($targetdir, <<_EOD);
 locales locales/default_environment_locale select en_US.UTF-8
@@ -1760,7 +1175,7 @@ _EOD
 
        unlink "$targetdir/etc/mailname";
        $postfix_main_cf =~ s/__FQDN__/${hostname}.${domain}/;
-       write_config($postfix_main_cf, "$targetdir/etc/postfix/main.cf");
+       file_write_all("$targetdir/etc/postfix/main.cf", $postfix_main_cf);
 
        # make sure we have all postfix directories
        syscmd("chroot $targetdir /usr/sbin/postfix check");
@@ -1774,22 +1189,22 @@ _EOD
        # set timezone
        unlink ("$targetdir/etc/localtime");
        symlink ("/usr/share/zoneinfo/$timezone", "$targetdir/etc/localtime");
-       write_config("$timezone\n", "$targetdir/etc/timezone");
+       file_write_all("$targetdir/etc/timezone", "$timezone\n");
 
        # set apt mirror
-       if (my $mirror = $cmap->{country}->{$country}->{mirror}) {
+       if (my $mirror = $env->{locales}->{country}->{$country}->{mirror}) {
            my $fn = "$targetdir/etc/apt/sources.list";
            syscmd("sed -i 's/ftp\\.debian\\.org/$mirror/' '$fn'");
        }
 
        # create extended_states for apt (avoid cron job warning if that
        # file does not exist)
-       write_config('', "$targetdir/var/lib/apt/extended_states");
+       file_write_all("$targetdir/var/lib/apt/extended_states", '');
 
        # allow ssh root login
        syscmd(['sed', '-i', 's/^#\?PermitRootLogin.*/PermitRootLogin yes/', "$targetdir/etc/ssh/sshd_config"]);
 
-       if ($setup->{product} eq 'pmg') {
+       if ($env->{product} eq 'pmg') {
            # install initial clamav DB
            my $srcdir = "${proxmox_cddir}/proxmox/clamav";
            foreach my $fn ("main.cvd", "bytecode.cvd", "daily.cvd", "safebrowsing.cvd") {
@@ -1800,7 +1215,7 @@ _EOD
                die "unable to set owner for clamav database files\n";
        }
 
-       if ($setup->{product} eq 'pve') {
+       if ($env->{product} eq 'pve') {
            # save installer settings
            my $ucc = uc ($country);
            debconfig_set($targetdir, "pve-manager pve-manager/country string $ucc\n");
@@ -1811,9 +1226,9 @@ _EOD
        if ($use_zfs) {
            # add ZFS options while preserving existing kernel cmdline
            my $zfs_snippet = "GRUB_CMDLINE_LINUX=\"\$GRUB_CMDLINE_LINUX root=ZFS=$zfspoolname/ROOT/$zfsrootvolname boot=zfs\"";
-           write_config($zfs_snippet, "$targetdir/etc/default/grub.d/zfs.cfg");
+           file_write_all("$targetdir/etc/default/grub.d/zfs.cfg", $zfs_snippet);
 
-           write_config("root=ZFS=$zfspoolname/ROOT/$zfsrootvolname boot=zfs\n", "$targetdir/etc/kernel/cmdline");
+           file_write_all("$targetdir/etc/kernel/cmdline", "root=ZFS=$zfspoolname/ROOT/$zfsrootvolname boot=zfs\n");
 
        }
 
@@ -1829,7 +1244,7 @@ _EOD
        }
        die "unable to detect kernel version\n" if !defined($kapi);
 
-       if (!$opt_testmode) {
+       if (!is_test_mode()) {
 
            unlink ("$targetdir/etc/mtab");
            symlink ("/proc/mounts", "$targetdir/etc/mtab");
@@ -1858,13 +1273,11 @@ _EOD
                            push @$bootloader_err_list, $@ if $@;
                        }
 
-                   eval {
                        if (my $esp = $di->{esp}) {
-                               prepare_grub_efi_boot_esp($dev, $esp, $targetdir);
-                           }
+                           eval { prepare_grub_efi_boot_esp($dev, $esp, $targetdir) };
+                           push @$bootloader_err_list, $@ if $@;
                        }
-                   };
-                   push @$bootloader_err_list, $@ if $@;
+                   }
                }
 
                syscmd("chroot $targetdir /usr/sbin/update-grub") == 0 ||
@@ -1892,12 +1305,11 @@ _EOD
        run_command("chroot $targetdir /usr/sbin/chpasswd", undef,
                    "root:$octets\n");
 
-       if ($setup->{product} eq 'pmg') {
+       if ($env->{product} eq 'pmg') {
            # save admin email
-           write_config("section: admin\n\temail ${mailto}\n",
-                        "$targetdir/etc/pmg/pmg.conf");
+           file_write_all("$targetdir/etc/pmg/pmg.conf", "section: admin\n\temail ${mailto}\n");
 
-       } elsif ($setup->{product} eq 'pve') {
+       } elsif ($env->{product} eq 'pve') {
 
            # create pmxcfs DB
 
@@ -1905,29 +1317,29 @@ _EOD
            mkdir $tmpdir;
 
            # write vnc keymap to datacenter.cfg
-           my $vnckmap = $cmap->{kmap}->{$keymap}->{kvm} || 'en-us';
-           write_config("keyboard: $vnckmap\n",
-                        "$tmpdir/datacenter.cfg");
+           my $vnckmap = $env->{locales}->{kmap}->{$keymap}->{kvm} || 'en-us';
+           file_write_all("$tmpdir/datacenter.cfg", "keyboard: $vnckmap\n");
 
            # save admin email
-           write_config("user:root\@pam:1:0:::${mailto}::\n", "$tmpdir/user.cfg");
+           file_write_all("$tmpdir/user.cfg", "user:root\@pam:1:0:::${mailto}::\n");
 
            # write storage.cfg
-           my $storage_cfg_fn = "$tmpdir/storage.cfg";
+           my $storage_cfg;
            if ($use_zfs) {
-               write_config($storage_cfg_zfs, $storage_cfg_fn);
+               $storage_cfg = $storage_cfg_zfs;
            } elsif ($use_btrfs) {
-               write_config($storage_cfg_btrfs, $storage_cfg_fn);
+               $storage_cfg = $storage_cfg_btrfs;
            } elsif ($datadev) {
-               write_config($storage_cfg_lvmthin, $storage_cfg_fn);
+               $storage_cfg = $storage_cfg_lvmthin;
            } else {
-               write_config($storage_cfg_local, $storage_cfg_fn);
+               $storage_cfg = $storage_cfg_local;
            }
+           file_write_all("$tmpdir/storage.cfg", $storage_cfg);
 
            run_command("chroot $targetdir /usr/bin/create_pmxcfs_db /tmp/pve /var/lib/pve-cluster/config.db");
 
            syscmd("rm -rf $tmpdir");
-       } elsif ($setup->{product} eq 'pbs') {
+       } elsif ($env->{product} eq 'pbs') {
            my $base_cfg_path = "/etc/proxmox-backup";
            mkdir "$targetdir/$base_cfg_path";
 
@@ -1935,7 +1347,7 @@ _EOD
            chroot_chmod($targetdir, $base_cfg_path, mode => '0700');
 
            my $user_cfg_fn = "$base_cfg_path/user.cfg";
-           write_config("user: root\@pam\n\temail ${mailto}\n", "$targetdir/$user_cfg_fn");
+           file_write_all("$targetdir/$user_cfg_fn", "user: root\@pam\n\temail ${mailto}\n");
            chroot_chown($targetdir, $user_cfg_fn, user => 'root', group => 'backup');
            chroot_chmod($targetdir, $user_cfg_fn, mode => '0640');
        }
@@ -1947,7 +1359,7 @@ _EOD
 
     print $err if $err;
 
-    if ($opt_testmode) {
+    if (is_test_mode()) {
        my $elapsed = Time::HiRes::tv_interval($starttime);
        print "Elapsed extract time: $elapsed\n";
 
@@ -1977,8 +1389,8 @@ _EOD
        syscmd("zfs set mountpoint=/ $zfspoolname/ROOT/$zfsrootvolname") == 0 ||
            die "zfs set mountpoint failed\n";
 
-       syscmd("zpool set bootfs=$zfspoolname/ROOT/$zfsrootvolname $zfspoolname")  == 0 ||
-           die "zfs set bootfs failed\n";
+       syscmd("zpool set bootfs=$zfspoolname/ROOT/$zfsrootvolname $zfspoolname") == 0 ||
+           die "zpool set bootfs failed\n";
        syscmd("zpool export $zfspoolname");
     }
 
@@ -1998,7 +1410,7 @@ my $display_info_items = [
     "extract2-rulesystem.htm",
     "extract3-spam.htm",
     "extract4-virus.htm",
-    ];
+];
 
 sub display_info {
 
@@ -2022,18 +1434,18 @@ sub display_html {
 
     my $htmldir = "${proxmox_libdir}/html";
     my $path;
-    if (-f "$htmldir/$setup->{product}/$filename") {
-       $path = "$htmldir/$setup->{product}/$filename";
+    if (-f "$htmldir/$env->{product}/$filename") {
+       $path = "$htmldir/$env->{product}/$filename";
     } else {
        $path = "$htmldir/$filename";
     }
 
-    my $data = file_get_contents($path);
+    my $data = file_read_all($path);
 
     if ($filename eq 'license.htm') {
-       my $license = eval { decode('utf8', file_get_contents("${proxmox_cddir}/EULA")) };
+       my $license = eval { decode('utf8', file_read_all("${proxmox_cddir}/EULA")) };
        if (my $err = $@) {
-           die $err if !$opt_testmode;
+           die $err if !is_test_mode();
            $license = "TESTMODE: Ignore non existent EULA...\n";
        }
        my $title = "END USER LICENSE AGREEMENT (EULA)";
@@ -2042,14 +1454,14 @@ sub display_html {
     } elsif ($filename eq 'success.htm') {
        my $addr = $ipversion == 6 ? "[${ipaddress}]" : "$ipaddress";
        $data =~ s/__IPADDR__/$addr/g;
-       $data =~ s/__PORT__/$setup->{port}/g;
+       $data =~ s/__PORT__/$env->{cfg}->{port}/g;
 
        my $autoreboot_msg = $config_options->{autoreboot}
            ? "Automatic reboot scheduled in $autoreboot_seconds seconds."
            : '';
        $data =~ s/__AUTOREBOOT_MSG__/$autoreboot_msg/;
     }
-    $data =~ s/__FULL_PRODUCT_NAME__/$setup->{fullname}/g;
+    $data =~ s/__FULL_PRODUCT_NAME__/$env->{cfg}->{fullname}/g;
 
     # always set base-path to common path, all resources are accesible from there.
     $htmlview->load_html($data,  "file://$htmldir/");
@@ -2086,27 +1498,35 @@ sub create_main_window {
 
     $window = Gtk3::Window->new();
     $window->set_default_size(1024, 768);
-    $window->set_has_resize_grip(0);
-    $window->fullscreen() if !$opt_testmode;
-    $window->set_decorated(0) if !$opt_testmode;
+    $window->signal_connect(map => sub { $window->set_resizable(0); });
+    $window->fullscreen() if !is_test_mode();
+    $window->set_decorated(0) if !is_test_mode();
+    $window->signal_connect(destroy => sub { Gtk3->main_quit(); });
 
-    my $vbox = Gtk3::VBox->new(0, 0);
+    my $vbox = Gtk3::Box->new('vertical', 0);
 
-    my $logofn = "$setup->{product}-banner.png";
+    my $logofn = "$env->{product}-banner.png";
     my $image = Gtk3::Image->new_from_file("${proxmox_libdir}/$logofn");
+
+    my $provider = Gtk3::CssProvider->new();
+    my $theming = "* {\nbackground: #171717;\n}";
+    $provider->load_from_data ([map ord, split //, $theming]);
+    my $context = $image->get_style_context();
+    $context->add_provider($provider, 600);
+
     $vbox->pack_start($image, 0, 0, 0);
 
-    my $hbox = Gtk3::HBox->new(0, 0);
+    my $hbox = Gtk3::Box->new('horizontal', 0);
     $vbox->pack_start($hbox, 1, 1, 0);
 
  #  my $f1 = Gtk3::Frame->new ('test');
  #  $f1->set_shadow_type ('none');
  #  $hbox->pack_start ($f1, 1, 1, 0);
 
-    my $sep1 = Gtk3::HSeparator->new();
+    my $sep1 = Gtk3::Separator->new('horizontal');
     $vbox->pack_start($sep1, 0, 0, 0);
 
-    $cmdbox = Gtk3::HBox->new();
+    $cmdbox = Gtk3::Box->new('horizontal', 0);
     $vbox->pack_start($cmdbox, 0, 0, 10);
 
     $next = Gtk3::Button->new('_Next');
@@ -2122,33 +1542,34 @@ sub create_main_window {
     my $abort = Gtk3::Button->new('_Abort');
     $abort->set_can_focus(0);
     $cmdbox->pack_start($abort, 0, 0, 10);
-    $abort->signal_connect(clicked => sub { exit (-1); });
+    $abort->signal_connect(clicked => sub { app_quit(-1); });
 
-    my $vbox2 = Gtk3::VBox->new(0, 0);
+    my $vbox2 = Gtk3::Box->new('vertical', 0);
     $hbox->add($vbox2);
 
     $htmlview = Gtk3::WebKit2::WebView->new();
+    $htmlview->set_hexpand(1);
     my $scrolls = Gtk3::ScrolledWindow->new();
     $scrolls->add($htmlview);
 
-    my $hbox2 = Gtk3::HBox->new(0, 0);
+    my $hbox2 = Gtk3::Box->new('horizontal', 0);
     $hbox2->pack_start($scrolls, 1, 1, 0);
 
     $vbox2->pack_start($hbox2, 1, 1, 0);
 
-    my $vbox3 = Gtk3::VBox->new(0, 0);
+    my $vbox3 = Gtk3::Box->new('vertical', 0);
     $vbox2->pack_start($vbox3, 0, 0, 0);
 
-    my $sep2 = Gtk3::HSeparator->new;
+    my $sep2 = Gtk3::Separator->new('horizontal');
     $vbox3->pack_start($sep2, 0, 0, 0);
 
-    $inbox = Gtk3::HBox->new(0, 0);
+    $inbox = Gtk3::Box->new('horizontal', 0);
     $vbox3->pack_start($inbox, 0, 0, 0);
 
     $window->add($vbox);
 
     $window->show_all;
-    $window->realize();
+    $window->present();
 }
 
 sub cleanup_view {
@@ -2202,7 +1623,7 @@ sub create_text_input {
 
     my $label = Gtk3::Label->new($text);
     $label->set_size_request(150, -1);
-    $label->set_alignment(1, 0.5);
+    $label->set_xalign(1.0);
     $hbox->pack_start($label, 0, 0, 10);
     my $e1 = Gtk3::Entry->new();
     $e1->set_width_chars(35);
@@ -2218,7 +1639,7 @@ sub create_cidr_inputs {
 
     my $label = Gtk3::Label->new('IP Address (CIDR)');
     $label->set_size_request(150, -1);
-    $label->set_alignment(1, 0.5);
+    $label->set_xalign(1.0);
     $hbox->pack_start($label, 0, 0, 10);
 
     my $ip_el = Gtk3::Entry->new();
@@ -2228,7 +1649,6 @@ sub create_cidr_inputs {
 
     $label = Gtk3::Label->new('/');
     $label->set_size_request(10, -1);
-    $label->set_alignment(0.5, 0.5);
     $hbox->pack_start($label, 0, 0, 2);
 
     my $cidr_el = Gtk3::Entry->new();
@@ -2239,70 +1659,6 @@ sub create_cidr_inputs {
     return ($hbox, $ip_el, $cidr_el);
 }
 
-sub get_ip_config {
-
-    my $ifaces = {};
-    my $default;
-
-    my $links = `ip -o l`;
-    foreach my $l (split /\n/,$links) {
-       my ($index, $name, $flags, $state, $mac) = $l =~ m/^(\d+):\s+(\S+):\s+<(\S+)>.*\s+state\s+(\S+)\s+.*\s+link\/ether\s+(\S+)\s+/;
-       next if !$name || $name eq 'lo';
-
-       my $driver = readlink "/sys/class/net/$name/device/driver" || 'unknown';
-       $driver =~ s!^.*/!!;
-
-       $ifaces->{"$index"} = {
-           name => $name,
-           driver => $driver,
-           flags => $flags,
-           state => $state,
-           mac => $mac,
-       };
-
-       my $addresses = `ip -o a s $name`;
-       foreach my $a (split /\n/,$addresses) {
-           my ($family, $ip, $prefix) = $a =~ m/^\Q$index\E:\s+\Q$name\E\s+(inet|inet6)\s+($IPRE)\/(\d+)\s+/;
-           next if !$ip;
-           next if $a =~ /scope\s+link/; # ignore link local
-
-           my $mask = $prefix;
-
-           if ($family eq 'inet') {
-               next if !$ip =~ /$IPV4RE/;
-               next if $prefix < 8 || $prefix > 32;
-               $mask = @$ipv4_reverse_mask[$prefix];
-           } else {
-               next if !$ip =~ /$IPV6RE/;
-           }
-
-           $default = $index if !$default;
-
-           $ifaces->{"$index"}->{"$family"} = {
-               prefix => $prefix,
-               mask => $mask,
-               addr => $ip,
-           };
-       }
-    }
-
-
-    my $route = `ip route`;
-    my ($gateway) = $route =~ m/^default\s+via\s+(\S+)\s+/m;
-
-    my $resolvconf = `cat /etc/resolv.conf`;
-    my ($dnsserver) = $resolvconf =~ m/^nameserver\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/m;
-    my ($domain) = $resolvconf =~ m/^domain\s+(\S+)$/m;
-
-    return {
-       default => $default,
-       ifaces => $ifaces,
-       gateway => $gateway,
-       dnsserver => $dnsserver,
-       domain => $domain,
-    }
-}
-
 sub display_message {
     my ($msg) = @_;
 
@@ -2396,16 +1752,16 @@ sub create_ipconf_view {
        $device_cb->set_active(0);
     }
 
-    my $devicebox = Gtk3::HBox->new(0, 0);
+    my $devicebox = Gtk3::Box->new('horizontal', 0);
     my $label = Gtk3::Label->new("Management Interface:");
     $label->set_size_request(150, -1);
-    $label->set_alignment(1, 0.5);
+    $label->set_xalign(1.0);
     $devicebox->pack_start($label, 0, 0, 10);
     $devicebox->pack_start($device_cb, 0, 0, 0);
 
     $vbox->pack_start($devicebox, 0, 0, 2);
 
-    my $hn = $config->{fqdn} //  "$setup->{product}." . ($ipconf->{domain} // "example.invalid");
+    my $hn = $config->{fqdn} // "$env->{product}." . ($ipconf->{domain} // "example.invalid");
 
     my ($hostbox, $hostentry) = create_text_input($hn, 'Hostname (FQDN):');
     $vbox->pack_start($hostbox, 0, 0, 2);
@@ -2460,17 +1816,9 @@ sub create_ipconf_view {
        }
 
        # verify ip address
-
        $text = $ipconf_entry_addr->get_text();
-       $text =~ s/^\s+//;
-       $text =~ s/\s+$//;
-       if ($text =~ m!^($IPV4RE)$!) {
-           $ipaddress = $text;
-           $ipversion = 4;
-       } elsif ($text =~ m!^($IPV6RE)$!) {
-           $ipaddress = $text;
-           $ipversion = 6;
-       } else {
+       ($ipaddress, $ipversion) = parse_ip_address($text);
+       if (!defined($ipaddress)) {
            display_message("IP address is not valid.");
            $ipconf_entry_addr->grab_focus();
            return;
@@ -2478,16 +1826,8 @@ sub create_ipconf_view {
        $config->{ipaddress} = $ipaddress;
 
        $text = $ipconf_entry_mask->get_text();
-       $text =~ s/^\s+//;
-       $text =~ s/\s+$//;
-       if ($ipversion == 6 && ($text =~ m/^(\d+)$/) && $1 >= 8 && $1 <= 126) {
-           $netmask = $text;
-       } elsif ($ipversion == 4 && ($text =~ m/^(\d+)$/) && $1 >= 8 && $1 <= 32) {
-           $netmask = $text;
-       } elsif ($ipversion == 4 && defined($ipv4_mask_hash->{$text})) {
-           # costs nothing to handle 255.x.y.z style masks, so continue to allow it
-           $netmask = $ipv4_mask_hash->{$text};
-       } else {
+       $netmask = parse_ip_mask($text, $ipversion);
+       if (!defined($netmask)) {
            display_message("Netmask is not valid.");
            $ipconf_entry_mask->grab_focus();
            return;
@@ -2496,32 +1836,28 @@ sub create_ipconf_view {
        $config->{netmask} = $netmask;
 
        $text = $ipconf_entry_gw->get_text();
-       $text =~ s/^\s+//;
-       $text =~ s/\s+$//;
-       if (($ipversion == 4) && ($text =~ m!^($IPV4RE)$!)) {
-           $gateway = $text;
-       } elsif (($ipversion == 6) && ($text =~ m!^($IPV6RE)$!)) {
-           $gateway = $text;
-       } else {
-           display_message("Gateway is not valid.");
+       my ($gateway_ip, $gateway_ip_version) = parse_ip_address($text);
+       if (!defined($gateway_ip) || $gateway_ip_version != $ipversion) {
+           my $msg = defined($gateway_ip)
+               ? "Gateway and host IP version must not differ (IPv$gateway_ip_version != IPv$ipversion)."
+               : "Gateway is not valid.";
+           display_message($msg);
            $ipconf_entry_gw->grab_focus();
            return;
        }
-       $config->{gateway} = $gateway;
+       $config->{gateway} = $gateway = $gateway_ip;
 
        $text = $ipconf_entry_dns->get_text();
-       $text =~ s/^\s+//;
-       $text =~ s/\s+$//;
-       if (($ipversion == 4) && ($text =~ m!^($IPV4RE)$!)) {
-           $dnsserver = $text;
-       } elsif (($ipversion == 6) && ($text =~ m!^($IPV6RE)$!)) {
-           $dnsserver = $text;
-       } else {
-           display_message("DNS server is not valid.");
+       my ($dns_ip, $dns_ip_version) = parse_ip_address($text);
+       if (!defined($dns_ip) || $dns_ip_version != $ipversion) {
+           my $msg = defined($gateway_ip)
+               ? "DNS and host IP version must not differ (IPv$gateway_ip_version != IPv$ipversion)."
+               : "DNS IP is not valid.";
+           display_message($msg);
            $ipconf_entry_dns->grab_focus();
            return;
        }
-       $config->{dnsserver} = $dnsserver;
+       $config->{dnsserver} = $dnsserver = $dns_ip;
 
        #print "TEST $ipaddress $netmask $gateway $dnsserver\n";
 
@@ -2536,7 +1872,7 @@ sub create_ack_view {
 
     cleanup_view();
 
-    my $vbox =  Gtk3::VBox->new(0, 0);
+    my $vbox =  Gtk3::Box->new('vertical', 0);
     $inbox->pack_start($vbox, 1, 0, 0);
 
     my $reboot_checkbox = Gtk3::CheckButton->new('Automatically reboot after successful installation');
@@ -2548,13 +1884,13 @@ sub create_ack_view {
     $vbox->pack_start($reboot_checkbox, 0, 0, 2);
 
     my $ack_template = "${proxmox_libdir}/html/ack_template.htm";
-    my $ack_html = "${proxmox_libdir}/html/$setup->{product}/$steps[$step_number]->{html}";
-    my $html_data = file_get_contents($ack_template);
+    my $ack_html = "${proxmox_libdir}/html/$env->{product}/$steps[$step_number]->{html}";
+    my $html_data = file_read_all($ack_template);
 
     my %config_values = (
        __target_hd__ => join(' | ', @{$config_options->{target_hds}}),
        __target_fs__ => $config_options->{filesys},
-       __country__ => $cmap->{country}->{$country}->{name},
+       __country__ => $env->{locales}->{country}->{$country}->{name},
        __timezone__ => $timezone,
        __keymap__ => $keymap,
        __mailto__ => $mailto,
@@ -2571,7 +1907,7 @@ sub create_ack_view {
        $html_data =~ s/$k/$v/g;
     }
 
-    write_config($html_data, $ack_html);
+    file_write_all($ack_html, $html_data);
 
     display_html();
 
@@ -2619,7 +1955,7 @@ sub update_layout {
     my $ind;
     my $def;
     my $i = 0;
-    my $kmaphash = $cmap->{kmaphash};
+    my $kmaphash = $env->{locales}->{kmaphash};
     foreach my $layout (sort keys %$kmaphash) {
        $def = $i if $kmaphash->{$layout} eq 'en-us';
        $ind = $i if $kmap && $kmaphash->{$layout} eq $kmap;
@@ -2640,39 +1976,32 @@ my $lastzonecb;
 sub update_zonelist {
     my ($box, $cc) = @_;
 
-    my $cczones = $cmap->{cczones};
-    my $zones = $cmap->{zones};
-
-    my $sel;
+    my $sel = $timezone; # initial default
     if ($lastzonecb) {
        $sel = $lastzonecb->get_active_text();
-       $box->remove ($lastzonecb);
-    } else {
-       $sel = $timezone; # used once to select default
+       $box->remove($lastzonecb);
     }
 
     my $cb = $lastzonecb = Gtk3::ComboBoxText->new();
     $cb->set_size_request(200, -1);
-
     $cb->signal_connect('changed' => sub {
        $timezone = $cb->get_active_text();
     });
 
-    my @za;
-    if ($cc && defined ($cczones->{$cc})) {
-       @za = keys %{$cczones->{$cc}};
-    } else {
-       @za = keys %$zones;
-    }
-    my $ind;
-    my $i = 0;
-    foreach my $zone (sort @za) {
-       $ind = $i if $sel && $zone eq $sel;
+    my ($cczones, $zones) = $env->{locales}->@{'cczones', 'zones'};
+    my @available_zones = $cc && defined($cczones->{$cc}) ? keys %{$cczones->{$cc}} : keys %$zones;
+
+    my ($i, $selected_index) = (0, undef);
+    for my $zone (sort @available_zones) {
+       $selected_index = $i if $sel && $zone eq $sel;
        $cb->append_text($zone);
        $i++;
     }
 
-    $cb->set_active($ind || 0);
+    # Append UTC here, so it is always the last item and never the default for any country.
+    $cb->append_text('UTC');
+
+    $cb->set_active($selected_index || 0);
 
     $cb->show;
     $box->pack_start($cb, 0, 0, 0);
@@ -2682,15 +2011,15 @@ sub create_password_view {
 
     cleanup_view();
 
-    my $vbox2 =  Gtk3::VBox->new(0, 0);
+    my $vbox2 =  Gtk3::Box->new('vertical', 0);
     $inbox->pack_start($vbox2, 1, 0, 0);
-    my $vbox =  Gtk3::VBox->new(0, 0);
+    my $vbox =  Gtk3::Box->new('vertical', 0);
     $vbox2->pack_start($vbox, 0, 0, 10);
 
-    my $hbox1 = Gtk3::HBox->new(0, 0);
+    my $hbox1 = Gtk3::Box->new('horizontal', 0);
     my $label = Gtk3::Label->new("Password");
     $label->set_size_request(150, -1);
-    $label->set_alignment(1, 0.5);
+    $label->set_xalign(1.0);
     $hbox1->pack_start($label, 0, 0, 10);
     my $pwe1 = Gtk3::Entry->new();
     $pwe1->set_visibility(0);
@@ -2698,10 +2027,10 @@ sub create_password_view {
     $pwe1->set_size_request(200, -1);
     $hbox1->pack_start($pwe1, 0, 0, 0);
 
-    my $hbox2 = Gtk3::HBox->new(0, 0);
+    my $hbox2 = Gtk3::Box->new('horizontal', 0);
     $label = Gtk3::Label->new("Confirm");
     $label->set_size_request(150, -1);
-    $label->set_alignment(1, 0.5);
+    $label->set_xalign(1.0);
     $hbox2->pack_start($label, 0, 0, 10);
     my $pwe2 = Gtk3::Entry->new();
     $pwe2->set_visibility(0);
@@ -2709,10 +2038,10 @@ sub create_password_view {
     $pwe2->set_size_request(200, -1);
     $hbox2->pack_start($pwe2, 0, 0, 0);
 
-    my $hbox3 = Gtk3::HBox->new(0, 0);
+    my $hbox3 = Gtk3::Box->new('horizontal', 0);
     $label = Gtk3::Label->new("Email");
     $label->set_size_request(150, -1);
-    $label->set_alignment(1, 0.5);
+    $label->set_xalign(1.0);
     $hbox3->pack_start($label, 0, 0, 10);
     my $eme = Gtk3::Entry->new();
     $eme->set_size_request(200, -1);
@@ -2775,12 +2104,11 @@ sub create_country_view {
 
     cleanup_view();
 
-    my $countryhash = $cmap->{countryhash};
-    my $ctr = $cmap->{country};
+    my $locales = $env->{locales};
 
-    my $vbox2 =  Gtk3::VBox->new(0, 0);
+    my $vbox2 =  Gtk3::Box->new('vertical', 0);
     $inbox->pack_start($vbox2, 1, 0, 0);
-    my $vbox =  Gtk3::VBox->new(0, 0);
+    my $vbox =  Gtk3::Box->new('vertical', 0);
     $vbox2->pack_start($vbox, 0, 0, 10);
 
     my $w = Gtk3::Entry->new();
@@ -2792,23 +2120,23 @@ sub create_country_view {
     $c->set_popup_set_width(1);
     $c->set_inline_completion(1);
 
-    my $hbox2 = Gtk3::HBox->new(0, 0);
+    my $hbox2 = Gtk3::Box->new('horizontal', 0);
     my $label = Gtk3::Label->new("Time zone");
     $label->set_size_request(150, -1);
-    $label->set_alignment(1, 0.5);
+    $label->set_xalign(1.0);
     $hbox2->pack_start($label, 0, 0, 10);
     update_zonelist ($hbox2);
 
-    my $hbox3 = Gtk3::HBox->new(0, 0);
+    my $hbox3 = Gtk3::Box->new('horizontal', 0);
     $label = Gtk3::Label->new("Keyboard Layout");
     $label->set_size_request(150, -1);
-    $label->set_alignment(1, 0.5);
+    $label->set_xalign(1.0);
     $hbox3->pack_start($label, 0, 0, 10);
 
     my $kmapcb = Gtk3::ComboBoxText->new();
     $kmapcb->set_size_request (200, -1);
-    foreach my $layout (sort keys %{$cmap->{kmaphash}}) {
-       $kmapcb->append_text ($layout);
+    for my $layout (sort keys %{$locales->{kmaphash}}) {
+       $kmapcb->append_text($layout);
     }
 
     update_layout($kmapcb);
@@ -2817,16 +2145,16 @@ sub create_country_view {
     $kmapcb->signal_connect ('changed' => sub {
        my $sel = $kmapcb->get_active_text();
        $last_layout = $kmapcb->get_active();
-       if (my $kmap = $cmap->{kmaphash}->{$sel}) {
-           my $xkmap = $cmap->{kmap}->{$kmap}->{x11};
-           my $xvar = $cmap->{kmap}->{$kmap}->{x11var};
+       if (my $kmap = $locales->{kmaphash}->{$sel}) {
+           my $xkmap = $locales->{kmap}->{$kmap}->{x11};
+           my $xvar = $locales->{kmap}->{$kmap}->{x11var};
            $keymap = $kmap;
 
            return if (defined($installer_kmap) && $installer_kmap eq $kmap);
 
            $installer_kmap = $keymap;
 
-           if (! $opt_testmode) {
+           if (!is_test_mode()) {
                syscmd ("setxkbmap $xkmap $xvar");
 
                my $kbd_config = qq{
@@ -2836,8 +2164,8 @@ sub create_country_view {
                };
                $kbd_config =~ s/^\s+//gm;
 
-               run_in_background( sub {
-                   write_config($kbd_config, '/etc/default/keyboard');
+               Proxmox::Sys::Command::run_in_background(sub {
+                   file_write_all('/etc/default/keyboard', $kbd_config);
                    system("setupcon");
                });
            }
@@ -2848,9 +2176,9 @@ sub create_country_view {
        my ($entry, $event) = @_;
        my $text = $entry->get_text;
 
-       if (my $cc = $countryhash->{lc($text)}) {
+       if (my $cc = $locales->{countryhash}->{lc($text)}) {
            update_zonelist($hbox2, $cc);
-           my $kmap = $ctr->{$cc}->{kmap} || 'en-us';
+           my $kmap = $locales->{country}->{$cc}->{kmap} || 'en-us';
            update_layout($kmapcb, $kmap);
        }
     });
@@ -2862,20 +2190,19 @@ sub create_country_view {
        my $val = $event->get_keyval;
 
        if ($val == Gtk3::Gdk::KEY_Tab) {
-           my $cc = $countryhash->{lc($text)};
+           my $cc = $locales->{countryhash}->{lc($text)};
 
            my $found = 0;
            my $compl;
 
            if ($cc) {
                $found = 1;
-               $compl = $ctr->{$cc}->{name};
+               $compl = $locales->{country}->{$cc}->{name};
            } else {
-               foreach my $cc (keys %$ctr) {
-                   my $ct = $ctr->{$cc}->{name};
-                   if ($ct =~ m/^\Q$text\E.*$/i) {
+               for my $country (values $locales->{country}->%*) {
+                   if ($country->{name} =~ m/^\Q$text\E.*$/i) {
                        $found++;
-                       $compl = $ct;
+                       $compl = $country->{name};
                    }
                    last if $found > 1;
                }
@@ -2901,19 +2228,20 @@ sub create_country_view {
        return undef;
     });
 
-    my $ls = Gtk3::ListStore->new('Glib::String');
-    foreach my $cc (sort {$ctr->{$a}->{name} cmp $ctr->{$b}->{name} } keys %$ctr) {
-       my $iter = $ls->append();
-       $ls->set ($iter, 0, $ctr->{$cc}->{name});
+    my $country_store = Gtk3::ListStore->new('Glib::String');
+    my $countries = $locales->{country};
+    for my $cc (sort { $countries->{$a}->{name} cmp $countries->{$b}->{name} } keys %$countries) {
+       my $iter = $country_store->append();
+       $country_store->set($iter, 0, $countries->{$cc}->{name});
     }
-    $c->set_model ($ls);
+    $c->set_model($country_store);
 
     $w->set_completion ($c);
 
-    my $hbox =  Gtk3::HBox->new(0, 0);
+    my $hbox =  Gtk3::Box->new('horizontal', 0);
 
     $label = Gtk3::Label->new("Country");
-    $label->set_alignment(1, 0.5);
+    $label->set_xalign(1.0);
     $label->set_size_request(150, -1);
     $hbox->pack_start($label, 0, 0, 10);
     $hbox->pack_start($w, 0, 0, 0);
@@ -2922,8 +2250,8 @@ sub create_country_view {
     $vbox->pack_start($hbox2, 0, 0, 5);
     $vbox->pack_start($hbox3, 0, 0, 5);
 
-    if ($country && $ctr->{$country}) {
-       $w->set_text ($ctr->{$country}->{name});
+    if ($country && (my $entry = $locales->{country}->{$country})) {
+       $w->set_text($entry->{name});
     }
 
     $inbox->show_all;
@@ -2933,7 +2261,7 @@ sub create_country_view {
 
        my $text = $w->get_text;
 
-       if (my $cc = $countryhash->{lc($text)}) {
+       if (my $cc = $locales->{countryhash}->{lc($text)}) {
            $country = $cc;
            $step_number++;
            create_password_view();
@@ -2977,7 +2305,7 @@ my $create_label_widget_grid = sub {
        my $widget = @$labeled_widgets[$i+1];
        my $label = Gtk3::Label->new(@$labeled_widgets[$i]);
        $label->set_visible(1);
-       $label->set_alignment (1, 0.5);
+       $label->set_xalign(1.0);
        $grid->attach($label, 0, $row, 1, 1);
        $widget->set_visible(1);
        $grid->attach($widget, 1, $row, 1, 1);
@@ -2993,8 +2321,9 @@ my $get_selected_hdsize = sub {
     return $hdsize if defined($hdsize);
 
     # compute the smallest disk size of the actually selected disks
-    my $hd_count = scalar(@$hds);
-    for (my $i = 0; $i < $hd_count; $i++) {
+    my $cached_disks = get_cached_disks();
+    my $disk_count = scalar(@$cached_disks);
+    for (my $i = 0; $i < $disk_count; $i++) {
        my $cur_hd = $config_options->{"disksel$i"} // next;
        my $disksize = int(@$cur_hd[2] / (2 * 1024 * 1024.0)); # size in GB
        $hdsize //= $disksize;
@@ -3040,17 +2369,18 @@ my sub get_hdsize_spin_button {
 my $create_raid_disk_grid = sub {
     my ($hdsize_buttons) = @_;
 
-    my $hd_count = scalar(@$hds);
+    my $cached_disks = get_cached_disks();
+    my $disk_count = scalar(@$cached_disks);
     my $disk_labeled_widgets = [];
-    for (my $i = 0; $i < $hd_count; $i++) {
+    for (my $i = 0; $i < $disk_count; $i++) {
        my $disk_selector = Gtk3::ComboBoxText->new();
        $disk_selector->append_text("-- do not use --");
        $disk_selector->set_active(0);
        $disk_selector->set_visible(1);
 
-       foreach my $hd (@$hds) {
+       for my $hd (@$cached_disks) {
            my ($disk, $devname, $size, $model, $logical_bsize) = @$hd;
-           $disk_selector->append_text(get_device_desc ($devname, $size, $model));
+           $disk_selector->append_text(get_device_desc($devname, $size, $model));
        }
 
        $disk_selector->{pve_disk_id} = $i;
@@ -3058,18 +2388,18 @@ my $create_raid_disk_grid = sub {
            my $w = shift;
            my $diskid = $w->{pve_disk_id};
            my $a = $w->get_active - 1;
-           $config_options->{"disksel${diskid}"} = ($a >= 0) ? $hds->[$a] : undef;
+           $config_options->{"disksel${diskid}"} = ($a >= 0) ? $cached_disks->[$a] : undef;
            for my $btn (@$hdsize_buttons) {
                update_hdsize_adjustment($btn->get_adjustment());
            }
        });
 
        if ($hdoption_first_setup) {
-           $disk_selector->set_active ($i+1) if $hds->[$i];
+           $disk_selector->set_active ($i+1) if $cached_disks->[$i];
        } else {
            my $hdind = 0;
            if (my $cur_hd = $config_options->{"disksel$i"}) {
-               foreach my $hd (@$hds) {
+               foreach my $hd (@$cached_disks) {
                    if (@$hd[1] eq @$cur_hd[1]) {
                        $disk_selector->set_active($hdind+1);
                        last;
@@ -3083,7 +2413,7 @@ my $create_raid_disk_grid = sub {
     }
 
     my $clear_all_button = Gtk3::Button->new('_Deselect All');
-    if ($hd_count > 3) {
+    if ($disk_count > 3) {
        $clear_all_button->signal_connect('clicked', sub {
            my $is_widget = 0;
            for my $disk_selector (@$disk_labeled_widgets) {
@@ -3096,7 +2426,7 @@ my $create_raid_disk_grid = sub {
 
     my $scrolled_window = Gtk3::ScrolledWindow->new();
     $scrolled_window->set_hexpand(1);
-    $scrolled_window->set_propagate_natural_height(1) if $hd_count > 4;
+    $scrolled_window->set_propagate_natural_height(1) if $disk_count > 4;
 
     my $diskgrid = $create_label_widget_grid->($disk_labeled_widgets);
 
@@ -3203,7 +2533,7 @@ sub create_hdoption_view {
 
     # Filesystem type
     my $label0 = Gtk3::Label->new("Filesystem");
-    $label0->set_alignment (1, 0.5);
+    $label0->set_xalign(1.0);
     $grid->attach($label0, 0, $row, 1, 1);
 
     my $fstypecb = Gtk3::ComboBoxText->new();
@@ -3218,7 +2548,7 @@ sub create_hdoption_view {
        'zfs (RAIDZ-3)',
     ];
     push @$fstype, 'btrfs (RAID0)', 'btrfs (RAID1)', 'btrfs (RAID10)'
-       if $setup->{enable_btrfs};
+       if $env->{cfg}->{enable_btrfs};
 
     my $tcount = 0;
     foreach my $tmp (@$fstype) {
@@ -3233,7 +2563,7 @@ sub create_hdoption_view {
 
     $row++;
 
-    my $sep = Gtk3::HSeparator->new();
+    my $sep = Gtk3::Separator->new('horizontal');
     $sep->set_visible(1);
     $grid->attach($sep, 0, $row, 2, 1);
     $row++;
@@ -3249,7 +2579,7 @@ sub create_hdoption_view {
     # size compute
     my $hdsize = 0;
     if ( -b $target_hd) {
-       $hdsize = int(hd_size ($target_hd) / (1024 * 1024.0)); # size in GB
+       $hdsize = int(Proxmox::Sys::Block::hd_size($target_hd) / (1024 * 1024.0)); # size in GB
     } elsif ($target_hd) {
        $hdsize = int((-s $target_hd) / (1024 * 1024 * 1024.0));
     }
@@ -3265,7 +2595,7 @@ sub create_hdoption_view {
     push @$hdsize_labeled_widgets, "swapsize", $entry_swapsize;
 
     my $entry_maxroot = Gtk3::Entry->new();
-    if ($setup->{product} eq 'pve') {
+    if ($env->{product} eq 'pve') {
        $entry_maxroot->set_tooltip_text("maximum size (GB) for LVM root volume");
        $entry_maxroot->signal_connect (key_press_event => \&check_float);
        $entry_maxroot->set_text($config_options->{maxroot}) if $config_options->{maxroot};
@@ -3279,7 +2609,7 @@ sub create_hdoption_view {
     push @$hdsize_labeled_widgets, "minfree", $entry_minfree;
 
     my $entry_maxvz;
-    if ($setup->{product} eq 'pve') {
+    if ($env->{product} eq 'pve') {
        $entry_maxvz = Gtk3::Entry->new();
        $entry_maxvz->set_tooltip_text("maximum size (GB) for LVM data volume");
        $entry_maxvz->signal_connect (key_press_event => \&check_float);
@@ -3320,7 +2650,7 @@ sub create_hdoption_view {
        if ($raid) {
            my $msg = "<b>Note</b>: " . ($is_zfs
                ? "ZFS is not compatible with hardware RAID controllers, for details see the documentation."
-               : "BTRFS integration in $setup->{fullname} is a technology preview!"
+               : "BTRFS integration in $env->{cfg}->{fullname} is a technology preview!"
            );
            $hw_raid_note->set_markup($msg);
        }
@@ -3354,7 +2684,7 @@ sub create_hdoption_view {
        &$switch_view();
     });
 
-    my $sep2 = Gtk3::HSeparator->new();
+    my $sep2 = Gtk3::Separator->new('horizontal');
     $sep2->set_visible(1);
     $contarea->pack_end($sep2, 1, 1, 10);
 
@@ -3415,8 +2745,9 @@ my $get_raid_devlist = sub {
 
     my $dev_name_hash = {};
 
+    my $cached_disks = get_cached_disks();
     my $devlist = [];
-    for (my $i = 0; $i < @$hds; $i++) {
+    for (my $i = 0; $i < @$cached_disks; $i++) {
        if (my $hd = $config_options->{"disksel$i"}) {
            my ($disk, $devname, $size, $model, $logical_bsize) = @$hd;
            die "device '$devname' is used more than once\n"
@@ -3439,7 +2770,7 @@ sub zfs_mirror_size_check {
 sub legacy_bios_4k_check {
     my ($lbs) = @_;
     die "Booting from 4kn drive in legacy BIOS mode is not supported.\n"
-       if (($boot_type ne 'efi') && ($lbs == 4096));
+       if $boot_type ne 'efi' && $lbs == 4096;
 }
 
 sub get_zfs_raid_setup {
@@ -3535,12 +2866,13 @@ sub create_hdsel_view {
 
     cleanup_view();
 
-    my $vbox =  Gtk3::VBox->new(0, 0);
+    my $vbox =  Gtk3::Box->new('vertical', 0);
     $inbox->pack_start($vbox, 1, 0, 0);
-    my $hbox =  Gtk3::HBox->new(0, 0);
+    my $hbox =  Gtk3::Box->new('horizontal', 0);
     $vbox->pack_start($hbox, 0, 0, 10);
 
-    my ($disk, $devname, $size, $model, $logical_bsize) = @{@$hds[0]};
+    my $cached_disks = get_cached_disks();
+    my ($disk, $devname, $size, $model, $logical_bsize) = $cached_disks->[0]->@*;
     $target_hd = $devname if !defined($target_hd);
 
     $target_hd_label = Gtk3::Label->new("Target Harddisk: ");
@@ -3548,7 +2880,7 @@ sub create_hdsel_view {
 
     $target_hd_combo = Gtk3::ComboBoxText->new();
 
-    foreach my $hd (@$hds) {
+    foreach my $hd ($cached_disks->@*) {
        ($disk, $devname, $size, $model, $logical_bsize) = @$hd;
        $target_hd_combo->append_text(get_device_desc($devname, $size, $model));
     }
@@ -3562,7 +2894,7 @@ sub create_hdsel_view {
     $target_hd_combo->set_active($last_hd_selected);
     $target_hd_combo->signal_connect(changed => sub {
        $a = shift->get_active;
-       my ($disk, $devname) = @{@$hds[$a]};
+       my ($disk, $devname) = @{@$cached_disks[$a]};
        $last_hd_selected = $a;
        $target_hd = $devname;
     });
@@ -3595,7 +2927,10 @@ sub create_hdsel_view {
            }
            $config_options->{target_hds} = [ map { $_->[1] } @$devlist ];
        } else {
-           eval { legacy_bios_4k_check(logical_blocksize($target_hd)) };
+           eval {
+               my $target_block_size = Proxmox::Sys::Block::logical_blocksize($target_hd);
+               legacy_bios_4k_check($target_block_size);
+           };
            if (my $err = $@) {
                display_message("Warning: $err\n");
                return;
@@ -3618,12 +2953,12 @@ sub create_extract_view {
     $prev_btn->set_sensitive(0);
     $prev_btn->hide();
 
-    my $vbox =  Gtk3::VBox->new(0, 0);
+    my $vbox =  Gtk3::Box->new('vertical', 0);
     $inbox->pack_start ($vbox, 1, 0, 0);
-    my $hbox =  Gtk3::HBox->new(0, 0);
+    my $hbox =  Gtk3::Box->new('horizontal', 0);
     $vbox->pack_start ($hbox, 0, 0, 10);
 
-    my $vbox2 =  Gtk3::VBox->new(0, 0);
+    my $vbox2 =  Gtk3::Box->new('vertical', 0);
     $hbox->pack_start ($vbox2, 0, 0, 0);
 
     $progress_status = Gtk3::Label->new('');
@@ -3637,16 +2972,16 @@ sub create_extract_view {
 
     $inbox->show_all();
 
-    my $tdir = $opt_testmode ? "target" : "/target";
+    my $tdir = is_test_mode() ? "target" : "/target";
     mkdir $tdir;
-    my $base = "${proxmox_cddir}/$setup->{product}-base.squashfs";
+    my $base = "${proxmox_cddir}/$env->{product}-base.squashfs";
 
     eval  { extract_data($base, $tdir); };
     my $err = $@;
 
     $next->set_sensitive(1);
 
-    set_next("_Reboot", sub { exit (0); } );
+    set_next("_Reboot", sub { app_quit(0); } );
 
     if ($err) {
        display_html("fail.htm");
@@ -3661,7 +2996,7 @@ sub create_extract_view {
                    $autoreboot_seconds--;
                    display_html("success.htm");
                } else {
-                   exit(0);
+                   app_quit(0);
                }
            });
        }
@@ -3676,17 +3011,17 @@ sub create_intro_view {
 
     if (int($total_memory) < 1024) {
        display_error("Less than 1 GiB of usable memory detected, installation will probably fail.\n\n".
-           "See 'System Requirements' in the $setup->{fullname} documentation.");
+           "See 'System Requirements' in the $env->{cfg}->{fullname} documentation.");
     }
 
-    if ($setup->{product} eq 'pve') {
-       eval {
-           my $cpuinfo = file_get_contents('/proc/cpuinfo');
-           if ($cpuinfo && !($cpuinfo =~ /^flags\s*:.*(vmx|svm)/m)) {
-               display_error("No support for KVM virtualization detected.\n\n" .
-                             "Check BIOS settings for Intel VT / AMD-V / SVM.")
-           }
-       };
+    if ($env->{product} eq 'pve') {
+       my $cpuinfo = eval { file_read_all('/proc/cpuinfo') };
+       if (!$cpuinfo || $cpuinfo !~ /^flags\s*:.*(vmx|svm)/m) {
+           display_error(
+               "No support for hardware-accelerated KVM virtualization detected.\n\n"
+               ."Check BIOS settings for Intel VT / AMD-V / SVM."
+           );
+       }
     }
 
     display_html();
@@ -3695,32 +3030,34 @@ sub create_intro_view {
     set_next("I a_gree", \&create_hdsel_view);
 }
 
-$ipconf = get_ip_config();
-
-$country = detect_country() if $ipconf->{default} || $opt_testmode;
+$ipconf = Proxmox::Sys::Net::get_ip_config();
 
-# read country, kmap and timezone infos
-$cmap = read_cmap();
+$country = detect_country() if $ipconf->{default} || is_test_mode();
 
-if (!defined($cmap->{country}->{$country})) {
-    print $logfd "ignoring detected country '$country', invalid or unknown\n";
+if (!defined($env->{locales}->{country}->{$country})) {
+    log_warn("ignoring detected country '$country', invalid or unknown\n");
     $country = undef;
 }
 
+Gtk3::init();
+
 create_main_window ();
 
 my $initial_error = 0;
 
-if (!defined ($hds) || (scalar (@$hds) <= 0)) {
-    print "no hardisks found\n";
-    $initial_error = 1;
-    display_html("nohds.htm");
-    set_next("Reboot", sub { exit(0); } );
-} else {
-    foreach my $hd (@$hds) {
-       my ($disk, $devname) = @$hd;
-       next if $devname =~ m|^/dev/md\d+$|;
-       print "found Disk$disk N:$devname\n";
+{
+    my $cached_disks = get_cached_disks();
+    if (!defined($cached_disks) || (scalar (@$cached_disks) <= 0)) {
+       print "no harddisks found\n";
+       $initial_error = 1;
+       display_html("nohds.htm");
+       set_next("Reboot", sub { app_quit(0); } );
+    } else {
+       foreach my $hd (@$cached_disks) {
+           my ($disk, $devname) = @$hd;
+           next if $devname =~ m|^/dev/md\d+$|;
+           print "found Disk$disk N:$devname\n";
+       }
     }
 }
 
@@ -3728,16 +3065,11 @@ if (!$initial_error && (scalar keys %{ $ipconf->{ifaces} } == 0)) {
     print "no network interfaces found\n";
     $initial_error = 1;
     display_html("nonics.htm");
-    set_next("Reboot", sub { exit(0); } );
+    set_next("Reboot", sub { app_quit(0); } );
 }
 
 create_intro_view () if !$initial_error;
 
 Gtk3->main;
 
-# reap left over zombie processes
-while ((my $child = waitpid(-1, POSIX::WNOHANG)) > 0) {
-    print "reaped child $child\n";
-}
-
-exit 0;
+app_quit(0);