| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# Exit on 1st error |
| 4 |
set -e |
| 5 |
|
| 6 |
# NFS exports file. Some code below keeps a cache of output derived |
| 7 |
# from exportfs(8). When this file is updated the cache is invalid |
| 8 |
# and needs to be regenerated. |
| 9 |
nfs_exports_file="${CTDB_NFS_EXPORTS_FILE:-/var/lib/nfs/etab}" |
| 10 |
|
| 11 |
# Do not set CTDB_NFS_DISTRO_STYLE - it isn't a configuration |
| 12 |
# variable, just a hook for testing. To change the style, edit the |
| 13 |
# default value below. |
| 14 |
nfs_distro_style="${CTDB_NFS_DISTRO_STYLE:-systemd-redhat}" |
| 15 |
|
| 16 |
# As above, edit the default value below. CTDB_SYS_ETCDIR is a |
| 17 |
# test variable only. |
| 18 |
etc_dir="${CTDB_SYS_ETCDIR:-/etc}" |
| 19 |
|
| 20 |
# A value of "AUTO" for any service means that service is usually |
| 21 |
# automatically started and stopped by one of the other services. |
| 22 |
# Such services will still be restarted by hand on failure, if |
| 23 |
# configured to do so. This allows services that should not be |
| 24 |
# running to be set to "". |
| 25 |
|
| 26 |
case "$nfs_distro_style" in |
| 27 |
systemd-*) |
| 28 |
# Defaults |
| 29 |
nfs_service="nfs-server" |
| 30 |
nfs_lock_service="rpc-statd" |
| 31 |
nfs_mountd_service="nfs-mountd" |
| 32 |
nfs_status_service="rpc-statd" |
| 33 |
nfs_rquotad_service="rpc-rquotad" |
| 34 |
nfs_config="${etc_dir}/sysconfig/nfs" |
| 35 |
nfs_rquotad_config="" # Not use with systemd, restart via service |
| 36 |
|
| 37 |
case "$nfs_distro_style" in |
| 38 |
*-redhat | *-suse) |
| 39 |
: # Defaults only |
| 40 |
;; |
| 41 |
*-debian) |
| 42 |
nfs_rquotad_service="quotarpc" |
| 43 |
;; |
| 44 |
*) |
| 45 |
echo "Internal error" |
| 46 |
exit 1 |
| 47 |
;; |
| 48 |
esac |
| 49 |
;; |
| 50 |
|
| 51 |
sysvinit-*) |
| 52 |
# Defaults |
| 53 |
nfs_service="nfs" |
| 54 |
nfs_lock_service="AUTO" |
| 55 |
nfs_mountd_service="AUTO" |
| 56 |
nfs_status_service="AUTO" |
| 57 |
nfs_rquotad_service="AUTO" |
| 58 |
nfs_config="${etc_dir}/sysconfig/nfs" |
| 59 |
nfs_rquotad_config="$nfs_config" |
| 60 |
|
| 61 |
case "$nfs_distro_style" in |
| 62 |
*-redhat) |
| 63 |
nfs_lock_service="nfslock" |
| 64 |
;; |
| 65 |
*-suse) |
| 66 |
nfs_service="nfsserver" |
| 67 |
;; |
| 68 |
*-debian) |
| 69 |
nfs_service="nfs-kernel-server" |
| 70 |
nfs_config="${etc_dir}/default/nfs-kernel-server" |
| 71 |
nfs_rquotad_config="${etc_dir}/default/quota" |
| 72 |
;; |
| 73 |
*) |
| 74 |
echo "Internal error" |
| 75 |
exit 1 |
| 76 |
;; |
| 77 |
esac |
| 78 |
;; |
| 79 |
|
| 80 |
*) |
| 81 |
echo "Internal error" |
| 82 |
exit 1 |
| 83 |
;; |
| 84 |
esac |
| 85 |
|
| 86 |
# Override for unit testing |
| 87 |
if [ -z "$PROCFS_PATH" ]; then |
| 88 |
PROCFS_PATH="/proc" |
| 89 |
fi |
| 90 |
|
| 91 |
################################################## |
| 92 |
|
| 93 |
usage() |
| 94 |
{ |
| 95 |
_c=$(basename "$0") |
| 96 |
cat <<EOF |
| 97 |
usage: $_c { shutdown | startup } |
| 98 |
$_c { stop | start } { nfs | nlockmgr } |
| 99 |
$_c { monitor-list-shares | monitor-post } |
| 100 |
$_c { register } |
| 101 |
EOF |
| 102 |
exit 1 |
| 103 |
} |
| 104 |
|
| 105 |
################################################## |
| 106 |
|
| 107 |
nfs_load_config() |
| 108 |
{ |
| 109 |
_config="${1:-${nfs_config}}" |
| 110 |
|
| 111 |
if [ -r "$_config" ]; then |
| 112 |
. "$_config" |
| 113 |
fi |
| 114 |
} |
| 115 |
|
| 116 |
################################################## |
| 117 |
|
| 118 |
service_is_auto_started() |
| 119 |
{ |
| 120 |
[ "$1" = "AUTO" ] |
| 121 |
} |
| 122 |
|
| 123 |
service_is_defined() |
| 124 |
{ |
| 125 |
_service="$1" |
| 126 |
|
| 127 |
[ -n "$_service" ] && ! service_is_auto_started "$_service" |
| 128 |
} |
| 129 |
|
| 130 |
service_if_defined() |
| 131 |
{ |
| 132 |
_service="$1" |
| 133 |
_action="$2" |
| 134 |
|
| 135 |
if service_is_defined "$_service"; then |
| 136 |
service "$_service" "$_action" |
| 137 |
fi |
| 138 |
} |
| 139 |
|
| 140 |
################################################## |
| 141 |
# Overall NFS service stop and start |
| 142 |
|
| 143 |
nfs_service_stop() |
| 144 |
{ |
| 145 |
service_if_defined "$nfs_rquotad_service" stop |
| 146 |
|
| 147 |
service "$nfs_service" stop |
| 148 |
|
| 149 |
service_if_defined "$nfs_lock_service" stop |
| 150 |
} |
| 151 |
|
| 152 |
nfs_service_start() |
| 153 |
{ |
| 154 |
service_if_defined "$nfs_lock_service" start |
| 155 |
|
| 156 |
service "$nfs_service" start |
| 157 |
|
| 158 |
service_if_defined "$nfs_rquotad_service" start |
| 159 |
} |
| 160 |
|
| 161 |
################################################## |
| 162 |
# service "stop" and "start" options for restarting |
| 163 |
|
| 164 |
manual_stop() |
| 165 |
{ |
| 166 |
case "$1" in |
| 167 |
mountd) |
| 168 |
killall -q -9 rpc.mountd |
| 169 |
;; |
| 170 |
rquotad) |
| 171 |
killall -q -9 rpc.rquotad |
| 172 |
;; |
| 173 |
status) |
| 174 |
killall -q -9 rpc.statd |
| 175 |
;; |
| 176 |
*) |
| 177 |
echo "$0: Internal error - invalid call to manual_stop()" |
| 178 |
exit 1 |
| 179 |
;; |
| 180 |
esac |
| 181 |
} |
| 182 |
|
| 183 |
service_or_manual_stop() |
| 184 |
{ |
| 185 |
_rpc_service="$1" |
| 186 |
_system_service="$2" |
| 187 |
|
| 188 |
if service_is_defined "$_system_service"; then |
| 189 |
service "$_system_service" stop |
| 190 |
elif service_is_auto_started "$_system_service"; then |
| 191 |
manual_stop "$_rpc_service" |
| 192 |
fi |
| 193 |
} |
| 194 |
|
| 195 |
service_stop() |
| 196 |
{ |
| 197 |
_rpc_service="$1" |
| 198 |
|
| 199 |
case "$_rpc_service" in |
| 200 |
nfs) |
| 201 |
echo 0 >"${PROCFS_PATH}/fs/nfsd/threads" |
| 202 |
nfs_service_stop >/dev/null 2>&1 || true |
| 203 |
pkill -9 nfsd |
| 204 |
;; |
| 205 |
nlockmgr) |
| 206 |
if service_is_defined "$nfs_lock_service" ; then |
| 207 |
service "$nfs_lock_service" stop >/dev/null 2>&1 || true |
| 208 |
else |
| 209 |
service "$nfs_service" stop >/dev/null 2>&1 || true |
| 210 |
fi |
| 211 |
;; |
| 212 |
mountd) |
| 213 |
service_or_manual_stop "$_rpc_service" "$nfs_mountd_service" |
| 214 |
;; |
| 215 |
rquotad) |
| 216 |
service_or_manual_stop "$_rpc_service" "$nfs_rquotad_service" |
| 217 |
;; |
| 218 |
status) |
| 219 |
service_or_manual_stop "$_rpc_service" "$nfs_status_service" |
| 220 |
;; |
| 221 |
*) |
| 222 |
usage |
| 223 |
;; |
| 224 |
esac |
| 225 |
} |
| 226 |
|
| 227 |
manual_start() |
| 228 |
{ |
| 229 |
case "$1" in |
| 230 |
mountd) |
| 231 |
nfs_load_config |
| 232 |
if [ -z "$RPCMOUNTDOPTS" ]; then |
| 233 |
RPCMOUNTDOPTS="${MOUNTD_PORT:+-p }$MOUNTD_PORT" |
| 234 |
fi |
| 235 |
# shellcheck disable=SC2086 |
| 236 |
rpc.mountd $RPCMOUNTDOPTS |
| 237 |
;; |
| 238 |
rquotad) |
| 239 |
nfs_load_config "$nfs_rquotad_config" |
| 240 |
if [ -z "$RPCRQUOTADOPTS" ]; then |
| 241 |
RPCRQUOTADOPTS="${RQUOTAD_PORT:+-p }$RQUOTAD_PORT" |
| 242 |
fi |
| 243 |
# shellcheck disable=SC2086 |
| 244 |
rpc.rquotad $RPCRQUOTADOPTS |
| 245 |
;; |
| 246 |
status) |
| 247 |
nfs_load_config |
| 248 |
# Red Hat uses STATDARG, Debian uses STATDOPTS |
| 249 |
opts="${STATDARG:-${STATDOPTS:-''}}" |
| 250 |
if [ -z "$opts" ]; then |
| 251 |
# shellcheck disable=SC2086 |
| 252 |
set -- \ |
| 253 |
${STATD_HA_CALLOUT:+-H} $STATD_HA_CALLOUT \ |
| 254 |
${STATD_HOSTNAME:+-n} $STATD_HOSTNAME \ |
| 255 |
${STATD_PORT:+-p} $STATD_PORT \ |
| 256 |
${STATD_OUTGOING_PORT:+-o} $STATD_OUTGOING_PORT |
| 257 |
opts="$*" |
| 258 |
fi |
| 259 |
# shellcheck disable=SC2086 |
| 260 |
rpc.statd $opts |
| 261 |
;; |
| 262 |
*) |
| 263 |
echo "$0: Internal error - invalid call to manual_start()" |
| 264 |
exit 1 |
| 265 |
;; |
| 266 |
esac |
| 267 |
} |
| 268 |
|
| 269 |
service_or_manual_start() |
| 270 |
{ |
| 271 |
_rpc_service="$1" |
| 272 |
_system_service="$2" |
| 273 |
|
| 274 |
if service_is_defined "$_system_service"; then |
| 275 |
service "$_system_service" start |
| 276 |
elif service_is_auto_started "$_system_service"; then |
| 277 |
manual_start "$_rpc_service" |
| 278 |
fi |
| 279 |
} |
| 280 |
|
| 281 |
service_start() |
| 282 |
{ |
| 283 |
_rpc_service="$1" |
| 284 |
|
| 285 |
case "$_rpc_service" in |
| 286 |
nfs) |
| 287 |
nfs_service_start |
| 288 |
;; |
| 289 |
nlockmgr) |
| 290 |
if service_is_defined "$nfs_lock_service" ; then |
| 291 |
service "$nfs_lock_service" start |
| 292 |
else |
| 293 |
service "$nfs_service" start |
| 294 |
fi |
| 295 |
;; |
| 296 |
mountd) |
| 297 |
service_or_manual_start "$_rpc_service" "$nfs_mountd_service" |
| 298 |
;; |
| 299 |
rquotad) |
| 300 |
service_or_manual_start "$_rpc_service" "$nfs_rquotad_service" |
| 301 |
;; |
| 302 |
status) |
| 303 |
service_or_manual_start "$_rpc_service" "$nfs_status_service" |
| 304 |
;; |
| 305 |
*) |
| 306 |
usage |
| 307 |
;; |
| 308 |
esac |
| 309 |
} |
| 310 |
|
| 311 |
################################################## |
| 312 |
# service init startup and final shutdown |
| 313 |
|
| 314 |
nfs_shutdown() |
| 315 |
{ |
| 316 |
nfs_service_stop |
| 317 |
} |
| 318 |
|
| 319 |
nfs_startup() |
| 320 |
{ |
| 321 |
nfs_service_stop || true |
| 322 |
nfs_service_start |
| 323 |
_f="${PROCFS_PATH}/sys/net/ipv4/tcp_tw_recycle" |
| 324 |
if [ -f "$_f" ]; then |
| 325 |
echo 1 >"$_f" |
| 326 |
fi |
| 327 |
} |
| 328 |
|
| 329 |
################################################## |
| 330 |
# monitor-post support |
| 331 |
|
| 332 |
nfs_check_thread_count() |
| 333 |
{ |
| 334 |
# Load NFS configuration to get desired number of threads. |
| 335 |
nfs_load_config |
| 336 |
|
| 337 |
# If $RPCNFSDCOUNT/$USE_KERNEL_NFSD_NUMBER isn't set then we could |
| 338 |
# guess the default from the initscript. However, let's just |
| 339 |
# assume that those using the default don't care about the number |
| 340 |
# of threads and that they have switched on this feature in error. |
| 341 |
_configured_threads="${RPCNFSDCOUNT:-${USE_KERNEL_NFSD_NUMBER}}" |
| 342 |
if [ -z "$_configured_threads" ] && type nfsconf >/dev/null 2>&1; then |
| 343 |
_configured_threads=$(nfsconf --get nfsd threads) || true |
| 344 |
fi |
| 345 |
[ -n "$_configured_threads" ] || return 0 |
| 346 |
|
| 347 |
_threads_file="${PROCFS_PATH}/fs/nfsd/threads" |
| 348 |
|
| 349 |
# nfsd should be running the configured number of threads. If |
| 350 |
# there are a different number of threads then tell nfsd the |
| 351 |
# correct number. |
| 352 |
read -r _running_threads <"$_threads_file" || { |
| 353 |
echo "WARNING: Reading \"${_threads_file}\" unexpectedly failed" |
| 354 |
exit 0 |
| 355 |
} |
| 356 |
|
| 357 |
# Intentionally not arithmetic comparison - avoids extra errors |
| 358 |
# when above read fails in an unexpected way... |
| 359 |
if [ "$_running_threads" != "$_configured_threads" ]; then |
| 360 |
echo "Attempting to correct number of nfsd threads from ${_running_threads} to ${_configured_threads}" |
| 361 |
echo "$_configured_threads" >"$_threads_file" |
| 362 |
fi |
| 363 |
} |
| 364 |
|
| 365 |
################################################## |
| 366 |
# list share directories |
| 367 |
|
| 368 |
nfs_monitor_list_shares() |
| 369 |
{ |
| 370 |
_cache_file="${CTDB_NFS_CALLOUT_STATE_DIR}/list_shares_cache" |
| 371 |
# -nt operator is well supported in Linux: dash, bash, ksh, ... |
| 372 |
# shellcheck disable=SC2039,SC3013 |
| 373 |
if [ ! -r "$nfs_exports_file" ] || [ ! -r "$_cache_file" ] || |
| 374 |
[ "$nfs_exports_file" -nt "$_cache_file" ]; then |
| 375 |
mkdir -p "$CTDB_NFS_CALLOUT_STATE_DIR" |
| 376 |
# We could just use the contents of $nfs_exports_file. |
| 377 |
# However, let's regard that file as internal to NFS and use |
| 378 |
# exportfs, which is the public API. |
| 379 |
if ! _exports=$(exportfs -v); then |
| 380 |
echo "WARNING: failed to run exportfs to list NFS shares" >&2 |
| 381 |
return |
| 382 |
fi |
| 383 |
|
| 384 |
echo "$_exports" | |
| 385 |
grep '^/' | |
| 386 |
sed -e 's@[[:space:]][[:space:]]*[^[:space:]()][^[:space:]()]*([^[:space:]()][^[:space:]()]*)$@@' | |
| 387 |
sort -u >"$_cache_file" |
| 388 |
fi |
| 389 |
|
| 390 |
cat "$_cache_file" |
| 391 |
} |
| 392 |
|
| 393 |
################################################## |
| 394 |
|
| 395 |
nfs_register() |
| 396 |
{ |
| 397 |
cat <<EOF |
| 398 |
shutdown |
| 399 |
startup |
| 400 |
stop |
| 401 |
start |
| 402 |
monitor-list-shares |
| 403 |
monitor-post |
| 404 |
EOF |
| 405 |
} |
| 406 |
|
| 407 |
################################################## |
| 408 |
|
| 409 |
case "$1" in |
| 410 |
shutdown) |
| 411 |
nfs_shutdown |
| 412 |
;; |
| 413 |
startup) |
| 414 |
nfs_startup |
| 415 |
;; |
| 416 |
stop) |
| 417 |
service_stop "$2" |
| 418 |
;; |
| 419 |
start) |
| 420 |
service_start "$2" |
| 421 |
;; |
| 422 |
monitor-list-shares) |
| 423 |
nfs_monitor_list_shares |
| 424 |
;; |
| 425 |
monitor-post) |
| 426 |
nfs_check_thread_count |
| 427 |
;; |
| 428 |
register) |
| 429 |
nfs_register |
| 430 |
;; |
| 431 |
monitor-pre | releaseip | takeip | startipreallocate) |
| 432 |
# Not required/implemented |
| 433 |
: |
| 434 |
;; |
| 435 |
*) |
| 436 |
usage |
| 437 |
;; |
| 438 |
esac |