1.自動檢測並安裝nfs-common,
2.自動創建目錄並mount
3.同時檢查/etc/fstab文件中是否有配置,沒有則加入。確保下次開機能自動mount。
install.sh腳本:
- #!/bin/bash
-
- source ../../common/tool.sh
-
- nfsClient="nfs-common"
- nfsServerFolder=10.112.18.158:/opt/share
- nfsClientFolder=~/test_nfs_dir
-
- hasDpkg $nfsClient
- r=$?
-
- if [ $r -eq 1 ]
- then
- echo "$nfsClient was installed"
- else
- echo "$nfsClient was not installed"
- apt-get install $nfsClient
- fi
-
-
- #config /opt/share as nfs folder
- createFolder $nfsClientFolder
- mount -t nfs4 $nfsServerFolder $nfsClientFolder
-
- mountString="$nfsServerFolder $nfsClientFolder nfs rsize=8192,wsize=8192,timeo=14,intr"
- searchString="$nfsServerFolder"
- mountConfigFile="/etc/fstab"
-
- findStringInFile $searchString $mountConfigFile
- m=$?
-
- if [ $m -eq 1 ]
- then
- echo "auto mount was configured"
- else
- echo "auto mount was not configured, configuring..."
- echo $mountString >> $mountConfigFile
- fi
這裡用了一個新函數: findStringInFile
這個新函數放在tool.sh腳本中:
- #$1 search string
- #$2 file path
- #return 1 if found
- #return 0 if not found
- function findStringInFile {
- h=`grep "$1" $2`
- echo "h: $h"
- if [ -n "$h" ]
- then
- return 1
- else
- return 0
- fi
- }