虛擬化環境中,宿主機重啟後 VM 能否自動上線直接關業務連續性(RTO)。XenServer/XCP-ng 提供原生 auto_poweron 機制,結合 Pool 級與 VM 級雙重開關,配合 rc.local 兼容方案,可實現生產級「斷電自癒」。本文詳述標準配置流程、Windows VM 特殊處理、驗證腳本與常見坑點。
XenServer 的自動啟動邏輯:Pool 開關為總閘,VM 開關為分閘,兩者皆需 true 才生效。
# 獲取 Pool UUID
xe pool-list --minimal
# 或
POOL_UUID=$(xe pool-list --minimal)
# 開啟 Pool 級 auto_poweron
xe pool-param-set uuid=$POOL_UUID other-config:auto_poweron=true
# 列出所有 VM 及 UUID
xe vm-list params=name-label,uuid
# 為目標 VM 開啟 auto_poweron
xe vm-param-set uuid= other-config:auto_poweron=true
# 檢查 Pool 設定
xe pool-param-get uuid=$POOL_UUID param-name=other-config | grep auto_poweron
# 輸出應含:auto_poweron: true
# 檢查 VM 設定
xe vm-param-get uuid= param-name=other-config | grep auto_poweron
由於 Windows VM 的 XenServer Tools / PV 驅動載入時序差異,原生 auto_poweron 偶爾失效。通用保底方案:在宿主機 /etc/rc.local 添加延遲啟動指令。
/etc/rc.local(需執行權限)#!/bin/bash
# XenServer VM 自動啟動保底腳本
# 建議按業務依賴順序啟動,關鍵 VM 間隔 20-30 秒
sleep 30
xe vm-start uuid= # 數據庫/AD 等核心基礎設施
sleep 20
xe vm-start uuid= # 應用伺服器
sleep 20
xe vm-start uuid= # Web/負載均衡
# 記錄啟動日誌
echo "$(date) - Auto-start VMs triggered" >> /var/log/vm-autostart.log
chmod +x /etc/rc.local
systemctl enable rc-local
systemctl start rc-local
# 簡潔列表:名稱 | UUID | 電源狀態
xe vm-list params=name-label,uuid,power-state --minimal | tr ',' '\n' | paste - - - | column -t
# 僅顯示 Running VM
xe vm-list power-state=running params=name-label,uuid
若環境啟用了 XenServer HA (High Availability):
ha-restart-priority:restart/best-effort/)自動在存活宿主機上重啟 VM。auto_poweron 僅在整個 Pool 完全掉電後重啟時生效(HA 無法跨 Pool 故障)。ha-restart-priority=restart 和 auto_poweron=true,雙重保險。| 現象 | 可能原因 | 解決 |
|---|---|---|
| Pool 重啟後 VM 未啟動 | Pool 級開關未開 | xe pool-param-set ... auto_poweron=true |
| 部分 VM 未啟動 | VM 級開關未開 / VM 掛載 ISO/處於掛起狀態 | 檢查 VM other-config;xe vm-cd-eject;確保非 suspended |
| Windows VM 偶爾不啟動 | PV 驅動未就緒 / 開機順序競態 | 啟用 rc.local 延遲啟動方案 |
| rc.local 不執行 | 缺少執行權限 / systemd 未啟用 rc-local | chmod +x /etc/rc.local && systemctl enable rc-local |
| 啟動順序錯誤導致服務異常 | 無依賴順序控制 | rc.local 中按依賴鏈分級 sleep 啟動;或編寫 systemd 服務單元定義 After= 依賴 |
#!/bin/bash
# check_autostart.sh
POOL_UUID=$(xe pool-list --minimal)
POOL_AUTO=$(xe pool-param-get uuid=$POOL_UUID param-name=other-config | grep -c 'auto_poweron: true')
echo "Pool auto_poweron: $([ $POOL_AUTO -eq 1 ] && echo ENABLED || echo DISABLED)"
echo "VM auto_poweron status:"
xe vm-list params=name-label,uuid,power-state,other-config | \
awk -F': ' '/name-label|uuid|power-state|auto_poweron/ {print $2}' | \
paste - - - - | column -t | grep -v 'auto_poweron: $'
將 auto_poweron 納入基線合規檢查、新 VM 上架清單、災難恢復演練驗證項,確保「重啟即服務」成為常態而非僥倖。