VirtualBox: easily access your NAT configured Guest from the Host

This is a placeholder for the actual post: VirtualBox: give Host-Only access to internet. The post deserved two titles.

VirtualBox: give Host-Only access to internet

This post describes a simple trick for VirtualBox to give host-only virtual machines access the internet or access your host from the guest using NAT. The title could also have been: “Easily access your NAT configured virtual machine.”

The original problem or requirements:

  • The host should be able to browser the webserver on the Guest (the virtual machine)
  • The guest should be able to access the outside world (intranet/internet)
  • Use fixed IP address, no problems with DHCP et al.

Yes, you can use port-forwarding using NAT network interface for the virtual machine. But configuring this is just way to complicate and error prone. A few googles more, and I couldn’t find anything good.

The solution I came up with: configure 2 network interfaces for your Virtual Box host. Simple, isn’t it?

First, we need to make a new network in VirtualBox. You can do this in the GUI preference, which is a bit platform specific, but it’s not easy to miss.

Create a new network in VirtualBox, named like vboxnet2 with following settings (change to suit your needs):

  • IPv4 Address: 10.88.12.0
  • IPv4 Network Mask: 255.0.0.0
  • Leave DHCP disabled if you don’t need it. Fixed IP address are just more predictable

Setup your virtual machine using 2 network adapters. The order doesn’t really matter, but you’ll need one NAT and one Host-Only:

  1. NAT (not much to configure)
  2. Host-Only using the network you created earlier, for example, vboxnet2

Boot your virtual machine, and configure the network interfaces in your guest OS (same order as previous point):

  1. First interface using NAT, which you can use with the internal DHCP
  2. Second interface using Host-Only will use a fixed IP address and netmask which you configured earlier for the vboxnet2 VirtualBox network. For example, 10.88.12.4.

Guest has access to outside, and host can access services running on guest. This is handy when you have a demo or develop using a service running in the virtual machine, and the virtual machine needs to access the outside world.

(If the above has been posted somewhere else: I was either blind, or it was not visible or indexed enough.)

VirtualBox juggling order of Network Adapters

It took me a while figuring out why in using VirtualBox I couldn’t get network in a machine configured with 2 network adapters. One was set up to be bridged with the host, the other is a host-only one.
The problem was that in CentOS I configured both assuming that eth0 equals to ‘Adapter 1′, and eth1 ‘Adapter 2′. This is not (always?) the case and juggling it again around made it work. Now ‘Adapter 2′ is eth0.
Now off to the real work and playing again with DRBD and MySQL, virtually!

Using RAW host disks in VirtualBox on Mac OS 10.5

The manual of VirtualBox has a section called Using a raw host hard disk from a guest which explains in detail how to use raw disks from the guest host. However, it’s a bit tricky on Mac OS as you have the disks automatically mounting. Here is a small HOWTO showing how to mount a USB stick (using Mac OS 10.5) so it can be used as the IDE Primary Master in VirtualBox.

Warning: this is probably for more advanced users. Careful what you do with mounting and umounting manually!

Start by inserting the USB stick and see if gets mounted in Finder. If you would Eject the device, it gets u(n)mounted, but you can’t use it anymore in VirtualBox. The trick here is to use diskutil (which replaces disktool) in a Terminal session. I assume the volume (e.g. USB stick) is empty.

Preparing the volume:

  1. Launch the Terminal application
  2. At the prompt do: diskutil list
  3. Look for your device in the list, probably at the end. In my case it was /dev/disk4
  4. Now lets u(n)mount it (not eject!): diskutil umountDisk /dev/disk4
  5. At this point, your Finder window might disappear. But that’s not a problem.

To make the volume available to VirtualBox we need to create a VMDK file. This is described fully in the manual, but here it is in a few steps:

  1. Launch the Terminal application
  2. Go to the location where you store your Hard Disk images, e.g. cd ~/Virtual
  3. Execute (on one line, changing filename and the actual rawdisk): VBoxManage internalcommands createrawvmdk -filename ./USBStick.vmdk -rawdisk /dev/disk4
  4. Open the VirtualBox application, and from the File Menu launch the Virtual Media Manager
  5. Click Add, and locate the file you created, e.g. USBStick, and click ‘Open’.

At this point, you should have the new Virtual Hard Disk available which you can use to make a new virtual machine. Use the disk you created as the primary hard disk just like you would do with hard disk images.

Some errors you might run into:

  • VBoxManage reports VERR_DEV_IO_ERROR: this means the volume is still in use by Mac OS. You should first unmount it using diskutil (see earlier in this post)
  • Mac OS keeps mount the volume: I didn’t find a good solution for this (i.g. using /etc/fstab is no good, and scripting was to much work). I just kept unmounting it using diskutil until VirtualBox locked it.
  • VBoxManage reports VBOX_E_IPRT_ERROR, and lots more: This is when storage format could not be read. I’m not sure why this is reported, but the RAW host disk VMDK was created successfully in my case, and can be used.

A caveat doing the above: you’ll need to remake the VMDK file if the disk is on a different device. This can for example happen when you first insert a DVD, and then the USB Stick. Maybe there is a solution for this using /etc/fstab or similar in Mac OS.

I had to do the above to make an installation on a USB stick. Ubuntu somehow failed during the installation (not sure why..); now I’m trying OpenSolaris.

Whatever you do, make sure your USB Stick is fast: I got a slow, cheap one now and it takes forever to format..