带有网络和 virtio 支持的用于 raspberry pi 3 的 QEMU 内核

xua*_*ctn 1 virtualization kernel qemu linux-kernel raspberry-pi3

我使用 QEMU(qemu-system-aarch64 -M raspi3) 从工作映像中模拟带有内核的 Raspberry pi3。一切正常,但没有网络。

qemu-system-aarch64 \
   -kernel ./bootpart/kernel8.img \
   -initrd ./bootpart/initrd.img-4.14.0-3-arm64 \
   -dtb ./debian_bootpart/bcm2837-rpi-3-b.dtb \
   -M raspi3 -m 1024 \
   -nographic \
   -serial mon:stdio \
   -append "rw earlycon=pl011,0x3f201000 console=ttyAMA0 loglevel=8 root=/dev/mmcblk0p3 fsck.repair=yes net.ifnames=0 rootwait memtest=1" \
   -drive file=./genpi64lite.img,format=raw,if=sd,id=hd-root \
   -no-reboot
Run Code Online (Sandbox Code Playgroud)

我试图添加这个选项

-device virtio-blk-device,drive=hd-root \
-netdev user,id=net0,hostfwd=tcp::5555-:22 \
-device virtio-net-device,netdev=net0 \
Run Code Online (Sandbox Code Playgroud)

但是会有错误

qemu-system-aarch64: -device virtio-blk-device,drive=hd-root: 没有找到设备“virtio-blk-device”的“virtio-bus”总线 我参考了一些论坛,并使用了“virt”机器代替 raspi3 以模拟 virtio-network

qemu-system-aarch64 \
  -kernel ./bootpart/kernel8.img \
  -initrd ./bootpart/initrd.img-4.14.0-3-arm64 \
  -m 2048 \
  -M virt \
  -cpu cortex-a53 \
  -smp 8 \
  -nographic \
  -serial mon:stdio \
  -append "rw root=/dev/vda3 console=ttyAMA0 loglevel=8 rootwait fsck.repair=yes memtest=1" \
  -drive file=./genpi64lite.img,format=raw,if=sd,id=hd-root \
  -device virtio-blk-device,drive=hd-root \
  -netdev user,id=net0,net=192.168.1.1/24,dhcpstart=192.168.1.234 \
       -device virtio-net-device,netdev=net0 \
  -no-reboot
Run Code Online (Sandbox Code Playgroud)

没有打印任何内容,终端被暂停。这意味着内核不适用于 virt 机器。

我决定为我自己的自定义内核构建。任何人都可以给我一些建议来构建与 QEMU 和 virtio 一起工作的内核吗?

提前致谢!

LSe*_*rni 7

我遇到了与用户 @peterbabic 相同的问题,虽然我可以看到带有 的小工具lsusb,但我看不到任何网络设备。

所以我尝试手动插入适当的模块g_ether- 它说找不到驱动程序

就在那时,我意识到kernelv8.img我下载的文件和我启动的 Raspbian OS 映像是不同的版本,因此内核无法找到其模块,因为它在错误的目录中查找它们。

另一方面,Raspbian OS 映像在其第一个分区中具有正确的内核(我可以在 /boot 中看到它)。唯一的问题是把它拿出来并用它来替换错误的kernelv8.img(我在网上找不到正确的——无论如何,Raspbian 图像的内核根据定义更正确)。

因此,我将 Raspbian OS 映像复制到我的 Linux 机器上,并使用循环安装它:

# fdisk raspbian.img
  - command "p" lists partitions and tells me that P#1 starts at sector 2048
  - command "q" exits without changes
# losetup -o $[ 2048 * 512 ] /dev/loop9 raspbian.img # because sectors are 512 bytes
# mkdir /mnt/raspi
# mount /dev/loop9 /mnt/raspi
  - now "ls -la /mnt/raspi" shows the content of image partition 1, with kernels
# cp /mnt/raspi/kernel8.img .
# umount /mnt/raspi
# losetup -d /dev/loop9 # destroy loop device
# rmdir /mnt/raspi # remove temporary mount point
# rm raspbian.img
  - I no longer need the raspbian.img copy so I delete it.
  - now current directory holds "kernel8.img". I can just copy it back.
Run Code Online (Sandbox Code Playgroud)

可以肯定的是,我还修改了 Raspberry 映像上的 /boot/cmdline.txt(在使用新内核重新启动之前),以便它现在添加了 dwc 和 g_ether 模块:

在此输入图像描述

启动时,该小工具现在会自动识别:

在此输入图像描述


lmc*_*cho 5

最新版本的 QEMU(5.1.0 和 5.0.1)具有适用于 raspi3 机器的 USB 仿真(qemu-system-aarch64 -M raspi3)。

如果您使用:-device usb-net,netdev=net0 -netdev user,id=net0,hostfwd=tcp::5555-:22 在 QEMU 中,您可以模拟网络和对 SSH 的访问

我测试了这个配置,我得到了这个:

QEMU raspi3 中的 USB 网络设备 QEMU raspi3 中的 USB 网络设备

QEMU raspi3 中的以太网接口 QEMU raspi3 中的以太网接口

这是我使用的完整命令和选项:

qemu-system-aarch64 -m 1024 -M raspi3 -kernel kernel8.img -dtb bcm2710-rpi-3-b-plus.dtb -sd 2020-08-20-raspios-buster-armhf.img -append "console=ttyAMA0 root=/dev/mmcblk0p2 rw rootwait rootfstype=ext4" -nographic -device usb-net,netdev=net0 -netdev user,id=net0,hostfwd=tcp::5555-:22
Run Code Online (Sandbox Code Playgroud)

使用的 QEMU 版本是 5.1.0。

  • 哇,我花了几个小时尝试在 Ubuntu Focus 上启动 bullseye,并认为这是无望的。但这是第一次。谢谢。具体来说,我从源代码编译了 QEMU 6.2.0,然后运行:“qemu-img resize -f raw 2022-01-28-raspios-bullseye-arm64.img 4G”,然后运行“qemu-6.2.0/build/qemu-” system-aarch64 -m 1024 -M raspi3b -kernel kernel8.img -dtb bcm2710-rpi-3-b-plus.dtb -drive file=2022-01-28-raspios-bullseye-arm64.img,if=sd,格式=raw -append“console=ttyAMA0 root=/dev/mmcblk0p2 rw rootwait rootfstype=ext4”-ngraphic -device usb-net,netdev=net0 -netdev user,id=net0,hostfwd=tcp::5555-:22`。 (3认同)

Pet*_*ell 2

您的 raspi3 命令行没有网络,因为在 raspi3 上,网络是通过 USB 进行的,而 QEMU 还没有该板的 USB 控制器模型。添加virtio相关选项不起作用,因为raspi3没有PCI,因此无法插入pci virtio设备。

你的 virt 命令行选项看起来基本上是正确的(至少足以启动;你可能想要“if=none”而不是“if=sd”,我不确定网络选项是否完全正确,但如果这些部分是错误的,它们将导致稍后来自客户内核的错误,而不是完全缺乏输出)。所以你的问题可能是内核配置缺少一些重要的项目。

您可以在虚拟板上启动 Debian 内核(此处的说明: https: //translatedcode.wordpress.com/2017/07/24/installing-debian-on-qemus-64-bit-arm-virt-board/)因此,要查找内核配置中的错误,您可以采取的一种方法是将您的配置与 Debian 内核的配置进行比较。上游内核源“defconfig”也应该可以工作。我发现从一个有效的配置开始并削减它比通过尝试找到所有需要存在的晦涩选项从无到有构建一个配置更快。