From 940ab9def7850171d3f181afc2111ec8a2dc1ab7 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Thu, 29 Oct 2026 00:49:35 +0800 Subject: Add configurable host or isolated network modes for container entry and command execution. Signed-off-by: Richard Qin --- debian-container/lib/container.sh | 54 ++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 6 deletions(-) (limited to 'debian-container/lib/container.sh') 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" "$@" } -- cgit v1.2.3