aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2026-10-29 00:49:35 +0800
committerRichard Qin <richard.qin.x@gmail.com>2026-10-29 00:49:35 +0800
commit940ab9def7850171d3f181afc2111ec8a2dc1ab7 (patch)
tree869876f2f6c3a4695a3c1a4e371201f72f6cda91
parent7ab30ab3ee572de1be2afc3083e4a28b594b1cba (diff)
downloadrutils-940ab9def7850171d3f181afc2111ec8a2dc1ab7.tar.gz
rutils-940ab9def7850171d3f181afc2111ec8a2dc1ab7.tar.xz
Add configurable host or isolated network modes for container entry and command execution.
Signed-off-by: Richard Qin <richard.qin.x@gmail.com>
-rw-r--r--debian-container/.gitignore1
-rw-r--r--debian-container/README3
-rwxr-xr-xdebian-container/debian-container.sh1
-rw-r--r--debian-container/lib/common.sh8
-rw-r--r--debian-container/lib/container.sh54
-rw-r--r--debian-container/lib/network.sh22
6 files changed, 81 insertions, 8 deletions
diff --git a/debian-container/.gitignore b/debian-container/.gitignore
index f8dc340..8d0baad 100644
--- a/debian-container/.gitignore
+++ b/debian-container/.gitignore
@@ -1,3 +1,4 @@
rootfs.ext4
debian-root/
+instances/
*.tar.gz
diff --git a/debian-container/README b/debian-container/README
index 7c5b66f..7c5eea6 100644
--- a/debian-container/README
+++ b/debian-container/README
@@ -17,10 +17,12 @@ sudo ./debian-container.sh init test-2 --tarball ./debian-stable.tar.gz
进入容器:
sudo ./debian-container.sh enter test-1
+sudo ./debian-container.sh enter test-1 --network host
在容器中执行命令:
sudo ./debian-container.sh exec test-1 -- uname -a
+sudo ./debian-container.sh exec test-1 --network host -- apt-get update
列出和查看容器:
@@ -58,5 +60,6 @@ DEBIAN_TARBALL 默认基础 tarball 路径
DEBIAN_SUITE Debian 版本,默认 stable
DEBIAN_MIRROR Debian 镜像地址
DEBIAN_INCLUDE 额外软件包,使用逗号分隔
+DEBIAN_NETWORK 默认网络模式,none 或 host
脚本实现按职责拆分在 lib/ 目录中,入口脚本只负责加载模块和分发命令。
diff --git a/debian-container/debian-container.sh b/debian-container/debian-container.sh
index 5838fde..6ef4133 100755
--- a/debian-container/debian-container.sh
+++ b/debian-container/debian-container.sh
@@ -10,6 +10,7 @@ DC_DIR=$(dirname -- "$DC_SCRIPT")
source "$DC_DIR/lib/config.sh"
source "$DC_DIR/lib/common.sh"
+source "$DC_DIR/lib/network.sh"
source "$DC_DIR/lib/mount.sh"
source "$DC_DIR/lib/container.sh"
diff --git a/debian-container/lib/common.sh b/debian-container/lib/common.sh
index 12a318b..a3c0486 100644
--- a/debian-container/lib/common.sh
+++ b/debian-container/lib/common.sh
@@ -51,8 +51,8 @@ dc_usage() {
命令:
base-create [路径] 下载一次并创建 debootstrap 软件包 tarball
init NAME [选项] 创建指定名称的容器镜像
- enter NAME 进入指定容器
- exec NAME -- CMD 在指定容器中执行命令
+ enter NAME [选项] 进入指定容器
+ exec NAME [选项] 在指定容器中执行命令
status NAME 查看指定容器状态
list 列出所有容器
delete NAME 删除指定容器
@@ -60,6 +60,9 @@ dc_usage() {
init 选项:
--tarball PATH 从本地 tarball 初始化,跳过软件包下载
+网络选项:
+ --network MODE 网络模式:none(默认)或 host
+
环境变量:
DEBIAN_INSTANCES 实例目录(默认:当前目录/instances)
DEBIAN_LOCK_FILE 锁文件路径(默认:/run/lock/debian-container.lock)
@@ -67,6 +70,7 @@ init 选项:
DEBIAN_SUITE Debian 版本(默认:stable)
DEBIAN_MIRROR Debian 镜像地址
DEBIAN_INCLUDE 额外软件包,使用逗号分隔
+ DEBIAN_NETWORK 默认网络模式(默认:none)
EOF
}
diff --git a/debian-container/lib/container.sh b/debian-container/lib/container.sh
index b3563eb..a03af32 100644
--- a/debian-container/lib/container.sh
+++ b/debian-container/lib/container.sh
@@ -100,10 +100,30 @@ dc_base_create() {
}
dc_enter() {
+ local network_mode=$DC_NETWORK_MODE
+
+ while (($# > 0)); do
+ case "$1" in
+ --network)
+ (($# >= 2)) || dc_die '--network 需要一个模式。'
+ network_mode=$2
+ shift 2
+ ;;
+ --help|-h)
+ dc_usage
+ return 0
+ ;;
+ *)
+ dc_die "未知的 enter 选项:$1"
+ ;;
+ esac
+ done
+
[[ -f "$DC_IMAGE" ]] || dc_die '没有找到镜像,请先运行 init。'
+ dc_network_prepare "$network_mode"
- # 单独创建网络命名空间,暂时没有联网功能。
- unshare --mount --uts --ipc --pid --net --fork --propagation private -- bash "$DC_SCRIPT" _enter "$DC_INSTANCE"
+ unshare --mount --uts --ipc --pid --fork --propagation private \
+ "${DC_NETWORK_UNSHARE_ARGS[@]}" -- bash "$DC_SCRIPT" _enter "$DC_INSTANCE"
}
dc_enter_rootfs() {
@@ -119,13 +139,35 @@ dc_enter_rootfs() {
}
dc_exec() {
- (($# > 0)) || dc_die '用法:exec NAME -- 命令 [参数...]'
- [[ "$1" == -- ]] || dc_die '用法:exec NAME -- 命令 [参数...]'
- shift
+ local network_mode=$DC_NETWORK_MODE
+
+ while (($# > 0)); do
+ case "$1" in
+ --network)
+ (($# >= 2)) || dc_die '--network 需要一个模式。'
+ network_mode=$2
+ shift 2
+ ;;
+ --)
+ shift
+ break
+ ;;
+ --help|-h)
+ dc_usage
+ return 0
+ ;;
+ *)
+ dc_die '用法:exec NAME [--network MODE] -- 命令 [参数...]'
+ ;;
+ esac
+ done
+
(($# > 0)) || dc_die 'exec 需要一个命令。'
[[ -f "$DC_IMAGE" ]] || dc_die '没有找到镜像,请先运行 init。'
+ dc_network_prepare "$network_mode"
- unshare --mount --uts --ipc --pid --net --fork --propagation private -- \
+ unshare --mount --uts --ipc --pid --fork --propagation private \
+ "${DC_NETWORK_UNSHARE_ARGS[@]}" -- \
bash "$DC_SCRIPT" _exec "$DC_INSTANCE" "$@"
}
diff --git a/debian-container/lib/network.sh b/debian-container/lib/network.sh
new file mode 100644
index 0000000..333869f
--- /dev/null
+++ b/debian-container/lib/network.sh
@@ -0,0 +1,22 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: GPL-3.0-or-later
+# Copyright (C) 2026 by Richard Qin
+
+DC_NETWORK_MODE=${DEBIAN_NETWORK:-none}
+DC_NETWORK_UNSHARE_ARGS=()
+
+dc_network_prepare() {
+ local mode=${1:-$DC_NETWORK_MODE}
+
+ case "$mode" in
+ none)
+ DC_NETWORK_UNSHARE_ARGS=(--net)
+ ;;
+ host)
+ DC_NETWORK_UNSHARE_ARGS=()
+ ;;
+ *)
+ dc_die "网络模式无效:$mode(可选:none、host)"
+ ;;
+ esac
+}