forked from hreinecke/tcm-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcm_restore.sh
345 lines (315 loc) · 7.01 KB
/
tcm_restore.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/bin/bash
##check if the user has root access
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
core=/sys/kernel/config/target/core
fc=/sys/kernel/config/target/fc
JSON=$1
apply_attributes () {
local attrs=$1
local jsonobj=$2
for a in ${attrs}/*; do
attr=${a##*/}
value=$(cat $a)
v=$(cat $JSON | jq -c "${jsonobj}.${attr}")
case ${attr} in
alua_access_type)
if [ "$value" == "Implicit and Explicit" ] ; then
value=3
elif [ "$value" == "Explicit" ] ; then
value=2
elif [ "$value" == "Implicit" ] ; then
value=1
fi
;;
alua_access_status)
value=$v
;;
esac
if [ "$v" != "null" ] && [ "$value" != "$v" ] ; then
echo "Apply $v to $a"
echo "$v" > $a
fi
done
}
apply_alua () {
local pname=$1
local jsonobj=$2
local j
j=0
while [ true ] ; do
n=$(cat $JSON | jq -c "${jsonobj}.alua_tpgs[$j].name")
name=$(eval echo $n)
[ "$name" = "null" ] && break
echo "Updating ALUA information for $name"
if [ ! -d "${pname}/alua/${name}" ] ; then
mkdir ${pname}/alua/${name} || exit 1
id=$(cat $JSON | jq -c "${jsonobj}.alua_tpgs[$j].tg_pt_gp_id")
tg_id=$(eval echo $id)
if [ "$tg_id" != "null" ] && [ "$tg_id" != "0" ] ; then
echo $tg_id > ${pname}/alua/${name}/tg_pt_gp_id
fi
fi
apply_attributes ${pname}/alua/${name} "${jsonobj}.alua_tpgs[$j]"
(( j++ ))
done
}
find_iblock() {
local core=$1
local iblockname=$2
local iblock
for iblock in ${core}/iblock_* ; do
if [ -d ${iblock}/$iblockname ] ; then
echo $iblock/$iblockname
return
fi
done
}
next_iblock() {
local core=$1
local iblock
local i
local iblocknum=0
for iblock in ${core}/iblock_* ; do
[ -d ${iblock} ] || continue
i=${iblock##*_}
if [ "$i" -gt "$iblocknum" ] ; then
iblocknum=$i
fi
done
(( iblocknum++ ))
echo $iblocknum
}
setup_iblock() {
local jsonobj=$1
local name=$2
local pblk
local pname
local enable
pname=$(find_iblock $core $name)
if [ -z "$pname" ] ; then
iblocknum=$(next_iblock $core)
pblk=${core}/iblock_$iblocknum
if ! mkdir ${pblk} 2> /dev/null ; then
echo "Cannot create ${pblk}"
exit 1
fi
pname=${pblk}/${name}
if ! mkdir ${pname} 2> /dev/null ; then
echo "Cannot create ${pname}"
exit 1
fi
fi
enable=$(cat ${pname}/enable)
if [ "$enable" -eq 0 ] ; then
d=$(cat $JSON | jq -c "${jsonobj}.dev")
dev=$(eval echo $d)
path=$(cat ${pname}/udev_path)
if [ -n "$path" ] && [ "$path" != "$dev" ] ; then
rmdir $pname || exit 1
mkdir $pname || exit 1
fi
if [ -z "$path" ] ; then
echo "udev_path=$dev" > ${pname}/control || exit 1
echo "$dev" > ${pname}/udev_path
fi
w=$(cat $JSON | jq -c "${jsonobj}.wwn")
wwn=$(eval echo $w)
if [ "$wwn" != "null" ] ; then
echo "$wwn" > ${pname}/wwn/vpd_unit_serial || exit 1
fi
echo 1 > ${pname}/enable || exit 1
fi
echo "${pname}"
}
gen_fc_port_name() {
local port_num
local i j p
port_num=${1#naa.}
for i in $(seq 1 2 13) ; do
j=$(expr $i + 1)
p=$(echo $port_num | cut --bytes=$i-$j)
printf "%02x:" $(( 16#$p ))
done
p=$(echo $port_num | cut --bytes=15-16)
printf "%02x\n" $(( 16#$p ))
}
setup_fc_acl_luns() {
local jsonobj=$1
local nname=$2
local l j v
local idx lname alias tpg_lun tdir wp
l=0
while [ true ] ; do
j="${jsonobj}.mapped_luns[$l]"
v=$(cat $JSON | jq -c "${j}.index")
idx=$(eval echo $v)
[ "$idx" = "null" ] && break
lname=$nname/lun_${idx}
if [ ! -d ${lname} ] ; then
mkdir ${lname} || exit 1
fi
v=$(cat $JSON | jq -c "${j}.alias")
alias=$(eval echo $v)
if [ "$alias" = "null" ] ; then
alias="mapped_lun"
fi
v=$(cat $JSON | jq -c "${j}.tpg_lun")
tpg_lun=$(eval echo $v)
tdir=${nname%/acls/*}
ln -s ${tdir}/lun/lun_$tpg_lun $alias || exit 1
v=$(cat $JSON | jq -c "${j}.write_protect")
if [ $(eval echo $v) = "true" ] ; then
wp=1
else
wp=0
fi
v=$(cat $nname/write_protect)
if [ "$v" != "$wp" ] ; then
echo "$wp" > $nname/write_protect
fi
(( l++ ))
done
}
setup_fc_acls() {
local jsonobj=$1
local tname=$2
local n
local j
local v
local w
local wwn nname
n=0
while [ true ] ; do
j="${jsonobj}.node_acls[$n]"
v=$(cat $JSON | jq -c "${j}.node_wwn")
w=$(eval echo $v)
[ "$w" = "null" ] && break
wwn=$(gen_fc_port_name $w)
nname=${tname}/${wwn}
if [ ! -d ${nname} ] ; then
mkdir ${nname} || exit 1
fi
setup_fc_mapped_luns $j $nname
done
}
setup_fc_luns() {
local jsonobj=$1
local tname=$2
local l
local v
local j
local idx lname storage_object iblockname backstore alua
l=0
while [ true ] ; do
j="${jsonobj}.luns[$l]"
v=$(cat $JSON | jq -c "${j}.index")
idx=$(eval echo $v)
[ "$idx" = "null" ] && break
lname=${tname}/lun/lun_${idx}
if [ ! -d ${lname} ] ; then
mkdir ${lname} || exit 1
fi
v=$(cat $JSON | jq -c "${j}.storage_object")
storage_object=$(eval echo $v)
case $storage_object in
/backstores/block/*)
iblockname=${storage_object##*/}
for d in ${core}/iblock_* ; do
[ -d $d ] || continue
if [ -d "$d/$iblockname" ] ; then
backstore="$d/$iblockname"
fi
done
;;
*)
backstore=
;;
esac
if [ -n "$backstore" ] ; then
v=$(cat $JSON | jq -c "${j}.alias")
alias=$(eval echo $v)
if [ "$alias" = "null" ] ; then
alias="mapped_lun"
fi
ln -s $backstore $lname/$alias
fi
v=$(cat $JSON | jq -c "${j}.alua_tg_pt_gp_name")
alua=$(eval echo $v)
if [ "$alua" != "null" ] && [ "$alua" != "default_tg_pt_gp" ] ; then
echo "$alua" > $tname/alua_tg_pt_gp
fi
(( l++ ))
done
}
setup_fc() {
local jsonobj=$1
local wwn
local t
local tpgt_id
if [ ! -d ${fc} ] ; then
mkdir ${fc} || exit 1
fi
wwn=$(gen_fc_port_name $2)
pname=${fc}/${wwn}
if [ ! -d ${pname} ] ; then
mkdir ${pname} || exit 1
fi
t=0
while [ true ] ; do
jo="${jsonobj}.tpgs[$t]"
j=$(cat $JSON | jq -c "${jo}")
[ $(eval echo $j) = "null" ] && return
tpgt_id=$(expr $t + 1)
tname="$pname/tpgt_${tpgt_id}"
if [ ! -d ${tname} ] ; then
mkdir ${tname} || exit 1
fi
setup_fc_luns $jo $tname
setup_fc_acls $jo $tname
(( t++ ))
done
}
i=0
while [ true ] ; do
jsonobj=".storage_objects[$i]"
p=$(cat $JSON | jq -c "${jsonobj}.plugin")
plugin=$(eval echo $p)
case "$plugin" in
block)
n=$(cat $JSON | jq -c "${jsonobj}.name")
name=$(eval echo $n)
if [ "$name" != "null" ] ; then
pname=$(setup_iblock ${jsonobj} $name)
fi
;;
null)
break
esac
if [ "${pname}" ] ; then
apply_attributes ${pname}/attrib "${jsonobj}.attributes"
apply_alua ${pname} "${jsonobj}"
fi
(( i++ ))
done
i=0
while [ true ] ; do
jsonobj=".targets[$i]"
f=$(cat $JSON | jq -c "${jsonobj}.fabric")
fabric=$(eval echo $f)
case "$fabric" in
tcm_fc)
w=$(cat $JSON | jq -c "${jsonobj}.wwn")
wwn=$(eval echo $w)
if [ "$wwn" != "null" ] ; then
echo "Setting up FC WWN $wwn"
setup_fc ${jsonobj} $wwn
fi
;;
null)
break
esac
(( i++ ))
done