Skip to content

Instantly share code, notes, and snippets.

@isaaclw
Last active January 3, 2022 16:53
Show Gist options
  • Save isaaclw/d3944457b3c49a42608d to your computer and use it in GitHub Desktop.
Save isaaclw/d3944457b3c49a42608d to your computer and use it in GitHub Desktop.
Force device rescan (see detect_sata.sh)
#!/bin/bash
set -e
# Note: 2021-09-09:
# I ran this script and scanned only the hosts that weren't listing a device
# However, it didn't find the block that I needed it to, until I did a global scan:
# ls /sys/class/scsi_host/ | while read host ; do echo "- - -" > /sys/class/scsi_host/$host/scan ; done
# I know this feels dangerous, I would avoid doing it if I understood what was going on, but I eventually just gave up.
# I think there's something wrong below with the matching of the `readlink /sys/block`
# After the full scan, two drives had the same 'host', so I'm not sure if that was the issue or what.
if [ "$(whoami)" != "root" ]; then
echo "Must be root"
exit 1
fi
scan_bus() {
path="$1"
if [ ! -e "$path" ]; then
echo "Does not exist: $path"
fi
if yes_no "Do you want to scan $path?"; then
echo "0 0 0" > "$path"
echo "done"
else
echo "Canceled"
fi
}
source ~/.bin/bash_aliases/confirm.sh
if yes_no "Scan Existing drive?"; then
read -p "Scan: " device
[ -b "$device" -o -b /dev/"$device" ] || { echo "Error: Invalid device '$device'"; exit 1; }
device=$(basename $(echo "$device" |sed -e 's/[0-9]$//g'))
host=$(ls -l /sys/block/$device/subsystem/ |grep "${device}$" |grep -oE 'host[0-9]+')
read -p "Rescan device or bus?: (D/b) " yn
case $yn in
d|D)
# Rescan the specific device
echo 1 > /sys/block/$device/device/rescan
;;
b|B)
# Rescan the bus
scan_bus "/sys/class/scsi_host/$host/scan"
;;
*) echo "Invalid option. Quitting"; exit 1;;
esac
else
for i in $(ls /dev/); do
if [[ "$i" =~ "sd" ]]; then
(echo -n "$i => "; readlink /sys/block/$i | cut -d '/' -f 6; echo "") | grep "host" || echo -n ''
fi
done
echo "Here are all of them: $(echo $(ls /sys/class/scsi_host))"
read -p "Which bus do you want to scan? (Which one is missing) " bus
full_bus="/sys/class/scsi_host/$bus/scan"
scan_bus "$full_bus"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment