forked from xcat2/xcat-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigeth
executable file
·1098 lines (1026 loc) · 40.2 KB
/
configeth
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# IBM(c) 2014 EPL license http://www.eclipse.org/legal/epl-v10.html
# Internal script used by confignics only.
# It configs the Ethernet adpaters on the node
if [ "$(uname -s|tr 'A-Z' 'a-z')" = "linux" ];then
str_dir_name=`dirname $0`
. $str_dir_name/xcatlib.sh
. $str_dir_name/nicutils.sh
fi
error_code=0
if [ -n "$LOGLABEL" ]; then
log_label=$LOGLABEL
else
log_label="xcat"
fi
#########################################################################
# ifdown/ifup will not be executed in diskful provision postscripts stage
#########################################################################
reboot_nic_bool=1
if [ -z "$UPDATENODE" ] || [ $UPDATENODE -ne 1 ] ; then
if [ "$NODESETSTATE" = "install" ] && ! grep "REBOOT=TRUE" /opt/xcat/xcatinfo >/dev/null 2>&1; then
reboot_nic_bool=0
fi
fi
########################################################################
# networkmanager_active=0: use network.service
# networkmanager_active=1: use NetworkManager
########################################################################
networkmanager_active=0
checkservicestatus NetworkManager > /dev/null
if [ $? -eq 0 ]; then
networkmanager_active=1
fi
function configipv4(){
str_if_name=$1
str_v4ip=$2
str_v4net=$3
str_v4mask=$4
num_v4num=$5
str_extra_params=$6
str_nic_mtu=$7
#parse the extra parameters
if [ "$str_extra_params" != "$str_default_token" ]; then
parse_nic_extra_params "$str_extra_params"
fi
if [ "$str_os_type" = "sles" ];then
str_conf_file="/etc/sysconfig/network/ifcfg-${str_if_name}"
if [ $num_v4num -eq 0 ];then
echo "DEVICE=${str_if_name}" > $str_conf_file
echo "BOOTPROTO=static" >> $str_conf_file
echo "IPADDR=${str_v4ip}" >> $str_conf_file
echo "NETMASK=${str_v4mask}" >> $str_conf_file
echo "NETWORK=${str_v4net}" >> $str_conf_file
echo "STARTMODE=onboot" >> $str_conf_file
echo "USERCONTROL=no" >> $str_conf_file
echo "_nm_name=static-0" >> $str_conf_file
if [ "$str_nic_mtu" != "$str_default_token" ]; then
echo "MTU=${str_nic_mtu}" >> $str_conf_file
fi
#add extra params
i=0
while [ $i -lt ${#array_extra_param_names[@]} ]
do
name="${array_extra_param_names[$i]}"
value="${array_extra_param_values[$i]}"
echo " $i: name=$name value=$value"
echo "${name}=${value}" >> $str_conf_file
i=$((i+1))
done
else
echo "IPADDR_${num_v4num}=${str_v4ip}" >> $str_conf_file
echo "NETMASK_${num_v4num}=${str_v4mask}" >> $str_conf_file
echo "NETWORK_${num_v4num}=${str_v4net}" >> $str_conf_file
echo "LABEL_${num_v4num}=${num_v4num}" >> $str_conf_file
if [ "$str_nic_mtu" != "$str_default_token" ]; then
echo "MTU_${num_v4num}=${str_nic_mtu}" >> $str_conf_file
fi
#add extra params
i=0
while [ $i -lt ${#array_extra_param_names[@]} ]
do
name="${array_extra_param_names[$i]}"
value="${array_extra_param_values[$i]}"
echo " $i: name=$name value=$value"
echo "${name}=${value}" >> $str_conf_file
i=$((i+1))
done
fi
if [[ ${str_if_name} == [a-zA-Z0-9]*.[0-9]* ]]; then
echo "VLAN=yes" >> $str_conf_file
fi
#debian ubuntu
elif [ "$str_os_type" = "debian" ];then
str_conf_file="/etc/network/interfaces.d/${str_if_name}"
if [ $num_v4num -eq 0 ];then
echo "auto ${str_if_name}" > $str_conf_file
echo "iface ${str_if_name} inet static" >> $str_conf_file
else
echo "auto ${str_if_name}:${num_v4num}" >> $str_conf_file
echo "iface ${str_if_name}:${num_v4num} inet static" >> $str_conf_file
fi
echo " address ${str_v4ip}" >> $str_conf_file
echo " netmask ${str_v4mask}" >> $str_conf_file
echo " network ${str_v4net}" >> $str_conf_file
if [ "$str_nic_mtu" != "$str_default_token" ]; then
echo " mtu ${str_nic_mtu}" >> $str_conf_file
fi
#add extra params
i=0
while [ $i -lt ${#array_extra_param_names[@]} ]
do
name="${array_extra_param_names[$i]}"
value="${array_extra_param_values[$i]}"
echo " $i: name=$name value=$value"
echo " ${name} ${value}" >> $str_conf_file
i=$((i+1))
done
if [[ ${str_if_name} == [a-zA-Z0-9]*.[0-9]* ]]; then
parent_device=`echo ${str_if_name} | sed -e 's/\([a-zA-Z0-9]*\)\.[0-9]*/\1/g'`
echo " vlan-raw-device ${parent_device}" >> $str_conf_file
fi
else
str_prefix=$(v4mask2prefix $str_v4mask)
# Write the info to the ifcfg file for redhat
con_name=""
str_conf_file=""
if [ $num_v4num -ne 0 ]; then
str_if_name=${str_if_name}:${num_v4num}
fi
str_conf_file="/etc/sysconfig/network-scripts/ifcfg-${str_if_name}"
if [ $networkmanager_active -eq 1 ]; then
con_name=$(nmcli dev show ${str_if_name}|grep CONNECTION|awk -F: '{print $2}'|sed 's/^[ \t]*$//g')
if [ "$con_name" == "--" ] ; then
nmcli con add type ethernet con-name ${str_if_name} ifname ${str_if_name} ipv4.method manual ipv4.addresses ${str_v4ip}/${str_prefix}
else
nmcli con mod "${con_name}" ipv4.method manual ipv4.addresses ${str_v4ip}/${str_prefix}
fi
else
echo "DEVICE=${str_if_name}" > $str_conf_file
echo "BOOTPROTO=none" >> $str_conf_file
echo "NM_CONTROLLED=no" >> $str_conf_file
echo "IPADDR=${str_v4ip}" >> $str_conf_file
echo "NETMASK=${str_v4mask}" >> $str_conf_file
echo "ONBOOT=yes" >> $str_conf_file
fi
if [ "$str_nic_mtu" != "$str_default_token" ]; then
echo "MTU=${str_nic_mtu}" >> $str_conf_file
fi
if [[ ${str_if_name} == [a-zA-Z0-9]*.[0-9]* ]]; then
echo "VLAN=yes" >> $str_conf_file
fi
#add extra params
i=0
while [ $i -lt ${#array_extra_param_names[@]} ]
do
name="${array_extra_param_names[$i]}"
value="${array_extra_param_values[$i]}"
echo " $i: name=$name value=$value"
echo "${name}=${value}" >> $str_conf_file
i=$((i+1))
done
fi
}
configipv6(){
str_if_name=$1
str_v6ip=$2
str_v6net=$3
str_v6prefix=$4
num_v6num=$5
num_v4num=$6
str_v6gateway=$7
str_extra_params=$8
#parse the extra parameters
if [ "$str_extra_params" != "$str_default_token" ]; then
parse_nic_extra_params "$str_extra_params"
fi
#remove the prefix length from the subnet
str_v6net=`echo $str_v6net | cut -d"/" -f 1`
#remove the "/" from mask
str_v6prefix=`echo $str_v6prefix | sed 's/\///'`
if [ "$str_os_type" = "sles" ];then
str_conf_file="/etc/sysconfig/network/ifcfg-${str_if_name}"
if [ $num_v4num -eq 0 -a $num_v6num -eq 0 ];then
echo "DEVICE=$str_if_name" > $str_conf_file
echo "BOOTPROTO=static" >> $str_conf_file
echo "NM_CONTROLLED=no" >> $str_conf_file
echo "STARTMODE=onboot" >> $str_conf_file
fi
echo "LABEL_ipv6${num_v6num}=ipv6$num_v6num" >> $str_conf_file
echo "IPADDR_ipv6${num_v6num}=${str_v6ip}" >> $str_conf_file
echo "PREFIXLEN_ipv6${num_v6num}=${str_v6prefix}" >> $str_conf_file
if [ "$str_v6gateway" != "$str_default_token" ] -a [ `echo $str_v6gateway | grep -v 'xcatmaster'` ];then
grep -E "default[:space:]+${str_v6gateway}[:space:]+" /etc/sysconfig/network/routes 2>&1 1>/dev/null
if [ $? -ne 0 ];then
echo "default $str_v6gateway - -" >> /etc/sysconfig/network/routes
fi
fi
#add extra params
i=0
while [ $i -lt ${#array_extra_param_names[@]} ]
do
name="${array_extra_param_names[$i]}"
value="${array_extra_param_values[$i]}"
echo " $i: name=$name value=$value"
echo "${name}=${value}" >> $str_conf_file
i=$((i+1))
done
elif [ "$str_os_type" = "debian" ];then
#debian or ubuntu
str_conf_file="/etc/network/interfaces.d/${str_if_name}"
if [ $num_v4num -eq 0 -a $num_v6num -eq 0 ];then
echo "auto ${str_if_name}" > $str_conf_file
fi
if [ $num_v6num -eq 0 ];then
echo "pre-up modprobe ipv6" >> $str_conf_file
echo "iface ${str_if_name} inet6 static" >> $str_conf_file
echo " address ${str_v6ip}" >> $str_conf_file
echo " netmask ${str_v6prefix}" >> $str_conf_file
if [ "$str_v6gateway" != "$str_default_token" ]; then
echo " gateway ${str_v6gateway}" >> $str_conf_file
fi
#add extra params
i=0
while [ $i -lt ${#array_extra_param_names[@]} ]
do
name="${array_extra_param_names[$i]}"
value="${array_extra_param_values[$i]}"
echo " $i: name=$name value=$value"
echo " ${name} ${value}" >> $str_conf_file
i=$((i+1))
done
else
echo " post-up /sbin/ifconfig ${str_if_name} inet6 add ${str_v6ip}/${str_v6prefix}" >> $str_conf_file
echo " pre-down /sbin/ifconfig ${str_if_name} inet6 del ${str_v6ip}/${str_v6prefix}" >> $str_conf_file
fi
else
#redhat
str_conf_file="/etc/sysconfig/network-scripts/ifcfg-${str_if_name}"
if [ $num_v4num -eq 0 -a $num_v6num -eq 0 ];then
echo "DEVICE=$str_if_name" > $str_conf_file
echo "BOOTPROTO=static" >> $str_conf_file
echo "NM_CONTROLLED=no" >> $str_conf_file
echo "ONBOOT=yes" >> $str_conf_file
fi
if [ $num_v6num -eq 0 ];then
echo "IPV6INIT=yes" >> $str_conf_file
echo "IPV6ADDR=${str_v6ip}/${str_v6prefix}" >> $str_conf_file
else
echo "IPV6ADDR_SECONDARIES=${str_v6ip}/${str_v6prefix}" >> $str_conf_file
fi
if [ "$str_v6gateway" != "$str_default_token" ] -a [ `echo $str_v6gateway | grep -v 'xcatmaster'` ];then
echo "IPV6_DEFAULTGW=$str_v6gateway" >> $str_conf_file
fi
#add extra params
i=0
while [ $i -lt ${#array_extra_param_names[@]} ]
do
name="${array_extra_param_names[$i]}"
value="${array_extra_param_values[$i]}"
echo " $i: name=$name value=$value"
echo "${name}=${value}" >> $str_conf_file
i=$((i+1))
done
fi
}
#delete all configuration file(s) on linux
function delete_nic_config_files(){
str_temp_name=$1
#delete the configuration files
#delete the configuration history
if [ "$str_os_type" = "debian" ];then
rm -f /etc/network/interfaces.d/$str_temp_name 2>/dev/null
sed -i "/${str_temp_name}/d" /etc/network/xcat_history_important
elif [ "$str_os_type" = "sles" ];then
rm -f /etc/sysconfig/network/ifcfg-${str_temp_name} 2>/dev/null
sed -i "/${str_temp_name}/d" /etc/sysconfig/network/xcat_history_important
else
rm -f /etc/sysconfig/network-scripts/ifcfg-${str_temp_name} 2>/dev/null
rm -f /etc/sysconfig/network-scripts/ifcfg-${str_temp_name}:* 2>/dev/null
sed -i "/${str_temp_name}/d" /etc/sysconfig/network-scripts/xcat_history_important
fi
}
function add_ip_temporary(){
local str_ip_prefix=$1
local str_temp_name=$2
local str_ip=`echo $str_ip_prefix | awk -F'_' '{print $1}'`
local str_mask=`echo $str_ip_prefix | awk -F'_' '{print $2}'`
if [ "$str_os_type" = "aix" ];then
echo $str_ip | grep ":" > /dev/null
#ipv6
if [ $? -eq 0 ];then
lsattr -El $str_temp_name | grep netaddr6 | awk '{print $2}' | grep ":"
if [ $? -ne 0 ];then
chdev -l $str_temp_name -a netaddr6=$str_ip -a prefixlen=$str_mask
else
chdev -l $str_temp_name -a alias6=${str_ip}/${str_mask}
fi
#ipv4
else
lsattr -El $str_temp_name | grep netaddr | awk '{print $2}' | grep '\.'
if [ $? -ne 0 ];then
chdev -l $str_temp_name -a netaddr=${str_ip} -a netmask=${str_mask}
else
chdev -l $str_temp_name -a alias4=${str_ip},${str_mask}
fi
fi
else
echo $str_ip | grep ":" > /dev/null
#ipv6
if [ $? = 0 ];then
lsmod |grep -w 'ipv6'
if [ $? -ne 0 ];then
modprobe ipv6
fi
ip addr add ${str_ip}/${str_mask} dev $str_temp_name
#ipv4
else
str_label=''
ip addr show dev $str_temp_name | grep inet | grep "global" | grep -v ':' | grep "${str_temp_name}"
if [ $? -eq 0 ];then
for num_i in {1..1000}
do
ip addr show dev $str_temp_name | grep inet | grep "global" | grep ":${num_i}"
if [ $? -ne 0 ];then
str_label=${str_nic_name}:${num_i}
break
fi
done
else
str_label=$str_nic_name
fi
str_bcase=$(v4calcbcase $str_ip $str_mask)
#the label is ready, add the ip address directly
ip addr add $str_ip/${str_mask} broadcast $str_bcase dev $str_nic_name scope global label $str_label
if [ $? -ne 0 ]; then
log_error "add the ip address $str_ip/${str_mask} failed."
error_code=1
fi
fi
fi
}
# This token is used for the value of an attributes that has not been assigned any value.
str_default_token="default"
str_nic_name=''
str_os_type=`uname | tr 'A-Z' 'a-z'`
str_cfg_dir=''
str_temp=''
if [ "$str_os_type" = "linux" ];then
str_temp=`echo $OSVER | grep -E '(sles|suse)'`
if [ -f "/etc/debian_version" ];then
debianpreconf
str_os_type="debian"
str_cfg_dir="/etc/network/"
elif [ -f "/etc/SuSE-release" -o -n "$str_temp" ];then
str_os_type="sles"
str_cfg_dir="/etc/sysconfig/network/"
else
str_os_type="redhat"
str_cfg_dir="/etc/sysconfig/network-scripts/"
fi
else
echo "configeth dose not support AIX in this build"
exit 1
fi
log_info "configeth on $NODE: os type: $str_os_type"
if [ "$1" = "-r" ];then
if [ $# -ne 2 ];then
log_error "configeth on $NODE: remove nic, but the nic name is missed"
exit 1
fi
str_nic_name=$2
log_info "configeth on $NODE: remove nic $str_nic_name"
if [ "$str_os_type" = "aix" ];then
old_ifs=$IFS
IFS=$'\n'
str_temp=`lsattr -El $str_nic_name | grep alias4 | awk '{print $2}'`
array_alias4_temp=($str_temp)
IFS=$old_ifs
for str_ip_alias4 in $str_temp
do
#the alias format should be ipaddr,netmask
echo $str_ip_alias4 | grep -E ,
if [ $? -eq 0 ];then
chdev -l $str_nic_name -a delalias4=$str_ip_alias4
fi
done
str_temp=`lsattr -El $str_nic_name | grep alias6 | awk '{print $2}'`
old_ifs=$IFS
IFS=$'\n'
array_alias6_temp=($str_temp)
IFS=$old_ifs
for str_ip_alias6 in ${array_alias6_temp[@]}
do
echo $str_ip_alias6 | grep -E /
if [ $? -eq 0 ];then
chdev -l $str_nic_name -a delalias6=$str_ip_alias6
fi
done
log_info "configeth on $NODE run command: chdev -l $str_nic_name -a netaddr='' -a netmask='' -a netaddr6='' -a prefixlen='' -a state=down"
chdev -l $str_nic_name -a netaddr='' -a netmask='' -a netaddr6='' -a prefixlen='' -a state=down
else
#shut down the nic if it is on
ip link show $str_nic_name | grep -i ',up'
if [ $? -eq 0 ];then
if [ "$str_os_type" = "debian" ];then
ifdown --force $str_nic_name
else
ifdown $str_nic_name
fi
fi
#delete the configuration files
delete_nic_config_files $str_nic_name
fi
exit $error_code
elif [ "$1" = "-s" ];then
if [ $# -lt 2 ];then
log_error "configeth on $NODE: config install nic, but the nic name is missed"
exit 1
fi
str_inst_nic=$2
str_inst_ip=''
str_inst_mask=''
str_inst_gateway=''
str_inst_mtu=''
if [ "$str_os_type" = "aix" ];then
log_error "configeth on $NODE: aix does not support -s flag"
exit 1
elif [ -f "/etc/debian_version" ];then
str_lease_file="/var/lib/dhcp/dhclient."$str_inst_nic".leases"
if [ -e "$str_lease_file" ];then
str_inst_ip=`grep fixed-address $str_lease_file | tail -n 1 | awk '{print $2}' | sed 's/;$//'`
str_inst_mask=`grep subnet-mask $str_lease_file | tail -n 1 | awk '{print $3}' | sed 's/;$//'`
str_inst_gateway=`grep routers $str_lease_file | tail -n 1 | awk '{print $3}' | sed 's/;$//'`
else
if [ -n "$MACADDRESS" ];then
str_inst_mac=$MACADDRESS
inst_nic=`ip -o link |grep -i ${str_inst_mac} |awk '{print $2}'|sed 's/://g'`
if [ ! -z "${inst_nic}" ];then
str_inst_ip=`ip -4 -o addr|grep -i ${inst_nic} |awk '{print $4}'|awk -F/ '{print $1}'`
if [ ! -z "$str_inst_ip" ];then
inst_prefix=`ip ro ls|grep -i ${str_inst_ip}|awk '{print $1}'|awk -F/ '{print $2}'`
if [ ! -z "$inst_prefix" ];then
str_inst_mask=`v4prefix2mask $inst_prefix`
fi
fi
fi
str_inst_gateway=`ip ro ls|grep default|awk '{print $3}'|head -1`
fi
fi
elif [ -f "/etc/SuSE-release" ];then
str_lease_file="/var/lib/dhcpcd/dhcpcd-"$str_inst_nic".info"
if [ -e "$str_lease_file" ];then
str_inst_ip=`grep IPADDR $str_lease_file | tail -n 1 | awk -F'=' '{print $2}' | sed "s/'//g"`
str_inst_mask=`grep NETMASK $str_lease_file | tail -n 1 | awk -F'=' '{print $2}' | sed "s/'//g"`
str_inst_gateway=`grep GATEWAYS $str_lease_file | tail -n 1 | awk -F'=' '{print $2}' | sed "s/'//g"`
else
if [ -n "$MACADDRESS" ];then
str_inst_mac=$MACADDRESS
inst_nic=`ip -o link |grep -i ${str_inst_mac} |awk '{print $2}'|sed 's/://g'`
if [ ! -z "${inst_nic}" ];then
str_inst_ip=`ip -4 -o addr|grep -i ${inst_nic} |awk '{print $4}'|awk -F/ '{print $1}'`
if [ ! -z "$str_inst_ip" ];then
inst_prefix=`ip ro ls|grep -i ${str_inst_ip}|awk '{print $1}'|awk -F/ '{print $2}'`
if [ ! -z "$inst_prefix" ];then
str_inst_mask=`v4prefix2mask $inst_prefix`
fi
fi
fi
str_inst_gateway=`ip ro ls|grep default|awk '{print $3}'|head -1`
echo "str_inst_gateway is $str_inst_gateway"
fi
fi
else
str_lease_file=`ls /var/lib/dhclient/*$str_inst_nic* | grep lease`
if [ -e "$str_lease_file" ];then
str_inst_ip=`grep fixed-address $str_lease_file | tail -n 1 | awk '{print $2}' | sed 's/;$//'`
str_inst_mask=`grep subnet-mask $str_lease_file | tail -n 1 | awk '{print $3}' | sed 's/;$//'`
str_inst_gateway=`grep routers $str_lease_file | tail -n 1 | awk '{print $3}' | sed 's/;$//'`
else
if [ -n "$MACADDRESS" ];then
str_inst_mac=$MACADDRESS
inst_nic=`ip -o link |grep -i ${str_inst_mac} |awk '{print $2}'|sed 's/://g'`
if [ ! -z "${inst_nic}" ];then
str_inst_ip=`ip -4 -o addr|grep -i ${inst_nic} |awk '{print $4}'|awk -F/ '{print $1}'`
if [ ! -z "$str_inst_ip" ];then
inst_prefix=`ip ro ls|grep -i ${str_inst_ip}|awk '{print $1}'|awk -F/ '{print $2}'`
if [ ! -z "$inst_prefix" ];then
str_inst_mask=`v4prefix2mask $inst_prefix`
fi
fi
fi
str_inst_gateway=`ip ro ls|grep default|awk '{print $3}'|head -1`
fi
fi
fi
if [ -n "$IPADDR" ];then
str_inst_ip=$IPADDR
fi
if [ -n "$MACADDRESS" ];then
str_inst_mac=$MACADDRESS
else
#str_inst_mac=`ifconfig $str_inst_nic | grep HWaddr | awk -F'HWaddr' '{print $2}' | sed 's/\s*//'`
str_inst_mac=`ip link show $netdev | grep ether | awk '{print $2}'`
fi
if [ -z "$str_inst_ip" -o -z "$str_inst_mask" ];then
log_info "configeth on $NODE: config install nic, can not find information from dhcp lease file, return."
exit 1
fi
str_inst_net=$(v4calcnet $str_inst_ip $str_inst_mask)
num_index=1
while [ $num_index -le $NETWORKS_LINES ];do
eval str_tmp=\$NETWORKS_LINE$num_index
str_tmp_name=`echo $str_tmp | awk -F'net=' '{print $2}' | awk -F'|' '{print $1}'`
if [ "$str_tmp_name" = "$str_inst_net" ];then
str_inst_mtu=`echo $str_tmp | awk -F'mtu=' '{print $2}' | awk -F'|' '{print $1}'`
break
fi
num_index=$((num_index+1))
done
#get extra configration parameters for each nic
#echo "str_inst_nic=$str_inst_nic, str_inst_ip=$str_inst_ip"
get_nic_extra_params $str_inst_nic "$NICEXTRAPARAMS"
if [ ${#array_nic_params[@]} -gt 0 ]; then
str_extra_params=${array_nic_params[0]}
parse_nic_extra_params "$str_extra_params"
fi
# cofniguring the interface
if [ -f "/etc/debian_version" ];then
str_conf_file="/etc/network/interfaces.d/${str_inst_nic}"
echo "auto ${str_inst_nic}" > $str_conf_file
echo "iface ${str_inst_nic} inet static" >> $str_conf_file
echo " address ${str_inst_ip}" >> $str_conf_file
echo " netmask ${str_inst_mask}" >> $str_conf_file
echo " hwaddress ether ${str_inst_mac}" >> $str_conf_file
if [ -n "${str_inst_mtu}" ];then
echo " mtu ${str_inst_mtu}" >> $str_conf_file
fi
if [ -n "$str_inst_gateway" ];then
echo " gateway $str_inst_gateway" >> $str_conf_file
fi
#add extra params
i=0
while [ $i -lt ${#array_extra_param_names[@]} ]
do
name="${array_extra_param_names[$i]}"
value="${array_extra_param_values[$i]}"
echo " $i: name=$name value=$value"
echo " ${name} ${value}" >> $str_conf_file
i=$((i+1))
done
hostname $NODE
echo $NODE > /etc/hostname
elif [ -f "/etc/SuSE-release" ];then
str_conf_file="/etc/sysconfig/network/ifcfg-${str_inst_nic}"
echo "DEVICE=${str_inst_nic}" > $str_conf_file
echo "BOOTPROTO=static" >> $str_conf_file
echo "IPADDR=${str_inst_ip}" >> $str_conf_file
echo "NETMASK=${str_inst_mask}" >> $str_conf_file
echo "HWADDR=${str_inst_mac}" >> $str_conf_file
if [ -n "${str_inst_mtu}" ];then
echo "MTU=${str_inst_mtu}" >> $str_conf_file
fi
echo "STARTMODE=onboot" >> $str_conf_file
if [ -n "$str_inst_gateway" ];then
grep -i "default" /etc/sysconfig/network/routes
if [ $? -eq 0 ];then
sed -i "s/.*default.*/default ${str_inst_gateway} - -/i" /etc/sysconfig/network/routes
else
echo "default ${str_inst_gateway} - -" >> /etc/sysconfig/network/routes
fi
fi
#add extra params
i=0
while [ $i -lt ${#array_extra_param_names[@]} ]
do
name="${array_extra_param_names[$i]}"
value="${array_extra_param_values[$i]}"
echo " $i: name=$name value=$value"
echo "${name}=${value}" >> $str_conf_file
i=$((i+1))
done
hostname $NODE
echo $NODE > /etc/HOSTNAME
else
#write ifcfg-* file for redhat
con_name=""
str_inst_prefix=$(v4mask2prefix ${str_inst_mask})
str_conf_file="/etc/sysconfig/network-scripts/ifcfg-${str_inst_nic}"
if [ $networkmanager_active -eq 1 ]; then
con_name=$(nmcli dev show ${str_if_name}|grep CONNECTION|awk -F: '{print $2}'|sed 's/^[ \t]*$//g')
if [ "$con_name" == "--" ] ; then
nmcli con add type ethernet con-name ${str_inst_nic} ifname ${str_inst_nic} ipv4.method manual ipv4.addresses ${str_inst_ip}/${str_inst_prefix}
else
nmcli con mod "System ${str_inst_nic}" ipv4.method manual ipv4.addresses ${str_inst_ip}/${str_inst_prefix}
fi
else
echo "DEVICE=${str_inst_nic}" > $str_conf_file
echo "IPADDR=${str_inst_ip}" >> $str_conf_file
echo "NETMASK=${str_inst_mask}" >> $str_conf_file
echo "BOOTPROTO=none" >> $str_conf_file
echo "ONBOOT=yes" >> $str_conf_file
echo "HWADDR=${str_inst_mac}" >> $str_conf_file
fi
if [ -n "${str_inst_mtu}" ];then
echo "MTU=${str_inst_mtu}" >> $str_conf_file
fi
if [ -n "$str_inst_gateway" ];then
grep -i "GATEWAY" /etc/sysconfig/network
if [ $? -eq 0 ];then
sed -i "s/.*GATEWAY.*/GATEWAY=${str_inst_gateway}/i" /etc/sysconfig/network
else
echo "GATEWAY=${str_inst_gateway}" >> /etc/sysconfig/network
fi
fi
#add extra params
i=0
while [ $i -lt ${#array_extra_param_names[@]} ]
do
name="${array_extra_param_names[$i]}"
value="${array_extra_param_values[$i]}"
echo " $i: name=$name value=$value"
echo "${name}=${value}" >> $str_conf_file
i=$((i+1))
done
hostname $NODE
if [ -f "/etc/hostname" ]; then
echo $NODE > /etc/hostname
else
grep -i "HOSTNAME" /etc/sysconfig/network
if [ $? -eq 0 ];then
sed -i "s/.*HOSTNAME.*/HOSTNAME=${NODE}/i" /etc/sysconfig/network
else
echo "HOSTNAME=${NODE}" >> /etc/sysconfig/network
fi
fi
fi
if [ "$UPDATENODE" = "1" ] || [ "$NODESETSTATE" = "netboot" ] || [ "$NODESETSTATE" = "statelite" ] || grep "REBOOT=TRUE" /opt/xcat/xcatinfo >/dev/null 2>&1; then
if [ "$str_os_type" = "debian" ];then
ifdown --force $str_inst_nic
else
ifdown $str_inst_nic
fi
ifup $str_inst_nic
fi
if [ $? -ne 0 ]; then
log_error "ifup $str_inst_nic failed."
error_code=1
fi
exit $error_code
fi
#main prcess
#1. get all ip,netmask,subnet,gateway for the nic
#2. get current configurations
#3. delete the undefined ips
#4. add the new defined ips
#5. no modification, return directly
#3. on linux modify the configuration files
if [ $# -ne 3 ];then
log_error "configeth on $NODE: paramters error currently is $@"
exit 1
fi
str_nic_name=$1
old_ifs=$IFS
IFS=$'|'
array_nic_ips=($2)
array_nic_networks=($3)
IFS=$old_ifs
if [ "$str_os_type" = "aix" ];then
str_temp=`lsattr -El $str_nic_name`
else
str_temp=`ip addr show dev $str_nic_name`
fi
logger -t $log_label -p local4.err "configeth: old configuration: $str_temp"
echo "configeth on $NODE: old configuration: $str_temp"
#parse the networks tables contains
declare -a array_ip_mask
declare -a array_ip_status
declare -a array_nic_network_config
declare -a array_nic_subnet
declare -a array_nic_netmask
declare -a array_nic_gateway
declare -a array_nic_mtu
#get extra configration parameters for each nic
get_nic_extra_params $str_nic_name "$NICEXTRAPARAMS"
j=0
while [ $j -lt ${#array_nic_params[@]} ]
do
token1="${array_nic_params[$j]}"
echo "array_nic_params $j=$token1"
j=$((j+1))
done
str_ip_mask_pair=''
num_index=1
while [ $num_index -le $NETWORKS_LINES ];do
eval str_temp=\$NETWORKS_LINE$num_index
str_temp_name=`echo $str_temp | awk -F'netname=' '{print $2}' | awk -F'|' '{print $1}'`
num_i=0
while [ $num_i -lt ${#array_nic_ips[*]} ]
do
if [ "$str_temp_name" = "${array_nic_networks[$num_i]}" ];then
array_nic_network_config[$num_i]=$str_temp
break
fi
num_i=$((num_i+1))
done
num_index=$((num_index+1))
done
log_info "configeth on $NODE: new configuration"
num_index=0
str_ipv6_gateway=''
while [ $num_index -lt ${#array_nic_ips[*]} ];do
#get the ip address and network name
str_ip=${array_nic_ips[$num_index]}
str_netname=${array_nic_networks[$num_index]}
if [ ! $str_netname ];then
log_error "configeth on $NODE: Network name is not defined on $str_nic_name for $str_ip."
error_code=1
num_index=$((num_index+1))
continue
fi
#find out the network definition
str_line=${array_nic_network_config[$num_index]}
if [ ! $str_line ];then
log_error "configeth on $NODE: Network object $str_netname is not defined."
error_code=1
num_index=$((num_index+1))
continue
fi
#fetch the subnet and netmask in networks definition
str_subnet=`echo $str_line | awk -F'net=' '{print $2}' | awk -F'|' '{print $1}'`
str_netmask=`echo $str_line | awk -F'mask=' '{print $2}' | awk -F'|' '{print $1}' | sed 's:^/::'`
str_gateway=`echo $str_line | awk -F'gateway=' '{print $2}' | awk -F'|' '{print $1}'`
str_mtu=`echo $str_line | awk -F'mtu=' '{print $2}' | awk -F'|' '{print $1}'`
if [ ! $str_subnet -o ! $str_netmask ];then
log_error "configeth on $NODE: subnet or netmask is not defined in network object $str_netname."
error_code=1
num_index=$((num_index+1))
continue
fi
array_nic_subnet[$num_index]=$str_subnet
array_nic_netmask[$num_index]=$str_netmask
array_nic_gateway[$num_index]=$str_gateway
array_nic_mtu[$num_index]=$str_mtu
echo "$str_gateway" | grep ':'
if [ $? -eq 0 ];then
str_ipv6_gateway=$str_gateway
fi
logger -t $log_label -p local4.info "configeth: $str_ip, $str_subnet, $str_netmask, $str_gateway"
echo " $str_ip, $str_subnet, $str_netmask, $str_gateway"
#on linux, call sub rutine to define ipv4 or ipv6 address for the persitent configuration
if [ `echo $str_ip | grep -E '^([0-9]{1,3}\.){3}[0-9]{1,3}$'` ];then
if [ "$str_os_type" = "aix" ];then
hashset hash_new_config "${str_ip}_${str_netmask}" "new"
str_ip_mask_pair=$str_ip_mask_pair"${str_ip}_${str_netmask} "
else
str_prefix=$(v4mask2prefix $str_netmask)
hashset hash_new_config "${str_ip}_${str_prefix}" "new"
str_ip_mask_pair=$str_ip_mask_pair"${str_ip}_${str_prefix} "
fi
elif [ `echo $str_ip | grep -E ":"` ];then
num_ipv6_index=$((num_ipv6_index+1))
hashset hash_new_config "${str_ip}_${str_netmask}" "new"
str_ip_mask_pair=$str_ip_mask_pair"${str_ip}_${str_netmask} "
else
log_error "configeth on $NODE: the ipaddress( $str_ip ) for $str_nic_name is invalid."
error_code=1
fi
num_index=$((num_index+1))
done
str_ip_mask_pair=`echo "$str_ip_mask_pair" | sed -e 's/ $//'`
str_old_conf=''
if [ "$str_os_type" = "aix" ];then
#check the netaddr
str_history=`lsattr -El $str_nic_name | grep netaddr | awk '{print $2}' | grep '\.'`
if [ $? -eq 0 ];then
str_temp=`lsattr -El $str_nic_name | grep netmask | awk '{print $2}'`
str_old_ip=${str_history}"_"${str_temp}
str_ip_status=$(hashget hash_new_config $str_old_ip)
if [ -n "$str_ip_status" ];then
hashset hash_new_config $str_old_ip "old"
else
chdev -l $str_nic_name -a netaddr='' -a netmask=''
log_error "configeth on $NODE: delete undefined ip address $str_old_ip"
error_code=1
fi
fi
#check the netaddr6
str_history=`lsattr -El $str_nic_name | grep netaddr6 | awk '{print $2}' | grep ':'`
if [ $? -eq 0 ];then
str_temp=`lsattr -El $str_nic_name | grep prefixlen | awk '{print $2}'`
str_old_ip=${str_history}"_"${str_temp}
str_ip_status=$(hashget hash_new_config $str_old_ip)
if [ -n "$str_ip_status" ];then
hashset hash_new_config $str_old_ip "old"
else
chdev -l $str_nic_name -a netaddr6='' -a prefixlen=''
log_error "configeth on $NODE: delete undefined ipv6 address $str_old_ip"
fi
fi
#check the ipv4 alias
str_history=`lsattr -El $str_nic_name | grep alias4 | awk '{print $2}' | grep '\.'`
if [ $? -eq 0 ];then
old_ifs=$IFS
IFS=$'\n'
array_alias4_temp=($str_history)
IFS=$old_ifs
for str_temp in ${array_alias4_temp[@]}
do
str_old_ip=`echo $str_temp | tr ',' '_'`
str_ip_staus=$(hashget hash_new_config $str_old_ip)
if [ -n "$str_ip_staus" ];then
hashset hash_new_config $str_old_ip "old"
else
chdev -l $str_nic_name -a delalias4=$str_temp
fi
done
fi
#check the ipv6 alias
str_history=`lsattr -El $str_nic_name | grep alias6 | awk '{print $2}' | grep '\.'`
if [ $? -eq 0 ];then
old_ifs=$IFS
IFS=$'\n'
array_alias6_temp=($str_history)
IFS=$old_ifs
for str_temp in ${array_alias6_temp[@]}
do
str_old_ip=`$str_temp | tr '/' '_'`
str_ip_staus=$(hashget hash_new_config $str_old_ip)
if [ -n "$str_ip_staus" ];then
hashset hash_new_config $str_old_ip "old"
else
chdev -l $str_nic_name -a delalias6=$str_temp
fi
done
fi
#add the new configured ip address
old_ifs=$IFS
IFS=$' '
array_ip_mask_temp=($str_ip_mask_pair)
IFS=$old_ifs
for str_new_ip in ${array_ip_mask_temp[@]}
do
str_ip_status=$(hashget hash_new_config $str_new_ip)
if [ "$str_ip_status" = "new" ];then
log_info "configeth on $NODE: add $str_new_ip for $str_nic_name temporary."
add_ip_temporary $str_new_ip $str_nic_name
fi
done
#change the nic status to up
chdev -l $str_nic_name -a state=up
if [ $? -ne 0 ]; then
log_error "chdev -l $str_nic_name -a state=up failed."
error_code=1
fi
else
str_history=''
bool_restart_flag=0
bool_modify_flag=0
str_nic_status='down'
str_his_file=${str_cfg_dir}xcat_history_important
str_history=`ip addr show dev $str_nic_name | grep inet | grep -iv dynamic | grep -iv link | grep $str_nic_name | awk '{print $2}'`
old_ifs=$IFS
IFS=$'\n'
array_ip_old_temp=($str_history)
IFS=$old_ifs
ip link show dev $str_nic_name | grep -i ,up
if [ $? -eq 0 ];then
str_nic_status='up'
if [ -f "${str_his_file}" ];then
cat ${str_his_file} | grep $str_nic_name
if [ $? -eq 0 ];then
str_nic_status='up'
for str_old_ip in ${array_ip_old_temp[@]}
do
str_old_ip=`echo $str_old_ip | tr '/' '_'`
str_ip_staus=$(hashget hash_new_config $str_old_ip)
if [ -n "$str_ip_staus" ];then
hashset hash_new_config $str_old_ip "old"
else
bool_modify_flag=1
log_info "configeth on $NODE: delete $str_old_ip for $str_nic_name temporary."
str_old_ip=`echo $str_old_ip | tr '_' '/'`
ip addr del $str_old_ip dev $str_nic_name
if [ $? -ne 0 ]; then
log_error "ip addr del $str_old_ip dev $str_nic_name failed."
error_code=1
fi
fi
done
else
bool_restart_flag=1
bool_modify_flag=1
fi
else
bool_restart_flag=1
bool_modify_flag=1
fi
else
bool_restart_flag=1
bool_modify_flag=1
fi
#check if there are extra param values have been set or not
#if set, always restart the the nic
if [ ${#array_nic_params[@]} -gt 0 ]; then
bool_restart_flag=1
bool_modify_flag=1
fi
if [ $bool_restart_flag = 0 ];then
#add the new defined ip
old_ifs=$IFS
IFS=$' '
array_ip_mask_temp=($str_ip_mask_pair)
IFS=$old_ifs
for str_new_ip in ${array_ip_mask_temp[@]}
do
str_ip_status=$(hashget hash_new_config $str_new_ip)
if [ "$str_ip_status" = "new" ];then
bool_modify_flag=1
if [ $bool_restart_flag -eq 0 ];then
log_info "configeth on $NODE: add $str_new_ip for $str_nic_name temporary."
add_ip_temporary $str_new_ip $str_nic_name
fi
fi
done
fi
#configure the ipv6 default route
if [ $bool_restart_flag -eq 0 -a -n "$str_ipv6_gateway" ];then
ip -6 route | grep default | grep $str_ipv6_gateway
if [ $? -ne 0 ];then
log_info "configeth on $NODE: the default ipv6 route changes to $str_ipv6_gateway."
ip -6 route del default
ip -6 route add default $str_ipv6_gateway dev $str_dev_name
fi
fi
#modify the configuration files