Last active
April 27, 2022 00:21
-
-
Save superswan/1821ad6a304ab929d1c126f544bc65b4 to your computer and use it in GitHub Desktop.
Script for raspberry pi emulation. Will create a QEMU vm with fresh raspian. https://azeria-labs.com/emulate-raspberry-pi-with-qemu/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
BLUE='\033[0;34m' | |
NC='\033[0m' | |
GIB_IN_BYTES="1073741824" | |
target="${1:-pi1}" | |
image_path="./sdcard/filesystem.img" | |
kernel_path="./kernel/qemu-rpi-kernel.zip" | |
zip_path="./filesystem.zip" | |
filesystem_image="http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2019-09-30/2019-09-26-raspbian-buster-lite.zip" | |
rpi_kernel="https://github.com/dhruvvyas90/qemu-rpi-kernel/archive/afe411f2c9b04730bcc6b2168cdc9adca224227c.zip" | |
mkdir -p sdcard | |
mkdir -p kernel | |
if [ ! -e $image_path ]; then | |
echo -e "${RED}No filesystem detected at ${image_path}!" | |
echo -e "${BLUE}Downloading filesystem${NC}" | |
wget -O filesystem.zip $filesystem_image | |
echo -e "${GREEN}Filesystem downloaded.${NC}" | |
fi | |
if [ -e $zip_path ]; then | |
echo -e "${BLUE}Extracting fresh filesystem...${NC}" | |
unzip $zip_path | |
mv -- *.img "$image_path" | |
fi | |
echo -e "${BLUE}Preparing filesystem image${NC}" | |
qemu-img info $image_path | |
image_size_in_bytes=$(qemu-img info --output json $image_path | grep "virtual-size" | awk '{print $2}' | sed 's/,//') | |
if [[ "$(($image_size_in_bytes % ($GIB_IN_BYTES * 2)))" != "0" ]]; then | |
new_size_in_gib=$((($image_size_in_bytes / ($GIB_IN_BYTES * 2) + 1) * 2)) | |
echo -e "${BLUE}Rounding image size up to ${new_size_in_gib}GiB so it's a multiple of 2GiB...${NC}" | |
qemu-img resize $image_path "${new_size_in_gib}G" | |
fi | |
if [ ! -e $kernel_path ]; then | |
echo -e "{$BLUE}Preparing Kernel${NC}" | |
cd kernel | |
wget -O qemu-rpi-kernel.zip $rpi_kernel | |
unzip qemu-rpi-kernel.zip | |
cp qemu-rpi-kernel-*/kernel-qemu-4.19.50-buster . | |
cp qemu-rpi-kernel-*/versatile-pb.dtb . | |
rm qemu-rpi-kernel.zip | |
rm -rf qemu-rpi-kernel-* | |
cd .. | |
fi | |
if [ "${target}" = "pi1" ]; then | |
emulator=qemu-system-arm | |
kernel="./kernel/kernel-qemu-4.19.50-buster" | |
dtb="./kernel/versatile-pb.dtb" | |
machine=versatilepb | |
memory=256m | |
root=/dev/sda2 | |
nic="--net nic --net user,hostfwd=tcp::5022-:22" | |
elif [ "${target}" = "pi2" ]; then | |
emulator=qemu-system-arm | |
machine=raspi2b | |
memory=1024m | |
kernel_pattern=kernel7.img | |
dtb_pattern=bcm2709-rpi-2-b.dtb | |
append="dwc_otg.fiq_fsm_enable=0" | |
nic="-netdev user,id=net0,hostfwd=tcp::5022-:22 -device usb-net,netdev=net0" | |
elif [ "${target}" = "pi3" ]; then | |
emulator=qemu-system-aarch64 | |
machine=raspi3b | |
memory=1024m | |
kernel_pattern=kernel8.img | |
dtb_pattern=bcm2710-rpi-3-b-plus.dtb | |
append="dwc_otg.fiq_fsm_enable=0" | |
nic="-netdev user,id=net0,hostfwd=tcp::5022-:22 -device usb-net,netdev=net0" | |
else | |
echo "Target ${target} not supported" | |
echo "Supported targets: pi1 pi2 pi3" | |
exit 2 | |
fi | |
if [ "${kernel_pattern}" ] && [ "${dtb_pattern}" ]; then | |
fat_path="./fat.img" | |
echo -e"{$NC}Extracting partitions" | |
fdisk -l ${image_path} \ | |
| awk "/^[^ ]*1/{print \"dd if=${image_path} of=${fat_path} bs=512 skip=\"\$4\" count=\"\$6}" \ | |
| sh | |
echo "Extracting boot filesystem" | |
fat_folder="./fat" | |
mkdir -p "${fat_folder}" | |
fatcat -x "${fat_folder}" "${fat_path}" | |
root=/dev/mmcblk0p2 | |
echo "Searching for kernel='${kernel_pattern}'" | |
kernel=$(find "${fat_folder}" -name "${kernel_pattern}") | |
echo "Searching for dtb='${dtb_pattern}'" | |
dtb=$(find "${fat_folder}" -name "${dtb_pattern}") | |
fi | |
if [ "${kernel}" = "" ] || [ "${dtb}" = "" ]; then | |
echo -e "{$RED}Missing kernel='${kernel}' or dtb='${dtb}'" | |
exit 2 | |
fi | |
echo -e "${GREEN}Booting QEMU machine \"${machine}\" with kernel=${kernel} dtb=${dtb}${NC}" | |
exec ${emulator} \ | |
--machine "${machine}" \ | |
--cpu arm1176 \ | |
--m "${memory}" \ | |
--drive "format=raw,file=${image_path}" \ | |
${nic} \ | |
--dtb "${dtb}" \ | |
--kernel "${kernel}" \ | |
--append "rw earlyprintk loglevel=8 console=ttyAMA0,115200 dwc_otg.lpm_enable=0 root=${root} rootwait panic=1 ${append}" \ | |
--no-reboot \ | |
--display none \ | |
--serial mon:stdio |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment