Statistiques
| Révision :

root / bin / add-iscsi-volume @ 40

Historique | Voir | Annoter | Télécharger (9,99 ko)

1
#!/bin/bash
2
# $Id: add-iscsi-volume 40 2013-10-08 14:37:55Z ltaulell $
3

    
4

    
5
function restrictedIP {
6

    
7
for IP in $(echo $restricted|sed -e "s/;/ /g")
8
do
9
    echo "iqn.$TODAY:$(hostname)-ens-zfs-iscsi-$target $IP" >>/etc/iet/initiators.allow
10
done
11

    
12
}
13

    
14
function nointeractive {
15
CHECK_TID=1
16
while (/bin/grep "tid:$CHECK_TID" /proc/net/iet/volume)
17
do
18
   CHECK_TID=$(($CHECK_TID + 1))
19
done 
20

    
21
TODAY=$(date +%Y%m%d%H%M%S)
22
ietadm --op new --tid=$CHECK_TID --params Name=iqn.$TODAY:$(hostname)-ens-zfs-iscsi-$target
23
ietadm --op new --tid=$CHECK_TID --lun=$lun --params Path=$devPath,Type=$exportType,IOMode=$iomode
24
ietadm --op new --tid=$CHECK_TID --user --params=IncomingUser=$login,Password=$passwd
25

    
26
cp /etc/iet/ietd.conf /etc/iet/ietd.conf.$TODAY
27

    
28
echo "" >>/etc/iet/ietd.conf
29
echo "Target iqn.$TODAY:$(hostname)-ens-zfs-iscsi-$target" >> /etc/iet/ietd.conf
30
echo "Lun $lun Path=$devPath,Type=$exportType,IOMode=$iomode" >> /etc/iet/ietd.conf
31
echo "IncomingUser $login $passwd" >>/etc/iet/ietd.conf
32
echo "Alias $target" >> /etc/iet/ietd.conf
33
echo "" >> /etc/iet/ietd.conf
34
echo "" >> /etc/iet/ietd.conf
35

    
36
if [ ! -z $restricted ]
37
then
38
   restrictedIP
39
fi
40

    
41
}
42

    
43
function interactive {
44
CHECK_TID=1
45
while (/bin/grep "tid:$CHECK_TID" /proc/net/iet/volume)
46
do
47
   CHECK_TID=$(($CHECK_TID + 1))
48
done 
49
echo "*** iscsi volume name: ***" 
50
while read inputline
51
do
52
   NAME_ISCSI=$inputline 
53
   echo "*** Are you sure: $inputline ? (y/n) ***"
54
   
55
   while read inputConfirm
56
   do
57
      if [ "$inputConfirm" = "y" ]||[ "$inputConfirm" = "n" ]
58
      then
59
         break
60
      fi
61
      echo "*** Are you sure: $inputline ? (y/n) ***" 
62
   done
63

    
64
   if [ "$inputConfirm" = "y" ]
65
   then
66
      break
67
   fi
68
   if [ "$inputConfirm" != "y" ]
69
   then
70
      echo "*** iscsi volume name: ***"
71
   fi
72
done
73

    
74
echo ""
75
echo "*** Step I: Initialization of TID and iscsi volume name ***"
76
TODAY=$(date +%Y%m%d%H%M%S)
77
ietadm --op new --tid=$CHECK_TID --params Name=iqn.$TODAY:$(hostname)-ens-zfs-iscsi-$NAME_ISCSI
78
if [ $? -eq 0 ]
79
then
80
   echo "*** Path of zfs device (zvol) to export: ***"
81
   
82

    
83
   while read -e inputline
84
   do
85
      PATH_ZVOL=$inputline
86
      echo "*** Are you sure: $inputline ? (y/n) ***"
87

    
88
      while read inputConfirm
89
      do
90
         if [ "$inputConfirm" = "y" ]||[ "$inputConfirm" = "n" ]
91
         then
92
            break
93
         fi
94
         echo "*** Are you sure: $inputline ? (y/n) ***"
95
      done
96

    
97
      if [ "$inputConfirm" = "y" ]
98
      then
99
         echo "*** Check if zvol path exists... ***"
100
         ls -l $PATH_ZVOL
101
         if [ $? -eq 0 ]
102
         then
103
            echo "*** The path is valid... "
104
            break
105
         else
106
            echo "*** Path doesn't exist... "
107
         fi
108
      fi
109
      if [ "$inputConfirm" != "y" ]
110
      then
111
         echo "*** Path of zfs device (zvol) to export: ***"
112
      fi
113
   done
114
  
115
   echo "" 
116
   echo "*** Step II: initialization of the PATH and iscsi volume LUN ***"
117
   if [ $? -eq 0 ]
118
   then
119
     echo "*** ISCSI export Type (fileio/blockio): ***"
120
     
121
     while read inputline
122
     do
123
        TYPE_EXPORT=$inputline
124
        echo "*** Are you sure: $inputline ? (y/n) ***"
125

    
126
        while read inputConfirm
127
        do
128
           if [ "$inputConfirm" = "y" ]||[ "$inputConfirm" = "n" ]
129
           then
130
              break
131
           fi
132
           echo "*** Are you sure: $inputline ? (y/n) ***"
133
        done
134

    
135
        if [ "$inputConfirm" = "y" ]
136
        then
137
           echo "*** Check if the parameter is valid... ***"
138
           if [ "$TYPE_EXPORT" = "fileio" ]||[ "$TYPE_EXPORT" = "blockio" ]
139
           then
140
              break
141
           else
142
              echo "*** ISCSI export Type (fileio/blockio): ***"
143
           fi
144
        fi
145
        if [ "$inputConfirm" != "y" ]
146
        then
147
           echo "*** ISCSI export Type (fileio/blockio): ***"
148
        fi
149
     done
150

    
151
   echo "" 
152
   echo "*** Step II.B: initialization iscsi IOMode ***"
153
     echo "*** ISCSI IOMode (ro/wt): ***"
154
     
155
     while read inputline
156
     do
157
        iomode=$inputline
158
        echo "*** Are you sure: $inputline ? (y/n) ***"
159

    
160
        while read inputConfirm
161
        do
162
           if [ "$inputConfirm" = "y" ]||[ "$inputConfirm" = "n" ]
163
           then
164
              break
165
           fi
166
           echo "*** Are you sure: $inputline ? (y/n) ***"
167
        done
168

    
169
        if [ "$inputConfirm" = "y" ]
170
        then
171
           echo "*** Check if the parameter is valid... ***"
172
           if [ "$iomode" = "ro" ]||[ "$iomode" = "wt" ]
173
           then
174
              break
175
           else
176
              echo "*** ISCSI IOMode (ro/wt): ***"
177
           fi
178
        fi
179
        if [ "$inputConfirm" != "y" ]
180
        then
181
           echo "*** ISCSI IOMode (ro/wt): ***"
182
        fi
183
     done
184
     
185
     echo ""
186
     echo "*** Step III: initialization of export Type and iscsi IOMode ***"
187
     ietadm --op new --tid=$CHECK_TID --lun=0 --params Path=$PATH_ZVOL,Type=$TYPE_EXPORT,IOMode=$iomode
188
     if [ $? -eq 0 ]
189
     then
190

    
191
       echo ""
192
       echo "*** Step IV: Login and password ***"
193
       echo "*** Do you enable a restriction login/password: (y/n)"
194
       while read inputline
195
       do
196
           if [ "$inputline" != "y" ] && [ "$inputline" != "n" ]
197
           then
198
              echo "*** Do you enable a restriction login/password: (y/n)"
199
           else
200
             if [ "$inputline" = "y" ]
201
             then
202
                 echo "*** Give a login:"
203
                 while read inputlogin
204
                 do
205
                   if [ "$inputlogin" != "" ]
206
                   then
207
                      break
208
                   else
209
                      echo "*** Give a login:"
210
                   fi
211
                  done
212

    
213
                  echo "*** Give a password:"
214
                  while read inputpass
215
                  do
216
                     if [ "$inputpass" = "" ]
217
                     then
218
                        echo "*** Give a password:"
219
                     else
220
                        break
221
                     fi
222
                  done
223
                  ietadm --op new --tid=$CHECK_TID --user --params=IncomingUser=$inputlogin,Password=$inputpass
224
                  if [ $? -eq 0 ]
225
                  then
226
                    echo "*** Authentication Configuration [OK]"
227
       
228
                    echo ""
229
                    echo "*** Step IV: Writting in file ietd.conf ***"
230
                    echo "*** Backup of /etc/iet/ietd.conf... "
231

    
232
                    cp /etc/iet/ietd.conf /etc/iet/ietd.conf.$TODAY
233

    
234
                    echo "" >>/etc/iet/ietd.conf
235
                    echo "Target iqn.$TODAY:$(hostname)-ens-zfs-iscsi-$NAME_ISCSI" >> /etc/iet/ietd.conf
236
                    echo "Lun 0 Path=$PATH_ZVOL,Type=$TYPE_EXPORT,IOMode=$iomode" >> /etc/iet/ietd.conf
237
                    echo "IncomingUser $inputlogin $inputpass" >>/etc/iet/ietd.conf
238
                    echo "Alias $NAME_ISCSI" >> /etc/iet/ietd.conf
239
                    echo "" >> /etc/iet/ietd.conf
240
                    echo "" >> /etc/iet/ietd.conf       
241
    
242
                    echo ""
243
                    echo "*** Add of ISCSI $NAME_ISCSI Path:$PATH_ZVOL [OK]"
244
                    echo "*** FIN ***"
245
                 else
246
                    echo "/!\\ Authentication Configuration [ERROR] /!\\"
247
                 fi
248
                 break
249
             else
250
                    echo ""
251
                    echo "*** Step IV: Writting in file ietd.conf ***"
252
                    echo "*** Backup of /etc/iet/ietd.conf... "
253

    
254
                    cp /etc/iet/ietd.conf /etc/iet/ietd.conf.$TODAY
255

    
256
                    echo "" >>/etc/iet/ietd.conf
257
                    echo "Target iqn.$TODAY:$(hostname)-ens-zfs-iscsi-$NAME_ISCSI" >> /etc/iet/ietd.conf
258
                    echo "Lun 0 Path=$PATH_ZVOL,Type=$TYPE_EXPORT,IOMode=$iomode" >> /etc/iet/ietd.conf
259
                    echo "IncomingUser $inputlogin $inputpass" >>/etc/iet/ietd.conf
260
                    echo "Alias $NAME_ISCSI" >> /etc/iet/ietd.conf
261
                    echo "" >> /etc/iet/ietd.conf
262
                    echo "" >> /etc/iet/ietd.conf       
263
    
264
                    echo ""
265
                    echo "*** Add of ISCSI $NAME_ISCSI Path:$PATH_ZVOL [OK]"
266
                    echo "*** END ***"
267
                    break
268
             fi
269
           fi
270
       done
271

    
272
     else
273
       echo "*** Problem during step III (initialization of iscsi export Type). Canceled process... ***"
274
       ietadm --op delete --tid=$CHECK_TID
275
     fi
276
     
277
   else
278
      echo "*** Problem during step II (initialization of PATH and iscsi volume LUN). Canceled process... ***"
279
      ietadm --op delete --tid=$CHECK_TID
280
   fi
281

    
282
else
283
   echo "*** Problem during step I (initialization of TID and iscsi volume name). Canceled process... ***"
284
   ietadm --op delete --tid=$CHECK_TID
285
fi
286
}
287

    
288
function usage {
289

    
290
echo "syntax: " 1>&2
291
echo "add-iscsi-volume [OPTIONS]" 1>&2
292
echo "" 1>&2
293
echo "OPTIONS :" 1>&2
294
echo "-h : display this message" 1>&2
295
echo "-d : path device to iscsi export" 1>&2
296
echo "-T : export type (Only fileio or blockio)" 1>&2
297
echo "-L : lun (Only numeric values)" 1>&2
298
echo "-o : IO Mode : ro (Read Only) or wt (Read Write)" 1>&2
299
echo "-l : login if you want a CHAP authentication" 1>&2
300
echo "-p : password if you want a CHAP authentication" 1>&2
301
echo "-R : Specify a list of restricted IP (Separated by a ;)" 1>&2
302
echo "-t : name of zfs volume (This field is used to generate alias name iscsi)" 1>&2
303
echo "-i : interactive mode (This option is not compatible with others)" 1>&2
304

    
305
}
306

    
307
while getopts "id:T:L:o:l:p:R:t:h" OPTION
308
do
309
     case $OPTION in
310
         h)
311
             usage
312
             exit 1
313
             ;;
314
         d)
315
            devPath=$OPTARG
316
            ;;
317
         T)
318
            exportType=$OPTARG
319
            ;;
320
         L)
321
            lun=$OPTARG
322
            ;;
323
         o)
324
            iomode=$OPTARG
325
            ;;
326
         l)
327
            login=$OPTARG
328
            ;;
329
         p)
330
            passwd=$OPTARG
331
            ;;
332
         R)
333
            restricted=$OPTARG
334
            ;;
335
         t)
336
            target=$OPTARG
337
            target=$(echo ${target//\//-})
338
            ;;
339
         i)
340
            interactive
341
            exit 0
342
            ;;
343
     esac
344
done
345
if [[ -z $devPath ]] || [[ -z $exportType ]] || [[ -z $lun ]] || [[ -z $iomode ]] || [[ -z $target ]]
346
then
347
     usage
348
     exit 1
349
fi
350

    
351
nointeractive
352