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 |
Author
Suggestion: add "managedsave" support, because I don't want to setup hibernation in the guest VM.
#!/bin/bash
#
# Usage: hibernate-vm NAME
#
# Hibernates the VM specified in NAME and waits for it to finish shutting down
#
domname="$1"
if virsh managedsave "$domname"; then
echo "Waiting for domain to finish saving states to disk.." >&2
while ! [ "$(virsh domstate "$domname")" == 'saved' ]; do
sleep 1
done
echo "Domain finished saving states to disk" >&2
fi
# If failed (e.g. VFIO migration not supported caused by GPU-passthroughing), fallback to domain powermanagement - hibernate (requires swap partition/file setup in guest)
elif virsh dompmsuspend "$domname" disk; then
echo "Waiting for domain to finish shutting down.." >&2
while ! [ "$(virsh domstate "$domname")" == 'shut off' ]; do
sleep 1
done
echo "Domain finished shutting down" >&2
fi
# DO NOT add "virsh suspend" as it isn't persist after shutting downvirsh dompmsuspend <domain name> disk requires corresponding domain to enable S4 state.
virsh edit: ensure <suspend-to-disk enabled='yes'/> in <pm>.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
.servicefiles go in/etc/systemd/system/, andhibernate-vmgoes in/usr/local/bin/. Make sure tochmod 755it.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.servicefor it to work right away.