diff --git a/vps_init.sh b/vps_init.sh index f9bdee9..9a2b745 100644 --- a/vps_init.sh +++ b/vps_init.sh @@ -12,7 +12,7 @@ # NEW_PASSWORD 已弃用 - 密码将在交互式设置中由用户输入 NEW_SSH_PORT="4399" # SSH新端口号(默认值) TIMEZONE="Asia/Shanghai" # 时区设置 -SWAP_SIZE=2048 # 交换分区大小(MB) +SWAP_SIZE=1024 # 交换分区大小(MB,默认值) SWAPPINESS=10 # 交换倾向默认值(0-100,越大越倾向使用交换) # =========================================== @@ -127,8 +127,25 @@ else echo -e "${GREEN}保持主机名不变${NC}" fi +# 询问交换分区大小(SWAP_SIZE) +echo -e "${YELLOW}4. 设置交换分区大小 (MB)? 当前默认: ${SWAP_SIZE}MB${NC}" +echo -e "${YELLOW} 常用值: 1024 / 2048 (建议为物理内存的1-2倍)${NC}" +while true; do + read -p "请输入交换分区大小 (MB, 默认${SWAP_SIZE}): " INPUT_SWAP_SIZE + # 用户直接回车则使用默认值 + INPUT_SWAP_SIZE=${INPUT_SWAP_SIZE:-$SWAP_SIZE} + # 校验:必须为正整数(MB) + if [[ "$INPUT_SWAP_SIZE" =~ ^[0-9]+$ ]] && [ "$INPUT_SWAP_SIZE" -gt 0 ]; then + SWAP_SIZE=$INPUT_SWAP_SIZE + echo -e "${GREEN}交换分区大小将设置为: ${SWAP_SIZE}MB${NC}" + break + else + echo -e "${RED}无效的输入,请输入大于0的整数(单位MB)${NC}" + fi +done + # 询问交换倾向(swappiness) -echo -e "${YELLOW}4. 设置交换倾向 swappiness (0-100)?${NC}" +echo -e "${YELLOW}5. 设置交换倾向 swappiness (0-100)?${NC}" echo -e "${YELLOW} 值越大越倾向使用交换(swap),越小越倾向保留物理内存${NC}" while true; do read -p "请输入 swappiness 值 (0-100, 默认${SWAPPINESS}): " INPUT_SWAPPINESS