linux - Archlinux 上的优雅关机
问题描述
直接从这里提出这个问题。
我正在运行archlinux,并且我有一个经常与系统一起运行的VM。实际上大部分时间。
我的目标是产生以下行为:
- 向系统发送关机/关机/重启/停止信号
- 除了尝试正常关闭虚拟机之外没有任何操作
- 如果虚拟机在 X 秒后正常关闭,则继续关闭主机系统。
- 如果不是,请执行不同的命令
给我一个关于该做什么的好主意,因为我什至不知道从哪里开始。我想有一个可以查看的内核调用。
让我知道。
我当前的代码
目前我正在使用这些脚本优雅地关闭我的 kvm 虚拟机,并且它可以正常工作!但只要我的用户使用他的 shell启动关机或重启。任何其他情况都行不通。
这些别名:
alias sudocheck="/bin/bash /home/damiano/.script/sudocheck"
alias sudo="sudocheck "
正在触发此功能:
#!/bin/bash
# This script checks for what is being passed to sudo.
# If the command passed is poweroff or reboot, it
# launches a custom script instead, that also looks
# fur currently running virtual machines and shuts them.
sudocheck() {
if [ $1 == "poweroff" ] || [ $1 == "reboot" ]; then
eval "sudo /home/damiano/.script/graceful $@"
else
eval "sudo $@"
fi
}
sudocheck $@
如果需要,它将启动此脚本:
#!/bin/bash
i=0
e=0
## if virsh finds VMs running
virsh -c qemu:///system list | awk '{ print $3}' | \
if grep running > /dev/null ; then
virsh -c qemu:///system list --all | grep running | awk '{print "-c qemu:///system shutdown "$2}' | \
## shuts them dow gracefully
xargs -L1 virsh
## wait 30 seconds for them to go down
until (( i >= 30 || e == 1 )) ; do
## check every second for their status
virsh -c qemu:///system list --all | awk '{ print $3}' | \
if grep -E '(running|shutdown)' > /dev/null ; then
## keep waiting if still running
if (( i <= 30 )) ; then
sleep 1 && let i++ && echo $i
else
e=1 && notify-send 'Shutdown has been canceled' 'Please check the status of your virtual machines: seems like even though a stop signal has been sent, some are still running.' --urgency=critical
fi
else
## if no machine is running anymore, original power command can be executed
e=1 && eval $@
fi
done
fi
系统单元
我还制作了以下草稿,以管理我的 VM 的执行:
bootvm@.service
[Unit]
Description=This service manages the execution of the %i virtual machine
Documentation=https://libvirt.org/manpages/virsh.html
[Service]
ExecStartPre=virsh -c qemu:///system
ExecStart=virsh start %i
ExecStop=virsh -c qemu:///system
ExecStop=virsh shutdown %i
TimeoutStopSec=30
KillMode=none
[Install]
WantedBy=multi-user.target
但是我怎么能告诉系统不要关闭桌面环境,保持原样直到虚拟机成功关闭?因为如果系统无法关闭虚拟机,我想在我的 DE 中关闭它。我不希望计算机开始停止所有服务并保持挂起状态,直到它强制关闭。
解决方案
推荐阅读
- c++ - 如何使用 C++ API 在 LLVM 中执行数组展开?
- c# - 统一文本的问题,NullReferenceException
- reactjs - 错误:无效的挂钩调用。Hooks 只能在函数组件的主体内部调用,同时将 kepler.gl 添加到 React 项目
- python - 将 TImestamp 系列转换为 Unix 时间戳
- git - Git rebase Visual Studio vs 源代码树
- python - 演示一个没有笔记本的 CLI 程序
- iphone - phpmailer 未处理电子邮件中的 Iphone 主题行
- javascript - Google Chrome 插件:“无法读取 null 的属性‘addEventListener’”
- python - 为什么我的代码无法识别列表的所有部分?
- django - 如何删除 django 中所有不匹配的查询结果?