Tuesday 18 April 2017

Testing OpenWRT in VirtualBox


I read on many different forums about the advantages of OpenWRT and really wanted to find out for myself but I didn't have a wireless router. I started to ponder on the possibility of using Oracle VirtualBox to test OpenWRT thus this article. I actually came upon a similar project which was a bit outdated so I decided to document this. 

If you are considering to test OpenWRT without a router or you don’t want to flash the router firmware over and over again. Here is a step by step article on running OpenWRT in a VirtualBox environment. OpenWRT in a virtualbox is an image file run in VirtualBox. At the time of writing this article, the VirtualBox version was Version 5.0.24 r108355 (the current release is 5.1.18, download available here) & OpenWRT image version openwrt-x86-generic-combined-ext4.img (download available here

Download links:

What is OpenWRT?
OpenWrt ​is a highly extensible ​GNU/​Linux ​distribution for embedded devices ​(typically wireless routers). Unlike many other distributions for these routers, OpenWrt ​is built from the ground up to be a full-featured, easily modifiable operating system for your router. In practice, this means that you can have all the features you need with none of the bloat, powered by a Linux kernel ​that's more recent than most other distributions. Read more here.

That aside, the real challenge begins when you don't have an actual router to test OpenWRT, you may decide to try it on VirtualBox which is exactly what I did.

Convert img to native VirtualBox format
The first hurdle is to convert the image file to a native virtualbox format. The file you downloaded above is not an installer. A file with an extension .img is a disk image, which is quite different. An installer boots on its own, and you then point it at a location to be installed. A disk image requires some other form of software that's able to write it onto the target disk. With OpenWRT you usually use the old router software, telling it where the new firmware is then it downloads and unpacks it. 

With OpenWRT, the command you need is:
 
VBoxManage convertfromraw --format VDI openwrt-15.05-x86-generic-combined-ext4.img openwrt.vdi


Step 1 
Unzip the raw image to VirtualBox folder (C:\Program Files\Oracle\VirtualBox)
  
Step 2
Open Command Prompt and enter C:\Program Files\Oracle\VirtualBox (This is the location where virtualbox install). Type cd (change directory) before you type the path.
Step 3
Enter VBoxManage convertfromraw --format VDI openwrt-15.05-x86-generic-combined-ext4.img openwrt.vdi 

Note: If you do get an error message that tells you virtualbox cannot convert access is denied. Ensure to apply full control to you as the administrator to the virtualbox folder.
Once you have done the above, you should now have a new vdi file named openwrt.vdi
You can now move this VDI file to your virtualbox machine folder or where you store your ISO files.

If you don't want to scratch your head to converting the vdi file. I provided a converted vdi which you can download and use. Just ensure to configure the correct network settings on the openwrt when run in vbox.

Install VirtualBox
  • Run VirtualBox 5.1.18.exe
  • Follow on screen instructions until complete installation (always use default settings)
Setup VirtualBox
  • Run VirtualBox
  • VirtualBox main screen should appear as shown in figure below.
  • Click New to create a new Virtual Machine.
  • Choose a name for the machine, it can be any name. Here I use openwrt
  • Choose Linux for the Operating System
  • Choose Linux 2.6 / 3.x / 4.x (32-bit) for Version
  • Click Next to continue as shown in figure below

  • Select the amount of memory, or keep the default setting. I changed it to 128MB since I will use it for testing only so I'm just giving it a low 128MB.
  • Click Next to continue

  • Choose Use an existing virtual hard disk file since you want to use the OpenWRT image file as the new virtual disk
  • Click the file icon to select the OpenWRT image file as shown in figure below
  • Finally click Create button to finish



Starting OpenWRT in VirtualBox

Now you are ready to start OpenWRT in VirtualBox.

Wait for it to load


Once you get to this screen, hit Enter and you should be able to see this. OpenWRT is running as shown in figure below.


Setting up Network
I configured two network adapters for the virtual machine. The first adapter use "NAT" & the second adapter use "Bridge".

The first adapter allows you to download additional OpenWrt software packages via the internet connection of the host system (that is your laptop or desktop). While the second network adapter is connected to the Ethernet adapter of the host system, which itself could then be connected to the network switch.

Modify Network Configuration File
The /etc/config/network configuration must match with the above network system. Use vim to edit the file.

Type  vim /etc/config/network
This is so you can open the network file and edit the ip address inside.
config interface loopback
        option ifname   lo
        option proto    static
        option ipaddr   127.0.0.1
        option netmask  255.0.0.0
config interface wan
        option ifname   eth0
        option proto    dhcp
config interface lan
        option ifname   eth1
        option proto    static
        option ipaddr   192.168.1.11
        option netmask  255.255.255.0

Above example set the OpenWRT IP address to 192.168.1.10.
Navigate to the end of the line and press i (this will allow you to insert before the cursor). Type in the ip address as 192.168.1.10
To save and exit, press Esc key, press : (colon). The cursor should reappear at the lower left corner of the screen beside a colon prompt. Enter the following command (type :x and press Enter key):

To verify use cat network  or cat /etc/config/network


Save the file and reboot OpenWRT by just typing reboot.

Later...I am tinkering on extending this work to look at testing the openwrt functionality in connecting to an external network. I have done similar work with pfsense but have not documented it here. PFSENSE is much easier than openWRT (at least that is what I think.).

Additional resources and references
  • https://wiki.openwrt.org/doc/howto/virtualbox \
  • http://ediy.com.my/blog/item/31-openwrt-in-virtualbox 
😼😼😼

1 comment: