[laptop-mode] RFC: USB disk spindown script

Johannes Niess email at johannes-niess.de
Sun Jun 22 18:57:25 CEST 2008


Hi list,

as laptop-mode is mostly concerned with disk spindown, my post might be on 
topic. I'd like to hear your comments about a possible solution to a common 
problem.

Most USB disk vendors include a proprietary spindown application for Windows, 
but nothing for Linux. With the current state of USB SCSI implementation in 
Linux only a few commands get executed on the IDE or SATA disk. The 
native "hdparm -S" or typical SCSI commands won't work to spin down USB 
disks. I've no idea about firewire.

I found some forum posts on using 'sdparm -C stop', but no suggested mechanism 
to track disk usage. I've written a poor mans implementation to spin down USB 
disks after inactivity. It is quite similar to 'smart spindown'.

The script below monitors /sys/block/$1/stat . When no change 
occurs 'sdparm -C stop' is issued. This has been tested on a Western Digital 
My Book and an inexpensive, stupid 'no name' enclosure. At least my disks 
start up again if necessary. I take no responsibility if the script bricks 
your disk or eats your data. But please let me know, so I could stop 
publishing,

Regards,

Johannes Niess


#! /bin/sh
# spindown contributed by Johannes Niess to laptop mode tools
device="/sys/block/$1/stat"
if test "$1" = ""; then
        echo 'Usage:  spindown <device> <time>'
        echo 'Spins down usb disk <device> after <time> of inactivity'
        echo '<device> is without path'
        echo 'see man sleep for <time>'
        echo 'Example: spindown sda 10m'
        echo
        echo 'Use at your own risk!'
        exit 1
fi
while true; do
        stat=`cat $device` || exit 1
        #echo $stat
        sleep $2 || exit 1
        if test "$stat" = "$oldstat"; then
                #echo "Stopping disk";
		# sync might spin up other disks
		#sync
                sdparm -q -C stop /dev/$1 || exit $?
        fi
        oldstat=$stat
done


More information about the laptop-mode mailing list