Skip to content

Instantly share code, notes, and snippets.

@brettbeeson

brettbeeson/write-pi-sd.sh

Last active Dec 16, 2020
Embed
What would you like to do?
Write a basic Raspberry Pi distro to an SD card from a linux box
#!/bin/sh
if [ $# -ne 1 ]
then
echo "Wrong args"
echo "write-pi-sd.sh <device>
echo <device> such as "/dev/sdc" for a SD card
exit 1
fi
dev=$1
boot="$1"1
root="$1"2
#set -x
echo dev=$1
echo boot=$boot
echo root=$root
# show device
lsblk -p | grep -v loop
if [ ! -e "$dev" ]; then
"$dev" does not exist
exit 1
fi
# unmount your sd if necessary
sudo umount $boot 2>/dev/null
sudo umount $root 2>/dev/null
if [ -f raspios.img ]; then
echo "Using existing image"
else
echo "Downloading new image"
#wget --no-verbose --show-progress -O raspios https://downloads.raspberrypi.org/raspios_armhf/images/raspios_armhf-2020-12-04/2020-12-02-raspios-buster-armhf.zip
wget --no-verbose --show-progress -O raspios https://downloads.raspberrypi.org/raspios_lite_armhf/images/raspios_lite_armhf-2020-12-04/2020-12-02-raspios-buster-armhf-lite.zip
unzip -p raspios > raspios.img
fi
echo Writing image. Wait one.
sudo dd bs=4M if=raspios.img of=$dev conv=fsync status=progress
# don't fret... it takes a while: https://superuser.com/questions/1220867/dd-function-seems-to-freeze-when-done
echo Mount boot and root file system to /media/sdcard1,2
if [ ! -d /media/sdcard1 ]; then
sudo mkdir /media/sdcard1
fi
if [ ! -d /media/sdcard2 ]; then
sudo mkdir /media/sdcard2
fi
echo Mounting
sudo mount $boot /media/sdcard1 # boot parition
sudo mount $root /media/sdcard2 # root
echo Configuring wifi
sudo cp wpa_supplicant.conf /media/sdcard1/
echo Configuring ssh
sudo touch /media/sdcard1/ssh
mkdir /media/sdcard2/home/pi/.ssh/
cp ~/.ssh/authorized_keys /media/sdcard2/home/pi/.ssh/ # ssh *to* it
echo Configuring optional
cp ~/.ssh/id_rsa /media/sdcard2/home/pi/.ssh/ # if you want to ssh *from* it
cp ~/.bashrc /media/sdcard2/home/pi/
cp -r ~/.aws /media/sdcard2/home/pi/
sudo umount $boot
sudo umount $root
sync
echo Done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment