Saturday, July 26, 2014

Interconnecting (Bridging) LAN with VirtualBox Host Only Adapter–Lubuntu/FatDog64

In this article we will discuss about interconnecting the Wired Ethernet with the VirtualBox’s Host Only network. But why think about such a scenario? See the below points that are our specific requirements.

a. VirtualBox Guests should be visible in the Physical LAN, like any other physical device attached to the network

b. Guest Machines should be able to acquire dynamic IP addresses from the DHCP server attached in the LAN

b. We should be able to RDP, the Guest, by directly specifying its name or IP Address

c. Guests should be able to directly access, network resources like a network share, Printer connected to LAN

d. Guests should be able to communicate with each other, even if the physical LAN cable is unplugged in the host machine

Note: We can use ‘bridged Adapter’ in VirtualBox UI (See below figure), to bridge the physical network, with the virtual box network. But the problem is, once the Physical LAN cable is unplugged, the guest should see their virtual network as unplugged as well, and they will not able to communicate with other Guest machines that resides in the same host machine itself.

image

e. Guest should have only one ‘Virtual Network Adapter’ be configured in the VirtualBox UI, for easy management.

Note: We can have multiple network adapters to handle this situation. Like one adapter (configured as host only) for communicating among only with guests and another adapter (Bridged to physical network, like in the above figure) to communicate  out side of the host machine. But we feel it as a less streamlined solution, as we’ve to manage 2 separate adapters inside the guest machine only for handling this scenario.

Being said that, we can now look into a solution on how to achieve this. Typically the solution will be, bridging the Ethernet (eth0) with the Virtualbox’s host only adapter (vboxnet0).

This article assumes, the below prerequisites.

i. You’ve a working Ubuntu (Or its derivatives) or FatDog64 Full installation (Like one discussed here)

j. You’ve a working VirtualBox installation and the Host only adapter have been created.

image

Note: Installing latest virtualbox in FatDog64 full install is discussed here.

k.Bridge Utility is available with your installation

Note: FatDog64 installation already contains ‘bridge-utils’. For ubuntu, use ‘apt-get install bridge-utils’ command.

The below figure, help us to grasp the over all picture.

image

Ok that’s all about the environment, now we will look into the implementation.

1. Bridge Physical LAN/Ethernet with VirtualBox’s Host Only Adapter

Please remember to replace the IP Addresses, Subnetmask, default gateway as per your environment.

In our case, we are using static IP’s for both our bridge (192.168.1.200) and vboxnet0 (192.168.1.201). Our default gateway is a ‘iBall Router’ (192.168.1.1) . For our virtualbox guest VM’s and other PC’s connected to LAN, IP address will be served by the ISC-DHCP-Server, as it is configured to listen through ‘br0 eth0’, on its own configuration file.

1.1 FatDog64 Implementation

Add the below script segments to the very end of ‘/etc/rc.d/rc.local’ file.

vboxmanage hostonlyif ipconfig vboxnet0 --ip 192.168.1.201 --netmask 255.255.255.0

brctl addbr br0
ifconfig eth0 0.0.0.0 down
ifconfig vboxnet0 0.0.0.0 down
brctl addif br0 eth0
brctl addif br0 vboxnet0
ifconfig eth0 up
ifconfig vboxnet0 up
ifconfig br0 192.168.1.200 netmask 255.255.255.0 up

route add default gw 192.168.1.1

#uncomment, if you've setup ISC-DHCP-Server, and not relying on Virtualbox built in DHCP Server
#/usr/local/etc/ISC-DHCP-Server/dhcp-server start

1.2 Ubuntu Implementation

Add the below script segments to the end of ‘/etc/rc.local’ file, just before the ‘exit 0’ statement.

sudo vboxmanage hostonlyif ipconfig vboxnet0 --ip 192.168.1.201 --netmask 255.255.255.0

sudo brctl addbr br0
sudo ifconfig eth0 0.0.0.0 down
sudo ifconfig vboxnet0 0.0.0.0 down
sudo brctl addif br0 eth0
sudo brctl addif br0 vboxnet0
sudo ifconfig eth0 up
sudo ifconfig vboxnet0 up
sudo ifconfig br0 192.168.1.200 netmask 255.255.255.0 up

sudo route add default gw 192.168.1.1

#uncomment, if you've setup ISC-DHCP-Server, and not relying on Virtualbox built in DHCP Server
#sudo restart isc-dhcp-server

