USB Hard drive spindown fix on Linux

Posted by Landon Cox on November 21st, 2008 in Open Source, Tutorials, technology

I wanted to share a fix for something that has been driving me insane on an Ubuntu Fiesty box (though don’t think it’s at all limited to that distro). This fix may be common knowledge, but, if you’re like me and you don’t know about it, then it’s not common, eh?

I have a 500G Maxtor USB drive as a backup drive for a Dell pizza box running Ubuntu Fiesty. I formatted it ext2, got it mounted, ran backup data to it, no problems. I made a daily cron job to do backups, but every morning when I’d look at my cron notifications, I would discovery that the backup failed because the file system was read-only which is not how I left it.

I would log in to my box, try to make a file on the backup drive and sure enough, couldn’t because it was read only. I’d unmount it, then remount it and it would be fine. By the next morning it was read-only again.

I reformatted the drive to ext3, reiserfs…no matter what, I had problems. Looking at log messages I would see things like this:

Nov 19 07:29:54 hurricane kernel: [724306.332761] sd 4:0:0:0: Device not ready: : Current: sense key: Not Ready
Nov 19 07:29:54 hurricane kernel: [724306.332771] Additional sense: Logical unit not ready, initializing command required
Nov 19 07:29:54 hurricane kernel: [724306.332781] end_request: I/O error, dev sdb, sector 10706
Nov 19 07:29:54 hurricane kernel: [724306.332785] printk: 4 messages suppressed.
Nov 19 07:29:54 hurricane kernel: [724306.332797] lost page write due to I/O error on sdb1

where sdb1 was my USB backup drive partition. After digging I finally found a thread:

http://ubuntuforums.org/showthread.php?t=494673

that talked about a similar issue. Some people saw it on NTFS, FAT32, etc - it has nothing to do with the drive format as far as I can tell.

The problem is that some drives will spin down (I suppose to save energy), but that hoses up the filesystem and kernel for writing to it.

For me, the fix was similar to that suggested in the link above:

1) create a file in /etc/udev/rules.d called 85-usb-hd-fix.rules and add this line to it:

BUS==”scsi”, SYSFS{vendor}==”Maxtor”, RUN+=”/usr/bin/usbhdfix %k”

(where you modify the vendor to be appropriate)

2) in /usr/bin add a file: usbhdfix and put this content in it:

#!/bin/bash
# http://ubuntuforums.org/showthread.php?t=494673

echo 1 > /sys/block/$1/device/scsi_disk:*/allow_restart

3) of course, chmod +x usbhdfix as appropriate for you.

After this, my backups never failed again due to the read-only issue.

Apparently a kernel fix is coming or is already there…I didn’t chase that down.

Hope this helps someone,

About me

I’m an independent consultant who used to do a lot of work for OpenLogic. I greatly appreciate OpenLogic and the mission they are fulfilling as well as their willingness to let me contribute to this blog. Views expressed here are not necessarily those of OpenLogic and any mistakes are 100% attributable to me. You can contact me at: landon at 360vl dot com or visit http://sawdust.see-do.org

Bookmark: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • Reddit
[Trackback URI]

Leave a Comment