Files
homelab/active/software_virsh/virsh.md
2025-10-23 10:40:10 -04:00

2.1 KiB

Virsh

Virtual Machine Management

Before you Begin

  1. Add yourself to the qemu and libvirt groups: usermod -aG libvirt,qemu ducoterra
  2. Change the images ownership to qemu: chown -R qemu:qemu /var/lib/libvirt/images
  3. Change the iso ownership to qemu: chown -R qemu:qemu /var/lib/libvirt/iso
  4. Allow group write access to images: chmod 770 /var/lib/libvirt/images
  5. Allow group write access to iso: chmod 770 /var/lib/libvirt/iso
  6. Tell virsh to connect to your root system rather than your user: export LIBVIRT_DEFAULT_URI='qemu:///system'

VM Details

# Show node info
virsh nodeinfo

# List OS variants
osinfo-query os

# List all current machines
virsh list --all

Creating VMs

If you have an osbuild image you can run

sudo systemctl start osbuild-composer.socket
composer-cli compose list
composer-cli compose image --filename /var/lib/libvirt/images/fedora-42-test.qcow2 image-uuid

now to have a qcow2 available during install.

# `--location /path/to/image.iso` supplies a disk installer. (Remove `--import`)
# `--import` skips the installation process.
# `--graphics spice --video qxl --channel spicevmc` installs graphics
# `--console pty,target.type=virtio` adds a console connection
# For any command, use `virt-install --arg=?` to see all available options
virt-install \
--name fedora42-test \
--description "Test VM with Fedora42" \
--cpu host-model --vcpus sockets=1,cores=8,threads=2 \
--ram=8192 \
--os-variant=fedora41 \
--import --disk path=/var/lib/libvirt/images/fedora-42-test.qcow2,bus=virtio \
--network bridge:virbr0 \
--graphics none \
--console pty,target.type=virtio

# Connect to console VM
virsh console fedora42-test

# Connect to graphical VM
virt-viewer --wait fedora42-test

# Get leased IP Addresses for the default network
virsh net-dhcp-leases default

# Reboot a VM
virsh reboot <domain>

# Shutdown a VM
virsh shutdown <domain>

# Force shutdown a VM
virsh destroy <domain>

# Remove a VM
virsh undefine <domain>

# Remove a VM including storage
virsh undefine <domain> --remove-all-storage