summaryrefslogtreecommitdiffstats
path: root/modules/dev.inc
diff options
context:
space:
mode:
Diffstat (limited to 'modules/dev.inc')
-rw-r--r--modules/dev.inc49
1 files changed, 49 insertions, 0 deletions
diff --git a/modules/dev.inc b/modules/dev.inc
new file mode 100644
index 0000000..c2046d3
--- /dev/null
+++ b/modules/dev.inc
@@ -0,0 +1,49 @@
+#!/bin/ash
+
+# Get all partitions with given id (list of /dev/sdXX)
+# Works for MBR/DOS by looking at the type (1 byte)
+# and for GPT by looking for the label 'OpenSLX-ID$1'
+# in case an id was given, or with the given UUID,
+# or with the given name.
+# The output will be a list of matching devices,
+# sorted from largest to smallest.
+dev_find_partitions() {
+ local ID dev exp target
+ exp=
+ # target for the scan, defaults to /dev to check everything
+ if [ -b "$1" ]; then
+ target="$1"
+ shift
+ elif [ -d "$1" ]; then
+ target="$1/"
+ else
+ target="/dev/"
+ fi
+ while [ "$#" -gt 0 ]; do
+ ID="$1"
+ shift
+ [ -z "$ID" ] && continue
+ # if single digit, e.g. 7, look for 0x7 and 0x07
+ if regex_imatch "$ID" "^[0-9a-f]$"; then
+ ID="0?$ID"
+ fi
+
+ if regex_imatch "$ID" "^[0-9a-f?]{2,3}$"; then # Match two digit and the expanded three digit version from above
+ # if double digit look for MBR types and OpenSLX-ID$ID GPT labels
+ exp="$exp|ID_PART_ENTRY_(NAME=OpenSLX-ID|TYPE=0x)$ID"
+ elif regex_imatch "$ID" "^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$"; then
+ # if UUID, look for TYPE
+ exp="$exp|ID_PART_ENTRY_TYPE=$ID"
+ else
+ # something else, look for names of partitions / filesystems
+ ID="$( regex_escape "$ID" )"
+ exp="$exp|ID_(PART_ENTRY_NAME|FS_LABEL)=$ID"
+ fi
+ done
+ exp="${exp:1}"
+ for dev in $(find $target* -type b); do
+ udevadm info --name="$dev" | grep -iqE "($exp)\$" \
+ && printf "%s\n" "$(blockdev --getsize64 "$dev") $dev"
+ done | sort -n -k1 -r | cut -d' ' -f2
+}
+