Blog

Using a VirtualBox VM to operate a network device for your Mac

VirtualBox running TinyCoreLinux, operating the RTL8187 wifi adapterI recently wrote about our long-range USB WiFi antenna over on Technomadics, which is the nifty little device that we use to connect to the Internet while we’re travelling. It’s great, but — and it’s a kinda large but — the OS X driver (it’s an RTL8187) is just mind-blowingly appalling. It crashes very frequently, I can’t sleep my Mac at night without unplugging the device, and when I plug the device back in in the morning, nine times out of ten I need to reboot. Plus, I have to run the whole system in 32-bit mode because the driver is ancient. Realtek refuse to assist, so that’s that.

Three thousand reboots later, I decided enough was enough, and set about seeing if I could run a virtual machine with a small Linux installation which operated the card on behalf of my Mac, and shared the resulting Internet connection. It was a long learning curve, but I managed to get it all working quite satisfactorily, along with an Internet Sharing setup on my mac to share the connection onwards to other machines on the local network.

What’s more, the drivers I’m using under Linux — actually, they’re Windows XP drivers, running under the fantastic ndiswrapper utility — are brilliant, and I get much better signal strength, stability and throughput, to my great surprise.

If you’re in a similar situation, and either have a crappy driver to contend with, or no driver at all, or even just want a more solid firewall between you and the dangerous open WiFi world, here’re some instructions on how to get it set up, along with a virtual appliance I put together to make it all happen.

A warning in advance: This is Linux, so it aint for the faint of heart. If you’re not familiar with Linux, by all means give it a go, but be forewarned that if everything doesn’t work for you out-of-the-box, it is a steep learning curve.

The virtual appliance is set up by default to run the RTL8187 device using ndiswrapper, but the standard wireless drivers package is installed, and should automatically work with another adapter. If not, let me know and I might be able to assist.

Step 1: Download

First, download and install VirtualBox, the fantastic and totally free VM software. You’ll also want to grab the “VirtualBox 4.1.2 Oracle VM VirtualBox Extension Pack”, which brings USB 2.0 support.

Next, download the Microcore Router virtual appliance that I put together (it’s 45Mb). This is set up for use with Microcore Linux, and contains:

  • Wireless device drivers
  • Ndiswrapper
  • The Windows XP version of the RTL8187 device driver
  • iptables, with a script to set up IP masquerading
  • dnsmasq, set up to provide a DHCP address to your Mac host
  • Wireless-tools (iwconfig, etc), and WICD

Finally, download the Microcore live CD image (the virtual appliance was set up with Microcore 3.8.4 – if you wish to use newer versions, you’ll also need to update the extensions, but you’re on your own there.).

Step 2: Installation and Setup

Install VirtualBox, then double-click on the extension pack to install that. Then, double-click on the Microcore-Router.ova virtual appliance to import it.

Importing the virtual appliance

Next, we need to create a virtual network interface to link the guest virtual machine, and your Mac. Open VirtualBox preferences, and click the “Network” tab. Click the “+” button to create a new host-only network, and enter the settings below (192.168.22.2, netmask 255.255.255.0). Click the “DHCP Server” tab and make sure it’s disabled, then click OK. This virtual interface is already selected for use within the VM’s settings.

Setting up the host only network interface

Now attach the Microcore ISO to the virtual appliance by clicking the “Storage” section of the VirtualBox VM settings, then selecting the CD item underneath “IDE Controller”, selecting “Choose a virtual CD/DVD disk file…”, and choosing the Microcore ISO. This lets us boot the VM into Microcore.

Attaching the Microcore iso to the virtual appliance

Microcore ISO attached to VM

Finally, if you’re using a USB network device, plug it in now. Attach the network device to the VM by opening the “Ports” (or “USB”) settings, then clicking the USB “+” symbol and selecting the device.

Note: I’ve already added the RTL8187 device to the VM setup, so if you’ve got one too, you shouldn’t have to do anything here.

USB Ports screen

Step 3: Boot VM

Now, you’re ready to boot. Close the settings window and hit “Start”. You should see the Microcore boot screen appear, with a prompt for boot options.

Microcore boot screen

Type the following in, to make Microcore boot with the setup contained within the virtual appliance:

mc opt=sda1 restore=sda1

If you’re using an RTL8187 too, then you’ll want to blacklist the buggy Linux device driver here, so that ndiswrapper can run the device with the improved Windows drivers:

