diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-10-29 02:12:35 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-10-29 02:12:35 +0800 |
| commit | 298dd9150ec8bc92af1bc1685726e1cd98d16b49 (patch) | |
| tree | dac8b52571821433123fb56ecc32b98fa229d996 /debian-container/lib/container.sh | |
| parent | d8bfc59e2f5dc0728326ae7a143f8fdc113932e2 (diff) | |
| download | rutils-298dd9150ec8bc92af1bc1685726e1cd98d16b49.tar.gz rutils-298dd9150ec8bc92af1bc1685726e1cd98d16b49.tar.xz | |
Add configurable host directory bind mounts 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 | 44 |
1 files changed, 41 insertions, 3 deletions
diff --git a/debian-container/lib/container.sh b/debian-container/lib/container.sh index eee6d76..5f040e3 100644 --- a/debian-container/lib/container.sh +++ b/debian-container/lib/container.sh @@ -181,6 +181,7 @@ dc_base_create() { dc_enter() { local network_mode=$DC_NETWORK_MODE + local -a bind_args=() while (($# > 0)); do case "$1" in @@ -189,6 +190,12 @@ dc_enter() { network_mode=$2 shift 2 ;; + --mount) + (($# >= 2)) || dc_die '--mount 需要一个挂载参数。' + dc_bind_add "$2" + bind_args+=(--mount "${DC_BIND_SPECS[$((${#DC_BIND_SPECS[@]} - 1))]}") + shift 2 + ;; --help|-h) dc_usage return 0 @@ -203,13 +210,15 @@ dc_enter() { dc_network_prepare "$network_mode" unshare --mount --uts --ipc --pid --fork --propagation private \ - "${DC_NETWORK_UNSHARE_ARGS[@]}" -- bash "$DC_SCRIPT" _enter "$DC_INSTANCE" + "${DC_NETWORK_UNSHARE_ARGS[@]}" -- bash "$DC_SCRIPT" _enter "$DC_INSTANCE" "${bind_args[@]}" } dc_enter_rootfs() { + dc_bind_load_options "$@" dc_mount_root trap dc_cleanup_mounts EXIT dc_mount_virtual_filesystems + dc_bind_mount_all hostname debian-container echo '已进入独立的 Debian 实验环境;输入 exit 可退出容器。' @@ -220,6 +229,7 @@ dc_enter_rootfs() { dc_exec() { local network_mode=$DC_NETWORK_MODE + local -a bind_args=() while (($# > 0)); do case "$1" in @@ -228,6 +238,12 @@ dc_exec() { network_mode=$2 shift 2 ;; + --mount) + (($# >= 2)) || dc_die '--mount 需要一个挂载参数。' + dc_bind_add "$2" + bind_args+=(--mount "${DC_BIND_SPECS[$((${#DC_BIND_SPECS[@]} - 1))]}") + shift 2 + ;; --) shift break @@ -248,17 +264,39 @@ dc_exec() { unshare --mount --uts --ipc --pid --fork --propagation private \ "${DC_NETWORK_UNSHARE_ARGS[@]}" -- \ - bash "$DC_SCRIPT" _exec "$DC_INSTANCE" "$@" + bash "$DC_SCRIPT" _exec "$DC_INSTANCE" "${bind_args[@]}" -- "$@" } dc_exec_rootfs() { + local -a command_args=() + + while (($# > 0)); do + case "$1" in + --mount) + (($# >= 2)) || dc_die '--mount 需要一个挂载参数。' + dc_bind_add "$2" + shift 2 + ;; + --) + shift + command_args=("$@") + break + ;; + *) + dc_die 'exec 子进程参数格式无效。' + ;; + esac + done + + (($# > 0)) || dc_die 'exec 需要一个命令。' dc_mount_root trap dc_cleanup_mounts EXIT dc_mount_virtual_filesystems + dc_bind_mount_all hostname debian-container setpriv --bounding-set=-sys_admin,-net_admin,-net_raw,-sys_module,-sys_boot,-sys_time,-sys_rawio,-sys_ptrace,-syslog -- \ - chroot "$DC_ROOT" "$@" + chroot "$DC_ROOT" "${command_args[@]}" } dc_status() { |
