mount_fileshare.sh 2.63 KB
Newer Older
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
#!/bin/bash

set -e 

#### FUNCTIONS
usage()
{
    echo ""
    echo "USAGE:"
    echo "$0 [OPTIONS] -s STORAGEACCTNAME -f FILESHARENAME"
    echo ""
    echo "OPTIONS"
    echo "   -s STORAGEACCTNAME"
    echo "          Storage account name [REQUIRED]."
    echo "   -f FILESHARENAME"
    echo "          Name of fileshare to mount [REQUIRED]."
    echo "   -m MOUNTPOINT"
    echo "          Directory where file share is to be mounted."
    echo "   -p"
    echo "          Persist mount adding it to /etc/fstab [NOT IMPLEMENTED]."
    echo "   -c"
    echo "          Samba credentials file (defaults to /etc/smbcredentials/STORAGEACCTNAME.cred)."
    echo "   -u INTEGER"
    echo "          UID for user that will own mount (default: 1000)."
    echo "   -g INTEGER"
    echo "          GID for group that will own mount (default: 1000)."
    echo "   -h"
    echo "          Print (this) help message."
    echo ""
}

###### PARSE CMDLINE

STORAGEACCT=
SHARENAME=
MOUNTPOINT=
PERSIST=0
SMBCRED=
USERID=1000
GROUPID=1000

while getopts ":s:f:m:pc:u:g:h" opt; do
    case ${opt} in 
        s)
            STORAGEACCT=$OPTARG
            ;;
        f)
            SHARENAME=$OPTARG
            ;;
        m)
            MOUNTPOINT=$OPTARG
            ;;
        p)
            PERSIST=1
            ;;
        c)
            SMBCRED=$OPTARG
            ;;
        u)
            USERID=$OPTARG
            ;;
        g)
            GROUPID=$OPTARG
            ;;
        h)
            usage
            exit 0
            ;;
        \?)
            echo "[ERROR] Invalid option: -$OPTARG" >&2
            echo "        (try $0 -h for help)"
            exit 1
            ;;
        :)
            echo "[ERROR] Option -$OPTARG requires an argument." >&2
            echo "        (try $0 -h for help)"
            exit 1
            ;;
    esac
done
shift $((OPTIND -1))

Gonzalo S Nido's avatar
Gonzalo S Nido committed
83
if [[ ${PERSIST} == 1 ]]; then
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
    echo "Option -p is still not implemented, sorry."
fi

if [[ $STORAGEACCT == "" ]]; then
    echo "[ERROR] Storage account name (-s option) is required."
    exit 1
fi
if [[ $SHARENAME == "" ]]; then
    echo "[ERROR] File share name (-f option) is required."
    exit 1
fi

if [[ $SMBCRED == "" ]]; then
    SMBCRED=/etc/smbcredentials/${STORAGEACCT}.cred
fi

if [[ $MOUNTPOINT == "" ]]; then
    MOUNTPOINT=/fileshares/${STORAGEACCT}/${SHARENAME}
fi

sudo mkdir -p ${MOUNTPOINT}

sudo mount -t cifs //${STORAGEACCT}.file.core.windows.net/${SHARENAME} \
    ${MOUNTPOINT} \
    -o vers=3.0,credentials=${SMBCRED},dir_mode=0755,file_mode=0644,serverino,gid=${GROUPID},uid=${USERID},forceuid,forcegid

if [[ $? == 0 ]]; then
    echo "File share mounted in ${MOUNTPOINT}"
exit 0