]> git.proxmox.com Git - pve-installer.git/blobdiff - Proxmox/Install/RunEnv.pm
bump version to 8.2.6
[pve-installer.git] / Proxmox / Install / RunEnv.pm
index c5a47b249ab12b0754cef15caea39a97e91d5780..7eaf96a08af9475d54acdb4632d4a26c27dcc792 100644 (file)
@@ -7,7 +7,8 @@ use Carp;
 use JSON qw(from_json to_json);
 
 use Proxmox::Log;
-use Proxmox::Sys::File qw(file_read_firstline);
+use Proxmox::Sys::Command qw(run_command CMD_FINISHED);
+use Proxmox::Sys::File qw(file_read_all file_read_firstline);
 use Proxmox::Sys::Block;
 use Proxmox::Sys::Net;
 
@@ -18,6 +19,8 @@ my sub fromjs : prototype($) {
 }
 
 my $mem_total = undef;
+# Returns the system memory size in MiB, and falls back to 512 MiB if it
+# could not be determined.
 sub query_total_memory : prototype() {
     return $mem_total if defined($mem_total);
 
@@ -186,35 +189,37 @@ my sub detect_country_tracing_to : prototype($$) {
     my ($ipver, $destination) = @_;
 
     print STDERR "trying to detect country...\n";
-    open(my $TRACEROUTE_FH, '-|', 'traceroute', "-$ipver", '-N', '1', '-q', '1', '-n', $destination)
-       or return undef;
 
+    my $traceroute_cmd = ['traceroute', "-$ipver", '-N', '1', '-q', '1', '-n', $destination];
     my $geoip_bin = ($ipver == 6) ? 'geoiplookup6' : 'geoiplookup';
 
     my $country;
-
-    my $previous_alarm = alarm (10);
+    my $previous_alarm;
     eval {
        local $SIG{ALRM} = sub { die "timed out!\n" };
-       my $line;
-       while (defined ($line = <$TRACEROUTE_FH>)) {
+       $previous_alarm = alarm (10);
+
+       run_command($traceroute_cmd, sub {
+           my $line = shift;
+
            log_debug("DC TRACEROUTE: $line");
            if ($line =~ m/^\s*\d+\s+(\S+)\s/) {
                my $geoip = qx/$geoip_bin $1/;
                log_debug("DC GEOIP: $geoip");
+
                if ($geoip =~ m/GeoIP Country Edition:\s*([A-Z]+),/) {
                    $country = lc ($1);
                    log_info("DC FOUND: $country\n");
-                   last;
+                   return CMD_FINISHED;
                }
+               return;
            }
-       }
+       }, undef, undef, 1);
+
     };
     my $err = $@;
     alarm ($previous_alarm);
 
-    close($TRACEROUTE_FH);
-
     if ($err) {
        die "unable to detect country - $err\n";
     } elsif ($country) {
@@ -264,6 +269,12 @@ sub query_installation_environment : prototype() {
        routes => $routes,
        dns => query_dns(),
     };
+
+    # avoid serializing out null or an empty string, that can trip up the UIs
+    if (my $fqdn = Proxmox::Sys::Net::get_dhcp_fqdn()) {
+       $output->{network}->{hostname} = $fqdn;
+    }
+
     # FIXME: move whatever makes sense over to Proxmox::Sys::Net:: and keep that as single source,
     # it can then use some different structure just fine (after adapting the GTK GUI to that) but
     # **never** to (slightly different!) things for the same stuff...
@@ -274,6 +285,16 @@ sub query_installation_environment : prototype() {
     $output->{hvm_supported} = query_cpu_hvm_support();
     $output->{boot_type} = -d '/sys/firmware/efi' ? 'efi' : 'bios';
 
+    if ($output->{boot_type} eq 'efi') {
+       my $content = eval { file_read_all("/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c") };
+       if ($@) {
+           log_warn("Failed to read secure boot state: $@\n");
+       } else {
+           my @secureboot = unpack("CCCCC", $content);
+           $output->{secure_boot} = $secureboot[4] == 1;
+       }
+    }
+
     my $err;
     my $country;
     if ($routes->{gateway4}) {
@@ -291,12 +312,56 @@ sub query_installation_environment : prototype() {
     if (defined($country)) {
        $output->{country} = $country;
     } else {
-       warn ($err // "unable to detect country\n");
+       warn ($err || "unable to detect country\n");
     }
 
     return $output;
 }
 
+# OpenZFS specifies 64 MiB as the absolute minimum:
+# https://openzfs.github.io/openzfs-docs/Performance%20and%20Tuning/Module%20Parameters.html#zfs-arc-max
+our $ZFS_ARC_MIN_SIZE_MIB = 64; # MiB
+
+# See https://bugzilla.proxmox.com/show_bug.cgi?id=4829
+our $ZFS_ARC_MAX_SIZE_MIB = 16 * 1024; # 16384 MiB = 16 GiB
+our $ZFS_ARC_SYSMEM_PERCENTAGE = 0.1; # use 10% of available system memory by default
+
+# Calculates the default upper limit for the ZFS ARC size.
+# Returns the default ZFS maximum ARC size in MiB.
+sub default_zfs_arc_max {
+    # Use ZFS default on non-PVE
+    return 0 if Proxmox::Install::ISOEnv::get('product') ne 'pve';
+
+    my $default_mib = get('total_memory') * $ZFS_ARC_SYSMEM_PERCENTAGE;
+    my $rounded_mib = int(sprintf('%.0f', $default_mib));
+
+    if ($rounded_mib > $ZFS_ARC_MAX_SIZE_MIB) {
+       return $ZFS_ARC_MAX_SIZE_MIB;
+    } elsif ($rounded_mib < $ZFS_ARC_MIN_SIZE_MIB) {
+       return $ZFS_ARC_MIN_SIZE_MIB;
+    }
+
+    return $rounded_mib;
+}
+
+# Clamps the provided ZFS arc_max value (in MiB) to the accepted bounds. The
+# lower is specified by `$ZFS_ARC_MIN_SIZE_MIB`, the upper by the available system memory.
+# Returns the clamped value in MiB.
+sub clamp_zfs_arc_max {
+    my ($mib) = @_;
+
+    return $mib if $mib == 0;
+
+    my $total_mem_mib = get('total_memory');
+    if ($mib > $total_mem_mib) {
+       return $total_mem_mib;
+    } elsif ($mib < $ZFS_ARC_MIN_SIZE_MIB) {
+       return $ZFS_ARC_MIN_SIZE_MIB;
+    }
+
+    return $mib;
+}
+
 my $_env = undef;
 sub get {
     my ($k) = @_;