diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-10-29 00:49:35 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-10-29 00:49:35 +0800 |
| commit | 940ab9def7850171d3f181afc2111ec8a2dc1ab7 (patch) | |
| tree | 869876f2f6c3a4695a3c1a4e371201f72f6cda91 /debian-container/lib/container.sh | |
| parent | 7ab30ab3ee572de1be2afc3083e4a28b594b1cba (diff) | |
| download | rutils-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>
Diffstat (limited to 'debian-container/lib/container.sh')
| -rw-r--r-- | debian-container/lib/container.sh | 54 |
1 files changed, 48 insertions, 6 deletions
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" "$@" } |
