Notes From My KVM (Kernel Virtual Machine) Talk.

Monday, 20. July 2009

Notes From My KVM (Kernel Virtual Machine) Talk.

First of all, thanks to all who attended the SCLug meeting on Saturday. I had fun talking with everyone there.

I wanted to follow up with some written examples of the KVM command lines I demonstrated at the meeting. I know I seemed to go over this stuff kind of fast, so I wanted to elaborate a bit in text.

Installing the Ubuntu packages:

stu@milo:~% sudo apt-get install kvm \
        uml-utilities \
        bridge-utils

Creating a disk image file.

stu@milo:~% qemu-img create /home/stu/myhdisc.img 20G

This creates a hard disk image in my home directory named “myhdisc.img” that is 20Gbytes large.

Booting a host off of an ISO image using SDL as your display.

stu@mio:~% kvm -m 512 -hda /home/stu/myhdisc.img \
        -cdrom /home/stu/ubuntu-install.iso -boot d \
        -net user -net nic \
        -daemonize

This starts a kvm guest with the following settings:

  • It has 512Megs of Ram
  • It’s hard drive is the drive image we created
  • The ubuntu-install.iso file is mounted as the it’s CD drive
  • It is using user mode networking
  • It will be booting off it’s CD Rom
  • It’s primary display will be an SDL window on the desktop

This is by far, the easiest way to get your first virtual machine up. If you are running “X”, this is the easiest way to get started with KVM.

Using VNC for your display.

stu@milo:~% kvm -m 512 -hda /home/stu/myhdisc.img \
        -cdrom /home/stu/ubuntu-install.iso -boot d \
        -vnc localhost:0 -usbdevice tablet \
        -net user -net nic \
        -daemonize

As you can see, we have added the additional settings “-vnc localhost:0 -usbdevice tablet” to our command. This uses a VNC server instead of the SDL display used in the previous example. Note that we are specifying “localhost” as we do not want the server to allow every one to connect to it. You can also use 0:0 if you want to open it up to all hosts, but that probably is a bad idea.

Setting up a bridged environment for your guests.

Although user mode networking is much easier to setup, it has it’s limitations, the biggest of which is it doesn’t interact well on the network with other hosts. It’s great if all you are going to do is surf the web, or test out a new operating system, but it’s not good for much more.

Some of these commands you will want to run as root, and some as your normal user. So to simplify things, I’ll put the sudo command in front of the stuff you need root access for. This should work on a Ubuntu based system running the default “Desktop” configuration.

Here are the steps to setup your base bridging environment:

stu@milo:~% sudo /etc/init.d/NetworkManager stop

stu@milo:~% sudo /etc/init.d/NetworkManager-Dispatcher stop

stu@milo:~% sudo brctl addbr br0

stu@milo:~% sudo pkill dhclient

stu@milo:~% sudo ifconfig eth0 0.0.0.0

stu@milo:~% sudo brctl addif br0 eth0

stu@milo:~% sudo dhclient br0

If all goes well, you should have a bridge setup for this boot only. If you want to make it permanent, then go here and read all about it.

You will now need to create a tap adapter, and give your normal user account access to it. this is done with the tunctl program.

Setting up your tap device:

stu@milo:~% sudo tunctl -u stu -t tap0

Now we are almost done. We need to create a script to add the tap adapter to the bridge that we created. Here is an example of my /etc/kvm-ifup-br0 script.

The ifup script for kvm:

#!/bin/bash
sudo /sbin/ifconfig $1 0.0.0.0 up
sudo /usr/sbin/brctl addif br0 $1
exit 0

then, just make sure you change the rights on the file:

stu@milo:~% sudo chmod 755 /etc/kvm-ifup-br0

We are now ready to start the guest up in bridged network mode!

stu@milo:~% kvm -m 512 -hda /home/stu/myhdisc.img \
        -cdrom /home/stu/ubuntu-install.iso -boot d \
        -net nic,macaddr=52:54:00:12:55:01 \
        -net tap,ifname=tap0,script=/etc/kvm-ifup-br0 \
        -daemonize

If all your setttings are right, and you have your bridge and kvm-ifup file working correctly, your guest will come up on the network looking and acting like a regular host.

Now, once you get your guest operating system installed, you can remove the “-cdrom /home/stu/ubuntu-install.iso -boot d” settings and you will boot into your new guest system!

I hope this helps those who were at the meeting, and ofcourse, it would be great if it helped someone googling that was struggling with kvm.

— Stu

Share

One Response to “Notes From My KVM (Kernel Virtual Machine) Talk.”



  1. charlesnw Says:

    If you are running on a headless display, you might want to add -nographic to the end of the invocation.

    Also for windows guests I’ve found -alt-grab -usb -usbdevice tablet to work better for console access.

Leave a Reply

You must be logged in to post a comment.