1
# LUN assignment script
3
# Copyright (C) 2007 Erez Zilber <erezz@voltaire.com>
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
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.
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
26
echo -e "\t$name -n tgt_name -d dev -b bs_name -t transport [initiator_IP1 initiator_IP2 ...]";
28
echo -e "\tbacking store: rdwr";
29
echo -e "\ttransport: iscsi";
30
echo -e "\tinitiator: ALL";
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";
39
if ! [ "$dev" -o "$bs_type" == "null" ]; then
40
echo "Error: a device is mandatory";
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";
52
if ! [ "$tgt_name" ]; then
53
echo "Error: target name is mandatory";
57
if ! [ "$lld_name" ]; then
58
echo "Error: lld name empty";
65
id_list=$(tgtadm --lld $lld_name --op show --mode target | grep Target | cut -d" " -f2 | sed s/://)
69
for id in $id_list; do
70
if (($id > $next_vacant_id)); then
73
next_vacant_id=$((next_vacant_id+1))
77
return $next_vacant_id
85
tmp_file=/tmp/target_list.txt
87
tgtadm --lld $lld_name --op show --mode target > $tmp_file
90
# Check if we finished going over this target
91
if ((tgt_found == 1 && $(echo $line | grep -c "^Target") == 1)); then
95
# Check if we found the requested target
96
if (($(echo $line | grep -c "Target $tid:") == 1)); then
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
106
next_vacant_lun=$((next_vacant_lun+1))
113
if ((tgt_found == 0)); then
114
echo "Error: could not find a LUN for target $tid"
118
return $next_vacant_lun
123
echo "Deleting new target, tid=$tid"
124
tgtadm --lld $lld_name --op delete --mode target --tid $tid
127
if [ $res -ne 0 ]; then
128
echo "Error: failed to delete target, tid=$tid"
134
check_if_tgt_exists()
136
tgt_list=$(tgtadm --lld $lld_name --op show --mode target | grep Target | cut -d" " -f3)
138
for curr_tgt in $tgt_list; do
139
if [ $tgt_name = $curr_tgt ]; then
147
if [ $# -eq 0 ]; then
154
while getopts "d:n:b:t:h:" opt
171
shift $(($OPTIND - 1))
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"
185
echo "Using transport: $lld_name"
187
tgt_name="iqn.2001-04.com.$(hostname -s)-$tgt_name"
189
# Make sure that a target with the same name doesn't exist
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
197
tid=$(tgtadm --lld $lld_name --op show --mode target | grep $tgt_name | cut -d" " -f2)
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
208
if [ $res -ne 0 ]; then
209
echo "Error: failed to create target (name=$tgt_name, tid=$tid)"
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"
223
tgtadm --lld $lld_name --op new --mode logicalunit --tid $tid --lun $lun -b $dev $bs_opt
226
if [ $res -ne 0 ]; then
227
echo "Error: failed to add a logical unit ($dev) to target, tid=$tid"
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
239
if [ $res -ne 0 ]; then
240
echo "Error: could not assign initiator $initiator to the target"
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
249
if [ $res -ne 0 ]; then
250
echo "Error: failed to set access for all initiators"