I have been playing around with KVM lately, just to see how it stacks up against VirtualBox and am loving it. I have found the following commands interesting while working with VMs.
List all VMs
Start stop a VM
virt-clone --original vmName --name newVmName --file /some/path/newVmName.qcow2
Clone VM and assign Random Mac address to new image
virt-clone --original vmName --name newVmName --file /some/path/newVmName.qcow2 --mac=RANDOM
Copy over the xml for the new clone which you can use to define your clone if moving the image to another host
cp /etc/libvirt/qemu/newVmName.xml /some/path/newVmName.xml
Shrink/Sparcify VM image
virt-sparsify --format qcow2 --compress vmName.bkp.qcow2 vmName.qcow2 --tmp /some/path
This command assumes you renamed the original VM image file to “vmName.bkp.qcow2” incase the new image has problems you can revert to original image. You can skip the –tmp argument if you have more than double the size of the VM image available in your /tmp directory. You will need to make sure you have package “libguestfs-tools” installed which supplies the sparsify command.
This removes the XMl configuration for qemu you might have to delete the VM image yourself.
List storage pools
Refresh pool data
virsh pool-refresh poolName
You will need to run this command if you delete a VM and you need to reuse the VM name you had used, so make sure you refresh the pool where your deleted image was located.
Disable autostarting of a VM
virsh autostart vmName --disable
Move VM to another system Offline
Step 1 from original host shutdown your VM, copy xml and image to backup location.
virsh dumpxml vmName > /bkpLocation/vmName.xml
cp /locationofVMs/vmname.qcow2 /bkpLocation/vmName.qcow2
Bye default if you didn’t change image storage location it will be “/var/lib/libvirt/images”. I prefer to store them in a separate partition from the OS.
Step 2 on new host get VM xml and image and enjoy.
virsh define /bkpLocation/vmName.xml
Domain vmName defined from /bkpLocation/vmName.xml
virsh start vmName
This all works if you are moving from host1 and host2 with similar cpus, for my purpose host2 had a different cpu type so I had to first create a new vm in host2 and compare the xml with my backed up copy then changed a few lines specific to CPU on host2.