--- 90-hdparm.sh.orig 2009-02-08 14:00:22.000000000 +0100 +++ 90-hdparm.sh 2009-02-08 20:29:12.000000000 +0100 @@ -9,33 +9,70 @@ . /usr/share/acpi-support/power-funcs -DO_HDPARM=y -if [ -e /usr/sbin/laptop_mode ] ; then - LMT_CONTROL_HD_POWERMGMT=$(. /etc/laptop-mode/laptop-mode.conf && echo "$CONTROL_HD_POWERMGMT") - if [ "$LMT_CONTROL_HD_POWERMGMT" != 0 ] \ - && [ -e /var/run/laptop-mode-tools/enabled ] - then - # Laptop mode controls hdparm -B settings, we don't. - DO_HDPARM=n +# Check whether Laptop Mode Tools controls the HD power management settings. +# returns +# true if LMT controls APM value of the specified harddisk ($1) +# false else +controlled_by_LMT () { + if [ -n "$1" ] && [ -e $LAPTOP_MODE ]; then + LMT_HD=$(. /etc/laptop-mode/laptop-mode.conf && echo "$HD") + LMT_CONTROL_HD_POWERMGMT=$(. /etc/laptop-mode/laptop-mode.conf && echo "$CONTROL_HD_POWERMGMT") + + if [ -n "$LMT_HD" ] \ + && [ "$LMT_CONTROL_HD_POWERMGMT" != 0 ] \ + && [ -e /var/run/laptop-mode-tools/enabled ]; then + for tmpDev in $LMT_HD; do + if [ "$(readlink -m $tmpDev | cut -c-8 )" = "$1" ]; then + # Laptop mode controls hdparm -B settings, we don't. + return 0 + fi + done + fi + fi + + return 1 +} + +# Check whether Hdparm is used to control the HD power management settings. +# returns +# true if the specified harddisk ($1) is controlled by /etc/hdparm.conf +# false else +controlled_by_HDPARM () { + if [ -n "$1" ] && [ -e /etc/hdparm.conf ]; then + egrep '^[[:space:]]*/dev[-_/[:alnum:]]*[-_[:alnum:]]+[[:space:]]+{' /etc/hdparm.conf | + { + while read tmpDev tmp; do + if [ "$(readlink -m $tmpDev | cut -c-8 )" = "$1" ]; then + return 0 + fi + done + return 1 + } + + return $? fi -fi -if [ "$DO_HDPARM" = y ] ; then - # Get the power state into STATE - getState; - - for dev in /dev/sd? /dev/hd? ; do - if [ -b $dev ] ; then - # Check for APM support; discard errors since not all drives - # support HDIO_GET_IDENTITY (-i). - if hdparm -i $dev 2> /dev/null | grep -q 'AdvancedPM=yes' ; then - if [ "$STATE" = "BATTERY" ] ; then - hdparm -B 128 $dev - else - hdparm -B 254 $dev - fi + return 1 +} + +# Get the power state into STATE +getState; + +# Update APM settings of drives that are not controlled elsewhere. +for dev in /dev/sd? /dev/hd? ; do + if [ -b $dev ] \ + && ! controlled_by_HDPARM $dev \ + && ! controlled_by_LMT $dev; then + + # Check for APM support; discard errors since not all drives + # support HDIO_GET_IDENTITY (-i). + if $HDPARM -i $dev 2> /dev/null | grep -q 'AdvancedPM=yes' ; then + if [ "$STATE" = "BATTERY" ] ; then + $HDPARM -B 128 $dev + else + $HDPARM -B 254 $dev fi fi - done -fi + fi +done