Basic KVM operations
SEE ALSO!
List all the VMs, and see which ones are running
# sudo virsh list --all
Start a VM
You can list the VMs first, then:
# sudo virsh start <name of your vm>
Shutdown a VM properly
Note: this require the VM to have support for ACPI - at the moment it doesn't seem to work with windows VMs (seems OK to me- Jo).
# sudo virsh shutdown <name of the vm>
Shutdown a VM
Same thing as taking the plug off. Use with care.
# sudo virsh destroy <name of the vm>
Delete a VM
THIS REMOVES A VM. NO RESTORE POSSIBLE. PLEASE USE WITH SPECIAL CARE.
# sudo virsh shutdown <name of the vm> # sudo virsh undefine <name of the vm> # sudo rm -rf /var/lib/kvm/<name of the vm>
Back up a VM
After shutting down:
# cd /var/lib/kvm # cp -R <name of the vm> /to/safe/place
Copy or move a VM to another host
- Using scp, we find that a qcow2 disk will expand to its full allocated size when moved or copied to another host (eg a 20gb VM may only take up 5gb of actual disk space, but when copied it takes up the full 20gb)
- Eduardo found a solution using rsync. Example:
rsync /var/lib/kvm/vanilla_templates/w2k3-vanilla3/w2k3-vanilla3.qcow2 ecoria@leibniz.goo.thehumanjourney.net:/home/ecoria/test/
Second method
In case you have some permission problem you could use this method. Copy the VM image to remote server in /tmp dir:
rsync /var/lib/kvm/<vm-name>/<vm-image> <remote-server-ip>:/tmp/
On the remote server create a dir for the VM and copy it from /tmp to that dir:
mkdir /var/lib/kvm/<vm-name> mv /tmp/<vm-image> /var/lib/kvm/<vm-name>/
Now you also need the xml file for that VM. Since it is just text you can copy it, create a file on remote server and paste it there. The xml file is found in this dir:
/etc/libvirt/qemu
After you copied the VM image and xml file you can define and start it:
virsh define /etc/libvirt/qemu/<vm-name>.xml virsh start <vm-name>
Rename a VM
Shutdown the VM, dump the xml file and edit it.
virsh shutdown oldname virsh dumpxml oldname > /etc/libvirt/qemu/newname.xml vi /etc/libvirt/qemu/newname.xml
In newname.xml change "<name>oldname</name>" to "<name>newname</name>" and "
" to "
". Undefine the VM and define the new one.
virsh undefine oldname mv /var/lib/kvm/oldname /var/lib/kvm/newname virsh define /etc/libvirt/qemu/newname.xml virsh start newname
ssh to VM and follow the steps from here: changing the hostname
VM-disk deleted - resolution
Solution 1
# lsof | grep 101 ... kvm 3649 root 18u REG 253,2 2147483648 524290 (deleted)/var/lib/vz/images/101/vm-101-disk-1.raw ...
Important is the pid (3649) and the filediscriptor (18) do an copy (before that, it's a good idea to stop important services with open files inside the VM, like databases)
cp /proc/3649/fd/18 /var/lib/vz/images/101/vm-101-disk-1.raw
Voila, the disk-file (of an open-VM!) is copied back.
Solution 2
To conver the image from raw to qcow2
qemu-img convert -p -f qcow2 -O raw /proc/2850/fd/19 vm-100-disk-1.raw
Test the copied file with an dummy-VM before shutdown the original VM!
Snapshoot operations
Create snapshoot
virsh shutdown vm_name virsh snapshot-create-as --domain vm_name \ --name "snapshot_name" \ --description "Snapshpot"
Restore snapshoot
virsh snapshot-list --domain vm_name virsh snapshot-revert --domain vm_name snapshot_name
Delete snapshoot
virsh snapshot-delete --domain vm_name --snapshotname snapshot_name_to_be_deleted
Adding a disk to VM
# virsh attach-disk {vm-name} \ --source /var/lib/libvirt/images/{img-name-here} \ --target vdb \ --persistent
Converting
Info about image
qemu-img info image.vmdk
From .vdi to .qcow2
qemu-img convert -f vdi -O qcow2 from.vdi to.qcow2
From qcow2 to .vdi
qemu-img convert -O vdi from.qcow2 to.vdi
From .img to .qcow2
qemu-img convert -f raw from.img -O qcow2 to.qcow2
From .qcow2 to .raw
qemu-img convert -O raw from.qcow2 to.raw