Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

virsh_start_stop-0.0.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

virsh_start_stop-0.0.1-py3.6.egg (5.4 kB view details)

Uploaded Egg

virsh_start_stop-0.0.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file virsh_start_stop-0.0.1.tar.gz.

File metadata

  • Download URL: virsh_start_stop-0.0.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5

File hashes

Hashes for virsh_start_stop-0.0.1.tar.gz
Algorithm Hash digest
SHA256 8c7fc04cf60d73f293533aeebed09654869c36b5f878718595485a251670ca4d
MD5 4a3f545a7cdf2ff6d2440624a4490d26
BLAKE2b-256 51e1356a93442a094fe92b058b3da866be10921a46538060ebf631456ac78f51

See more details on using hashes here.

File details

Details for the file virsh_start_stop-0.0.1-py3.6.egg.

File metadata

  • Download URL: virsh_start_stop-0.0.1-py3.6.egg
  • Upload date:
  • Size: 5.4 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5

File hashes

Hashes for virsh_start_stop-0.0.1-py3.6.egg
Algorithm Hash digest
SHA256 ece2c1df1dcbf557abe6e2252fd0984a148dbf591d5361678cc5b0d2374fe347
MD5 e0abf29255b3b67b28a746fe9976c70e
BLAKE2b-256 d021f8877a702d04c3a6c56c1c951a26805bd296d355a4e80e61f3dc65d93510

See more details on using hashes here.

File details

Details for the file virsh_start_stop-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: virsh_start_stop-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5

File hashes

Hashes for virsh_start_stop-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 951385cc5ebddb71377a9a58a7ff103b4c437e88dd250e68697447a076ab37cd
MD5 222b82fa222372933238c0b13c762fed
BLAKE2b-256 af87f40e006a35186df9262f90316cfa568490f016b5620f063f15a09aea7590

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page