| 1 | #!/bin/bash |
| 2 | # rp.sdcard.sh |
| 3 | # Preparing an SD card with a bootable image for the Raspberry Pi. |
| 4 | |
| 5 | # $1 = imagefile name |
| 6 | # $2 = sdcard (device file) |
| 7 | # Otherwise defaults to the defaults, see below. |
| 8 | |
| 9 | DEFAULTbs=4M # Block size, 4 mb default. |
| 10 | DEFAULTif="2013-07-26-wheezy-raspbian.img" # Commonly used distro. |
| 11 | DEFAULTsdcard="/dev/mmcblk0" # May be different. Check! |
| 12 | ROOTUSER_NAME=root # Must run as root! |
| 13 | E_NOTROOT=81 |
| 14 | E_NOIMAGE=82 |
| 15 | |
| 16 | username=$(id -nu) # Who is running this script? |
| 17 | if [ "$username" != "$ROOTUSER_NAME" ] |
| 18 | then |
| 19 | echo "This script must run as root or with root privileges." |
| 20 | exit $E_NOTROOT |
| 21 | fi |
| 22 | |
| 23 | if [ -n "$1" ] |
| 24 | then |
| 25 | imagefile="$1" |
| 26 | else |
| 27 | imagefile="$DEFAULTif" |
| 28 | fi |
| 29 | |
| 30 | if [ -n "$2" ] |
| 31 | then |
| 32 | sdcard="$2" |
| 33 | else |
| 34 | sdcard="$DEFAULTsdcard" |
| 35 | fi |
| 36 | |
| 37 | if [ ! -e $imagefile ] |
| 38 | then |
| 39 | echo "Image file \"$imagefile\" not found!" |
| 40 | exit $E_NOIMAGE |
| 41 | fi |
| 42 | |
| 43 | echo "Last chance to change your mind!"; echo |
| 44 | read -s -n1 -p "Hit a key to write $imagefile to $sdcard [Ctl-c to exit]." |
| 45 | echo; echo |
| 46 | |
| 47 | echo "Writing $imagefile to $sdcard ..." |
| 48 | dd bs=$DEFAULTbs if=$imagefile of=$sdcard |
| 49 | |
| 50 | exit $? |
| 51 | |
| 52 | # Exercises: |
| 53 | # --------- |
| 54 | # 1) Provide additional error checking. |
| 55 | # 2) Have script autodetect device file for SD card (difficult!). |
| 56 | # 3) Have script sutodetect image file (*img) in $PWD. |