First

Before we are going into depth, first check if the (external) hard disk is able to go in spindown. Disable all services which has disk access periodically, for example:

$ /etc/init.d/asterisk stop
$ /etc/init.d/rsyslog stop
$ /etc/init.d/apache2 stop
$ /etc/init.d/mysql stop
$ /etc/init.d/samba stop
$ /etc/init.d/cron stop
$ /etc/init.d/ntp stop
$ /etc/init.d/rsync stop

And all other daemons which are suspicious…

–MARK– in /var/log/messages

In /var/log/messages every 30 minutes a —- MARK —- row will be written. This can be switched off by configuring the rsyslogd. Open the rsyslog.conf file and comment out the “$ModLoad imark” by putting a ‘#’ before this:

$ vi /etc/rsyslog.conf
#$ModLoad immark # provides --MARK-- message capability

Adding the noatime mount option in /etc/fstab

By default the access times of directories and files are stored with this file or directory. This will keep the hard disk spinning, even when a file is read from the cache. With the noatime mount option in fstab, this access times will not be kept anymore. To add noatime, open fstab and add the (bold) noatime option to the hard disk partitions. Do not do this with the swap space(s).

$ vi /etc/fstab
$ /dev/sda1
UUID=2e0e754e-7dc2-4c21-9101-b539b8a501a8  /       ext4   errors=remount-ro,noatime 0   1
$ /dev/sda3
UUID=a4a2e073-1315-4051-9811-6a1831d25327  /home   ext4   noatime,noatime           0   2

Searching for the changed file

To search to files which are continuously changing on the hard disk, I made a script for which files are changed in the past XX minutes. Knowing which files are changing, you can find a solution for this. For example moving the file on a ram disk (tmpfs). To create the script, do the following:

$ vi /usr/local/sbin/ff_changed

#!/bin/sh
echo Search whole filesystem for changed files in the last $1 minutes
find /bin /dev /etc /home /lib /media /mnt /opt /root /sbin /srv /tmp /usr /var -cmin -$1

And make the ff_changed script executable:

$ chmod +x /usr/local/sbin/ff_changed

Disable all cronjobs

We need the cronjobs, but maybe not all. You can make it maybe a little bit more specific to your task. But first we are going to disable them all to look if the hard disk will spin down or not. Put a ‘#’ before every cron job. Like in this example:

$ vi /etc/crontab

# 17 *  * * *   root    cd / && run-parts --report /etc/cron.hourly
# 25 6  * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
# 47 6  * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
# 52 6  1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

Samba

In case you using Samba, probably your hard disk will not spindown, because browse.dat in the /var/cache/samba directory is very often updated. This will keep the hard disk spinning. Mounting this directory as a tmpfs (ram disk) will fix this problem. The total size of the files in this directory is 75k. To create the tmpfs, open the samba startup file and add the bold mount and umount rows to /etc/init.d/samba:

$ vi /etc/init.d/samba

#!/bin/sh

### BEGIN INIT INFO
# Provides:          samba
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $network $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Should-Start:      slapd
# Should-Stop:       slapd
# Short-Description: start Samba daemons (nmbd and smbd)
### END INIT INFO

# Defaults
RUN_MODE="daemons"

# Reads config file (will override defaults above)
[ -r /etc/default/samba ] && . /etc/default/samba

PIDDIR=/var/run/samba
NMBDPID=$PIDDIR/nmbd.pid
SMBDPID=$PIDDIR/smbd.pid

# clear conflicting settings from the environment
unset TMPDIR

# See if the daemons are there
test -x /usr/sbin/nmbd -a -x /usr/sbin/smbd || exit 0

. /lib/lsb/init-functions

