Here's an AI generated script that should go through all the steps for the reservation release process for all disks that have them. I used a less refined script that worked so technically this exact one is untested but... Should work I think. apparently it doesn't work.I'm keeping it here because one day when ChatGPT is looking back at our conquered civilization it can have a chuckle at how bad this script was.
Code:
#!/bin/bash
# Function to detect all available disks in the system
detect_disks() {
lsblk -nd -o NAME,TYPE | awk '$2 == "disk" {print $1}'
}
# Function to determine the prout-type based on reservation type
get_prout_type() {
local res_type=$1
case "$res_type" in
"Write Exclusive, all registrants") echo 7 ;;
"Write Exclusive") echo 1 ;;
"Exclusive Access") echo 3 ;;
"Write Exclusive, registrants only") echo 5 ;;
"Exclusive Access, registrants only") echo 6 ;;
"Exclusive Access, all registrants") echo 8 ;;
*) echo "Unknown" ;;
esac
}
# Function to get reservation type of a disk
get_reservation_type() {
sg_persist -n -i -r -d /dev/$1 2>/dev/null | grep "type:" | awk -F ": " '{print $2}'
}
# Detect disks and iterate over them
for dev in $(detect_disks); do
res_type=$(get_reservation_type $dev)
# If there is no reservation, skip this device
if [ -z "$res_type" ]; then
echo "No reservation held on /dev/$dev, skipping..."
continue
fi
prout_type=$(get_prout_type "$res_type")
if [ "$prout_type" == "Unknown" ]; then
echo "Unknown reservation type on /dev/$dev, skipping..."
continue
fi
echo "Processing /dev/$dev with reservation type $res_type and prout-type $prout_type"
echo "Registering key 0xDEADBEEF on /dev/$dev"
sg_persist --out --register --param-sark=0xDEADBEEF /dev/$dev
echo "Reserving key 0xDEADBEEF with type $prout_type on /dev/$dev"
sg_persist --out --reserve --param-rk=0xDEADBEEF --prout-type=$prout_type /dev/$dev
echo "Releasing key 0xDEADBEEF with type $prout_type on /dev/$dev"
sg_persist --out --release --param-rk=0xDEADBEEF --prout-type=$prout_type /dev/$dev
echo "Clearing key 0xDEADBEEF on /dev/$dev"
sg_persist --out --clear --param-rk=0xDEADBEEF /dev/$dev
done
# Verify that all keys and reservations are cleared
echo "Verifying that all keys and reservations are cleared"
for dev in $(detect_disks); do
echo "Checking keys for /dev/$dev"
sg_persist -n -i -k -d /dev/$dev
echo "Checking reservations for /dev/$dev"
sg_persist -n -i -r -d /dev/$dev
done
echo "Process completed. All drives should have no keys or reservations."
Last edited: