Skip to content

Instantly share code, notes, and snippets.

@Robpol86
Last active December 4, 2023 12:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Robpol86/ef6102ddb35fed4587b6b4e6a1e51b93 to your computer and use it in GitHub Desktop.
Save Robpol86/ef6102ddb35fed4587b6b4e6a1e51b93 to your computer and use it in GitHub Desktop.

Slots Tool

I'm running TrueNAS-SCALE-23.10.0.1 on an old Supermicro motherboard in a Genesys S208B-TWIN-ITX-12G chassis. This chassis has hot swappable 3.5" SAS drive bays oriented in two rows of four drives. My storage drives are connected through an LSI Broadcom SAS 9300-8i HBA. My HBA does not support the LOCATE feature, so I cannot blink LEDs to locate hard drives to replace.

In order to quickly associate which hard drive in TrueNAS corresponds to what physical slot it is stored in I wrote a script. This script uses the description column of TrueNAS's Storage Disks table to label what slot a particular drive is installed in. One caveat with this script is I must run it after every reboot or after I install/remove a hard drive, so running sudo slots is now at the beginning of my disk repalcement workflows.

Installation

Copy slots.sh (attached below) to /usr/local/sbin/slots and then run sudo chmod +x /usr/local/sbin/slots. Afterward you will be able to use the tool anywhere by running sudo slots.

Usage

Here is an example output:

$ sudo slots
Updating...
cli -c 'storage disk update "{serial_lunid}A8U9V69MF02U_50002c69cf006e22" description="Slot 0"'
cli -c 'storage disk update "{serial_lunid}ERKE3ODC_500072751ec5ac8f" description="Slot 1"'
cli -c 'storage disk update "{serial_lunid}G38Y5GR9_50005f7976c6b9d2" description="Slot 2"'
cli -c 'storage disk update "{serial_lunid}J93BE0VO_5000abcd935fa962" description="Slot 4"'
cli -c 'storage disk update "{serial_lunid}RHBDQCAW_500029dc633bc768" description="Slot 5"'
cli -c 'storage disk update "{serial_lunid}E8OGD9VZ_500064a75b8d3b55" description="Slot 6"'

Printing table...
+-------------------------+-------------------------+-------------------------+-------------------------+
| 0/sdc/18TB/A8U9V69MF02U | 1/sdd/18TB/ERKE3ODC     | 2/sdb/20TB/G38Y5GR9     |                         |
+-------------------------+-------------------------+-------------------------+-------------------------+
| 4/sdf/18TB/J93BE0VO     | 5/sdg/16TB/RHBDQCAW     | 6/sde/16TB/E8OGD9VZ     |                         |
+-------------------------+-------------------------+-------------------------+-------------------------+

Persist Upgrades

When you upgrade TrueNAS everything in /usr/local/sbin will be lost. To keep the script on your machine permanently it must be stored as an init script in your TrueNAS configuration. This can be done with the following commands:

PAYLOAD="$(cat /usr/local/sbin/slots |gzip -9 - |base64 -w0)"
COMMAND="echo $PAYLOAD|base64 -d|gzip -d|install -m0755 /dev/stdin /usr/local/sbin/slots"
cli -c "system init_shutdown_script create comment=\"slots.sh\" type=COMMAND when=POSTINIT command=\"$COMMAND\""
#!/bin/bash
set -euo pipefail
echo "Updating..."
sas3ircu 0 DISPLAY |awk -F'[ :]+' '
/^ Slot # +: [0-9]+/ { t = sprintf("%s", $4) }
/^ Unit Serial No[(]VPD[)] +: / { s = sprintf("%s", $5) }
/^ GUID +: / { lunid = sprintf("%s", $3) }
!/^ [^ ]/ {
if (t && s && lunid) {
id = sprintf(lunid == "N/A" ? "{serial}%s" : "{serial_lunid}%s_%s", s, lunid)
cmd = sprintf("storage disk update \"%s\" description=\"Slot %s\"", id, t)
printf(cmd "\0")
t = s = lunid = ""
p = 1
}
}
END { if (!p) { print("E: No valid input") > "/dev/stderr"; exit(1) } }
' |xargs -t -n 1 -r -0 cli -c || r=$?
[ ${r:-0} -eq 0 ] && echo -e "\nPrinting table..." || echo -e "\nW: STALE DATA..."
cli -c "storage disk query name,serial,size,description" |awk '
match($0, /\| (sd[a-z])\s+\| ([a-zA-Z0-9_-]+)\s+\| ([0-9]+)\s+\| Slot ([0-9]+)\s+/, arr) {
t[arr[4]] = sprintf("%d/%s/%dTB/%s", arr[4], arr[1], arr[3] / 1000^4, arr[2])
w = (w > length(t[arr[4]])) ? w : length(t[arr[4]])
s = "+"
d = ""
for (j = 1; j <= w+2; j++) d = d "-"
for (i = 0; i < 4; i++) s = s d "+"
}
END {
print s
for (i = 0; i <= 3; i++) printf("| %-*s ", w, t[i]); print "|"
print s
for (i = 4; i <= 7; i++) printf("| %-*s ", w, t[i]); print "|"
print s
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment