3/30/2013

An approach to KVM management with libvirt

Context

The following guide is dedicated to anyone that loves commandline interface and scriptability. It also serves as a solution against integrated tools in KVM management (this sounds weird! ^_^)

I will use seperate tools to complete the job.

Essential tools for regular tasks

Create a disk image

Use tool qemu-img in linux to create disk image:

qemu-img create -f format image_name size

qemu-img create -f qcow2 Windows7.qcow2 100G

See manpage of qemu-img to learn more options.

Convert image

If you need to use an existing disk image whose format is different from qcow2 or raw, you can convert it with qemu-img

qemu-img convert -f source_format -O output_format source_image output_image

qemu-img convert -f vmdk -O qcow2 Windows7.vmdk Windows7.qcow2

Create a VM

Use virt-install tool to create a new VM. Five basic options required for a new VM includes name, ram, disk, install method, graphics.

virt-install --name domain_name --ram size --disk path=/path/to/disk/image,format=disk_format --cdrom iso_image --graphics type,options

virt-install --name Centos4 --ram 1024 --disk path=centos.qcow2,format=qcow2 --cdrom CentOS-6.4-i386-minimal.iso --graphics vnc,listen=192.168.10.4

Install method usually takes cdrom or http url. The url must be directory listings of install content not an iso image. Below is an example:

virt-install --name Fedora18 --ram 1024 --disk path=fedora18.qcow2,format=qcow2 --location http://archive.fedoraproject.org/pub/fedora/linux/releases/18/Fedora/i386/os/ --graphics vnc,listen=192.168.10.4

The graphics option can be omitted if graphical display is not necessary for VM. By default vnc display of VM listens at 127.0.0.1. So if you need to access the display remotely, an IP address must specified. There are also other display methods than vnc. Check virt-install manpage for more.

List VMs

Use virsh tool to manage VMs.

List running VMs:

virsh list

List all existing VM:

virsh list --all

Power off VMs

virsh destroy domain_name

Start VMs

virsh start domain_name

Edit VM setting

Because a running VM corresponds to a process named qemu-kvm launched with passed arguments, so it is impossible to edit machine setting on runtime. The VM must be destroyed before editing. To edit VM settings:

virsh edit domain_name

Settings of VM are stored in XML format. You must learn XML tags at libvirt.org to customize your VM.

View VM serial/graphical console

User can interact with his VM via serial console or graphical console. Serial console may not be always present. It depends on whether OS on the VM outputs via serial console or not. To access serial console:

virsh console domain_name

For vnc display, each VM is bound to a vnc port for user to connect. The binding is done automatically during VM installation if user don't specify port option. To see at which port the vnc display of a VM is listening:

virsh vncdisplay domain_name

Then use a vnc viewer e.g tigervnc to access the display.

vncviewer ip:port

Essential concepts from libvirt

No comments:

Post a Comment