惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

WuSiYu Blog

Arduino UNO Q 开发板初体验 - WuSiYu Blog 解决Open WebUI接入Qwen3.5/3.6模型后无法自动生成对话标题的问题 - WuSiYu Blog 世界加钱可及?若干 AWS / GreenCloud / Lightlayer / RFCHOST / SiliCloud / Vultr 的日本/新加坡/香港/美国VPS全天ping测试 - WuSiYu Blog 让UniFi中的拓扑图正确显示第三方交换机和虚拟机 站点迁移后测试中 虚拟机检测绕过和nvme性能优化的libvirt配置 UCG Fiber 主路由器 + OpenWrt 透明代理 + 高可用 方案和评测(对比ROS) OpenWrt上AdGuardHome前置配置的一些小坑 Unraid 7.2+ WebUI美化主题和自定义CSS插件 杂谈:libvirt/qemu Windows游戏VM的一些优化配置 群晖ddrescue与暂时禁用USB外接硬盘自动挂载 OpenWrt One 路由器(MT7981,主线op)超频bl2编译教程 白群晖之 在“不受支持”的机型上启用btrfs和快照功能 解决群晖Photos iOS客户端备份大视频时报错“备份已暂停-空间不足”
Fedora + libvirt 下在宿主关机时安全关闭虚拟机
SiYu Wu · 2025-07-29 · via WuSiYu Blog

很简单的一个问题,但过程比较难绷,所以特此记录

为了能方便的既跑虚拟机,又跑Docker容器,我经常使用Fedora Server来作为宿主系统,配合红帽官方的Cockpit面板进行管理,其对虚拟机(基于libvirt/qemu/kvm)和容器(官方只支持Podman)的支持尚可。

但其在关机时虚拟机会直接被强制停止,并不安全。我们希望在直接对宿主机进行关机或重启时,其能够像PVE、EXSi等虚拟化系统一样发起并等待虚拟机的安全关机。

首先这个功能libvirt是提供了的,由libvirt-guests.service实现,但不知为何在Fedora并没有被默认启用。

进一步的,我们发现其配置文件/etc/sysconfig/libvirt-guests从某个版本起被移除了(你还能搜到关于这件事的相关讨论,暴躁用户 vs 倔强maintainer),更不方便我们配置了,因此首先恢复其内容:

# URIs to check for running guests
# example: URIS='default xen:/// vbox+tcp://host/system lxc:///'
#URIS=default

# action taken on host boot
# - start   all guests which were running on shutdown are started on boot
#           regardless on their autostart settings                                 
# - ignore  libvirt-guests init script won't start any guest on boot, however,     
#           guests marked as autostart will still be automatically started by      
#           libvirtd                                                               
#ON_BOOT=start                                                                     

# Number of seconds to wait between each guest start. Set to 0 to allow            
# parallel startup.
#START_DELAY=0

# action taken on host shutdown
# - suspend   all running guests are suspended using virsh managedsave
# - shutdown  all running guests are asked to shutdown. Please be careful with
#             this settings since there is no way to distinguish between a
#             guest which is stuck or ignores shutdown requests and a guest
#             which just needs a long time to shutdown. When setting
#             ON_SHUTDOWN=shutdown, you must also set SHUTDOWN_TIMEOUT to a
#             value suitable for your guests.
ON_SHUTDOWN=shutdown

# If set to non-zero, shutdown will suspend guests concurrently. Number of
# guests on shutdown at any time will not exceed number set in this variable.
#PARALLEL_SHUTDOWN=0

# Number of seconds we're willing to wait for a guest to shut down. If parallel
# shutdown is enabled, this timeout applies as a timeout for shutting down all
# guests on a single URI defined in the variable URIS. If this is 0, then there
# is no time out (use with caution, as guests might not respond to a shutdown
# request). The default value is 300 seconds (5 minutes).
#SHUTDOWN_TIMEOUT=300

# If non-zero, try to bypass the file system cache when saving and
# restoring guests, even though this may give slower operation for
# some file systems.
#BYPASS_CACHE=0

注意我这里设置的是ON_SHUTDOWN=shutdown,因为对于有PCIe设备直通的虚拟机suspend可能无法使用。

然后启用libvirt-guests.service服务:

sudo systemctl enable --now libvirt-guests

然后在虚拟机关机时(或libvirt-guests服务关闭时)正在运行的虚拟机应当能正常关机,并且这些虚拟机会在宿主机下次启动时自动启动(恢复状态)。

这里不得不吐槽一下红帽经常搞一些breaking change,之前升级就被Network UPS Tools (NUT)的服务配置坑过一次