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
+27 -114
View File
@@ -13,6 +13,31 @@ print_red() { echo -e "\033[31m\033[01m$1$2\033[0m"; }
print_green() { echo -e "\033[32m\033[01m$1$2\033[0m"; } print_green() { echo -e "\033[32m\033[01m$1$2\033[0m"; }
print_yellow() { echo -e "\033[33m\033[01m$1$2\033[0m"; } print_yellow() { echo -e "\033[33m\033[01m$1$2\033[0m"; }
SCRIPT_BASE_URL="https://git.1-h.cc/Scripts/Linux/raw/branch/2026"
SCRIPT_DIR=""
if [ -n "${BASH_SOURCE[0]:-}" ]; then
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "${BASH_SOURCE[0]}")" 2>/dev/null && pwd || true)
fi
load_shared_script() {
local relative_path="$1"
local local_path remote_url
local_path="$SCRIPT_DIR/$relative_path"
remote_url="$SCRIPT_BASE_URL/$relative_path"
if [ -n "$SCRIPT_DIR" ] && [ -f "$local_path" ]; then
source "$local_path"
elif command -v curl >/dev/null 2>&1; then
source <(curl -fsSL "$remote_url")
elif command -v wget >/dev/null 2>&1; then
source <(wget -q -O - "$remote_url")
else
print_red "未找到 curl 或 wget,无法加载 $relative_path"
exit 1
fi
}
if [ "$EUID" -ne 0 ]; then if [ "$EUID" -ne 0 ]; then
print_red "请使用 sudo 运行此脚本" print_red "请使用 sudo 运行此脚本"
exit 1 exit 1
@@ -46,6 +71,8 @@ else
exit 1 exit 1
fi fi
load_shared_script "zram/install.sh"
sysctl_config() { sysctl_config() {
print_green "###################" print_green "###################"
print_green "##### sysctl ######" print_green "##### sysctl ######"
@@ -271,120 +298,6 @@ start_watchtower() {
containrrr/watchtower "${WATCHTOWER_ARGS[@]}" $WATCHTOWER_NAME containrrr/watchtower "${WATCHTOWER_ARGS[@]}" $WATCHTOWER_NAME
} }
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}"
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
}
echo "检测到总内存: ${total_mem}MB"
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
# 自动生成的 zram 配置
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 "---------------------------------------"
}
modify_authorized_keys modify_authorized_keys
bash_aliases bash_aliases
install_docker install_docker
+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
+18 -21
View File
@@ -1,9 +1,16 @@
#!/usr/bin/env bash #!/usr/bin/env bash
#
# ... format_zram_bytes() {
# awk -v bytes="$1" 'BEGIN {
# 使用方法: split("B KiB MiB GiB TiB PiB", units, " ")
# URL="https://git.1-h.cc/Scripts/Linux/raw/branch/2026/show_zram_status.sh"; curl -fsSL "$URL" | bash idx = 1
while (bytes >= 1024 && idx < 6) {
bytes /= 1024
idx++
}
printf "%.2f %s", bytes, units[idx]
}'
}
show_zram_status() { show_zram_status() {
echo "========== zram 运行战报 ==========" echo "========== zram 运行战报 =========="
@@ -48,25 +55,15 @@ EOF
ratio=$(awk -v data="$data_size" -v total="$total_mem" 'BEGIN { printf "%.2f", data / total }') ratio=$(awk -v data="$data_size" -v total="$total_mem" 'BEGIN { printf "%.2f", data / total }')
format_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]
}'
}
echo "----------- 汇总 -----------" echo "----------- 汇总 -----------"
echo "1. 原始数据量: $(format_bytes "$data_size")" echo "1. 原始数据量: $(format_zram_bytes "$data_size")"
echo "2. 压缩后大小: $(format_bytes "$compr_size")" echo "2. 压缩后大小: $(format_zram_bytes "$compr_size")"
echo "3. 实际占用内存: $(format_bytes "$total_mem")" echo "3. 实际占用内存: $(format_zram_bytes "$total_mem")"
echo "4. 压缩倍率: ${ratio}x" echo "4. 压缩倍率: ${ratio}x"
echo "5. 为系统节省了: $(format_bytes "$saved_bytes") 物理内存" echo "5. 为系统节省了: $(format_zram_bytes "$saved_bytes") 物理内存"
echo "===================================" echo "==================================="
} }
if [ "${BASH_SOURCE[0]:-}" = "$0" ] || [ -z "${BASH_SOURCE[0]:-}" ]; then
show_zram_status show_zram_status
fi