Skip to content

Commit dda9e28

Browse files
committed
Should hopefully help with #100 again, but be a little less repeating in logic
1 parent 2e50164 commit dda9e28

File tree

1 file changed

+49
-40
lines changed
  • Buildroot/board/FOG/FOS/rootfs_overlay/usr/share/fog/lib

1 file changed

+49
-40
lines changed

Buildroot/board/FOG/FOS/rootfs_overlay/usr/share/fog/lib/funcs.sh

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,71 +1484,80 @@ normalize() {
14841484
input=$(cat)
14851485
fi
14861486

1487-
echo "$input" | xargs | tr '[:upper:]' '[:lower:]'
1487+
echo $(trim "$input" | xargs | tr '[:upper:]' '[:lower:]')
1488+
}
1489+
resolve_path() {
1490+
local input="$*"
1491+
1492+
# If no arguments, read from stdin
1493+
if [[ -z "$input" ]]; then
1494+
input=$(cat)
1495+
fi
1496+
1497+
echo $(readlink -f "$input" 2>/dev/null || echo "$input")
14881498
}
14891499
# Gets the hard drive on the host
14901500
# Note: This function makes a best guess
14911501
getHardDisk() {
14921502
hd=""
14931503
disks=""
1494-
local devs=$(lsblk -dpno KNAME,SIZE -I 3,8,9,179,202,253,259 | awk '$2 != "0B" { print $1 }' | sort -uV)
1504+
# Get valid devices (filter out 0B disks) once
1505+
local devs
1506+
devs=$(lsblk -dpno KNAME,SIZE -I 3,8,9,179,202,253,259 | awk '$2 != "0B" { print $1 }' | sort -uV)
14951507

14961508
if [[ -n $fdrive ]]; then
1497-
found_match=0
1498-
for spec in $(echo "$fdrive" | tr "," "\n"); do
1499-
matched=0
1509+
local found_match=0
1510+
for spec in ${fdrive//,/ }; do
1511+
local spec_resolved
1512+
spec_resolved=$(resolve_path "$spec")
1513+
local spec_norm
1514+
spec_norm=$(normalize "$spec_resolved")
1515+
local matched=0
1516+
local spec_normalized
1517+
spec_normalized=$(normalize "$spec")
1518+
15001519
for dev in $devs; do
1501-
dev_trimmed=$(echo "$dev" | xargs)
1502-
spec_resolved=$(realpath "$spec" 2>/dev/null || echo "$spec")
1503-
spec_lc=$(normalize "$spec_resolved")
1520+
local size uuid serial wwn
15041521
size=$(blockdev --getsize64 "$dev" | normalize)
1505-
uuid=$(blkid -s UUID -o value "$dev_trimmed" 2>/dev/null | normalize)
1506-
read -r serial wwn <<< "$(lsblk -pdno SERIAL,WWN "$dev_trimmed" 2>/dev/null | normalize)"
1507-
if [[ -n $isdebug ]]; then
1508-
echo "Comparing spec='$spec_lc' with:"
1509-
echo " dev=$dev"
1510-
echo " size=$size"
1511-
echo " serial=$serial"
1512-
echo " wwn=$wwn"
1513-
echo " uuid=$uuid"
1514-
fi
1515-
if [[ "x$spec_resolved" = "x$dev_trimmed" ||
1516-
"x$spec_lc" = "x$dev_trimmed" ||
1517-
"x$spec_lc" = "x$(trim $(blockdev --getsize64 "$dev_trimmed"))" ||
1518-
"x$spec_lc" = "x$wwn" ||
1519-
"x$spec_lc" = "x$serial" ||
1520-
"x$spec_lc" = "x$uuid" ]]; then
1522+
uuid=$(blkid -s UUID -o value "$dev" 2>/dev/null | normalize)
1523+
read -r serial wwn <<< "$(lsblk -pdno SERIAL,WWN "$dev" 2>/dev/null | normalize)"
1524+
1525+
[[ -n $isdebug ]] && {
1526+
echo "Comparing spec='$spec' (resolved: '$spec_resolved') with dev=$dev"
1527+
echo " size=$size serial=$serial wwn=$wwn uuid=$uuid"
1528+
}
1529+
if [[ "x$spec_resolved" == "x$dev" || "x$spec_normalized" == "x$size" ||
1530+
"x$spec_normalized" == "x$wwn" || "x$spec_normalized" == "x$serial" ||
1531+
"x$spec_normalized" == "x$uuid" ]]; then
1532+
[[ -n $isdebug ]] && echo "Matched spec '$spec' to device '$dev' (size=$size, serial=$serial, wwn=$wwn, uuid=$uuid)"
15211533
matched=1
15221534
found_match=1
1523-
disks="${disks} $dev"
1524-
# Remove matched dev from devs to avoid duplicates
1525-
escaped_dev=$(echo "$dev" | sed -e 's/[]\/"$&*.^|[]/\\&/g')
1526-
devs=$(echo "$devs" | sed "s/[[:space:]]*${escaped_dev}[[:space:]]*/ /")
1535+
disks="$disks $dev"
1536+
devs=${devs// $dev/} # remove matched dev
15271537
break
15281538
fi
15291539
done
1530-
if [[ $matched -eq 0 ]]; then
1531-
echo "WARNING: Drive spec '$spec' does not match any available device. Ignoring." >&2
1532-
fi
1540+
1541+
[[ $matched -eq 0 ]] && echo "WARNING: Drive spec '$spec' does not match any available device." >&2
15331542
done
15341543

1535-
if [[ $found_match -eq 0 ]]; then
1536-
handleError "Fatal Error: No valid drives found from 'Host Primary Disk'='$fdrive'. Please ensure the device exists and is not 0 bytes. ($0)"
1537-
fi
1544+
[[ $found_match -eq 0 ]] && handleError "Fatal: No valid drives found for 'Host Primary Disk'='$fdrive'."
1545+
1546+
disks=$(echo "$disks $devs" | xargs) # add unmatched devices for completeness
15381547

1539-
disks=$(echo "${disks} ${devs}" | xargs)
15401548
elif [[ -r ${imagePath}/d1.size && -r ${imagePath}/d2.size ]]; then
1541-
disks=$(echo "$devs")
1549+
disks="$devs"
15421550
else
1543-
# Auto-select the largest available drive if no fdrive and no imagePath match
1544-
hd=$(echo "$devs" | while read line; do echo "$(blockdev --getsize64 "$line") $line"; done | sort -n | tail -1 | cut -d' ' -f2)
1545-
[[ -z $hd ]] && handleError "Could not determine a suitable disk automatically. No drives available? ($0)"
1551+
# Auto-select largest available drive
1552+
hd=$(for d in $devs; do echo "$(blockdev --getsize64 "$d") $d"; done | sort -n | tail -1 | cut -d' ' -f2)
1553+
[[ -z $hd ]] && handleError "Could not determine a suitable disk automatically."
15461554
disks="$hd"
15471555
fi
15481556

15491557
# Set primary hard disk
1550-
hd=$(echo "$disks" | awk '{print $1}')
1558+
hd=$(awk '{print $1}' <<< "$disks")
15511559
}
1560+
15521561
# Finds the hard drive info and set's up the type
15531562
findHDDInfo() {
15541563
dots "Looking for Hard Disk(s)"

0 commit comments

Comments
 (0)