summaryrefslogtreecommitdiff
path: root/mknfs
blob: 5b04a9f2758ee6660d5d6e5a3c99a452ed07a7e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/sh
#
# Configures and exports an NFS share 

help() {
	echo "usage: mknfs --clients nfs_client --path nfs_path"\
			"[--options \"opt1,opt2,opt3...\"] [--sec sec_option] [-f]"
	echo "\n-c, --clients\tNFS export client"
	echo "-f, --force\tmake directory if it doesn't exist"
	echo "-o, --options\tAdditional NFS export options - quoted and comma separated"
	echo "-p, --path\tPath of directory to be exported - must be absolute"
	echo "-s, --sec\tNFS security settings - defaults to sys"
	echo "\nexample: mknfs --clients server.example.com --path /srv/nfs/backups"\
			"--options \"crossmnt,async\" --sec krb5p"
	exit
}

opts=$(getopt -o "c:,f,h,o:,p:,s:" -l "clients:,force, help,options:,path:,sec:" -- "$@")
eval set -- "$opts"
clients=
options=""
path=
sec="sys"
force=0
while true
do 
	case "$1" in
		'-c' | '--clients') clients="$2" shift 2; continue ;;
		'-f' | '--force') force=1 shift; continue ;;
		'-o' | '--options') options="$2" shift 2; continue ;;
		'-p' | '--path') path="$2" shift 2; continue ;;
		'-s' | '--sec') sec="$2" shift 2; continue ;;
		'-h' | '--help') help ;;
		'--') shift; break ;;
	esac 
done
[ -z "$clients" ] && help
[ -z "$path" ] && help

# Validate path
[ "$(echo $path | cut -d'/' -f1)" != "" ] && 
		echo "error: path is not absolute" && exit 1
[ ! -d $path -a $force -eq 0 ] && 
		echo "error: directory does not exist (use -f to create)" && exit 1
[ ! -d $path -a $force -eq 1 ] && mkdir -p $path 

# Set some sane defaults if no options are specified
[ "$options" = "" ] && options="rw,sync,no_subtree_check"

# Make sure security option is valid
[ $sec != "sys" -a $sec != "krb5" -a $sec != "krb5i" -a $sec != "krb5p" ] &&
		echo "error: invalid security option - must be one of sys,krb5,krb5i,krb5p"

echo "$path\t$clients(sec=$sec,$options)" >> /etc/exports
exportfs -au
exportfs -ar