Spin down USB disks when not used

The script files used in this tutorial are currently missing.
Until they are restored by the author, you can use this alternative solution.

This article refers to K26 builds of Tomato.
The scripts do not work as-is with K24, and some concepts that are explained might not fully apply.
Contributions are welcome.

USB spin-down

Tomato USB comes with a builtin command called scsi-stop: you can invoke it at any time to force a spin-down of the USB disks. Spinning down the disks reduce the noise and dramatically decrease the power consumption; some disks externally show that the disk is spinned down by turning off a LED. There is no scsi-start command, since it is usually not required: as soon as the disk is accessed, it is automatically spinned-up by the kernel.

Depending on the exact kernel version and on the exact disk chipset, the scsi-stop command might now work or even make the kernel crash altogether, though it will never brick the disk nor cause data damage; so you can safely test it on your disk: the worst that might happen is that you will be forced to reboot your router.

Even if there is a manual command, most people would like the command to be manually executed whenever the disk is idle for a while. This tutorial explains how to achieve this beahavior.

Find out if your disks are compatible with scsi-stop

The first thing you will have to do is to find out whether your USB disks are compatible with scsi-stop or not, and write down the USB ids of the compatible disk. This might be tricky if you are not familiar with USB ids and Linux devices, so follow the following steps:

  • Unplug all your USB devices, to start in a clean situation.
  • Plug the first USB device you want to test.
  • Wait a few seconds for the disk to spin up and the kernel to recognize it.
  • Make sure the disk is now visible in the web interface (USB and NAS » USB Support).
  • In the router console, type the following command:
scsi-stop /dev/sda
  • If the disk is fully compatible, you should hear the disk spinning down. You might also see a LED turning off (but not all discs do this).
  • Now try accessing the disk. For instance, if the disk contains a partition which is mounted as /mnt/FOOBAR, you can do something like touch /mnt/FOOBAR/test-file; this command will create a zero-size file in the root of the partition. Alternatively, just open a file in it with your computer, or do whatever else you want. At this point, you should hear the disc spinning up.
  • Try again spinning it down and then up for a few times, to make sure everything is ok.
  • Have a look at the kernel messages by running dmesg | tail. Make sure there are no scary warning/error messages about USB drives or devices.
  • When you are positive that the spinning down and up mechanism is working correctly, you need to find out the USB ID of the disk. The quickest way is running the following commands in the console. The first command will show the vendor ID (VID) and the second command will show the product ID (PID).
# cat /sys/block/sda/device/../../../../idVendor
# cat /sys/block/sda/device/../../../../idProduct
  • Double check the IDs by looking at the output of the lsusb command. The command will list all the USB devices (including internal hubs). One of the lines should report the VID:PID couple. NOTE: if you use Optware's lsusb instead of the builtin one, you will also get some textual description for each device.

Repeat the above sequence for all your USB devices. Make sure to always disconnect all devices before testing one, so that you are sure that /dev/sda refers to the unique attached device.

Setting up idle_disks.sh

idle_disks.sh is a script which is meant to be run in background. It monitors disk activity of all your USB devices. Whenever it detects that a disk has not been used for a 15 minutes, it spins it down by invoking the scsi-stop command. The time interval can be configured.

Since not all disks are compatible with scsi-stop, idle_disks.sh will use a very conservative approach: it will handle only those dvices

# cd /tmp/root/home
# wget http://tomatousb.wdfiles.com/local--files/tut:spin-down-usb-disks-when-not-used/idle_disks.sh
# wget http://tomatousb.wdfiles.com/local--files/tut:spin-down-usb-disks-when-not-used/idle_disks.org
  • Assuming that you have done the steps above, you should already know the USB IDs of your disks (at least those compatible with the scsi-stop command. You now need to put those IDs in a whitelist so that idle_disks.sh will handle them.
      • First, create the whitelist directory, within your home:
mkdir idle_whl
cd idle_whl

* The whitelist
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License