Fix inefficient DRIVE_MAP declaration pattern
Declare DRIVE_MAP as global at function start and populate directly, instead of creating a local array and copying to global. Also added proper variable quoting and function documentation. Fixes: #1 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -243,28 +243,33 @@ get_storage_controllers() {
|
||||
done
|
||||
}
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
# build_drive_map
|
||||
#
|
||||
# Builds a global associative array mapping physical bay numbers to device names.
|
||||
# Uses PCI paths from SERVER_MAPPINGS to resolve current device assignments.
|
||||
#
|
||||
# Sets: DRIVE_MAP (global associative array)
|
||||
# Keys: Bay identifiers (1, 2, ..., m2-1, m2-2, etc.)
|
||||
# Values: Device names (sda, nvme0n1, etc.)
|
||||
#------------------------------------------------------------------------------
|
||||
build_drive_map() {
|
||||
local host=$(hostname)
|
||||
declare -A drive_map
|
||||
local host="$(hostname)"
|
||||
local mapping="${SERVER_MAPPINGS[$host]}"
|
||||
|
||||
local mapping=${SERVER_MAPPINGS[$host]}
|
||||
# Declare global array directly instead of copying from local
|
||||
declare -g -A DRIVE_MAP=()
|
||||
|
||||
if [[ -n "$mapping" ]]; then
|
||||
while read -r path slot; do
|
||||
[[ -z "$path" || -z "$slot" ]] && continue
|
||||
|
||||
if [[ -L "/dev/disk/by-path/$path" ]]; then
|
||||
local drive=$(readlink -f "/dev/disk/by-path/$path" | sed 's/.*\///')
|
||||
drive_map[$slot]=$drive
|
||||
local drive="$(readlink -f "/dev/disk/by-path/$path" | sed 's/.*\///')"
|
||||
DRIVE_MAP[$slot]="$drive"
|
||||
fi
|
||||
done <<< "$mapping"
|
||||
fi
|
||||
|
||||
# Make drive_map available globally
|
||||
declare -g -A DRIVE_MAP=()
|
||||
for key in "${!drive_map[@]}"; do
|
||||
DRIVE_MAP[$key]=${drive_map[$key]}
|
||||
done
|
||||
}
|
||||
|
||||
get_drive_smart_info() {
|
||||
|
||||
Reference in New Issue
Block a user