-
Notifications
You must be signed in to change notification settings - Fork 1
/
cherry-elastic-storage
executable file
·691 lines (567 loc) · 23.5 KB
/
cherry-elastic-storage
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#!/bin/bash
# Cherryservers.com block storage script for attaching and detaching volume on Linux
function print_usage() {
echo "Usage: $(basename $0) [-h] -v <vlan id> -z <vlan ip> -d <portal ip> -i <initiator name> -e -q"
echo
echo " -i initiator name"
echo " -d discovery IP address"
echo " -v VLAN"
echo " -z VLAN IP address"
echo " -e attach volume"
echo " -q detach volume"
echo
}
shopt -s nocasematch
template=$(mktemp)
systemctl=0
systemctl 2>/dev/null |grep -q '\-\.mount'
[[ $? -eq 0 ]] && systemctl=1
attach=no
detach=no
while getopts ":a:cheqwti:P:v:d:f:s:z:n:" opt; do
case $opt in
a) ipaddr=$OPTARG ;;
h) help=yes ;;
i) initiator=$OPTARG ;;
d) discovery_ip_1=$OPTARG ;;
z) vlan_ip_1=$OPTARG ;;
v) vlan_id=$OPTARG ;;
e) attach=yes ;;
q) detach=yes ;;
\?)
echo "Invalid option: -$OPTARG" >&2
print_usage >&2
exit 1
;;
esac
done
if [[ "$help" == "yes" ]]; then
print_usage
exit 0;
fi
if [[ "$attach" == "yes" ]]; then
if [[ -z "$vlan_ip_1" ]]; then
echo "No vlan IP address provided!"
exit 1;
fi
if [[ -z "$vlan_id" ]]; then
echo "No vlan ID provided!"
exit 1;
fi
if [[ -z "$discovery_ip_1" ]]; then
echo "No discovery IP provided!"
exit 1;
fi
if [[ -z "$initiator" ]]; then
echo "No initiator name provided!"
exit 1;
fi
elif [[ "$detach" == "yes" ]]; then
if [[ -z "$vlan_ip_1" ]]; then
echo "No vlan IP address provided!"
exit 1;
fi
if [[ -z "$discovery_ip_1" ]]; then
echo "No discovery IP provided!"
exit 1;
fi
else
echo "Either attach or detach option is required!"
print_usage
exit 0;
fi
function make_network_config_template() {
local vlan_interface=$1
local vlan_id=$2
local vlan_ip_2=$3
if [[ "$dist_name" == "ubuntu" ]] && [[ "$dist_vers" =~ 18|20|22 ]]; then
cat > $template << EOF
network:
version: 2
$([[ "$dist_vers" =~ 20|22 ]] && { echo "renderer: NetworkManager"; })
vlans:
$vlan_interface.$vlan_id:
addresses:
- $vlan_ip_2/24
id: $vlan_id
link: $vlan_interface
nameservers:
addresses:
- 46.166.166.46
- 5.199.160.160
search:
- localdomain
EOF
elif [[ "$dist_name" == "ubuntu" ]] || [[ "$dist_name" == "debian" ]]; then
cat > $template << EOF
auto $vlan_interface.$vlan_id
iface $vlan_interface.$vlan_id inet static
address $vlan_ip_2/24
mtu 1500
vlan-raw-device $vlan_interface
vlan_id $vlan_id
EOF
elif [[ "$dist_name" == "centos" ]] || [[ "$dist_name" == "almalinux" ]]; then
cat > $template << EOF
BOOTPROTO=none
DEVICE=$vlan_interface.$vlan_id:1
IPADDR=$vlan_ip_2
MTU=1500
NETMASK=255.255.255.0
NM_CONTROLLED=no
ONBOOT=yes
PHYSDEV=$vlan_interface
USERCTL=no
VLAN=yes
EOF
elif [[ "$dist_name" == "opensuse" ]]; then
cat > $template << EOF
IPADDR_1=$vlan_ip_2
NETMASK_1=255.255.255.0
EOF
else
echo "Unknown distribution."
exit 1
fi
}
function check_if_secondary_vlan_ip_is_up() {
local vlan_ip_2=$1
ip -4 a|grep -qwo $vlan_ip_2; [[ $? -eq 0 ]] && { return 0; } || { return 1; }
}
function detect_distribution() {
echo -e "........";
echo "Detect distribution:"
if [[ -f /etc/os-release ]]; then
t_var=$(grep -Po '^ID=["]*\K(\w+)|^VERSION_ID=["]*\K(\w+)' /etc/os-release 2>/dev/null|sort -r|tr '\n' ' ')
dist_name=$(cut -d ' ' -f1 <<< $t_var)
dist_vers=$(cut -d ' ' -f2 <<< $t_var)
elif [[ -f /etc/centos-release ]]; then
dist_name="centos"
dist_vers=$(grep -Po 'release \K(\d)' /etc/centos-release)
fi
echo -e "\tDist: $dist_name\tVer: $dist_vers"
echo "Detect distribution. Done"
}
function restart_network() {
local vlan_interface=$1
local vlan_id=$2
# Restart network
if [[ "$dist_name" == "ubuntu" ]] && [[ "$dist_vers" =~ 18|20 ]]; then
netplan apply
elif [[ "$dist_name" == "ubuntu" ]] && [[ "$dist_vers" == "22" ]]; then
netplan apply && sleep 3 \
&& netplan apply && systemctl start NetworkManager \
&& nmcli networking off && nmcli networking on
elif [[ "$dist_name" == "ubuntu" ]] && [[ "$dist_vers" == "14" ]]; then
ifdown $vlan_interface.$vlan_id && ifup $vlan_interface.$vlan_id
elif [[ "$dist_name" == "centos" ]] && [[ "$dist_vers" != "8" ]]; then
ip addr flush $vlan_interface.$vlan_id
[[ "$systemctl" -eq 1 ]] && systemctl restart network || service network restart
elif [[ "$dist_name" == "centos" ]] && [[ "$dist_vers" == "8" ]]; then
ip addr flush $vlan_interface.$vlan_id
ifdown $vlan_interface.$vlan_id && ifup $vlan_interface.$vlan_id
elif [[ "$dist_name" == "almalinux" ]] && [[ "$dist_vers" == "8" ]]; then
ip addr flush $vlan_interface.$vlan_id
ifdown $vlan_interface.$vlan_id && ifup $vlan_interface.$vlan_id
elif [[ "$dist_name" == "almalinux" ]] && [[ "$dist_vers" == "9" ]]; then
systemctl restart NetworkManager
elif [[ "$dist_name" == "opensuse" ]]; then
[[ "$systemctl" -eq 1 ]] && systemctl restart network || service network restart
else
[[ "$systemctl" -eq 1 ]] && systemctl restart networking
fi
sleep 20;
}
function reboot_iscsid() {
# Restart iscsid
echo -e "........"
echo "Reboot iscsid service:"
if [[ "$dist_name" == "ubuntu" ]] || [[ "$dist_name" == "debian" ]]; then
if [[ "$dist_name" == "ubuntu" ]] && [[ "$dist_vers" != "14" ]]; then
[[ "$systemctl" -eq 1 ]] && systemctl restart iscsid || service iscsid restart
elif [[ "$dist_name" == "debian" ]] && [[ "$dist_vers" != "8" ]]; then
[[ "$systemctl" -eq 1 ]] && systemctl restart iscsid || service iscsid restart
elif [[ "$dist_name" == "ubuntu" ]] && [[ "$dist_vers" == "14" ]]; then
[[ "$systemctl" -eq 1 ]] && systemctl restart open-iscsi || service open-iscsi restart
elif [[ "$dist_name" == "debian" ]] && [[ "$dist_vers" == "8" ]]; then
[[ "$systemctl" -eq 1 ]] && systemctl restart open-iscsi || service open-iscsi restart
fi
elif
[[ "$dist_name" == "centos" ]] || [[ "$dist_name" == "almalinux" ]]; then
[[ "$systemctl" -eq 1 ]] && systemctl restart iscsid || service iscsid restart
elif
[[ "$dist_name" == "opensuse" ]]; then
[[ "$systemctl" -eq 1 ]] && systemctl restart iscsid || service iscsid restart
else
echo -e "Unknown distribution!"
fi
echo "Reboot iscsid service. Done."
}
function calculate_ips() {
local vlan_ip_1=$1
local discovery_ip_1=$2
# Get secondary vlan and discovery IP addresses
if [[ "$vlan_ip_1" =~ "10.160." ]] || [[ "$vlan_ip_1" =~ "10.16ą." ]]; then
vlan_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $vlan_ip_1)
discovery_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $discovery_ip_1)
elif [[ "$vlan_ip_1" =~ "10.162." ]] || [[ "$vlan_ip_1" =~ "10.163." ]] || [[ "$vlan_ip_1" =~ "10.164." ]]; then
vlan_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $vlan_ip_1)
discovery_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $discovery_ip_1)
elif [[ "$vlan_ip_1" =~ "10.165." ]] || [[ "$vlan_ip_1" =~ "10.166." ]] || [[ "$vlan_ip_1" =~ "10.167." ]]; then
vlan_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $vlan_ip_1)
discovery_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $discovery_ip_1)
elif [[ "$vlan_ip_1" =~ "10.168." ]] || [[ "$vlan_ip_1" =~ "10.169." ]] || [[ "$vlan_ip_1" =~ "10.170." ]]; then
vlan_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $vlan_ip_1)
discovery_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $discovery_ip_1)
elif [[ "$vlan_ip_1" =~ "10.171." ]] || [[ "$vlan_ip_1" =~ "10.172." ]] || [[ "$vlan_ip_1" =~ "10.173." ]]; then
vlan_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $vlan_ip_1)
discovery_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $discovery_ip_1)
elif [[ "$vlan_ip_1" =~ "10.174." ]] || [[ "$vlan_ip_1" =~ "10.175." ]] || [[ "$vlan_ip_1" =~ "10.176." ]]; then
vlan_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $vlan_ip_1)
discovery_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $discovery_ip_1)
elif [[ "$vlan_ip_1" =~ "10.177." ]] || [[ "$vlan_ip_1" =~ "10.178." ]] || [[ "$vlan_ip_1" =~ "10.179." ]]; then
vlan_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $vlan_ip_1)
discovery_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $discovery_ip_1)
elif [[ "$vlan_ip_1" =~ "10.180." ]] || [[ "$vlan_ip_1" =~ "10.181." ]] || [[ "$vlan_ip_1" =~ "10.182." ]]; then
vlan_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $vlan_ip_1)
discovery_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $discovery_ip_1)
elif [[ "$vlan_ip_1" =~ "10.183." ]] || [[ "$vlan_ip_1" =~ "10.184." ]] || [[ "$vlan_ip_1" =~ "10.185." ]]; then
vlan_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $vlan_ip_1)
discovery_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $discovery_ip_1)
elif [[ "$vlan_ip_1" =~ "10.186." ]] || [[ "$vlan_ip_1" =~ "10.187." ]] || [[ "$vlan_ip_1" =~ "10.188." ]]; then
vlan_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $vlan_ip_1)
discovery_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $discovery_ip_1)
elif [[ "$vlan_ip_1" =~ "10.189." ]] || [[ "$vlan_ip_1" =~ "10.190." ]] || [[ "$vlan_ip_1" =~ "10.191." ]]; then
vlan_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $vlan_ip_1)
discovery_ip_2=$(awk -F '.' '{print $1"."$2-32"."$3"."$4}' <<< $discovery_ip_1)
elif [[ "$vlan_ip_1" =~ "192.168." ]]; then
vlan_ip_2=$(awk -F '.' '{print "10.157."$3"."$4}' <<< $vlan_ip_1)
discovery_ip_2=$(awk -F '.' '{print "10.157."$3"."$4}' <<< $discovery_ip_1)
else
echo "Unknown VLAN subnet: $vlan_ip_1";
exit 1
fi
}
function make_secondary_vlan_ip_up() {
local vlan_id=$1
local vlan_ip_1=$2
echo -e "........";
echo "Configure secondary VLAN IP:"
# Get VLAN interface
vlan_interface=$(ip -4 a | grep $vlan_id | grep 'state UP' | cut -d '@' -f2|cut -d ':' -f1)
calculate_ips $vlan_ip_1 $discovery_ip_1
if [[ "$dist_name" == "ubuntu" ]] && [[ "$dist_vers" =~ 18|20|22 ]]; then
net_cfg="/etc/netplan/60-$vlan_ip_2.yaml"
elif [[ "$dist_name" == "ubuntu" ]] || [[ "$dist_name" == "debian" ]]; then
net_cfg="/etc/network/interfaces.d/60-$vlan_ip_2.cfg"
elif [[ "$dist_name" == "centos" ]] || [[ "$dist_name" == "almalinux" ]]; then
net_cfg="/etc/sysconfig/network-scripts/ifcfg-$vlan_interface.$vlan_id:1"
elif [[ "$dist_name" == "opensuse" ]]; then
net_cfg="/etc/sysconfig/network/ifcfg-$vlan_interface.$vlan_id"
else
echo "Unknown distribution."
exit 1
fi
check_if_secondary_vlan_ip_is_up $vlan_ip_2
if [[ $? -eq 1 ]]; then
echo -e "\tNo secondary VLAN IP setuped. Let's setup one."
make_network_config_template $vlan_interface $vlan_id $vlan_ip_2
if [[ ! -e "$net_cfg" ]]; then
cp $template $net_cfg
# Restart network
for attempt in $(seq 1 3); do
sleep 3;
check_if_secondary_vlan_ip_is_up $vlan_ip_2
if [[ $? -eq 1 ]]; then
restart_network $vlan_interface $vlan_id
else
break
fi
done
else
if [[ "$dist_name" == "opensuse" ]]; then
cat ${template} >> ${net_cfg}
restart_network $vlan_interface $vlan_id
else
diff -q $template $net_cfg >/dev/null
if [[ $? -eq 1 ]]; then
echo -e "\tNetwork config file for $vlan_ip_2 already exists."
echo -e "\t* Network config file exists but content differs from template."
echo -e "\t Ensure $vlan_ip_2 is up and running by either removing"
echo -e "\t $net_cfg and rerun script"
echo -e "\t or configure it manually and try to run script again."
return 1;
else
echo -e "\tNetwork config file for $vlan_ip_2 already exists."
echo -e "The contents are valid. Try to restart networking."
# Restart network
restart_network $vlan_interface $vlan_id
fi
fi
fi
else
echo -e "\tSecondary IP already exists. Skiping..."
fi
check_if_secondary_vlan_ip_is_up $vlan_ip_2
[[ $? -eq 0 ]] && { echo "Configure secondary VLAN IP. Done."; return 0; } || { return 1; }
}
function load_multipath_modules() {
modprobe dm_multipath
modprobe dm_round_robin
}
function ensure_services_up_and_enabled() {
echo -e "........"
echo "Enable services:"
if [[ "$dist_name" == "ubuntu" ]] || [[ "$dist_name" == "debian" ]]; then
if [[ "$dist_name" == "ubuntu" ]] && [[ "$dist_vers" != "14" ]]; then
[[ "$systemctl" -eq 1 ]] && systemctl enable iscsid &>/dev/null
elif [[ "$dist_name" == "debian" ]] && [[ "$dist_vers" != "8" ]]; then
[[ "$systemctl" -eq 1 ]] && systemctl enable iscsid &>/dev/null
elif [[ "$dist_name" == "ubuntu" ]] && [[ "$dist_vers" == "14" ]]; then
[[ "$systemctl" -eq 1 ]] && systemctl enable open-iscsi &>/dev/null
elif [[ "$dist_name" == "debian" ]] && [[ "$dist_vers" == "8" ]]; then
[[ "$systemctl" -eq 1 ]] && systemctl enable open-iscsi &>/dev/null
fi
elif
[[ "$dist_name" == "centos" ]] || [[ "$dist_name" == "almalinux" ]]; then
if [[ "$systemctl" -eq 1 ]]; then
systemctl enable multipathd &>/dev/null; systemctl enable iscsid &>/dev/null;
systemctl start multipathd &>/dev/null
else
chkconfig iscsi on &>/dev/null; chkconfig multipathd on &>/dev/null;
service multipathd start &>/dev/null
fi
else
echo -e "Unknown distribution!"
fi
echo "Enable services. Done."
}
function install_required_packages() {
echo -e "........"
echo -e "Installing required packages:"
if [[ "$dist_name" == "ubuntu" ]] || [[ "$dist_name" == "debian" ]]; then
export DEBIAN_FRONTEND=noninteractive
[[ ! $(which iscsiadm 2>/dev/null) ]] && { apt -qq update 1>/dev/null && apt -yq install open-iscsi 1>/dev/null; }
[[ ! $(which multipath 2>/dev/null) ]] && { apt -qq update 1>/dev/null && apt -yq install multipath-tools 1>/dev/null; }
elif [[ "$dist_name" == "centos" ]] || [[ "$dist_name" == "almalinux" ]]; then
[[ ! $(which iscsiadm 2>/dev/null) ]] && { yum -y install -d1 iscsi-initiator-utils 1>/dev/null; }
[[ ! $(which multipath 2>/dev/null) ]] && { yum -y install -d1 device-mapper-multipath 1>/dev/null; }
elif [[ "$dist_name" == "opensuse" ]]; then
[[ ! $(which iscsiadm 2>/dev/null) ]] && { zypper -q install -y open-iscsi 1>/dev/null; }
[[ ! $(which multipath 2>/dev/null) ]] && { zypper -q install -y multipath-tools 1>/dev/null; }
else
echo "Unknown packaging system!"
exit 1
fi
load_multipath_modules
echo -e "Installing required packages. Done."
}
function configure_iscsi() {
echo -e "........"
echo -e "Configure iscsid:"
if [[ $(grep 'node.startup = manual' /etc/iscsi/iscsid.conf) ]]; then
echo -e "\tUpdating iscsid.conf file..."
sed -i.bak 's/node.startup = manual/node.startup = automatic/g' /etc/iscsi/iscsid.conf
fi
echo -e "Configure iscsid. Done."
}
function create_multipath_config() {
echo -e "........"
echo -e "Create multipath config:"
cat <<- EOF > /etc/multipath.conf
defaults {
polling_interval 3
fast_io_fail_tmo 5
path_selector "round-robin 0"
rr_min_io 100
rr_weight priorities
failback immediate
user_friendly_names yes
}
blacklist {
devnode "^(ram|raw|loop|fd|md|dm-|sr|scd|st)[0-9]*"
devnode "^hd[a-z][[0-9]*]"
devnode "^vd[a-z]"
devnode "^cciss!c[0-9]d[0-9]*[p[0-9]*]"
device {
vendor "Micron"
product ".*"
}
device {
vendor "Intel"
product ".*"
}
device {
vendor "DELL"
product ".*"
}
}
devices {
device {
vendor "StorPool"
product "iSCSI DISK"
path_grouping_policy group_by_prio
path_checker tur
hardware_handler "1 alua"
}
}
EOF
echo -e "Create multipath config. Done."
}
function change_initiator_name() {
local initiator_name=$1
echo -e "........"
echo -e "Change initiator name:"
if [[ ! $(grep $initiator_name /etc/iscsi/initiatorname.iscsi) ]]; then
echo -e "\tUpdate initiator name to $initiator_name"
sed -i "s/InitiatorName=.*/InitiatorName=$initiator_name/g" /etc/iscsi/initiatorname.iscsi
fi
echo -e "Change initiator name. Done."
}
function clean_mpath_bindings() {
echo -e "........"
echo -e "Clean mpath bindings:"
[[ -f /etc/multipath/bindings ]] && sed -i "/^mpath.*/d" /etc/multipath/bindings
if [[ $(ls /dev/mapper/mpath* 2>/dev/null) ]]; then
echo -e "\tmpath entried found. Cleaning..."
for m in $(ls /dev/mapper/mpath* 2>/dev/null); do
multipath -f $m
done
fi
multipath >/dev/null
echo -e "Clean mpath bindings. Done."
}
function configure_multipath() {
echo -e "\t........"
echo -e "\tConfigure multipath:"
dev_name=$(ls -l /dev/disk/by-path/|grep "$target_name"|grep "$discovery_ip_1"|awk '{print $11}'|sed 's/\.\.\/\.\.\///g')
if [[ $(multipath -ll|grep -Eo '.*dm'|awk '{if (NF == 3){print $2} else if(NF == 2){print $1}}'|sed 's/[()]//g'|grep StorPool) ]]; then
wwid=$(multipath -ll|grep -Eo '.*dm'|awk '{if (NF == 3){print $2} else if(NF == 2){print $1}}'|sed 's/[()]//g'|grep StorPool)
else
wwid=$(udevadm info --query=property --name=/dev/$dev_name | grep -Po 'ID_SERIAL=\K(.*)')
fi
vol_name=$(cut -d ':' -f3 <<< $target_name)
if [[ $(grep "^$vol_name" /etc/multipath/bindings 2>/dev/null) ]]; then
sed -i "s/^$vol_name .*/$vol_name $wwid/" /etc/multipath/bindings
else
echo "$vol_name $wwid" >> /etc/multipath/bindings
fi
multipath $vol_name &>/dev/null
echo -e "\tConfigure multipath. Done."
}
function attach_volume() {
local discovery_ip_1=$1
local discovery_ip_2=$2
portals=($discovery_ip_1 $discovery_ip_2)
for portal in ${portals[*]}; do
echo -e "........"
echo -e "Discover targets on portal $portal:"
if [[ $(iscsiadm --mode discovery -t sendtargets --portal $portal) ]]; then
echo -e "\tTargets were discovered on $portal"
else
echo -e "\tError: Can't discover targets on $portal. Try again later."
exit 1;
fi
echo -e "Discover targets on portals. Done."
target_name=$(iscsiadm -m node -o show|grep 'node.name'|awk '{print $3}'|uniq)
echo -e "........"
echo -e "Login on portal $portal:"
if [[ ! $(iscsiadm -m session -o show 2>/dev/null|grep $portal) ]]; then
if [[ $(iscsiadm -m node -T $target_name -p $portal -l) ]]; then
echo -e "\tLogged in to $target_name on $portal"
sleep 5
configure_multipath
else
echo "Can't login to $target_name on portal $portal"
fi
else
echo -e "\tSession already exists."
configure_multipath
fi
echo -e "Login on portal $portal. Done."
done
}
function detach_volume() {
echo -e "........"
echo -e "Detach volume:"
calculate_ips $vlan_ip_1 $discovery_ip_1
vol_name=$(iscsiadm -m session -o show 2>/dev/null|grep -E "$discovery_ip_1|$discovery_ip_2"|grep -Po "iqn.2019-03.com.cherryservers:cherryservers:\K(.*)"|awk '{print $1}')
if [[ $vol_name ]]; then
vol_name=$(echo $vol_name|awk '{print $1}')
mnt=$(grep $vol_name /proc/mounts 2>/dev/null)
if [[ $mnt ]]; then
echo -e "\tVolume $vol_name is mounted. Please unmount it first and try again."
exit 1
fi
target_name=$(iscsiadm -m node -o show|grep 'node.name'|awk '{print $3}'|uniq)
for portal in $(iscsiadm -m session | grep $target_name | awk {'print $3'} | egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'); do
echo -e "\tLog out from $target_name on $portal"
iscsiadm --mode node --targetname $target_name --portal $portal --logout &>/dev/null
sed -i "/^$vol_name.*/d" /etc/multipath/bindings
multipath -f $vol_name &>/dev/null
done
clean_mpath_bindings
rm -rf /etc/iscsi/{nodes,send_targets}/*
rm -rf /var/lib/iscsi/{nodes,send_targets}/*
else
echo -e "\tCan't find any active sessions with provided subnets"
fi
echo -e "Detach volume. Done."
}
function get_status() {
local vol_name=$1
if [[ -f /etc/multipath/bindings ]]; then
wwid=$(grep "${vol_name:-none}" /etc/multipath/bindings|awk '{print $2}')
fi
echo -e "#####################"
echo -e "Configuration report:"
if [[ -b "/dev/mapper/$vol_name" ]]; then
echo -e "\tBlock device /dev/mapper/$vol_name is ready. Format, mount and use it."
elif [[ -b "/dev/mapper/$wwid" ]]; then
echo -e "\tBlock device found as WWID: /dev/mapper/$wwid. Releading devmap..."
multipath -r >/dev/null
if [[ -b "/dev/mapper/$vol_name" ]]; then
echo -e "\tBlock device /dev/mapper/$vol_name is ready. Format, mount and use it."
fi
else
echo -e "\tCan't find block device /dev/mapper/$vol_name. Please contact support for assistance or try again."
fi
echo -e "Configuration report. Done"
echo -e "#####################"
}
function check_if_portals_reachable() {
local discovery_ip_1=$1
local discovery_ip_2=$2
echo -e "........"
echo -e "Check portal IPs:"
portals=($discovery_ip_1 $discovery_ip_2)
for portal in ${portals[*]}; do
if ! ping -q -c 3 "${portal}" >/dev/null; then
echo -e "\tIt seems that internal VLAN is not ready yet for $portal address."
echo -e "\tPlease try to run command again a bit later."
echo -e "Check portal IPs. Done."
return 1
fi
done
echo -e "Check portal IPs. Done."
return 0
}
if [[ "$attach" == "yes" ]]; then
detect_distribution
make_secondary_vlan_ip_up $vlan_id $vlan_ip_1; [[ $? -eq 0 ]] || { echo "Unable to setup secondary IP!"; exit 1; }
check_if_portals_reachable $discovery_ip_1 $discovery_ip_2; [[ $? -eq 0 ]] || exit 1
install_required_packages
create_multipath_config
change_initiator_name $initiator
configure_iscsi
reboot_iscsid
ensure_services_up_and_enabled
change_initiator_name $initiator
reboot_iscsid
attach_volume $discovery_ip_1 $discovery_ip_2
clean_mpath_bindings
get_status $vol_name
fi
if [[ "$detach" == "yes" ]]; then
detach_volume $discovery_ip_1 $vlan_ip_1
fi