summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan Bauer2019-02-18 12:24:51 +0100
committerJonathan Bauer2019-02-18 12:24:51 +0100
commitf312ec055e6e374453c241a870dab04fb826ff99 (patch)
treea9519e86e91555413c309382cfcc2152ea597d74
parentinitial commit (diff)
downloadslx-tools-f312ec055e6e374453c241a870dab04fb826ff99.tar.gz
slx-tools-f312ec055e6e374453c241a870dab04fb826ff99.tar.xz
slx-tools-f312ec055e6e374453c241a870dab04fb826ff99.zip
initial collections of beta functions
-rw-r--r--slx-tools77
1 files changed, 77 insertions, 0 deletions
diff --git a/slx-tools b/slx-tools
new file mode 100644
index 0000000..cce30a2
--- /dev/null
+++ b/slx-tools
@@ -0,0 +1,77 @@
+#!/bin/bash
+
+# Collection of small bash functions utilized in various scripts.
+################################################################################
+export PATH=$PATH:/opt/openslx/bin:/opt/openslx/sbin
+
+declare -rg dmstate="/run/openslx/dmsetup.state"
+
+get_backing_part() {
+ local _dev _mp
+ read -r _dev _mp _rest <<< \
+ "$(df -P "$1" 2>&1 | awk '$1 ~ /^\/dev\// {print $1" "$6}')"
+ [ -b "$_dev" ] || return 1
+ echo $_dev
+}
+# Helper to check whether given directory resides in RAM, either
+# by being mounted as tmpfs or not mounted at all in which case
+# we assume the same. Returns 0 if so, 1 if otherwise backed,
+# 2 when errors occur.
+is_volatile() {
+ [ -z "$1" ] && return 2
+ local _dev="$(get_backing_part $1)"
+ [ "$_dev" = "${_dev//\/dev\/mapper/}" ] && return 1
+ # it is a device mapper device, check if it was setup in dracut's initramfs.
+ if [ -s "$dmstate" ]; then
+ grep -qE "^${_dev}\s+${_mp}\s+type=0" "$dmstate"
+ return $?
+ fi
+ return 2
+}
+
+get_dm_backing_size() {
+ [ -z "$1" ] && return 1
+ local _dev="$(get_backing_part $1)"
+ [ "$_dev" = "${_dev//\/dev\/mapper/}" ] && return 1
+ local _dm_line="$(grep -m1 -E "^${_dev}\s+/\s+type=[^0]" "$dmstate")"
+ [ -z "$_dm_line" ] && return 1
+ local _dm_dev_size="$(grep -Po '(?<=physical_size=|virtual_size=)\w*' <<< "$_dm_line" )"
+ echo ${_dm_dev_size:--1}
+}
+
+get_partitions_by_id () {
+ local ID dev exp target
+ exp=
+ # target for the scan, defaults to /dev to check everything
+ target=/dev
+ if [ -b "$1" ]; then
+ target="$1"'*'
+ shift
+ fi
+ # support commas and pipes to separate identifiers
+ local args=$@
+ set -- ${args//[,|]/ }
+ while [ $# -gt 0 ]; do
+ ID=$1
+ shift
+ [ -z "$ID" ] && continue
+ # if single digit, e.g. 7, look for 0x7 and 0x07
+ [[ $ID =~ ^[0-9]$ ]] && ID="0?$ID"
+ if [[ $ID =~ ^[0-9]{2}$ ]]; then
+ # 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 [[ $ID =~ ^(0x[0-9]{2}|[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})$ ]]; then
+ # if full MBR type (e.g. 0x44) or UUID look for TYPE
+ exp="$exp|ID_PART_ENTRY_TYPE=$ID"
+ else
+ # something else, look for names of partitions / filesystems
+ exp="$exp|ID_(PART_ENTRY_NAME|FS_LABEL)=$ID"
+ fi
+ done
+ exp=${exp:1}
+ #echo "Partition find is '$exp'" >&2
+ for dev in $(find $target -type b); do
+ udevadm info --name="$dev" | grep -iqE "($exp)\$" \
+ && echo "$(blockdev --getsize64 "$dev") $dev"
+ done | sort -n -k1 -r | cut -d' ' -f2
+}