mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2026-11-02 10:52:22 +00:00
fix(post-install): prioritize ZRAM ahead of disk swap
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
#!/usr/bin/env bash
|
||||
# Pure helpers shared by the ZRAM installer and its fixture tests.
|
||||
|
||||
# The kernel's maximum priority puts ZRAM ahead of standard Proxmox swap.
|
||||
# A disk swap explicitly configured at this same maximum cannot be outranked.
|
||||
if ! declare -p ZRAM_SWAP_PRIORITY >/dev/null 2>&1; then
|
||||
readonly ZRAM_SWAP_PRIORITY=32767
|
||||
fi
|
||||
|
||||
zram_render_generator_config() {
|
||||
local algorithm=zstd
|
||||
[[ "${1:-false}" == true ]] && algorithm='zstd(level=9)'
|
||||
@@ -9,19 +15,32 @@ zram_render_generator_config() {
|
||||
[zram0]
|
||||
zram-size = ram / 10
|
||||
compression-algorithm = $algorithm
|
||||
swap-priority = 100
|
||||
swap-priority = $ZRAM_SWAP_PRIORITY
|
||||
EOF
|
||||
}
|
||||
|
||||
zram_list_non_zram_swap() {
|
||||
local swaps_file="$1" fstab_file="$2"
|
||||
awk 'NR > 1 && $1 !~ /^\/dev\/zram[0-9]+$/ { print "active: " $1 }' "$swaps_file"
|
||||
awk '$0 !~ /^[[:space:]]*#/ && NF >= 3 && $3 == "swap" && $1 !~ /^\/dev\/zram[0-9]+$/ { print "fstab: " $1 }' "$fstab_file"
|
||||
zram_list_foreign_active_zram_swap() {
|
||||
local swaps_file="$1" owned_device="${2:-}"
|
||||
local swap_paths swap_path real_device
|
||||
[[ -r "$swaps_file" ]] || return 2
|
||||
swap_paths="$(awk 'NR > 1 { print $1 }' "$swaps_file")" || return 2
|
||||
while IFS= read -r swap_path; do
|
||||
[[ -z "$swap_path" ]] && continue
|
||||
real_device="$(readlink -f -- "$swap_path" 2>/dev/null)" || real_device="$swap_path"
|
||||
if [[ "$real_device" =~ ^/dev/zram[0-9]+$ && "$real_device" != "$owned_device" ]]; then
|
||||
printf 'active zram: %s\n' "$swap_path"
|
||||
fi
|
||||
done <<< "$swap_paths"
|
||||
}
|
||||
|
||||
zram_has_non_zram_swap() {
|
||||
[[ -r "$1" && -r "$2" ]] || return 2
|
||||
[[ -n "$(zram_list_non_zram_swap "$1" "$2")" ]]
|
||||
zram_list_foreign_fstab_zram_swap() {
|
||||
local fstab_file="$1"
|
||||
[[ -r "$fstab_file" ]] || return 2
|
||||
awk '
|
||||
$0 !~ /^[[:space:]]*#/ && NF >= 3 && $3 == "swap" && $1 ~ /^\/dev\/zram[0-9]+$/ {
|
||||
print "fstab zram: " $1
|
||||
}
|
||||
' "$fstab_file"
|
||||
}
|
||||
|
||||
# Print generator configuration outside this feature's dedicated drop-in.
|
||||
@@ -37,7 +56,8 @@ zram_foreign_generator_config() {
|
||||
[[ "$file" == "$owned_file" ]] && continue
|
||||
# Ignore package defaults containing only comments, whitespace and
|
||||
# section headers. Any directive or malformed active line is foreign.
|
||||
if awk '
|
||||
local active
|
||||
active="$(awk '
|
||||
{
|
||||
line=$0
|
||||
sub(/^[[:space:]]+/, "", line)
|
||||
@@ -45,27 +65,70 @@ zram_foreign_generator_config() {
|
||||
if (line == "" || line ~ /^[#;]/ || line ~ /^\[[^]]+\]$/) next
|
||||
found=1
|
||||
}
|
||||
END { exit !found }
|
||||
' "$file"; then
|
||||
END { if (found) print "active" }
|
||||
' "$file")" || return 2
|
||||
if [[ -n "$active" ]]; then
|
||||
printf '%s\n' "$file"
|
||||
fi
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
zram_list_defined_systemd_swap_units() {
|
||||
local fixture_root="${1:-}" root file
|
||||
local roots=(/etc/systemd/system /run/systemd/system)
|
||||
zram_list_foreign_systemd_zram_units() {
|
||||
local fixture_root="${1:-}" root file unit what unit_files
|
||||
local roots=(/etc/systemd/system /run/systemd/system /usr/local/lib/systemd/system /usr/lib/systemd/system /lib/systemd/system)
|
||||
if [[ -n "$fixture_root" ]]; then
|
||||
roots=("$fixture_root/etc/systemd/system" "$fixture_root/run/systemd/system")
|
||||
roots=("$fixture_root/etc/systemd/system" "$fixture_root/run/systemd/system" "$fixture_root/usr/local/lib/systemd/system" "$fixture_root/usr/lib/systemd/system" "$fixture_root/lib/systemd/system")
|
||||
fi
|
||||
for root in "${roots[@]}"; do
|
||||
[[ -d "$root" ]] || continue
|
||||
# Skip activation links; systemd-generator creates links under
|
||||
# *.wants and they must not look like foreign unit definitions.
|
||||
unit_files="$(find "$root" \( -type d \( -name '*.wants' -o -name '*.requires' \) -prune \) -o \( \( -name '*.swap' -o -path '*.swap.d/*.conf' \) \( -type f -o -type l \) -print \) 2>/dev/null)" || return 2
|
||||
while IFS= read -r file; do
|
||||
# Files in these roots are administrator-defined units. The
|
||||
# generator's own output lives under /run/systemd/generator and
|
||||
# is intentionally outside this scan.
|
||||
printf 'systemd: %s\n' "$file"
|
||||
done < <(find "$root" -name '*.swap' \( -type f -o -type l \) -print 2>/dev/null)
|
||||
[[ -z "$file" ]] && continue
|
||||
unit="$(basename "$file")"
|
||||
if [[ -L "$file" && "$(readlink -f -- "$file" 2>/dev/null || true)" == /dev/null ]]; then
|
||||
if [[ "$unit" == "dev-zram0.swap" ]]; then
|
||||
printf 'systemd zram unit masked: %s\n' "$file"
|
||||
fi
|
||||
continue
|
||||
fi
|
||||
# A persistent unit named for a zram device could shadow the
|
||||
# generated unit, even if it currently points at another device.
|
||||
what="$(awk '
|
||||
/^[[:space:]]*\[Swap\][[:space:]]*$/ { in_swap=1; next }
|
||||
/^[[:space:]]*\[/ { in_swap=0 }
|
||||
in_swap && /^[[:space:]]*What[[:space:]]*=[[:space:]]*\/dev\/zram[0-9]+([[:space:]]|$)/ {
|
||||
found=1
|
||||
}
|
||||
END { if (found) print "zram" }
|
||||
' "$file")" || return 2
|
||||
if [[ "$unit" =~ ^dev-zram[0-9]+\.swap$ || -n "$what" ]]; then
|
||||
printf 'systemd zram unit: %s\n' "$file"
|
||||
fi
|
||||
done <<< "$unit_files"
|
||||
done
|
||||
}
|
||||
|
||||
zram_profile_verify_active() {
|
||||
local expected_algorithm="$1" expected_priority="${2:-$ZRAM_SWAP_PRIORITY}"
|
||||
local sys_block_root="${3:-/sys/block}" meminfo_path="${4:-/proc/meminfo}" swaps_path="${5:-/proc/swaps}"
|
||||
local zram0="$sys_block_root/zram0" expected_bytes actual_bytes active_algorithm
|
||||
|
||||
[[ -r "$zram0/disksize" && -r "$zram0/comp_algorithm" && -r "$meminfo_path" && -r "$swaps_path" ]] || return 1
|
||||
expected_bytes="$(awk '/^MemTotal:/ { print int($2 * 1024 / 10) }' "$meminfo_path")"
|
||||
actual_bytes="$(cat "$zram0/disksize")"
|
||||
[[ "$expected_bytes" =~ ^[0-9]+$ && "$actual_bytes" =~ ^[0-9]+$ ]] || return 1
|
||||
(( actual_bytes >= expected_bytes - 1048576 && actual_bytes <= expected_bytes + 1048576 )) || return 1
|
||||
active_algorithm="$(grep -oE '\[[^]]+\]' "$zram0/comp_algorithm" | tr -d '[]')"
|
||||
[[ "$active_algorithm" == "$expected_algorithm" ]] || return 1
|
||||
awk -v priority="$expected_priority" '
|
||||
NR > 1 && $1 == "/dev/zram0" {
|
||||
found=1
|
||||
if ($5 + 0 != priority + 0) bad=1
|
||||
}
|
||||
NR > 1 && $1 ~ /^\/dev\/zram[0-9]+$/ && $1 != "/dev/zram0" { bad=1 }
|
||||
END { exit !(found && !bad) }
|
||||
' "$swaps_path"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user