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