KVM on Debian mini-HOWTO

A quick look about KVM (http://kvm.qumranet.com/kvmwiki/KVM-Wiki):

KVM (for Kernel-based Virtual Machine) is a full virtualization solution for Linux on x86 hardware. It consists of a loadable kernel module (kvm.ko) and a userspace component.

Using KVM, one can run multiple virtual machines running unmodified Linux or Windows images. Each virtual machine has private virtualized hardware: a network card, disk, graphics adapter, etc.

The kernel component of KVM is included in mainline Linux, and will appear in Linux 2.6.20.

KVM is open source software.

So how to set it up under Debian? Since KVM is still under active development, using Debian sid (unstable) would be a good idea; on the other hand, a VT supported CPU is also required; finally, you should have X11 installed. You may handle the setup of KVM client in many different way: manual command line parameter, custom pre-defined start/stop/config shell scripting, or by using libvirt + virt-manager. For this simple mini-HOWTO, I will guide you though the 1st solution with the least dependence.

Prepare your Debian sid

First of all, let's setup your APT with unstable/testing/stable hybrid support. Here is my /etc/apt/sources.list as reference:

deb http://ftp.hk.debian.org/debian/ stable main contrib non-free
deb-src http://ftp.hk.debian.org/debian/ stable main contrib non-free
deb http://ftp.jp.debian.org/debian/ stable main contrib non-free
deb-src http://ftp.jp.debian.org/debian/ stable main contrib non-free

deb http://ftp.hk.debian.org/debian/ testing main contrib non-free
deb-src http://ftp.hk.debian.org/debian/ testing main contrib non-free
deb http://ftp.jp.debian.org/debian/ testing main contrib non-free
deb-src http://ftp.jp.debian.org/debian/ testing main contrib non-free

deb http://ftp.hk.debian.org/debian/ unstable main contrib non-free
deb-src http://ftp.hk.debian.org/debian/ unstable main contrib non-free
deb http://ftp.jp.debian.org/debian/ unstable main contrib non-free
deb-src http://ftp.jp.debian.org/debian/ unstable main contrib non-free

deb http://volatile.debian.org/debian-volatile stable/volatile main contrib non-free
deb http://security.debian.org/ stable/updates main contrib non-free
deb-src http://security.debian.org/ stable/updates main contrib non-free
deb http://ftp.debian-unofficial.org/debian unstable main contrib non-free restricted
deb-src http://ftp.debian-unofficial.org/debian unstable main contrib non-free restricted

You may also setup /etc/apt/preference as below for additional APT-pinning handle (this example pin your Debian as unstable, check its priorities according to your needs):

Package: *
Pin: release a=stable
Pin-Priority: 600

Package: *
Pin: release a=testing
Pin-Priority: 650

Package: *
Pin: release a=unstable
Pin-Priority: 700

Moreover, you will need to create /etc/apt/apt.conf.d/99cache as below, or else your cache may be busted:

APT::Cache-Limit "134217728";

Whatever you are now using Debian stable/testing, just run the following command and upgrade your Debian as unstable, install latest Linux kernel + kernal header + KVM + module-assistant, and install kqemu plug-in as spare:

apt-get update
apt-get dist-upgrade
apt-get install linux-image-2.6-amd64 linux-headers-2.6-amd64 kvm module-assistant
module-assistant auto-install kqemu

After reboot, check your kernel version with uname -a, e.g.:

Linux dc.edin.no-ip.com 2.6.24-1-amd64 #1 SMP Mon Feb 11 13:47:43 UTC 2008 x86_64 GNU/Linux

Setup your hardware and network

Let's probe KVM required modules:

modprobe kvm_amd kvm kqemu

And check the result with lsmod

Next we should setup the network connection. Before further configuration, stop your current active network interface:

ifdown eth0

Edit /etc/network/interface as like as below, e.g. replace the original eth0 configuration as br0, and add other required parameters:

auto br0
iface br0 inet static
         address 192.168.2.4
         netmask 255.255.255.0
         network 192.168.2.0
         broadcast 192.168.2.255
         gateway 192.168.2.2
         bridge_ports eth0
         bridge_stp off
         bridge_maxwait 5

Now start the new interface with:

ifup br0

Some kernel parameters are required, too. Add the following lines in /etc/sysctl.conf:

net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0

Load the new setting with:

sysctl -p

For further more details of libvirt networking, please refer to here:
http://libvirt.org/formatnetwork.html#examplesNAT
http://wiki.libvirt.org/page/Networking

Reboot, and check your network with ifconfig.

Create KVM client disk image

I would like to suggest hold all your KVM client disk image under a specific directory, e.g.:

mkdir -p /srv/demo

Go to that directory and run the following command (32GB image with qcow2 format):

qemu-image create -f qcow2 /srv/demo/hda.qcow2 32G

Start your KVM client

Assume you hope to install Debian stable in this KVM client (prepare you debian-40r3-i386-netinst.iso in /srv), run the following command to start the image (you should run kvm --help for more detail of this configuration):

kvm -hda /srv/demo/hda.qcow2 -net tap,vlan=0 -m 256 -vnc :0 -k en-us -daemonize -cdrom /srv/debian-40r3-i386-netinst.iso -boot d

Then use any VNC client, and connect to this KVM client (e.g. localhost:5900). Start the installation and enjoy with it :)


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <h1> <h2> <h3> <h4> <h5> <h6> <em> <strong> <code> <del> <blockquote> <q> <sub> <p> <br> <ul> <ol> <li> <dl> <dt> <dd> <a> <b> <u> <i> <sup> <acronym> <pre> <img>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Images can be added to this post.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.