From 4b085236452c973cdc8786e77122d3a3d159e946 Mon Sep 17 00:00:00 2001 From: Dave Russell <39733752+007revad@users.noreply.github.com> Date: Sun, 14 Dec 2025 11:00:56 +1100 Subject: [PATCH] Create check_script_location.sh This script checks the location of itself and warns if it is located on an NVMe volume. --- check_script_location.sh | 60 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 check_script_location.sh diff --git a/check_script_location.sh b/check_script_location.sh new file mode 100644 index 0000000..0996e09 --- /dev/null +++ b/check_script_location.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2317 + +Yellow='\e[0;33m' # ${Yellow} +Off='\e[0m' # ${Off} + +# Get script location +# https://stackoverflow.com/questions/59895/ +source=${BASH_SOURCE[0]} +while [ -L "$source" ]; do # Resolve $source until the file is no longer a symlink + scriptpath=$( cd -P "$( dirname "$source" )" >/dev/null 2>&1 && pwd ) + source=$(readlink "$source") + # If $source was a relative symlink, we need to resolve it + # relative to the path where the symlink file was located + [[ $source != /* ]] && source=$scriptpath/$source +done +scriptpath=$( cd -P "$( dirname "$source" )" >/dev/null 2>&1 && pwd ) +scriptfile=$( basename -- "$source" ) +echo "Running from: ${scriptpath}/$scriptfile" + + +ding(){ + printf \\a +} + + +# Warn if script located on M.2 drive +get_script_vol() { + local script_root vol_num vg_name + script_root="${scriptpath#/*}" + script_root="${script_root%%/*}" + if [[ $script_root =~ ^volume ]] + then + vol_num="${script_root:6}" + vg_name=$(lvs --noheadings --select=lv_name="volume_$vol_num" --options=vg_name) + vg_name="${vg_name// }" + #vol_name=$(pvs --noheadings --select=vg_name="$vg_name" --options=pv_name) + #vol_name="${vol_name// }" + # Only get first partition on volume group + vol_name=$(pvs --noheadings --select=vg_name="$vg_name" --options=pv_name | awk '{print $1}') + else + vol_name=$(df --output=source "/$script_root" |sed 1d) + fi +} + +#set -x # debug ##################################################################### + +get_script_vol # sets $vol_name to /dev/whatever +if grep -qE "^${vol_name#/dev/} .+ nvme" /proc/mdstat +then + ding + echo -e "\n${Yellow}WARNING${Off} Don't store this script on an NVMe volume!" + exit 3 +fi + +echo -e "\n$scriptfile is not on an NVMe volume" + +exit + +