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