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}"
11 echo "Missing helper script ${SCRIPT_COMMON}" && exit 1
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}
40 $0 [hvudS] [module-options]
43 Load/unload the ZFS module stack.
50 -S Enable kernel stack tracer
54 while getopts 'hvruS' OPTION
; do
82 if [ -f "$ZED_PIDFILE" ]; then
83 PID
=$
(cat "$ZED_PIDFILE")
88 check_modules_linux
() {
92 for KMOD
in $KMOD_SPL $KMOD_ZAVL $KMOD_ZNVPAIR $KMOD_ZUNICODE $KMOD_ZCOMMON \
93 $KMOD_ZLUA $KMOD_ZZSTD $KMOD_ICP $KMOD_ZFS; do
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"
106 if [ -n "$LOADED_MODULES" ]; then
107 printf "Unload the kernel modules by running '%s -u':\n" "$PROG"
108 printf "%b" "$LOADED_MODULES"
112 if [ -n "$MISSING_MODULES" ]; then
113 printf "The following kernel modules can not be found:\n"
114 printf "%b" "$MISSING_MODULES"
121 load_module_linux
() {
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"
139 load_modules_freebsd
() {
140 kldload
"$KMOD_FREEBSD" ||
return 1
142 if [ "$VERBOSE" = "yes" ]; then
143 echo "Successfully loaded ZFS module stack"
149 load_modules_linux
() {
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
166 if [ "$VERBOSE" = "yes" ]; then
167 echo "Successfully loaded ZFS module stack"
173 unload_module_linux
() {
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"
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"
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
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!"
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"
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
245 if [ -e "$STACK_MAX_SIZE" ]; then
246 STACK_SIZE
=$
(cat "$STACK_MAX_SIZE")
248 if [ "$STACK_SIZE" -ge "$STACK_LIMIT" ]; then
250 echo "Warning: max stack size $STACK_SIZE bytes"
256 if [ "$(id -u)" != 0 ]; then
257 echo "Must run as root"
263 if [ "$UNLOAD" = "yes" ]; then
268 unload_modules_freebsd
276 if [ "$LOAD" = "yes" ]; then
284 load_modules_linux
"$@"