kernel_fpu fixes
[zfs.git] / scripts / zfs.sh
blob015b3ba9de6648e731a6f4f998e9a77ad8912865
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 STACK_TRACER="no"
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}
34 usage() {
35 cat << EOF
36 USAGE:
37 $0 [hvudS] [module-options]
39 DESCRIPTION:
40 Load/unload the ZFS module stack.
42 OPTIONS:
43 -h Show this message
44 -v Verbose
45 -u Unload modules
46 -S Enable kernel stack tracer
47 EOF
50 while getopts 'hvuS' OPTION; do
51 case $OPTION in
53 usage
54 exit 1
57 VERBOSE="yes"
60 UNLOAD="yes"
63 STACK_TRACER="yes"
66 usage
67 exit
69 esac
70 done
72 kill_zed() {
73 if [ -f "$ZED_PIDFILE" ]; then
74 PID=$(cat "$ZED_PIDFILE")
75 kill "$PID"
79 check_modules() {
80 LOADED_MODULES=""
81 MISSING_MODULES=""
83 for KMOD in $KMOD_SPL $KMOD_ZAVL $KMOD_ZNVPAIR \
84 $KMOD_ZUNICODE $KMOD_ZCOMMON $KMOD_ZLUA $KMOD_ICP $KMOD_ZFS; do
85 NAME=$(basename "$KMOD" .ko)
87 if lsmod | grep -E -q "^${NAME}"; then
88 LOADED_MODULES="$LOADED_MODULES\t$NAME\n"
91 if ! modinfo "$KMOD" >/dev/null 2>&1; then
92 MISSING_MODULES="$MISSING_MODULES\t${KMOD}\n"
94 done
96 if [ -n "$LOADED_MODULES" ]; then
97 printf "Unload the kernel modules by running '%s -u':\n" "$PROG"
98 printf "%b" "$LOADED_MODULES"
99 exit 1
102 if [ -n "$MISSING_MODULES" ]; then
103 printf "The following kernel modules can not be found:\n"
104 printf "%b" "$MISSING_MODULES"
105 exit 1
108 return 0
111 load_module() {
112 KMOD=$1
114 FILE=$(modinfo "$KMOD" | awk '/^filename:/ {print $2}')
115 VERSION=$(modinfo "$KMOD" | awk '/^version:/ {print $2}')
117 if [ "$VERBOSE" = "yes" ]; then
118 echo "Loading: $FILE ($VERSION)"
121 $LDMOD "$KMOD" >/dev/null 2>&1
122 # shellcheck disable=SC2181
123 if [ $? -ne 0 ]; then
124 echo "Failed to load $KMOD"
125 return 1
128 return 0
131 load_modules() {
132 mkdir -p /etc/zfs
134 if modinfo "$KMOD_ZLIB_DEFLATE" >/dev/null 2>&1; then
135 modprobe "$KMOD_ZLIB_DEFLATE" >/dev/null 2>&1
138 if modinfo "$KMOD_ZLIB_INFLATE">/dev/null 2>&1; then
139 modprobe "$KMOD_ZLIB_INFLATE" >/dev/null 2>&1
142 for KMOD in $KMOD_SPL $KMOD_ZAVL $KMOD_ZNVPAIR \
143 $KMOD_ZUNICODE $KMOD_ZCOMMON $KMOD_ZLUA $KMOD_ICP $KMOD_ZFS; do
144 load_module "$KMOD" || return 1
145 done
147 if [ "$VERBOSE" = "yes" ]; then
148 echo "Successfully loaded ZFS module stack"
151 return 0
154 unload_module() {
155 KMOD=$1
157 NAME=$(basename "$KMOD" .ko)
158 FILE=$(modinfo "$KMOD" | awk '/^filename:/ {print $2}')
159 VERSION=$(modinfo "$KMOD" | awk '/^version:/ {print $2}')
161 if [ "$VERBOSE" = "yes" ]; then
162 echo "Unloading: $KMOD ($VERSION)"
165 rmmod "$NAME" || echo "Failed to unload $NAME"
167 return 0
170 unload_modules() {
171 for KMOD in $KMOD_ZFS $KMOD_ICP $KMOD_ZLUA $KMOD_ZCOMMON $KMOD_ZUNICODE \
172 $KMOD_ZNVPAIR $KMOD_ZAVL $KMOD_SPL; do
173 NAME=$(basename "$KMOD" .ko)
174 USE_COUNT=$(lsmod | grep -E "^${NAME} " | awk '{print $3}')
176 if [ "$USE_COUNT" = "0" ] ; then
177 unload_module "$KMOD" || return 1
179 done
181 if modinfo "$KMOD_ZLIB_DEFLATE" >/dev/null 2>&1; then
182 modprobe -r "$KMOD_ZLIB_DEFLATE" >/dev/null 2>&1
185 if modinfo "$KMOD_ZLIB_INFLATE">/dev/null 2>&1; then
186 modprobe -r "$KMOD_ZLIB_INFLATE" >/dev/null 2>&1
189 if [ "$VERBOSE" = "yes" ]; then
190 echo "Successfully unloaded ZFS module stack"
193 return 0
196 stack_clear() {
197 STACK_MAX_SIZE=/sys/kernel/debug/tracing/stack_max_size
198 STACK_TRACER_ENABLED=/proc/sys/kernel/stack_tracer_enabled
200 if [ "$STACK_TRACER" = "yes" ] && [ -e "$STACK_MAX_SIZE" ]; then
201 echo 1 >"$STACK_TRACER_ENABLED"
202 echo 0 >"$STACK_MAX_SIZE"
206 stack_check() {
207 STACK_MAX_SIZE=/sys/kernel/debug/tracing/stack_max_size
208 STACK_TRACE=/sys/kernel/debug/tracing/stack_trace
209 STACK_LIMIT=15362
211 if [ -e "$STACK_MAX_SIZE" ]; then
212 STACK_SIZE=$(cat "$STACK_MAX_SIZE")
214 if [ "$STACK_SIZE" -ge "$STACK_LIMIT" ]; then
215 echo
216 echo "Warning: max stack size $STACK_SIZE bytes"
217 cat "$STACK_TRACE"
222 if [ "$(id -u)" != 0 ]; then
223 echo "Must run as root"
224 exit 1
227 if [ "$UNLOAD" = "yes" ]; then
228 kill_zed
229 umount -t zfs -a
230 stack_check
231 unload_modules
232 else
233 stack_clear
234 check_modules
235 load_modules "$@"
236 udevadm trigger
237 udevadm settle
240 exit 0