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
19 ZED_PIDFILE
=${ZED_PIDFILE:-/var/run/zed.pid}
20 LDMOD
=${LDMOD:-/sbin/modprobe}
22 KMOD_ZLIB_DEFLATE
=${KMOD_ZLIB_DEFLATE:-zlib_deflate}
23 KMOD_ZLIB_INFLATE
=${KMOD_ZLIB_INFLATE:-zlib_inflate}
24 KMOD_SPL
=${KMOD_SPL:-spl}
25 KMOD_ZAVL
=${KMOD_ZAVL:-zavl}
26 KMOD_ZNVPAIR
=${KMOD_ZNVPAIR:-znvpair}
27 KMOD_ZUNICODE
=${KMOD_ZUNICODE:-zunicode}
28 KMOD_ZCOMMON
=${KMOD_ZCOMMON:-zcommon}
29 KMOD_ZLUA
=${KMOD_ZLUA:-zlua}
30 KMOD_ICP
=${KMOD_ICP:-icp}
31 KMOD_ZFS
=${KMOD_ZFS:-zfs}
32 KMOD_FREEBSD
=${KMOD_FREEBSD:-openzfs}
33 KMOD_ZZSTD
=${KMOD_ZZSTD:-zzstd}
39 $0 [hvudS] [module-options]
42 Load/unload the ZFS module stack.
48 -S Enable kernel stack tracer
52 while getopts 'hvuS' OPTION
; do
75 if [ -f "$ZED_PIDFILE" ]; then
76 PID
=$
(cat "$ZED_PIDFILE")
81 check_modules_linux
() {
85 for KMOD
in $KMOD_SPL $KMOD_ZAVL $KMOD_ZNVPAIR $KMOD_ZUNICODE $KMOD_ZCOMMON \
86 $KMOD_ZLUA $KMOD_ZZSTD $KMOD_ICP $KMOD_ZFS; do
87 NAME
=$
(basename "$KMOD" .ko
)
89 if lsmod |
grep -E -q "^${NAME}"; then
90 LOADED_MODULES
="$LOADED_MODULES\t$NAME\n"
93 if ! modinfo
"$KMOD" >/dev
/null
2>&1; then
94 MISSING_MODULES
="$MISSING_MODULES\t${KMOD}\n"
98 if [ -n "$LOADED_MODULES" ]; then
99 printf "Unload the kernel modules by running '%s -u':\n" "$PROG"
100 printf "%b" "$LOADED_MODULES"
104 if [ -n "$MISSING_MODULES" ]; then
105 printf "The following kernel modules can not be found:\n"
106 printf "%b" "$MISSING_MODULES"
113 load_module_linux
() {
116 FILE
=$
(modinfo
"$KMOD" |
awk '/^filename:/ {print $2}')
117 VERSION
=$
(modinfo
"$KMOD" |
awk '/^version:/ {print $2}')
119 if [ "$VERBOSE" = "yes" ]; then
120 echo "Loading: $FILE ($VERSION)"
123 $LDMOD "$KMOD" >/dev
/null
2>&1
124 # shellcheck disable=SC2181
125 if [ $?
-ne 0 ]; then
126 echo "Failed to load $KMOD"
133 load_modules_freebsd
() {
134 kldload
"$KMOD_FREEBSD" ||
return 1
136 if [ "$VERBOSE" = "yes" ]; then
137 echo "Successfully loaded ZFS module stack"
143 load_modules_linux
() {
146 if modinfo
"$KMOD_ZLIB_DEFLATE" >/dev
/null
2>&1; then
147 modprobe
"$KMOD_ZLIB_DEFLATE" >/dev
/null
2>&1
150 if modinfo
"$KMOD_ZLIB_INFLATE">/dev
/null
2>&1; then
151 modprobe
"$KMOD_ZLIB_INFLATE" >/dev
/null
2>&1
154 for KMOD
in $KMOD_SPL $KMOD_ZAVL $KMOD_ZNVPAIR \
155 $KMOD_ZUNICODE $KMOD_ZCOMMON $KMOD_ZLUA $KMOD_ZZSTD \
156 $KMOD_ICP $KMOD_ZFS; do
157 load_module_linux
"$KMOD" ||
return 1
160 if [ "$VERBOSE" = "yes" ]; then
161 echo "Successfully loaded ZFS module stack"
167 unload_module_linux
() {
170 NAME
=$
(basename "$KMOD" .ko
)
171 FILE
=$
(modinfo
"$KMOD" |
awk '/^filename:/ {print $2}')
172 VERSION
=$
(modinfo
"$KMOD" |
awk '/^version:/ {print $2}')
174 if [ "$VERBOSE" = "yes" ]; then
175 echo "Unloading: $KMOD ($VERSION)"
178 rmmod
"$NAME" ||
echo "Failed to unload $NAME"
183 unload_modules_freebsd
() {
184 kldunload
"$KMOD_FREEBSD" ||
echo "Failed to unload $KMOD_FREEBSD"
186 if [ "$VERBOSE" = "yes" ]; then
187 echo "Successfully unloaded ZFS module stack"
193 unload_modules_linux
() {
194 for KMOD
in $KMOD_ZFS $KMOD_ICP $KMOD_ZZSTD $KMOD_ZLUA $KMOD_ZCOMMON \
195 $KMOD_ZUNICODE $KMOD_ZNVPAIR $KMOD_ZAVL $KMOD_SPL; do
196 NAME
=$
(basename "$KMOD" .ko
)
197 USE_COUNT
=$
(lsmod |
grep -E "^${NAME} " |
awk '{print $3}')
199 if [ "$USE_COUNT" = "0" ] ; then
200 unload_module_linux
"$KMOD" ||
return 1
202 echo "Module ${NAME} is still in use!"
207 if modinfo
"$KMOD_ZLIB_DEFLATE" >/dev
/null
2>&1; then
208 modprobe
-r "$KMOD_ZLIB_DEFLATE" >/dev
/null
2>&1
211 if modinfo
"$KMOD_ZLIB_INFLATE">/dev
/null
2>&1; then
212 modprobe
-r "$KMOD_ZLIB_INFLATE" >/dev
/null
2>&1
215 if [ "$VERBOSE" = "yes" ]; then
216 echo "Successfully unloaded ZFS module stack"
222 stack_clear_linux
() {
223 STACK_MAX_SIZE
=/sys
/kernel
/debug
/tracing
/stack_max_size
224 STACK_TRACER_ENABLED
=/proc
/sys
/kernel
/stack_tracer_enabled
226 if [ "$STACK_TRACER" = "yes" ] && [ -e "$STACK_MAX_SIZE" ]; then
227 echo 1 >"$STACK_TRACER_ENABLED"
228 echo 0 >"$STACK_MAX_SIZE"
232 stack_check_linux
() {
233 STACK_MAX_SIZE
=/sys
/kernel
/debug
/tracing
/stack_max_size
234 STACK_TRACE
=/sys
/kernel
/debug
/tracing
/stack_trace
237 if [ -e "$STACK_MAX_SIZE" ]; then
238 STACK_SIZE
=$
(cat "$STACK_MAX_SIZE")
240 if [ "$STACK_SIZE" -ge "$STACK_LIMIT" ]; then
242 echo "Warning: max stack size $STACK_SIZE bytes"
248 if [ "$(id -u)" != 0 ]; then
249 echo "Must run as root"
255 if [ "$UNLOAD" = "yes" ]; then
260 unload_modules_freebsd
275 load_modules_linux
"$@"