summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2019-05-27 16:58:01 +0200
committerSimon Rettberg2019-05-27 16:58:01 +0200
commit58a01fd257b6f190d6a44662fbd78944fbc608aa (patch)
tree9c92b4b9f7bf6acf4b710a29e56ca638db73a09b
parentMatch proper field (diff)
downloadslx-tools-58a01fd257b6f190d6a44662fbd78944fbc608aa.tar.gz
slx-tools-58a01fd257b6f190d6a44662fbd78944fbc608aa.tar.xz
slx-tools-58a01fd257b6f190d6a44662fbd78944fbc608aa.zip
Modularize
-rw-r--r--.gitignore1
-rwxr-xr-xmake.sh30
-rw-r--r--modules/dev.inc49
-rw-r--r--modules/fs/path.inc (renamed from slx-tools)70
-rw-r--r--modules/regex.inc17
-rw-r--r--slx-tools.template23
6 files changed, 121 insertions, 69 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..1b2f329
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/slx-tools
diff --git a/make.sh b/make.sh
new file mode 100755
index 0000000..a8a4760
--- /dev/null
+++ b/make.sh
@@ -0,0 +1,30 @@
+#!/bin/bash
+
+if [ -z "$1" ]; then
+ echo "Pass abolute path to where slx-tools will be located"
+ exit 1
+fi
+
+declare -rg BASE="$1"
+
+shopt -s globstar
+set -e
+
+
+DEFS=
+
+for file in modules/**/*.inc; do
+ name="${file#modules/}"
+ name="${name//\//_}"
+ name="${name%.inc}"
+ [[ "$name" =~ ^[a-z0-9_]+$ ]] || continue
+ echo " * Handling $file"
+ for var in $( grep -oP '^[a-z0-9_]+\s*\(\)' "$file" | grep -oE '^[a-z0-9_]+' ); do
+ echo "Found $var"
+ DEFS+="$var() { . \$BASE/$file; $var "'"$@"; }\n'
+ done
+done
+
+sed "s#%DEFS%#${DEFS}#;s#^BASE=.*\$#BASE='$BASE'#" slx-tools.template > slx-tools
+chmod +x slx-tools
+
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
+}
+
diff --git a/slx-tools b/modules/fs/path.inc
index 1a95fa1..f3a041b 100644
--- a/slx-tools
+++ b/modules/fs/path.inc
@@ -1,12 +1,5 @@
#!/bin/ash
-# Collection of small bash functions utilized in various scripts.
-################################################################################
-case "$PATH" in
- *opt/openslx*) ;;
- *) export PATH="$PATH:/opt/openslx/bin:/opt/openslx/sbin" ;;
-esac
-
DM_STATE_FILE="/run/openslx/dmsetup.state"
readonly DM_STATE_FILE
@@ -21,6 +14,7 @@ fs_path_getdev() {
}
# Return base mount point of given path
+# eg. call with "/mnt/nfs/temp/data", returns "/mnt/nfs"
fs_path_getmountpoint() {
local _mp
_mp="$( df -P "$1" | awk '$6 ~ "^/" {print $6}' )"
@@ -116,65 +110,3 @@ fs_path_space() {
printf "%s" "$_free $_total"
}
-# Match $1 against perl regex $2, case insensitive
-regex_imatch() {
- printf "%s" "$1" | grep -qPi "$2"
-}
-
-# Escapes $1 for use in grep -E/-P, sed -r, etc.
-# Pass --slash to escape '/' too. (e.g. for sed)
-regex_escape() {
- if [ "x$1" = "x--slash" ]; then
- printf "%s" "$2" | sed 's,[][()\.^$?*+/],\\&,g'
- else
- printf "%s" "$1" | sed 's/[][()\.^$?*+]/\\&/g'
- fi
-}
-
-# 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
-}
-
diff --git a/modules/regex.inc b/modules/regex.inc
new file mode 100644
index 0000000..21c3d65
--- /dev/null
+++ b/modules/regex.inc
@@ -0,0 +1,17 @@
+#!/bin/ash
+
+# Match $1 against perl regex $2, case insensitive
+regex_imatch() {
+ printf "%s" "$1" | grep -qPi "$2"
+}
+
+# Escapes $1 for use in grep -E/-P, sed -r, etc.
+# Pass --slash to escape '/' too. (e.g. for sed)
+regex_escape() {
+ if [ "x$1" = "x--slash" ]; then
+ printf "%s" "$2" | sed 's,[][()\.^$?*+/],\\&,g'
+ else
+ printf "%s" "$1" | sed 's/[][()\.^$?*+]/\\&/g'
+ fi
+}
+
diff --git a/slx-tools.template b/slx-tools.template
new file mode 100644
index 0000000..0fb99b3
--- /dev/null
+++ b/slx-tools.template
@@ -0,0 +1,23 @@
+#!/bin/ash
+
+# Collection of small ash functions utilized in various scripts.
+################################################################################
+
+case "$PATH" in
+ *opt/openslx*) ;;
+ *) export PATH="$PATH:/opt/openslx/bin:/opt/openslx/sbin" ;;
+esac
+
+BASE=.
+
+%DEFS%
+
+if [ "${0##*/}" = "slx-tools" ]; then
+ if [ $# -ge 1 ]; then
+ "$@"
+ else
+ echo "slx-tools [function] [args...]"
+ fi
+else
+ :
+fi