Virsh notes


List all VMs virsh list --all
Start a vm virsh start [vm name]
Stop a vm virsh destroy [vm name]
Remove a vm (does not remove .vmdk) virsh undefine [vm name]
Create a vm virsh define [vm name].xml

Create and resize kvm image

Create KVM image qemu-img create -f raw vm-example.img 20G
Resize KVM image qemu-img resize vm-example.img +30G

Build vm

Build a vm named vm-example, mount debian 12 iso and connect to bridge br0.
osinfo can be found with osinfo-query os
virt-install --name vm-example --osinfo debian11 --ram 2048 --vcpus 2 --disk path=/root/vm-example/vm-example.img,bus=virtio,size=20 --graphics none --cdrom /root/iso/debian-12.5.0-amd64-DVD-1.iso --network bridge:br0

Setup new VM

Attach disk, enable VNC, and set boot order after setup (virsh edit [vm name]). Add these into the xml file. Disable vnc and remove iso after completion.
#### Attach disk ::
    <disk type='file' device='cdrom'>
    <driver name='qemu' type='raw'/>
    <source file='/root/iso/debian-12.5.0-amd64-DVD-1.iso'/>
    <target dev='sda' bus='sata'/>
    <address type='drive' controller='0' bus='0' target='0' unit='0'/>

#### Enable VNC (virsh edit [vm name])

    <graphics type='vnc' port='-1' autoport='yes' listen='' keymap='en-us'/>

#### Add cdrom into boot order

    <boot dev='cdrom'/>

#### VNC into machine and finish setup virsh domdisplay vm-name

#### Setup virsh console
systemctl enable serial-getty@ttyS0.service
systemctl start serial-getty@ttyS0.service

Creating a VM from an existing template

The steps for this are the same as above, however make these changes to the template
#Change vm name
#Change UUID - randomize
#Change RAM
#Change CPU
#Change hdd location
#Increment mac address by one or change to reflect vlan

Fix Debian bridge forwarding issues

While setting up a virtual machinge on Debian 12 using KVM/Libvrt and Virsh I found that all of my VMs could connect via IPv6, but not via IPv4. I found the solution was to uncomment these two lines in /etc/sysctl.conf