virsh
KVM virtual machine management via libvirt. Lifecycle, autostart, console access, disk/network inspection, snapshots, and bulk operations.
VM Lifecycle
List all VMs — running and stopped
sudo virsh list --all
Start a single VM
sudo virsh start vyos-01
Start VMs in dependency order — with loop and status
for vm in vyos-01 bind-01 vault-01 home-dc01 ipa-01 ipsk-mgr-01 9800-WLC-01 k3s-master-01; do
sudo virsh start $vm && echo "$vm started" || echo "$vm FAILED"
sleep 5
done
Graceful shutdown — sends ACPI power button signal
sudo virsh shutdown vyos-02
Hard power off — immediate, no graceful shutdown (like pulling the power cord)
sudo virsh destroy 9800-WLC-02
destroy does NOT delete the VM or its disk. It’s a hard power-off. Use it when shutdown hangs or you need the VM stopped immediately.
|
Reboot
sudo virsh reboot vyos-01
Autostart
List VMs with autostart status
sudo virsh list --all --autostart
Enable autostart — VM starts automatically on hypervisor boot
sudo virsh autostart vyos-01
Enable autostart for all critical VMs — loop pattern
for vm in vyos-01 bind-01 vault-01 home-dc01 ipa-01 ipsk-mgr-01 9800-WLC-01 k3s-master-01; do
sudo virsh autostart $vm && echo "$vm autostart enabled"
done
Disable autostart
sudo virsh autostart --disable k3s-master-01
Console Access
Attach to VM serial console — like plugging a monitor into the VM
sudo virsh console vyos-02
Exit console — Ctrl+] (right bracket)
Ctrl + ]
| Some VMs (IOS-XE, ISE) don’t have serial console configured. You’ll connect but see no output. Use SSH instead for those. |
VM Inspection
List VM disk images — shows where qcow2 files are stored
sudo virsh domblklist 9800-WLC-02
# Target Source
# hda /mnt/nas/vms/9800-WLC-02.qcow2
List VM network interfaces — shows bridge, MAC, vnet mapping
sudo virsh domiflist ise-02
# Interface Type Source Model MAC
# vnet2 bridge br-mgmt virtio 52:54:00:27:3d:70
Dump full VM XML config
sudo virsh dumpxml vyos-02
Extract just the disk section from XML
sudo virsh dumpxml 9800-WLC-02 | awk '/<disk/,/<\/disk/'
VM Modification
Change VM disk path — move image from NAS to local storage
sudo virt-xml 9800-WLC-02 --edit --disk path=/var/lib/libvirt/images/9800-WLC-02.qcow2
Attach a new network interface — add NIC to bridge
sudo virsh attach-interface vyos-02 bridge br-ispb --model virtio --persistent
Detach a network interface by MAC
sudo virsh detach-interface vyos-02 bridge --mac 52:54:00:31:eb:e3 --persistent
--persistent saves the change to the VM definition. Without it, the change is lost on VM restart.
|
Snapshots
Create a snapshot — safety net before changes
sudo virsh snapshot-create-as vyos-02 pre-migration "Before NIC reorder"
List snapshots
sudo virsh snapshot-list vyos-02
Revert to snapshot — instant rollback
sudo virsh snapshot-revert vyos-02 pre-migration
Delete snapshot — do this within 24-48h, snapshots degrade disk I/O
sudo virsh snapshot-delete vyos-02 pre-migration
Bulk Operations
Shutdown all VMs on a hypervisor
for vm in $(sudo virsh list --name); do
sudo virsh shutdown $vm
done
Restart all VMs — triggers libvirt hooks on start
for vm in vyos-02 ise-02 bind-02 vault-02 vault-03 9800-WLC-02; do
sudo virsh shutdown $vm
done
sleep 15
for vm in vyos-02 ise-02 bind-02 vault-02 vault-03 9800-WLC-02; do
sudo virsh start $vm
done
See Also
-
qemu-img — disk image health check and repair
-
Bridge VLAN — VM network connectivity via bridge VLANs