ddimage 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/sh
  2. #BLACKLIST_DEVICES="/dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde"
  3. BLACKLIST_DEVICES="/dev/sda"
  4. # 1MB blocksize
  5. BLOCKSIZE=1048576
  6. function usage() {
  7. echo "Usage: $(basename $0) IMAGE DEVICE"
  8. }
  9. function image_details() {
  10. IMG=$1
  11. echo "Image details"
  12. echo "============="
  13. echo " image: $(stat --printf '%N\n' $IMG)"
  14. echo " size: $(stat -L --printf '%s bytes\n' $IMG)"
  15. echo " modified: $(stat -L --printf '%y\n' $IMG)"
  16. echo " type: $(file -L -b $IMG)"
  17. echo ""
  18. }
  19. function device_details() {
  20. DEV=$1
  21. BLOCK_SIZE=512
  22. echo "Device details"
  23. echo "=============="
  24. echo " device: $DEVICE"
  25. if [ -f "/sys/class/block/$DEV/device/vendor" ]; then
  26. echo " vendor: $(cat /sys/class/block/$DEV/device/vendor)"
  27. else
  28. echo " vendor: UNKOWN"
  29. fi
  30. if [ -f "/sys/class/block/$DEV/device/model" ]; then
  31. echo " model: $(cat /sys/class/block/$DEV/device/model)"
  32. else
  33. echo " model: UNKNOWN"
  34. fi
  35. if [ -f "/sys/class/block/$DEV/size" ]; then
  36. echo " size: $[$(cat /sys/class/block/$DEV/size)*BLOCK_SIZE] bytes"
  37. else
  38. echo " size: UNKNOWN"
  39. fi
  40. echo ""
  41. }
  42. if [ $# -ne 2 ]; then
  43. usage
  44. exit 1
  45. fi
  46. IMAGE=$1
  47. DEVICE=$2
  48. if [ ! -e "$IMAGE" ]; then
  49. echo "ERROR: Image $IMAGE does not exist"
  50. usage
  51. exit 1
  52. fi
  53. if [ "${BLACKLIST_DEVICES/${DEVICE}/ERROR}" != "$BLACKLIST_DEVICES" ]; then
  54. echo "ERROR: Device $DEVICE is blacklisted"
  55. exit 1
  56. fi
  57. if [ ! -w "$DEVICE" ]; then
  58. echo "ERROR: Device $DEVICE does not exist or is not writable"
  59. usage
  60. exit 1
  61. fi
  62. image_details $IMAGE
  63. device_details $(basename $DEVICE)
  64. printf "Write $IMAGE to $DEVICE [y/N]? "
  65. read RESPONSE
  66. if [ "$RESPONSE" != "y" ]; then
  67. echo "Write aborted"
  68. exit 0
  69. fi
  70. echo "Writing image..."
  71. dd if="$IMAGE" of="$DEVICE" bs="$BLOCKSIZE"
  72. sync