KVM tweaks

Hi, this time I post only some little teaks for Linux’ Kernel based Virtual Machine (KVM). If you followed my Installer article, then you might use little code snippets that hold the kvm binary call to start a single virtual machine. You can modify it this way that it is started in a “screen” and detaches itself.

/usr/bin/screen -dmS kvm-db -- /usr/bin/kvm \
-uuid 56150ea4-d959-4230-b66e-bf27676041c9 \
-m 256M \
-cpu host \
-smp 1,cores=1 \
-balloon virtio \
-name db \
-drive file=/data/kvm/db.qcow2,if=virtio,boot=on,cache=writeback \
-net nic,vlan=0,macaddr=54:52:00:bf:e6:bc,model=virtio \
-net tap,vlan=0,script=/etc/kvm/scripts/vlan0-ifup,downscript=/etc/kvm/scripts/vlan0-ifdown \
-net nic,vlan=1,macaddr=54:52:00:25:da:5e,model=virtio \
-net tap,vlan=1,script=/etc/kvm/scripts/vlan1-ifup,downscript=/etc/kvm/scripts/vlan1-ifdown \
-watchdog i6300esb \
-nographic \
-vga none \
-serial stdio \
-monitor unix:/var/run/kvm/db.sock,server,nowait \
-chroot /chroot \
-runas db

sleep 3

# Put the database server into a realtime class
/usr/bin/ionice -c 1 -n 2 -p $(ps -o pid=,args= -C kvm | grep -- "-uuid 56150ea4-d959-4230-b66e-bf27676041c9" | awk '{ print $1; }')

# Better IO scheduling
/usr/bin/renice -n -10 -u db > /dev/null 2>&1

This is a sample, how I start my database server. I provide a serial option that uses standard I/O. If you put a getty to ttyS0 in the guest, then this screen will prompt you a nice login.

If using Ubuntu, you might use a upstart script like this in /etc/init:

# ttyS0 - getty
# This service maintains a getty on ttyS0 from the point the system is
# started until it is shut down again.

start on stopped rc RUNLEVEL=[2345]
stop on runlevel [!2345]

exec /sbin/getty -L ttyS0 9600 vt102

If you have more than one virtual machine, the command screen -ls will show you the list of all running machines:

There are screens on:
24264.kvm-kdc (13.09.2010 16:56:09) (Detached)
24168.kvm-jabber (13.09.2010 16:55:29) (Detached)
24019.kvm-mx0 (13.09.2010 16:54:49) (Detached)
23924.kvm-www (13.09.2010 16:54:09) (Detached)
23842.kvm-dns (13.09.2010 16:53:29) (Detached)
23752.kvm-db (13.09.2010 16:52:49) (Detached)
2280.init-monitor (06.09.2010 11:20:25) (Detached)
7 Sockets in /var/run/screen/S-root.

If you had a close look in my sample output above, you might have realized that I use renice to change the process nice value.

And a last problem I solved is using the kernel module virtio-rng in all my guests to use the host systems entropy. On the host system itself, I have installed haveged. You can download it at SourceForge.net here

Go to /usr/local/src. Download it and extract it. Change into the newly created directory and run the triple ./configure make make install. The latter is installing an init runlevel script in /etc/init.d, which does not work on Ubuntu. Remove it and replace it with the following upstart script:

# haveged - Generate random entropy
# This service maintains a getty on tty6 from the point the system is
# started until it is shut down again.

start on runlevel [23]
stop on runlevel [!23]

exec /usr/local/sbin/haveged -w 1024 -v 1

Afterwards you can start it with: service haveged start

A last comment about the code fragment above: I changed back from VDE to bridged networking. It is faster. I look forward to use Open-vSwitch. See here for more information about this project.

Enjoy. Feedback as usual welcome 🙂