Script to Offline and Remove A Disk In Linux

By | October 12, 2013

This script will offline and remove a hard drive from your Linux machine.  That doesn’t mean it physically pulls it out of your server 😉  Instead the script spins down the disk, removes it from your server’s list of devices under /dev, and powers it off. This is really handy if your system keeps locking up because of a bad disk.  Honestly, because there’s risk of corruption and data loss if you don’t run backups and cleanly dismount any filesystems on the disk before you run this script, I put a BIG FAT DISCLAIMER on the code.  So basically just don’t be a moron; if you don’t know what you are doing or what happens when you offline and remove a disk in Linux, then don’t use this script!

For those of you who do know what you’re doing, I hope it helps you get the job done more easily and safely.  It’s actually quite simple, and should you desire, you can just take the relevant commands from the source code of the script below and run them without the script at all–in which case you are still doing so at your own risk and I assume no liability whatsoever.  Second time: don’t use this unless you really and truly know what you’re doing first!

Call the script with the name of the disk you want to remove as the first argument. If you supply no argument, you will be prompted to enter one. Then after a few sanity checks on your input, you will be asked to confirm if you really want to proceed. That in and of itself is justification enough for the existence of the script–to attempt to make sure you don’t delete the wrong device by mistake which risk exists when doing it manually.

Third time: don’t use this unless you know what you’re doing and are aware beforehand of what will happen when you do.

Have Fun and be careful!