forked from hreinecke/tcm-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_fcoe_single.sh
executable file
·182 lines (161 loc) · 4.8 KB
/
setup_fcoe_single.sh
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
83
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/bash
##check if the user has root access
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
##check if the FCoE module is loaded, otherwise terminate
if ! lsmod | grep "fcoe" &> /dev/null; then
echo "FCoE module not found, please load the FCoE module first using \"modprobe fcoe"\"
exit 1
fi
gen_sas_address() {
prefix="naa.6001405"
uuid=$(uuidgen | sed 's/-//g' | tail --bytes 10)
echo "${prefix}${uuid}"
}
gen_fc_port_name() {
port_num=$1
for i in $(seq 3 2 15) ; do
j=$(expr $i + 1)
p=$(echo $port_num | cut --bytes=$i-$j)
printf "%02x:" $(( 16#$p ))
done
p=$(echo $port_num | cut --bytes=17-18)
printf "%02x\n" $(( 16#$p ))
}
test_and_set_value() {
attr=$1
new_val=$2
[ -f ${attr} ] || return 0
read orig_val < ${attr}
if [ -z "$orig_val" ] || [ ${orig_val} -ne ${new_val} ] ; then
echo ${new_val} > ${attr}
if [ $? -ne 0 ] ; then
echo "Failed to set ${attr}"
exit 1
fi
fi
}
imgdir=/home/kvm
img0=sles-mpath-0.img
wwid0=2969579b-c1a2-4181-a963-fe7c57bed635
size=2048
tcm_fc=/sys/kernel/config/target/fc
core=/sys/kernel/config/target/core
prim_tpg="default_tg_pt_gp"
sec_tpg="secondary_tg_pt_gp"
target_0=20:00:0c:fd:37:d4:44:7a
target_1=20:00:0c:fd:37:d4:44:7b
initiator_0=20:00:0c:fd:37:04:1a:1e
initiator_1=20:00:0c:fd:37:04:1a:1f
if [ ! -d /sys/kernel/config ] ; then
modprobe target_core_mod
modprobe tcm_fc
fi
#
# configure fileio
#
imgnum=0
for img in $img0 ; do
if [ ! -f ${imgdir}/${img} ] ; then
[ -d ${imgdir} ] || mkdir ${imgdir}
dd if=/dev/zero of=${imgdir}/${img} bs=1M count=${size} conv=sparse
fi
if [ ! -f ${imgdir}/${img} ] ; then
echo "Image file ${imgdir}/${img} not found"
exit 1
fi
bs=fileio_${imgnum}
disk=fd_${imgnum}
hba=${bs}/${disk}
imgnum=$(expr $imgnum + 1)
imgsize=$(stat -c "%s" ${imgdir}/${img})
if [ ! -d ${core}/${hba} ] ; then
[ -d ${core}/${hba} ] || mkdir -p ${core}/${hba}
echo "Create ${hba} image ${img}"
echo "fd_dev_name=${imgdir}/${img},fd_dev_size=${imgsize}" > ${core}/${hba}/control
if [ "$img" = "$img0" ] ; then
wwid=${wwid0}
fi
echo ${wwid} > ${core}/${hba}/wwn/vpd_unit_serial
echo 1 > ${core}/${hba}/enable
fi
#
# configure ALUA
#
if [ ! -d ${core}/${hba}/alua/${prim_tpg} ] ; then
echo "Target not configured"
exit 1
fi
# test_and_set_value ${core}/${hba}/enable 1
test_and_set_value ${core}/${hba}/alua/${prim_tpg}/tg_pt_gp_id 0
test_and_set_value ${core}/${hba}/alua/${prim_tpg}/alua_access_state 0
echo 1 > ${core}/${hba}/alua/${prim_tpg}/alua_access_type
echo 0 > ${core}/${hba}/alua/${prim_tpg}/alua_support_offline
echo 0 > ${core}/${hba}/alua/${prim_tpg}/alua_support_unavailable
echo 0 > ${core}/${hba}/alua/${prim_tpg}/alua_support_lba_dependent
test_and_set_value ${core}/${hba}/alua/${prim_tpg}/implicit_trans_secs 30
if [ ! -d ${core}/${hba}/alua/${sec_tpg} ] ; then
mkdir ${core}/${hba}/alua/${sec_tpg}
if [ $? -ne 0 ] ; then
echo "Failed to create ${core}/${hba}/alua/${sec_tpg}"
exit 1
fi
fi
test_and_set_value ${core}/${hba}/alua/${sec_tpg}/tg_pt_gp_id 16
test_and_set_value ${core}/${hba}/alua/${sec_tpg}/alua_access_state 1
echo 1 > ${core}/${hba}/alua/${sec_tpg}/alua_access_type
echo 0 > ${core}/${hba}/alua/${sec_tpg}/alua_support_offline
echo 0 > ${core}/${hba}/alua/${sec_tpg}/alua_support_unavailable
echo 0 > ${core}/${hba}/alua/${sec_tpg}/alua_support_lba_dependent
test_and_set_value ${core}/${hba}/alua/${sec_tpg}/implicit_trans_secs 30
done
#
# configure fcoe
#
[ -d ${tcm_fc} ] || mkdir ${tcm_fc}
#
# Map LUNs
#
for t in /sys/bus/fcoe/devices/ctlr_*/host* ; do
[ -d ${t} ] || continue
host=${t##*/}
[ -d ${t}/fc_host/${host} ] || continue
port_name=$(cat ${t}/fc_host/${host}/port_name)
fc_port_name=$(gen_fc_port_name $port_name)
[ -d ${tcm_fc}/${fc_port_name} ] || mkdir ${tcm_fc}/${fc_port_name}
t=${tcm_fc}/${fc_port_name}/tpgt_1
[ -d ${t} ] || mkdir ${t}
num_lun=0
for l in ${core}/fileio_*/fd_* ; do
[ -d ${l} ] || continue
[ -d ${t}/lun/lun_${num_num} ] || mkdir ${t}/lun/lun_${num_lun}
ln -s ${l} ${t}/lun/lun_${num_lun}/mapped_lun 2> /dev/null
if [ ${fc_port_name} == ${target_1} ] ; then
echo ${sec_tpg} > ${t}/lun/lun_${num_lun}/alua_tg_pt_gp
fi
(( num_lun++ ))
done
#
# Map initiator ACLs
#
if [ ${fc_port_name} == ${target_0} ] ; then
fc_initiator_name=${initiator_0}
else
fc_initiator_name=${initiator_1}
fi
acl=${t}/acls/${fc_initiator_name}
if [ ! -d ${acl} ] ; then
mkdir ${acl}
fi
for l in ${t}/lun/* ; do
[ -d ${l} ] || continue
lun_acl=${acl}/${l##*/}
[ -d ${lun_acl} ] || mkdir ${lun_acl}
if [ ! -L ${lun_acl}/mapped_lun ] ; then
echo "Map ${l##*/}"
ln -s ${l} ${lun_acl}/mapped_lun
fi
done
done