Utilize the python libvirt API to start and stop qemu-kvm machines in a blocking fashion
Project description
virsh-start-stop
I wanted to start and stop individual libvirt virtual machines with a systemd unit.
Calling virsh start %i
or virsh stop %i
from the unit doesn't cut it.
This is because it doesn't block while shutting down so you don't know if the machine is really down unless you wrap
it all in a bash script and employ a dirty hack along the lines of virsh list | grep myVM
.
Starting a machine is also challenging because virsh start
uses exit status 1 for multiple error conditions,
including "machine is already running".
The purpose of this script is to start / stop a given virtual machine through the libvirt python bindings in a way that is easily consumable from systemd. This means I try to handle the "already running" case gracefully. Shutting down happens in a blocking fashion, with the option to yank the virtual power cord from the machine in case it ignores our polite requests.
I also took the opportunity to issue shutdown requests every second until the machine is down. This should be unnecessary when the virtual machine has the libvirt guest agent running, but it can sometimes help to convince windows to really shut down instead of asking "really shutdown?" on the virtual console with nobody around to click ok.
Example systemd unit file:
[Unit]
Description=virsh start / stop %i
Requires=libvirtd.service
After=libvirtd.service
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/opt/vollmerhaus.net/venv/bin/virsh-start-stop --machine %i --state started
ExecStop=/opt/vollmerhaus.net/venv/bin/virsh-start-stop --machine %i --state stopped --timeout 80
[Install]
WantedBy=default.target
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for virsh_start_stop-0.0.1-py3.6.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | ece2c1df1dcbf557abe6e2252fd0984a148dbf591d5361678cc5b0d2374fe347 |
|
MD5 | e0abf29255b3b67b28a746fe9976c70e |
|
BLAKE2b-256 | d021f8877a702d04c3a6c56c1c951a26805bd296d355a4e80e61f3dc65d93510 |
Hashes for virsh_start_stop-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 951385cc5ebddb71377a9a58a7ff103b4c437e88dd250e68697447a076ab37cd |
|
MD5 | 222b82fa222372933238c0b13c762fed |
|
BLAKE2b-256 | af87f40e006a35186df9262f90316cfa568490f016b5620f063f15a09aea7590 |