Skip to content

Commit fe87770

Browse files
committed
fix:整体优化
1 parent dc4536b commit fe87770

5 files changed

Lines changed: 213 additions & 34 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2026.06.02
2+
- 统一无交互入口:安装、批量创建、卸载脚本均支持 `export noninteractive=true`,并在无交互模式下使用清晰默认值避免 stdin 阻塞
3+
- 批量创建脚本新增环境变量覆盖:`CONTAINERD_CREATE_COUNT``CONTAINERD_CONTAINER_MEMORY``CONTAINERD_CONTAINER_CPU``CONTAINERD_CONTAINER_DISK``CONTAINERD_CONTAINER_SYSTEM``CONTAINERD_CONTAINER_IPV6`
4+
- 优化批量创建日志展示,使用一次 shell 字段解析替代每行多次 `awk` 调用,避免不必要的重复外部命令
5+
16
2026.03.02
27
- 修复容器创建后未正确配置 NAT 到宿主机公网 IP 的问题:在 containerdinstall.sh 新增 setup_iptables_nat() 函数,显式添加 IPv4 MASQUERADE 和 FORWARD 规则(基于子网而非网桥接口,避免接口未创建时规则失效)
38
- 修复 adapt_ipv6() 仅配置 sysctl 而缺少 ip6tables FORWARD 规则的问题:新增 ip6tables FORWARD 规则允许 ctn-br1 和 IPv6 子网双向流量

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@
1919
bash <(wget -qO- https://raw.githubusercontent.com/oneclickvirt/containerd/main/containerdinstall.sh)
2020
```
2121

22+
无交互安装统一使用:
23+
24+
```bash
25+
export noninteractive=true
26+
bash <(wget -qO- https://raw.githubusercontent.com/oneclickvirt/containerd/main/containerdinstall.sh)
27+
```
28+
29+
可配合环境变量覆盖默认值:
30+
31+
```bash
32+
export noninteractive=true
33+
export NEED_DISK_LIMIT=y
34+
export CONTAINERD_POOL_SIZE=20
35+
export CONTAINERD_INSTALL_PATH=/var/lib/containerd
36+
export CONTAINERD_LOOP_FILE=/opt/containerd-pool.img
37+
bash <(wget -qO- https://raw.githubusercontent.com/oneclickvirt/containerd/main/containerdinstall.sh)
38+
```
39+
2240
## 开设单个容器
2341

2442
```bash
@@ -58,6 +76,19 @@ chmod +x create_containerd.sh
5876

5977
交互式脚本,自动递增容器名(ct1, ct2, ...)、SSH 端口、公网端口,容器信息记录到 `ctlog` 文件。
6078

79+
无交互批量开设统一使用:
80+
81+
```bash
82+
export noninteractive=true
83+
export CONTAINERD_CREATE_COUNT=1
84+
export CONTAINERD_CONTAINER_MEMORY=512
85+
export CONTAINERD_CONTAINER_CPU=1
86+
export CONTAINERD_CONTAINER_DISK=0
87+
export CONTAINERD_CONTAINER_SYSTEM=debian
88+
export CONTAINERD_CONTAINER_IPV6=n
89+
./create_containerd.sh
90+
```
91+
6192
## 查看与管理容器
6293

6394
```bash
@@ -80,6 +111,13 @@ bash <(wget -qO- https://raw.githubusercontent.com/oneclickvirt/containerd/main/
80111

81112
脚本会在执行前要求输入 `yes` 确认,操作不可逆。
82113

114+
无交互卸载统一使用:
115+
116+
```bash
117+
export noninteractive=true
118+
bash <(wget -qO- https://raw.githubusercontent.com/oneclickvirt/containerd/main/containerduninstall.sh)
119+
```
120+
83121
## 镜像说明
84122

85123
本仓库自编镜像通过 GitHub Actions 构建并发布到 Releases:
@@ -102,4 +140,3 @@ bash <(wget -qO- https://raw.githubusercontent.com/oneclickvirt/containerd/main/
102140
## Stargazers over time
103141

104142
[![Stargazers over time](https://starchart.cc/oneclickvirt/containerd.svg)](https://starchart.cc/oneclickvirt/containerd)
105-

containerdinstall.sh

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
# 2026.03.01
55
#
66
# Supported environment variables (non-interactive mode / 支持的环境变量,可实现无交互安装):
7+
# noninteractive=true - Use defaults for prompts / 使用默认值跳过交互提示
78
# WITHOUTCDN=TRUE - Disable CDN acceleration / 禁用 CDN 加速
89
# NEED_DISK_LIMIT=y - Enable container disk size limitation (btrfs) / 启用容器磁盘大小限制 (btrfs);默认: n
910
# CONTAINERD_INSTALL_PATH= - containerd data root path / containerd 数据根路径;默认: /var/lib/containerd
10-
# CONTAINERD_POOL_SIZE=20 - Storage pool size in GB / 存储池大小(GB),NEED_DISK_LIMIT=y 时必填
11+
# CONTAINERD_POOL_SIZE=20 - Storage pool size in GB / 存储池大小(GB),NEED_DISK_LIMIT=y 时默认: 20
1112
# CONTAINERD_LOOP_FILE= - Loop file path / 循环文件路径;默认: /opt/containerd-pool.img
1213
#
1314
# Example / 示例:
15+
# export noninteractive=true
16+
# bash containerdinstall.sh
1417
# NEED_DISK_LIMIT=y CONTAINERD_POOL_SIZE=20 bash containerdinstall.sh
1518
# CONTAINERD_INSTALL_PATH=/data/containerd bash containerdinstall.sh
1619

@@ -19,6 +22,21 @@ _green() { echo -e "\033[32m\033[01m$*\033[0m"; }
1922
_yellow() { echo -e "\033[33m\033[01m$*\033[0m"; }
2023
_blue() { echo -e "\033[36m\033[01m$*\033[0m"; }
2124
reading() { read -rp "$(_green "$1")" "$2"; }
25+
is_noninteractive() {
26+
case "$(printf '%s' "${noninteractive:-}" | tr '[:upper:]' '[:lower:]')" in
27+
true|yes|y|1) return 0 ;;
28+
*) return 1 ;;
29+
esac
30+
}
31+
is_yes() {
32+
case "$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')" in
33+
y|yes|true|1) return 0 ;;
34+
*) return 1 ;;
35+
esac
36+
}
37+
DEFAULT_CONTAINERD_INSTALL_PATH="/var/lib/containerd"
38+
DEFAULT_CONTAINERD_POOL_SIZE="20"
39+
DEFAULT_CONTAINERD_LOOP_FILE="/opt/containerd-pool.img"
2240
export DEBIAN_FRONTEND=noninteractive
2341
utf8_locale=$(locale -a 2>/dev/null | grep -i -m 1 -E "UTF-8|utf8")
2442
if [[ -z "$utf8_locale" ]]; then
@@ -995,6 +1013,9 @@ main() {
9951013
if [[ -n "${NEED_DISK_LIMIT:-}" ]]; then
9961014
need_disk_limit_input="${NEED_DISK_LIMIT}"
9971015
_blue "[non-interactive] NEED_DISK_LIMIT=${NEED_DISK_LIMIT}"
1016+
elif is_noninteractive; then
1017+
need_disk_limit_input="n"
1018+
_blue "[non-interactive] noninteractive=true, NEED_DISK_LIMIT defaulting to n"
9981019
else
9991020
_green "Do you need containerd with container disk size limitation? (Support btrfs snapshotter)"
10001021
_green "是否需要支持容器硬盘大小限制的 containerd 环境?(使用 btrfs 快照器)"
@@ -1009,18 +1030,21 @@ main() {
10091030
if [[ -n "${CONTAINERD_INSTALL_PATH:-}" ]]; then
10101031
containerd_install_path="${CONTAINERD_INSTALL_PATH}"
10111032
_blue "[non-interactive] CONTAINERD_INSTALL_PATH=${CONTAINERD_INSTALL_PATH}"
1033+
elif is_noninteractive; then
1034+
containerd_install_path="$DEFAULT_CONTAINERD_INSTALL_PATH"
1035+
_blue "[non-interactive] noninteractive=true, CONTAINERD_INSTALL_PATH defaulting to ${containerd_install_path}"
10121036
else
1013-
_green "Where do you want to install containerd? (Enter to default: /var/lib/containerd):"
1014-
reading "containerd 安装路径?(回车则默认:/var/lib/containerd):" containerd_install_path
1037+
_green "Where do you want to install containerd? (Enter to default: ${DEFAULT_CONTAINERD_INSTALL_PATH}):"
1038+
reading "containerd 安装路径?(回车则默认:${DEFAULT_CONTAINERD_INSTALL_PATH}):" containerd_install_path
10151039
if [ -z "$containerd_install_path" ]; then
1016-
containerd_install_path="/var/lib/containerd"
1040+
containerd_install_path="$DEFAULT_CONTAINERD_INSTALL_PATH"
10171041
fi
10181042
fi
10191043
echo "$containerd_install_path" > /usr/local/bin/containerd_install_path
10201044

10211045
containerd_pool_size=""
10221046
containerd_loop_file=""
1023-
if [ "$need_disk_limit_input" = "y" ] || [ "$need_disk_limit_input" = "Y" ]; then
1047+
if is_yes "$need_disk_limit_input"; then
10241048
echo "true" > /usr/local/bin/containerd_need_disk_limit
10251049
# 存储池大小(支持环境变量 CONTAINERD_POOL_SIZE)
10261050
if [[ -n "${CONTAINERD_POOL_SIZE:-}" ]]; then
@@ -1031,6 +1055,9 @@ main() {
10311055
_red "Invalid CONTAINERD_POOL_SIZE='${CONTAINERD_POOL_SIZE}', must be a positive integer."
10321056
exit 1
10331057
fi
1058+
elif is_noninteractive; then
1059+
containerd_pool_size="$DEFAULT_CONTAINERD_POOL_SIZE"
1060+
_blue "[non-interactive] noninteractive=true, CONTAINERD_POOL_SIZE defaulting to ${containerd_pool_size}"
10341061
else
10351062
while true; do
10361063
_green "How large a containerd storage pool is needed? (unit: GB, e.g., enter 20 for 20G):"
@@ -1047,11 +1074,14 @@ main() {
10471074
if [[ -n "${CONTAINERD_LOOP_FILE:-}" ]]; then
10481075
containerd_loop_file="${CONTAINERD_LOOP_FILE}"
10491076
_blue "[non-interactive] CONTAINERD_LOOP_FILE=${CONTAINERD_LOOP_FILE}"
1077+
elif is_noninteractive; then
1078+
containerd_loop_file="$DEFAULT_CONTAINERD_LOOP_FILE"
1079+
_blue "[non-interactive] noninteractive=true, CONTAINERD_LOOP_FILE defaulting to ${containerd_loop_file}"
10501080
else
1051-
_green "Where do you want to store the containerd loop file? (Enter to default: /opt/containerd-pool.img):"
1052-
reading "containerd 循环文件存储位置?(回车则默认:/opt/containerd-pool.img):" containerd_loop_file
1081+
_green "Where do you want to store the containerd loop file? (Enter to default: ${DEFAULT_CONTAINERD_LOOP_FILE}):"
1082+
reading "containerd 循环文件存储位置?(回车则默认:${DEFAULT_CONTAINERD_LOOP_FILE}):" containerd_loop_file
10531083
if [ -z "$containerd_loop_file" ]; then
1054-
containerd_loop_file="/opt/containerd-pool.img"
1084+
containerd_loop_file="$DEFAULT_CONTAINERD_LOOP_FILE"
10551085
fi
10561086
fi
10571087
else
@@ -1128,4 +1158,3 @@ main() {
11281158
}
11291159

11301160
main "$@"
1131-

containerduninstall.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,30 @@
55
# 完整卸载 containerd 环境及所有容器
66
#
77
# Supported environment variables (non-interactive mode / 支持的环境变量,可实现无交互卸载):
8+
# noninteractive=true - Skip confirmation prompt / 跳过确认提示直接卸载
89
# CONFIRM_UNINSTALL=yes - Skip confirmation prompt / 跳过确认提示直接卸载
910
#
1011
# Example / 示例:
12+
# export noninteractive=true
13+
# bash containerduninstall.sh
1114
# CONFIRM_UNINSTALL=yes bash containerduninstall.sh
1215

1316
_red() { echo -e "\033[31m\033[01m$*\033[0m"; }
1417
_green() { echo -e "\033[32m\033[01m$*\033[0m"; }
1518
_yellow() { echo -e "\033[33m\033[01m$*\033[0m"; }
1619
_blue() { echo -e "\033[36m\033[01m$*\033[0m"; }
20+
is_noninteractive() {
21+
case "$(printf '%s' "${noninteractive:-}" | tr '[:upper:]' '[:lower:]')" in
22+
true|yes|y|1) return 0 ;;
23+
*) return 1 ;;
24+
esac
25+
}
26+
is_yes() {
27+
case "$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')" in
28+
y|yes|true|1) return 0 ;;
29+
*) return 1 ;;
30+
esac
31+
}
1732

1833
if [ "$(id -u)" != "0" ]; then
1934
_red "This script must be run as root"
@@ -27,7 +42,10 @@ echo " 包含:所有运行中/停止的容器、所有镜像、"
2742
echo " CNI 网络、systemd 服务、nerdctl/containerd 二进制"
2843
echo " 操作不可逆!"
2944
echo "======================================================"
30-
if [[ "${CONFIRM_UNINSTALL:-}" == "yes" ]]; then
45+
if is_noninteractive; then
46+
confirm="yes"
47+
_blue "[non-interactive] noninteractive=true, proceeding with uninstall..."
48+
elif is_yes "${CONFIRM_UNINSTALL:-}"; then
3149
confirm="yes"
3250
_blue "[non-interactive] CONFIRM_UNINSTALL=yes, proceeding with uninstall..."
3351
else

0 commit comments

Comments
 (0)