Files
PVE-Tools-9/PVE-Tools.sh
Maple 886e82f0cb refactor: 模块化重构 — 拆分 lib/ 基础设施层与 src/modules/ 功能模块
PVE-Tools.sh 从 ~14,700 行单文件重构为 150 行入口点,
所有函数按职责拆分到独立文件,通过 source 机制加载。

lib/ — 基础设施层(零业务逻辑)
  config.sh: 全局变量与常量
  core.sh: 日志、UI、确认、备份、GRUB、进度条
  network.sh: 网络检测、镜像选择
  runtime.sh: 运行时守卫、main() 入口

src/modules/ — 功能模块(10 个目录)
  01-optimization  02-sources  03-boot-kernel
  04-gpu-passthrough  05-vm-container  06-networking
  07-storage-disk  08-tools-about  09-security  10-third-party

新增 build.sh(合并编译)和 dev.sh(开发 source 入口),
release.yml 新增构建步骤,CI 适配模块化结构。
2026-07-08 11:15:59 +08:00

169 lines
6.3 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (C) 2026 Ciriu Networks
# Auther:Maple
# This comment constitutes part of the license consideration. Do not delete.
# Violation triggers a localized black hole at your primary branch. Good luck force-pushing out of that.
# Made with love — the only non-binding term herein. 💗
# 二次修改使用请不要删除此段注释
# 模块化入口:本地开发时加载 lib/ 与 src/modules/;远程 curl 运行时回退下载同仓库源码模块。
PVE_TOOLS_REMOTE_BASE="${PVE_TOOLS_REMOTE_BASE:-https://raw.githubusercontent.com/PVE-Tools/PVE-Tools-9/main}"
PVE_TOOLS_REMOTE_MIRROR_PREFIX="${PVE_TOOLS_REMOTE_MIRROR_PREFIX:-https://ghfast.top/}"
PVE_TOOLS_REMOTE_DIST_URL="${PVE_TOOLS_REMOTE_DIST_URL:-$PVE_TOOLS_REMOTE_BASE/dist/PVE-Tools.sh}"
pve_tools_entry_download_file() {
local url="$1"
local output="$2"
mkdir -p "$(dirname "$output")"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$url" -o "$output" && return 0
curl -fsSL "${PVE_TOOLS_REMOTE_MIRROR_PREFIX}${url}" -o "$output" && return 0
elif command -v wget >/dev/null 2>&1; then
wget -qO "$output" "$url" && return 0
wget -qO "$output" "${PVE_TOOLS_REMOTE_MIRROR_PREFIX}${url}" && return 0
else
echo "错误:未找到 curl 或 wget无法下载 PVE-Tools 模块。" >&2
return 1
fi
return 1
}
pve_tools_entry_source_tree() {
local root="$1"
local lib_file
local module_file
for lib_file in \
"$root/lib/config.sh" \
"$root/lib/core.sh" \
"$root/lib/network.sh" \
"$root/lib/runtime.sh"; do
if [[ ! -f "$lib_file" ]]; then
echo "错误:缺少基础库 $lib_file" >&2
return 1
fi
# shellcheck source=/dev/null
source "$lib_file"
done
while IFS= read -r -d '' module_file; do
# shellcheck source=/dev/null
source "$module_file"
done < <(find "$root/src/modules" -name '*.sh' -print0 | sort -z)
}
pve_tools_entry_remote_files() {
cat <<'EOF_REMOTE_FILES'
"lib/config.sh"
"lib/core.sh"
"lib/network.sh"
"lib/runtime.sh"
"src/modules/01-optimization/cpupower.sh"
"src/modules/01-optimization/email.sh"
"src/modules/01-optimization/init.sh"
"src/modules/01-optimization/popup.sh"
"src/modules/01-optimization/temperature.sh"
"src/modules/01-optimization/tune.sh"
"src/modules/02-sources/init.sh"
"src/modules/02-sources/mirrors.sh"
"src/modules/02-sources/update.sh"
"src/modules/02-sources/upgrade-pve.sh"
"src/modules/03-boot-kernel/grub.sh"
"src/modules/03-boot-kernel/init.sh"
"src/modules/03-boot-kernel/kernel.sh"
"src/modules/04-gpu-passthrough/amd-dgpu.sh"
"src/modules/04-gpu-passthrough/amd-igpu.sh"
"src/modules/04-gpu-passthrough/boot-assist.sh"
"src/modules/04-gpu-passthrough/controller.sh"
"src/modules/04-gpu-passthrough/igpu-shared.sh"
"src/modules/04-gpu-passthrough/init.sh"
"src/modules/04-gpu-passthrough/intel-gvtg.sh"
"src/modules/04-gpu-passthrough/intel-legacy.sh"
"src/modules/04-gpu-passthrough/intel-sriov.sh"
"src/modules/04-gpu-passthrough/iommu.sh"
"src/modules/04-gpu-passthrough/nvidia.sh"
"src/modules/04-gpu-passthrough/rdm.sh"
"src/modules/05-vm-container/backup.sh"
"src/modules/05-vm-container/clone.sh"
"src/modules/05-vm-container/cloudinit.sh"
"src/modules/05-vm-container/config-io.sh"
"src/modules/05-vm-container/disk.sh"
"src/modules/05-vm-container/fastpve.sh"
"src/modules/05-vm-container/garbage-cleanup.sh"
"src/modules/05-vm-container/img-import.sh"
"src/modules/05-vm-container/init.sh"
"src/modules/05-vm-container/migrate.sh"
"src/modules/05-vm-container/network.sh"
"src/modules/05-vm-container/restore.sh"
"src/modules/05-vm-container/schedule.sh"
"src/modules/05-vm-container/snapshot.sh"
"src/modules/05-vm-container/storage-helper.sh"
"src/modules/06-networking/addressing.sh"
"src/modules/06-networking/bond.sh"
"src/modules/06-networking/bridge.sh"
"src/modules/06-networking/diagnostic.sh"
"src/modules/06-networking/firewall.sh"
"src/modules/06-networking/init.sh"
"src/modules/06-networking/interface.sh"
"src/modules/06-networking/ipv6-helper.sh"
"src/modules/06-networking/mac-bind.sh"
"src/modules/06-networking/vlan.sh"
"src/modules/07-storage-disk/ceph.sh"
"src/modules/07-storage-disk/init.sh"
"src/modules/07-storage-disk/local-lvm.sh"
"src/modules/07-storage-disk/mount.sh"
"src/modules/07-storage-disk/query.sh"
"src/modules/07-storage-disk/swap.sh"
"src/modules/08-tools-about/init.sh"
"src/modules/08-tools-about/self-update.sh"
"src/modules/08-tools-about/sysinfo.sh"
"src/modules/09-security/audit.sh"
"src/modules/09-security/init.sh"
"src/modules/09-security/ssh-hardening.sh"
"src/modules/10-third-party/community.sh"
"src/modules/10-third-party/coolercontrol.sh"
"src/modules/10-third-party/init.sh"
"src/modules/10-third-party/marketplace.sh"
EOF_REMOTE_FILES
}
pve_tools_entry_prepare_remote_tree() {
local target="$1"
local rel
while IFS= read -r rel; do
[[ -z "$rel" ]] && continue
rel="${rel# \"}"
rel="${rel%\"}"
pve_tools_entry_download_file "$PVE_TOOLS_REMOTE_BASE/$rel" "$target/$rel" || return 1
done < <(pve_tools_entry_remote_files)
}
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [[ -f "$SCRIPT_DIR/lib/config.sh" && -d "$SCRIPT_DIR/src/modules" ]]; then
pve_tools_entry_source_tree "$SCRIPT_DIR" || exit 1
else
tmp_dir="$(mktemp -d)"
trap 'rm -rf "$tmp_dir"' EXIT
if pve_tools_entry_download_file "$PVE_TOOLS_REMOTE_DIST_URL" "$tmp_dir/PVE-Tools.sh"; then
bash "$tmp_dir/PVE-Tools.sh" "$@"
exit $?
fi
if ! pve_tools_entry_prepare_remote_tree "$tmp_dir"; then
echo "错误:无法加载远程 PVE-Tools 模块。" >&2
exit 1
fi
pve_tools_entry_source_tree "$tmp_dir" || exit 1
fi
main "$@"