summaryrefslogtreecommitdiffstats
path: root/builder
diff options
context:
space:
mode:
authorjandob2016-03-01 15:45:22 +0100
committerjandob2016-03-01 15:45:22 +0100
commitc61bbf71a5bd5d00a504a8387d87f874dc8af8f1 (patch)
tree0906bafcf70872aa2165073ef38667af4d0c2caf /builder
parentupdate rebash (diff)
downloadsystemd-init-c61bbf71a5bd5d00a504a8387d87f874dc8af8f1.tar.gz
systemd-init-c61bbf71a5bd5d00a504a8387d87f874dc8af8f1.tar.xz
systemd-init-c61bbf71a5bd5d00a504a8387d87f874dc8af8f1.zip
move block device discovery out of rebash
Diffstat (limited to 'builder')
-rwxr-xr-xbuilder/dnbd3-rootfs/hooks/prepare-root-partition.sh7
-rwxr-xr-xbuilder/dnbd3-rootfs/module-setup.sh1
-rw-r--r--builder/dnbd3-rootfs/scripts/tools.sh82
3 files changed, 87 insertions, 3 deletions
diff --git a/builder/dnbd3-rootfs/hooks/prepare-root-partition.sh b/builder/dnbd3-rootfs/hooks/prepare-root-partition.sh
index 88568995..fab1d45e 100755
--- a/builder/dnbd3-rootfs/hooks/prepare-root-partition.sh
+++ b/builder/dnbd3-rootfs/hooks/prepare-root-partition.sh
@@ -2,6 +2,7 @@
# region imports
type getarg >/dev/null 2>&1 || source /lib/dracut-lib.sh
source "/usr/lib/rebash/core.sh"
+core.import "/usr/lib/openslx/tools.sh"
core.import exceptions
exceptions.activate
core.import utils
@@ -15,7 +16,7 @@ SLX_SERVER_BASE="$(getargs slxbase=)"
source /etc/openslx
# region find writable partition
-if ! persistent_device=$(utils.find_block_device \
+if ! persistent_device=$(tools.find_block_device \
"$SLX_WRITABLE_DEVICE_IDENTIFIER"); then
logging.warn "Failed to find unique device with identifier" \
"'${SLX_WRITABLE_DEVICE_IDENTIFIER}'; matched devices:" \
@@ -23,7 +24,7 @@ if ! persistent_device=$(utils.find_block_device \
fi
# TODO move somewhere else
-#tmp_device="$(utils.find_block_device \
+#tmp_device="$(tools.find_block_device \
#'$SLX_TMP_PARTITION_IDENTIFIER')"
#if [ -n $tmp_device ]; then
@@ -109,7 +110,7 @@ fi
# region find read-only partition
if [ -z "$SLX_SYSTEM_PARTITION_PREPARATION_SCRIPT" ]; then
- read_only_partition="$(utils.find_block_device \
+ read_only_partition="$(tools.find_block_device \
"$SLX_SYSTEM_PARTITION_IDENTIFIER" "$read_only_device")"
else
eval "$SLX_SYSTEM_PARTITION_PREPARATION_SCRIPT"
diff --git a/builder/dnbd3-rootfs/module-setup.sh b/builder/dnbd3-rootfs/module-setup.sh
index 7585e7cf..27e62304 100755
--- a/builder/dnbd3-rootfs/module-setup.sh
+++ b/builder/dnbd3-rootfs/module-setup.sh
@@ -205,6 +205,7 @@ install() {
for file_path in "$moddir/scripts/rebash/"*; do
inst "$file_path" "/usr/lib/rebash/$(basename "$file_path")"
done
+ inst "$moddir/scripts/tools.sh" "/usr/lib/openslx/tools.sh"
# TODO currently not used
# This script is triggered by udev upon finding the right partitions for
diff --git a/builder/dnbd3-rootfs/scripts/tools.sh b/builder/dnbd3-rootfs/scripts/tools.sh
new file mode 100644
index 00000000..1a87cff1
--- /dev/null
+++ b/builder/dnbd3-rootfs/scripts/tools.sh
@@ -0,0 +1,82 @@
+#!/usr/bin/env bash
+# shellcheck source=./rebash/core.sh
+source "/usr/lib/rebash/core.sh"
+core.import logging
+
+tools_find_block_device() {
+ # shellcheck disable=SC2034,SC2016
+ local __doc__='
+ >>> lsblk() {
+ >>> if [[ "${@: -1}" == "" ]];then
+ >>> echo "lsblk: : not a block device"
+ >>> return 1
+ >>> fi
+ >>> if [[ "${@: -1}" != "/dev/sdb" ]];then
+ >>> echo "/dev/sda disk"
+ >>> echo "/dev/sda1 part SYSTEM_LABEL 0x7"
+ >>> echo "/dev/sda2 part"
+ >>> fi
+ >>> if [[ "${@: -1}" != "/dev/sda" ]];then
+ >>> echo "/dev/sdb disk"
+ >>> echo "/dev/sdb1 part boot_partition "
+ >>> echo "/dev/sdb2 part system_partition"
+ >>> fi
+ >>> }
+ >>> blkid() {
+ >>> [[ "${@: -1}" != "/dev/sda2" ]] && return 0
+ >>> echo "gpt"
+ >>> echo "only discoverable by blkid"
+ >>> echo "boot_partition"
+ >>> echo "192d8b9e"
+ >>> }
+ >>> tools_find_block_device "boot_partition"
+ >>> tools_find_block_device "boot_partition" /dev/sda
+ >>> tools_find_block_device "discoverable by blkid"
+ >>> tools_find_block_device "_partition"
+ >>> tools_find_block_device "not matching anything" || echo not found
+ >>> tools_find_block_device "" || echo not found
+ /dev/sdb1
+ /dev/sda2
+ /dev/sda2
+ /dev/sdb1 /dev/sdb2
+ not found
+ not found
+ '
+ local partition_pattern="$1"
+ local device="$2"
+
+ [ "$partition_pattern" = "" ] && return 1
+ tools_find_block_device_simple() {
+ local device_info
+ lsblk --noheadings --list --paths --output \
+ NAME,TYPE,LABEL,PARTLABEL,UUID,PARTUUID ${device:+"$device"} \
+ | sort --unique | while read -r device_info; do
+ local current_device
+ current_device=$(echo "$device_info" | cut -d' ' -f1)
+ if [[ "$device_info" = *"${partition_pattern}"* ]]; then
+ echo "$current_device"
+ fi
+ done
+ }
+ tools_find_block_device_deep() {
+ local device_info
+ lsblk --noheadings --list --paths --output NAME ${device:+"$device"} \
+ | sort --unique | cut -d' ' -f1 | while read -r current_device; do
+ blkid -p -o value "$current_device" \
+ | while read -r device_info; do
+ if [[ "$device_info" = *"${partition_pattern}"* ]]; then
+ echo "$current_device"
+ fi
+ done
+ done
+ }
+ local candidates
+ candidates=($(tools_find_block_device_simple))
+ [ ${#candidates[@]} -eq 0 ] && candidates=($(tools_find_block_device_deep))
+ unset -f tools_find_block_device_simple
+ unset -f tools_find_block_device_deep
+ [ ${#candidates[@]} -eq 0 ] && return 1
+ [ ${#candidates[@]} -ne 1 ] && echo "${candidates[@]}" && return 1
+ logging.plain "${candidates[0]}"
+}
+alias tools.find_block_device="tools_find_block_device"