nfs_lib.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. # Copyright (c) 2015-2018 Oracle and/or its affiliates. All Rights Reserved.
  4. # Copyright (c) International Business Machines Corp., 2001
  5. VERSION=${VERSION:=3}
  6. NFILES=${NFILES:=1000}
  7. SOCKET_TYPE="${SOCKET_TYPE:-udp}"
  8. NFS_TYPE=${NFS_TYPE:=nfs}
  9. nfs_usage()
  10. {
  11. echo "-t x Socket type, tcp or udp, default is udp"
  12. echo "-v x NFS version, default is '3'"
  13. }
  14. nfs_parse_args()
  15. {
  16. case "$1" in
  17. v) VERSION="$(echo $2 | tr ',' ' ')";;
  18. t) SOCKET_TYPE="$(echo $2 | tr ',' ' ')";;
  19. esac
  20. }
  21. TST_OPTS="v:t:"
  22. TST_PARSE_ARGS=nfs_parse_args
  23. TST_USAGE=nfs_usage
  24. TST_NEEDS_TMPDIR=1
  25. TST_NEEDS_ROOT=1
  26. TST_NEEDS_CMDS="$TST_NEEDS_CMDS mount exportfs"
  27. TST_SETUP="${TST_SETUP:-nfs_setup}"
  28. TST_CLEANUP="${TST_CLEANUP:-nfs_cleanup}"
  29. # When set and test is using netns ($TST_USE_NETNS set) NFS traffic will go
  30. # through lo interface instead of ltp_ns_veth* netns interfaces (useful for
  31. # debugging whether test failures are related to veth/netns).
  32. LTP_NFS_NETNS_USE_LO=${LTP_NFS_NETNS_USE_LO:-}
  33. . tst_net.sh
  34. get_socket_type()
  35. {
  36. local t
  37. local k=0
  38. for t in $SOCKET_TYPE; do
  39. if [ "$k" -eq "$1" ]; then
  40. echo "${t}${TST_IPV6}"
  41. return
  42. fi
  43. k=$(( k + 1 ))
  44. done
  45. }
  46. nfs_server_udp_enabled()
  47. {
  48. local config f
  49. tst_rhost_run -c "[ -f /etc/nfs.conf ]" || return 0
  50. config=$(tst_rhost_run -c 'for f in $(grep ^include.*= '/etc/nfs.conf' | cut -d = -f2); do [ -f $f ] && printf "$f "; done')
  51. tst_rhost_run -c "grep -q \"^[# ]*udp *= *y\" /etc/nfs.conf $config"
  52. }
  53. nfs_setup_server()
  54. {
  55. local export_cmd="exportfs -i -o fsid=$$,no_root_squash,rw *:$remote_dir"
  56. if ! tst_rhost_run -c "test -d $remote_dir"; then
  57. tst_rhost_run -s -c "mkdir -p $remote_dir; $export_cmd"
  58. fi
  59. }
  60. nfs_mount()
  61. {
  62. local host_type=rhost
  63. local mount_dir
  64. [ -n "$LTP_NETNS" ] && host_type=
  65. if [ $TST_IPV6 ]; then
  66. mount_dir="[$(tst_ipaddr $host_type)]:$remote_dir"
  67. else
  68. mount_dir="$(tst_ipaddr $host_type):$remote_dir"
  69. fi
  70. local mnt_cmd="mount -t nfs $opts $mount_dir $local_dir"
  71. tst_res TINFO "Mounting NFS: $mnt_cmd"
  72. if [ -n "$LTP_NETNS" ] && [ -z "$LTP_NFS_NETNS_USE_LO" ]; then
  73. tst_rhost_run -c "$mnt_cmd"
  74. else
  75. $mnt_cmd > /dev/null
  76. fi
  77. if [ $? -ne 0 ]; then
  78. if [ "$type" = "udp" -o "$type" = "udp6" ] && tst_kvcmp -ge 5.6; then
  79. tst_brk TCONF "UDP support disabled with the kernel config NFS_DISABLE_UDP_SUPPORT?"
  80. fi
  81. tst_brk TBROK "mount command failed"
  82. fi
  83. }
  84. nfs_setup()
  85. {
  86. # Check if current filesystem is NFS
  87. if [ "$(stat -f . | grep "Type: nfs")" ]; then
  88. tst_brk TCONF "Cannot run nfs-stress test on mounted NFS"
  89. fi
  90. local i
  91. local type
  92. local n=0
  93. local opts
  94. local local_dir
  95. local remote_dir
  96. local mount_dir
  97. for i in $VERSION; do
  98. type=$(get_socket_type $n)
  99. tst_res TINFO "setup NFSv$i, socket type $type"
  100. if [ "$type" = "udp" -o "$type" = "udp6" ] && ! nfs_server_udp_enabled; then
  101. tst_brk TCONF "UDP support disabled on NFS server"
  102. fi
  103. local_dir="$TST_TMPDIR/$i/$n"
  104. remote_dir="$TST_TMPDIR/$i/$type"
  105. mkdir -p $local_dir
  106. nfs_setup_server
  107. opts="-o proto=$type,vers=$i"
  108. nfs_mount
  109. n=$(( n + 1 ))
  110. done
  111. if [ "$n" -eq 1 ]; then
  112. cd ${VERSION}/0
  113. fi
  114. }
  115. nfs_cleanup()
  116. {
  117. tst_res TINFO "Cleaning up testcase"
  118. cd $LTPROOT
  119. local i
  120. local type
  121. local local_dir
  122. local remote_dir
  123. local n=0
  124. for i in $VERSION; do
  125. local_dir="$TST_TMPDIR/$i/$n"
  126. grep -q "$local_dir" /proc/mounts && umount $local_dir
  127. n=$(( n + 1 ))
  128. done
  129. n=0
  130. for i in $VERSION; do
  131. type=$(get_socket_type $n)
  132. remote_dir="$TST_TMPDIR/$i/$type"
  133. tst_rhost_run -c "test -d $remote_dir && exportfs -u *:$remote_dir"
  134. tst_rhost_run -c "test -d $remote_dir && rm -rf $remote_dir"
  135. n=$(( n + 1 ))
  136. done
  137. }