WLS2Linux 子系统编译 Openwrt
上一篇文章中讲解 windows 安装 WSL2Linux 子系统的简单安装过程。如今有了 WSL 子系统可以完成很多以前不能完成的事,例如编译源代码、转换音视频、转换文档格式(pdf 转 word,word 转 md,word 转 html),定制私有 OpenWRT 路由器等。说到这个系统比较陌生,提到路由器生活中处处可见,二者联系—路由器基于openwrt 开发。
此篇文章详解 如何从源码编译 OpenWRT 文件系统。
提示:WSL2Linux 编译 Openwrt,下一篇 WSL2Linux 编译 uboot。
文章目录
- WLS2Linux 子系统编译 Openwrt
- 1. 编译OpenWRT 准备
- wsl2 install library
- 获取 openwrt 源码
- 2. 开启 OpenWRT 编译踩坑
- a) 配置编译选项
- b) 编译固件
- c) 刷机
- 总结
1. 编译OpenWRT 准备
wsl2 install library
sudo apt update
sudo apt-get install build-essential subversion libncurses5-dev zlib1g-dev gawk gcc-multilib flex git-core gettext libssl-dev unzip python -y
获取 openwrt 源码
获取代码尽量在 WSL 根目录下, 否则被win10访问权限被篡改
# openwrt source
# https://git.openwrt/openwrt/openwrt.git
# https://github/openwrt/openwrt.git
git clone https://git.openwrt/openwrt/openwrt.git
# 因网络异常导致下载失败,重新下载即可
git checkout -t origin/openwrt-21.02
# git end of EOF
2. 开启 OpenWRT 编译踩坑
a) 配置编译选项
cd ~/openwrt
./scripts/feeds update -a #更新组件
./scripts/feeds install -a #安装组件
make menuconfig -j1 V=99
看到编译配置界面,根据自己设备型号配置相关属性。
以树梅派4B 为例子构建 openwrt
# 必选配置
Target System -> Broadcom BCM27xx
Subtarget -> BCM2711 boards (64 bit)
Target Profile -> Raspberry Pi 4B
# 镜像参数
Target Images -> ext4 # ext4格式的固件可方便地调整分区大小
Target Images -> squashfs # squashfs格式的固件可恢复出厂设置
Target Images -> Kernel partition size = 64 # boot分区大小为64M
Target Images -> Root filesystem partition size = 1024 # root分区大小为1024M
# 可选工具
Base system -> block-mount # 在LuCI界面添加<挂载点>菜单
Base system -> blockd # 自动挂载设备
Base system -> wireless-tools # 无线扩展工具
Administration -> htop # 添加htop命令
Administration -> netatop # 添加netatop命令
Firmware -> Broadcom Bcm43455 # 选择需要的网卡固件,4B板载 bcm43455
# 文件系统
Kernel modules -> Filesystems -> kmod-fs-ext4
Kernel modules -> Filesystems -> kmod-fs-ntfs
Kernel modules -> Filesystems -> kmod-fs-squashfs
Kernel modules -> Filesystems -> kmod-fs-vfat
Kernel modules -> Filesystems -> kmod-fuse
Kernel modules -> Filesystems -> kmod-fs-nfs #nfs client
Kernel modules -> Filesystems -> kmod-fs-nfsd #nfs server
# 网卡支持
Kernel modules -> Network Devices -> kmod-xxx # 有线网卡支持,按需添加
Kernel modules -> USB Support -> kmod-usb-net-rtl8150 # 添加USB 有线网卡 RTL8150/2 驱动
Kernel modules -> USB Support -> kmod-usb-net-sr9700 # 添加USB2.0的有线网卡SR9700驱动
Kernel modules -> USB Support -> kmod-usb-net-asix # 添加支持亚信的有线网卡驱动
Kernel modules -> USB Support -> kmod-usb-net-asix-ax88179 # 添加USB3.0的有线网卡AX88179芯片的驱动
Kernel modules -> USB Support -> kmod-usb-serail-ch341 #ch341 串口驱动
Kernel modules -> USB Support -> kmod-usb-serail-cp210x #cp210x驱动
Kernel modules -> USB Support -> kmod-usb-serail-ftdi # ft232 ft32 驱动支持
# USB支持
Kernel modules -> USB Support -> kmod-usb-core # 启用USB支持
Kernel modules -> USB Support -> kmod-usb-hid # USB键鼠支持
Kernel modules -> USB Support -> kmod-usb-ohci # 添加OHCI支持
Kernel modules -> USB Support -> kmod-usb-uhci # 添加UHCI支持
Kernel modules -> USB Support -> kmod-usb-storage # 启用USB存储
Kernel modules -> USB Support -> kmod-usb-storage-extras
Kernel modules -> USB Support -> kmod-usb-usb2 # 开启USB2支持
Kernel modules -> USB Support -> kmod-usb-usb3 # 开启USB3支持
# LuCI设置
LuCI -> Collections -> luci # 开启luci
LuCI -> Modules -> Translations -> Chinese Simplified (zh_Hans) # 简体中文支持
LuCI -> Themes -> luci-theme-openwrt-2020 # 添加主题
# LuCI应用
LuCI -> Applications -> luci-app-aria2 # 下载工具
LuCI -> Applications -> luci-app-firewall # 防 火 墙
LuCI -> Applications -> luci-app-hd-idle # 硬盘休眠
LuCI -> Applications -> luci-app-opkg # 软 件 包
LuCI -> Applications -> luci-app-qos # 服务质量
LuCI -> Applications -> luci-app-samba4 # 网络共享
LuCI -> Applications -> luci-app-frpc # 内网穿透
LuCI -> Applications -> luci-app-shadowsocks-libev # 翻墙软件
LuCI -> Applications -> luci-app-upnp # UPnP服务
LuCI -> Applications -> luci-app-wol # 网络唤醒
# 其他工具
Network -> Download Manager -> ariang # Aria2管理页面
Network -> File Transfer -> Aria2 Configuration -> *** # 选择Aria2支持的功能
Network -> File Transfer -> curl # 添加curl命令
Network -> File Transfer -> wget # 添加wget命令
Utilities -> Compression -> bsdtar # tar打包工具
Utilities -> Compression -> gzip # GZ 压缩套件
Utilities -> Compression -> xz-utils # XZ 压缩套件
Utilities -> Compression -> unzip # zip解压工具
Utilities -> Compression -> zip # zip压缩工具
Utilities -> Disc -> fdisk # 磁盘分区工具
Utilities -> Disc -> lsblk # 磁盘查看工具
Utilities -> Editors -> vim # vim编辑器
Utilities -> Filesystem -> ntfs-3g # NTFS读写支持
Utilities -> Filesystem -> resize2fs # 分区大小调整
Utilities -> Terminal -> screen # 添加screen
Utilities -> pciutils # 添加lspci命令
Utilities -> usbutils # 添加lsusb命令
# IPv6支持
Global build settings -> Enable IPv6 support in packages # 启用IPv6项
Network -> odhcp6c # IPv6客户端
Network -> odhcpd-ipv6only # IPv6服务端
Network -> Firewall -> ip6tables # IPv6防火墙
LuCI -> Protocols -> luci-proto-ipv6 # WebUI支持
文件夹 files/etc/config 创建 network 与 wireless 网卡配置文件。
# network config
config wifi-device 'radio0'
option type 'mac80211'
option channel '36'
option hwmode '11a'
option path 'platform/soc/fe300000.mmcnr/mmc_host/mmc1/mmc1:0001/mmc1:00
01:1'
option htmode 'VHT40'
config wifi-iface 'default_radio0'
option device 'radio0'
option mode 'ap'
option encryption 'none'
option ssid 'RPI4'
option network 'lan'
# wireless config
config wifi-device 'radio0'
option type 'mac80211'
option channel '36'
option hwmode '11a'
option path 'platform/soc/fe300000.mmcnr/mmc_host/mmc1/mmc1:0001/mmc1:00
01:1'
option htmode 'VHT40'
config wifi-iface 'default_radio0'
option device 'radio0'
option mode 'ap'
option encryption 'none'
option ssid 'RPI4'
option network 'lan'
b) 编译固件
开机相关代码包下载
make download -j4 V=s
火力全开编译
make -j12 V=99
编译完成生成固件几种固件
ls bin/targets/bcm27xx/bcm2711/openwrt-bcm27xx-bcm2711-rpi-4*
bin/targets/bcm27xx/bcm2711/openwrt-bcm27xx-bcm2711-rpi-4-ext4-factory.img.gz
bin/targets/bcm27xx/bcm2711/openwrt-bcm27xx-bcm2711-rpi-4-ext4-sysupgrade.img.gz
bin/targets/bcm27xx/bcm2711/openwrt-bcm27xx-bcm2711-rpi-4-squashfs-factory.img.gz
bin/targets/bcm27xx/bcm2711/openwrt-bcm27xx-bcm2711-rpi-4-squashfs-sysupgrade.img.gz
#切换系统环境变量 再执行编译命令
source /etc/environment
编译过程出错误
- 使用 root 权限编译错误提示
you should not run configure as root (set FORCE_UNSAFE_CONFIGURE=1 in environment to bypass this check
方法一:
强制在不安全模式下编译
make FORCE_UNSAFE_CONFIGURE=1 -j1 V=s
方法二:
保存刚在配置文件,改用普通用户重新下载代码 初始化刚刚的配置文件继续编译
- libsoncpp-dev missing
fatal error: json/json.h: No such file or directory
安装 相关库既可以解决
sudo apt-get install libjsoncpp-dev
sudo ln -s /usr/include/jsoncpp/json/ /usr/include/json
如需重新编译可选择如下几种方式:
make clean -j4 #清除 bin
make dirclean -j4 #清除 toolchain bin
make distclean -4 #清除 config feed package 等文件
c) 刷机
编译完成 生成固件路径
ls `~/openwrt/bin/targets/bcm27xx/bcm2711/`
bin/targets/bcm27xx/bcm2711/openwrt-bcm27xx-bcm2711-rpi-4-ext4-factory.img.gz
bin/targets/bcm27xx/bcm2711/openwrt-bcm27xx-bcm2711-rpi-4-ext4-sysupgrade.img.gz
bin/targets/bcm27xx/bcm2711/openwrt-bcm27xx-bcm2711-rpi-4-squashfs-factory.img.gz
bin/targets/bcm27xx/bcm2711/openwrt-bcm27xx-bcm2711-rpi-4-squashfs-sysupgrade.img.gz
选 ext4 格式镜像文件,制作成 sd 卡启动;上TF卡开机测试
login IP: 192.168.1.1
login user: root
login passwd: password
Tips:
想单独编译某个插件,然后手动安装;编译后生成文件路径
ls ~/openwrt/bin/packages/aarch64_cortex-a72/packages/
Packages libexpat_2.2.10-1_aarch64_cortex-a72.ipk
Packages.gz libgnutls_3.7.1-1_aarch64_cortex-a72.ipk
Packages.manifest libtasn1_4.16.0-1_aarch64_cortex-a72.ipk
Packages.sig libtirpc_1.3.2-1_aarch64_cortex-a72.ipk
aria2_1.35.0-4_aarch64_cortex-a72.ipk liburing_0.7-1_aarch64_cortex-a72.ipk
attr_2.4.48-2_aarch64_cortex-a72.ipk netatop_3.1-1_aarch64_cortex-a72.ipk
......
总结
人生贵在生生不息,今天分享折腾路由器方法你 get 到没!