Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
HOWTO: large spindown delays on data drives in server
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Luud
Apprentice
Apprentice


Joined: 05 Jun 2003
Posts: 246
Location: Netherlands

PostPosted: Thu Aug 13, 2009 9:01 pm    Post subject: HOWTO: large spindown delays on data drives in server Reply with quote

HOWTO: large spindown delays on data drives in server

Objective

Make idle data drives spin down during long times of inactivity.

Assumptions and problem statement

Here it is assumed that you have other harddisk for your data than the one that contains your operating system. This is other drives, not other partitions.

In my case I have /dev/sda contain all my operating system partitions, which includes a data partition for the webserver. All other data is stored on /dev/sdb, /dev/sdc, /dev/sdd, and /dev/sde. I know that these four data drives will be infrequently used. These are drives that contain data (backups) for easy access. The usage pattern shows infrequent access (might be days, weeks or even months apart), but when the drives are accessed we can expect it to be a couple of times over a day. I am happy with my system drive being active 24/7 as long as the other 4 drives can drop into power save mode.

To limit the spin up/down cycles (desktop and server disks are rated for just 40.000 of those) the drives should not spin down to quickly after being accessed. The firmware of my Samsung drives do not allow me to specify spindown times in firmware that are longer than 20 minutes (any value above 240 for hdparm -S seems not to work).

Solution

I opted for a simple solution that did not rely on laptop-mode(-utils). By creating a script that monitors the disk statistics we can spin down the drives at a time of our choosing. Just run the script regularly using a cron daemon (I picked once a minute, but less is good as well). To keep things speedy and clean I put the statistics files in a tmpfs filesystem. The script is set up to log to syslog and you can find some statistics data on spin-down times there.

Here is the script that I call from crontab:
Code:
#!/bin/bash

# List drives that need to be monitored:
DRIVES="sda sdb sdc sdd sde"

# Set some parameters
SCRIPTNAME="Spindown"
STATSMOUNT="/tmpfs"
STATSDIR="spindown"
STATSPATH="${STATSMOUNT}/${STATSDIR}"

# We use hdparm to spin down the drives
HDPARM="/sbin/hdparm"
HDPARM_OPTS="-y"

# The best option is to log to syslog, filling up our email box is not useful
LOGGER="/usr/bin/logger -p daemon.info"
# LOGGER="echo"

# Specify idle timeoutdelay in seconds.
#
# First we define some useful constants for defining time:
minute="60"
hour=$((${minute}*60))

# Specify the idle timeout. Better make this a long time, i.e. multiple hours.
# A normal desktop drive is rated for say 40.000 spin up/down cycles.
# Picking a short time might wear out your drives prematurely.
#
# Specify 20 seconds
# IDLETIMEOUT="20"
# Specify 1 minute:
# IDLETIMEOUT=${minute}
# Specify 15 minutes:
# IDLETIMEOUT=$((${minute}*15))
# Specify 1 hour:
# IDLETIMEOUT=${hour}
# Specify 8 hours:
IDLETIMEOUT=$((${hour}*8))

# Get the current date/time
now=`date "+%H:%M:%S %d-%m-%Y"`

tmpfsmount=`mount | grep "${STATSMOUNT}"`
if [ -z "${tmpfsmount}" ]; then
   if [ ! -d ${STATSMOUNT} ]; then
      ${LOGGER} "${SCRIPTNAME}: creating /tmpfs mount point..."
      mkdir -p "${STATSMOUNT}"
   fi
   ${LOGGER} "${SCRIPTNAME}: mounting tmpfs filesystem under ${STATSMOUNT}..."
   mount -t tmpfs none "${STATSMOUNT}"
   tmpfsmount=`mount | grep "${STATSMOUNT}"`
   if [ -z "${tmpfsmount}" ]; then
      ${LOGGER} "${SCRIPTNAME}: failed to mount ${STATSMOUNT}, aborting."
      exit 1
   fi
fi

# Try to create stats storage directory if it dos not exist
[ ! -d "${STATSPATH}" ] && mkdir -p "${STATSPATH}"
# Check if stats directory exists
if [ ! -d ${STATSPATH} ]; then
   ${LOGGER} "$SCRIPTNAME: stats storage directory ${STATSPATH} does not exist, aborting."
   exit 1
fi

# Monitor all drives one by one
for drive in ${DRIVES}
do
   # We use the seconds since 1970
   # (Will this break the script due to the Y2K38 problem?
   # Likely we are safe on 64bit systems... )
   timestamp=`date +%s`
   diskstat=`cat /sys/block/$drive/stat`

   if [ -f "${STATSPATH}/stat-$drive" ]
   then
      oldstat=`head -n 1 "${STATSPATH}/stat-${drive}" | cut -d":" -f 2`
      oldstamp=`head -n 1 "${STATSPATH}/stat-${drive}" | cut -d":" -f 1`
      #
      # Detect if drive is inactive:
      if [ "${diskstat}" == "${oldstat}" ]; then
         seconds=$(( ${timestamp} - ${oldstamp} ))
         hours=`expr ${seconds} / ${hour}`
         remainder=`expr ${seconds} % ${hour}`
         minutes=`expr ${remainder} / ${minute}`
         remainder=`expr ${remainder} % ${minute}`
         #
         # Only spin down drive if we have not done that yet
         if [ "${seconds}" -gt "${IDLETIMEOUT}" ] && [ ! -f "${STATSPATH}/isdown-${drive}" ]; then
            ${LOGGER} "${SCRIPTNAME}: Spinning down drive ${drive} (detected inactivity for ${hours}:${minutes}:${remainder})..."
            echo "${timestamp}: Drive spun down at ${now}" > "${STATSPATH}/isdown-${drive}"
            ${HDPARM} ${HDPARM_OPTS} "/dev/${drive}" >> "${STATSPATH}/isdown-${drive}" 2>&1
         fi
      #
      # Drive activity detected:
      else
         if [ -f "${STATSPATH}/isdown-${drive}" ]; then
            oldstamp=`head -n 1 "${STATSPATH}/isdown-${drive}" | cut -d":" -f 1`
            seconds=$(( ${timestamp} - ${oldstamp} ))
            hours=`expr ${seconds} / ${hour}`
            remainder=`expr ${seconds} % ${hour}`
            minutes=`expr ${remainder} / ${minute}`
            remainder=`expr ${remainder} % ${minute}`
            ${LOGGER} "${SCRIPTNAME}: Detected spin up for drive ${drive} at ${now} (drive has been in standby for ${hours}:${minutes}:${remainder})."
            \rm -f "${STATSPATH}/isdown-${drive}"
         fi
         echo "${timestamp}:${diskstat}" > "${STATSPATH}/stat-$drive"
      fi
   #
   # First time the script is run for monitoring this drive:
   else
      ${LOGGER} "${SCRIPTNAME}: No monitoring data for drive ${drive}, starting..."
      [ -f "${STATSPATH}/isdown-${drive}" ] && \rm -f "${STATSPATH}/isdown-${drive}"
      echo "${timestamp}:${diskstat}" > "${STATSPATH}/stat-$drive"
   fi
done
This script could use some more streamlining, but it works for me. Maybe it is useful for someone else.
_________________
"Great minds don't think alike. If they did, the patent office would only have about fifty inventions." - Wally
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum