Created
August 20, 2017 17:04
-
-
Save ntrrgc/406644508a4bac485517e1bd81d6274c to your computer and use it in GitHub Desktop.
Gracefully shutdown libvirtd VMs on shutdown. Start and enable the provided systemd service for it to work.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Wait for VMs to shutdown gracefully | |
After=libvirtd.service | |
[Service] | |
Type=oneshot | |
RemainAfterExit=true | |
ExecStop=/home/ntrrgc/dotfiles/bin/vm-shutdown | |
[Install] | |
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# File: /root/bin/virt-shutdown.sh | |
# Description: Shutdown active virtual machines | |
# Author: Ed Heron <Ed@Heron-ent.com> | |
# Date: 2009.11.13 Copy placed on centos-virt@centos.org | |
# Note: 2009.11.23 Bugfixes and hoovering by torkil | |
DEBUG=1 # Comment this out if you really want to use this script in production | |
#FAKE=1 # Comment this out if you really want to use this script in production | |
# Get list of active virtual machines | |
vmList="`virsh list | ( | |
while read vmID vmName vmStatus | |
do | |
if [ -n "$vmName" -a "$vmName" != "Name" -a "$vmName" != "Domain-0" ] | |
then | |
[ -z "$vmList" ] && vmList="$vmName" || vmList="$vmList $vmName" | |
fi | |
done | |
echo $vmList )`" | |
# check there are some active VM's | |
if [ -n "$vmList" ] | |
then | |
# Shutdown VM's with verification | |
for vmName in $vmList | |
do | |
# Send initial request | |
[ -n "$DEBUG" ] && echo -n "Attempting to shutdown $vmName " | |
[ -z "$FAKE" ] && virsh shutdown $vmName | |
# wait a limited time for the VM to be not running | |
count=30 | |
while ( virsh list | grep $vmName >/dev/null ) && [ $count -gt 0 ] | |
do | |
sleep 1 | |
let count=count-1 | |
[ -n "$DEBUG" ] && echo -n "." | |
done | |
# report current status | |
( virsh list | grep $vmName >/dev/null ) && echo " failed!" || echo " down." | |
# if still running, destroy it | |
if ( virsh list | grep $vmName >/dev/null ) | |
then | |
[ -n "$DEBUG" ] && echo -n "Attempting to destroy $vmName " | |
[ -z "$FAKE" ] && virsh destroy $vmName | |
# wait a limited time for the VM to be not running | |
count=60 | |
while ( virsh list | grep $vmName >/dev/null ) && [ $count -gt 0 ] | |
do | |
sleep 1 | |
let count=count-1 | |
[ -n "$DEBUG" ] && echo -n "." | |
done | |
# report current status | |
( virsh list | grep $vmName >/dev/null ) && echo " failed!" || echo " down." | |
fi | |
done | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment