dhruvvyas90 / qemu-rpi-kernel

Qemu kernel for emulating Rpi on QEMU
2.05k stars 362 forks source link

Unable to get networking in Native emulation. #131

Open Jugulaire opened 3 years ago

Jugulaire commented 3 years ago

Hey !

I have successfully started Raspbian in QEMU with this command :

sudo /usr/local/bin/qemu-system-aarch64 -M raspi3 -append "rw earlyprintk loglevel=8 console=ttyAMA0,115200 dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootdelay=1" -dtb '/home/jugu/SSD/home/jugu/VM-HDD/raspbian/qemu-rpi-kernel/native-emulation/dtbs/bcm2710-rpi-3-b-plus.dtb' -sd '/home/jugu/SSD/home/jugu/VM-HDD/raspbian/raspbian.qcow2' -kernel "/home/jugu/SSD/home/jugu/VM-HDD/raspbian/qemu-rpi-kernel/native-emulation/5.4.51 kernels/kernel8.img" -m 1G -smp 4 -serial stdio -usb -device usb-mouse -device usb-kbd

But i'm unable to get networking inside this box. I've followed the QEMU guide, but I cannot figure out. Did someone have successfully booted a native emulated raspbian with networking enabled ?

Thanks in advance.

Jugulaire commented 3 years ago

I have some result.

Here is what i did :

  1. Get 2021-01-11-raspios-buster-armhf image (MUST BE THIS ONE)
  2. Extract the dtd and kernel file (follow this part to do this.
  3. Resized the img file qemu-img resize -f raw raspbian.img 8G
  4. Botted using this exact command qemu-system-aarch64_6 -M raspi3 -append "rw earlyprintk loglevel=8 console=ttyAMA0,115200 dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootdelay=1" -dtb '/home/jugu/SSD/home/jugu/VM-HDD/raspbian/Emulation_pi3/bcm2710-rpi-3-b-plus.dtb' -sd '/home/jugu/SSD/home/jugu/VM-HDD/raspbian/Emulation_pi3/raspbian.img' -kernel '/home/jugu/SSD/home/jugu/VM-HDD/raspbian/Emulation_pi3/kernel8.img' -m 1G -smp 4 -serial stdio -usb -device usb-mouse -device usb-kbd -device usb-net,netdev=net0 -netdev user,id=net0,hostfwd=tcp::5555-:22

Now i'm able to get networking inside the virtual Pi and i'm able to log in ssh. So now i can do port forwarding if required to test stuff thru SSH.

Jugulaire commented 3 years ago

But now i'm wondering if it's possible to connect my qemu RPI to my local bridge to have full networking since my port frowarding are limited to SSH with this techinque.

ptrsr commented 3 years ago

You should be able to create a network tap using QEMU. Then use bridge-utils to bridge your network and tap interface.

The reason why networking does not work with all distro images is because (native) ethernet emulation is done through a virtual USB ethernet adapter. This adapter requires kernel modules USB_ETH and USB_DWC2, that are installed under /lib/modules in the root partition. I believe that these modules have to match the (external) kernel version, which is why a lot of images are not supported.

Radoom commented 3 years ago

I'm trying to get a special raspi image to boot (eblocker-2.6). It won't start with the way Jugulaire shows above (thanks!). So I first repeated what Jugulaire did, with the same raspbian image/version mentioned above - that works fine, i.e. with the image "2021-01-11-raspios-buster-armhf". I also tried a more recent image "2021-05-07-raspios-buster-armhf", but that one does not start at all. Same for the "eblocker" image.

I then used the kernel (5.4.83-v8+) and dtb extracted from the "2021-01-11" image in conjunction with the eblocker-2.6 image. THAT combination then boots. But eblocker then misses its network, because it tries to load the respective module from the kernel version folders it was booted with (5.4.83-v8+) - which of course differ from those in the image itself (5.10.17 something).

So, it seems to heavily depend on what kernel version is used with qemu, even though all three images tested are dedicated raspi-3 compatible images. Anyone got a clue what distinguishes the 2021-01-11 / 5.4.83-v8+ kernel from the one in the 2021-05-07 image? (edit: I mean, sure, a ton of things; but what might be relevant for qemu (not) even booting the first few basic steps?) Would like to know what causes the one to work with qemu but not the other.

markostamcar commented 2 years ago

Just to add my experience: