diff options
Diffstat (limited to 'debian-container/lib')
| -rw-r--r-- | debian-container/lib/common.sh | 8 | ||||
| -rw-r--r-- | debian-container/lib/container.sh | 54 | ||||
| -rw-r--r-- | debian-container/lib/network.sh | 22 |
3 files changed, 76 insertions, 8 deletions
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 +} |
