Skip to content

Instantly share code, notes, and snippets.

@RobertKrawitz
Created August 29, 2025 20:35
Show Gist options
  • Save RobertKrawitz/88b43e755d43002ccf88a96976f9c865 to your computer and use it in GitHub Desktop.
Save RobertKrawitz/88b43e755d43002ccf88a96976f9c865 to your computer and use it in GitHub Desktop.
Find one unused disk on each OCP node supporting storage for use in e. g. creating a LocalVolumeSet
#!/bin/bash
read -r -d '' script <<'EOF'
for f in /dev/sd* ; do
mountpoints="$(lsblk -J "$f" | jq -r '.blockdevices[0].mountpoints[]?')"
if [[ -n "$mountpoints" && $mountpoints != "null" ]] ; then continue; fi
children="$(lsblk -J "$f" | jq -r '.blockdevices[0].children[]?')"
if [[ -n "$children" && $children != "null" ]] ; then continue; fi
# If we immediately continued after finding a used disk
# the find would get a broken pipe, which is ugly
# albeit harmless
failed=0
if [[ -d /mnt/local-storage ]] ; then
while read -r l ; do
if [[ $(readlink -f "$l") = "$f" ]] ; then failed=1; fi
done <<< "$(sudo find /mnt/local-storage -type l -print)"
if ((failed)) ; then continue; fi
fi
size="$(lsblk -J "$f" | jq -r '.blockdevices[0].size')"
if [[ $size =~ ([0-9]+(\.[0-9]+)?)([KMGTP])? ]] ; then
dsize=${BASH_REMATCH[1]}
suffix=${BASH_REMATCH[3]}
case "$suffix" in
K) scale=1000 ;;
M) scale=$((1000 * 1000)) ;;
G) scale=$((1000 * 1000 * 1000)) ;;
T) scale=$((1000 * 1000 * 1000 * 1000)) ;;
P) scale=$((1000 * 1000 * 1000 * 1000 * 1000)) ;;
*) continue ;;
esac
bytes=$(python3 <<< "print('%d' % ($dsize * $scale))")
if ((bytes < 1500 * 1000 * 1000 * 1000)) ; then continue;fi
echo "$f"
break
fi
done
EOF
while read -r node _ ; do
disk=$(ssh core@$node "cat > /tmp/diskfinder && chmod +x /tmp/diskfinder && /tmp/diskfinder" <<< "$script")
if [[ -n "$disk" ]] ; then
printf "%s %s\n" "$node" "$disk"
else
echo "Can't find usable disk on $node" 1>&2
fi
done <<< "$(oc get node --no-headers -l cluster.ocs.openshift.io/openshift-storage)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment