Skip to content

Instantly share code, notes, and snippets.

@onedr0p
Last active September 22, 2019 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onedr0p/fc336c6de4d045eadc9e1a75b3c9b138 to your computer and use it in GitHub Desktop.
Save onedr0p/fc336c6de4d045eadc9e1a75b3c9b138 to your computer and use it in GitHub Desktop.
Create a Raspbian image with SSH already enabled
#!/bin/bash
# raspbian-enable-ssh
#
# Purpose:
# Creates a raspbian img with ssh already enabled
#
# Note:
# This script takes one argument, either the zip or the img file
# zip file will be extracted and the image file will be used
# img file will be in the same directory as the imput file
# img file will be overwritten with the new image
#
# Example:
# sudo ./raspbian-enable-ssh.sh ~/Downloads/2019-07-10-raspbian-buster-lite.zip
INPUT_FILE=$1
# ensure only running as root
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
echo "$(date "+%d.%m.%Y %T") Error: Not running as root"
exit 1
fi
# ensure file exists
if [[ ! -f "${INPUT_FILE}" ]]; then
echo "$(date "+%d.%m.%Y %T") Error: ${INPUT_FILE} does not exist"
exit 1
fi
# ensure script isn't already running
if pidof -o %PPID -x "$0"; then
echo "$(date "+%d.%m.%Y %T") Error: Already running"
exit 1
fi
# ensure file is either zip or img
case "${INPUT_FILE}" in
*.zip | *.img )
echo "$(date "+%d.%m.%Y %T") Debug: Found file ${INPUT_FILE}..."
;;
*)
echo "$(date "+%d.%m.%Y %T") Error: Input file is not a img or zip file"
exit 1
;;
esac
# unzip if the input file is a zip file then update INPUT_FILE
if [ "${INPUT_FILE##*.}" = "zip" ]; then
echo "$(date "+%d.%m.%Y %T") Info: Found zip file ${INPUT_FILE}, unzipping..."
ZIP_ABS_DIR=$(realpath "$INPUT_FILE")
echo "$(date "+%d.%m.%Y %T") Debug: Zip directory ${ZIP_ABS_DIR}"
ZIP_FILE=$(basename "$INPUT_FILE")
echo "$(date "+%d.%m.%Y %T") Debug: Zip filename ${ZIP_FILE}"
unzip -q -o -d "${ZIP_ABS_DIR%/*}" "${INPUT_FILE}"
INPUT_FILE="${ZIP_ABS_DIR%.*}.img"
echo "$(date "+%d.%m.%Y %T") Debug: Image filename ${INPUT_FILE}"
fi
# get the absolution path for the input file
IMG_ABS_FILE=$(realpath "$INPUT_FILE")
echo "$(date "+%d.%m.%Y %T") Debug: Image directory ${IMG_ABS_FILE}"
# get the filename of the input file
IMG_FILE=$(basename "$IMG_ABS_FILE")
echo "$(date "+%d.%m.%Y %T") Debug: Image filename ${IMG_FILE}"
# remove the extension from the input file
IMG_NOEXT_FILE="${IMG_FILE%.*}"
echo "$(date "+%d.%m.%Y %T") Debug: Image filename without extension ${IMG_NOEXT_FILE}"
# ensure mount path is not already mounted
mountpoint -q "/mnt/${IMG_NOEXT_FILE}"
if [ "$?" != 0 ]; then
echo "$(date "+%d.%m.%Y %T") Error: /mnt/${IMG_NOEXT_FILE} already mounted"
exit 1
fi
# get the offset for mounting
echo "$(date "+%d.%m.%Y %T") Info: Mounting at /mnt/${IMG_NOEXT_FILE}..."
typeset -i SECTOR_OFFSET=$(fdisk -l "${IMG_ABS_FILE}" | awk '/img1/{print $2}')
BYTE_OFFSET=$((${SECTOR_OFFSET}*512))
# create the mount path
mkdir -p "/mnt/${IMG_NOEXT_FILE}"
# mount the input file
mount -o "loop,offset=${BYTE_OFFSET}" "${IMG_ABS_FILE}" "/mnt/${IMG_NOEXT_FILE}"
if [ "$?" != 0 ]; then
echo "$(date "+%d.%m.%Y %T") Error: Unable to mount /mnt/${IMG_NOEXT_FILE}"
echo "$(date "+%d.%m.%Y %T") Error: You need to unmount manually /mnt/${IMG_NOEXT_FILE}"
exit 1
fi
# enable ssh
echo "$(date "+%d.%m.%Y %T") Info: Creating ssh file in /mnt/${IMG_NOEXT_FILE}/ssh..."
touch "/mnt/${IMG_NOEXT_FILE}/ssh"
# unmount the input file
echo "$(date "+%d.%m.%Y %T") Info: Unmounting /mnt/${IMG_NOEXT_FILE}..."
umount -f "/mnt/${IMG_NOEXT_FILE}"
if [ "$?" != 0 ]; then
echo "$(date "+%d.%m.%Y %T") Error: Unable to unmount /mnt/${IMG_NOEXT_FILE}"
echo "$(date "+%d.%m.%Y %T") Error: You need to unmount manually /mnt/${IMG_NOEXT_FILE}"
exit 1
fi
echo "$(date "+%d.%m.%Y %T") Info: Success..."
echo "$(date "+%d.%m.%Y %T") Info: Ready to flash ${IMG_ABS_FILE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment