~ubuntu-branches/ubuntu/precise/tgt/precise

« back to all changes in this revision

Viewing changes to .pc/make-tgt-setup-lun-executable/scripts/tgt-setup-lun

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-02-08 10:31:04 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110208103104-oots1az6acnkfvuw
Tags: 1:1.0.13-0ubuntu1
* New upstream release.
* debian/patches/make-tgt-setup-lun-executable: Dropped no longer needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# LUN assignment script
2
 
#
3
 
# Copyright (C) 2007 Erez Zilber <erezz@voltaire.com>
4
 
#
5
 
# This program is free software; you can redistribute it and/or
6
 
# modify it under the terms of the GNU General Public License as
7
 
# published by the Free Software Foundation, version 2 of the
8
 
# License.
9
 
#
10
 
# This program is distributed in the hope that it will be useful, but
11
 
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 
# General Public License for more details.
14
 
#
15
 
# You should have received a copy of the GNU General Public License
16
 
# along with this program; if not, write to the Free Software
17
 
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18
 
# 02110-1301 USA
19
 
 
20
 
#!/bin/bash
21
 
 
22
 
usage()
23
 
{
24
 
        name=$(basename $0);
25
 
        echo "usage:";
26
 
        echo -e "\t$name -n tgt_name -d dev -b bs_name -t transport [initiator_IP1 initiator_IP2 ...]";
27
 
        echo "defaults:";
28
 
        echo -e "\tbacking store: rdwr";
29
 
        echo -e "\ttransport: iscsi";
30
 
        echo -e "\tinitiator: ALL";
31
 
        echo "examples:";
32
 
        echo -e "\t$name -n tgt-1 -d /dev/sdb1 192.168.1.2";
33
 
        echo -e "\t$name -n tgt-2 -d /tmp/null -b null -t iser";
34
 
        echo -e "\t$name -n tgt-3 -d ~/disk3.bin -b rdwr 192.168.1.2 192.168.1.3";
35
 
}
36
 
 
37
 
verify_params()
38
 
{
39
 
        if ! [ "$dev" -o "$bs_type" == "null" ]; then
40
 
                echo "Error: a device is mandatory";
41
 
                exit 1;
42
 
        else
43
 
                return;
44
 
        fi
45
 
 
46
 
        # Make sure that the device exists
47
 
        if ! [ -b $dev -o -f $dev -o -c $dev ]; then
48
 
                echo "Error: $dev is not a device";
49
 
                exit 1;
50
 
        fi
51
 
 
52
 
        if ! [ "$tgt_name" ]; then
53
 
                echo "Error: target name is mandatory";
54
 
                exit 1;
55
 
        fi
56
 
 
57
 
        if ! [ "$lld_name" ]; then
58
 
                echo "Error: lld name empty";
59
 
                exit 1;
60
 
        fi
61
 
}
62
 
 
63
 
find_vacant_tgt_id()
64
 
{
65
 
        id_list=$(tgtadm --lld $lld_name --op show --mode target | grep Target | cut -d" " -f2 | sed s/://)
66
 
 
67
 
        next_vacant_id=1
68
 
 
69
 
        for id in $id_list; do
70
 
                if (($id > $next_vacant_id)); then
71
 
                        break;
72
 
                else
73
 
                        next_vacant_id=$((next_vacant_id+1))
74
 
                fi
75
 
        done
76
 
 
77
 
        return $next_vacant_id
78
 
}
79
 
 
80
 
find_vacant_lun()
81
 
{
82
 
        tid=$1
83
 
        tgt_found=0
84
 
        next_vacant_lun=0
85
 
        tmp_file=/tmp/target_list.txt
86
 
 
87
 
        tgtadm --lld $lld_name --op show --mode target > $tmp_file
88
 
 
89
 
        while read line; do
90
 
                # Check if we finished going over this target
91
 
                if ((tgt_found == 1 && $(echo $line | grep -c "^Target") == 1)); then
92
 
                        break
93
 
                fi
94
 
 
95
 
                # Check if we found the requested target
96
 
                if (($(echo $line | grep -c "Target $tid:") == 1)); then
97
 
                        tgt_found=1
98
 
                        continue
99
 
                fi
100
 
 
101
 
                if ((tgt_found == 1 && $(echo $line | grep -c "LUN:") == 1)); then
102
 
                        curr_lun=$(echo $line | cut -d" " -f2)
103
 
                        if (($curr_lun > $next_vacant_lun)); then
104
 
                                break
105
 
                        else
106
 
                                next_vacant_lun=$((next_vacant_lun+1))
107
 
                        fi
108
 
                fi
109
 
        done < $tmp_file
110
 
 
111
 
        rm -f $tmp_file
112
 
 
113
 
        if ((tgt_found == 0)); then
114
 
                echo "Error: could not find a LUN for target $tid"
115
 
                return -1
116
 
        fi
117
 
 
118
 
        return $next_vacant_lun
119
 
}
120
 
 
121
 
err_exit()
122
 
{
123
 
        echo "Deleting new target, tid=$tid"
124
 
        tgtadm --lld $lld_name --op delete --mode target --tid $tid
125
 
        res=$?
126
 
 
127
 
        if [ $res -ne 0 ]; then
128
 
                echo "Error: failed to delete target, tid=$tid"
129
 
        fi
130
 
 
131
 
        exit 1
132
 
}
133
 
 
134
 
check_if_tgt_exists()
135
 
{
136
 
        tgt_list=$(tgtadm --lld $lld_name --op show --mode target | grep Target | cut -d" " -f3)
137
 
 
138
 
        for curr_tgt in $tgt_list; do
139
 
                if [ $tgt_name = $curr_tgt ]; then
140
 
                        return 1
141
 
                fi
142
 
        done
143
 
 
144
 
        return 0
145
 
}
146
 
 
147
 
if [ $# -eq 0 ]; then
148
 
        usage
149
 
        exit 1
150
 
fi
151
 
 
152
 
lld_name="iscsi"
153
 
 
154
 
while getopts "d:n:b:t:h:" opt
155
 
do
156
 
        case ${opt} in
157
 
        d)
158
 
                dev=$OPTARG;;
159
 
        n)
160
 
                tgt_name=$OPTARG;;
161
 
        b)
162
 
                bs_type=$OPTARG;;
163
 
        t)
164
 
                lld_name=$OPTARG;;
165
 
        h*)
166
 
                usage
167
 
                exit 1
168
 
        esac
169
 
done
170
 
 
171
 
shift $(($OPTIND - 1))
172
 
 
173
 
initiators=$*
174
 
 
175
 
verify_params
176
 
 
177
 
# Check if tgtd is running (we should have 2 daemons)
178
 
tgtd_count=`pidof tgtd | wc -w`
179
 
if [ $tgtd_count -lt 1 ]; then
180
 
        echo "tgtd is not running"
181
 
        echo "Exiting..."
182
 
        exit 1
183
 
fi
184
 
 
185
 
echo "Using transport: $lld_name"
186
 
 
187
 
tgt_name="iqn.2001-04.com.$(hostname -s)-$tgt_name"
188
 
 
189
 
# Make sure that a target with the same name doesn't exist
190
 
check_if_tgt_exists
191
 
if [ $? -eq 1 ]; then
192
 
        echo "Error: target named $tgt_name already exists"
193
 
        read -p "Add a new lun to the existing target? (yes/NO): " add_lun
194
 
        if [ $add_lun != "yes" ]; then
195
 
                exit 1
196
 
        fi
197
 
        tid=$(tgtadm --lld $lld_name --op show --mode target | grep $tgt_name | cut -d" " -f2)
198
 
        tid=${tid%:}
199
 
else
200
 
        find_vacant_tgt_id
201
 
        tid=$?
202
 
 
203
 
        # Create the new target
204
 
        echo "Creating new target (name=$tgt_name, tid=$tid)"
205
 
        tgtadm --lld $lld_name --op new --mode target --tid $tid -T $tgt_name
206
 
        res=$?
207
 
 
208
 
        if [ $res -ne 0 ]; then
209
 
                echo "Error: failed to create target (name=$tgt_name, tid=$tid)"
210
 
                exit 1
211
 
        fi
212
 
fi
213
 
 
214
 
find_vacant_lun $tid
215
 
lun=$?
216
 
 
217
 
# Add a logical unit to the target
218
 
echo "Adding a logical unit ($dev) to target, tid=$tid"
219
 
if [ $bs_type ]; then
220
 
        echo "Setting backing store type: $bs_type"
221
 
        bs_opt="-E $bs_type"
222
 
fi
223
 
tgtadm --lld $lld_name --op new --mode logicalunit --tid $tid --lun $lun -b $dev $bs_opt
224
 
res=$?
225
 
 
226
 
if [ $res -ne 0 ]; then
227
 
        echo "Error: failed to add a logical unit ($dev) to target, tid=$tid"
228
 
        err_exit
229
 
fi
230
 
 
231
 
# Define which initiators can use this target
232
 
if test "$initiators" ; then
233
 
        # Allow access only for specific initiators
234
 
        echo "Accepting connections only from $initiators"
235
 
        for initiator in $initiators; do
236
 
                tgtadm --lld $lld_name --op bind --mode target --tid $tid -I $initiator
237
 
                res=$?
238
 
 
239
 
                if [ $res -ne 0 ]; then
240
 
                        echo "Error: could not assign initiator $initiator to the target"
241
 
                fi
242
 
        done
243
 
else
244
 
        # Allow access for everyone
245
 
        echo "Accepting connections from all initiators"
246
 
        tgtadm --lld $lld_name --op bind --mode target --tid $tid -I ALL
247
 
        res=$?
248
 
 
249
 
        if [ $res -ne 0 ]; then
250
 
                echo "Error: failed to set access for all initiators"
251
 
                err_exit
252
 
        fi
253
 
fi