Skip to content

Instantly share code, notes, and snippets.

@konsumer
Last active July 8, 2020 23:48
Show Gist options
  • Save konsumer/8c739515297b630a44caa75c44d4bdee to your computer and use it in GitHub Desktop.
Save konsumer/8c739515297b630a44caa75c44d4bdee to your computer and use it in GitHub Desktop.
This is a basic system to modify a pi image & boot it in an emulator. Only docker is required. Instead of docker, you could also use chroot in create_image script. I like using qemu (in docker) so I can really test if the image will work. It constrains memory and runs slower. A chroot is much faster, but less similar to a real pi.
#!/bin/bash
IMG_FILE="${PWD}/yours-buster.img"
if [ ! -f "2020-05-27-raspios-buster-lite-armhf-firstboot.zip" ]; then
echo "Downloading disk image."
curl -OL https://github.com/meeDamian/raspios/releases/download/2020-05-27/2020-05-27-raspios-buster-lite-armhf-firstboot.zip || (
echo "Unable to extract."
exit 1
)
fi
if [ ! -f "${IMG_FILE}" ]; then
echo "Extracting ${IMG_FILE}"
(unzip 2020-05-27-raspios-buster-lite-armhf-firstboot.zip && mv 2020-05-27-raspios-buster-lite-armhf-firstboot.img "${IMG_FILE}") || (
echo "Unable to extract."
exit 1
)
fi
SECTOR_START_BOOT="$(file "${IMG_FILE}" | grep -Eo 'startsector [[:digit:]]+' | cut -d' ' -f2 | head -n1)"
SECTOR_END_BOOT="$(file "${IMG_FILE}" | grep -Eo '[[:digit:]]+ sectors' | cut -d' ' -f1 | head -n1)"
SECTOR_START_ROOT="$(file "${IMG_FILE}" | grep -Eo 'startsector [[:digit:]]+' | cut -d' ' -f2 | sort -nr | head -n1)"
function cleanup {
echo "Unmounting image."
sudo umount -f pi/boot
sudo umount -f pi
}
trap cleanup EXIT
mkdir -p pi
echo "Mounting root"
if ! out="$(sudo mount -o "loop,offset=$((SECTOR_START_ROOT * 512))" "${IMG_FILE}" pi)"; then
echo "Unable to mount: $out"
exit 1
fi
echo "Mounting boot"
if ! out="$(sudo mount -o "loop,offset=$((SECTOR_START_BOOT * 512)),sizelimit=$((SECTOR_END_BOOT * 512))" "${IMG_FILE}" pi/boot)"; then
echo "Unable to mount: $out"
exit 1
fi
sudo cp firstboot.sh pi/boot
# do any other things like copying files from here into pi/
# you could also just chroot a bash-prompt here, if you have qemu & binfmt misc setup
# sudo chroot pi /bin/bash
# or run a script
# sudo cp setup.sh pi/tmp
# sudo chroot pi /bin/bash /tmp/setup.sh
#!/bin/bash
# Use this script to boot the pi image
# after it runs you can login directly, or SSH with:
# ssh -p 5022 pi@localhost
# use Ctrl-A then X to exit from console
IMG_FILE="${PWD}/yours-buster.img"
docker run -p 127.0.0.1:5022:5022 -it -v ${IMG_FILE}:/sdcard/filesystem.img lukechilds/dockerpi:vm
#!/bin/bash
# This is run on first-boot of pi.
# Put whatever you like in here to set things up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment