blob: 70eee8ebea2cd4e61b230ef21ad5dbc8b8b114d2 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/bin/sh
Brad Bishopc342db32019-05-15 21:57:59 -04002#
Patrick Williams92b42cb2022-09-03 06:53:57 -05003# Copyright OpenEmbedded Contributors
4#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-only
6#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008# 1MB blocksize
9BLOCKSIZE=1048576
10
11usage() {
12 echo "Usage: $(basename $0) IMAGE DEVICE"
13}
14
15image_details() {
16 IMG=$1
17 echo "Image details"
18 echo "============="
19 echo " image: $(basename $IMG)"
20 # stat format is different on Mac OS and Linux
21 if [ "$(uname)" = "Darwin" ]; then
22 echo " size: $(stat -L -f '%z bytes' $IMG)"
23 echo " modified: $(stat -L -f '%Sm' $IMG)"
24 else
25 echo " size: $(stat -L -c '%s bytes' $IMG)"
26 echo " modified: $(stat -L -c '%y' $IMG)"
27 fi
28 echo " type: $(file -L -b $IMG)"
29 echo ""
30}
31
32device_details() {
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033 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
Brad Bishop96ff1982019-08-19 13:50:42 -040045 ACTUAL_DEVICE=`readlink -f $DEVICE`
46 DEV=`basename $ACTUAL_DEVICE`
47 if [ "$ACTUAL_DEVICE" != "$DEVICE" ] ; then
48 echo " device: $DEVICE -> $ACTUAL_DEVICE"
49 else
50 echo " device: $DEVICE"
51 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052 if [ -f "/sys/class/block/$DEV/device/vendor" ]; then
53 echo " vendor: $(cat /sys/class/block/$DEV/device/vendor)"
54 else
Brad Bishop96ff1982019-08-19 13:50:42 -040055 echo " vendor: UNKNOWN"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056 fi
57 if [ -f "/sys/class/block/$DEV/device/model" ]; then
58 echo " model: $(cat /sys/class/block/$DEV/device/model)"
59 else
60 echo " model: UNKNOWN"
61 fi
62 if [ -f "/sys/class/block/$DEV/size" ]; then
63 echo " size: $(($(cat /sys/class/block/$DEV/size) * $BLOCK_SIZE)) bytes"
64 else
65 echo " size: UNKNOWN"
66 fi
67 echo ""
68}
69
Brad Bishop96ff1982019-08-19 13:50:42 -040070check_mount_device() {
71 if cat /proc/self/mounts | awk '{ print $1 }' | grep /dev/ | grep -q -E "^$1$" ; then
72 return 0
73 fi
74 return 1
75}
76
77is_mounted() {
78 if [ "$(uname)" = "Darwin" ]; then
79 if df | awk '{ print $1 }' | grep /dev/ | grep -q -E "^$1(s[0-9]+)?$" ; then
80 return 0
81 fi
82 else
83 if check_mount_device $1 ; then
84 return 0
85 fi
86 DEV=`basename $1`
87 if [ -d /sys/class/block/$DEV/ ] ; then
88 PARENT_BLKDEV=`basename $(readlink -f "/sys/class/block/$DEV/..")`
89 if [ "$PARENT_BLKDEV" != "block" ] ; then
90 if check_mount_device $PARENT_BLKDEV ; then
91 return 0
92 fi
93 fi
94 for CHILD_BLKDEV in `find /sys/class/block/$DEV/ -mindepth 1 -maxdepth 1 -name "$DEV*" -type d`
95 do
96 if check_mount_device /dev/`basename $CHILD_BLKDEV` ; then
97 return 0
98 fi
99 done
100 fi
101 fi
102 return 1
103}
104
105is_inuse() {
106 HOLDERS_DIR="/sys/class/block/`basename $1`/holders"
107 if [ -d $HOLDERS_DIR ] && [ `ls -A $HOLDERS_DIR` ] ; then
108 return 0
109 fi
110 return 1
111}
112
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113if [ $# -ne 2 ]; then
114 usage
115 exit 1
116fi
117
118IMAGE=$1
119DEVICE=$2
120
121if [ ! -e "$IMAGE" ]; then
122 echo "ERROR: Image $IMAGE does not exist"
123 usage
124 exit 1
125fi
126
Brad Bishop96ff1982019-08-19 13:50:42 -0400127if [ ! -e "$DEVICE" ]; then
128 echo "ERROR: Device $DEVICE does not exist"
129 usage
130 exit 1
131fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132
Brad Bishop96ff1982019-08-19 13:50:42 -0400133if [ "$(uname)" = "Darwin" ]; then
134 # readlink doesn't support -f on MacOS, just assume it isn't a symlink
135 ACTUAL_DEVICE=$DEVICE
136else
137 ACTUAL_DEVICE=`readlink -f $DEVICE`
138fi
139if is_mounted $ACTUAL_DEVICE ; then
140 echo "ERROR: Device $DEVICE is currently mounted - check if this is the right device, and unmount it first if so"
141 device_details
142 exit 1
143fi
144if is_inuse $ACTUAL_DEVICE ; then
145 echo "ERROR: Device $DEVICE is currently in use (possibly part of LVM) - check if this is the right device!"
146 device_details
147 exit 1
148fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500149
150if [ ! -w "$DEVICE" ]; then
Brad Bishop96ff1982019-08-19 13:50:42 -0400151 echo "ERROR: Device $DEVICE is not writable - possibly use sudo?"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500152 usage
153 exit 1
154fi
155
156image_details $IMAGE
Brad Bishop96ff1982019-08-19 13:50:42 -0400157device_details
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500158
159printf "Write $IMAGE to $DEVICE [y/N]? "
160read RESPONSE
161if [ "$RESPONSE" != "y" ]; then
162 echo "Write aborted"
163 exit 0
164fi
165
166echo "Writing image..."
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600167if which pv >/dev/null 2>&1; then
168 pv "$IMAGE" | dd of="$DEVICE" bs="$BLOCKSIZE"
169else
170 dd if="$IMAGE" of="$DEVICE" bs="$BLOCKSIZE"
171fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500172sync