Thursday, August 04, 2011

Magically Provisioning VirtualBox

Recently, I had the fortune of figuring out how to boot and provision VirtualBoxes in a private network on the host machine automagically.

First, the primary objectives:
  • The virtual machines should be connected in a private network on the host machine so that they can communicate securely with each other. The machines should have an ip address in the private address space.
  • The virtual machines should also be able to communicate with the "outside world". From the point of view of the guest machine, this can be other machines on the same network as the host or the Internet-at-large.
  • The host machine should be able to network (primarily ssh) with the virtual machines. Typically, this can be done through the router gateway of the host or a "host-only adapter" (a virtual adapter that resides on the host).
Possible Solutions:
  • TunTap networking. This can be tricky to setup correctly and varies across platforms.
  • Each VM uses two network interface cards (NICs):
    - One Host-only adapter with its own dhcp server for the private network OR use the "Internal Network" feature of virtual box.
    - One bridge adapter that get its ip address from the outside world (possibly the router to which the host machine connects to).
Next, secondary objectives:
  • We want to boot the VMs automagically, find out the ip address.
  • Then, we want to ssh into the machine and do provisioning via chef or puppet.
Possible Solutions:
  • Use VBoxManage guestcontrol to execute the /sbin/ifconfig command
  • Use NAT with port forwarding for ssh
    ($ VBoxManage --natpf<1-N> [],tcp|udp,[] )
  • Hack /etc/init.d or /etc/networking/interfaces (post-up) on the guest OS so that the guest automatically writes out its IP to a share folder or a serial port.


No comments: