首页 > 解决方案 > Archlinux 上的优雅关机

问题描述

直接从这里提出这个问题。

我正在运行archlinux,并且我有一个经常与系统一起运行的VM。实际上大部分时间。

我的目标是产生以下行为:

给我一个关于该做什么的好主意,因为我什至不知道从哪里开始。我想有一个可以查看的内核调用。

让我知道。


我当前的代码

目前我正在使用这些脚本优雅地关闭我的 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 中关闭它。我不希望计算机开始停止所有服务并保持挂起状态,直到它强制关闭。

标签: linuxbashsystemdkvm

解决方案


推荐阅读