Never Mail me: Hunny Never Mail me: Hunny Never click here!
powered by EUserv
This page is part of my Western Digital My Book World Edition scratch pad.

And still it moves! To spin or not to spin.

Summary

The point is not to set up an aggressive mode to force the disk to spin down. The point is to modify system tasks in a way they only write to disk while it is spinning anyway!

Sometimes you can read suggestions to spin down the disk after a rediculous short idle time, like 1 minute. This is a BAD BAD BAD idea!
You might want to know: Experts, even hard disk manufacturers, talk quite controversial about sleeping disks, stating that spinning up and down too frequently will age it more than letting it run. You might mean good to set up an aggressive configuration, but in fact hit the opposite.

Leaving an external hard drive powered on all the time?

Quoting Western Digital here:

The initial power-on process is generally harder on the internal components of a hard drive than spinning for extended periods. However, Western Digital drives are designed to handle either scenario. Most users outgrow their drive before repeated turning on and off becomes a problem. Turning on the drive a few times per day is considered normal usage and should not pose any problems. If a drive is turned on and off excessively on a daily basis, this could affect the longevity of the hard drive's components.

Having read this, I say:
Yes, generally I want to save electricity, reduce heat and noise.
No, I do not set up an agressive mode. I would say, even the 10 min in monitorio are a bit low. (I raised it to 15.)

So our aim will not be to put it to sleep too often, but for longer periods of time! I promise when you are through this little chapter, your drive will sleep all the time through as long as your computers and media players etc. don't ask it for something... This is a good reason to put those to sleep as often as possible as well! In additon, I personally was not even confident with a sleeping disk. I taught my box to shutdown by itself when there is nothing to do. (My way is to switch it on when I come home from work and need it for my computer or media. It will switch off itself after a lengthy time of idling.)

Optimising monitorio.sh

This little script /usr/bin/monitorio.sh watches disk IO to send it sleeping at the right time, then toggle the LED effects to reflect its state.

First I thought: Why have this script at all? Instead hdparm can program the disks to spin down after so much idle time, and you are done, the HW itself takes care. You do this by calling hdparm once with the right parameters. This is how most systems do it. At WD they did not, because they want to toggle the LEDs at the same time. As I did not care much for the lights, I actually lived without monitorio for some time, but later found it to be a nice low-weight tool to do a lot more things for me (like replacing cron and even being better than cron itself).

Besides what else I will do to this script later, there is a little bit of optimisation possible by just adding a single line (click this link to read why this helps, but be careful, they talk about another edition there, so the scripts have different names) just before the command to actually put the drive to sleep (hdparm -y)

[...]
ls -Rl /shares > /dev/null
/usr/sbin/hdparm -y /dev/sda >/dev/null
[...]

Spindown vs. Powerdown

I decided I want my MBWE to power down, when and if it is obviously not needed. This is difficult to judge, but I decided and implemented this:
Nobody wants to talk to me! Am I alone? If it wants to spindown it also checks if the other systems that usually want its data are even there. Ping all of them (I wrote AmIalone.sh for that) and if nobody answers, go down! In monitorio.sh you add:

AmIalone.sh && { /etc/rc.poweroff ; exit } # this line just before:
hdparm -y ...

This will power down instead of just stop the disk if and when it's clients are sleeping anyway. Of course the backside is, once in a while you have to power it on when you decide to listen to your media NOW and found your box off unexpectedly. Oh how much I would like the box to support wake on lan (WoL). But as far as I could find out so far, the HW does not support this :-(

Cron? anacron?! MyAnaCron!

Summary

In this chapter we found jobs that cause disk access every so often and stay resident in memory. Instead we want to make these jobs one-time runners and do their work at a convenient moment. Here I develope an approach to have an almost cron-like behaviour, but with the additional effect to try to let disks sleeping. I make use of the fact, that none of the aforementioend jobs need to run at a really specific time or even intervall.

What is wrong with cron?

Nothing is wrong, and some poeple in the Wiki suggest to install cron for all the little niceties it can do for you. But this collides with our goal to have the box on a diet concerning noise and energy! Cron uses resources we don't care much about on a standard system, but on the MBWE we do. For myself I found a better way. Check if you can make use of this too. You can, if your cronjobs do not have to run at exactly pre-scheduled times, but a description like after-start, hourly, daily, weekly...(an idea invented by anacron) is good enough for you and missing (that is delaying for an undefined time) a job is not critical. In return for accepting this you will keep a light-weight system that still behaves like it almost had a cron. (If you are looking for something like this for your full blown-up unix/linux system, you should try my YAcron.)

Here is the idea: Do your jobs while the system is idle, but don't wake it up either!

You already know /usr/bin/monitorio.sh. (If not, go back and read this first.) It is a script that already does some cron-like work: It wakes up every minute to see if something has to be done (putting disks to sleep and toggle the LEDs accordingly). It could be used to do all kind of things, like starting other programs on a regular basis. As its primary function is to watch over disk activity, it knows the disk's state and can keep the silence while the disk is down. On the other hand it can check activity and hold back your cron jobs to not impact performance as long as there is a lot of system load already.

Okay, how can we do that? When is a good time to let the box do its housekeeping? I decided, this is when IO ceased, but the disks are still spinning. At this time system load is low and the box has nothing better to do than work on our cronjobs! We most likely waste our sleepcount, because calling anacron might cause IO. But hey, I only sacrify one minute, then the box will sleep all the better later. So I hook in the inner loop (while :, do) and check if sleepcount=1 (no disk activity since a minute). Now call a script that does all our cronjobs. We must remember we did our jobs, so we do not disturb the sleepcount again next minute. We raise a flag (firstleep) to remember this and won't start again on next sleepcount=1. We lower the flag again next time the disk wakes up anyway (in the outer loop), so next time before spindown our cron runs again.

[...snip...]
firstsleep=true # NEW: initialise our flag, so we run only once each spin-up/down
while :; do
/bin/sleep 60
iow=`/bin/cat /proc/diskstats | /bin/grep -E 'sda ' | /usr/bin/cut -d' ' -f16`
ior=`/bin/cat /proc/diskstats | /bin/grep -E 'sda4' | /usr/bin/cut -d' ' -f10`
[ -z "$IOR" ] && IOR=0
io2=`/usr/bin/expr $iow + $ior`
if [ "$MODEL" == "2NC" ];then
ior=`/bin/cat /proc/diskstats | /bin/grep -E 'sdb4' | /usr/bin/cut -d' ' -f10`
[ -z "$ior" ] && ior=0
io2=`/usr/bin/expr $io2 + $ior`
fi
if [ "$io" -eq "$io2" ]; then
sleepcount=$((sleepcount+1))
# NEW 6 lines: call our "XXXcron" at "convenient" times
if [ "$sleepcount" -eq "1" ]; then
if $firstsleep ; then
firstsleep=false
/path_to_your/anacron # <<<< this is it!
fi
fi
[...snip...]

Here we call the little program that actually does the work. (I decided against backgrounding, since monitorio has nothing to do while anacron does its job.) If anacron is good enough for you, get it via ipkg and use it! Your advantage will be you use a standard tool. Only if you need a higher granulation than "daily" you need to think about another solution. For this reason I developed my own version and called it myanacron.sh. Whenever it is called it checks when it indeed did run the hourly, daily, weekly... jobs and will run those that need to. This is done with help of simple timestamp files as flags.

The trick with both anacron and myanacron is to have it called at a convenient time (concerning economy and performance) through monitorio.sh!

Both my complete monitorio.sh and MyAnacron will be available soon!

Do not forget to disable cron!

I gathered, in some firmware versions, cron was enabled by default. If you follow the approach given here, you SHOULD disable it! Otherwise you wouldn't have gained much. Read the crontabs and transfer the commands to anacron.

What can and should be done by (my)anacron?

Everything that cron has possibly done before you disabled it, plus all the standard jobs the system itself wants to run once in a while. I call these the housekeeping. logrotate, time sync, clean tmp, are the typical examples.

Own usefull jobs. For example, I created a script that builds a playlist to be used by my media station. It writes a m3u that includes "long not played" songs. This is overwritten every so often, so I point my player to always use the same playlist name, still I never listen to a same song twice for weeks!

Any kind of backup or other data protection measures. I call my script to save the configuration regularly so I can recover from any disaster I provoked ;-) I create a poor man's shadowcopy. I create my lists for my simple locate that is defined as a shell function in .profile. You can find so many more things.