Created
December 11, 2019 12:56
-
-
Save qiuchengxuan/041a8cfb2331c8a1af241a4cd2ebb7e5 to your computer and use it in GitHub Desktop.
A simple script to hotplug hard drives on linux
This file contains hidden or 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 | |
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