case "$1" in
        start)
                log_daemon_msg "Starting Samba daemons"
                mount -t tmpfs tmpfs /var/run/samba
                mount -t tmpfs tmpfs /var/cache/samba

                # Make sure we have our PIDDIR, even if it's on a tmpfs
                install -o root -g root -m 755 -d $PIDDIR

                NMBD_DISABLED=`testparm -s --parameter-name='disable netbios' 2>/dev/null`
                if [ "$NMBD_DISABLED" != 'Yes' ]; then
                        log_progress_msg "nmbd"
                        if ! start-stop-daemon --start --quiet --oknodo --exec /usr/sbin/nmbd -- -D
                        then
                                log_end_msg 1
                                exit 1
                        fi
                fi

                if [ "$RUN_MODE" != "inetd" ]; then
                        log_progress_msg "smbd"
                        if ! start-stop-daemon --start --quiet --oknodo --exec /usr/sbin/smbd -- -D; then
                                log_end_msg 1
                                exit 1
                        fi
                fi

                log_end_msg 0
                ;;
        stop)
                log_daemon_msg "Stopping Samba daemons"
                log_progress_msg "nmbd"

                start-stop-daemon --stop --quiet --pidfile $NMBDPID
                # Wait a little and remove stale PID file
                sleep 1

                if [ -f $NMBDPID ] && ! ps h `cat $NMBDPID` > /dev/null
                then
                        # Stale PID file (nmbd was succesfully stopped),
                        # remove it (should be removed by nmbd itself IMHO.)
                        rm -f $NMBDPID
                fi

                if [ "$RUN_MODE" != "inetd" ]; then
                        log_progress_msg "smbd"
                        start-stop-daemon --stop --quiet --pidfile $SMBDPID
                        # Wait a little and remove stale PID file
                        sleep 1

                        if [ -f $SMBDPID ] && ! ps h `cat $SMBDPID` > /dev/null
                        then
                                # Stale PID file (nmbd was succesfully stopped),
                                # remove it (should be removed by smbd itself IMHO.)

                                rm -f $SMBDPID
                        fi
                fi

                umount /var/run/samba
                umount /var/cache/samba

                log_end_msg 0

                ;;
        reload)
                log_daemon_msg "Reloading /etc/samba/smb.conf" "smbd only"

                start-stop-daemon --stop --signal HUP --pidfile $SMBDPID

                log_end_msg 0
                ;;
        restart|force-reload)
                $0 stop
                sleep 1
                $0 start
                ;;
        status)
                status="0"
                NMBD_DISABLED=`testparm -s --parameter-name='disable netbios' 2>/dev/null`
                if [ "$NMBD_DISABLED" != "Yes" ]; then
                        status_of_proc -p $NMBDPID /usr/sbin/nmbd nmbd || status=$?
                fi
                if [ "$RUN_MODE" != "inetd" ]; then
                        status_of_proc -p $SMBDPID /usr/sbin/smbd smbd || status=$?
                fi
                if [ "$NMBD_DISABLED" = "Yes" -a "$RUN_MODE" = "inetd" ]; then
                        status="4"
                fi
                exit $status
                ;;
        *)
                echo "Usage: /etc/init.d/samba {start|stop|reload|restart|force-reload|status}"
                exit 1
                ;;
esac

exit 0

Asterisk

This depends on how you configure Asterisk on what the hard disk will spin up. What I found out with debug tools, is that the file astdb accessed and changed a lot. Also when you do not use the Asterisk DB. If you do not use the logging, you can also comment this out. Change this in the file asterisk.conf:

$ vi /etc/asterisk/asterisk.conf

[directories]
; astvarlibdir => /opt/var/lib/asterisk      ; Commented out to spinning down harrdisk
; astlogdir => /opt/var/log/asterisk         ; Commented out to spinning down harddisk

Installing and configuring hdparm

I tested tools/modules like laptop-mode-tools, pm-utils en noflushd. But these did not work well. I had more success with hdparm. This command will not work on external USB hard disks, via SATA it works perfect. Open the /etc/hdparm.conf file and set the desired spindown time:

$ vi /etc/hdparm.conf

/dev/sda {
    #spindown_time = 60  # 5 min
    #spindown_time = 240 # 20 min
    #spindown_time = 250 # 30 min
    #spindown_time = 280 # 1 hour
    spindown_time = 340 # 2 hours
}

To check if the hard disk is standby, you can use the following command:

$ hdparm -C /dev/sda

/dev/sda:
drive state is:  standby

See also: manpages.ubuntu.com/manpages/hardy/en/man8/hdparm.8.html

The lifetime of the hard disk

Remark! Switching off the hard disk too often is not good for the lifetime of the hard disk. It is also not very handy to wait every time for spinning up the disk. At home I’ve set the spindown time to 30 min. For business environment I am using 2 hours spindown time or let it active for the whole working day. Otherwise the disk will be switched off to often.

