Skip to content

Instantly share code, notes, and snippets.

@wnielson
Last active April 3, 2024 15:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wnielson/4503779 to your computer and use it in GitHub Desktop.
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.
#!/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