# networking kvm (kernel virtual machine)

## XenoTerraCide

anyone know of a good article on setting up networking for kvm? I need to be able to remote into the vm from the net. pretty sure I need a tun/tap and a bridge and maybe something else... but i'm not exactly sure what it all means or how it goes together.

----------

## Gambit89

Have you tried the KVM article on gentoo wiki?  It looks complete... I was going to go through it, but I then found that Sony disables KVM in their BIOSes   :Evil or Very Mad: 

----------

## XenoTerraCide

yes... and then networking section doesn't work... at least not for my purposes... also it's not very gentoo-esque doesn't use the init scripts or anything. I can get outbound network from that. just not inbound.

----------

## Hu

You can provide inbound access in one of several ways.  If you want to use a tap device, then you need the host to bridge that device onto your main network or you need the host to act as a NAT gateway, and add appropriate DNAT rules to redirect traffic to the guest.  The same principles apply whether you are doing this for a KVM guest or for a system hidden on a LAN, so the home router guide might offer some insight.

----------

## stask

 *XenoTerraCide wrote:*   

> yes... and then networking section doesn't work... at least not for my purposes... also it's not very gentoo-esque doesn't use the init scripts or anything. I can get outbound network from that. just not inbound.

 

Here is what i did.

1. Followed the http://en.gentoo-wiki.com/wiki/KVM#Networking (compiled bridge and vlan support as modules, but it doesn't matter)

2. Added following to my /etc/conf.d/net (removing everything else). You will need to replace 'stask' with your username:

```

config_eth0=( "null" )

tuntap_qtap0="tap"

tuntap_qtap1="tap"

tuntap_qtap2="tap"

tuntap_qtap3="tap"

tuntap_qtap4="tap"

tuntap_qtap5="tap"

tuntap_qtap6="tap"

tuntap_qtap7="tap"

tuntap_qtap8="tap"

tuntap_qtap9="tap"

config_qtap0=( "null" )

config_qtap1=( "null" )

config_qtap2=( "null" )

config_qtap3=( "null" )

config_qtap4=( "null" )

config_qtap5=( "null" )

config_qtap6=( "null" )

config_qtap7=( "null" )

config_qtap8=( "null" )

config_qtap9=( "null" )

tunctl_qtap0="-u stask"

tunctl_qtap1="-u stask"

tunctl_qtap2="-u stask"

tunctl_qtap3="-u stask"

tunctl_qtap4="-u stask"

tunctl_qtap5="-u stask"

tunctl_qtap6="-u stask"

tunctl_qtap7="-u stask"

tunctl_qtap8="-u stask"

tunctl_qtap9="-u stask"

config_br0=( "dhcp" )

bridge_br0="eth0 qtap0 qtap1 qtap2 qtap3 qtap4 qtap5 qtap6 qtap7 qtap8 qtap9"

depend_br0()

{

   need net.eth0

   need net.qtap0

   need net.qtap1

   need net.qtap2

   need net.qtap3

   need net.qtap4

   need net.qtap5

   need net.qtap6

   need net.qtap7

   need net.qtap8

   need net.qtap9

}

brctl_br0=( "setfd 0" )

```

3. Created net.br0 and net.qtap[0-9] in /etc/init.d:

```

stask@cartman ~ $ ls -l /etc/init.d/net.*

lrwxrwxrwx 1 root root    18 Feb 16 18:57 /etc/init.d/net.br0 -> /etc/init.d/net.lo

-rwxr-xr-x 1 root root 15179 Feb 21 00:03 /etc/init.d/net.lo

lrwxrwxrwx 1 root root    18 Feb 16 18:57 /etc/init.d/net.qtap0 -> /etc/init.d/net.lo

lrwxrwxrwx 1 root root    18 Feb 16 18:57 /etc/init.d/net.qtap1 -> /etc/init.d/net.lo

lrwxrwxrwx 1 root root    18 Feb 16 18:57 /etc/init.d/net.qtap2 -> /etc/init.d/net.lo

lrwxrwxrwx 1 root root    18 Feb 16 18:57 /etc/init.d/net.qtap3 -> /etc/init.d/net.lo

lrwxrwxrwx 1 root root    18 Feb 16 18:57 /etc/init.d/net.qtap4 -> /etc/init.d/net.lo

lrwxrwxrwx 1 root root    18 Feb 19 13:33 /etc/init.d/net.qtap5 -> /etc/init.d/net.lo

lrwxrwxrwx 1 root root    18 Feb 19 13:33 /etc/init.d/net.qtap6 -> /etc/init.d/net.lo

lrwxrwxrwx 1 root root    18 Feb 19 13:33 /etc/init.d/net.qtap7 -> /etc/init.d/net.lo

lrwxrwxrwx 1 root root    18 Feb 19 13:33 /etc/init.d/net.qtap8 -> /etc/init.d/net.lo

lrwxrwxrwx 1 root root    18 Feb 19 13:33 /etc/init.d/net.qtap9 -> /etc/init.d/net.lo

```

4. Two more commands:

```

sudo rc-update del net.eth0 default

sudo rc-update add net.br0 default

```

That's it. Now i can run up to 10 virtual machines simultaneously. Of course you have to have DHCP server running in your network (otherwise you will need to adjust the /etc/conf.d/net).

I'm running the virtual machines using following command:

```

kvm -hda /data/virtual_machines/gentoo-x86_64_0.img -boot c -m 2G -smp 2 -net nic,macaddr=52:54:00:12:34:50 -net tap,ifname=qtap0,script=no,downscript=no -daemonize

```

Each instance should use it's own tap device (qtap0, qtap1, ...) and have different macaddr.

Hope it helps.

----------

