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