diff options
author | Paul Eggleton <paul.eggleton@linux.intel.com> | 2019-07-02 16:12:45 +1200 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2019-07-03 17:00:57 +0100 |
commit | d3a9f43305f28ef229fc621ff1273c69a435dec5 (patch) | |
tree | a60f11388f89ef2074ecf5db8894d4761df52764 /scripts | |
parent | 8ff741d0752cae18a0d0921f32011810c67eee81 (diff) | |
download | poky-d3a9f43305f28ef229fc621ff1273c69a435dec5.tar.gz |
scripts/contrib/ddimage: replace blacklist with mount check
The blacklist, whilst previously useful for safety, is now becoming
obsolete - on my current system, the main storage is at /dev/nvme* and
if I plug in a USB stick it shows up as /dev/sdb which was previously
blacklisted. To make this more flexible, remove the blacklist and
instead check if the specified device is mounted, has a partition
that is mounted, or is otherwise in use according to the kernel, and
show an appropriate error and quit if so.
To make this robust, also ensure we handle where the specified device is
a symlink to another device.
(From OE-Core rev: 49043de1a7716ad612fb92a2e8a52e43d253c800)
Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/contrib/ddimage | 80 |
1 files changed, 67 insertions, 13 deletions
diff --git a/scripts/contrib/ddimage b/scripts/contrib/ddimage index 01ff431851..a7dc5f7487 100755 --- a/scripts/contrib/ddimage +++ b/scripts/contrib/ddimage | |||
@@ -3,10 +3,6 @@ | |||
3 | # SPDX-License-Identifier: GPL-2.0-only | 3 | # SPDX-License-Identifier: GPL-2.0-only |
4 | # | 4 | # |
5 | 5 | ||
6 | # Default to avoiding the first two disks on typical Linux and Mac OS installs | ||
7 | # Better safe than sorry :-) | ||
8 | BLACKLIST_DEVICES="/dev/sda /dev/sdb /dev/disk1 /dev/disk2" | ||
9 | |||
10 | # 1MB blocksize | 6 | # 1MB blocksize |
11 | BLOCKSIZE=1048576 | 7 | BLOCKSIZE=1048576 |
12 | 8 | ||
@@ -32,7 +28,6 @@ image_details() { | |||
32 | } | 28 | } |
33 | 29 | ||
34 | device_details() { | 30 | device_details() { |
35 | DEV=$1 | ||
36 | BLOCK_SIZE=512 | 31 | BLOCK_SIZE=512 |
37 | 32 | ||
38 | echo "Device details" | 33 | echo "Device details" |
@@ -45,7 +40,13 @@ device_details() { | |||
45 | fi | 40 | fi |
46 | 41 | ||
47 | # Default / Linux information collection | 42 | # Default / Linux information collection |
48 | echo " device: $DEVICE" | 43 | ACTUAL_DEVICE=`readlink -f $DEVICE` |
44 | DEV=`basename $ACTUAL_DEVICE` | ||
45 | if [ "$ACTUAL_DEVICE" != "$DEVICE" ] ; then | ||
46 | echo " device: $DEVICE -> $ACTUAL_DEVICE" | ||
47 | else | ||
48 | echo " device: $DEVICE" | ||
49 | fi | ||
49 | if [ -f "/sys/class/block/$DEV/device/vendor" ]; then | 50 | if [ -f "/sys/class/block/$DEV/device/vendor" ]; then |
50 | echo " vendor: $(cat /sys/class/block/$DEV/device/vendor)" | 51 | echo " vendor: $(cat /sys/class/block/$DEV/device/vendor)" |
51 | else | 52 | else |
@@ -64,6 +65,49 @@ device_details() { | |||
64 | echo "" | 65 | echo "" |
65 | } | 66 | } |
66 | 67 | ||
68 | check_mount_device() { | ||
69 | if cat /proc/self/mounts | awk '{ print $1 }' | grep /dev/ | grep -q -E "^$1$" ; then | ||
70 | return 0 | ||
71 | fi | ||
72 | return 1 | ||
73 | } | ||
74 | |||
75 | is_mounted() { | ||
76 | if [ "$(uname)" = "Darwin" ]; then | ||
77 | if df | awk '{ print $1 }' | grep /dev/ | grep -q -E "^$1(s[0-9]+)?$" ; then | ||
78 | return 0 | ||
79 | fi | ||
80 | else | ||
81 | if check_mount_device $1 ; then | ||
82 | return 0 | ||
83 | fi | ||
84 | DEV=`basename $1` | ||
85 | if [ -d /sys/class/block/$DEV/ ] ; then | ||
86 | PARENT_BLKDEV=`basename $(readlink -f "/sys/class/block/$DEV/..")` | ||
87 | if [ "$PARENT_BLKDEV" != "block" ] ; then | ||
88 | if check_mount_device $PARENT_BLKDEV ; then | ||
89 | return 0 | ||
90 | fi | ||
91 | fi | ||
92 | for CHILD_BLKDEV in `find /sys/class/block/$DEV/ -mindepth 1 -maxdepth 1 -name "$DEV*" -type d` | ||
93 | do | ||
94 | if check_mount_device /dev/`basename $CHILD_BLKDEV` ; then | ||
95 | return 0 | ||
96 | fi | ||
97 | done | ||
98 | fi | ||
99 | fi | ||
100 | return 1 | ||
101 | } | ||
102 | |||
103 | is_inuse() { | ||
104 | HOLDERS_DIR="/sys/class/block/`basename $1`/holders" | ||
105 | if [ -d $HOLDERS_DIR ] && [ `ls -A $HOLDERS_DIR` ] ; then | ||
106 | return 0 | ||
107 | fi | ||
108 | return 1 | ||
109 | } | ||
110 | |||
67 | if [ $# -ne 2 ]; then | 111 | if [ $# -ne 2 ]; then |
68 | usage | 112 | usage |
69 | exit 1 | 113 | exit 1 |
@@ -78,13 +122,23 @@ if [ ! -e "$IMAGE" ]; then | |||
78 | exit 1 | 122 | exit 1 |
79 | fi | 123 | fi |
80 | 124 | ||
125 | if [ "$(uname)" = "Darwin" ]; then | ||
126 | # readlink doesn't support -f on MacOS, just assume it isn't a symlink | ||
127 | ACTUAL_DEVICE=$DEVICE | ||
128 | else | ||
129 | ACTUAL_DEVICE=`readlink -f $DEVICE` | ||
130 | fi | ||
131 | if is_mounted $ACTUAL_DEVICE ; then | ||
132 | echo "ERROR: Device $DEVICE is currently mounted - check if this is the right device, and unmount it first if so" | ||
133 | device_details | ||
134 | exit 1 | ||
135 | fi | ||
136 | if is_inuse $ACTUAL_DEVICE ; then | ||
137 | echo "ERROR: Device $DEVICE is currently in use (possibly part of LVM) - check if this is the right device!" | ||
138 | device_details | ||
139 | exit 1 | ||
140 | fi | ||
81 | 141 | ||
82 | for i in ${BLACKLIST_DEVICES}; do | ||
83 | if [ "$i" = "$DEVICE" ]; then | ||
84 | echo "ERROR: Device $DEVICE is blacklisted" | ||
85 | exit 1 | ||
86 | fi | ||
87 | done | ||
88 | 142 | ||
89 | if [ ! -w "$DEVICE" ]; then | 143 | if [ ! -w "$DEVICE" ]; then |
90 | echo "ERROR: Device $DEVICE does not exist or is not writable" | 144 | echo "ERROR: Device $DEVICE does not exist or is not writable" |
@@ -93,7 +147,7 @@ if [ ! -w "$DEVICE" ]; then | |||
93 | fi | 147 | fi |
94 | 148 | ||
95 | image_details $IMAGE | 149 | image_details $IMAGE |
96 | device_details $(basename $DEVICE) | 150 | device_details |
97 | 151 | ||
98 | printf "Write $IMAGE to $DEVICE [y/N]? " | 152 | printf "Write $IMAGE to $DEVICE [y/N]? " |
99 | read RESPONSE | 153 | read RESPONSE |