diff --git a/Proxmox/Install.pm b/Proxmox/Install.pm index 1117fc4..f371716 100644 --- a/Proxmox/Install.pm +++ b/Proxmox/Install.pm @@ -291,6 +291,19 @@ sub get_zfs_raid_setup { return ($devlist, $cmd); } +# If the maximum ARC size for ZFS was explicitly changed by the user, applies +# it to the new system by setting the `zfs_arc_max` module parameter in /etc/modprobe.d/zfs.conf +my sub zfs_setup_module_conf { + my ($targetdir) = @_; + + my $arc_max = Proxmox::Install::Config::get_zfs_opt('arc_max'); + my $arc_max_mib = Proxmox::Install::RunEnv::clamp_zfs_arc_max($arc_max) * 1024 * 1024; + + if ($arc_max > 0) { + file_write_all("$targetdir/etc/modprobe.d/zfs.conf", "options zfs zfs_arc_max=$arc_max\n") + } +} + sub get_btrfs_raid_setup { my $filesys = Proxmox::Install::Config::get_filesys(); @@ -1141,6 +1154,7 @@ _EOD file_write_all("$targetdir/etc/kernel/cmdline", "root=ZFS=$zfs_pool_name/ROOT/$zfs_root_volume_name boot=zfs\n"); + zfs_setup_module_conf($targetdir); } diversion_remove($targetdir, "/usr/sbin/update-grub"); diff --git a/Proxmox/Install/Config.pm b/Proxmox/Install/Config.pm index 024f62a..f496d61 100644 --- a/Proxmox/Install/Config.pm +++ b/Proxmox/Install/Config.pm @@ -72,6 +72,7 @@ my sub init_cfg { compress => 'on', checksum => 'on', copies => 1, + arc_max => Proxmox::Install::RunEnv::default_zfs_arc_max(), # in MiB }, # TODO: single disk selection config target_hd => undef, diff --git a/Proxmox/Install/RunEnv.pm b/Proxmox/Install/RunEnv.pm index 3e810b2..9116397 100644 --- a/Proxmox/Install/RunEnv.pm +++ b/Proxmox/Install/RunEnv.pm @@ -303,6 +303,51 @@ sub query_installation_environment : prototype() { 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)); + print "total_memory:" . get('total_memory') . " mib_rounded:$rounded_mib\n"; + + 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) = @_;