blob: ab929957a5a2bf76452c6856fa648befd8f7d6d6 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/bin/sh
2
3# Default to avoiding the first two disks on typical Linux and Mac OS installs
4# Better safe than sorry :-)
5BLACKLIST_DEVICES="/dev/sda /dev/sdb /dev/disk1 /dev/disk2"
6
7# 1MB blocksize
8BLOCKSIZE=1048576
9
10usage() {
11 echo "Usage: $(basename $0) IMAGE DEVICE"
12}
13
14image_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
31device_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
64if [ $# -ne 2 ]; then
65 usage
66 exit 1
67fi
68
69IMAGE=$1
70DEVICE=$2
71
72if [ ! -e "$IMAGE" ]; then
73 echo "ERROR: Image $IMAGE does not exist"
74 usage
75 exit 1
76fi
77
78
79for i in ${BLACKLIST_DEVICES}; do
80 if [ "$i" = "$DEVICE" ]; then
81 echo "ERROR: Device $DEVICE is blacklisted"
82 exit 1
83 fi
84done
85
86if [ ! -w "$DEVICE" ]; then
87 echo "ERROR: Device $DEVICE does not exist or is not writable"
88 usage
89 exit 1
90fi
91
92image_details $IMAGE
93device_details $(basename $DEVICE)
94
95printf "Write $IMAGE to $DEVICE [y/N]? "
96read RESPONSE
97if [ "$RESPONSE" != "y" ]; then
98 echo "Write aborted"
99 exit 0
100fi
101
102echo "Writing image..."
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600103if which pv >/dev/null 2>&1; then
104 pv "$IMAGE" | dd of="$DEVICE" bs="$BLOCKSIZE"
105else
106 dd if="$IMAGE" of="$DEVICE" bs="$BLOCKSIZE"
107fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108sync