Created
April 14, 2020 12:00
-
-
Save Roliga/928cd44440f4df74e796e4e1315034bf to your computer and use it in GitHub Desktop.
Hibernate libvirt VM on host shutdown/sleep.
This file contains hidden or 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 | |
# | |
# Usage: hibernate-vm NAME | |
# | |
# Hibernates the VM specified in NAME and waits for it to finish shutting down | |
# | |
if virsh dompmsuspend "$1" disk; then | |
echo "Waiting for domain to finish shutting down.." >&2 | |
while ! [ "$(virsh domstate "$1")" == 'shut off' ]; do | |
sleep 1 | |
done | |
echo "Domain finished shutting down" >&2 | |
fi |
This file contains hidden or 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=Hibernate VM %I when host shuts down | |
Requires=virt-guest-shutdown.target | |
After=libvirt-guests.service | |
After=libvirtd.service | |
After=virt-guest-shutdown.target | |
[Service] | |
Type=oneshot | |
RemainAfterExit=yes | |
ExecStop=/usr/local/bin/hibernate-vm %i | |
[Install] | |
WantedBy=multi-user.target |
This file contains hidden or 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=Hibernate VM %I when host goes to sleep | |
Before=sleep.target | |
[Service] | |
Type=oneshot | |
ExecStart=/usr/local/bin/hibernate-vm %i | |
[Install] | |
WantedBy=sleep.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
.service
files go in/etc/systemd/system/
, andhibernate-vm
goes in/usr/local/bin/
. Make sure tochmod 755
it.The sleep service can be enabled with
systemctl enable hibernate-vm-sleep@SOME_VM.service
.The shutdown service should always be active while your system is on, so enable it with
systemctl enable --now hibernate-vm-shutdown@SOME_VM.service
for it to work right away.