Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[HOWTO] Setting up KVM with bridged networking
View unanswered posts
View posts from last 24 hours

Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message

Joined: 02 Jul 2002
Posts: 36
Location: Gothenburg, Sweden

PostPosted: Sun Oct 26, 2008 12:35 pm    Post subject: [HOWTO] Setting up KVM with bridged networking Reply with quote

Setting up KVM with bridged networking

0. Background

I use Windows for two things: remote desktop for connecting to my computer when I'm at work (RDP is so much better than VNC), and uTorrent's RSS downloading features. This does not warrant an entire computer, so I run Windows virtualized in Parallels Desktop on my Mac. However, I also have a Gentoo-server running 24/7, and I figured that having my Mac powered on during the day solely for running Windows virtualized is a waste when I might as well run the virtualization on my server.

I have previously run virtualization on Gentoo using VMWare, but this time I wanted to try something different. I spoke to some of my Linux-savvy co-workers and they suggested KVM, mainly because they hadn't worked with it and wanted me to try it out and give them a full heads up afterwards.

I had one specific requirement for the virtualization, and that is that the guest host should have direct access to my home network, in essence, the connection should be bridged. This proved to be a bit tricky to set up, mainly because there were no good documentation for it. Since I managed to get it up and running I figured I should post a tutorial describing how I went about it in case anyone else wanted to do the same thing.

1. Requirements

1.1 Kernel requirements

Loadable module support for KVM kernel modules:
[*] Enable loadable module support  --->

SMP is needed, otherwise KVM fails to emerge (at least for me):
Processor type and features  --->
    [*] Symmetric multi-processing support

Device Drivers  --->
    Character devices  --->
        <*> Enhanced Real Time Clock Support

Bridging support:
Networking  --->
    Networking options  --->
        <*> 802.1d Ethernet Bridging

Support for TUN/TAP:
Device Drivers  --->
    [*] Network device support  --->
        <*>   Universal TUN/TAP device driver support

(Disclaimer: there might be more options that are required that were enabled in my kernel config before I started to work with KVM)

1.2 Software requirements

We need KVM, and bridge-utils, and I also installed iproute2. I think iproute2 is optional from what I can gather in the /etc/kvm/kvm-if* scripts.
# emerge -av kvm bridge-utils iproute2

2. Bridge networking setup

I set up the bridge interface in the networking config to have it automatically running on boot:

brctl_br0=( "setfd 9" "sethello 2" "setmaxage 12" "stp off" )

config_eth0=( "null" )

config_br0=( "dhcp" )

depend_br0() {
    need net.eth0
# ln -s /etc/init.d/net.lo /etc/init.d/net.br0

I needed to make sure that br0 was online before running any init scripts that depended on net:


When running ifconfig, br0 is present and assigned an IP adress from DHCP while eth0 has no address.

3. Running KVM with bridged networking

Load the KVM modules if not already loaded:

For AMD:
# modprobe kvm-amd

For Intel:
# modprobe kvm-intel

Create a KVM image:
# kvm-img create <image name> 2G

Generate a MAC address for the virtual machine:
# echo -n DE:AD:BE:EF ; for i in `seq 1 2` ; do echo -n `echo ":$RANDOM$RANDOM" | cut -n -c -3` ;done

This could be put in a script for randomizing the MAC address each time you start an image, but I wanted a fixed address since I use static DHCP. See the references for examples of scripts.

Finally, run the image:
# kvm -hda <image name> -cdrom install-x86-minimal-2008.0.iso -boot d -vnc <your ip>:0 -net nic,vlan=0,macaddr=<generated MAC address> -net tap,vlan=0

Here I boot with the minimal install cd to test network connectivity and to verify that I get an address on my home network. Since my server is headless I start a VNC-server so I can access the virtual machine remotely.

4. References

Back to top
View user's profile Send private message

Joined: 06 Mar 2004
Posts: 182

PostPosted: Sun Oct 26, 2008 2:58 pm    Post subject: Reply with quote

thanks for the info, I tried looking into bridging before but found it a little difficult to setup
For info I've got some info up here about using the vde approach
Back to top
View user's profile Send private message
Tux's lil' helper
Tux's lil' helper

Joined: 31 Mar 2004
Posts: 117
Location: Dortmund - Germany

PostPosted: Fri Dec 19, 2008 7:55 pm    Post subject: Reply with quote

Many thanks for this tutorial, although I've gone a slightly different way in letting Gentoo handle the bridge and TAP device.

Interesting parts in /etc/conf.d/net:
config_mp0=( "null" )

bridge_br0="eth1 mp0"
config_br0=( "" )

config_eth1=( "null" )

You would then start kvm with following -net option: -net nic,macaddr=DE:AD:BE:EF:12:34 -net tap,ifname=mp0,script=no
Back to top
View user's profile Send private message

Joined: 08 Oct 2007
Posts: 15

PostPosted: Sat May 23, 2009 3:40 pm    Post subject: Reply with quote

Many thanks too for the HOWTO. I am a newbie and followed it and it just worked after I ran /etc/init.d/net.br0 start
(this can be run automatically at each boot with :
rc-update add net.br0 default
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum