failcmd.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/bin/bash
  2. #
  3. # NAME
  4. # failcmd.sh - run a command with injecting slab/page allocation failures
  5. #
  6. # SYNOPSIS
  7. # failcmd.sh --help
  8. # failcmd.sh [<options>] command [arguments]
  9. #
  10. # DESCRIPTION
  11. # Run command with injecting slab/page allocation failures by fault
  12. # injection.
  13. #
  14. # NOTE: you need to run this script as root.
  15. #
  16. usage()
  17. {
  18. cat >&2 <<EOF
  19. Usage: $0 [options] command [arguments]
  20. OPTIONS
  21. -p percent
  22. --probability=percent
  23. likelihood of failure injection, in percent.
  24. Default value is 1
  25. -t value
  26. --times=value
  27. specifies how many times failures may happen at most.
  28. Default value is 1
  29. --oom-kill-allocating-task=value
  30. set /proc/sys/vm/oom_kill_allocating_task to specified value
  31. before running the command.
  32. Default value is 1
  33. -h, --help
  34. Display a usage message and exit
  35. --interval=value, --space=value, --verbose=value, --task-filter=value,
  36. --stacktrace-depth=value, --require-start=value, --require-end=value,
  37. --reject-start=value, --reject-end=value, --ignore-gfp-wait=value
  38. See Documentation/fault-injection/fault-injection.txt for more
  39. information
  40. failslab options:
  41. --cache-filter=value
  42. fail_page_alloc options:
  43. --ignore-gfp-highmem=value, --min-order=value
  44. ENVIRONMENT
  45. FAILCMD_TYPE
  46. The following values for FAILCMD_TYPE are recognized:
  47. failslab
  48. inject slab allocation failures
  49. fail_page_alloc
  50. inject page allocation failures
  51. If FAILCMD_TYPE is not defined, then failslab is used.
  52. EOF
  53. }
  54. if [ $UID != 0 ]; then
  55. echo must be run as root >&2
  56. exit 1
  57. fi
  58. DEBUGFS=`mount -t debugfs | head -1 | awk '{ print $3}'`
  59. if [ ! -d "$DEBUGFS" ]; then
  60. echo debugfs is not mounted >&2
  61. exit 1
  62. fi
  63. FAILCMD_TYPE=${FAILCMD_TYPE:-failslab}
  64. FAULTATTR=$DEBUGFS/$FAILCMD_TYPE
  65. if [ ! -d $FAULTATTR ]; then
  66. echo $FAILCMD_TYPE is not available >&2
  67. exit 1
  68. fi
  69. LONGOPTS=probability:,interval:,times:,space:,verbose:,task-filter:
  70. LONGOPTS=$LONGOPTS,stacktrace-depth:,require-start:,require-end:
  71. LONGOPTS=$LONGOPTS,reject-start:,reject-end:,oom-kill-allocating-task:,help
  72. if [ $FAILCMD_TYPE = failslab ]; then
  73. LONGOPTS=$LONGOPTS,ignore-gfp-wait:,cache-filter:
  74. elif [ $FAILCMD_TYPE = fail_page_alloc ]; then
  75. LONGOPTS=$LONGOPTS,ignore-gfp-wait:,ignore-gfp-highmem:,min-order:
  76. fi
  77. TEMP=`getopt -o p:i:t:s:v:h --long $LONGOPTS -n 'failcmd.sh' -- "$@"`
  78. if [ $? != 0 ]; then
  79. usage
  80. exit 1
  81. fi
  82. eval set -- "$TEMP"
  83. fault_attr_default()
  84. {
  85. echo N > $FAULTATTR/task-filter
  86. echo 0 > $FAULTATTR/probability
  87. echo 1 > $FAULTATTR/times
  88. }
  89. fault_attr_default
  90. oom_kill_allocating_task_saved=`cat /proc/sys/vm/oom_kill_allocating_task`
  91. restore_values()
  92. {
  93. fault_attr_default
  94. echo $oom_kill_allocating_task_saved \
  95. > /proc/sys/vm/oom_kill_allocating_task
  96. }
  97. #
  98. # Default options
  99. #
  100. declare -i oom_kill_allocating_task=1
  101. declare task_filter=Y
  102. declare -i probability=1
  103. declare -i times=1
  104. while true; do
  105. case "$1" in
  106. -p|--probability)
  107. probability=$2
  108. shift 2
  109. ;;
  110. -i|--interval)
  111. echo $2 > $FAULTATTR/interval
  112. shift 2
  113. ;;
  114. -t|--times)
  115. times=$2
  116. shift 2
  117. ;;
  118. -s|--space)
  119. echo $2 > $FAULTATTR/space
  120. shift 2
  121. ;;
  122. -v|--verbose)
  123. echo $2 > $FAULTATTR/verbose
  124. shift 2
  125. ;;
  126. --task-filter)
  127. task_filter=$2
  128. shift 2
  129. ;;
  130. --stacktrace-depth)
  131. echo $2 > $FAULTATTR/stacktrace-depth
  132. shift 2
  133. ;;
  134. --require-start)
  135. echo $2 > $FAULTATTR/require-start
  136. shift 2
  137. ;;
  138. --require-end)
  139. echo $2 > $FAULTATTR/require-end
  140. shift 2
  141. ;;
  142. --reject-start)
  143. echo $2 > $FAULTATTR/reject-start
  144. shift 2
  145. ;;
  146. --reject-end)
  147. echo $2 > $FAULTATTR/reject-end
  148. shift 2
  149. ;;
  150. --oom-kill-allocating-task)
  151. oom_kill_allocating_task=$2
  152. shift 2
  153. ;;
  154. --ignore-gfp-wait)
  155. echo $2 > $FAULTATTR/ignore-gfp-wait
  156. shift 2
  157. ;;
  158. --cache-filter)
  159. echo $2 > $FAULTATTR/cache_filter
  160. shift 2
  161. ;;
  162. --ignore-gfp-highmem)
  163. echo $2 > $FAULTATTR/ignore-gfp-highmem
  164. shift 2
  165. ;;
  166. --min-order)
  167. echo $2 > $FAULTATTR/min-order
  168. shift 2
  169. ;;
  170. -h|--help)
  171. usage
  172. exit 0
  173. shift
  174. ;;
  175. --)
  176. shift
  177. break
  178. ;;
  179. esac
  180. done
  181. [ -z "$1" ] && exit 0
  182. echo $oom_kill_allocating_task > /proc/sys/vm/oom_kill_allocating_task
  183. echo $task_filter > $FAULTATTR/task-filter
  184. echo $probability > $FAULTATTR/probability
  185. echo $times > $FAULTATTR/times
  186. trap "restore_values" SIGINT SIGTERM EXIT
  187. cmd="echo 1 > /proc/self/make-it-fail && exec $@"
  188. bash -c "$cmd"