Skip to content

Instantly share code, notes, and snippets.

@tommybutler
Created October 12, 2013 18:56
Show Gist options
  • Save tommybutler/6953563 to your computer and use it in GitHub Desktop.
Save tommybutler/6953563 to your computer and use it in GitHub Desktop.
Force a disk offline and power it down in Linux
#!/bin/bash
# AUTHOR: Tommy Butler
#
# DESCRIPTION:
# Run this script to offline and delete a disk from your Linux system.
# It should work for most people, but if you've got an old kernel it may not.
# Unless you know what you're doing, DO NOT USE THIS SCRIPT!
#
# LICENSE: Perl Artistic License - http://dev.perl.org/licenses/artistic.html
#
# DISCLAIMER AND LIMITATION OF WARRANTY:
# This software is distributed in the hope that it will be useful, but without
# any warranty; without even the implied warranty of merchantability or fitness
# for a particular purpose. USE AT YOUR OWN RISK. I ASSUME NO LIABILITY.
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DISK=$1;
if [[ `id -u` -ne 0 ]];
then
echo Run this script with root or sudo
exit 1
fi
while true;
do
[[ "$DISK" != "" ]] && break;
read -p 'Enter the name of the disk you want to offline and delete: ' DISK
done
if [[ "$( expr substr $DISK 1 4 )" == '/dev' ]];
then
DISK=$( expr substr $DISK 6 10 )
fi
if [[ ! -e /sys/block/$DISK ]];
then
echo No entry for /dev/$DISK was not found in /sys/block/ - Cannot continue
exit 1
fi
echo Are you sure you want to offline and delete /dev/${DISK}?
select yn in "Yes" "No"; do
case $yn in
Yes ) break;;
No ) exit;;
esac
done
echo offline > /sys/block/$DISK/device/state
echo 1 > /sys/block/$DISK/device/delete
echo DONE
exit;
@elasti-GuyT
Copy link

hi
it worked perfectly.
is there any way to get the drive back online?
i tried to make the bus to rescan but the device didnt came back.
thanks!

@mailinglists35
Copy link

mailinglists35 commented Jan 18, 2020

the difference between offline and 1 > delete is that offline only makes the disk unavailable to the block subsystem, while the latter physically tells the drive to power down.

# find /sys/bus/scsi/devices/host*/target*|grep state
/sys/bus/scsi/devices/host2/target2:0:0/2:0:0:0/state
/sys/bus/scsi/devices/host2/target2:0:0/2:0:0:2/state
/sys/bus/scsi/devices/host2/target2:0:0/2:0:0:1/state

# cat /sys/bus/scsi/devices/host2/target2:0:0/2:0:0:1/state
offline

the drawback is that if offlined first, apparently the kernel remembers it is in the offline state, so it does not come back online after rescan, so you must online it like this:
the problem is that the magic word to return online is not "online", but "running"... so who wrote the code had some reason to not call it "online"

# echo online > /sys/bus/scsi/devices/host2/target2:0:0/2:0:0:1/state
-su: echo: write error: Invalid argument

# echo running > /sys/bus/scsi/devices/host2/target2:0:0/2:0:0:1/state
#

this seems to be useful when you really want just to make the disk temporarily unavailable but not power it down, so instead of deleting it, you would just offline/running

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment