summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/contrib/ddimage80
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 :-)
8BLACKLIST_DEVICES="/dev/sda /dev/sdb /dev/disk1 /dev/disk2"
9
10# 1MB blocksize 6# 1MB blocksize
11BLOCKSIZE=1048576 7BLOCKSIZE=1048576
12 8
@@ -32,7 +28,6 @@ image_details() {
32} 28}
33 29
34device_details() { 30device_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
68check_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
75is_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
103is_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
67if [ $# -ne 2 ]; then 111if [ $# -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
79fi 123fi
80 124
125if [ "$(uname)" = "Darwin" ]; then
126 # readlink doesn't support -f on MacOS, just assume it isn't a symlink
127 ACTUAL_DEVICE=$DEVICE
128else
129 ACTUAL_DEVICE=`readlink -f $DEVICE`
130fi
131if 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
135fi
136if 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
140fi
81 141
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 142
89if [ ! -w "$DEVICE" ]; then 143if [ ! -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
93fi 147fi
94 148
95image_details $IMAGE 149image_details $IMAGE
96device_details $(basename $DEVICE) 150device_details
97 151
98printf "Write $IMAGE to $DEVICE [y/N]? " 152printf "Write $IMAGE to $DEVICE [y/N]? "
99read RESPONSE 153read RESPONSE