Backstory
About a year ago I bought an external SATA drive for backups. My normal usage consisted of:
- Power on and connect the drive
- mount /media/backup
- Run my backup script
- umount /media/backup
- Power off and unplug the drive
This seemed to work pretty well–at the very least, I wasn’t losing data–except the drive made a strange sound when I powered it off. It wasn’t a normal drive spin down sound; it was louder and shorter. So, I googled for authoritative instructions on using external drives with Linux. While most sources suggest doing exactly what I did, it’s not ideal.
It turns out that most cheap external USB/SATA/firewire enclosures don’t properly issue a stop command to the drive when you flick the power switch. Instead, the power switch simply cuts power to the drive, which forces the drive to do an emergency head retract. If you think that sounds bad, you’re right. Emergency retracts aren’t going to brick your drive immediately, but if they occur regularly they’re putting a lot of unnecessary wear and tear on the drive. In fact, some drives monitor how often this happens with S.M.A.R.T. attribute 192. (Check Wikipedia’s S.M.A.R.T. page for a comprehensive list of attributes)
Solution
The solution is to spin down the drive via software before turning it off and unplugging it. The best way to do this is with a utility called scsiadd. This program can add and remove drives to Linux’s SCSI subsystem. Additionally, with fairly modern kernels, removing a device will issue a stop command, which is exactly what we’re looking for. Run:
$ sudo scsiadd -p
which should print something like:
Attached devices: Host: scsi0 Channel: 00 Id: 00 Lun: 00 Vendor: ATA Model: SAMSUNG HD300LJ Rev: ZT10 Type: Direct-Access ANSI SCSI revision: 05 Host: scsi4 Channel: 00 Id: 00 Lun: 00 Vendor: LITE-ON Model: DVDRW LH-20A1L Rev: BL05 Type: CD-ROM ANSI SCSI revision: 05 Host: scsi5 Channel: 00 Id: 00 Lun: 00 Vendor: ATA Model: WDC WD10EACS-00Z Rev: 01.0 Type: Direct-Access ANSI SCSI revision: 05
Identify the drive you want to remove and then issue:
$ sudo scsiadd -r host channel id lun
substituting the corresponding values from the scsiadd -p output. For example, if I wanted to remove “WDC WD10EACS-00Z”, I would run:
$ sudo scsiadd -r 5 0 0 0
If everything works, scsiadd should print:
Attached devices: Host: scsi0 Channel: 00 Id: 00 Lun: 00 Vendor: ATA Model: SAMSUNG HD300LJ Rev: ZT10 Type: Direct-Access ANSI SCSI revision: 05 Host: scsi4 Channel: 00 Id: 00 Lun: 00 Vendor: LITE-ON Model: DVDRW LH-20A1L Rev: BL05 Type: CD-ROM ANSI SCSI revision: 05
You can double-check the end of dmesg. You should see:
[608188.235216] sd 5:0:0:0: [sdb] Synchronizing SCSI cache [608188.235362] sd 5:0:0:0: [sdb] Stopping disk [608188.794296] ata6.00: disabled
At this point, the drive is removed from Linux’s SCSI subsystem and it should not be spinning. It’s safe to unplug and turn off.
Using scsiadd directly can be inconvenient because it requires looking up the host, channel, id, and lun of the drive. I wrote a short script that will take a normal Linux device file like /dev/sdb, figure out the correct arguments to scsiadd, and run scsiadd -r. I use this script in my larger backup script.
#!/bin/sh if [ $# -ne 1 ]; then echo "Usage: $0" exit 1 fi if ! which lsscsi >/dev/null 2>&1; then echo "Error: lsscsi not installed"; exit 1 fi if ! which scsiadd >/dev/null 2>&1; then echo "Error: scsiadd not installed" exit 1 fi device=`lsscsi | grep $1` if [ -z "$device" ]; then echo "Error: could not find device: $1" exit 1 fi hcil=`echo $device | awk \ '{split(substr($0, 2, 7),a,":"); print a[1], a[2], a[3], a[4]}'` scsiadd -r $hcil
It does require the lsscsi command to be present on the system.
11 replies on “Safely Removing External Drives in Linux”
Thanks. :)
I’ve just bought an external sata housing and came across this while doing a bit of research for it. That script of yours will save me some time.
Thanks again.
Does not work here (Arch Linux 2.6.30). But calling sdparm (twice) does work:
sdparm –command=stop $device
‘eject’ should also do the same job — it either ejects a CD-ROM drive, or will also (IME) spin down hard drives. For example:
$ sudo eject /dev/sdd
spins down /dev/sdd
When I was originally writing this, eject didn’t actually spin down the hard drive, it just removed the device from the system. That said, eject could have been improved since.
Thank you very much for this! It appears to work in Ubuntu Jaunty – at least all the right messages are coming up. And I can even hear a kind of soft “clink” sound as soon as I issue the command – which I think is the heads parking. I still have a rather loud sound when cutting the power to the drive, however, but maybe it’s the fan shutting off combined with the click of the switch – I’m not sure. Was there a noticable change in the sound when you cut the power after doing this? I’m using an eSATA enclosure – it’s a Rosewill brand that cost about $40.00.
I wish there were a way to be absolutely sure the drive has spun down properly. But I do get this with dmesg, so maybe that should be assurance enough:
sd 6:0:0:0: [sdb] Synchronizing SCSI cache
sd 6:0:0:0: [sdb] Stopping disk
ata7.00: disabled
Again, thanks very much for this!
Yes, I could hear the drive spin down. It also stopped (every so slightly) vibrating.
Thank you for all the info very useful for us I will try this method to backup my fiele server and media server thank you again
Thank you for inspiring me to write a GUI for just this (see screenshots):
http://github.com/ingob/dfmon
This tool lists all scsi devices in the system and umounts/removes them (with truecrypt support) from the scsi bus at request. Often, I want to remove SATA devices from the system which are mounted with truecrypt.
Atm, it’s a python script which uses Qt. I’ll add a standalone binary soon.
What do you think about this ?
Updated link: https://github.com/ibressler/dfmon
As I am aware of some of the linux command this would be little bit easier task to handle the situation as i understand what each command says. Your script will be really useful as well as will save lot of my time.
I also found a command “scsi-spin” on my Ubuntu 10.04 system. It can work on most disks since the disk layer has been abstracted into SCSI (even for IDE, USB, and so on). I actually wanted to see what my wattmeter would say with the main disk not spinning, so I issued scsi-spin –force –down /dev/sda. (–force is necessary because it will warn you otherwise that the disk you’re trying to deactivate is mounted.) After that, I simply issued init 6; it spun up sda without any further help (kernel must have realized it would need to do that, and issued the appropriate command to the disk), and executed a reboot.
Removing the disk from the system is OK, but scsi-spin is a more targeted, direct way of doing it. Removing the USB connector from the system or otherwise causing the disk to disconnect from the USB will prompt the system (probably udev) to remove it as well.