2. Configure VirtualBox Guest Machines, with Host Only Adapter

Now for each virtual machine, that should be directly exposed to the physical LAN, Select ‘Host-Only Adapter’ and ‘vboxnet0’, in the Virtual machine’s Network property page.

image

Once done, these machines will be exposed to the physical LAN, like every other physical machine attached to it.

Advantages:

You can create network shares inside, the virtual machine and can be accessed directly across other physical machines attached to the LAN and vice versa.

Virtual Machines can be configured for ‘DHCP’ and will be able to lease dynamic IP addresses from the actual physical DHCP server hosted on the network. This is worth, if you’re managing a large number of virtual box guest machines (i.e Configuring static IP addresses, default gateway to each one is a tedious and time consuming). This is a versatile design, if you’re going for a failover mechanism once the default gateway is down and you want to redirect all traffic to another router.

You can directly connect to the virtual machines, from any where in the network using its IP or Host Name.

Even if the physical network cable is unplugged in the Host Machine, virtual machines hosted on the same virtual box host machine, will be able to communicate with each other, as the bridge will still work inside the host machine.

3. Verify that Virtual Machine is directly exposed in Physical Network

You can verify this, in many ways. Like you can check whether virtual machine is getting a valid IP from your DHCP-Server. In our case, we’ve done the below:

We’ve created a read only ‘samba share’ in our virtual box host machine (Lubuntu Installation). Now from our virtual machine (Windows 7), we’ve tried to access the ‘samba share’ in the host machine, using it’s UNC path. Like (\\HostMachineName\ShareName). Now we’ve been able to view and browse the network share contents without any issues as below.

image

Appendix: Setting up a Bridge in Linux Variants – A generic Approach

Though the above article describes on bridging between LAN and Virtualbox host only network in specific, bridging concept is a generic term that is not specific to any specific virtual network adapter implementation.

Bridge works at the Data Link Layer (Layer2) of the OSI network model. Bridges inspect incoming traffic and decide whether to forward or discard it. An Ethernet bridge, for example, inspects each incoming Ethernet frame - including the source and destination MAC addresses, and sometimes the frame size - in making individual forwarding decisions.Bridges serve a similar function as network switches that also operate at Layer 2. Traditional bridges, though, support one network boundary (accessible through a hardware port), whereas switches usually offer four or more hardware ports. Switches are sometimes called "multi-port bridges" for this reason.

In Linux, we can define bridges in two places, so that they are functional at the very startup of the system.

Method A: (rc.local)

One is, as described in the above article ‘rc.local’ file. The above example can be extended to have a generic approach, that can bridge any network interfaces in theory (Both physical and virtual network interfaces). For example a virtual network adapter, created with the KVM Virtualization utility can be bridged along with Virtualbox Host Only Adapter. We can define that generic approach as below.

brctl addbr br0 

#for each interface $iface in the list to be bridged
; do
#ifconfig $iface 0.0.0.0 down
#brctl addif br0
$iface 
#done

#for each interface $iface in the list to be bridged ; do
ifconfig $iface up 
#done 

ifconfig br0 <IP> netmask <subnetmask> up
 

In the above example, a base bridge (br0) has been setup and we are adding each network interfaces (that is to be bridged) using a ‘For Loop’ (each interfaces will be iterated through the variable ‘$iface’). The ‘$iface’ can take any network interface, including the ‘VirtualBox host only adapter-vboxnet0’ as in the above example.

Method B: (/etc/network/interfaces)

There is one more streamlined approach to define bridges. Similar to defining network interfaces in ‘/etc/network/interfaces’ file, we can also define bridges as well in that file. As sample is given below.

auto br0
              iface br0 inet static
                  address 192.168.1.200
                  network 192.168.1.0
                  netmask 255.255.255.0
                  broadcast 192.168.1.255
                  gateway 192.168.1.1
                   bridge_ports
eth0 vboxnet0
                   bridge_maxwait 0

The above snippet does the same thing as we’ve done with the ‘rc.local’ example in the entire article. See how ‘eth0’ and ‘vboxnet0’ has been bridged using ‘bridge_ports’ element. You can include many more options in this way, like bridging every network interfaces available in one go. Read more on this page.

Note: Direct bridging will only work with wired Ethernet (eth0) and vboxnet0. If you’re using wireless physical adapter (wlan0), then you may have to do some extra configurations (like hostpad) to make it work. You should probably start here.

No comments:

Post a Comment