Tuesday, May 29, 2012

Building and Sharing VirtualBox VM for development purposes

Here is a handy script (tested so far in OSX but hopefully with contributions it will support other OS) to build VirtualBox VMs from command line and share them with your team.

It can be handy when you share development environments within your team as I have posted before. I have tested this with Ubuntu 12.04 server.

Here is how to call the script to build a VM with Ubuntu 12.4 LTS:
$ ./build-vm.sh  Ubuntu_64 ~/Downloads/ubuntu-12.04-server-amd64.iso devbox 20000
The script configures the VM with two network interfaces. The first uses NAT so you get connection to Internet and the second is configured as host-only so you get access to it from your local host only. The internal network is created as

When you use the Ubuntu iso file you are installing the operating system. After you finish the manual installation you will have to manually configure the second interface as it will need a static IP (You want to easily connect to it via SSH):
$vi /etc/network/interfaces 
auto lo
iface lo inet loopback

# NAT network interface
auto eth0
iface eth0 inet dhcp

# Host-Only network interface
iface eth1 inet static
Bring down and up the network interfaces:
$ sudo ifdown eth0
$ sudo ifdown eth1
$ sudo ifup eth0
$ sudo ifup eth1
Note that depending on your actions SSH might be missing and you will need to install it:
$ sudo apt-get install ssh
It is unavoidable that at some point you start fresh however configuring network interfaces again and again does not sound like a great approach. Rather you can distribute the Virtualbox vdi file contained in the "VirtualBox VMs" directory. Before stopping the VM run the below to make sure everybody gets just eth0 and eth1 rather than eth2 and eth3 (Virtual Box assigned MAC addresses will be for sure different)
$ sudo rm /etc/udev/rules.d/70-persistent-net.rules
The receiver will just run the same command as before but pointing to the vdi file, for example:
$ ./build-vm.sh Ubuntu_64 ~/Desktop/devbox.vdi devbox 20000
If you get the below after trying to restart networking or trying to bring up eth1 all you need to do is ignore it as basically that means there is one of the cards already up and apparently NAT and host-only share the same device:
RNETLINK answers: File exists Failed to bring up eth1
Same story if you get the below when trying to put down the interface:
RNETLINK answers: File exists interface eth1 not configured
You will notice ifconfig shows the eth1 interface with the IP but the network is unreachable when you put eth0 down. Go figure.

No comments: