From e7236907dfe3e9130d532d5e8a7f2b17af13df1c Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Wed, 28 Oct 2026 23:13:09 +0800 Subject: Refactor Debian container script into modular components with configurable paths and documentation. Signed-off-by: Richard Qin --- debian-container/lib/common.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 debian-container/lib/common.sh (limited to 'debian-container/lib/common.sh') diff --git a/debian-container/lib/common.sh b/debian-container/lib/common.sh new file mode 100644 index 0000000..1fd08b0 --- /dev/null +++ b/debian-container/lib/common.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: GPL-3.0-or-later +# Copyright (C) 2026 by Richard Qin + +dc_die() { + echo "$*" >&2 + exit 1 +} + +dc_require_root() { + [[ ${EUID} -eq 0 ]] || dc_die '请使用 sudo 执行此脚本。' +} + +dc_require_commands() { + local tool + for tool in "$@"; do + command -v "$tool" >/dev/null || dc_die "缺少工具:$tool" + done +} + +dc_acquire_lock() { + mkdir -p "$(dirname -- "$DC_LOCK_FILE")" + exec 9>"$DC_LOCK_FILE" + flock -n 9 || dc_die 'Debian 容器已经在运行或正在初始化。' +} + +dc_usage() { + cat < + +命令: + init 创建 Debian 容器镜像 + enter 进入容器 + delete 删除容器镜像 + +环境变量: + DEBIAN_IMAGE 镜像路径(默认:当前目录/rootfs.ext4) + DEBIAN_ROOT 挂载目录(默认:当前目录/debian-root) + DEBIAN_LOCK_FILE 锁文件路径 +EOF +} + -- cgit v1.2.3