1. 安装virtualbox、ubuntu虚拟机、交叉编译环境
1.1 virtualbox
官网下载7.0版本安装即可
链接:https://pan.baidu.com/s/1SV1JTYmnaWU6quUNLXuw1Q
提取码:igx2
1.2 ubuntu虚拟机
下载最新ubuntu最新镜像:
Thank you for downloading Ubuntu Desktop | Ubuntu
2. 安装qemu、交叉编译工具
2.1 交叉编译器
因为我们是在x86平台上进行编译,而运行的平台是ARM系统,这2个平台的指令集不一样,所以需要交叉编译得到ARM系统上可以执行的程序。
进入ubuntu虚拟机
- 安装交叉编译器:
sudo apt-get install gcc-arm-linux-gnueabi
- 验证安装结果:
dpkg -l gcc-arm-linux-gnueabi
注意: 通过apt-get安装,可能安装的版本过低(u-boot编译依赖可能6以上的版本)
2.2 安装qemu (ubuntu环境)
- 安装
sudo apt-get install qemu qemu-system-arm
- 查看qemu支持的板子
qemu-system-aarch64 -M help
2.3 安装qemu(windows环境)
官网下载最新qemu安装包
安装好后,命令行进入到安装目录。
- 产看qemu支持的板子
./qemu-system-aarch64.exe -M help
3. 编译openwrt
- Modernized set for Ubuntu 22.04 for OpenWrt master, 22.03 and 21.02 branches that do not need python2.7 any more. Python3 is installed by default in Ubuntu22.04.
sudo apt update
sudo apt install build-essential clang flex g++ gawk gcc-multilib gettext \
git libncurses5-dev libssl-dev python3-distutils rsync unzip zlib1g-dev
- 下载和编译
git clone https://github.com/openwrt/openwrt.git
cd openwrt
# Select a specific code revision
git branch -a
git tag
git checkout v22.03.2
# Update the feeds
./scripts/feeds update -a
./scripts/feeds install -a
make menuconfig
make -j 12 download V=99
make -j 12 V=99
#编译好的镜像在
openwrt/bin/targets/armvirt/64/ 目录
备注:make menuconfig的几个配置:
target->system->qemu-arm-virtual
->subsystem->a53
kernel-modules->wireless-driver->kmod-mac802.11-hwsim
luci->collections->Luci
4. 运行
sudo qemu-system-aarch64 -m 1024 -smp 4 -cpu cortex-a53 -M virt -nographic \
-kernel openwrt/bin/targets/armvirt/64/openwrt-armvirt-64-Image-initramfs
结果:
5. qemu虚拟机与host通信网络配置
- create tap interface in host which will be connected to OpenWrt LAN NIC
clourney@ubuntu22:~/work$ sudo ip tuntap add mode tap tap0
clourney@ubuntu22:~/work$ sudo ip link set dev tap0 up
clourney@ubuntu22:~/work$ sudo ip addr add 192.168.1.101/24 dev tap0
配置结果:
- start qemu with parameter: -device, -netdev
sudo qemu-system-aarch64 \
-m 256 \
-smp 4 \
-cpu cortex-a53 \
-M virt -nographic \
-kernel openwrt/bin/targets/armvirt/64/openwrt-armvirt-64-Image-initramfs \
-device virtio-net-pci,netdev=lan \
-netdev tap,id=lan,ifname=tap0,script=no,downscript=no \
-device virtio-net-pci,netdev=wan \
-netdev user,id=wan,hostfwd=tcp::2222-:22
备注:wan口设置待定
- 配置好后,从host ssh到qemu虚拟机
clourney@ubuntu22:~/work$ ssh root@192.168.1.1
- 设置多个tap口
# 再添加个tap1 interface
clourney@ubuntu22:~/work$ sudo ip tuntap add mode tap tap1
clourney@ubuntu22:~/work$ sudo ip link set dev tap1 up
clourney@ubuntu22:~/work$ sudo ip addr add 192.168.2.101/24 dev tap1
sudo qemu-system-aarch64 \
-m 256 \
-smp 4 \
-cpu cortex-a53 \
-M virt -nographic \
-kernel openwrt/bin/targets/armvirt/64/openwrt-armvirt-64-Image-initramfs \
-device virtio-net-pci,netdev=lan \
-netdev tap,id=lan,ifname=tap0,script=no,downscript=no \
-device virtio-net-pci,netdev=wan \
-netdev tap,id=wan,ifname=tap1,script=no,downscript=no
# 启动后手动给eth1配上tap1同网段地址即可
ip a add 192.168.2.1/24 dev eth1
参考来源:
[OpenWrt Wiki] OpenWrt in QEMU
Quick Start — QEMU 7.1.50 documentation