Skip to content

Instantly share code, notes, and snippets.

@0xd61
Created October 7, 2020 19:59
Show Gist options
  • Save 0xd61/7e1b4116f7f41ae734a54a707d420b25 to your computer and use it in GitHub Desktop.
Save 0xd61/7e1b4116f7f41ae734a54a707d420b25 to your computer and use it in GitHub Desktop.
Script to run an rpi image on QEMU
#!/bin/sh
# This script follows the instructions found in https://github.com/dhruvvyas90/qemu-rpi-kernel
QEMU=`which qemu-system-arm`
BASE_DIR=.
RPI_KERNEL=${BASE_DIR}/kernel-qemu-5.4.51-buster
RPI_DTB=${BASE_DIR}/versatile-pb-buster.dtb
IMAGE_BASE=2020-08-20-raspios-buster-armhf-lite
IMAGE=${IMAGE_BASE}.img
mkdir -p $BASE_DIR; cd $BASE_DIR
if [ ! -f ${RPI_KERNEL} ]; then
wget https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/${RPI_KERNEL}?raw=true \
-O ${RPI_KERNEL}
fi
if [ ! -f ${RPI_DTB} ]; then
wget https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/${RPI_DTB}?raw=true \
-O ${RPI_DTB}
fi
if [ ! -f ${IMAGE} ]; then
wget https://downloads.raspberrypi.org/raspios_lite_armhf/images/raspios_lite_armhf-2020-08-24/${IMAGE_BASE}.zip
unzip $IMAGE_BASE.zip
fi
# create 2 GIB SD card
if [ ! -f ${BASE_DIR}/rpi.img ]; then
cat $RPI_FS /dev/zero | dd bs=4096 count=524288 > ${BASE_DIR}/rpi.img
fi
if [ "$1" = "run" ]; then
$QEMU -kernel ${RPI_KERNEL} \
-cpu arm1176 -m 256M \
-M versatilepb \
-dtb ${RPI_DTB} \
-append "root=/dev/vda2 panic=1" \
-drive "file=${BASE_DIR}/rpi.img,if=none,index=0,media=disk,format=raw,id=disk0" \
-device "virtio-blk-pci,drive=disk0,disable-modern=on,disable-legacy=off" \
-no-reboot \
-serial stdio
elif [ "$1" = "mount" ]; then
sudo mkdir -p /mnt/rpi/img1
sudo mkdir -p /mnt/rpi/img2
sudo kpartx -a ${BASE_DIR}/rpi.img
sudo mount /dev/mapper/loop0p2 /mnt/rpi/img1 -o loop,rw
sudo mount /dev/mapper/loop0p2 /mnt/rpi/img2 -o loop,rw
echo "mounted at:"
echo " /mnt/rpi/img1"
echo " /mnt/rpi/img2"
elif [ "$1" = "umount" ]; then
sudo umount /mnt/rpi/img1
sudo umount /mnt/rpi/img2
sudo kpartx -d ${BASE_DIR}/rpi.img
sudo rmdir /mnt/rpi/img1
sudo rmdir /mnt/rpi/img2
else
echo "usage: qemu_script.sh <command>"
echo ""
echo "commands:"
echo "run - starts qemu"
echo "mount - mounts the image partitions"
echo "unmount - unmounts the image partitions"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment