summaryrefslogtreecommitdiffstats
path: root/xlators/mount/fuse/utils/mount.glusterfs.in
blob: 37cc2f9f60a0d749c51d85cb4b76ca2a43f06a49 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/bin/bash
# (C) 2006, 2007, 2008 Gluster Inc. <http://www.gluster.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

_init ()
{
    # log level definitions
    LOG_NONE=NONE;
    LOG_CRITICAL=CRITICAL;
    LOG_ERROR=ERROR;
    LOG_WARNING=WARNING;
    LOG_INFO=INFO
    LOG_DEBUG=DEBUG;
    LOG_TRACE=TRACE;

    prefix="@prefix@";
    exec_prefix=@exec_prefix@;
    cmd_line=$(echo "@sbindir@/glusterfs");

    UPDATEDBCONF=/etc/updatedb.conf
}

# Mount happens asynchronously, so the command status alone will never be
# sufficient.  Instead, we have to wait for multiple events representing
# different possible outcomes.
wait_for ()
{
    local daemon_pid=$1
    local mount_point=$2

    waited=0
    while true; do
        kill -s 0 $daemon_pid
        if [ $? != 0 ]; then
                echo "Gluster client daemon exited unexpectedly."
                umount $mount_point &> /dev/null
                exit 1
        fi
        inode=$(stat -c %i $mount_point 2>/dev/null);
        if [ "$inode" = "1" ]; then
            break
        fi
        if [ $waited -ge 10 ]; then
            break
        fi
        sleep 1
        waited=$((waited+1))
    done

    echo "$inode"
}

start_glusterfs ()
{
    if [ -n "$log_level_str" ]; then
	case "$log_level_str" in
	    "ERROR")
		log_level=$LOG_ERROR;
		;;
            "INFO")
                log_level=$LOG_INFO
                ;;
	    "DEBUG")
		log_level=$LOG_DEBUG;
		;;
	    "CRITICAL")
		log_level=$LOG_CRITICAL;
		;;
	    "WARNING")
		log_level=$LOG_WARNING;
		;;
	    "TRACE")
		log_level=$LOG_TRACE;
		;;
	    "NONE")
		log_level=$LOG_NONE;
		;;
	    *)
		echo "invalid log level $log_level_str, using INFO";
		log_level=$LOG_INFO;
		;;
	esac
    fi
    if [ -n "$log_level" ]; then
        cmd_line=$(echo "$cmd_line --log-level=$log_level");
    fi

    if [ -n "$read_only" ]; then
	cmd_line=$(echo "$cmd_line --read-only");
    fi

    if [ -n "$acl" ]; then
	cmd_line=$(echo "$cmd_line --acl");
    fi

    if [ -n "$worm" ]; then
        cmd_line=$(echo "$cmd_line --worm");
    fi

    if [ -n "$log_file" ]; then
	cmd_line=$(echo "$cmd_line --log-file=$log_file");
    fi

    if [ -n "$volfile_check" ]; then
	cmd_line=$(echo "$cmd_line --volfile-check");
    fi

    if [ -n "$direct_io_mode" ]; then
	cmd_line=$(echo "$cmd_line --direct-io-mode=$direct_io_mode");
    fi

    if [ -n "$volume_name" ]; then
        cmd_line=$(echo "$cmd_line --volume-name=$volume_name");
    fi

    if [ -z "$volfile_loc" ]; then
        if  [ -n "$server_ip" ]; then
            if [ -n "$server_port" ]; then
                cmd_line=$(echo "$cmd_line --volfile-server-port=$server_port");
            fi
	    if [ -n "$transport" ]; then
                cmd_line=$(echo "$cmd_line --volfile-server-transport=$transport");
            fi
            if [ -n "$volume_id" ]; then
                cmd_line=$(echo "$cmd_line --volfile-id=$volume_id");
            fi

            if [ -n "$backupvolfile_server" ]; then
                cmd_line1=$(echo "$cmd_line --volfile-server=$backupvolfile_server");
            fi
            if [ -n "$volfile_max_fetch_attempts" ]; then
                cmd_line=$(echo "$cmd_line --volfile-max-fetch-attempts=$volfile_max_fetch_attempts");
            fi
	    cmd_line=$(echo "$cmd_line --volfile-server=$server_ip");
        fi
    else
        cmd_line=$(echo "$cmd_line --volfile=$volfile_loc");
    fi

    cmd_line=$(echo "$cmd_line $mount_point")
    err=0
    $cmd_line
    daemon_pid=$$

    # Wait for the inode to change *or* for the daemon to exit (indicating a
    # problem with the mount).
    inode=$(wait_for $daemon_pid $mount_point)
    # this is required if the stat returns error
    if [ -z "$inode" ]; then
        inode="0"
    fi

    # retry the failover
    if [ $inode -ne 1 ]; then
        if [ -n "$cmd_line1" ]; then
            cmd_line1=$(echo "$cmd_line1 $mount_point");
            $cmd_line1
            daemon_pid=$$

            inode=$(wait_for $daemon_pid $mount_point)
            # this is required if the stat returns error
            if [ -z "$inode" ]; then
                inode="0";
            fi
            if [ $inode -ne 1 ]; then
                err=1
            fi
        fi
    fi

    if [ $err -eq "1" ]; then
            echo "Mount failed. Please check the log file for more details."
            umount $mount_point > /dev/null 2>&1;
            exit 1;
    fi
}

usage ()
{

echo "Usage:  mount.glusterfs <volumeserver>:<volumeid/volumeport> -o <options> <mountpoint>
Options:
man 8 mount.glusterfs

To display the version number of the mount helper:
mount.glusterfs --version"

}

# check for recursive mounts. i.e, mounting over an existing brick
check_recursive_mount ()
{
    if [ $2 = "/" ]; then
        echo Cannot mount over root;
        exit 2;
    fi
    # GFID check first
    # remove trailing / from mount point
    mnt_dir=${2%/};

    export PATH;
    # check whether getfattr exists
    which getfattr > /dev/null 2>&1;
    if [ $? -ne 0 ]; then
        return;
    fi

    getfattr -n trusted.gfid $mnt_dir 2>/dev/null | grep -iq "trusted.gfid=";
    if [ $? -eq 0 ]; then
        echo "ERROR: $mnt_dir is in use as a brick of a gluster volume";
        exit 2;
    fi

    # check if the mount point is a brick's parent directory
    GLUSTERD_WORKDIR="/etc/glusterd";

    ls -L "$GLUSTERD_WORKDIR"/vols/*/bricks/* > /dev/null 2>&1;
    if [ $? -ne 0 ]; then
        return;
    fi

    brick_path=(`grep ^path "$GLUSTERD_WORKDIR"/vols/*/bricks/* | cut -d "=" -f 2`);
    root_inode=`stat -Lc %i /`;
    root_dev=`stat -Lc %d /`;
    mnt_inode=`stat -Lc %i $mnt_dir`;
    mnt_dev=`stat -Lc %d $mnt_dir`;
    for brick in "$brick_path";
    do
        # evaluate brick path to see if this is local, if non-local, skip iteration
        ls $brick > /dev/null 2>&1;
        if [ $? -ne 0 ]; then
            continue;
        fi
        getfattr -n trusted.gfid "$brick" 2>/dev/null | grep -iq "trusted.gfid=";
        if [ $? -ne 0 ]; then
            continue;
        else
            # brick is local
            while [ 1 ];
            do
                tmp_brick="$brick";
                brick="$brick"/..;
                brick_dev=`stat -Lc %d $brick`;
                brick_inode=`stat -Lc %i $brick`;
                if [ "$mnt_inode" -eq "$brick_inode" -a "$mnt_dev" -eq "$brick_dev" ]; then
                    echo ERROR: $mnt_dir is a parent of the brick $tmp_brick;
                    exit 2;
                fi
                [ "$root_inode" -ne "$brick_inode" -o "$root_dev" -ne "$brick_dev" ] || break;
            done;
        fi
    done;
}

main ()
{
    helper=$(echo "$@" | sed -n 's/.*\--[ ]*\([^ ]*\).*/\1/p');

    in_opt="no"
    pos_args=0
    for opt in "$@"; do
        if [ "$in_opt" = "yes" ]; then
            for pair in $(echo "$opt" | tr "," " "); do
                # Handle options without values.
                case "$pair" in
                    "ro")       read_only=1 ;;
                    "acl")      acl=1 ;;
                    "worm")     worm=1 ;;
                    # "mount -t glusterfs" sends this, but it's useless.
                    "rw")       ;;
                    *)
                        read key value < <(echo "$pair" | tr "=" " ")
                        # Handle options with values.
                        case "$key" in
                            "log-level")        log_level_str=$value ;;
                            "log-file")         log_file=$value ;;
                            "transport")        transport=$value ;;
                            "direct-io-mode")   direct_io_mode=$value ;;
                            "volume-name")      volume_name=$value ;;
                            "volume-id")        volume_id=$value ;;
                            "volfile-check")    volfile_check=$value ;;
                            "server-port")      server_port=$value ;;
                            "fetch-attempts")
                                volfile_max_fetch_attempts=$value ;;
                            "backupvolfile-server")
                                backupvolfile_server=$value ;;
                            *) echo "unknown option $key (ignored)" ;;
                        esac
                esac
            done
            in_opt="no"
        elif [ "$opt" = "-o" ]; then
            in_opt="yes"
        else
            case $pos_args in
                0)      volfile_loc=$opt ;;
                1)      mount_point=$opt ;;
                *)      echo "extra arguments at end (ignored)" ;;
            esac
            pos_args=$((pos_args+1))
        fi
    done
    if [ $in_opt = "yes" -o $pos_args -lt 2 ]; then
        usage
        exit 1
    fi

    [ -r "$volfile_loc" ] || {
        server_ip=$(echo "$volfile_loc" | sed -n 's/\([a-zA-Z0-9:.\-]*\):.*/\1/p');
        test_str=$(echo "$volfile_loc" | sed -n 's/.*:\([^ ]*\).*/\1/p');
        [ -n "$test_str" ] && {
            volume_id="$test_str";
        }
	volfile_loc="";
    }

    #
    [ -n "$helper" ] && {
        cmd_line=$(echo "$cmd_line --$helper");
        exec $cmd_line;
        exit 0;
    }

    # No need to do a ! -d test, it is taken care while initializing the
    # variable mount_point
    [ -z "$mount_point" -o ! -d "$mount_point" ] && {
        echo "ERROR: Mount point does not exist."
        usage;
        exit 0;
    }

    # Simple check to avoid multiple identical mounts
    if grep -q " $mount_point fuse" /etc/mtab; then
        echo -n "$0: according to mtab, GlusterFS is already mounted on "
        echo "$mount_point"
        exit 0;
    fi

    check_recursive_mount "$@";

    # Append fuse.glusterfs to PRUNEFS variable in updatedb.conf(5). updatedb(8)
    # should not index files under GlusterFS, indexing will slow down GlusteFS
    # if the filesystem is several TB in size.
    test -f $UPDATEDBCONF && {
        if ! grep -q 'glusterfs' $UPDATEDBCONF; then
            sed 's/\(PRUNEFS.*\)"/\1 fuse.glusterfs"/' $UPDATEDBCONF \
                > ${UPDATEDBCONF}.bak
            mv -f ${UPDATEDBCONF}.bak $UPDATEDBCONF
        fi
    }

    start_glusterfs;
}

_init "$@" && main "$@";