hdparm-functions 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. hdparm_is_on_battery() {
  2. on_ac_power 2>/dev/null
  3. [ $? -eq 1 ]
  4. }
  5. hdparm_set_option()
  6. {
  7. local NEW_OPT= NEW_DEF=
  8. if test -n "$DISC"; then
  9. for i in $OPTIONS; do
  10. if test x${i%${i#??}} != x${1%${1#??}}; then
  11. NEW_OPT="$NEW_OPT $i"
  12. else
  13. NEW_OPT=${NEW_OPT%-q}
  14. fi
  15. done
  16. OPTIONS="$NEW_OPT $OPT_QUIET $1"
  17. else
  18. for i in $DEFAULT; do
  19. if test x${i%${i#??}} != x${1%${1#??}}; then
  20. NEW_DEF="$NEW_DEF $i"
  21. else
  22. NEW_DEF=${NEW_DEF%-q}
  23. fi
  24. done
  25. DEFAULT="$NEW_DEF $DEF_QUIET $1"
  26. fi
  27. }
  28. hdparm_eval_value()
  29. {
  30. case $1 in
  31. off|0)
  32. hdparm_set_option "$2"0
  33. ;;
  34. on|1)
  35. hdparm_set_option "$2"1
  36. ;;
  37. *)
  38. log_failure_msg "Unknown Value for $2: $1"
  39. exit 1
  40. ;;
  41. esac
  42. }
  43. # It isn't safe to set an APM policy by default on Firewire or USB devices.
  44. # See https://bugs.launchpad.net/bugs/515023.
  45. hdparm_try_apm()
  46. {
  47. # set our default global apm policy here.
  48. if [ -z "$ID_PATH" ]; then
  49. local ID_PATH="$(udevadm info -n "$1" -q property 2>/dev/null | sed -n 's/^ID_PATH=//p')" || true
  50. fi
  51. case $ID_PATH in
  52. pci-*-ieee1394-*|pci-*-usb-*)
  53. return 1
  54. ;;
  55. esac
  56. # Only activate APM on disks that support it.
  57. if [ -z "$ID_ATA_FEATURE_SET_APM" ]; then
  58. local ID_ATA_FEATURE_SET_APM="$(udevadm info -n "$1" -q property 2>/dev/null | sed -n 's/^ID_ATA_FEATURE_SET_APM=//p')" || true
  59. fi
  60. if [ "$ID_ATA_FEATURE_SET_APM" = "1" ]; then
  61. return 0
  62. fi
  63. return 1
  64. }
  65. # parse /etc/hdparm.conf and spit out a list of options for the specified
  66. # disk, taking into account global settings in /etc/hdparm.conf, current AC
  67. # power status, and (as a fallback) the default apm settings on Ubuntu.
  68. # arguments: device_name
  69. # returns 0 on success, 1 on failure.
  70. # options to be passed to hdparm are returned on stdout.
  71. hdparm_options()
  72. {
  73. local WANTED_DISK="$1"
  74. local DISC= DEFAULT= DEF_QUIET= COMMAND_LINE=
  75. # if the below is guaranteed to spawn a subshell, then this next line is
  76. # unnecessary
  77. local OPTIONS OPT_QUIET KEY SEP VALUE
  78. egrep -v '^[[:space:]]*(#|$)' /etc/hdparm.conf |
  79. {
  80. # set our default global apm policy here.
  81. if hdparm_try_apm "$WANTED_DISK"; then
  82. if hdparm_is_on_battery; then
  83. hdparm_set_option -B128
  84. # set a spindown time of 3 minutes by default so the disk isn't
  85. # constantly power cycling on a busy machine if spindown is
  86. # enabled
  87. hdparm_set_option -S36
  88. else
  89. hdparm_set_option -B254
  90. fi
  91. fi
  92. while read KEY SEP VALUE; do
  93. case $SEP in
  94. '{')
  95. case $KEY in
  96. command_line)
  97. unset DISC
  98. unset OPTIONS
  99. unset OPT_QUIET
  100. COMMAND_LINE=1
  101. ;;
  102. *)
  103. if [ -h "$KEY" ]
  104. then
  105. DISC=$(readlink -m "$KEY")
  106. DISC=${DISC%%[[:digit:]]*}
  107. else
  108. DISC=$KEY
  109. fi
  110. OPTIONS=$DEFAULT
  111. OPT_QUIET=$DEF_QUIET
  112. ;;
  113. esac
  114. ;;
  115. =)
  116. case $KEY in
  117. read_ahead_sect)
  118. hdparm_set_option -a$VALUE
  119. ;;
  120. lookahead)
  121. hdparm_eval_value $VALUE -A
  122. ;;
  123. bus)
  124. hdparm_eval_value $VALUE -b
  125. ;;
  126. apm)
  127. if ! hdparm_is_on_battery; then
  128. hdparm_set_option -B$VALUE
  129. fi
  130. ;;
  131. apm_battery)
  132. if hdparm_is_on_battery; then
  133. hdparm_set_option -B$VALUE
  134. fi
  135. ;;
  136. io32_support)
  137. hdparm_set_option -c$VALUE
  138. ;;
  139. dma)
  140. hdparm_eval_value $VALUE -d
  141. ;;
  142. defect_mana)
  143. hdparm_eval_value $VALUE -D
  144. ;;
  145. cd_speed)
  146. hdparm_set_option -E$VALUE
  147. ;;
  148. mult_sect_io)
  149. hdparm_set_option -m$VALUE
  150. ;;
  151. prefetch_sect)
  152. hdparm_set_option -P$VALUE
  153. ;;
  154. read_only)
  155. hdparm_eval_value $VALUE -r
  156. ;;
  157. write_read_verify)
  158. hdparm_eval_value $VALUE -R
  159. ;;
  160. spindown_time)
  161. hdparm_set_option -S$VALUE
  162. ;;
  163. force_spindown_time)
  164. hdparm_set_option force_spindown_time$VALUE
  165. ;;
  166. poweron_standby)
  167. hdparm_eval_value $VALUE -s
  168. ;;
  169. interrupt_unmask)
  170. hdparm_eval_value $VALUE -u
  171. ;;
  172. write_cache)
  173. hdparm_eval_value $VALUE -W
  174. ;;
  175. transfer_mode)
  176. hdparm_set_option -X$VALUE
  177. ;;
  178. acoustic_management)
  179. hdparm_set_option -M$VALUE
  180. ;;
  181. keep_settings_over_reset)
  182. hdparm_eval_value $VALUE -k
  183. ;;
  184. keep_features_over_reset)
  185. hdparm_eval_value $VALUE -K
  186. ;;
  187. chipset_pio_mode)
  188. hdparm_set_option -p$VALUE
  189. ;;
  190. security_unlock)
  191. hdparm_set_option --security-unlock $VALUE
  192. ;;
  193. security_pass)
  194. hdparm_set_option --security-set-pass $VALUE
  195. ;;
  196. security_disable)
  197. hdparm_set_option --security-disable $VALUE
  198. ;;
  199. user-master)
  200. hdparm_set_option --user-master $VALUE
  201. ;;
  202. security_mode)
  203. hdparm_set_option --security-mode $VALUE
  204. ;;
  205. ROOTFS)
  206. ;;
  207. *)
  208. echo "Unknown option $KEY"
  209. exit 1
  210. ;;
  211. esac
  212. ;;
  213. "")
  214. case $KEY in
  215. '}')
  216. if [ -z "$DISC" ] && [ -z "$COMMAND_LINE" ]; then
  217. echo "No disk enabled. Exiting" >&2
  218. return 1
  219. fi
  220. if [ -n "$OPTIONS" ] && [ "$DISC" = "$WANTED_DISK" ]
  221. then
  222. echo $OPTIONS
  223. return 0
  224. fi
  225. COMMAND_LINE=
  226. ;;
  227. quiet)
  228. if [ -n "$DISC" ]; then
  229. OPT_QUIET=-q
  230. else
  231. DEF_QUIET=-q
  232. fi
  233. ;;
  234. standby)
  235. hdparm_set_option -y
  236. ;;
  237. sleep)
  238. hdparm_set_option -Y
  239. ;;
  240. disable_seagate)
  241. hdparm_set_option -Z
  242. ;;
  243. security_freeze)
  244. hdparm_set_option --security-freeze
  245. ;;
  246. *)
  247. if [ -z "$COMMAND_LINE" ]; then
  248. echo "unknown option $KEY" >&2
  249. return 1
  250. fi
  251. ;;
  252. esac
  253. ;;
  254. *)
  255. if [ -z "$COMMAND_LINE" ]; then
  256. echo "unknown separator $SEP" >&2
  257. return 1
  258. fi
  259. ;;
  260. esac
  261. done
  262. # parsed the whole file and didn't find the disk,
  263. # so try the default instead.
  264. if [ -n "$DEFAULT" ]; then
  265. echo $DEFAULT
  266. return 0
  267. fi
  268. }
  269. }