fix: address CUPS runtime review findings and add regression tests

This commit is contained in:
Codex
2026-09-11 13:19:37 +08:00
parent 6051b05676
commit 5f49605d24
7 changed files with 490 additions and 38 deletions
+42 -32
View File
@@ -77,16 +77,15 @@ fix_apt_lock() {
local count=0
while pgrep -x "unattended-upgr" > /dev/null 2>&1 && [ $count -lt 30 ]; do
sleep 1
((count++))
count=$((count + 1))
echo -ne "\r 等待自动更新进程结束... ${count}s"
done
echo ""
# 如果还在运行,强制结束
# 不强制中断正在写入软件包数据库的进程。
if pgrep -x "unattended-upgr" > /dev/null 2>&1; then
warn "强制结束自动更新进程..."
killall unattended-upgr 2>/dev/null || true
sleep 2
warn "等待自动更新超时,请稍后重新运行安装脚本"
return 1
fi
success "自动更新服务已停止"
@@ -98,17 +97,17 @@ fix_apt_lock() {
local count=0
while fuser /var/lib/dpkg/lock-frontend > /dev/null 2>&1 && [ $count -lt 60 ]; do
sleep 1
((count++))
count=$((count + 1))
echo -ne "\r 等待 apt 锁释放... ${count}s"
done
echo ""
if fuser /var/lib/dpkg/lock-frontend > /dev/null 2>&1; then
warn "等待 apt 锁超时,请稍后重新运行安装脚本"
return 1
fi
fi
# 清理可能残留的锁文件
rm -f /var/lib/dpkg/lock-frontend 2>/dev/null || true
rm -f /var/lib/dpkg/lock 2>/dev/null || true
rm -f /var/cache/apt/archives/lock 2>/dev/null || true
# 锁由内核随进程释放;删除锁文件会破坏其他 apt/dpkg 进程的互斥。
# 修复可能中断的安装
dpkg --configure -a 2>/dev/null || true
@@ -244,23 +243,28 @@ EOF
# 获取本机IP地址
get_ip() {
ip addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v '127.0.0.1' | head -n1
local iface="${1:-$(get_interface)}"
[ -n "$iface" ] || return 1
ip -4 -o addr show dev "$iface" scope global | awk '{split($4, address, "/"); print address[1]; exit}'
}
# 获取默认网关
get_gateway() {
ip route | grep default | awk '{print $3}' | head -n1
local iface="${1:-$(get_interface)}"
[ -n "$iface" ] || return 1
ip -4 route show default dev "$iface" | awk '{for (i=1; i<NF; i++) if ($i == "via") {print $(i+1); exit}}'
}
# 获取主网络接口名称
get_interface() {
ip route | grep default | awk '{print $5}' | head -n1
ip -4 route show default | awk '{for (i=1; i<NF; i++) if ($i == "dev") {print $(i+1); exit}}'
}
# 获取子网掩码(CIDR格式)
get_netmask() {
local iface=$(get_interface)
ip addr show "$iface" | grep -oP 'inet \K[\d.]+/\d+' | head -n1 | cut -d'/' -f2
local iface="${1:-$(get_interface)}"
[ -n "$iface" ] || return 1
ip -4 -o addr show dev "$iface" scope global | awk '{split($4, address, "/"); print address[2]; exit}'
}
# 获取DNS服务器
@@ -277,12 +281,13 @@ configure_static_ip() {
echo -e "${YELLOW}═══════════════════════════════════════════════════════════${NC}"
echo ""
local current_ip=$(get_ip)
local current_gateway=$(get_gateway)
local current_interface=$(get_interface)
local current_netmask=$(get_netmask)
local current_ip=$(get_ip "$current_interface")
local current_gateway=$(get_gateway "$current_interface")
local current_netmask=$(get_netmask "$current_interface")
local current_dns=$(get_dns)
[ -n "$current_interface" ] || error "未找到 IPv4 默认路由对应的网卡,无法设置静态 IP"
echo -e "${BLUE}当前网络配置:${NC}"
echo " 接口名称: $current_interface"
echo " IP 地址: $current_ip"
@@ -305,6 +310,9 @@ configure_static_ip() {
local static_dns=""
if [[ "$use_current" =~ ^[Yy]$ ]]; then
if [ -z "$current_ip" ] || [ -z "$current_netmask" ] || [ -z "$current_gateway" ]; then
error "当前网卡的 IPv4 地址、掩码或网关不完整,未修改网络配置"
fi
static_ip="$current_ip"
static_gateway="$current_gateway"
static_netmask="$current_netmask"
@@ -1368,10 +1376,10 @@ install_watchdogs() {
[ -n "$tmp_dir" ] && rm -rf "$tmp_dir"
local current_ip=$(get_ip)
local current_gateway=$(get_gateway)
local current_interface=$(get_interface)
local current_netmask=$(get_netmask)
local current_ip=$(get_ip "$current_interface")
local current_gateway=$(get_gateway "$current_interface")
local current_netmask=$(get_netmask "$current_interface")
local current_dns=$(get_dns)
local current_host_octet="${current_ip##*.}"
current_dns="${current_dns:-114.114.114.114 223.5.5.5}"
@@ -1451,24 +1459,26 @@ install_pdf_printer() {
else
# 添加 PDF 虚拟打印机
# 使用 lpadmin 命令创建打印机
lpadmin -p PDF \
if ! lpadmin -p PDF \
-v cups-pdf:/ \
-E \
-m lsb/usr/cups-pdf/CUPS-PDF_opt.ppd \
-D "虚拟PDF打印机 (测试用)" \
-L "本地" \
-o printer-is-shared=true
# 如果上面的 PPD 文件不存在,尝试其他路径
if ! lpstat -p PDF 2>/dev/null | grep -q "PDF"; then
# 尝试使用通用 PPD
lpadmin -p PDF \
-o printer-is-shared=true; then
# 从 CUPS 实际提供的模型中选备用 PPD,不创建无法转换 PDF 的 raw 队列。
local pdf_model
pdf_model=$(lpinfo -m 2>/dev/null | awk 'tolower($0) ~ /cups-pdf/ && $1 != "lsb/usr/cups-pdf/CUPS-PDF_opt.ppd" {print $1; exit}')
if [ -z "$pdf_model" ] || ! lpadmin -p PDF \
-v cups-pdf:/ \
-E \
-m raw \
-m "$pdf_model" \
-D "虚拟PDF打印机 (测试用)" \
-L "本地" \
-o printer-is-shared=true 2>/dev/null || true
-o printer-is-shared=true; then
warn "未能使用可用的 CUPS-PDF 驱动创建队列,跳过虚拟 PDF 打印机"
return 0
fi
fi
# 启用打印机
@@ -1596,7 +1606,7 @@ main() {
show_banner
check_root
LOCAL_IP=$(get_ip)
LOCAL_IP=$(get_ip) || error "未找到 IPv4 默认路由对应的网卡"
info "检测到本机IP: ${LOCAL_IP}"
echo ""