# Virtual Network Device - How to Create - TUN/TAP [Solved]

## jagdpanther

In the latest VirtualBox-2.1.0, which doesn't seem to be in portage, you can still use 'Host Interface' networking to attach to a host interface.  Unfortunately,  the command VBoxAddIF is no longer provided that I used to create virtual network devices (vbox0, vbox1 ...)

How do you create a virtual network device?  I assume with TUN/TAP?  (I tried to use the 'dummy' network device, it didn't work.)Last edited by jagdpanther on Tue Jan 20, 2009 11:35 pm; edited 1 time in total

----------

## SeaTiger

Are you trying to create tap/tun to be used by virtualbox 2.1.0?

I can post the setting later tonight once I got home, but so far I cannot make them work with 2.1.0 and people are asking in VBox forum too. It seems that no one is able to do it yet, though the manual say it is doable.

Edit:

Method 1:

Following is /etc/conf.d/net

```
config_eth0="null"

# set user to the one who going to use the

# interface.

tuntap_tap0="tap"

tunctl_tap0="-u username"

config_tap0="null"

tuntap_tap1="tap"

tunctl_tap1="-u username"

config_tap1="null"

tuntap_tap2="tap"

tunctl_tap2="-u username"

config_tap2="null"

tuntap_tap3="tap"

tunctl_tap3="-u username"

config_tap3="null"

tuntap_tap4="tap"

tunctl_tap4="-u username"

config_tap4="null"

tuntap_tap5="tap"

tunctl_tap5="-u username"

config_tap5="null"

tuntap_tap6="tap"

tunctl_tap6="-u username"

config_tap6="null"

tuntap_tap7="tap"

tunctl_tap7="-u username"

config_tap7="null"

# Add here all interfaces that you want to bridge

# eg eth0, but make sure to add config_eth0="null"

rc_need_br0="net.tap0 net.tap1 net.tap2 net.tap3 net.tap4 net.tap5 net.tap6 net.tap7"

bridge_br0="eth0 tap0 tap1 tap2 tap3 tap4 tap5 tap6 tap7"

# The following will be the host IP, it can be the default router

# for the VM in routing mode.

# You can use dhcp here if you like, it makes sense if you want

# to bridge your VM into the real network.

#config_br0="dhcp"

config_br0="192.168.1.80/24"

routes_br0=( "default via 192.168.1.1" )

brctl_br0="setfd 0

sethello 0

stp off"
```

Then

```
cd /etc/init.d

ln -s net.lo net.br0

ln -s net.lo net.tap0

ln -s net.lo net.tap1

ln -s net.lo net.tap2

ln -s net.lo net.tap3

ln -s net.lo net.tap4

ln -s net.lo net.tap5

ln -s net.lo net.tap6

ln -s net.lo net.tap7

rc-update del net.eth0

rc-update add net.br0

reboot
```

Method 2:

```
#!/bin/sh

USERNAME=username           # login name of HOST system

DHCP=0      # set to 1 to use dynamic ip for bridge

IP_ADDRESS=192.168.0.2    # static ip address of bridge (only used if DHCP set to 0)

ifconfig eth0 up

ifconfig eth0 0.0.0.0 promisc

brctl addbr br0

brctl addif br0 eth0

for ((i=0;i<8;i++))

do

tunctl -t tap$i -u $USERNAME

brctl addif br0 tap$i

ifconfig tap$i up

done

if [ DHCP = 1 ]; then

 dhclient br0

else

 ifconfig br0 $IP_ADDRESS

fi 

#chmod 0666 /dev/net/tun
```

Either method need:

```
net-misc/bridge-utils
```

----------

## jagdpanther

Yes, I am trying to create virtual network interfaces for use with VirtualBox-2.1.0.  In 2.0.6 I used VBoxAddIF to create vbox0 and vbox1.  I did not use a bridge.  I want to be able to totally isolate the virtual interfaces from real ones like eth0.  (Then when I want my virtual guest to connect to the 'real' world I just enable forwarding in iptables.)

----------

## rrbrussell

Only add the virtual interfaces to the bridge, then use iptables to handle the firewalling/routing as needed.

----------

## SeaTiger

From what I am experiencing the past few days, VBox 2.1.0 doesn't work with virtual interface properly   :Crying or Very sad: 

----------

## Audiossis

Are you using the same version of virtualbox-modules? The versions of virtualbox and virtualbox-modules must match in my experience.

Have you explicitly loaded the vboxdrv module?

You will also need the tun/tap module loaded from the kernel.

Make sure that these modules are loaded with lsmod.

eg. 

~#lsmod | grep vboxdrv

and

~#lsmod | grep tap

Here is the /etc/conf.d/net that I use. Albeit I use virtualbox-1.6.6 but I see no reason this shouldn't work with later versions, provided Virtualbox hasn't been broken upstream.

<<#### SNIP ####>>

tuntap_vbox0="tap"

## For a txqueuelen problem with tap device

postup() {  

	if [ ${IFACE} = vbox0 ] ; then

		/sbin/ip link set ${IFACE} txqueuelen 500

	fi

}

config_vbox0=( "10.0.1.1 netmask 255.0.0.0 broadcast 10.0.255.255" )

<<#### SNIP ####>

I've also found that tun/tap devices don't always initialize correctly unless they are configured at boot time, so you may need a reboot.

Make sure of course, that the tap module and vboxdrv module are loaded at boot up by added them to /etc/modules.autoload.d/kernel-X.X

Then just configure Virtualbox to use vbox0 as its interface. I've never used VBoxAddIF to create this interface. This method has always worked for me.

Good Luck.

----------

## jagdpanther

What tap driver?

Audiossis.  Thank you for your reply.

>

> ~#lsmod | grep tap 

>

What tap kernel module.  I found tun but not tap.

----------

## Audiossis

Sorry, I think you're right, it is the tun driver. I'll double check my setup when I get home from work. I wrote that post in kind of a hurry as it was getting late (for me).

I always get the names confused because it's very often refered to as the tun/tap driver.

I'm sure it doesn't, but it might pay to check your kernel config just to make sure that tap doesn't exist. If tun is all you can find then that'll be what you want. 

Bear in mind though that simply doing:

~#modprobe tun

may not cut it. In my experience the tun/tap interface does not start correctly like this. The driver needs to be loaded at boot up by putting it in /etc/modules.autoload.d/kernel-x.x (where x.x is your kernel series).

Also ensure that you link /etc/init.d/net.lo to /etc/init.d/net.vbox0 and /etc/init.d/net.vbox0 to /etc/runlevels/default/net.vbox0 with:

~#ln -s /etc/init.d/net.lo /etc/init.d/net.vbox0

and 

~#ln -s /etc/init.d/net.vbox0 /etc/runlevels/default/net.vbox0

to start the interface at boot up.

reboot and try it out. If the interface starts OK, it should just be a matter of configuring Virtualbox to use it. I can't remember how I have Virtualbox setup. I haven't looked at it for a while so again, I'll check it when I get home tonight.

----------

## SeaTiger

Audiossis,

Which version of vbox are you using? I am 100% sure that 2.0.6 works. However 2.1.0 is not working so far. Juist want to confirm before I spend time on this again.

----------

## Audiossis

I'm using 1.6.6 so I don't know if my suggestion will work for the versions you guys are playing with, just offering my experiences as I had similar problems with 1.6.6.

As I said earlier I've never used VBoxAddIF to setup the network interface as I couldn't get it to play nicely. I just setup the tap interface manually and it has always worked for me so I left it like that.

----------

## Audiossis

OK, I'm home now.

SO, to confirm details:

1: The driver name IS tun and NOT tap (my mistake, sorry)

2: ABSOLUTELY CRITICAL - make sure that the version of Virtualbox you are using is compatible with the version of Virtualbox-modules you are using.

3: Use these entries in /etc/modules.autoload.d/kernel-x.x to load the tun/tap driver and the vbox driver (this is what I use):

     tun -o vbox0

     vboxdrv

4: I am using Virtualbox 1.6.6 (as stated earlier) in "Host Interface" mode, with the PCnet-FAST III (am79c973) virtual device.

Just add the modules to the autoload file for your kernel, configure vbox0 with the details I posted earlier, set vbox0 to start on boot-up and then re-boot.

If all goes well you should be able to simply start Virtualbox, select the VM you want to use and open the network settings. Set the driver to the PCnet-FAST III and the mode to "Host Interface". Set the interface name to vbox0 and save your settings.

Start your VM and you should be go to go. This does not take care of routing but it will allow you to mount NFS/CIFS shares, print to network printers etcetera. If you want your VM to have internet access you'll likely need to set up a special route using iptables on your host OS.

Let me know how you get on, because I'd like to update my Virtualbox installation as well.

----------

## jagdpanther

From these posts and some on the VirtualBox forum, I guess I should stay with VirtualBox-2.0.6 for now because I depend on virtual network interfaces.  2.1.0 was the version I was having trouble with.

----------

## Audiossis

I guess that means you couldn't get it to work?

Ah well, it was worth having a go....

----------

## jagdpanther

I can't get it working yet.  I have not yet tried setting up the virtual interfaces at boot time.

----------

## SeaTiger

 *jagdpanther wrote:*   

> I can't get it working yet.  I have not yet tried setting up the virtual interfaces at boot time.

 To setup tap/tun during boot, use method 1 of my 1st reply.

----------

## Nerevar

This works for 2.0.x and has baselayout 1/2 differences:

http://en.gentoo-wiki.com/wiki/VirtualBox#Bridged

----------

## jagdpanther

Solved using 'tunctl' from Usermode Linux.

My goal:  A virtual network interface for VirtualBox 2.1.0 that will allow communications between guest AND host systems.  I do not want to use a bridge.  (When I need to connect to outside boxes, I use forwarding from the host's iptables and treat the host as a router.)

In Virtual Box 2.1.0  VBoxAddIF went away so ...

emerge usermode-utilities

modprobe tun

ifconfig -a

tunctl -t tap0      # can also use tap1 ...

ifconfig tap0 192.168.1.1

ifconfig -a

when done:

tunctl -d tap0

----------

## Audiossis

I found that was the simplest way of getting it working.

I do have my vbox0 as part of a bridge, but only because it simplified my iptables scripts. It works on its own if taken out of the bridge and given its own IP address.

I did it this way because:

1> I could not get VBoxAddIF to play nicely 

and

2> This is the way I did it when using I was using VMware.

The only thing you did differently to me was the use of tunctl to start the interface. I don't bother because my vbox0 is started at boot time.

Glad to hear you had a win!

----------

