Skip to content

Instantly share code, notes, and snippets.

@LogCreative
Last active December 7, 2022 14:31
Show Gist options
  • Select an option

  • Save LogCreative/f87d968d91cf554ccf48d3b3f7fd7987 to your computer and use it in GitHub Desktop.

Select an option

Save LogCreative/f87d968d91cf554ccf48d3b3f7fd7987 to your computer and use it in GitHub Desktop.
QEMU emulation of Raspberry Pi 3b
  1. Install QEMU.
  2. Since the recent Raspberry Pi OS has removed the preset user, use a legacy version like 2022-01-28 will work. Download the Raspberry Pi OS and extract these files from the image:
  • bcm2710-rpi-3-b.dtb
  • kernel8.img
  1. Resize the image to 8G to avoid the disk error. Since the disk space has to be the square of 2 and the image itself occupies 3.8G already. If you want to make more actions, please allocate a larger space.
qemu-img resize 2022-01-28-raspios-bullseye-arm64.img 8G
  1. Write the following code to a bash file launch.sh, reference to this repo and this spec.
qemu-system-aarch64 \
    -M raspi3b \
    -cpu cortex-a53 \
    -append "rw earlyprintk loglevel=8 console=ttyAMA0,115200 dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootdelay=1" \
    -dtb bcm2710-rpi-3-b.dtb \
    -sd 2022-01-28-raspios-bullseye-arm64.img \
    -kernel 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::2222-:22,hostfwd=tcp::9090-:9090 \

and add the permission to run the script by

chmod +x launch.sh

You can now run the OS by

./launch.sh
  1. The default user for logging in:
pi
raspberry
  1. Open SSH connection on the raspberry pi, use the command
sudo raspi-config

And then go to [Interface Options]->[SSH]->[Yes]->[Finish], reboot the system. And on the host side, use the command to connect to the raspberry pi by SSH.

ssh -p 2222 pi@localhost
  1. Extend the disk. First check the sector number by
$ sudo fdisk -l

Device         Boot  Start     End Sectors  Size Id Type
/dev/mmcblk0p1        8192  532479  524288  256M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      532480 8118271 7585792  3.6G 83 Linux

note down the start section of mmcblk0p2 (here is 532480). Next change the partition by fdisk:

pi@raspberrypi:~$ sudo fdisk /dev/mmcblk0 # mmcblk0 is the device

Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d  # delete the partition
Partition number (1,2, default 2): 2 # number 2

Partition 2 has been deleted.

Command (m for help): n # new a partition
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p # primary
Partition number (2-4, default 2): 2 # the second
First sector (2048-16777215, default 2048): 532480 # the start section noted before
Last sector, +/-sectors or +/-size{K,M,G,T,P} (532480-16777215, default 16777215): # use default end

Created a new partition 2 of type 'Linux' and of size 7.7 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N # just extend, no need to remove the signature

Command (m for help): w # execute.

The partition table has been altered.
Syncing disks.

Now reboot the system

sudo reboot

And then take the change into effect.

sudo resize2fs /dev/mmcblk0p2

You can now see the space is extended:

pi@raspberrypi:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       7.6G  3.3G  4.0G  46% /
# ...
  1. To avoid the certificate expired error, we need to calibrate the date and time.
sudo date --s="2022-12-07 22:30:00"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment