1. 程式人生 > 實用技巧 >openwrt環境的搭建(以docker ubuntu18為例)

openwrt環境的搭建(以docker ubuntu18為例)

   OpenWrt 可以被描述為一個嵌入式的 Linux 發行版,是路由器的韌體,擁有強大的網路元件和擴充套件性,常常被用於工控裝置、電話、小型機器人、智慧家居、路由器以及VOIP裝置中。 同時,它還提供了100多個已編譯好的軟體,而且數量還在不斷增加,而OpenWrt SDK 更簡化了開發軟體的工序。官網

  我這裡使用的docker搭建開發環境,在普通ubuntu18裡操作是一樣的。docker的操作在這裡https://www.cnblogs.com/lx--/p/14073513.html

1、首先下載ubuntu18.04的映象,換源,裝基本編譯環境

  注意,docker下載的ubuntu映象沒有vi編輯器,需要用echo換源,這裡以阿里云為例:

  輸入以下程式碼到命令列中換源

echo -e "deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse \n\
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse \n\
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse \n\
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse \n\
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse \n\
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse \n\
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse \n\
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse \n\
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse \n\
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse\n" > /etc/apt/sources.list

我這裡增加了一個lx的使用者,一般用普通使用者編譯openwrt,

首先passwd root 設定root 密碼,然後安裝apt install sudo 來安裝sudo命令。之後用adduser lx 增加lx使用者,passwd root 設定root 密碼,我這裡使用者密碼和root密碼均為lx。

首先安裝開發的依賴包

apt-get install g++ libncurses5-dev libssl-dev zlib1g-dev bison flex unzip autoconf gawk make gettext gcc binutils patch bzip2 libz-dev python2.7
wget git subversion

安裝完成後安裝對應的openwrt 可以選擇最新的主線,詳情參考官方wiki

 #最新的

git clone git://github.com/openwrt/openwrt.git
 #下載1806分支的程式碼
$ git clone -b openwrt-18.06 https://github.com/openwrt/openwrt.git openwrt1806

$ #下載19.07分支的程式碼

$ git clone -b openwrt-19.07 https://github.com/openwrt/openwrt.git openwrt1907

我這裡下載最新主幹

git clone git://github.com/openwrt/openwrt.git

下載完成後

openwrt原始碼目錄結構

  • tools和toolchain目錄:包含了一些通用命令, 用來生成韌體, 編譯器, 和C庫.
  • build dir/host目錄:是一個臨時目錄, 用來儲存不依賴於目標平臺的工具.
  • build dir/toolchain-目錄:用來儲存依賴於指定平臺的編譯鏈. 只是編譯檔案存放目錄無需修改.
  • build dir/target-目錄:用來儲存依賴於指定平臺的軟體包的編譯檔案, 其中包括linux核心, u-boot, packages, 只是編譯檔案存放目錄無需修改.
  • staging_dir目錄:是編譯目標的最終安裝位置, 其中包括rootfs, package, toolchain.
  • package目錄:軟體包的下載編譯規則, 在OpenWrt韌體中, 幾乎所有東西都是.ipk, 這樣就可以很方便的安裝和解除安裝.
  • target目錄:目標系統指嵌入式裝置, 針對不同的平臺有不同的特性, 針對這些特性, “target/linux”目錄下按照平臺進行目錄劃分, 裡面包括了針對標準核心的補丁, 特殊配置等.
  • bin目錄:編譯完OpenWrt的二進位制檔案生成目錄, 其中包括sdk, uImage, u-boot, dts, rootfs構建一個嵌入式系統完整的二進位制檔案.
  • config目錄:存放著整個系統的的配置檔案.
  • docs目錄:裡面不斷包含了整個宿主機的檔案原始碼的介紹, 裡面還有Makefile為目標系統生成docs.
  • include目錄:裡面包括了整個系統的編譯需要的標頭檔案, 但是是以Make進行連線的.
  • feeds目錄:擴充套件軟體包索引目錄.
  • scripts目錄:組織編譯整個OpenWrt的規則.
  • tmp目錄:編譯資料夾, 一般情況為空.
  • dl目錄:所有軟體的下載目錄, 包括u-boot, kernel.
  • logs目錄:如果編譯出錯, 可以在這裡找到編譯出錯的log

執行feeds更新

 ./scripts/feeds update -a
./scripts/feedsinstall–a

過程中會報錯,安裝apt install rsync python3 ethtool解決

OpenWRT的feeds包括:

  1. packages – 提供眾多庫, 工具等基本功能. 也是其他feed所依賴的軟體源, 因此在安裝其他feed前一定要先安裝packages!
  2. luci – OpenWrt預設的GUI(WEB管理介面).
  3. xwrt – 另一種可替換LuCI的GUI
  4. qpe – DreamBox維護的基於Qt的圖形介面, 包含Qt2, Qt4, Qtopia, OPIE, SMPlayer等眾多圖形介面.
  5. device – DreamBox維護與硬體密切相關的軟體, 如uboot, qemu等.
  6. dreambox_packages – DreamBox維護的國內常用網路工具, 如oh3c, njit8021xclient等.
  7. desktop – OpenWrt用於桌面的一些軟體包.
  8. xfce – 基於Xorg的著名輕量級桌面環境. Xfce建基在GTK+2.x之上, 它使用Xfwm作為視窗管理器.
  9. efl – 針對enlightenment.
  10. phone -針對fso, paroli.

安裝完成後,使用以下命令生成配置檔案

make defconfig 

然後make menuconfig 配置

這裡我有一塊mt7688的板子,所以這裡我Target system 選擇(MediaTek Ralink MIPS)聯發科 mips架構 ,subtarget選擇 MT76X8,如圖所示

然後make V=s -j4 進行編譯。

編譯完成後韌體會放在bin資料夾下。