编辑
2024-12-17
记录知识
0
请注意,本文编写于 191 天前,最后修改于 59 天前,其中某些信息可能已经过时。

目录

一:编译qemu
二:编译linux内核
三.rootfs镜像
四.启动系统
扩展:
参考链接:

之前有借助uml进行启动linux系统,借助uml的基础上,这里使用qemu来实现跨架构的虚拟系统启动,主要包括如下步骤

  1. 编译qemu-system-aarch64

  2. 编译linux内核

  3. 下载aarch64版本的rootfs镜像

  4. 利用qemu-system-aarch64启动内核和系统

一:编译qemu

apt install gcc-10-aarch64-linux-gnu wget https://download.qemu.org/qemu-7.0.0-rc0.tar.bz2 ./configure –target-list=aarch64-softmmu make -j8 && make install apt install gcc-10-aarch64-linux-gnu ln -s /usr/bin/aarch64-linux-gnu-gcc-10 /usr/bin/aarch64-linux-gnu-gcc export ARCH=arm64 export CROSS_COMPILE=aarch64-linux-gnu-

安装之后,可以查看qemu工具版本如下

qemu-system-aarch64 --version QEMU emulator version 6.2.90 Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers

二:编译linux内核

linux源码仍借用 linux-source-5.13.0 这个安装包

apt install linux-source-5.13.0

实际目录在 /usr/src/linux-source-5.13.0/linux-source-5.13.0/

编译命令如下:

ARCH=arm64 CROSS_COPILE=aarch64-linux-gnu- make defconfig ARCH=arm64 CROSS_COPILE=aarch64-linux-gnu- make -j8

编译完成后会生成内核文件为

file arch/arm64/boot/Image.gz arch/arm64/boot/Image.gz: gzip compressed data, max compression, from Unix, original size modulo 2^32 33122816

三.rootfs镜像

wget https://cdimage.ubuntu.com/ubuntu-base/focal/daily/current/focal-base-arm64.tar.gz

将这个tar做成rootfs.img 的ext4格式镜像即可

四.启动系统

直接通过内核启动rootfs镜像如下

qemu-system-aarch64 -M virt -cpu cortex-a72 -kernel Image.gz -append "root=/dev/vda" -hda rootfs.img -nographic

因为是-nographic,所以系统只会串口输出。

下载的rootfs.img默认没有systemd启动,可以chroot进去进行安装对应的包,并设置系统为字符模式,并确保getty能够启动ttyAMA0

chroot rootfs/ apt update systemctl set-default multi-user.target ln -s /lib/systemd/system/getty@.service /etc/systemd/system/getty.target.wants/getty@ttyAMA0.service

到这里,系统已经能够正常启动了,但是系统还不能使用网络,这里仍借助nettap的方法,创建一个tap3的虚拟网卡,然后和虚拟机进行通信即可

主机上创建tap3网卡如下

ip tuntap add tap3 mode tap group tf ip addr add 192.168.0.100/24 dev tap3 ip link set dev tap3 up echo 1 > /proc/sys/net/ipv4/conf/tap3/proxy_arp iptables -I FORWARD -i tap3 -j ACCEPT iptables -I FORWARD -o tap3 -j ACCEPT

qemu启动命令如下

qemu-system-aarch64 -M virt -cpu cortex-a72 -smp 4 -m 4096 -kernel Image.gz -nographic -append "console=ttyAMA0 root=/dev/vda rw rootfstype=ext4 ignore_loglevel" -drive if=none,file=rootfs.img,id=hd0 -device virtio-blk-device,drive=hd0 -device virtio-blk-device,drive=hd0 -netdev tap,id=n1,ifname=tap3,script=no -device e1000,netdev=n1,mac=02:27:d1:32:44:7f

-netdev tap,id=n1,ifname=tap3,script=no

指明主机网卡使用名字为tap3的tap类型网卡,并禁用系统的ifup脚本。id为n1

-device e1000,netdev=n1,mac=02:27:d1:32:44:7f

指明虚拟机使用e1000网卡驱动,设备为id=n1的网卡,mac地址为主机tap3的mac地址

ifconfig tap3 | grep ether ether 02:27:d1:32:44:7f txqueuelen 1000 (以太网)

虚拟机内网络配置如下

/etc/network/interfaces auto enp0s1 allow-hotplug enp0s1 iface enp0s1 inet static address 192.168.0.200 netmask 255.255.255.0 gateway 192.168.0.1

然后就可以ssh登录即可

扩展:

使用qemu-system-aarch64进行uboot的仿真(通过浏览网页发现的)

  1. 编译uboot
git clone https://github.com/ARM-software/u-boot.git ARCH=arm64 CROSS_COPILE=aarch64-linux-gnu- make qemu_arm64_defconfig ARCH=arm64 CROSS_COPILE=aarch64-linux-gnu- make -j8
  1. 启动uboot
qemu-system-aarch64 -machine virt -cpu cortex-a57 -bios u-boot.bin -nographic

uboot就成功启动了

参考链接:

https://github.com/ARM-software/u-boot/blob/master/doc/README.qemu-arm
https://www.qemu.org/docs/master/system/linuxboot.html
https://pandysong.github.io/blog/post/run_u-boot_in_qemu/
https://stackoverflow.com/questions/58028789/how-to-build-and-boot-linux-aarch64-with-u-boot-with-buildroot-on-qemu
https://zhuanlan.zhihu.com/p/41258581
https://wiki.qemu.org/Documentation/Networking