Skip to content

Instantly share code, notes, and snippets.

@mahboobkarimian
Last active December 28, 2022 15:32
Show Gist options
  • Save mahboobkarimian/6c1cc038fac1af65b72fb0290c731711 to your computer and use it in GitHub Desktop.
Save mahboobkarimian/6c1cc038fac1af65b72fb0290c731711 to your computer and use it in GitHub Desktop.
Run Raspbian OS in QEMU (add user to the image before running!)

Install qemu

sudo apt install qemu-system-arm

Download Raspbian OS and resize it

Download from https://www.raspberrypi.com/software/operating-systems/#raspberry-pi-os-64-bit and extract it somewhere.

Resize to 4GB by:

 qemu-img resize 2022-09-22-raspios-bullseye-arm64-lite.img 4G

Extract kernel and device tree file

which is needed for qemu, also add a user (since there is no default pi user anymore!)

First create 2 directories for mount: mkdir ./boot ./rootfs Then check the starting sector as offset for boot and rootfs with:

file 2022-09-22-raspios-bullseye-arm64-lite.img

The output is like this:

file 2022-09-22-raspios-bullseye-arm64-lite.img 
2022-09-22-raspios-bullseye-arm64-lite.img: DOS/MBR boot sector; partition 1 : ID=0xc, start-CHS (0x40,0,1), end-CHS (0x3ff,3,32), startsector 8192, 524288 sectors; partition 2 : ID=0x83, start-CHS (0x3ff,3,32), end-CHS (0x3ff,3,32), startsector 532480, 3391488 sectors

Mount the boot partition:

sudo mount -o loop,offset=$[512*8192] 2022-09-22-raspios-bullseye-arm64-lite.img ./boot/

Do cd boot and copy these 2 files: bcm2710-rpi-3-b.dtb and kernel8.img.

Now, and Add a user according to raspberry pi official documentation https://www.raspberrypi.com/documentation/computers/configuration.html#configuring-a-user

Unmounting the boot directory, will save the modifications on the image file:

sudo umount ./boot/
  • Note: To mount the rootfs on Raspbian image and edit, you need to grab the startsector from the file command output. In our case it is 532480 and this value should be used as offset in mount command; e.g.
sudo mount -o loop,offset=$[512*532480] 2022-09-22-raspios-bullseye-arm64-lite.img ./rootfs/

Run

qemu-system-aarch64 -M raspi3b -cpu cortex-a72 -append "rw earlyprintk loglevel=8 console=ttyAMA0,115200 root=/dev/mmcblk0p2 rootdelay=1" -serial stdio -dtb ./bcm2710-rpi-3-b.dtb -sd ./2022-09-22-raspios-bullseye-arm64-lite.img -kernel kernel8.img -m 1G -smp 4

Do ssh

Apped -device usb-net,netdev=net0 -netdev user,id=net0,hostfwd=tcp::4321-:22 to the end of the command in Run section. When Raspbian is up, start ssh service by sudo systemctl start ssh. Now, in your host machine type:

ssh -p 4321 pi@localhost

To do scp (secure copy) you can use the following:

scp -P 4321 [email protected]:/tmp/xyz /tmp

Sources

https://www.raspberrypi.com/documentation/computers/configuration.html#configuring-a-user

https://blog.videgro.net/2015/11/modify-disk-image-raspbian/

https://www.marcusfolkesson.se/blog/rpi-qemu/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment