Last active
April 3, 2024 15:58
-
-
Save wnielson/4503779 to your computer and use it in GitHub Desktop.
A very simple script that can be used to hot-swap an SATA device running in AHCI mode on Linux.
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 | |
# | |
# Usage: | |
# | |
# hotswap.sh [dev] | |
# | |
# Where [dev] is the name of the device such as "sda". Do not use | |
# partitions (like "sda1"). | |
# | |
# Example: | |
# | |
# hotswap.sh sdb | |
# | |
# Source: http://askubuntu.com/questions/28600/ahci-hotswap-in-lucid-server | |
# | |
device=/dev/$1 | |
cwd=`pwd` | |
if [ -b "$device" ] | |
then | |
a=`printf '%d\n' \'a` | |
d=`echo ${1:\`expr ${#1}-1\`}` | |
d=`printf '%d\n' \'$d` | |
d=`expr $d - $a` | |
c=`dmesg | grep scsi$d | grep ahci` | |
if [[ "$c" != "" ]]; then | |
echo "Make sure that you have unmounted all partitions on this disk!" | |
read -p "Press [Enter] if all partitions are unmounted" | |
cd -P /sys/block/$1/device | |
echo 1 > delete | |
cd ../../scsi_host/host* | |
read -p "Swap drives and press [Enter] to scan for new drive" | |
echo '- - -' > scan | |
cd $cwd | |
fi | |
else | |
echo "Invalid device." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment