Monday, April 30, 2007

debian in kvm mini howto

I started by downloading the debian-40r0-i386-CD-1.iso via jigdo.
Then I created a disk image file like this:

qemu-img create vdisk.qcow 1G

Then I boot kvm like this:

kvm -hda vdisk.qcow -cdrom debian-40r0-i386-CD-1.iso -boot d -m 256

(It may be needed to change the permissions of /dev/kvm)

Proceed with installation as a normal debian installation. I only installed the minimum.
The default kvm uses a build-in user space networking. However I'd like to have bridged
networking.
First I create a tap0 device in /etc/network/interfaces:

iface tap0 inet manual
pre-up tunctl -b -u <my_user_name> -t tap0
pre-up ifconfig tap0 up
post-down tunctl -d tap0

Make sure you have tunctl installed, its part of the uml-utilities package.
Replace the <my_user_name> by the user you want to run kvm as.
Normally my network interface is configured via DHCP, soo I want to keep that.
However I want kvm to be also able to access my LAN. Therefor I create a bridge interface:

iface br0 inet dhcp
bridge_ports eth0 tap0
bridge_maxwait 0

Which I bring up via DHCP. Of course I'll have to change eth0 also:

iface eth0 inet manual

Finually make sure they are brought up in the right order:

auto lo eth0 tap0 br0

In total this gives an /etc/network/interfaces like this:

auto lo eth0 tap0 br0

iface lo inet loopback

iface br0 inet dhcp
bridge_ports eth0 tap0
bridge_maxwait 0

iface eth0 inet manual

iface tap0 inet manual
pre-up tunctl -b -u <my_user_name> -t tap0
pre-up ifconfig tap0 up
post-down tunctl -d tap0

Finually edit the file /etc/kvm/kvm-ifup and put everything in it in comments.
Now bring down the network and bring it up again:

ifdown -a
ifup -a

And then start the kvm:

kvm -net nic -net tap,ifname=tap0 -hda vdisk.qcow -m 256 -localtime

Once booted, log in and change the /etc/network/interfaces file in the image to use DHCP:

iface eth0 inet dhcp

If you bring down the net and bring it up again (within the kvm!):

ifdown -a
ifup -a

you should get a DHCP lease from your LAN and the kvm is available on the LAN.

Now it's time to start using it!