Created
March 26, 2018 16:06
-
-
Save alekratz/5d896bbba20b5c72bb59862b32d72226 to your computer and use it in GitHub Desktop.
Writes table info of /dev/sd* disk info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# Determines whether the specified disk is partitioned or not. | |
has-partition() { | |
local dev="$1" | |
local parts=("$dev"?) | |
if [[ ${#parts[@]} == 0 ]]; then | |
return 1 | |
else | |
return 0 | |
fi | |
} | |
# Gets the serial number of the specified device. | |
get-serial() { | |
local dev="$1" | |
smartctl -i "$dev" | grep -i "^serial number:" | sed -e 's/.\+:[ ]\+\(.\+\)$/\1/' | |
} | |
# Gets the inode of the specified file, following symlinks. | |
get-inode() { | |
local f="$1" | |
stat -Lc '%i' "$f" | |
} | |
# Gets the SCSI ID of the specified device. | |
get-scsi() { | |
local dev disks inode | |
dev="$1" | |
disks=(/dev/disk/by-id/*) | |
inode="$(get-inode "$dev")" | |
for d in "${disks[@]}"; do | |
if [[ "$(get-inode "$d")" == "$inode" ]]; then | |
basename "$d" | |
return | |
fi | |
done | |
echo "?" | |
} | |
print-table() { | |
local all="$1" | |
shift | |
local disks=("$@") | |
echo "Partitioned Disk Serial ID" | |
for d in "${disks[@]}"; do | |
if has-partition "$d"; then | |
if [[ "$all" == "0" ]]; then continue | |
else part="yes" | |
fi | |
else | |
part="no" | |
fi | |
serial=$(get-serial "$d") | |
scsi=$(get-scsi "$d") | |
echo "$part $d $serial $scsi" | |
done | |
} | |
if [[ $(id -u) != "0" ]]; then | |
echo "this script must be run as root (sudo $0)" | |
exit 1 | |
fi | |
if [[ $1 == "-a" ]]; then | |
all=1 | |
else | |
all=0 | |
fi | |
disks=(/dev/sd?) | |
# This will print: | |
# * serial number | |
# * SCSI device number | |
# * device file | |
# If "all" is not specified, it will skip devices that already have partitions. | |
print-table "$all" "${disks[@]}" | column -t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment