Running and using OpenWrt in VirtualBox
In this article, I will talk about how to run OpenWrt in an emulator and configure it to work on the network, as well as touch on the use of vi, ssh and scp clients. The main reason for all this action, I think, is the ability to touch the firmware with your hands without fear of being left without the Internet, in case of problems with the router. This article is intended for people who are not familiar with Linux.
Wikipedia suggests that OpenWrt is Linux-based firmware for home routers. In fact, this is a whole family of firmware. OpenWrt, along with DD-Wrt and Gargoyle, are available for a wide variety of routers on a variety of hardware platforms. Repository available at downloads.openwrt.org... In addition to the main branch (which has several implementations within the platform) called attitude_adjustment, the backfire and kamikaze versions are common.Installing programs
First of all, install the VirtualBox emulator if you don't have one. To be able to execute commands outside the console of the emulator window, you need an SSH client. For example Putty . In this case, the clipboard of the home system will become available to you. For convenient access to the file system of the "router" you need a graphical SCP client. For example, WinSCP for Windows . For Linux, the built-in file manager will suffice. For example, Krusader, Dolphin, Nautilus.
Firmware selection
To emulate, you need firmware for the x86 platform, preferably with the Ext2 or Ext4 file system.
Although the official wiki advises putting the attitude_adjustment version , I had problems with it: it refused to start, reporting a kernel panic. So it's easier to stop at one of the other two. Having chosen the firmware, look in the options for downloading the ready .vdi file.
Link for backfire version 10.03.1: http://downloads.openwrt.org/backfire/10.03.1/x86_generic/openwrt-x86-generic-combined-ext2.vdi .
If the firmware you are interested in is available only as a raw image (img.gz, image.gz, bin), you will need to use the included VirtualBox converter. For Windows, save the following text file in the VirtualBox folder (you may need administrator rights) and give it the .bat or .cmd extension:
"%~dp0VBoxManage.exe" convertfromraw --format VDI %1 "%~dpn1.vdi"
pause
Then unpack the downloaded firmware image and drag it to this script. The resulting .vdi image can be connected to a virtual machine.
Setting up VirtualBox
Create a new virtual machine. Type - Linux, version - Linux 2.6. Refuse to connect the disk at this stage - this will be done later.
Open the properties of the new virtual machine (the "Configure" button) and go to the Network section. Select Bridged instead of NAT to allow access to the virtual machine from other devices on the network. If you don't have a home network (at least a router), leave NAT and add a second "Virtual Host Adapter".
Then, in the COM Ports section, be sure to check the box to enable port emulation. Otherwise, OpenWrt will fail to boot.
Finally, in the Media section, add the OpenWrt firmware image to the IDE controller as a hard drive. If devices are connected to the SATA controller, remove them.
Click OK to save the settings.
Network configuration
Start the virtual machine. When the download is complete, press Enter. The console will become available.
First, set an administrator password by entering passwd and a new password when prompted.
By default, OpenWrt creates its own subnet, ignoring the existing ones and receives the address 192.168.1.1 in it. You can try to connect to the virtual machine at this address by setting your computer (network card or virtual host adapter, depending on the VirtualBox settings made) a static address in the 192.168.1. * Subnet. But the more convenient option is in which the emulated "router" connects to the existing network, gaining access to the Internet and to devices on your network. Configuration files can be edited with the built-in vi text editor. Type vi / etc / config / networkto change network settings. Cursor control in vi is carried out by arrows, to delete a line, you can press dd , go to edit mode by pressing i , return to command mode by Esc . Reduce this file to the following form (change option proto from static to dhcp ):
# Copyright (C) 2006 OpenWrt.org
config interface loopback
ifname lo option
option proto static
option ipaddr 127.0.0.1
option netmask 255.0.0.0
config interface lan
option ifname eth0
option proto dhcp
After that, by going to Esc in command mode, type : wq and Enter to save and exit the editor. To exit without saving, type the command : q! ... These settings mean that the emulated "router" will try to get an available ip address. If you do not have a real router and the ability to get more than one address, select the "Virtual host adapter" mode in the VirtualBox network settings.
Restart the virtual machine or enter /etc/init.d/network restart to apply the new settings .
After booting, look at the address of the virtual machine by typing ifconfig in the inet addr field of eth0:
root@OpenWrt:~# ifconfig
eth0 Link encap:Ethernet HWaddr 08:00:27:13:00:42
inet addr:192.168.1.23 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:91 errors:0 dropped:0 overruns:0 frame:0
TX packets:65 errors:0 dropped:0 overruns:0 carrier:0
collisions: 0 txqueuelen: 1000
RX bytes:10016 (9.7 KiB) TX bytes:7749 (7.5 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:48 errors:0 dropped:0 overruns:0 frame:0
TX packets:48 errors:0 dropped:0 overruns:0 carrier:0
collisions: 0 txqueuelen: 0
RX bytes:3888 (3.7 KiB) TX bytes:3888 (3.7 KiB)
root@OpenWrt:~#
Congratulations, OpenWrt is up and running on the web. Keep in mind: this address (192.168.1.23 in my case) is temporary and may change after restarting the emulator or after some time (several hours). Launch Putty and try to connect to the virtual machine at this address. Login to connect - root, you set the password at the first boot.
File system access
Launch WinSCP (or equivalent) and connect to the virtual machine.
You will see a standard two-pane file manager. Now you can edit configuration files in a convenient graphical mode. It is also possible to copy files to the "router" and vice versa. Try going on the right side to root ( / ) and then to / etc / config / and run network .
Make changes if desired and save the result.
Installing packages
First, update the list of components available in the repository by typing opkg update , then try installing , for example, openvpn by typing opkg install openvpn . You can check the presence of a component and specify its name with the command opkg list PartName . You can also install packages without Internet access. Using WinSCP, rewrite the packages to a writable directory like / tmp / or to a USB flash drive if available and enter the command opkg install package_path. You can use the Tab key to avoid typing the full title. For example, download the Russian language pack for LuCI at http://downloads.openwrt.org/backfire/10.03.1/x86_generic/packages/luci-i18n-russian_0.10.0-1_x86.ipk, rewrite it with WinSCP in / tmp / and in emulator console, terminal or Putty (see WinSCP toolbar buttons after Synchronize) enter opkg install /tmp/luci-i18n-russian_0.10.0-1_x86.ipk
root@OpenWrt:~# opkg install /tmp/luci-i18n-russian_0.10.0-1_x86.ipk
Installing luci-i18n-russian (0.10.0-1) to root...
Configuring luci-i18n-russian.
root@OpenWrt:~#
Keep in mind that the architecture of your firmware and the package you install must be the same.
Web interface
Most versions of OpenWrt natively include the LuCI web shell. LuCI stands for Lua Configuration Interface. Navigate to the network address of your "router" in your browser, enter the password you entered at the beginning, and you can experiment with LuCI.
source: https://habr.com/ru/post/195592/