diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-10-29 01:08:39 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-10-29 01:08:39 +0800 |
| commit | d9932da9c00fd92aaa0cafa5d6d63a6f8e724ad1 (patch) | |
| tree | 623ff5183c149cff592183d602e8b664dec55fed | |
| parent | 940ab9def7850171d3f181afc2111ec8a2dc1ab7 (diff) | |
| download | rutils-d9932da9c00fd92aaa0cafa5d6d63a6f8e724ad1.tar.gz rutils-d9932da9c00fd92aaa0cafa5d6d63a6f8e724ad1.tar.xz | |
Add configurable architecture, package inclusion, exclusion, and debootstrap options for base tarballs.
Signed-off-by: Richard Qin <richard.qin.x@gmail.com>
| -rw-r--r-- | debian-container/README | 8 | ||||
| -rw-r--r-- | debian-container/lib/common.sh | 12 | ||||
| -rw-r--r-- | debian-container/lib/config.sh | 2 | ||||
| -rw-r--r-- | debian-container/lib/container.sh | 60 |
4 files changed, 75 insertions, 7 deletions
diff --git a/debian-container/README b/debian-container/README index 7c5eea6..ef3bb97 100644 --- a/debian-container/README +++ b/debian-container/README @@ -7,7 +7,11 @@ Debian 容器 第一次创建基础软件包 tarball: -sudo ./debian-container.sh base-create ./debian-stable.tar.gz +sudo ./debian-container.sh base-create \ + --output ./debian-stable.tar.gz \ + --suite stable \ + --arch amd64 \ + --include sudo,vim-tiny,curl 从 tarball 创建容器: @@ -58,8 +62,10 @@ DEBIAN_INSTANCES 实例目录 DEBIAN_LOCK_FILE 锁文件路径 DEBIAN_TARBALL 默认基础 tarball 路径 DEBIAN_SUITE Debian 版本,默认 stable +DEBIAN_ARCH 目标架构 DEBIAN_MIRROR Debian 镜像地址 DEBIAN_INCLUDE 额外软件包,使用逗号分隔 +DEBIAN_EXCLUDE 要排除的软件包,使用逗号分隔 DEBIAN_NETWORK 默认网络模式,none 或 host 脚本实现按职责拆分在 lib/ 目录中,入口脚本只负责加载模块和分发命令。 diff --git a/debian-container/lib/common.sh b/debian-container/lib/common.sh index a3c0486..d28948a 100644 --- a/debian-container/lib/common.sh +++ b/debian-container/lib/common.sh @@ -49,7 +49,7 @@ dc_usage() { 用法:sudo bash $DC_SCRIPT <命令> 命令: - base-create [路径] 下载一次并创建 debootstrap 软件包 tarball + base-create [选项] 下载一次并创建 debootstrap 软件包 tarball init NAME [选项] 创建指定名称的容器镜像 enter NAME [选项] 进入指定容器 exec NAME [选项] 在指定容器中执行命令 @@ -60,6 +60,14 @@ dc_usage() { init 选项: --tarball PATH 从本地 tarball 初始化,跳过软件包下载 +base-create 选项: + --output PATH 输出 tarball 路径 + --suite SUITE Debian 版本 + --arch ARCH 目标架构 + --mirror URL Debian 镜像地址 + --include PACKAGES 要加入的软件包,使用逗号分隔 + --exclude PACKAGES 要排除的软件包,使用逗号分隔 + 网络选项: --network MODE 网络模式:none(默认)或 host @@ -68,8 +76,10 @@ init 选项: DEBIAN_LOCK_FILE 锁文件路径(默认:/run/lock/debian-container.lock) DEBIAN_TARBALL 基础 tarball(默认:当前目录/debian-base.tar.gz) DEBIAN_SUITE Debian 版本(默认:stable) + DEBIAN_ARCH 目标架构 DEBIAN_MIRROR Debian 镜像地址 DEBIAN_INCLUDE 额外软件包,使用逗号分隔 + DEBIAN_EXCLUDE 要排除的软件包,使用逗号分隔 DEBIAN_NETWORK 默认网络模式(默认:none) EOF } diff --git a/debian-container/lib/config.sh b/debian-container/lib/config.sh index 6efcab2..0d91aad 100644 --- a/debian-container/lib/config.sh +++ b/debian-container/lib/config.sh @@ -7,8 +7,10 @@ DC_INSTANCES=${DEBIAN_INSTANCES:-"$PWD/instances"} DC_LOCK_FILE=${DEBIAN_LOCK_FILE:-/run/lock/debian-container.lock} DC_TARBALL=${DEBIAN_TARBALL:-"$PWD/debian-base.tar.gz"} DC_SUITE=${DEBIAN_SUITE:-stable} +DC_ARCH=${DEBIAN_ARCH:-} DC_MIRROR=${DEBIAN_MIRROR:-http://deb.debian.org/debian} DC_INCLUDE=${DEBIAN_INCLUDE:-passwd,util-linux,vim-tiny,e2fsprogs} +DC_EXCLUDE=${DEBIAN_EXCLUDE:-} DC_INSTANCE= DC_INSTANCE_DIR= diff --git a/debian-container/lib/container.sh b/debian-container/lib/container.sh index a03af32..2c0aaad 100644 --- a/debian-container/lib/container.sh +++ b/debian-container/lib/container.sh @@ -76,10 +76,57 @@ dc_init_rootfs() { } dc_base_create() { - local output=${1:-$DC_TARBALL} + local output=$DC_TARBALL + local suite=$DC_SUITE + local arch=$DC_ARCH + local mirror=$DC_MIRROR + local include=$DC_INCLUDE + local exclude=$DC_EXCLUDE local temporary_root + local -a debootstrap_options=(--variant=minbase) + + while (($# > 0)); do + case "$1" in + --output) + (($# >= 2)) || dc_die '--output 需要一个文件路径。' + output=$2 + shift 2 + ;; + --suite) + (($# >= 2)) || dc_die '--suite 需要一个版本名称。' + suite=$2 + shift 2 + ;; + --arch) + (($# >= 2)) || dc_die '--arch 需要一个架构名称。' + arch=$2 + shift 2 + ;; + --mirror) + (($# >= 2)) || dc_die '--mirror 需要一个镜像地址。' + mirror=$2 + shift 2 + ;; + --include) + (($# >= 2)) || dc_die '--include 需要软件包列表。' + include=$2 + shift 2 + ;; + --exclude) + (($# >= 2)) || dc_die '--exclude 需要软件包列表。' + exclude=$2 + shift 2 + ;; + --help|-h) + dc_usage + return 0 + ;; + *) + dc_die "未知的 base-create 选项:$1" + ;; + esac + done - (($# <= 1)) || dc_die '用法:base-create [tarball 路径]' output=$(readlink -m -- "$output") [[ ! -e "$output" ]] || dc_die "tarball 已存在:$output" @@ -87,13 +134,16 @@ dc_base_create() { mkdir -p "$(dirname -- "$output")" temporary_root=$(mktemp -d) + [[ -n "$arch" ]] && debootstrap_options+=("--arch=$arch") + [[ -n "$include" ]] && debootstrap_options+=("--include=$include") + [[ -n "$exclude" ]] && debootstrap_options+=("--exclude=$exclude") + ( trap 'rm -rf -- "$temporary_root"' EXIT debootstrap \ - --variant=minbase \ - --include="$DC_INCLUDE" \ + "${debootstrap_options[@]}" \ --make-tarball="$output" \ - "$DC_SUITE" "$temporary_root" "$DC_MIRROR" + "$suite" "$temporary_root" "$mirror" ) echo "基础 tarball 已创建:$output" |
