D
D
Dmitry Aitkulov2016-08-10 14:57:37
System administration
Dmitry Aitkulov, 2016-08-10 14:57:37

What is the correct way to shut down KVM guest OSes?

Good afternoon! There is a server (centos 7) in the office + ups must 3000VA with a console COM port. I connected the whole thing to each other and installed nut. The server has kvm, which runs windows and linux machines. Actually the task: when the light is turned off, the oops gives a signal that it is running on batteries, then when the discharge reaches a minimum, it sends the nut-monitor command, which in turn executes the script for turning off the machines and turning off the host machine. Actually, the whole problem is that it is the windows of the machine that receive the shutdown signal and freeze and do not want to turn off. What was done:
set group policies in windows to shut down the server without logging in and not displaying a window on how to shut down the server. I set the value ShutdownWarningDialogTimeout=00000001 in the registry
did not help
set settings in /etc/sysconfig/libvirt-guests file

ON_SHUTDOWN=shutdown
PARALLEL_SHUTDOWN=1
SHUTDOWN_TIMEOUT=90

config /etc/ups/ups.conf did not help
[Must]
        driver = blazer_ser
        port = /dev/ttyS0
        desc = "server"
        offdelay = 300
        ondelay = 0

config /etc/ups/upsmon.conf
RUN_AS_USER nut
MONITOR [email protected] 1 monuser password master
MINSUPPLIES 1
SHUTDOWNCMD "sudo /etc/ups/shutdown.sh --shutdown"
POLLFREQ 5
POLLFREQALERT 5
HOSTSYNC 15
DEADTIME 15
POWERDOWNFLAG /etc/ups/killpower
RBWARNTIME 43200
NOCOMMWARNTIME 300
FINALDELAY 0

config /etc/ups/upsd.users
[monuser]
   password = password
   actions = SET
   instcmds = ALL
   upsmon master

well, the shutdown script itself
#!/bin/bash
shutdown_quests(){
LIST_VM=`virsh list | grep running | awk '{print $2}'`
TIMEOUT=90
DATE=`date -R`
LOGFILE="/etc/ups/kvm-quest-shutdown.log"
if ; then
        touch $LOGFILE
        #echo "$LOGFILE created" 1>&2
elif ; then
        echo "$DATE : Start script" >> $LOGFILE
fi


for activevm in $LIST_VM
do
        PIDNO=`ps ax | grep $activevm | grep kvm | cut -c 1-6 | head -n1`
        echo "$DATE : Shutdown : $activevm : $PIDNO" >> $LOGFILE
        virsh shutdown $activevm > /dev/null
        COUNT=0
        while [ "$COUNT" -lt "$TIMEOUT" ]
        do
                ps --pid $PIDNO > /dev/null
                if [ "$?" -eq "1" ]
                        then
                        COUNT=110
                else
                        sleep 5
                        COUNT=$(($COUNT+5))
                fi
        done
        if [ $COUNT -lt 110 ]
                then
                echo "$DATE : $activevm not successful force shutdown" >> $LOGFILE
                virsh destroy $activevm > /dev/null
        fi
done
}
note(){
        echo -e "--------------------------------------------------------\n"
        echo -e "\nYou need usage script with arguments: --reboot ot --shutdown:\n\n $0 --shutdown\n"
        exit 1
}
reboot_node(){
        reboot
}

shutdown_node(){
        systemctl poweroff -i
}


if [ $# -ne 1 ]; then
    note
fi

for i in "[email protected]" ; do

    if  ; then
        shutdown_quests
        reboot_node
        break
    fi

    if  ; then
        shutdown_quests
        shutdown_node
        break
    fi

done

changed sudo file
Defaults    !requiretty
nut ALL=(ALL) NOPASSWD: /etc/ups/shutdown.sh

Tell me what I'm doing it like that?! Why is it that with the usual systemctl poweroff -i, the machines are not turned off, but kvm is destroyed? Thanks in advance everyone
managed to find the service that monitors the guest machines, libvirt-guests.service, started it. Now if you stop it, then the machines turn off as expected. And if you send a shutdown signal to the host machine, then the shutdown does not wait for the shutdown of the guest machines to finish. I'll try to make a script that will stop the libvirt-guests.service service, check if there are any running machines left and turn off the host machine

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Aitkulov, 2016-08-15
@Scarfase1989

Empirically, it was found that when window server 2008 shut down, open user programs waited for shutdown, but since the command virsh shutdown vmsends a shutdown signal on behalf of the "system" user, which does not have rights to shut down the server with a forced shutdown of terminal users, it is necessary :
1. Give the necessary rights to the "system" account to shut down (done in group policies)
or 2. install a kvm
agent that will shut down the server . Which option is convenient for you and use it, I settled on installing the agent

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question