summaryrefslogtreecommitdiffstats
path: root/extras/hook-scripts/set/post/S31ganesha-set.sh
blob: 972f82c57302e665ece78ae91cb56364a80442be (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/bin/bash
PROGNAME="Sganesha-set"
OPTSPEC="volname:"
VOL=
declare -i EXPORT_ID
GANESHA_DIR="/var/lib/glusterfs-ganesha"
CONF1="$GANESHA_DIR/nfs-ganesha.conf"
GANESHA_LOG_DIR="/var/log/nfs-ganesha/"
LOG="$GANESHA_LOG_DIR/ganesha.nfsd.log"
gnfs="enabled"
enable_ganesha=""
host_name="none"
LOC=""



function parse_args ()
{
        ARGS=$(getopt -l $OPTSPEC  -o "o" -name $PROGNAME $@)
        eval set -- "$ARGS"

        while true; do
            case $1 in
            --volname)
                shift
                VOL=$1
                ;;
            *)
                shift
                for pair in $@; do
                        read key value < <(echo "$pair" | tr "=" " ")
                        case "$key" in
                          "nfs-ganesha.enable")
                                    enable_ganesha=$value
                                    ;;
                          "nfs-ganesha.host")
                                    host_name=$value
                                    ;;
                            *)
                                    ;;
                        esac
                done

                shift
                break
                ;;
            esac
            shift
        done
}


function check_if_host_set()
{
        if ! cat /var/lib/glusterd/vols/$VOL/info | grep -q "nfs-ganesha.host"
                then
                exit 1
        fi
}

function check_nfsd_loc()
{
        if ls /usr/bin | grep "[g]anesha.nfsd"
                then
                LOC="/usr"
        else
                LOC="/usr/local"
        fi
}


function check_gluster_nfs()
{
        if cat /var/lib/glusterd/vols/$VOL/info | grep -q "nfs.disable=ON"
                 then
                 gnfs="disabled"
        fi
}

function check_cmd_status()
{
        if [ "$1" != "0" ]
                 then
                 rm -rf $GANESHA_DIR/exports/export.$VOL.conf
                 exit 1
        fi
}



#This function generates a new export entry as export.volume_name.conf
function write_conf()
{
        echo "EXPORT{
        "
        echo "Export_Id = ;"
        echo "Path=\"/$1\";"
        echo "FSAL {
        "
        echo "name = "GLUSTER";"
        echo "hostname=\"$2\";"
        echo  "volume=\"$1\";"
        echo "}"
        echo "Access_type = RW;"
        echo "Squash = No_root_squash;"
        echo "Disable_ACL = TRUE;"
        echo "Pseudo=\"/$1\";"
        echo "Protocols = \"3,4\" ;"
        echo "Transports = \"UDP,TCP\" ;"
        echo "SecType = \"sys\";"
        echo "Tag = \"$1\";"
        echo "}"
}

#This function keeps track of export IDs and increments it with every new entry
function export_add()
{
        count=`ls -l $GANESHA_DIR/exports/*.conf | wc -l`
        if [ "$count" = "1" ] ;
                then
                EXPORT_ID=1
        else
        #if [ -s /var/lib/ganesha/export_removed ];
        #               then
        #               EXPORT_ID=`head -1 /var/lib/ganesha/export_removed`
        #               sed -i -e "1d" /var/lib/ganesha/export_removed
        #               else

                 EXPORT_ID=`cat $GANESHA_DIR/.export_added`
                 check_cmd_status `echo $?`
                 EXPORT_ID=EXPORT_ID+1
        #fi
        fi
        echo $EXPORT_ID > $GANESHA_DIR/.export_added
        check_cmd_status `echo $?`
        sed -i s/Export_Id.*/"Export_Id = $EXPORT_ID ;"/ \
$GANESHA_DIR/exports/export.$VOL.conf
        echo "%include \"$GANESHA_DIR/exports/export.$VOL.conf\"" >> $CONF1
        check_cmd_status `echo $?`
}

#This function removes an export dynamically(uses the export_id of the export)
function dynamic_export_remove()
{
        removed_id=`cat $GANESHA_DIR/exports/export.$VOL.conf |\
grep Export_Id | cut -d " " -f3`
        check_cmd_status `echo $?`
        dbus-send --print-reply --system \
--dest=org.ganesha.nfsd /org/ganesha/nfsd/ExportMgr \
org.ganesha.nfsd.exportmgr.RemoveExport uint16:$removed_id
        check_cmd_status `echo $?`

}

#This function adds a new export dynamically by sending dbus signals
function dynamic_export_add()
{
        dbus-send --print-reply --system --dest=org.ganesha.nfsd \
/org/ganesha/nfsd/ExportMgr org.ganesha.nfsd.exportmgr.AddExport \
string:$GANESHA_DIR/exports/export.$VOL.conf string:"EXPORT(Tag=$VOL)"
        check_cmd_status `echo $?`

}

function start_ganesha()
{
        check_gluster_nfs
        #Remove export entry from nfs-ganesha.conf
        sed -i /$VOL.conf/d  $CONF1
        #Create a new export entry
        export_add
        if ! ps aux | grep -q  "[g]anesha.nfsd"
                then
                if ls /usr/bin/ganesha.nfsd
                       then
                       /usr/bin/ganesha.nfsd -f $CONF1 -L $LOG -N NIV_EVENT -d
                        sleep 2
                else
                        /usr/local/bin/ganesha.nfsd -f $CONF1 -L $LOG -N NIV_EVENT -d
                        sleep 2
                fi
        else
                        dynamic_export_add $VOL
        fi


        if  !(  ps aux | grep -q "[g]anesha.nfsd")
                then
                rm -rf $GANESHA_DIR/exports/*
                rm -rf $GANESHA_DIR/.export_added
                exit 1
        fi

}

#This function generates a new config file when ganesha.host is set
#If the volume is already exported, only hostname is changed
function set_hostname()
{
        if  ! ls $GANESHA_DIR/exports/  | grep -q $VOL.conf
                then
                write_conf $VOL $host_name >\
$GANESHA_DIR/exports/export.$VOL.conf
        else
                sed -i  s/hostname.*/"hostname=\
\"$host_name\";"/ $GANESHA_DIR/exports/export.$VOL.conf
        fi
}


function check_ganesha_dir()
{
        #Check if the configuration file is placed in /etc/glusterfs-ganesha
        if ! ls  /etc/glusterfs-ganesha  | grep "nfs-ganesha.conf"
        then
               exit 1
        else
                if [ ! -d "$GANESHA_DIR" ];
                         then
                         mkdir $GANESHA_DIR
                         check_cmd_status `echo $?`
                fi
                cp /etc/glusterfs-ganesha/nfs-ganesha.conf $GANESHA_DIR/
                check_cmd_status `echo $?`
        fi
        if [ ! -d "$GANESHA_DIR/exports" ];
                then
                mkdir $GANESHA_DIR/exports
                check_cmd_status `echo $?`
        fi
        if [ ! -d "$GANESHA_LOG_DIR" ] ;
                then
                mkdir $GANESHA_LOG_DIR
                check_cmd_status `echo $?`
        fi



}

function stop_ganesha()
{
        dynamic_export_remove $VOL
        #Remove the specfic export configuration file
        rm -rf $GANESHA_DIR/exports/export.$VOL.conf
        #Remove that entry from nfs-ganesha.conf
        sed -i /$VOL.conf/d  $CONF1
        #If there are no other volumes exported, stop nfs-ganesha
        if [ ! "$(ls -A $GANESHA_DIR/exports)" ];
                then
                pkill ganesha.nfsd
                rm -rf $GANESHA_DIR/.export_added
        fi
}

        parse_args $@
        check_ganesha_dir $VOL
        if echo $enable_ganesha | grep -q -i "ON"
                then
                check_if_host_set $VOL
                if ! showmount -e localhost | cut -d "" -f1 | grep -q "$VOL[[:space:]]"
                        then
                        start_ganesha
                fi
        elif echo $enable_ganesha | grep -q -i "OFF"
                then
                check_if_host_set $VOL
                stop_ganesha
        fi
        if [ "$host_name" != "none" ];
                then
                if showmount -e localhost | cut -d "" -f1 | grep -q "$VOL[[:space:]]"
                        then
                        dynamic_export_remove $VOL
                        set_hostname
                        start_ganesha
                else
                        set_hostname
                fi

        fi