Last active
May 6, 2018 22:48
-
-
Save mdaffin/8833587fef466c002c98419622bcce7d to your computer and use it in GitHub Desktop.
A simple wrapper around ddrescue that reduces the error involved in burning images to SD cards.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Usage: ./flasher path/to/rpi.img | |
# | |
# A interactive wrapper around ddrescue that offers a menu to select which | |
# removable media to write the given image to. This makes it easier and | |
# safer to flash images to SD Cards then simply using dd. | |
# | |
set -uo pipefail | |
trap 's=$?; echo "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR | |
IFS=$'\n\t' | |
all_drives() { | |
/usr/bin/ls -d1 /sys/block/* | grep -v "/sys/block/loop" | |
} | |
removable_drives() { | |
for drive in $(all_drives); do | |
if [[ "$(cat "$drive/removable")" == "1" ]]; then | |
echo "$drive" | |
fi | |
done | |
} | |
device_label() { | |
local vendor="$(cat "$1/device/vendor")" | |
local model="$(cat "$1/device/model")" | |
local size_bytes="$(expr "$(cat "$1/size")" "*" 512)" | |
local size="$(numfmt --to=iec-i --suffix=B --padding=7 "${size_bytes}")" | |
echo "$vendor $model $size" | |
} | |
dev_file() { | |
echo "/dev/$(basename "$1")" | |
} | |
menu() { | |
for drive in $(removable_drives); do | |
dev_file "$drive" | |
local size_bytes="$(expr "$(cat "$drive/size")" "*" 512)" | |
local size="$(numfmt --to=iec-i --suffix=B --padding=7 "${size_bytes}")" | |
device_label "$drive" | |
done | |
} | |
clean_exit() { | |
clear | |
exit $@ | |
} | |
image="${1?"missing image"}" | |
device="$(dialog --stdout --clear --menu "Select a drive" 0 0 0 $(menu))" || clean_exit 1 | |
clear | |
sudo ddrescue --force "${image}" "${device}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment