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
Clone VM and assign Random Mac address to new image
Copy over the xml for the new clone which you can use to define your clone if moving the image to another host
Shrink/Sparcify VM image
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
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.
Move VM to another system Offline
Step 1 from original host shutdown your VM, copy xml and image to backup location.
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.
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.