Skip to main content

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

Metadata

Release files for virsh-start-stop 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for virsh-start-stop 0.0.1
File Size Uploaded Provenance Actions
virsh_start_stop-0.0.1.tar.gz 5.6 kB Without provenance Details for virsh_start_stop-0.0.1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for virsh-start-stop 0.0.1
File Interpreter ABI Platform Actions
virsh_start_stop-0.0.1-py3.6.egg Legacy Egg format - - Details for virsh_start_stop-0.0.1-py3.6.egg
virsh_start_stop-0.0.1-py3-none-any.whl Python 3 none any Details for virsh_start_stop-0.0.1-py3-none-any.whl

Total release size: 15.9 kB

Release files / virsh_start_stop-0.0.1.tar.gz

Download URL virsh_start_stop-0.0.1.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
8c7fc04cf60d73f293533aeebed09654869c36b5f878718595485a251670ca4d
BLAKE2b-256 checksum
How to use checksums
51e1356a93442a094fe92b058b3da866be10921a46538060ebf631456ac78f51
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / virsh_start_stop-0.0.1-py3.6.egg

Download URL virsh_start_stop-0.0.1-py3.6.egg
Size 5.4 kB
Tags Egg
SHA-256 checksum
How to use checksums
ece2c1df1dcbf557abe6e2252fd0984a148dbf591d5361678cc5b0d2374fe347
BLAKE2b-256 checksum
How to use checksums
d021f8877a702d04c3a6c56c1c951a26805bd296d355a4e80e61f3dc65d93510
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / virsh_start_stop-0.0.1-py3-none-any.whl

Download URL virsh_start_stop-0.0.1-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
951385cc5ebddb71377a9a58a7ff103b4c437e88dd250e68697447a076ab37cd
BLAKE2b-256 checksum
How to use checksums
af87f40e006a35186df9262f90316cfa568490f016b5620f063f15a09aea7590
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for virsh-start-stop 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

3 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page