diff options
Diffstat (limited to 'scripts/contrib/ddimage')
-rwxr-xr-x | scripts/contrib/ddimage | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/scripts/contrib/ddimage b/scripts/contrib/ddimage new file mode 100755 index 0000000000..93ebeafc31 --- /dev/null +++ b/scripts/contrib/ddimage | |||
@@ -0,0 +1,89 @@ | |||
1 | #!/bin/sh | ||
2 | |||
3 | #BLACKLIST_DEVICES="/dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde" | ||
4 | BLACKLIST_DEVICES="/dev/sda" | ||
5 | |||
6 | # 1MB blocksize | ||
7 | BLOCKSIZE=1048576 | ||
8 | |||
9 | usage() { | ||
10 | echo "Usage: $(basename $0) IMAGE DEVICE" | ||
11 | } | ||
12 | |||
13 | image_details() { | ||
14 | IMG=$1 | ||
15 | echo "Image details" | ||
16 | echo "=============" | ||
17 | echo " image: $(stat --printf '%N\n' $IMG)" | ||
18 | echo " size: $(stat -L --printf '%s bytes\n' $IMG)" | ||
19 | echo " modified: $(stat -L --printf '%y\n' $IMG)" | ||
20 | echo " type: $(file -L -b $IMG)" | ||
21 | echo "" | ||
22 | } | ||
23 | |||
24 | device_details() { | ||
25 | DEV=$1 | ||
26 | BLOCK_SIZE=512 | ||
27 | |||
28 | echo "Device details" | ||
29 | echo "==============" | ||
30 | echo " device: $DEVICE" | ||
31 | if [ -f "/sys/class/block/$DEV/device/vendor" ]; then | ||
32 | echo " vendor: $(cat /sys/class/block/$DEV/device/vendor)" | ||
33 | else | ||
34 | echo " vendor: UNKOWN" | ||
35 | fi | ||
36 | if [ -f "/sys/class/block/$DEV/device/model" ]; then | ||
37 | echo " model: $(cat /sys/class/block/$DEV/device/model)" | ||
38 | else | ||
39 | echo " model: UNKNOWN" | ||
40 | fi | ||
41 | if [ -f "/sys/class/block/$DEV/size" ]; then | ||
42 | echo " size: $(($(cat /sys/class/block/$DEV/size) * $BLOCK_SIZE)) bytes" | ||
43 | else | ||
44 | echo " size: UNKNOWN" | ||
45 | fi | ||
46 | echo "" | ||
47 | } | ||
48 | |||
49 | if [ $# -ne 2 ]; then | ||
50 | usage | ||
51 | exit 1 | ||
52 | fi | ||
53 | |||
54 | IMAGE=$1 | ||
55 | DEVICE=$2 | ||
56 | |||
57 | if [ ! -e "$IMAGE" ]; then | ||
58 | echo "ERROR: Image $IMAGE does not exist" | ||
59 | usage | ||
60 | exit 1 | ||
61 | fi | ||
62 | |||
63 | |||
64 | for i in ${BLACKLIST_DEVICES}; do | ||
65 | if [ "$i" = "$DEVICE" ]; then | ||
66 | echo "ERROR: Device $DEVICE is blacklisted" | ||
67 | exit 1 | ||
68 | fi | ||
69 | done | ||
70 | |||
71 | if [ ! -w "$DEVICE" ]; then | ||
72 | echo "ERROR: Device $DEVICE does not exist or is not writable" | ||
73 | usage | ||
74 | exit 1 | ||
75 | fi | ||
76 | |||
77 | image_details $IMAGE | ||
78 | device_details $(basename $DEVICE) | ||
79 | |||
80 | printf "Write $IMAGE to $DEVICE [y/N]? " | ||
81 | read RESPONSE | ||
82 | if [ "$RESPONSE" != "y" ]; then | ||
83 | echo "Write aborted" | ||
84 | exit 0 | ||
85 | fi | ||
86 | |||
87 | echo "Writing image..." | ||
88 | dd if="$IMAGE" of="$DEVICE" bs="$BLOCKSIZE" | ||
89 | sync | ||