ZAP: Reduce leaf array and free chunks fragmentation
[zfs.git] / scripts / zfs.sh
blob502c5430ab058ccb250c21fed6d9ef24f8e7099f
1 #!/bin/sh
3 # A simple script to load/unload the ZFS module stack.
6 BASE_DIR=${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 VERBOSE="no"
15 UNLOAD="no"
16 LOAD="yes"
17 STACK_TRACER="no"
19 ZED_PIDFILE=${ZED_PIDFILE:-/var/run/zed.pid}
20 LDMOD=${LDMOD:-/sbin/modprobe}
21 DELMOD=${DELMOD:-/sbin/rmmod}
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_ZFS=${KMOD_ZFS:-zfs}
27 KMOD_FREEBSD=${KMOD_FREEBSD:-openzfs}
30 usage() {
31 cat << EOF
32 USAGE:
33 $0 [hvudS]
35 DESCRIPTION:
36 Load/unload the ZFS module stack.
38 OPTIONS:
39 -h Show this message
40 -v Verbose
41 -r Reload modules
42 -u Unload modules
43 -S Enable kernel stack tracer
44 EOF
45 exit 1
48 while getopts 'hvruS' OPTION; do
49 case $OPTION in
51 VERBOSE="yes"
54 UNLOAD="yes"
55 LOAD="yes"
58 UNLOAD="yes"
59 LOAD="no"
62 STACK_TRACER="yes"
65 usage
67 esac
68 done
69 shift $(( OPTIND - 1 ))
70 [ $# -eq 0 ] || usage
72 kill_zed() {
73 if [ -f "$ZED_PIDFILE" ]; then
74 read -r PID <"$ZED_PIDFILE"
75 kill "$PID"
79 check_modules_linux() {
80 LOADED_MODULES=""
81 MISSING_MODULES=""
83 for KMOD in $KMOD_SPL $KMOD_ZFS; do
84 NAME="${KMOD##*/}"
85 NAME="${NAME%.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" "$0"
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_linux() {
112 KMOD=$1
114 FILE=$(modinfo "$KMOD" 2>&1 | awk 'NR == 1 && /zlib/ && /not found/ {print "(builtin)"; exit} /^filename:/ {print $2}')
115 [ "$FILE" = "(builtin)" ] && return
117 if [ "$VERBOSE" = "yes" ]; then
118 VERSION=$(modinfo "$KMOD" | awk '/^version:/ {print $2}')
119 echo "Loading: $FILE ($VERSION)"
122 if ! $LDMOD "$KMOD" >/dev/null 2>&1; then
123 echo "Failed to load $KMOD"
124 return 1
127 return 0
130 load_modules_freebsd() {
131 kldload "$KMOD_FREEBSD" || return 1
133 if [ "$VERBOSE" = "yes" ]; then
134 echo "Successfully loaded ZFS module stack"
137 return 0
140 load_modules_linux() {
141 mkdir -p /etc/zfs
143 for KMOD in "$KMOD_ZLIB_DEFLATE" "$KMOD_ZLIB_INFLATE" $KMOD_SPL $KMOD_ZFS; do
144 load_module_linux "$KMOD" || return 1
145 done
147 if [ "$VERBOSE" = "yes" ]; then
148 echo "Successfully loaded ZFS module stack"
151 return 0
154 unload_modules_freebsd() {
155 kldunload "$KMOD_FREEBSD" || echo "Failed to unload $KMOD_FREEBSD"
157 if [ "$VERBOSE" = "yes" ]; then
158 echo "Successfully unloaded ZFS module stack"
161 return 0
164 unload_modules_linux() {
165 legacy_kmods="icp zzstd zlua zcommon zunicode znvpair zavl"
166 for KMOD in "$KMOD_ZFS" $legacy_kmods "$KMOD_SPL"; do
167 NAME="${KMOD##*/}"
168 NAME="${NAME%.ko}"
169 ! [ -d "/sys/module/$NAME" ] || $DELMOD "$NAME" || return
170 done
172 if [ "$VERBOSE" = "yes" ]; then
173 echo "Successfully unloaded ZFS module stack"
177 stack_clear_linux() {
178 STACK_MAX_SIZE=/sys/kernel/debug/tracing/stack_max_size
179 STACK_TRACER_ENABLED=/proc/sys/kernel/stack_tracer_enabled
181 if [ "$STACK_TRACER" = "yes" ] && [ -e "$STACK_MAX_SIZE" ]; then
182 echo 1 >"$STACK_TRACER_ENABLED"
183 echo 0 >"$STACK_MAX_SIZE"
187 stack_check_linux() {
188 STACK_MAX_SIZE=/sys/kernel/debug/tracing/stack_max_size
189 STACK_TRACE=/sys/kernel/debug/tracing/stack_trace
190 STACK_LIMIT=15362
192 if [ -e "$STACK_MAX_SIZE" ]; then
193 read -r STACK_SIZE <"$STACK_MAX_SIZE"
194 if [ "$STACK_SIZE" -ge "$STACK_LIMIT" ]; then
195 echo
196 echo "Warning: max stack size $STACK_SIZE bytes"
197 cat "$STACK_TRACE"
202 if [ "$(id -u)" != 0 ]; then
203 echo "Must run as root"
204 exit 1
207 UNAME=$(uname)
209 if [ "$UNLOAD" = "yes" ]; then
210 kill_zed
211 umount -t zfs -a
212 case $UNAME in
213 FreeBSD)
214 unload_modules_freebsd
216 Linux)
217 stack_check_linux
218 unload_modules_linux
221 echo "unknown system: $UNAME" >&2
222 exit 1
224 esac
226 if [ "$LOAD" = "yes" ]; then
227 case $UNAME in
228 FreeBSD)
229 load_modules_freebsd
231 Linux)
232 stack_clear_linux
233 check_modules_linux
234 load_modules_linux
235 udevadm trigger
236 udevadm settle
239 echo "unknown system: $UNAME" >&2
240 exit 1
242 esac
245 exit 0