1
votes

I used the QEMU(qemu-system-aarch64 -M raspi3) for emulating the Raspberry pi3 with the kernel from the working image. Everything was working but there was no networking.

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

I tried to add this option

-device virtio-blk-device,drive=hd-root \
-netdev user,id=net0,hostfwd=tcp::5555-:22 \
-device virtio-net-device,netdev=net0 \

But there would be an error

qemu-system-aarch64: -device virtio-blk-device,drive=hd-root: No 'virtio-bus' bus found for device 'virtio-blk-device' I have referenced some forum, and used the "virt" machine instead of raspi3 in order of emulating 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

There is nothing printed and the terminal was suspended. It means the kernel does not work with virt machine.

I decided to build for my own custom kernel. Could anyone give me advice for options to build the kernel that works with both the QEMU and the virtio?

Thanks in advance!

3

3 Answers

5
votes

The latest versions of QEMU (5.1.0 and 5.0.1) have USB emulation for the raspi3 machine (qemu-system-aarch64 -M raspi3).

You can emulate networking and access to SSH if you use: -device usb-net,netdev=net0 -netdev user,id=net0,hostfwd=tcp::5555-:22 in QEMU

I tested this configuration, and I got this:

The USB network device in QEMU raspi3 USB network device in QEMU raspi3

Ethernet interface in QEMU raspi3 Ethernet interface in QEMU raspi3

Here the full command and options that I used:

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

The QEMU version used was 5.1.0.

1
votes

I have had the same problems as user @peterbabic, in that while I could see the gadget with lsusb, I could not see any net device.

So I tried manually inserting the appropriate module g_ether -- and it said that it could not find the driver.

It was then that I realized that the kernelv8.img file I had downloaded and the Raspbian OS image that I was booting were different versions, so the kernel could not find its modules because it looked for them in the wrong directory.

On the other hand, the Raspbian OS image had the correct kernel in its first partition (I could see it in /boot). The only problem was getting it out and use it to replace the wrong kernelv8.img (I could not find the correct one online -- and anyway, the kernel of the Raspbian image is by definition more correct).

So, I copied the Raspbian OS image on my Linux box, and mounted it with loop:

# 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.

To be sure, I also modified /boot/cmdline.txt on the Raspberry image (before rebooting with the new kernel) so that it now added the dwc and g_ether modules:

enter image description here

On boot, the gadget is now automatically recognized:

enter image description here

0
votes

Your raspi3 command line has no networking because on a raspi3 the networking is via USB, and QEMU doesn't have a model of the USB controller for that board yet. Adding virtio-related options won't work, because the raspi3 has no PCI and so there's no way to plug in a pci virtio device.

Your command line option with virt looks basically right (at least enough so to boot; you probably want "if=none" rather than "if=sd" and I'm not sure if the network options are quite right, but if those parts are wrong they will result in errors from the guest kernel later rather than total lack of output). So your problem is likely that the kernel config is missing some important items.

You can boot a stock Debian kernel on the virt board (instructions here: https://translatedcode.wordpress.com/2017/07/24/installing-debian-on-qemus-64-bit-arm-virt-board/) so one approach you could take to finding the error in your kernel config is to compare your config with the one the Debian kernel has. The upstream kernel source 'defconfig' also should work. I find that starting with a configm that works and cutting it down is faster than building one up from nothing by trying to find all the obscure options that need to be present.