Virtualbox and Network Scripts on Ubuntu

| | | |

Virtualbox (http://www.virtualbox.org/) is a virtualzation platform. I use it to test out new Linux distributions as well as to run some limited tests of new software for customers. It can run on Linux or windows hosts and can run quite a few guest operating systems. Installation in Ubuntu is a snap. First enable the VirtualBox repo if you don't want to use the open source edition. Edit your /etc/apt/sources.list. I added this to mine for gutsy:

deb http://www.virtualbox.org/debian gutsy non-free

I did then an apt-get update && apt-get install VirtualBox and followed the on screen instructions. For more information on installing VirtualBox visit http://www.virtualbox.org/wiki/Downloads

Next I wanted to be able to use services that my virtual machines may offer. To do that I needed to setup a TAP interface. A TAP interface is like a bridge in this case, it allows the virtual machine to access the hosts network. My TAP interfaces are dynamic so when a virtual machine is started it automatically adds the need tap interface and when it is done run that same interface is deleted.

First you need to modify your network interfaces file in Ubuntu to setup a bridge for you tap interfaces to attach to. My /etc/network/interfaces file looks like this:


auto lo

iface lo inet loopback

auto br0

iface br0 inet dhcp

bridge_ports eth0

up chmod 0666 /dev/net/tun

auto eth0

iface eth0 inet manual

Then I created the the scripts needed to dynamically add and remove the tap interfaces. I put these scripts in /usr/local/sbin. I called them TapUp and TapDown


#!/bin/sh

# TapUP
# Script to automaticlly add tap interface for VirtualBox requires kdesu
# Also requires the the SUID bit is set on VBoxTunctl
# as root: chmod +s /usr/bin/VBoxTunctl
# Andrew Niemantsverdriet 12/23/07

# Find the current user id
userid=`id -u`

# Create TAP interface for current user and add interface to bridge
tapdevice=`/usr/bin/VBoxTunctl -b -u $userid`

gksudo echo $tapdevice
gksudo /sbin/ifconfig $tapdevice up
gksudo /usr/sbin/brctl addif br0 $tapdevice


#!/bin/sh

# TapDown
# Script to remove tap interface once virtual machine is closed
# Andrew Niemantsverdriet 12/23/07

# Remove interface from bridge and delete tap device
gksudo /usr/sbin/brctl delif br0 $2
gksudo "/usr/bin/VBoxTunctl -d $2"

Make sure that the scripts are executable and that the SUID bit is set on the /usr/bin/VBoxTunctl
file.

Next go to your virtual machine that you have created and edit the network settings. Under Enable Network Adapter change the “Attached to” to say Host Interface. Then in Host Interface Settings leave the interface name blank (VirtualBox will auto assign this) and change your setup application to be /usr/local/sbin/TapUP. The Terminate Application should be /usr/local/sbin/TapDown. Once you get that entered you should be good to go to bring up and down your TAP interfaces for VirtualBox to use.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Here is my variation of this to work with Network Manager.

interfaces

auto lo
iface lo inet loopback

iface br0 inet dhcp
bridge_ports eth0
up chmod 0666 /dev/net/tun

TapUP

#!/bin/sh

# TapUP
# Script to automaticlly add tap interface for VirtualBox requires kdesu
# Also requires the the SUID bit is set on VBoxTunctl
# DO THIS ON VBOX UPGRADE as root: chmod +s /usr/bin/VBoxTunctl
# Modified by Jason Villalta

# Find the current user id
userid=`id -u`

gksudo chmod +s /usr/bin/VBoxTunctl

# Create TAP interface for current user and add interface to bridge
tapdevice=`/usr/bin/VBoxTunctl -b -u $userid`

# If for some reason the interface could not be created, return 1 to
# tell this to VirtualBox.
if [ -z "$tapdevice" ]; then
exit 1
fi

gksudo echo $tapdevice
gksudo /sbin/ifconfig $tapdevice up
gksudo ifup br0
gksudo /usr/sbin/brctl addif br0 $tapdevice

TapDown

#!/bin/sh

# TapDown
# Script to remove tap interface once virtual machine is closed
# Modified by Jason Villalta

# Remove interface from bridge and delete tap device
gksudo /usr/sbin/brctl delif br0 $2
gksudo "/usr/bin/VBoxTunctl -d $2"
gksudo ifdown br0
# Bring Network Manager back online
gksudo /etc/init.d/NeworkManager restart


VBox status code: -3100 (VERR_INIT_HOSTIF_FAILED)

Hi,

I'm getting the same error.
Before that I got some other errors which
were because I made the mistake to do chmod as root
not as user with elevated priviliges.

Could this error come from the same mistake ?


Set SUID Bit on /usr/bin/VBoxTunctl

sudo chmod 4555 /usr/bin/VBoxTunctl


help

I have tried to follow your instruction but can't get it working the errormessage is

Failed to initialize Host Interface Networking.
VBox status code: -3100 (VERR_HOSTIF_INIT_FAILED).

Result Code:
NS_ERROR_FAILURE (0x80004005)
Component:
Console
Interface:
IConsole {e3c6d4a1-a935-47ca-b16d-f9e9c496e53e}

what shall I do


For those who don't know

For those who don't know like myself, how does one set the SUID bit on the /usr/bin/VBoxTunctl?
Only that one little part is missing from this.

Thanks for the scripts


Scott Dowdle's picture

man chmod

You can use any file manager app that let's you set permissions or you can kick it oldschool and use chmod. For more information about using chmod, man chmod.


not working

So, I have a MacBook with Ubuntu 8.04 installed. VBox is version 1.5.6 OSE.

So, I've installed Vista. But for the life of me I can't get networking to work. I followed your instructions, but nonetheless when I start VBox now, I get the following error:
Failed to initialize Host Interface Networking.
VBox status code: -3100 (VERR_INIT_HOSTIF_FAILED)
Result Code: 0x80004005
Component: Console
Interface: IConsole{1dea5c4b-0753-4193-b909-22330f64ec45}

Any input would be most appreciated. You can write me directly at:
cavaughan AT gmail DOT com
Thanks!


Change to scripts

I changed the scripts up because kdesu was not realiable enough, sometimes the window would not pop up to enter the password and there for make the virtual machine fail. gksudo does a much better job.

_
/-\ ndrew


Thomas's picture

VirtualBox

Innotek VirtualBox is very slick! I use it on my MacBook to run Microsuck XP, Debian, and Ubuntu. Once you install the guest OS drivers, it works even better! You decide how much RAM and HD space you will give to each Guest OS. I suggest less than half of your Host OS's RAM. If you have less than 256MB of RAM; I would recommend a partition & dual boot instead.

Thomas

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.