Systemd 实践 - KVM 虚拟机自动重启

Systemd 实践 - KVM 虚拟机自动重启

最近遇到一个需求,需要使用 systemd 实现 KVM 虚拟机的自动重启功能。

1. 单次任务脚本:自动重启已关闭的虚拟机

首先,编写一个 Shell 脚本:遍历所有关闭(shut off)状态的虚拟机,并尝试启动。

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
virsh list --all --state-shutoff --name | while read vm; do
echo "尝试启动虚拟机:$vm"
virsh start "$vm"
if [ $? -eq 0 ]; then
echo "成功启动虚拟机:$vm"
logger -t VM_AUTOSTART "启动记录:$vm"
else
echo "启动失败:$vm" >&2
fi
done

注意:要确保脚本有执行权限,否则无法被 systemd 调用。

1
chmod +x /root/vm_autostart.sh

2. 使用 systemd 定时定期执行

systemd 定时任务(timer)通常需要对应的 service 和 timer 两个文件:

2.1 服务配置文件

路径:/etc/systemd/system/vm-autostart.service

1
2
3
4
5
6
[Unit]
Description=Check and auto-start KVM VMs in shut off state

[Service]
Type=oneshot
ExecStart=/root/vm_autostart.sh

2.2 定时器配置文件

路径:/etc/systemd/system/vm-autostart.timer

1
2
3
4
5
6
7
8
9
10
[Unit]
Description=Auto-check KVM VMs every 5 minutes

[Timer]
OnBootSec=5min
OnUnitActiveSec=5min
AccuracySec=1s

[Install]
WantedBy=timers.target
  • 解释:
    • OnBootSec=5min # 启动后延迟 5 分钟运行
    • OnUnitActiveSec=5min # 之后每 5 分钟运行一次
    • Type=oneshot # 表示脚本只执行一次并退出,非常适合自动任务

激活定时器:

1
2
systemctl daemon-reload
systemctl enable --now vm-autostart.timer

说明:和常规 long-running 的服务不同,看状态时,.service 为 inactive,.timer 应为 active(waiting),计时等待下次触发。


3. 查看日志

可通过如下命令查看服务执行和 logger 输出的日志:

  • 查看服务日志:

    1
    journalctl -u vm-autostart.service
  • 查看 logger 输出(使用 Tag 检索):

    1
    journalctl -t VM_AUTOSTART

小结:
配合 systemd 定时器,实现 KVM 虚拟机宕机自动检测与重启,脚本简洁易扩展,日志也便于追溯。如果有更精细化需求,也可以改写成 Python 或用 ansible playbook 实现。


Systemd 实践 - KVM 虚拟机自动重启

https://zion4h.github.io/2025/04/19/2025-4-19-systemd-practic/

作者

zion h4

发布于

2025-04-19

更新于

2025-05-06

许可协议

评论