ddimage 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/bin/sh
  2. # Default to avoiding the first two disks on typical Linux and Mac OS installs
  3. # Better safe than sorry :-)
  4. BLACKLIST_DEVICES="/dev/sda /dev/sdb /dev/disk1 /dev/disk2"
  5. # 1MB blocksize
  6. BLOCKSIZE=1048576
  7. usage() {
  8. echo "Usage: $(basename $0) IMAGE DEVICE"
  9. }
  10. image_details() {
  11. IMG=$1
  12. echo "Image details"
  13. echo "============="
  14. echo " image: $(basename $IMG)"
  15. # stat format is different on Mac OS and Linux
  16. if [ "$(uname)" = "Darwin" ]; then
  17. echo " size: $(stat -L -f '%z bytes' $IMG)"
  18. echo " modified: $(stat -L -f '%Sm' $IMG)"
  19. else
  20. echo " size: $(stat -L -c '%s bytes' $IMG)"
  21. echo " modified: $(stat -L -c '%y' $IMG)"
  22. fi
  23. echo " type: $(file -L -b $IMG)"
  24. echo ""
  25. }
  26. device_details() {
  27. DEV=$1
  28. BLOCK_SIZE=512
  29. echo "Device details"
  30. echo "=============="
  31. # Collect disk info using diskutil on Mac OS
  32. if [ "$(uname)" = "Darwin" ]; then
  33. diskutil info $DEVICE | egrep "(Device Node|Media Name|Total Size)"
  34. return
  35. fi
  36. # Default / Linux information collection
  37. echo " device: $DEVICE"
  38. if [ -f "/sys/class/block/$DEV/device/vendor" ]; then
  39. echo " vendor: $(cat /sys/class/block/$DEV/device/vendor)"
  40. else
  41. echo " vendor: UNKOWN"
  42. fi
  43. if [ -f "/sys/class/block/$DEV/device/model" ]; then
  44. echo " model: $(cat /sys/class/block/$DEV/device/model)"
  45. else
  46. echo " model: UNKNOWN"
  47. fi
  48. if [ -f "/sys/class/block/$DEV/size" ]; then
  49. echo " size: $(($(cat /sys/class/block/$DEV/size) * $BLOCK_SIZE)) bytes"
  50. else
  51. echo " size: UNKNOWN"
  52. fi
  53. echo ""
  54. }
  55. if [ $# -ne 2 ]; then
  56. usage
  57. exit 1
  58. fi
  59. IMAGE=$1
  60. DEVICE=$2
  61. if [ ! -e "$IMAGE" ]; then
  62. echo "ERROR: Image $IMAGE does not exist"
  63. usage
  64. exit 1
  65. fi
  66. for i in ${BLACKLIST_DEVICES}; do
  67. if [ "$i" = "$DEVICE" ]; then
  68. echo "ERROR: Device $DEVICE is blacklisted"
  69. exit 1
  70. fi
  71. done
  72. if [ ! -w "$DEVICE" ]; then
  73. echo "ERROR: Device $DEVICE does not exist or is not writable"
  74. usage
  75. exit 1
  76. fi
  77. image_details $IMAGE
  78. device_details $(basename $DEVICE)
  79. printf "Write $IMAGE to $DEVICE [y/N]? "
  80. read RESPONSE
  81. if [ "$RESPONSE" != "y" ]; then
  82. echo "Write aborted"
  83. exit 0
  84. fi
  85. echo "Writing image..."
  86. if which pv >/dev/null 2>&1; then
  87. pv "$IMAGE" | dd of="$DEVICE" bs="$BLOCKSIZE"
  88. else
  89. dd if="$IMAGE" of="$DEVICE" bs="$BLOCKSIZE"
  90. fi
  91. sync