As part of the process of upgrading my FreeNAS box, I'm going through and sorting all my scripts.
I put together this script to aid in drive identification so that when a drive fails it's easier to locate the correct drive for replacement.
The similar scripts I have come across used smartctl which has a lot of overhead, so I found a way to avoid using it. I also avoid multiple redundant calls to glabel and diskinfo. I use glabel as the source of drive device names, but since I only have one FreeNAS box, I have no way of knowing if this method will work on a wide range of hardware.
I welcome any constructive feedback on style, technique or I've employed any bad practices or if there are any bugs. What happens if this script is run on a pool with a bad drive (since that's the main reason for this scripts existence). Does it give useful output, or does it fail?
I've included a sample output (with a few numbers changed for privacy), and the complete source code. Feel free to use it as you see fit, I offer it to the community without warranty or accepting any liability - enjoy!
EDIT: It works under /bin/sh... so I changed the bang path accordingly.
Edit: Thanks to @toadman for identifying a small bug where the script gets tripped up on disks with swap (p1, p2) by listing them twice (two rows per disk) and printing their gptid twice (and line wrapping). I've applied a quick patch here:
https://forums.freenas.org/index.php?posts/430669
that displays the slice information and fixes the line wrap by only showing the gptid that belongs to that slice.
Given that this is a script that would likely be used for troubleshooting, I decided it's better not to sanitize and delete information. If something unexpected is going on, I wouldn't want to hide it. As always, I'm open to suggestions.
I put together this script to aid in drive identification so that when a drive fails it's easier to locate the correct drive for replacement.
The similar scripts I have come across used smartctl which has a lot of overhead, so I found a way to avoid using it. I also avoid multiple redundant calls to glabel and diskinfo. I use glabel as the source of drive device names, but since I only have one FreeNAS box, I have no way of knowing if this method will work on a wide range of hardware.
I welcome any constructive feedback on style, technique or I've employed any bad practices or if there are any bugs. What happens if this script is run on a pool with a bad drive (since that's the main reason for this scripts existence). Does it give useful output, or does it fail?
I've included a sample output (with a few numbers changed for privacy), and the complete source code. Feel free to use it as you see fit, I offer it to the community without warranty or accepting any liability - enjoy!
Code:
driveid - Mounted Drives on hostname FreeNAS-11.1-RELEASE (dc7d195f4) Tue Jan 9 01:14:12 EST 2018 +========+==========================+==================+============================================+ | Device | DISK DESCRIPTION | SERIAL NUMBER | GPTID | +========+==========================+==================+============================================+ | ada0 | Hitachi HDS724040ALE640 | PK1331PAG91XSS | gptid/eb2ee48c-a398-11e2-bccb-001517d2673c | +--------+--------------------------+------------------+--------------------------------------------+ | ada1 | Hitachi HDS724040ALE640 | PK1331PAG21SXC | gptid/ec0615be-a398-11e2-bccb-001517d2673c | +--------+--------------------------+------------------+--------------------------------------------+ | ada2 | HP SSD S700 120GB | HBSA17660907645 | gptid/62a1164b-9da1-11e7-af23-001517d2673c | +--------+--------------------------+------------------+--------------------------------------------+ | da0 | ATA WDC WD60EFRX-68L | WD-WX21D7243NEX | gptid/77285d08-f115-11e7-addb-001517d2673c | +--------+--------------------------+------------------+--------------------------------------------+ | da1 | ATA WDC WD60EFRX-68L | WD-WX11D66CQ29N | gptid/79586ddf-f115-11e7-addb-001517d2673c | +--------+--------------------------+------------------+--------------------------------------------+ | da2 | ATA WDC WD60EFRX-68M | WD-WX11FU2FKUL8 | gptid/7a88a3fa-f115-11e7-addb-001517d2673c | +--------+--------------------------+------------------+--------------------------------------------+ | da3 | ATA WDC WD60EFRX-68M | WD-WX31DA43KKK4 | gptid/7b573fdd-f115-11e7-addb-001517d2673c | +--------+--------------------------+------------------+--------------------------------------------+ | da4 | ATA WDC WD60EFRX-68M | WD-WX11DY4U93HD | gptid/7c1ae2a3-f115-11e7-addb-001517d2673c | +--------+--------------------------+------------------+--------------------------------------------+ | da5 | ATA WDC WD60EFRX-68M | WD-WX21D26E7LXN | gptid/7cdc01d2-f115-11e7-addb-001517d2673c | +--------+--------------------------+------------------+--------------------------------------------+ | da6 | ATA WDC WD60EFRX-68L | WD-WX11T4243VS2 | gptid/7ed11b20-f115-11e7-addb-001517d2673c | +--------+--------------------------+------------------+--------------------------------------------+ | da7 | ATA WDC WD60EFRX-68L | WD-WX11B48EPJD6 | gptid/811b73c6-f115-11e7-addb-001517d2673c | +--------+--------------------------+------------------+--------------------------------------------+
Code:
#!/bin/sh echo echo $(basename $0) - Mounted Drives on $(hostname) cat /etc/version date echo diskinfo="$(glabel status | tail -n +2 | awk '{split($3,a,"p"); print a[1],$1}')" echo "+========+==========================+==================+============================================+" echo "| Device | DISK DESCRIPTION | SERIAL NUMBER | GPTID |" echo "+========+==========================+==================+============================================+" for d in $(echo "$diskinfo" | cut -d" " -f 1) do diskinf=$(diskinfo -v $d | grep '# Disk ') diskdescription=$(echo "$diskinf" | grep '# Disk desc' | cut -d# -f 1 | xargs) diskserialno=$(echo "$diskinf" | grep '# Disk ident' | cut -d# -f 1 | xargs) diskgptid=$(echo "$diskinfo" | grep ^$d | cut -d" " -f 2) printf "| %-6s | %-24s | %-16s | %-42s |\n" "$d" "$diskdescription" "$diskserialno" "$diskgptid" echo "+--------+--------------------------+------------------+--------------------------------------------+" done
EDIT: It works under /bin/sh... so I changed the bang path accordingly.
Edit: Thanks to @toadman for identifying a small bug where the script gets tripped up on disks with swap (p1, p2) by listing them twice (two rows per disk) and printing their gptid twice (and line wrapping). I've applied a quick patch here:
https://forums.freenas.org/index.php?posts/430669
Code:
# diskinfo="$(glabel status | tail -n +2 | awk '{split($3,a,"p"); print a[1],$1}')" diskinfo="$(glabel status | tail -n +2 | awk '{print $3,$1}')" # Temp Bug fix to accomodate botH p1/p2
Given that this is a script that would likely be used for troubleshooting, I decided it's better not to sanitize and delete information. If something unexpected is going on, I wouldn't want to hide it. As always, I'm open to suggestions.
Last edited: