From b1c4d9d40e7aa55bbd48af95d67919cddf9e90c3 Mon Sep 17 00:00:00 2001 From: oddlama Date: Thu, 23 Apr 2020 23:15:41 +0200 Subject: [PATCH] Removed need for internally stored id->device map --- scripts/functions.sh | 5 ++-- scripts/internal_config.sh | 2 -- scripts/utils.sh | 57 +++++++++++++++++++++----------------- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/scripts/functions.sh b/scripts/functions.sh index 6eb069e..fb895bb 100644 --- a/scripts/functions.sh +++ b/scripts/functions.sh @@ -138,7 +138,7 @@ disk_create_gpt() { fi local ptuuid="${DISK_ID_TO_UUID[$new_id]}" - create_resolve_entry "$new_id" ptuuid "$ptuuid" "$device" + create_resolve_entry "$new_id" ptuuid "$ptuuid" einfo "Creating new gpt partition table ($new_id) on $device_desc" sgdisk -Z -U "$ptuuid" "$device" >/dev/null \ @@ -215,7 +215,7 @@ disk_create_raid() { local mddevice="/dev/md/$name" local uuid="${DISK_ID_TO_UUID[$new_id]}" - create_resolve_entry "$new_id" mdadm "$uuid" "$mddevice" + create_resolve_entry "$new_id" mdadm "$uuid" einfo "Creating raid$level ($new_id) on $devices_desc" mdadm \ @@ -451,7 +451,6 @@ apply_disk_configuration() { # Clean old resolved ids rm -rf "$RESOLVABLE_MAP_DIR" &>/dev/null unset DISK_ID_TO_RESOLVABLE; declare -A -g DISK_ID_TO_RESOLVABLE - unset DISK_UUID_TO_DEVICE; declare -A -g DISK_UUID_TO_DEVICE summarize_disk_actions diff --git a/scripts/internal_config.sh b/scripts/internal_config.sh index d5ea608..f521f43 100644 --- a/scripts/internal_config.sh +++ b/scripts/internal_config.sh @@ -27,8 +27,6 @@ USED_LUKS=false DISK_ACTIONS=() # An associative array from disk id to a resolvable string declare -A DISK_ID_TO_RESOLVABLE -# An associative from uuid to device -declare -A DISK_UUID_TO_DEVICE # An associative array from disk id to parent gpt disk id (only for partitions) declare -A DISK_ID_PART_TO_GPT_ID # An associative array to check for existing ids (maps to uuids) diff --git a/scripts/utils.sh b/scripts/utils.sh index 24fa806..c753ee5 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -138,11 +138,32 @@ get_device_by_uuid() { get_device_by_blkid_field 'UUID' "$1" } -get_device_by_stored_uuid() { - local key="${1,,}" - [[ -v "DISK_UUID_TO_DEVICE[$key]" ]] \ - || die "Could not resolve uuid $key to device (not stored)" - echo -n "${DISK_UUID_TO_DEVICE[$key]}" +get_device_by_ptuuid() { + local ptuuid="${1,,}" + local dev + dev="$(lsblk --all --path --pairs --output NAME,PTUUID,PARTUUID)" \ + || die "Error while executing lsblk to find PTUUID=$ptuuid" + dev="$(grep "ptuuid=\"$ptuuid\" partuuid=\"\"" <<< "${dev,,}")" \ + || die "Could not find PTUUID=... in lsblk output" + dev="${dev%' ptuuid='*}" + dev="${dev#'name="'}" + echo -n "$dev" +} + +get_device_by_mdadm_uuid() { + local mduuid="${1,,}" + mduuid="${mduuid//-/}" + mduuid="${mduuid:0:8}:${mduuid:8:8}:${mduuid:16:8}:${mduuid:24:8}" + local dev + dev="$(mdadm --examine --scan)" \ + || die "Error while executing mdadm to find array with UUID=$mduuid" + dev="$(grep "uuid=$mduuid" <<< "${dev,,}")" \ + || die "Could not find UUID=... in mdadm output" + dev="${dev%'metadata='*}" + dev="${dev#'array'}" + dev="${dev#"${dev%%[![:space:]]*}"}" + dev="${dev%"${dev##*[![:space:]]}"}" + echo -n "$dev" } get_device_by_luks_uuid() { @@ -176,19 +197,6 @@ create_resolve_entry() { local id="$1" local type="$2" local arg="${3,,}" - local device="$4" # optional - - case "$type" in - 'partuuid') ;; - 'uuid') ;; - 'luks') ;; - 'ptuuid') ;& # fallthrough - 'mdadm') - DISK_UUID_TO_DEVICE[$arg]="$device" - save_map_entry DISK_UUID_TO_DEVICE "$arg" "$device" - ;; - *) die "Cannot add resolvable entry for '$type:$arg' (unknown type)" - esac DISK_ID_TO_RESOLVABLE[$id]="$type:$arg" save_map_entry DISK_ID_TO_RESOLVABLE "$id" "$type:$arg" @@ -201,9 +209,6 @@ load_resolvable_entries() { lambda() { DISK_ID_TO_RESOLVABLE[$1]="$2" }; load_map_entries DISK_ID_TO_RESOLVABLE lambda - lambda() { - DISK_UUID_TO_DEVICE[$1]="$2" - }; load_map_entries DISK_UUID_TO_DEVICE lambda } resolve_device_by_id() { @@ -215,11 +220,11 @@ resolve_device_by_id() { local arg="${DISK_ID_TO_RESOLVABLE[$id]#*:}" case "$type" in - 'partuuid') get_device_by_partuuid "$arg" ;; - 'ptuuid') get_device_by_stored_uuid "$arg" ;; - 'uuid') get_device_by_uuid "$arg" ;; - 'mdadm') get_device_by_stored_uuid "$arg" ;; - 'luks') get_device_by_luks_uuid "$arg" ;; + 'partuuid') get_device_by_partuuid "$arg" ;; + 'ptuuid') get_device_by_ptuuid "$arg" ;; + 'uuid') get_device_by_uuid "$arg" ;; + 'mdadm') get_device_by_mdadm_uuid "$arg" ;; + 'luks') get_device_by_luks_uuid "$arg" ;; *) die "Cannot resolve '$type:$arg' to device (unknown type)" esac }