Sunday, April 12, 2009

VirtualBox VM with Host Interface Setup on Debian Lenny

For sometime now I had been wishing for a debian virtual machine which can talk to the host - another debian or a ubuntu. If you have used VirtualBox on debian/ubuntu before, I am sure you know NAT is the easiest type you can setup in VirtualBox OSE; while Host Interface configuration is probably the toughest to figure out on your own, especially if you are not used to setting up bridges and stuff. But necessity is mother of reasearch, for engineers. So after a day of struggle I have finally got what I wanted. Here I share with you not only the installation of virtualbox ose in a trouble free manner, but also the simplest way host interface can be configured.

Installing virtualbox on ubuntu turned out to be a breeze for me, as I used the VirtualBox package directly from the virtualbox website. On my Debian Lenny 5.0 though, the DVDs installed VirtualBox-OSE v1.6x which is the open-source-edition; and totally inline with the debian tradition and philosophy. Since I am becoming more and more inclined towards this total-GPL thingy, I decided to set up this GPLed virtualbox only, come what may. I hear Virtualbox v2.1 makes things a lot easier though, just in case you wanna try.

To start with, first I installed the virtualbox-ose packages.
mrutyunjay:~$ sudo apt-get install virtualbox-ose virtualbox-ose-modules module-assistant

module-assistant is required for setting up 'vboxdrv' module required by virtualbox.

Once the installation is through, auto-install the vboxdrv module using module-assistant.
mrutyunjay:~$ sudo m-a prepare
mrutyunjay:~$ sudo m-a -f get virtualbox-ose
mrutyunjay:~$ sudo m-a a-i virtualbox-ose
mrutyunjay:~$ sudo modprobe vboxdrv


Add yourself to the 'vboxusers' group (so that virtualbox can access vboxdrv)
mrutyunjay:~$ sudo adduser username vboxusers

Check your 'kernel' argument list in /boot/grub/menu.lst. Just in case it has something like nmi-watchdog in it, remove that portion. (Mine didnt have this). This is to avoid one of the most commonly observed troubles while setting up virtualbox.
Check if 'vboxdrv' module was loaded successfully -
mrutyunjay:~$ dmesg | grep vboxdrv

You should see a success message, like the one below -
[ 1883.765045] vboxdrv: Successfully loaded version 1.6.6_OSE (interface 0x00080000).

If you get the above message, you are done with the first part. Now reboot your system (relogin should do as well).

Host Interface Configuration

For setting up Host Interface, we will need to install bridge-utils package.
mrutyunjay:~$ sudo apt-get install bridge-utils

There are at least two ways to setup the bridge which will enable the host to talk to guest, and vice versa. One is to set a simple bridge definition in /etc/network/interface file. The other is to write setup and cleanup scripts for bridge and TAP interfaces (using VBoxTunctl and brctl); and then run these scripts before and after start-stop of the virtual machine. Out of these, the first approach of modifying the  /etc/network/interface file, is easiest one and gets the job done in short time.

To setup the bridge, first you need to create a permanent TAP interface for virtualbox. Open the /etc/vbox/interfaces file -
mrutyunjay:~$ sudo emacs /etc/vbox/interfaces

Add following definition to it -

vbox0 username br0

Goes without saying that you should replace 'username' with your user name in the above definition.
Here br0 is the name of the bridge that we are going to setup now.

Open the /etc/network/interfaces file for editing.
mrutyunjay:~$ sudo emacs /etc/network/interface

Add following bridge definition to the file -

auto br0
iface br0 inet static
      address 10.0.0.1
      netmask 255.255.255.0
      bridge_ports vbox0


You can customize the IP address and netmask. I have set it up to be static, for simplicity.

Now that we are done with network interface settings, time to restart both the networking interfaces -
mrutyunjay:~$ sudo /etc/init.d/networking restart
mrutyunjay:~$ sudo /etc/init.d/virtualbox-ose restart


Our 'vbox0' TAP interface requires bridge 'br0' defined before it gets attached to the bridge. So make sure you dont alter the order above.
After restarting network interfaces, ensure that our TAP and bridge interfaces are up and running, using /sbin/ifconfig. It should show 'br0' with above entered IP address and 'vbox0' without any IP address.
We enter this TAP interface name (vbox0) in virtualbox network configuration. Set the network to Host Interface and enter vbox0 as interface name and start the virtual machine.

Guest VM Setting (linux flavour)


I had lightweight text-mode debian lenny 5 as my guest VM. So I will explain the network setting accordingly. But for any other linux flavours, network setting should be on these lines only.

Here in the guest VM, you need to setup the networking interface eth0 to be aware of 10.0.0.1 bridge. The bridge is your host machine.
So add following information to your /etc/network/intertface file -

shaunak:~$ sudo emacs /etc/network/interface

iface eth0 inet static
    address 10.0.0.2
    netmask 255.255.255.0
    network 10.0.0.0
    broadcast 10.0.0.255
    gateway 10.0.0.1



And finally, restart networking -
shaunak:~$ sudo /etc/init.d/networking restart

That's it! Now try pinging your bridge 10.0.0.1 from VM and vice versa -

shaunak:~$ ping -c3 10.0.0.1

mrutyunjay:~$ ping -c3 10.0.0.2

If both the pings are successful, YO!, your network is up and running.


1 comment:

UuTheExplorer said...

how to setting web server in pc guest ???
please talk to me....