Skip to content

Instantly share code, notes, and snippets.

@fvbock
Created February 23, 2019 12:54
Show Gist options
  • Save fvbock/ae37404b568d20acd5cfc19deb985db4 to your computer and use it in GitHub Desktop.
Save fvbock/ae37404b568d20acd5cfc19deb985db4 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
if [ -z $1 ]; then
echo "Usage: ./make-rpi.sh <hostname>"
echo " ./make-rpi.sh node-1"
echo " ./make-rpi.sh node-2"
exit 1
fi
export DEV=sda
export IMAGE=2018-11-13-raspbian-stretch-lite.img
if [ -z "$SKIP_FLASH" ];
then
echo "Writing Raspbian Lite image to SD card"
time dd if=$IMAGE of=/dev/$DEV bs=1M
fi
sync
echo "Mounting SD card from /dev/$DEV"
if [ ! -d "[/mnt/rpi/boot" ]; then
mkdir -p /mnt/rpi/boot
fi
if [ ! -d "[/mnt/rpi/root" ]; then
mkdir -p /mnt/rpi/root
fi
mount /dev/${DEV}1 /mnt/rpi/boot
mount /dev/${DEV}2 /mnt/rpi/root
# Add our SSH key
mkdir -p /mnt/rpi/root/home/pi/.ssh/
cat template-authorized_keys > /mnt/rpi/root/home/pi/.ssh/authorized_keys
# Enable ssh
touch /mnt/rpi/boot/ssh
# Disable password login
sed -ie s/#PasswordAuthentication\ yes/PasswordAuthentication\ no/g /mnt/rpi/root/etc/ssh/sshd_config
echo "Setting hostname: $1"
sed -ie s/raspberrypi/$1/g /mnt/rpi/root/etc/hostname
sed -ie s/raspberrypi/$1/g /mnt/rpi/root/etc/hosts
# Reduce GPU memory to minimum
echo "gpu_mem=16" >> /mnt/rpi/boot/config.txt
# Set static IP
# cp /mnt/rpi/root/etc/dhcpcd.conf /mnt/rpi/root/etc/dhcpcd.conf.orig
# sed s/100/$2/g template-dhcpcd.conf > /mnt/rpi/root/etc/dhcpcd.conf
echo "Unmounting SD Card"
umount /mnt/rpi/boot
umount /mnt/rpi/root
sync
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment