| 1 | #!/bin/bashscip-shell filesystem . . `/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/mmcblk0scip-shell filesystem . . `/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=$(idscip-shell system . . id. -nu) # Who is running this script? |
| 17 | if [scip-shell system . . `[`. "$username" != "$ROOTUSER_NAME" ] |
| 18 | then |
| 19 | echoscip-shell system . . echo. "This script must run as root or with root privileges." |
| 20 | exit $E_NOTROOT |
| 21 | fi |
| 22 | |
| 23 | if [scip-shell system . . `[`. -n "$1" ] |
| 24 | then |
| 25 | imagefile="$1" |
| 26 | else |
| 27 | imagefile="$DEFAULTif" |
| 28 | fi |
| 29 | |
| 30 | if [scip-shell system . . `[`. -n "$2" ] |
| 31 | then |
| 32 | sdcard="$2" |
| 33 | else |
| 34 | sdcard="$DEFAULTsdcard" |
| 35 | fi |
| 36 | |
| 37 | if [scip-shell system . . `[`. ! -e $imagefile ] |
| 38 | then |
| 39 | echoscip-shell system . . echo. "Image file \"$imagefile\" not found!" |
| 40 | exit $E_NOIMAGE |
| 41 | fi |
| 42 | |
| 43 | echoscip-shell system . . echo. "Last chance to change your mind!"; echoscip-shell system . . echo. |
| 44 | read -s -n1 -p "Hit a key to write $imagefile to $sdcard [Ctl-c to exit]." |
| 45 | echoscip-shell system . . echo.; echoscip-shell system . . echo. |
| 46 | |
| 47 | echoscip-shell system . . echo. "Writing $imagefile to $sdcard ..." |
| 48 | ddscip-shell system . . 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. |