Skip to content

Instantly share code, notes, and snippets.

@andrewvaughan
Created April 30, 2016 18:44
Show Gist options
  • Save andrewvaughan/dcecd7636a896a96712fce8da95d0d23 to your computer and use it in GitHub Desktop.
Save andrewvaughan/dcecd7636a896a96712fce8da95d0d23 to your computer and use it in GitHub Desktop.
Easy installer for Raspberry Pi images
#!/bin/bash
# SD Card Image Installer for Mac
#
# Formats a given SD Card and writes an image to it - now with progress bars!
#
# Before using, ensure "pv" is installed with Homebrew (brew install pv)
#
# Usage: sudo install-pi.sh disk2 your-pi-image.img
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
if [ $# -lt 2 ]; then
echo "Usage: install diskN target.img"
exit 1
fi
if ! which -s pv; then
echo "pv required - please install with homebrew (brew install pv)"
exit 2
fi
if [ ! -f $2 ]; then
echo "Image \"$2\" not found"
exit 3
fi
if ! diskutil info $1 | grep -q "SD Card"; then
echo "SD Card Reader not found at /dev/$1"
exit 4
fi
TARGET="/dev/$1"
printf "\n\e[1m\e[32mCard reader found at \e[33m$TARGET\e[0m\n"
printf "\n\e[1m\e[32mFormatting to \e[33mFAT32\e[32m...\e[0m\n"
diskutil eraseDisk fat32 "UNTITLED" "$TARGET"
printf "\n\e[1m\e[32mUnmounting...\e[0m\n"
diskutil unmountDisk "$TARGET"
printf "\n\e[1m\e[32mCopying image...\e[0m\n"
pv -tpreb "$2" | dd of="$TARGET" bs=1m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment