OpenWrt Overview and Quick Start

1 Introduction to OpenWrt

1.1 Historical origin

The OpenWrt project is a Linux operating system for embedded devices, commonly used on routers. As a brief introduction of embedded Linux operating system, OpenWrt is highly modular and automated, not only occupying a small space, but also having powerful network components . Many domestic home routers in China have the shadow of OpenWrt, which is based on OpenWrt for secondary development. Mainstream router firmware includes DD-WRT, Tomato, and OpenWrt, which shows its important position.

OpenWrt project began in January 2004, it adopted the first version LinkSysof the source code. After the LinkSys code is charged, it is changed to Linux kernel integration, and OpenWrt is completely modularized, and patches and drivers are continuously introduced. The main feature of OpenWrt is its high scalability, and the file system is writable. Developers do not need to completely recompile after each modification, just compile their own software packages, which speeds up the development process. In addition, OpenWrt provides an SDK, and each running software can be compiled with the SDK, and installed and uninstalled as a software package. Its main features are as follows 1

  • The source code does not contain tripartite code, only links
  • When compiling, download different source codes and patches according to different platforms, and modify the Makefile to get the latest software package
  • Use LuCIas an end user management interface, that is our common router Web management interface
  • UCI Universal Configuration Management Method
  • Use a script to call iptable to implement the firewall function, and the configuration file is saved in the UCI file
  • Use the open and extensible OPKG format to install upgrade packages

1.2 OpenWrt project directory

OpenWrt contains the following directories

table of Contents Description
./config Compilation options configuration file: contains global compilation settings, developer settings and kernel compilation settings
./include Prepare environment script, download patch script, compile Makefile and compile instructions
./package Various software packages, the package only contains Makefile, patch and configuration file
./scripts Stored some scripts, used bash, Python, perl and other scripting languages
./target Embedded platform, including specific embedded platform content
./toolchain Compiler and C library, for example, including gcc and glibc libraries
./tools General commands, auxiliary tools for generating firmware, such as patch / make / spuashfs, etc.

The OpenWrt root directory contains the following files

file Description
Config.in As you can see in include / toplevel.mk, this is the file associated with make menuconfig
Makefile The entry file for the make command in the top-level directory
feeds.conf.default Configure the address used when downloading some third-party software packages
rules.mk Define some common variables and functions used in Makefile

After compilation, the source code will have more files. We can use the source code compilation method to generate firmware that supports specific platforms, or we can directly download the official image. The official also provides binary image files that support multiple architectures and platforms. Next, we will introduce the basic methods of source code compilation and firmware generation, and official image installation. And will show OpenWrt installed in two different hardware platforms, our platform is installed OpenWrt ARM-based Raspberry Pi platform and x86-based Windows virtual machine.

2 Compile the source code

This method is suitable for customized OpenWrt. We can customize it as needed. If it is an advanced user, you can modify the source code to achieve the effect of secondary development.

2.1 Download source code

The source code can be downloaded on github or on the official website of OpenWrt

# 任选其一
git clone https://github.com/openwrt/openwrt.git
git clone https://git.openwrt.org/openwrt/openwrt.git

2.2 Installation project dependencies

Dependency package 2 required for installing and compiling source code : Environment support: gcc, binutils, bzip2, flex, python3.5 +, perl, make, find, grep, diff, unzip, gawk, getopt, subversion, libz-dev and libc headers

Debian / Ubuntu system

sudo apt install subversion g++ zlib1g-dev build-essential git python python3 python3-distutils libncurses5-dev gawk gettext \
                 unzip file libssl-dev wget libelf-dev ecj fastjar java-propose-classpath

Ubuntu 18.04 and above

sudo apt install build-essential libncursesw5-dev python unzip   

Phaedrus 30

sudo dnf install @c-development @development-tools @development-libs zlib-static elfutils-libelf-devel gawk unzip file wget python3

CentOS

# CentOS
yum install binutils bzip2 gcc gcc-c++ gawk gettext flex ncurses-devel zlib-devel zlib-static make patch unzip perl-ExtUtils-MakeMaker glibc glibc-devel glibc-static ncurses-libs sed sdcc intltool sharutils bison wget git-core openssl-devel xz

2.3 Modify the configuration file

Generate default configuration files, save all configurations, including compilation environment check, and generate cross compilation chain

cd openwrt
./scripts/feeds update -a
./scripts/feeds install -a
make menuconfig

