Thursday, 5 September 2013

Debian and Ubuntu guide to KVM and virsh commands

Before I continue, just noticed my one servers drive is 60737 hours old today - 6.92 years (SAMSUNG SpinPoint P80 SD) and power cycled 12 times, incredible! Back to KVM...

Install the required packages using (all commands assume root user access):

apt-get install qemu-kvm libvirt-bin virtinst virt-viewer virt-manager

Then create your image file, this will become the virtual guest's "hard drive", you can adjust the size, it won't reserve the space on your host's hard drive:

qemu-img create -f qcow2 /var/lib/libvirt/images/myserver.qcow2 25G

You can use the command below to install from an ISO image, you may want to tweak -r as it specifies how much memory to assign to the instance in MB and the vcpus depending on how many CPU cores you would like to assign to your instance. Before running the command you may want to login with ssh -X if your doing this remotely on a server:

virt-install -r 4096 --vcpus=8 --accelerate -n MyServer -f /var/lib/libvirt/images/myserver.qcow2 --cdrom /tmp/debian-7.1.0-amd64-netinst.iso --vnc

Need to attach a second drive to your instance? No problem, create a new image with the above qemu-img command then:

virsh attach-disk --driver qemu --subdriver qcow2 MyServer /var/lib/libvirt/images/mysever_disk2.qcow2 sdb

To clone a virtual instance (domain), create a new image file eg. yourserver.qcow2 then:

virt-clone --original MyServer --name YourServer --file /var/lib/libvirt/images/yourserver.qcow2

To see a list of all your KVM's: virsh list --all
You can open the virt-viewer show the display of your domain by: virt-viewer MyServer
To remove a domain from your host system: virsh undefine MyServer
To poweroff the domain: virsh destroy MyServer
To shutdown the domain: virsh shutdown MyServer
To reboot the domain: virsh reboot MyServer

More to come soon...