Skip to content

Commit

Permalink
🐛 Detect invalid WWN configuration of hbmh.
Browse files Browse the repository at this point in the history
  • Loading branch information
guettli committed Feb 14, 2024
1 parent 81374d3 commit fb88b61
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pkg/services/baremetal/host/detect-linux-on-other-disk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash
set -euo pipefail

trap 'echo "Warning: A command has failed. Exiting the script. Line was ($0:$LINENO): $(sed -n "${LINENO}p" "$0")"; exit 3' ERR

function usage() {
echo "$0 wwn1 [wwn2 ...]"
echo " Check if there is a Linux partition, but skip all WWNs given as arguments"
echo " Background: If we provision a disk, then there must not be a Linux OS on an other partition"
echo " otherwise it is likely that the old OS gets booted, and not the new OS."
echo " Exit 0: If there is no Linux installation found."
echo " Exit 1: There is a Linux on a different disk.".
echo " Exit 3: Unexpected error."
echo "Existing WWNs:"
lsblk -oNAME,WWN | grep -vi loop || true
}

if [ $# -eq 0 ]; then
echo "Error: No WWN was provided."
echo
usage
exit 3
fi

# Iterate over all input arguments
for wwn in "$@"; do
if ! lsblk -l -oWWN | grep -qP '^'${wwn}'$'; then
echo "$wwn is not a WWN of this machine"
echo
usage
exit 3
fi
done
fail=0
while read name wwn type parttype; do
if [[ " $* " == *" $wwn "* ]]; then
echo "ok: skipping $name $wwn, since it was an argument to the script."
continue
fi
root_directory_content=$(grub-fstest /dev/$name ls / 2>/dev/null || true | tr ' ' '\n' | sort | tr '\n' ' ')
if [[ $root_directory_content =~ .*boot/.*etc/.* ]]; then
echo "FAIL: $name $wwn $parttype looks like a Linux root partition on another disk."
fail=1
continue
fi
if [[ $root_directory_content =~ .*initrd.*vmlinuz.* ]]; then
echo "FAIL: $name $wwn $parttype looks like a Linux /boot partition on another disk."
fail=1
continue
fi
echo "ok: $name $wwn $parttype, does not look like root Linux partition."
done < <(lsblk -r -oNAME,WWN,TYPE,PARTTYPENAME | grep -v NAME | grep -i part)
if [ $fail -eq 1 ]; then
exit 1
fi
echo "Looks good. No Linux root partition on other devices"

0 comments on commit fb88b61

Please sign in to comment.