1. 安裝virtualbox、ubuntu虛擬機缆巧、交叉編譯環(huán)境
1.1 virtualbox
官網(wǎng)下載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系統(tǒng),這2個平臺的指令集不一樣甚牲,所以需要交叉編譯得到ARM系統(tǒng)上可以執(zhí)行的程序。
進入ubuntu虛擬機
- 安裝交叉編譯器:
sudo apt-get install gcc-arm-linux-gnueabi
- 驗證安裝結(jié)果:
dpkg -l gcc-arm-linux-gnueabi
示例.png
注意: 通過apt-get安裝蝶柿,可能安裝的版本過低(u-boot編譯依賴可能6以上的版本)
2.2 安裝qemu (ubuntu環(huán)境)
- 安裝
sudo apt-get install qemu qemu-system-arm
- 查看qemu支持的板子
qemu-system-aarch64 -M help
image.png
2.3 安裝qemu(windows環(huán)境)
官網(wǎng)下載最新qemu安裝包
安裝好后丈钙,命令行進入到安裝目錄。
- 產(chǎn)看qemu支持的板子
./qemu-system-aarch64.exe -M help
image.png
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
結(jié)果:
image.png
5. qemu虛擬機與host通信網(wǎng)絡(luò)配置
- 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
配置結(jié)果:
image.png
- 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口設(shè)置待定
- 配置好后交汤,從host ssh到qemu虛擬機
clourney@ubuntu22:~/work$ ssh root@192.168.1.1
image.png
- 設(shè)置多個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同網(wǎng)段地址即可
ip a add 192.168.2.1/24 dev eth1
參考來源:
[OpenWrt Wiki] OpenWrt in QEMU
Quick Start — QEMU 7.1.50 documentation