95hdparm-apm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #! /bin/sh
  2. #
  3. # This script adjusts hard drive APM settings using hdparm. The hardware
  4. # defaults (usually hdparm -B 127) cause excessive head load/unload cycles
  5. # on many modern hard drives. We therefore set hdparm -B 254 while on AC
  6. # power. On battery we set hdparm -B 128; this still does not guarantee
  7. # disk parking, but is safer than causing lots of mechanical wear on disks
  8. # as we seem to get currently with 127.
  9. #
  10. # Refactored from acpi-support's 90-hdparm.sh for pm-utils
  11. if grep -wq "nohdparm" /proc/cmdline ; then
  12. exit 0
  13. fi
  14. # Do nothing when called via /etc/init.d/acpi-support; udev rules take care
  15. # of setting the initial hdparm policy for us.
  16. if ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ]; then
  17. exit 0
  18. fi
  19. if [ -e /usr/sbin/laptop_mode ] ; then
  20. LMT_CONTROL_HD_POWERMGMT=$(. /etc/laptop-mode/laptop-mode.conf && echo "$CONTROL_HD_POWERMGMT")
  21. if [ "$LMT_CONTROL_HD_POWERMGMT" != 0 ] \
  22. && [ -e /var/run/laptop-mode-tools/enabled ]
  23. then
  24. # Laptop mode controls hdparm -B settings, we don't.
  25. exit 0
  26. fi
  27. fi
  28. . /lib/hdparm/hdparm-functions
  29. resume_hdparm_apm()
  30. {
  31. for dev in /dev/sd? /dev/hd? ; do
  32. apm_opt=
  33. if [ -b $dev ] && hdparm_try_apm $dev ; then
  34. for option in $(hdparm_options $dev); do
  35. case $option in
  36. -B*)
  37. apm_opt=$option
  38. ;;
  39. *)
  40. ;;
  41. esac
  42. done
  43. if [ -n "$apm_opt" ]; then
  44. hdparm $apm_opt $dev
  45. fi
  46. fi
  47. done
  48. }
  49. resume_hdparm_spindown()
  50. {
  51. for dev in /dev/sd? /dev/hd? ; do
  52. # Check for force_spindown_time option
  53. # If defined apply hdaprm -S even if APM is not supported
  54. # See also #758988
  55. ignore_apm=
  56. options=$(hdparm_options $dev)
  57. case $options in
  58. (*'force_spindown_time'*)
  59. ignore_apm='true'
  60. ;;
  61. esac
  62. apm_opt=
  63. if [ -b $dev ]; then
  64. if hdparm_try_apm $dev || [ "$ignore_apm" = true ] ; then
  65. for option in $(hdparm_options $dev); do
  66. # Convert manually introduced option "force_spindown_time"
  67. # back to "-S" understandable by hdparm
  68. option=$(echo $option| sed 's/force_spindown_time/-S/')
  69. case $option in
  70. -S*)
  71. apm_opt=$option
  72. ;;
  73. *)
  74. ;;
  75. esac
  76. done
  77. if [ -n "$apm_opt" ]; then
  78. hdparm $apm_opt $dev
  79. fi
  80. fi
  81. fi
  82. done
  83. }
  84. case "$1" in
  85. true|false) # powersaving on/off
  86. resume_hdparm_apm
  87. ;;
  88. thaw|resume)
  89. resume_hdparm_apm
  90. # only set the -S option on resuming, not necessary for power.d
  91. resume_hdparm_spindown
  92. ;;
  93. *)
  94. exit 254
  95. ;;
  96. esac