mc opt=sda1 restore=sda1 blacklist=rtl8187

Hit enter to start booting, and within a few seconds you should be faced with an empty screen and a prompt.

With a bit of luck, your network device has been detected and started up; type wicd-curses and hit enter to launch the WiFi control panel.

wicd-curses WiFi control panel

If you don’t see any networks here, and you expect to, then it could be that your network device hasn’t been picked up. Otherwise, breathe a sigh of relief and press on.

Step 4: Configure the Mac

Now that we’re booted and running the VM, we need to set up your Mac to access the internet via the VM’s connection.

Minimise the VM window — we’re done for now — and open up a Terminal window on your Mac. Ping 192.168.22.1, which is the VM’s configured address. Do you see ping replies? Then you’re almost there.

The final step is to make your Mac use the guest’s DHCP server, to obtain the correct settings. Type the following in Terminal to make it happen:

sudo ipconfig set vboxnet0 DHCP

To make sure it’s done the right thing, type ifconfig vboxnet0. You should see something like:

vboxnet0: flags=8843 mtu 1500
    ether 0a:00:27:00:00:00 
    inet 192.168.22.2 netmask 0xffffff00 broadcast 192.168.22.255
    inet 192.168.22.72 netmask 0xffffff00 broadcast 192.168.22.255

That is, the default address we specified when setting up the VirtualBox host adapter, and a new alias that has an address assigned by the DHCP server running within the VM. If you see this, then you should be good to go: Try opening up a web browser or pinging google.com.

If it’s working, then congratulations! Mission accomplished.

Optional Step 5: Internet Sharing

For bonus points, you can set up your Mac so that other computers on your network can share the connection. It’s not as simple as just starting up Internet Sharing from System Preferences, however, because System Preferences doesn’t know about the virtual network device we’re using.

Instead, we have to do it the old-fashioned manual way: Set up IP forwarding, NAT, and set up a DHCP server. I’ve created a service (loosely based upon Schley Stauffer Andrew Kutz’s akin service) that monitors the VM’s network state, and automatically does all this, sharing the VM’s connection via Ethernet and Wi-Fi.

Download the service resources here:

vmboxnat.zip

Extract, then go to the folder in Terminal and type:

sudo ./install

This will install to /usr/local/vmboxnat, and register the service with launchctl. This is a script which runs in the background and starts up internet sharing when the VM is detected, then stops it when the VM goes away.

If you open up Console now, you should see something like:

…au.id.tyson.michael.vmboxnat[13099]: Starting vmboxnat
…au.id.tyson.michael.vmboxnat[13099]: assigned DHCP address
…au.id.tyson.michael.vmboxnat[13099]: created alias interfaces
…au.id.tyson.michael.vmboxnat[13099]: enabled ip forwarding
…au.id.tyson.michael.vmboxnat[13099]: enabled natd
…au.id.tyson.michael.vmboxnat[13099]: established ipfw rules
…au.id.tyson.michael.vmboxnat[13099]: enabled bootpd

That means you’re all set.

Note that you’ll need to manually create an ad-hoc WiFi network, as we can’t automatically create a network the way the official Internet Sharing system does. Once you’ve created it, though, other hosts accessing that network should automatically receive an address via DHCP, and be good to go.

Tagged , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.

67 Comments

  1. David
    Posted December 5, 2012 at 4:24 pm | Permalink

    Thanks for this great guide! I’m almost in goal but have a problem. Internet is working from Microcore, but no response if I ping 192.168.22.2 or any other adress from the mac side of things. I’m running OS X 10.8.2 and VirtualBox 4.2.4

    Here is a screenshot. http://image.bayimg.com/3ac1ecbcf5db4717c9e5ba5190898c7fb81a201b.jpg

    As you can see I now have two interfaces for vbox0. One looks good but I don’t like the 169. address. Can this be the problem or do you have another idea?

    Blue Skies, /David

  2. Disco
    Posted August 6, 2013 at 10:50 am | Permalink

    Hey if i did this would i be able to use xiaopan or similar software with the wificard?

  3. rafael leal
    Posted August 23, 2013 at 2:54 am | Permalink

    I managed to make it work here but the internet rate fell considerably, you can do this using the vmwarefusion?

  4. cris
    Posted September 30, 2013 at 1:27 am | Permalink

    the link for downloading the Microcore Router virtual appliance is not working anymore!