Created
July 17, 2025 17:34
-
-
Save sdsalyer/d4283c3601281dfb584d7524ba8ef329 to your computer and use it in GitHub Desktop.
A Bash script to start, stop, and query status of a QEMU/virt-manager VM using virsh
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 | |
# Generated by Claude Haiku 3.5 at Duck.ai | |
# Modified by b3rts to make it work | |
# Script: apuevm.sh | |
# Purpose: Manage a QEMU/virt-manager virtual machine in "headless" mode | |
# Note: By default, virsh uses `--connect qemu:///session` forp VMs in the current user's session | |
# I had to specify `--connect qemu:///system para` for system-level VM | |
# Virtual machine name | |
VM_NAME="apue-netbsd" | |
# Function to check VM status | |
check_status() { | |
# use "list" without a VM name to list / status all VMs | |
virsh --connect qemu:///system domstate "$VM_NAME" | |
} | |
# Function to start VM in headless mode | |
start_vm() { | |
# the `--autodestroy` option seemed to prevent starting the VM | |
virsh --connect qemu:///system start "$VM_NAME" | |
} | |
# Function to stop VM | |
stop_vm() { | |
virsh --connect qemu:///system shutdown "$VM_NAME" | |
} | |
# Main script logic | |
case "$1" in | |
"status") | |
check_status | |
;; | |
"start") | |
start_vm | |
;; | |
"stop") | |
stop_vm | |
;; | |
*) | |
echo "Usage: $0 [status|start|stop]" | |
echo " status: List state of VM." | |
echo " start: Start VM in headless mode." | |
echo " stop: Stop VM." | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment