views:

370

answers:

4

I've written a script to backup my server's HD every night. At the end of the script, I sync, wait a couple of minutes, sync and then I issue sg_start --stop to stop the device. The idea is to extend the lifetime of the device by switching the HD off after ten minutes of incremental backup (desktop disks will survive several thousand on/off cycles but only a few hundred hours of continuous running).

This doesn't always work; I often find the drive still spinning the next morning. Is there a shell command which I can use to check that the drive has stopped (so I can issue the stop command again [EDIT2]or write a script to create a process list when the drive is running so I can debug this[/EDIT2])?

[EDIT] I've tried sg_inq (as suggested by the sg_start man page) but this command always returns 0.

I've tried hdparm but it always returns "drive state is: unknown" for USB drives (attached via /dev/sdX) and when trying to spin down the drive, I get "HDIO_DRIVE_CMD(setidle1) failed: Input/output error".

sdparm seems to support to set the idle timer on the drive (see "Power mode condition page") but the IDLE option has "Changeable: n" and I haven't found an option which tells me the drive power state.

[EDIT2] Note: I can stop the drive with sg_start --stop from the console. That always works; it just doesn't always stay down until midnight. The sever is in the basement (where it's nice and cool) and I'd rather have a way to check whether the drive is up or not from the warm living room :) If I had a command which told me the status of the drive, I could write a script to alert me when it spins up (check every minute) and then I could try to figure out what might be causing this.

If that matters: I'm using openSUSE 11.1.

A: 

I often find the drive still spinning the next morning.

Couldn't this just be because it was spun up again when the server eg. wrote to a log file or something during the night? You might try sdparm, which can return status information on a drive. But I think it's better to just set the option in your BIOS that lets your HD automatically spin down after an amount of inactivity, it's easier.

FredV
Nothing accesses the drive but the backup script. It runs once (find -newer | cpio) and that's it.
Aaron Digulla
+2  A: 

When you say you've tried hdparm, you haven't said what you have tried. I have some usb hard drives in an enclosure, and some of the commands work for it and others don't, but I guess it all depends on all facets of the transport mechanism.

  hdparm  -S 120  /dev/sda

Should tell the drive to sleep itself after ~10 minutes of inactivity.

I'm guessing you have already tried this, but its not obvious, and writing this as an answer may help a future reader.

Nothing accesses the drive but the backup script.

This is nice in theory, but I have found on odd occasions this is not enough. There are lots of processes and tasks that if they even look at the drive a spin up may occur if the lookup is for some reason out of the disk-cache.

Common culprits are tools like updatedb scanning all mounts for files, and fam or gamin doing funky stuff to monitor disks for changes.

If in doubt, add a layer of certainty by mounting the device before executing the script, and unmounting it when you are done.

Seeing things that could cause a wakeup

 lsof +D /mountpoint

You should probably parse the output of this as well before attempting to unmount to be sure that nothing is still trying to use it.

You should probably also be doing a lazy umount,

 umount -l /mountpoint

so if anything accesses it between you doing lsof| grep and calling umount, it will still unmount the drive and stop things being able to read it.

HAL and friends

Its also possible that HAL and friends are doing wakeups to the drive probing for connect/removal state. I really hope it isn't, but it does that on some device types. It seems an unlikely cause, but I'll consider anything possible.

Try fun stuff like

lsof /dev/devicehere

and

lsof /dev/devicehere1

Which appears to get a comprehensive list of all things that would be accessing the handle either directly or indirectly.

Kent Fredric
A: 

Have you tried stopping the drive with the following command:

eject -t /dev/yourHD

This works quite good for my USB hard drives.

Bob
I'm still looking for a way to *check* whether the drive is spinning or not :)
Aaron Digulla
+1  A: 

You also need to check the mount options and use "noatime" as a mount option, otherwise the kernel still updates the access times periodically. So this may be the cause of your problem too.

Tom Deblauwe