refactor(zram): 模块化 zram 脚本并引入通用加载机制支持远程调用

This commit is contained in:
严浩
2026-03-23 11:10:15 +08:00
parent 88603904c0
commit 2978818923
3 changed files with 190 additions and 136 deletions
+144
View File
@@ -0,0 +1,144 @@
#!/usr/bin/env bash
if ! declare -F print_red >/dev/null 2>&1; then
print_red() { echo -e "\033[31m\033[01m$1$2\033[0m"; }
fi
if ! declare -F print_green >/dev/null 2>&1; then
print_green() { echo -e "\033[32m\033[01m$1$2\033[0m"; }
fi
if ! declare -F print_yellow >/dev/null 2>&1; then
print_yellow() { echo -e "\033[33m\033[01m$1$2\033[0m"; }
fi
if [ -z "${SYSTEM_TYPE:-}" ]; then
if [ -f /etc/debian_version ]; then
SYSTEM_TYPE="debian"
elif [ -f /etc/alpine-release ]; then
SYSTEM_TYPE="alpine"
else
SYSTEM_TYPE="unknown"
fi
fi
install_zram() {
print_green "###################"
print_green "###### zram #######"
print_green "###################"
if [ "$SYSTEM_TYPE" != "debian" ]; then
print_yellow "当前仅在 Debian 系统上配置 zram-tools,已跳过"
return 0
fi
if [ -f /.dockerenv ] || grep -qaE '(docker|containerd|kubepods|lxc)' /proc/1/cgroup 2>/dev/null; then
print_yellow "检测到容器环境,跳过 zram 配置"
return 0
fi
echo "正在准备 zram-tools..."
if dpkg -s zram-tools >/dev/null 2>&1; then
echo "zram-tools 已安装,跳过安装步骤"
else
apt update && apt install zram-tools -y
fi
if command -v modprobe >/dev/null 2>&1; then
if modprobe zram 2>/dev/null; then
echo "zram 内核模块已加载"
else
print_yellow "内核不支持 zram 模块,跳过 zram 配置"
return 0
fi
else
print_yellow "未找到 modprobe,继续尝试配置 zram-tools"
fi
if command -v udevadm >/dev/null 2>&1; then
udevadm settle || true
fi
if [ ! -d /sys/block/zram0 ] || [ ! -b /dev/zram0 ]; then
print_yellow "未检测到可用的 zram0 设备,跳过 zram 配置"
return 0
fi
local total_mem algo percent available_algorithms
total_mem=$(awk '/MemTotal/{print int($2 / 1024)}' /proc/meminfo)
if ! [[ "$total_mem" =~ ^[0-9]+$ ]] || [ "$total_mem" -le 0 ]; then
print_yellow "无法准确检测总内存,跳过 zram 配置"
return 0
fi
available_algorithms=$(tr ' ' '\n' </sys/block/zram0/comp_algorithm 2>/dev/null | tr -d '[]' || true)
echo "检测到支持的 zram 算法: ${available_algorithms:-unknown}"
echo "检测到总内存: ${total_mem}MB"
choose_zram_algorithm() {
local candidate
for candidate in "$@"; do
if printf '%s\n' "$available_algorithms" | grep -qx "$candidate"; then
printf '%s\n' "$candidate"
return 0
fi
done
return 1
}
if [ "$total_mem" -le 2500 ]; then
percent="100"
if algo=$(choose_zram_algorithm zstd lz4 lzo-rle lzo); then
if [ "$algo" = "zstd" ]; then
echo "策略:内存 <= 2G,使用 zstd 算法 (100% 占比)"
else
echo "策略:内存 <= 2G,但内核未报告 zstd 支持,回退到 $algo (100% 占比)"
fi
else
print_yellow "未检测到受支持的 zram 压缩算法,跳过 zram 配置"
return 0
fi
else
percent="60"
if algo=$(choose_zram_algorithm lz4 zstd lzo-rle lzo); then
if [ "$algo" = "lz4" ]; then
echo "策略:内存 > 2G,使用 lz4 算法 (60% 占比)"
else
echo "策略:内存 > 2G,但内核未报告 lz4 支持,回退到 $algo (60% 占比)"
fi
else
print_yellow "未检测到受支持的 zram 压缩算法,跳过 zram 配置"
return 0
fi
fi
cat <<EOF >/etc/default/zramswap
ALGO=$algo
PERCENT=$percent
PRIORITY=100
EOF
if ! systemctl restart zramswap; then
print_red "zramswap 服务重启失败"
systemctl status zramswap.service --no-pager || true
journalctl -u zramswap.service -n 20 --no-pager || true
return 1
fi
echo "---------------------------------------"
echo "zram 部署完成!当前状态:"
zramctl || true
echo "---------------------------------------"
}
if [ "${BASH_SOURCE[0]:-}" = "$0" ] || [ -z "${BASH_SOURCE[0]:-}" ]; then
if [ "$EUID" -ne 0 ]; then
print_red "请使用 sudo 运行此脚本"
exit 1
fi
install_zram
fi
+69
View File
@@ -0,0 +1,69 @@
#!/usr/bin/env bash
format_zram_bytes() {
awk -v bytes="$1" 'BEGIN {
split("B KiB MiB GiB TiB PiB", units, " ")
idx = 1
while (bytes >= 1024 && idx < 6) {
bytes /= 1024
idx++
}
printf "%.2f %s", bytes, units[idx]
}'
}
show_zram_status() {
echo "========== zram 运行战报 =========="
if ! command -v zramctl >/dev/null 2>&1; then
echo "未安装 zramctl。"
return 1
fi
local table
table=$(zramctl --noheadings --output NAME,ALGORITHM,DISKSIZE,DATA,COMPR,TOTAL,COMP-RATIO,MOUNTPOINT 2>/dev/null || true)
if [ -z "$table" ]; then
echo "未发现活跃的 zram 设备。"
return 0
fi
printf '%s\n' "$table"
local stats
stats=$(zramctl --bytes --raw --noheadings --output DATA,COMPR,TOTAL 2>/dev/null || true)
if [ -z "$stats" ]; then
echo "未发现活跃的 zram 设备。"
return 0
fi
local data_size compr_size total_mem saved_bytes ratio
read -r data_size compr_size total_mem <<EOF
$(printf '%s\n' "$stats" | awk '{data += $1; compr += $2; total += $3} END {print data+0, compr+0, total+0}')
EOF
if [ "$total_mem" -le 0 ]; then
echo "当前 zram 尚无可统计的数据。"
return 0
fi
saved_bytes=$((data_size - total_mem))
if [ "$saved_bytes" -lt 0 ]; then
saved_bytes=0
fi
ratio=$(awk -v data="$data_size" -v total="$total_mem" 'BEGIN { printf "%.2f", data / total }')
echo "----------- 汇总 -----------"
echo "1. 原始数据量: $(format_zram_bytes "$data_size")"
echo "2. 压缩后大小: $(format_zram_bytes "$compr_size")"
echo "3. 实际占用内存: $(format_zram_bytes "$total_mem")"
echo "4. 压缩倍率: ${ratio}x"
echo "5. 为系统节省了: $(format_zram_bytes "$saved_bytes") 物理内存"
echo "==================================="
}
if [ "${BASH_SOURCE[0]:-}" = "$0" ] || [ -z "${BASH_SOURCE[0]:-}" ]; then
show_zram_status
fi