run_nfs.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env bash
  2. # Copyright 2015 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. function start()
  16. {
  17. unset gid
  18. # accept "-G gid" option
  19. while getopts "G:" opt; do
  20. case ${opt} in
  21. G) gid=${OPTARG};;
  22. *):;;
  23. esac
  24. done
  25. shift $((OPTIND - 1))
  26. # prepare /etc/exports
  27. for i in "$@"; do
  28. # fsid=0: needed for NFSv4
  29. echo "$i *(rw,fsid=0,insecure,no_root_squash)" >> /etc/exports
  30. if [ -v gid ] ; then
  31. chmod 070 "$i"
  32. chgrp "$gid" "$i"
  33. fi
  34. # move index.html to here
  35. /bin/cp /tmp/index.html "$i/"
  36. chmod 644 "$i/index.html"
  37. echo "Serving $i"
  38. done
  39. # start rpcbind if it is not started yet
  40. /usr/sbin/rpcinfo 127.0.0.1 > /dev/null; s=$?
  41. if [ $s -ne 0 ]; then
  42. echo "Starting rpcbind"
  43. /usr/sbin/rpcbind -w
  44. fi
  45. mount -t nfsd nfsd /proc/fs/nfsd
  46. # -V 3: enable NFSv3
  47. /usr/sbin/rpc.mountd -N 2 -V 3
  48. /usr/sbin/exportfs -r
  49. # -G 10 to reduce grace time to 10 seconds (the lowest allowed)
  50. /usr/sbin/rpc.nfsd -G 10 -N 2 -V 3
  51. /usr/sbin/rpc.statd --no-notify
  52. echo "NFS started"
  53. }
  54. function stop()
  55. {
  56. echo "Stopping NFS"
  57. /usr/sbin/rpc.nfsd 0
  58. /usr/sbin/exportfs -au
  59. /usr/sbin/exportfs -f
  60. kill "$( pidof rpc.mountd )"
  61. umount /proc/fs/nfsd
  62. echo > /etc/exports
  63. exit 0
  64. }
  65. trap stop TERM
  66. start "$@"
  67. # Ugly hack to do nothing and wait for SIGTERM
  68. while true; do
  69. sleep 5
  70. done