Insert picture description here
This is some information that needs to be configured on the target device to facilitate the generation of firmware that meets the requirements. We take the Raspberry Pi as an example. The hardware information of the Raspberry Pi can be found in the Raspberry Pi Raspberry Pi Raspberry Pi version code Daquan . According to the hardware information, fill in the aboveOpenWrt Configuration

According to the hardware information, Target System selects Broadcom BCM27xx, and the following items will be automatically generated. The configuration options that need to be manually modified are actually customized on demand, not necessary

Global buildsettings

  • compile with support for patentedfunctionality(default)

Kernel Module

  • Block Devices-> kmod-block2mtd (MTD: memory technology device memory technology device)
  • Cryptographic API Modules -> kmod-crypto-ccm kmod-crypto-cmac

The following also belong to the Kernel Module sub-item

filesystem

  • kmod-fs-ext4
  • kmod-fs-ntfs
  • kmod-fs-vfat

USB Supprot

  • kmod-usb-ohci
  • kmod-usb-uhci
  • kmod-usb-storage-extras
  • kmod-usb2

Wireless Drivers

  • kmod-ath
  • kmod-ath9k-common
  • kmod-ath9k-htc
  • kmod-brcmfmac

Enter make defconfigand print the following information, indicating that there is no error
Insert picture description here

2.4 Compile

# 编译前可更新,也可以不更新
make

This creates a customized image. Of course, we can also use the official compiled image file.

3 Raspberry Pi OpenWrt image installation

OpenWrt's official website provides mirror packages suitable for various hardware. Take the Raspberry Pi as an example. The supported images are as follows. Since it is the first time to install OpenWRT using the Raspberry Pi, choose the factory image.
Insert picture description here
If you feel that the official image download speed is too slow, you can use the domestic Provided mirror address, OpenWrt mirror of China University of Science and Technology

Win32 Disk Imager is a commonly used tool to burn the image to the SD card. If you think the link speed is too slow, you can also download it by Baidu.
Insert picture description here
Practice has proved that when using Win32 Disk Imager to burn, the possible problem is that OpenWrt cannot start normally and has been stuck at the rainbow interface
Insert picture description here

Etcher is an excellent U disk image creation tool with powerful functions. Of course, the Ether software itself is also very large.
Insert picture description here
Note that when burning files, you need to format the SD card first. SDCardFormatter is recommended.

After the burning is successful, start the Raspberry Pi, and the subsequent web page configuration operation allows the Raspberry Pi to act as a router. The Raspberry Pi Lab provides more precise instructions for the Raspberry Pi to install OpenWrt to create a super router

4 Install OpenWrt on the virtual machine

Download the OpenWrt of x86 architecture from the official website or domestic mirror source . Unzip and convert the image into a format that conforms to Vmware support
Insert picture description here

$ gzip -d openwrt-19.07.2-x86-generic-combined-ext4.img.gz 
$ sudo qemu-img convert -f raw openwrt-19.07.2-x86-generic-combined-ext4.img -O vmdk openwrt-19.07.2-x86-generic-combined-ext4.vmdk

Vmware creates a new virtual machine, and the parameter configuration is reasonable, without allocating too much memory space. After creating a virtual machine, the virtual machine in the directory, we will change the name of the image 虚拟机名 + vmdkformat can be.
Insert picture description here
Open the virtual machine, OpenWrt runs successfully!
Insert picture description here
The IP address needs to be manually configured to allow the host to access OpenWrt, so that they are on the same network segment. Check the OpenWrt original network card to
Insert picture description here
configure the network so that the host machine and the physical machine are on the same network segment

ifconfig br-lan 192.168.229.100 netmask 255.255.255.0

You can now access the router happy friends
Insert picture description here
follow other settings, you can access the Raspberry Pi install OpenWrt router to create a super 3

5 Summary

As an excellent open source project, OpenWrt has laid a solid foundation for the development of home routers. This article only introduces OpenWrt and analyzes its general usage. It not only introduces the general method of source code compilation, but also introduces the official image installation method. By installing the OpenWrt image on two different platforms: Raspberry Pi and virtual machine, we Can have a perceptual understanding of OpenWrt. Our ultimate goal is to use OpenWrt to customize your own router firmware so that you can have a comprehensive understanding of the principles behind the home router. Therefore, next, we will continue to dig deeper into the secrets behind OpenWrt.


  1. https://yq.aliyun.com/articles/97610 ↩︎

  2. https://openwrt.org/docs/guide-developer/quickstart-build-images ↩︎

  3. https://www.lxx1.com/3294 ↩︎

Published 52 original articles · Like 30 · Visits 50,000+

Guess you like

Origin blog.csdn.net/song_lee/article/details/104608599