#!/bin/bash # # Copy a img.gz image file to a Compact Flash Card # # Copyright (C) 2009-2016 Lonnie Abelbeck # This is free software, licensed under the GNU General Public License # version 3 as published by the Free Software Foundation; you can # redistribute it and/or modify it under the terms of the GNU # General Public License; and comes with ABSOLUTELY NO WARRANTY. # # Input Arg1: diskN # Input Arg2: img.gz or .img image file # if [ -z "$1" ]; then echo "Usage: os-x-cf-write diskN image_file.img.gz" diskutil list exit 1 fi DISK="${1#/dev/}" if [ "${DISK%[1-9]}" != "disk" ]; then echo "Usage: os-x-cf-write diskN image_file.img.gz" exit 1 fi if [ -z "$2" ]; then echo "Usage: os-x-cf-write diskN image_file.img.gz" exit 1 fi IMAGEFILE="$2" if [ ! -f "$IMAGEFILE" ]; then echo "File: $IMAGEFILE, can not be found." echo "Exiting, no action performed." exit 1 fi if ! diskutil list $DISK >/dev/null; then echo "Disk: $DISK, can not be found." echo "Exiting, no action performed." exit 1 fi if diskutil list $DISK | grep -q "_HFS"; then echo "Disk: $DISK, does not appear to be a Flash Card." echo "Exiting, no action performed." exit 1 fi if [ "$(id -u)" -ne 0 ]; then echo "Permission denied, run with 'sudo'" echo "Exiting, no action performed." exit 1 fi if [ ! -e "/dev/r$DISK" ]; then echo "Device not found: /dev/r$DISK" echo "Exiting, no action performed." exit 1 fi # # Inputs look good, off to work... # diskutil unmountDisk $DISK echo echo "Erasing first 1 GB of /dev/$DISK..." echo "This may take a long time..." dd if=/dev/zero of=/dev/r$DISK bs=64k count=16000 echo echo "Writing \"$IMAGEFILE\" to /dev/r$DISK" echo "This may take a long time..." if [ "${IMAGEFILE}" != "${IMAGEFILE%.gz}" ]; then gzcat "$IMAGEFILE" | dd of=/dev/r$DISK bs=64k else dd if="$IMAGEFILE" of=/dev/r$DISK bs=64k fi echo echo "Trying to eject $DISK..." echo cnt=5 while [ $cnt -gt 0 ]; do cnt=$((cnt - 1)) sleep 2 if diskutil eject $DISK; then echo "You can safely remove your $DISK." break fi done