Login or register    
Website screenshot service | LinkPeek.com

Install packages

Debian or Ubuntu

  • sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils

Redhat or Fedora

  • yum install kvm
  • yum install virt-manager libvirt libvirt-python python-virtinst
  • chkconfig libvirtd on
  • service libvirtd start

KVM virsh cheat sheet!

This is a list of commands to use in the virsh cli.

To start the virsh cli run the following cmd:

$ virsh


virsh --connect qemu:///system

KVM has its own commands, but we like to use virsh.

Here is a list comparing Xen and KVM (virsh) commands.

List running guests

xm list
virsh list
virsh dominfo <dom name>

Start a guest

xm create /etc/xen/<dom name>
virsh create /etc/libvirt/qemu/<dom name>.xml
virsh start <dom name>

Console to guest

xm console <dom name>
virsh console <dom name>

Shutdown a guest

xm shutdown <dom name>
virsh shutdown <dom name>

This command doesn't always work...

Be sure the guests XML file has the following entry:


Also make sure the apci is installed on the guest operating system.

debian / ubuntu
sudo apt-get install acpid
fedora / redhat
yum install acpid

Autostart guest on reboot

ln -s /etc/xen/<dom name> /etc/xen/auto/<dom name>
virsh autostart <dom name>

Edit a guests configuration file

virsh edit <dom name>

Pull the power on a guest

xm destroy <dom name>
virsh destroy <dom name>

Quit virsh cli

virsh quit

Virtual Serial Console configuration

From the KVM host run:

virsh edit <dom>

Then add the following inside the <device></device> directives:

<serial type="pty">
    <source path="/dev/pts/3" />
    <target port="1" />

From the Guest run:

sudo cp -p /etc/init/tty06.conf /etc/init/ttyS0.conf
sudo vi /etc/init/ttyS0.conf
sudo diff /etc/init/tty06.conf /etc/init/ttyS0.conf

Vmbuilder Example

This example assumes ubuntu linux.

Install vmbuilder

sudo apt-get install python-vm-builder

Display Help vmbuilder

vmbuilder kvm ubuntu --help

Custom vmbuilder string

mbison example

mkdir /vms/$VMNAME

sudo vmbuilder kvm ubuntu --libvirt=qemu:///system --suite=lucid --flavour=virtual \
--hostname=$VMNAME --domain='foxhop.net' --rootsize='10240' --mem='1024' \
--ip= --gw= --dns= --bridge=br0 \
--addpkg=openssh-server --addpkg=acpid --timezone=EDT --verbose

cammy example

mkdir /vms/$VMNAME

sudo vmbuilder kvm ubuntu       \
    --libvirt=qemu:///system    \
    --suite=lucid               \
    --arch=amd64 -o             \
    --flavour=virtual           \
    --hostname=$VMNAME          \
    --dest=/vms/$VMNAME         \
    --rootsize=20480            \
    --mem=1024                  \
    --bridge=br0                \
    --ip=           \
    --gw=          \
    --dns=          \
    --user=john                 \
    --pass=doe                  \
    --addpkg=openssh-server     \
    --addpkg=acpid              \
    --timezone=EDT              \


debian netboot example

This method shows the virt-install script installing from a debian netboot image hosted on the internet


sudo virt-install \
--name=$HOSTNAME \
--vcpu=1 \
--ram=396 \
--disk=/KVMROOT/$HOSTNAME.qcow2,size=10 \
--os-type=linux \
--autostart \
--location=http://ftp.nl.debian.org/debian/dists/wheezy/main/installer-amd64/ \
--extra-args="auto=true priority=critical keymap=us locale=en_US hostname=$HOSTNAME domain=$DOMAIN url="

ubuntu netboot example

This method shows the virt-install script installing from an ubuntu netboot image hosted on the internet


sudo virt-install \
--name=$HOSTNAME \
--vcpu=1 \
--ram=396 \
--disk=/KVMROOT/$HOSTNAME.qcow2,size=10 \
--os-type=linux \
--autostart \
--location=http://archive.ubuntu.com/ubuntu/dists/raring/main/installer-amd64/ \
--extra-args="auto=true priority=critical keymap=us locale=en_US hostname=$HOSTNAME domain=$DOMAIN url="

Mounting a qcow2 image on the host

Sometimes it is helpful to be able to mount a drive image under the host system. For example, if the guest doesn't have network support, the only way to transfer files into and out of the guest will be by the storage devices it can address or to restore files from a backup image.

To mount qcow2 images there is (at least in F-11 qemu) very useful qemu-nbd util. It shares image through kernel network block device protocol and this allows to mount it:

sudo modprobe nbd max_part=63
sudo qemu-nbd -c /dev/nbd0 image.img
sudo mount /dev/nbd0p1 /mnt/image

Add another disk image to a guest

This is how you add an additional disk to a guest. In this example I will be adding a 20GB disk image to a guest dom named cammy.

fallocate -l 20g /vms/cammy/cammy-aux.img


qemu-img create -f qcow2 /vms/cammy/cammy-aux.img 20G

If you want to look that the image statistics try this command.

qemu-img info /vms/cammy/cammy-aux.img

Now we need to mount this disk:

sudo virsh attach-disk cammy /vms/cammy/cammy-aux.img vdb

That command only mounted the disk. To make this change permanent we need to alter the dom's xml:

# sudo virsh edit cammy

 <disk type='file' device='disk'>
   <driver name='qemu' type='qcow2'/>
   <source file='/KVMROOT/cammy.qcow2'/>
   <target dev='vda' bus='virtio'/>
   <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>

 <disk type='file' device='disk'>
   <driver name='qemu' type='raw'/>
   <source file='/KVMROOT/cammy-aux.img'/>
   <target dev='vdb' bus='virtio'/>
 <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>

SSH to the guest and verify that the disk shows up in fdisk -l. You may now partition this disk how you like. For more information view linux raw filesystem management.


Leave a comment

Please login or register to leave a comment!