flash.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/sh
  2. # Flash usb.img onto a USB stick.
  3. #
  4. # make flash USB=/dev/sdX (re-runs itself through sudo if needed)
  5. # make flash USB=/dev/sdX FORCE=1 (skip the USB/removable device check)
  6. #
  7. # Refuses to touch partitioned devices or devices with mounted filesystems,
  8. # prints a summary and needs "yes" before writing anything.
  9. set -eu
  10. # Third argument (or IMG= in the environment) selects the image. Passing it
  11. # as an argument keeps it alive across the sudo re-exec below.
  12. IMG=${3:-${IMG:-usb.img}}
  13. dev=${1:-}
  14. force=${2:-}
  15. if [ -z "$dev" ]; then
  16. echo "usage: make flash USB=/dev/sdX [FORCE=1] [IMAGE]" >&2
  17. exit 1
  18. fi
  19. if [ ! -f "$IMG" ]; then
  20. echo "flash: $IMG not found (run 'make usb.img' first)" >&2
  21. exit 1
  22. fi
  23. if [ ! -b "$dev" ]; then
  24. echo "flash: $dev: not a block device" >&2
  25. exit 1
  26. fi
  27. command -v lsblk >/dev/null 2>&1 || { echo "flash: lsblk not found" >&2; exit 1; }
  28. dev_type=$(lsblk -ndo TYPE "$dev" | tr -d ' ')
  29. rm_flag=$(lsblk -ndo RM "$dev" | tr -d ' ')
  30. tran=$(lsblk -ndo TRAN "$dev" | tr -d ' ')
  31. model=$(lsblk -ndo MODEL "$dev" | tr -s ' ' | sed 's/^ *//;s/ *$//')
  32. dev_size=$(lsblk -bndo SIZE "$dev" | tr -d ' ')
  33. img_size=$(stat -c%s "$IMG")
  34. case "$dev_type" in
  35. part)
  36. echo "flash: $dev is a partition; pass the whole disk (e.g. /dev/sdb)" >&2
  37. exit 1
  38. ;;
  39. esac
  40. if [ "$force" != "1" ] && [ "$tran" != "usb" ] && [ "$rm_flag" != "1" ]; then
  41. echo "flash: $dev does not look like a USB/removable device (TRAN=$tran RM=$rm_flag)" >&2
  42. echo "flash: pass FORCE=1 if you really mean it" >&2
  43. exit 1
  44. fi
  45. mounted=$(lsblk -nro MOUNTPOINT "$dev" | sed '/^$/d')
  46. if [ -n "$mounted" ]; then
  47. echo "flash: $dev has mounted filesystems:" >&2
  48. echo "$mounted" | sed 's/^/ /' >&2
  49. echo "flash: unmount them first (e.g. sudo umount $dev?*)" >&2
  50. exit 1
  51. fi
  52. if [ "$dev_size" -eq 0 ]; then
  53. echo "flash: $dev reports 0 bytes - the kernel sees no medium" >&2
  54. echo "flash: the stick probably dropped off the bus; replug it into a direct" >&2
  55. echo "flash: port (not a hub) and watch 'lsblk' / 'journalctl -k -f'" >&2
  56. exit 1
  57. fi
  58. if [ "$dev_size" -lt "$img_size" ]; then
  59. echo "flash: $dev ($dev_size bytes) is smaller than $IMG ($img_size bytes)" >&2
  60. exit 1
  61. fi
  62. # dd needs root; re-exec through sudo if we are not root yet.
  63. if [ "$(id -u)" -ne 0 ]; then
  64. if command -v sudo >/dev/null 2>&1; then
  65. echo "flash: re-running with sudo..."
  66. exec sudo "$0" "$@"
  67. fi
  68. echo "flash: must run as root (no sudo found)" >&2
  69. exit 1
  70. fi
  71. echo "About to write $IMG ($img_size bytes) to:"
  72. echo " device: $dev"
  73. echo " size: $dev_size bytes"
  74. echo " model: ${model:-unknown}"
  75. echo " bus: ${tran:-unknown} (removable=$rm_flag)"
  76. printf 'ALL DATA ON %s WILL BE DESTROYED. Type "yes" to continue: ' "$dev"
  77. read -r answer
  78. if [ "$answer" != "yes" ]; then
  79. echo "flash: aborted"
  80. exit 1
  81. fi
  82. echo "flash: writing..."
  83. dd if="$IMG" of="$dev" bs=4M conv=fsync status=progress
  84. echo "flash: syncing..."
  85. sync
  86. printf 'flash: verifying... '
  87. if head -c "$img_size" "$dev" | cmp -s - "$IMG"; then
  88. echo "OK"
  89. else
  90. echo "MISMATCH" >&2
  91. exit 1
  92. fi
  93. echo "flash: done. Safe to remove $dev."