Statistiques
| Révision :

root / bin / add-iscsi-volume

Historique | Voir | Annoter | Télécharger (10,12 ko)

1
#!/bin/bash
2
# $Id: add-iscsi-volume 51 2013-10-10 11:20:37Z ltaulell $
3
# Copyright (C) 2013 Kevin Reverchon, Loïs Taulelle
4
# This file/program is part of gZFS free software
5
# See COPYING file for details
6
#
7

    
8
function restrictedIP {
9

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

    
15
}
16

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

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

    
29
cp /etc/iet/ietd.conf /etc/iet/ietd.conf.$TODAY
30

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

    
39
if [ ! -z $restricted ]
40
then
41
   restrictedIP
42
fi
43

    
44
}
45

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

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

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

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

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

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

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

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

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

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

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

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

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

    
235
                    cp /etc/iet/ietd.conf /etc/iet/ietd.conf.$TODAY
236

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

    
257
                    cp /etc/iet/ietd.conf /etc/iet/ietd.conf.$TODAY
258

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

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

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

    
291
function usage {
292

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

    
308
}
309

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

    
354
nointeractive
355