Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/bin/sh |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 2 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 3 | # Copyright OpenEmbedded Contributors |
| 4 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 5 | # SPDX-License-Identifier: GPL-2.0-only |
| 6 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | # 1MB blocksize |
| 9 | BLOCKSIZE=1048576 |
| 10 | |
| 11 | usage() { |
| 12 | echo "Usage: $(basename $0) IMAGE DEVICE" |
| 13 | } |
| 14 | |
| 15 | image_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 | |
| 32 | device_details() { |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 45 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | if [ -f "/sys/class/block/$DEV/device/vendor" ]; then |
| 53 | echo " vendor: $(cat /sys/class/block/$DEV/device/vendor)" |
| 54 | else |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 55 | echo " vendor: UNKNOWN" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | 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 Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 70 | check_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 | |
| 77 | is_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 | |
| 105 | is_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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 113 | if [ $# -ne 2 ]; then |
| 114 | usage |
| 115 | exit 1 |
| 116 | fi |
| 117 | |
| 118 | IMAGE=$1 |
| 119 | DEVICE=$2 |
| 120 | |
| 121 | if [ ! -e "$IMAGE" ]; then |
| 122 | echo "ERROR: Image $IMAGE does not exist" |
| 123 | usage |
| 124 | exit 1 |
| 125 | fi |
| 126 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 127 | if [ ! -e "$DEVICE" ]; then |
| 128 | echo "ERROR: Device $DEVICE does not exist" |
| 129 | usage |
| 130 | exit 1 |
| 131 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 132 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 133 | if [ "$(uname)" = "Darwin" ]; then |
| 134 | # readlink doesn't support -f on MacOS, just assume it isn't a symlink |
| 135 | ACTUAL_DEVICE=$DEVICE |
| 136 | else |
| 137 | ACTUAL_DEVICE=`readlink -f $DEVICE` |
| 138 | fi |
| 139 | if 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 |
| 143 | fi |
| 144 | if 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 |
| 148 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 149 | |
| 150 | if [ ! -w "$DEVICE" ]; then |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 151 | echo "ERROR: Device $DEVICE is not writable - possibly use sudo?" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 152 | usage |
| 153 | exit 1 |
| 154 | fi |
| 155 | |
| 156 | image_details $IMAGE |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 157 | device_details |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 158 | |
| 159 | printf "Write $IMAGE to $DEVICE [y/N]? " |
| 160 | read RESPONSE |
| 161 | if [ "$RESPONSE" != "y" ]; then |
| 162 | echo "Write aborted" |
| 163 | exit 0 |
| 164 | fi |
| 165 | |
| 166 | echo "Writing image..." |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 167 | if which pv >/dev/null 2>&1; then |
| 168 | pv "$IMAGE" | dd of="$DEVICE" bs="$BLOCKSIZE" |
| 169 | else |
| 170 | dd if="$IMAGE" of="$DEVICE" bs="$BLOCKSIZE" |
| 171 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 172 | sync |