Skip to content

Instantly share code, notes, and snippets.

@qiuchengxuan
Created December 11, 2019 12:56
Show Gist options
  • Save qiuchengxuan/041a8cfb2331c8a1af241a4cd2ebb7e5 to your computer and use it in GitHub Desktop.
Save qiuchengxuan/041a8cfb2331c8a1af241a4cd2ebb7e5 to your computer and use it in GitHub Desktop.
A simple script to hotplug hard drives on linux
#!/bin/bash
entries=($(find /dev/disk/by-id -type l -printf '%l %f\n' | grep -v '\(part\|wwn\)' | sed 's#../../##g' | tr '\n' ' '))
entries+=('' '' 'rescan' 'RESCAN')
choice=$(whiptail --notags --title "Hotplug" --menu "Select device" 16 100 9 "${entries[@]}" 3>&1 1>&2 2>&3)
test -n "$choice" || exit 0
case $choice in
"rescan")
echo 'Rescan disks...'
echo '- - -' > /sys/class/scsi_host/host0/scan
;;
*)
echo "Unplug $choice"
[ -e /dev/$choice ] || exit -1
echo 1 >/sys/block/$choice/device/delete
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment