ZTS: Fix zpool_reopen_[1-5] on Fedora 35
[zfs.git] / scripts / zfs.sh
blob940c83ffa28fc2d3fdb6e90e98a7c9040a116537
1 #!/bin/sh
3 # A simple script to load/unload the ZFS module stack.
6 BASE_DIR=$(dirname "$0")
7 SCRIPT_COMMON=common.sh
8 if [ -f "${BASE_DIR}/${SCRIPT_COMMON}" ]; then
9 . "${BASE_DIR}/${SCRIPT_COMMON}"
10 else
11 echo "Missing helper script ${SCRIPT_COMMON}" && exit 1
14 PROG=zfs.sh
15 VERBOSE="no"
16 UNLOAD="no"
17 LOAD="yes"
18 STACK_TRACER="no"
20 ZED_PIDFILE=${ZED_PIDFILE:-/var/run/zed.pid}
21 LDMOD=${LDMOD:-/sbin/modprobe}
23 KMOD_ZLIB_DEFLATE=${KMOD_ZLIB_DEFLATE:-zlib_deflate}
24 KMOD_ZLIB_INFLATE=${KMOD_ZLIB_INFLATE:-zlib_inflate}
25 KMOD_SPL=${KMOD_SPL:-spl}
26 KMOD_ZAVL=${KMOD_ZAVL:-zavl}
27 KMOD_ZNVPAIR=${KMOD_ZNVPAIR:-znvpair}
28 KMOD_ZUNICODE=${KMOD_ZUNICODE:-zunicode}
29 KMOD_ZCOMMON=${KMOD_ZCOMMON:-zcommon}
30 KMOD_ZLUA=${KMOD_ZLUA:-zlua}
31 KMOD_ICP=${KMOD_ICP:-icp}
32 KMOD_ZFS=${KMOD_ZFS:-zfs}
33 KMOD_FREEBSD=${KMOD_FREEBSD:-openzfs}
34 KMOD_ZZSTD=${KMOD_ZZSTD:-zzstd}
37 usage() {
38 cat << EOF
39 USAGE:
40 $0 [hvudS] [module-options]
42 DESCRIPTION:
43 Load/unload the ZFS module stack.
45 OPTIONS:
46 -h Show this message
47 -v Verbose
48 -r Reload modules
49 -u Unload modules
50 -S Enable kernel stack tracer
51 EOF
54 while getopts 'hvruS' OPTION; do
55 case $OPTION in
57 usage
58 exit 1
61 VERBOSE="yes"
64 UNLOAD="yes"
65 LOAD="yes"
68 UNLOAD="yes"
69 LOAD="no"
72 STACK_TRACER="yes"
75 usage
76 exit
78 esac
79 done
81 kill_zed() {
82 if [ -f "$ZED_PIDFILE" ]; then
83 PID=$(cat "$ZED_PIDFILE")
84 kill "$PID"
88 check_modules_linux() {
89 LOADED_MODULES=""
90 MISSING_MODULES=""
92 for KMOD in $KMOD_SPL $KMOD_ZAVL $KMOD_ZNVPAIR $KMOD_ZUNICODE $KMOD_ZCOMMON \
93 $KMOD_ZLUA $KMOD_ZZSTD $KMOD_ICP $KMOD_ZFS; do
94 NAME="${KMOD##*/}"
95 NAME="${NAME%.ko}"
97 if lsmod | grep -E -q "^${NAME}"; then
98 LOADED_MODULES="$LOADED_MODULES\t$NAME\n"
101 if ! modinfo "$KMOD" >/dev/null 2>&1; then
102 MISSING_MODULES="$MISSING_MODULES\t${KMOD}\n"
104 done
106 if [ -n "$LOADED_MODULES" ]; then
107 printf "Unload the kernel modules by running '%s -u':\n" "$PROG"
108 printf "%b" "$LOADED_MODULES"
109 exit 1
112 if [ -n "$MISSING_MODULES" ]; then
113 printf "The following kernel modules can not be found:\n"
114 printf "%b" "$MISSING_MODULES"
115 exit 1
118 return 0
121 load_module_linux() {
122 KMOD=$1
124 FILE=$(modinfo "$KMOD" | awk '/^filename:/ {print $2}')
125 VERSION=$(modinfo "$KMOD" | awk '/^version:/ {print $2}')
127 if [ "$VERBOSE" = "yes" ]; then
128 echo "Loading: $FILE ($VERSION)"
131 if ! $LDMOD "$KMOD" >/dev/null 2>&1; then
132 echo "Failed to load $KMOD"
133 return 1
136 return 0
139 load_modules_freebsd() {
140 kldload "$KMOD_FREEBSD" || return 1
142 if [ "$VERBOSE" = "yes" ]; then
143 echo "Successfully loaded ZFS module stack"
146 return 0
149 load_modules_linux() {
150 mkdir -p /etc/zfs
152 if modinfo "$KMOD_ZLIB_DEFLATE" >/dev/null 2>&1; then
153 modprobe "$KMOD_ZLIB_DEFLATE" >/dev/null 2>&1
156 if modinfo "$KMOD_ZLIB_INFLATE">/dev/null 2>&1; then
157 modprobe "$KMOD_ZLIB_INFLATE" >/dev/null 2>&1
160 for KMOD in $KMOD_SPL $KMOD_ZAVL $KMOD_ZNVPAIR \
161 $KMOD_ZUNICODE $KMOD_ZCOMMON $KMOD_ZLUA $KMOD_ZZSTD \
162 $KMOD_ICP $KMOD_ZFS; do
163 load_module_linux "$KMOD" || return 1
164 done
166 if [ "$VERBOSE" = "yes" ]; then
167 echo "Successfully loaded ZFS module stack"
170 return 0
173 unload_module_linux() {
174 KMOD=$1
176 NAME="${KMOD##*/}"
177 NAME="${NAME%.ko}"
178 FILE=$(modinfo "$KMOD" | awk '/^filename:/ {print $2}')
179 VERSION=$(modinfo "$KMOD" | awk '/^version:/ {print $2}')
181 if [ "$VERBOSE" = "yes" ]; then
182 echo "Unloading: $KMOD ($VERSION)"
185 rmmod "$NAME" || echo "Failed to unload $NAME"
187 return 0
190 unload_modules_freebsd() {
191 kldunload "$KMOD_FREEBSD" || echo "Failed to unload $KMOD_FREEBSD"
193 if [ "$VERBOSE" = "yes" ]; then
194 echo "Successfully unloaded ZFS module stack"
197 return 0
200 unload_modules_linux() {
201 for KMOD in $KMOD_ZFS $KMOD_ICP $KMOD_ZZSTD $KMOD_ZLUA $KMOD_ZCOMMON \
202 $KMOD_ZUNICODE $KMOD_ZNVPAIR $KMOD_ZAVL $KMOD_SPL; do
203 NAME="${KMOD##*/}"
204 NAME="${NAME%.ko}"
205 USE_COUNT=$(lsmod | awk '/^'"${NAME}"'/ {print $3}')
207 if [ "$USE_COUNT" = "0" ] ; then
208 unload_module_linux "$KMOD" || return 1
209 elif [ "$USE_COUNT" != "" ] ; then
210 echo "Module ${NAME} is still in use!"
211 return 1
213 done
215 if modinfo "$KMOD_ZLIB_DEFLATE" >/dev/null 2>&1; then
216 modprobe -r "$KMOD_ZLIB_DEFLATE" >/dev/null 2>&1
219 if modinfo "$KMOD_ZLIB_INFLATE">/dev/null 2>&1; then
220 modprobe -r "$KMOD_ZLIB_INFLATE" >/dev/null 2>&1
223 if [ "$VERBOSE" = "yes" ]; then
224 echo "Successfully unloaded ZFS module stack"
227 return 0
230 stack_clear_linux() {
231 STACK_MAX_SIZE=/sys/kernel/debug/tracing/stack_max_size
232 STACK_TRACER_ENABLED=/proc/sys/kernel/stack_tracer_enabled
234 if [ "$STACK_TRACER" = "yes" ] && [ -e "$STACK_MAX_SIZE" ]; then
235 echo 1 >"$STACK_TRACER_ENABLED"
236 echo 0 >"$STACK_MAX_SIZE"
240 stack_check_linux() {
241 STACK_MAX_SIZE=/sys/kernel/debug/tracing/stack_max_size
242 STACK_TRACE=/sys/kernel/debug/tracing/stack_trace
243 STACK_LIMIT=15362
245 if [ -e "$STACK_MAX_SIZE" ]; then
246 STACK_SIZE=$(cat "$STACK_MAX_SIZE")
248 if [ "$STACK_SIZE" -ge "$STACK_LIMIT" ]; then
249 echo
250 echo "Warning: max stack size $STACK_SIZE bytes"
251 cat "$STACK_TRACE"
256 if [ "$(id -u)" != 0 ]; then
257 echo "Must run as root"
258 exit 1
261 UNAME=$(uname -s)
263 if [ "$UNLOAD" = "yes" ]; then
264 kill_zed
265 umount -t zfs -a
266 case $UNAME in
267 FreeBSD)
268 unload_modules_freebsd
270 Linux)
271 stack_check_linux
272 unload_modules_linux
274 esac
276 if [ "$LOAD" = "yes" ]; then
277 case $UNAME in
278 FreeBSD)
279 load_modules_freebsd
281 Linux)
282 stack_clear_linux
283 check_modules_linux
284 load_modules_linux "$@"
285 udevadm trigger
286 udevadm settle
288 esac
291 exit 0