Manufacturers specifying that the hard disk can be switched on and off for 200.000 times in it’s life. With a lifetime of 5 years, that is 40.000 times a year and 109 times a day. Keep in mind that in the weekends and holidays the server shall not be used. If you set a spindown_time of 10 min. Multiplying with 109, this is 18 hours a day. This is not bad, but 10 min is the limit in this case. It is beter to switch off less ofthen.

Story about this: ubuntudemon.wordpress.com/2007/10/28/laptop-hardrive-killer-bug-how-to-discover-whether-you-are-affected

NTP

The NTP synchronizes the time continuously. This is not always necessary. Sometimes it is also OK to synchronizes the time at once. To disable NTP, you stop NTP and put the following in the top of the NTP startup script:

$ /etc/init.d/ntp stop
$ vi /etc/init.d/ntp

exit 0 # disable NTP

Or just remove the NTP package with

$ apt-get remove ntp

Now we need to install ntpdate with

$ apt-get install ntpdate

Try the ntpdate command with:

$ ntpdate pool.ntp.org

Add this to the crontab and synchronize the time every day on 5:15 in the night or just a minute before another periodically job:

$ vi /etc/crontab
15  5   *   *   *  /usr/sbin/ntpdate pool.ntp.org  &>/dev/null

Activating the harddisk

Tip: Keep the hard disk spinning during the day

It is not always handy when the hard disk spins down. For example with Asterisk (VOIP), it takes some time before the hard disk is up. Also for a file server it is good that it remains active during working hours.

What I use is a simple cron job, which is executing a ‘ls’ command somewhere in a nearly empty directory on that hard disk. In case the hard disk is going in sleep after 10 min, it is good to execute the command every 9 minutes. With the following cron job, the disk remains active from 8:00 till 18:00 from monday (1) till friday (5). ‘ls’ will be executed every 9 minutes:

$ vi /etc/crontab
*/9  8-18  *   *   1-5  /bin/ls /home  &>/dev/null

Activate the hard disk when somebody calls (in case of Asterisk)

The following solution will activate the hard disk, before the voice respond menu will be active. In the time the telephone rings, the hard disk has time to spin up and is ready for the voice respond menu. This can be done like this:

$ vi /etc/asterisk/extensions.conf
[external]
exten => s,1,System(ls /opt) ; Execute a command on a non-cached disk, to spinup the hard disk
exten => s,n,Dial(SIP/501&SIP/502,30) ; The telephones are ringing for 30 seconds

In more difficult cases

In some case it is not enough to find the changed files with the ff_changed script. In that case ‘strace’ is very handy. ‘strace’ shows all system calls and signals. It generates a lot of logging, but you can find the one who wakes up your hard disk. First you need to install strace:

$ apt-get install strace

Check in the /etc/init.d directory for the service you want to trace. Look in the script file which parameters are used to start the service. Now we are ready to start strace, for example for asterisk:

$ strace -ff -o asterisk.log asterisk -c

Used options for strace:

  • -ff => follow child processes + add pid to logfilename
  • -o  => output logging to debug.log

Another example for tracing the samba daemons is:

$ cd
$ mkdir samba_log
$ cd samba_log
$ strace -ff -o nmbd.log nmbd -D &
$ strace -ff -o smbd.log smbd -D &
$ tail -f smbd.log*

Cache write back time

Until now all tricks were trying to prevent that a process writes to the hard disk. If a process wants to write to the hard disk, it will be immediately stored. But the write action can also be delayed for a while. This is by default 5 seconds. You can check this by entering the following command:

$ cat /proc/sys/vm/dirty_writeback_centisecs
500

This value is in centiseconds. And can also be set to a higher value, for example 1 hour.

$ echo 360000 > /proc/sys/vm/dirty_writeback_centisecs

Warning: if you have a power failure in this hour, you will loose this data. To prevent this, you need to use an UPS.

Laptop mode

In newer Linux kernels (>= 2.6.6 and >= 2.4.23) there is a laptop mode available. The laptop mode groups the disk write activity into “chunks” that are spaced at larger intervals than they normally would be. This works good with the cache write back time. If the disk is active, the laptop mode will arrange that all disk activities will be done at that time. To check in which mode it is right now, enter the following command:

$ cat /proc/sys/vm/laptop_mode   
0

0 means that that the laptop mode is disabled. To enable the laptop mode, enter the following command:

$ echo 5 > /proc/sys/vm/laptop_mode

Information about the laptop mode: