flash.sh 2.8 KB

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