-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvminit.sh
1557 lines (1315 loc) · 39.9 KB
/
vminit.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
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/sh
# Target: Automatic configuration hostname,password,network,datastore and install cloudsafe client for VMware virtual machine.
# Application platform: CentOS FreeBSD Ubuntu Debian OpenSUSE
# Update date: 2017/11/9
# Version: 1.94
HN_MOD={HN_MOD} # hostname
PS_MOD={PS_MOD} # password
IP_MOD={IP_MOD} # ipaddress
MS_MOD={MS_MOD} # netmask
GW_MOD={GW_MOD} # gateway
DN_MOD={DN_MOD} # dnsserver
FM_MOD={FM_MOD} # whether partition
# Examples like this
#HN_MOD=niaoyun10000
#PS_MOD=niaoyun.0
#IP_MOD=192.168.80.203,10.98.80.203
#MS_MOD=255.255.255.0,255.255.255.0
#GW_MOD=192.168.80.1
#DN_MOD=202.96.134.133,114.114.114.114
#FM_MOD=YES
# system parameter
ch_m=0
ci_m=0
home="/opt/vminit"
log=$home/vminit.log
tmp=$home/vminit.tmp
auto_tmp=$home/autovm.tmp
exec 1>$log 2>&1
echo "$(date '+%Y-%m-%d %H:%M:%S') vminit script starting run" !
# For CentOS or Red Hat System
CentOS ()
{
# parameters: filename
time_stamp ()
{
if [ $1 ]
then
mod_date=`stat $1 | grep "^Modify" | awk -F. '{print $1}' | awk '{print $2$3}'| awk -F- '{print $1$2$3}'`
mod_pre=`echo $mod_date | awk -F: '{print $1}'`
mod_minute=`echo $mod_date | awk -F: '{print $2}'`
now_pre=`date +%Y%m%d%H`
now_minute=`date +%M`
if [ $mod_pre == $now_pre ]
then
if [ $mod_minute == $now_minute ]
then
mod=1
elif [ $(expr $mod_minute + 1) == $now_minute ]
then
mod=1
else
mod=0
fi
else
mod=0
fi
else
echo "Error,No files have been specified" ! && exit 1
fi
}
# parameters: nicname ipaddress netmask gateway
network_parameter ()
{
echo $1 $2 $3 $4
if [ $# -lt 3 ]
then
echo "error: config_network parameter less than 3" !
return 1
fi
local network_file=/etc/sysconfig/network-scripts/ifcfg-$1
if [ $(grep -c '^DNS' $network_file) -ge 1 ]
then
sed -i '/DNS/d' $network_file
fi
if [ $(grep '^DEVICE' $network_file | grep -c "${1}") -eq 0 ] && [ ! "${1}" == "" ]
then
sed -i '/DEVICE/d' $network_file
echo DEVICE=${1} >> $network_file
fi
macaddress=`cat /sys/class/net/$1/address | tr '[a-z]' '[A-Z]'`
if [ $macaddress ] && [ $(grep '^HWADDR' $network_file | grep -c $macaddress) -eq 0 ]
then
sed -i '/HWADDR/d' $network_file
echo HWADDR=$macaddress >> $network_file
fi
if [ $(grep '^TYPE' $network_file | grep -c "Ethernet") -eq 0 ]
then
sed -i '/TYPE/d' $network_file
echo "TYPE=Ethernet" >> $network_file
fi
if [ $(grep '^ONBOOT' $network_file | grep -c 'yes') -eq 0 ]
then
sed -i '/ONBOOT/d' $network_file
echo "ONBOOT=yes" >> $network_file
fi
if [ $(grep '^BOOTPROTO' $network_file | grep -c 'static') -eq 0 ]
then
sed -i '/BOOTPROTO/d' $network_file
echo "BOOTPROTO=static" >> $network_file
fi
if [ $(grep '^NM_CONTROLLED' $network_file | grep -c 'no') -eq 0 ]
then
sed -i '/NM_CONTROLLED/d' $network_file
echo 'NM_CONTROLLED=no' >> $network_file
fi
if [ $(grep '^PEERDNS' $network_file | grep -c 'no') -eq 0 ]
then
sed -i '/PEERDNS/d' $network_file
echo 'PEERDNS=no' >> $network_file
fi
if [ ! "${2}" == "" ]
then
local curr_ip=`grep '^IPADDR' $network_file | grep -oP '\d+\.\d+\.\d+\.\d+'`
if [ ! "${curr_ip}" == "" ]
then
echo "${1}:current ipaddress is ${curr_ip}" !
if [ ! "${curr_ip}" == "${2}" ]
then
sed -i '/IPADDR/d' $network_file
echo IPADDR=${2} >> $network_file
echo "configuration ipaddress for network interface $1 success" !
fi
else
sed -i '/IPADDR/d' $network_file
echo IPADDR=${2} >> $network_file
echo "configuration ipaddress for network interface $1 success" !
fi
fi
if [ ! "${3}" == "" ]
then
local curr_netmask=`grep '^NETMASK' $network_file | grep -oP '\d+\.\d+\.\d+\.\d+'`
if [ ! "${curr_netmask}" == "" ]
then
echo "${1}:current netmask is ${curr_netmask}" !
if [ ! "${curr_netmask}" == "${3}" ]
then
sed -i '/NETMASK/d' $network_file
echo NETMASK=${3} >> $network_file
echo "configuration netmask for network interface $1 success" !
fi
else
sed -i '/NETMASK/d' $network_file
echo NETMASK=${3} >> $network_file
echo "configuration netmask for network interface $1 success" !
fi
fi
if [ ! "${4}" == "" ]
then
local curr_gateway=`grep '^GATEWAY' $network_file | grep -oP '\d+\.\d+\.\d+\.\d+'`
if [ ! "${curr_gateway}" == "" ]
then
echo "${1}:current gateway is ${curr_gateway}"
if [ ! "${curr_gateway}" == "${4}" ]
then
sed -i '/GATEWAY/d' $network_file
echo GATEWAY=${4} >> $network_file
echo "configuration gateway for network interface $1 success" !
fi
else
sed -i '/GATEWAY/d' $network_file
echo GATEWAY=${4} >> $network_file
echo "configuration gateway for network interface $1 success" !
fi
fi
time_stamp $network_file
[ $mod == 1 ] && ci_m=1
return 0
}
hostname ()
{
if [ $(uname -r | awk -F'-' '{ print $1 }') == "3.10.0" ]
then
local curr_hostname=`cat /etc/hostname`
if [ -n "$HN_MOD" ] && [ "$curr_hostname" != "$HN_MOD" ]
then
echo $HN_MOD > /etc/hostname && echo "hostname chang" !
echo -e "127.0.0.1 $HN_MOD\n::1 $HN_MOD" >> /etc/hosts && echo "hosts file changed" !
ch_m=1
fi
else
local curr_hostname=`grep "^HOSTNAME" /etc/sysconfig/network | awk -F'=' '{ print $2 }' | sed -e 's/"//g'`
if [ -n "$HN_MOD" ] && [ "$curr_hostname" != "$HN_MOD" ]
then
sed -i "s/$curr_hostname/$HN_MOD/" /etc/sysconfig/network
echo -e "127.0.0.1 $HN_MOD\n::1 $HN_MOD" >> /etc/hosts && echo "hosts file changed" !
ch_m=1
fi
fi
}
dnsserver ()
{
if [ ! $DN_MOD == "" ]
then
dns1=`echo $DN_MOD | awk -F "," '{ print $1 }'`
dns2=`echo $DN_MOD | awk -F "," '{ print $2 }'`
dns3=`echo $DN_MOD | awk -F "," '{ print $3 }'`
if [ ! $dns1 == "" ] && [ $(sed -n '/^nameserver/p' /etc/resolv.conf | sed -n 1p | grep -c $dns1) -eq 0 ]
then
echo "nameserver $dns1" > /etc/resolv.conf
echo "configuration dnsserver1 success" !
else
echo "dnsserver1 not change" !
fi
if [ ! $dns2 == "" ]
then
if [ $(grep -c "^nameserver" /etc/resolv.conf) -ge 2 ]
then
if [ $(sed -n '/^nameserver/p' /etc/resolv.conf | sed -n 2p | grep -c $dns2) -eq 0 ]
then
curr_nameserver2=`grep "^nameserver" /etc/resolv.conf | sed -n 2p | awk '{ print $2 }'`
sed -i "s/$curr_nameserver2/$dns2/" /etc/resolv.conf
echo "nameserver2 $curr_nameserver2 existence" !
echo "configuration dnsserver2 success" !
else
echo "dnsserver2 not change" !
fi
else
echo "nameserver $dns2" >> /etc/resolv.conf
echo "configuration dnsserver2 success" !
ci_m=1
fi
fi
if [ ! $dns3 == "" ]
then
if [ $(grep -c "^nameserver" /etc/resolv.conf) -ge 3 ]
then
if [ $(sed -n '/^nameserver/p' /etc/resolv.conf | sed -n 3p | grep -c $dns3) -eq 0 ]
then
curr_nameserver3=`grep "^nameserver" /etc/resolv.conf | sed -n 3p | awk '{ print $2 }'`
sed -i "s/$curr_nameserver3/$dns3/" /etc/resolv.conf
echo "nameserver3 $curr_nameserver3 existence" !
echo "configuration dnsserver3 success" !
else
echo "dnsserver3 not change" !
fi
else
echo "nameserver $dns3" >> /etc/resolv.conf
echo "configuration dnsserver3 success" !
fi
fi
chmod 644 /etc/resolv.conf
if [ -f /etc/NetworkManager/NetworkManager.conf ]
then
if [ $(grep '^dns' /etc/NetworkManager/NetworkManager.conf | grep -c 'none') -eq 0 ]
then
echo "dns=none" >> /etc/NetworkManager/NetworkManager.conf
fi
fi
fi
}
password ()
{
if [ "$PS_MOD" ]
then
if [ -f $auto_tmp ]
then
. $auto_tmp
if [ "$PS_MOD_LAST" ]
then
if [ ! "$PS_MOD" == "$PS_MOD_LAST" ]
then
echo "root:$PS_MOD" | chpasswd && echo "password changed success" !
else
echo "password not change" !
fi
else
echo "root:$PS_MOD" | chpasswd && echo "password changed success" !
fi
else
echo "root:$PS_MOD" | chpasswd && echo "password changed success" !
fi
fi
}
restart_service ()
{
if [ "$ci_m" -eq 1 ]
then
/etc/init.d/network restart
fi
if [ "$ch_m" -eq 1 ]
then
reboot && echo "$(date '+%Y-%m-%d %H:%M:%S') system will reboot now" !
fi
echo "restart service success" !
}
datastore ()
{
kernel_version=`uname -r | awk -F'-' '{ print $1 }'`
if [ "$kernel_version" == "2.6.18" ]
then
filesystem="ext3"
elif [ "$kernel_version" == "2.6.32" ]
then
filesystem="ext4"
elif [ "$kernel_version" == "3.10.0" ]
then
filesystem="ext4"
fi
if [ "$FM_MOD" == "YES" ]
then
if [ -b /dev/sdb ]
then
if [ -b /dev/sdb1 ]
then
if [ $(/sbin/blkid /dev/sdb1 | egrep -c 'ext2|ext3|ext4|xfs') -ge 1 ]
then
filesystem=`/sbin/blkid /dev/sdb1 | awk '{ print $3 }' | awk -F'=' '{ print $2 }' | sed -e 's/"//g'`
else
[ -b /dev/sdb1 ] && /sbin/parted -s /dev/sdb rm 1
/sbin/parted -s /dev/sdb mklabel msdos
/sbin/parted -s /dev/sdb mkpart primary 0% 100%
mkfs -t $filesystem /dev/sdb1 || mkfs -t $filesystem -f /dev/sdb1
fi
else
/sbin/parted -s /dev/sdb mklabel msdos
/sbin/parted -s /dev/sdb mkpart primary 0% 100%
mkfs -t $filesystem /dev/sdb1 && sleep 10
fi
if [ $(mount | grep -c 'sdb1') -eq 0 ]
then
[ -d /data ] || mkdir /data
mount -t $filesystem /dev/sdb1 /data
sed -i '/sdb1/d' /etc/fstab
echo "/dev/sdb1 /data $filesystem defaults 0 0" >> /etc/fstab
echo "mount /dev/sdb1 disk success" !
fi
fi
else
if [ -b /dev/sdb1 ] && [ $(/sbin/blkid /dev/sdb1 | egrep -c 'ext2|ext3|ext4|xfs') -ge 1 ]
then
if [ $(mount | grep -c 'sdb1') -eq 0 ]
then
[ -d /data ] || mkdir /data
mount /dev/sdb1 /data && "mount /dev/sdb1 disk success" !
if [ $(mount | grep -c 'sdb1') -ge 1 ]
then
sed -i '/sdb1/d' /etc/fstab
filesystem=`/sbin/blkid /dev/sdb1 | awk '{ print $3 }' | awk -F'=' '{ print $2 }' | sed -e 's/"//g'`
echo "/dev/sdb1 /data $filesystem defaults 0 0" >> /etc/fstab
else
echo "mount /dev/sdb1 disk failed" !
fi
fi
else
echo "can not find correct filesystem on /dev/sdb1" !!
fi
fi
}
# parameters: ipaddress_lan
static_route ()
{
if [ ! "$1" == "" ]
then
IP_123=`echo $1 | awk -F "." '{printf("%d.%d.%d.",$1,$2,$3)}'`
IP_4=`echo $1 | awk -F "." '{printf("%d", $4)}'`
if [ $IP_4 -ge 2 ] && [ $IP_4 -le 61 ]
then
IP_4="1"
elif [ $IP_4 -ge 66 ] && [ $IP_4 -le 125 ]
then
IP_4="65"
elif [ $IP_4 -ge 130 ] && [ $IP_4 -le 189 ]
then
IP_4="129"
elif [ $IP_4 -ge 194 ] && [ $IP_4 -le 253 ]
then
IP_4="193"
fi
local route_lan_num=`grep -c "^any net 10.0.0.0 netmask 255.0.0.0" /etc/sysconfig/static-routes`
if [ ! "$route_lan_num" == "" ]
then
if [ $route_lan_num -eq 0 ]
then
echo "any net 10.0.0.0 netmask 255.0.0.0 gw $IP_123$IP_4" >> /etc/sysconfig/static-routes
ci_m=1
else
sed -i "/any net 10.0.0.0 netmask 255.0.0.0/d" /etc/sysconfig/static-routes
echo "any net 10.0.0.0 netmask 255.0.0.0 gw $IP_123$IP_4" >> /etc/sysconfig/static-routes
ci_m=1
fi
else
echo "any net 10.0.0.0 netmask 255.0.0.0 gw $IP_123$IP_4" > /etc/sysconfig/static-routes
fi
fi
}
network ()
{
if [ ! "$IP_MOD" == "" ]
then
ip_wlan=`echo $IP_MOD | awk -F "," '{printf $1}'`
ip_lan=`echo $IP_MOD | awk -F "," '{printf $2}'`
fi
if [ ! "$MS_MOD" == "" ]
then
netmask_wlan=`echo $MS_MOD | awk -F "," '{printf $1}'`
netmask_lan=`echo $MS_MOD | awk -F "," '{printf $2}'`
fi
dev_array=`ip -o link | grep '\<link/ether\>' | awk -F ": " '{printf("%s ", $2)}'`
dev_count=$(i=0;for j in $dev_array;do i=`expr $i + 1`;done;echo $i)
dev_wlan=`echo $dev_array | awk '{ print $1 }'`
echo "network interface $dev_wlan existence" !
if [ -n "${dev_wlan}" ] && [ -n "${ip_wlan}" ] && [ -n "${netmask_wlan}" ] && [ -n "${GW_MOD}" ]
then
network_parameter $dev_wlan $ip_wlan $netmask_wlan $GW_MOD
fi
if [ $dev_count -ge 2 ]
then
dev_lan=`echo $dev_array | awk '{ print $2 }'`
echo "network interface $dev_lan existence" !
if [ -n "${dev_lan}" ] && [ -n "${ip_lan}" ] && [ -n "${netmask_lan}" ]
then
network_parameter $dev_lan $ip_lan $netmask_lan
static_route $ip_lan
fi
fi
}
hostname
network
password
datastore
restart_service
dnsserver
}
# For FreeBSD System
FreeBSD ()
{
# parameters: nicname ipaddress netmask gateway
network_parameter ()
{
curr_ip=`grep '^ifconfig' /etc/rc.conf | grep $1 | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | sed -n 1p`
curr_netmask=`grep '^ifconfig' /etc/rc.conf | grep $1 | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | sed -n 2p`
curr_gateway=`grep '^defaultrouter' /etc/rc.conf | grep $4 | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'`
if [ ! "${1}" == "" ] && [ ! "${2}" == "" ] && [ ! "${3}" == "" ]
then
if [ ! "${curr_ip}" == "" ] && [ ! "${curr_netmask}" == "" ]
then
if [ ! "${curr_ip}" == "${2}" ] && [ ! "${curr_netmask}" == "${3}" ]
then
sed -e "/ifconfig_${1}/d" /etc/rc.conf > $tmp
echo ifconfig_${1}=\"inet ${2} netmask ${3}\" >> $tmp
cat $tmp > /etc/rc.conf
echo "configuration ipaddress and netmask for network interface $1 success" !
ci_m=1
fi
echo "${1}:current ipaddress is ${curr_ip}" !
echo "${1}:current netmask is ${curr_netmask}" !
else
echo ifconfig_${1}=\"inet ${2} netmask ${3}\" >> /etc/rc.conf
echo "configuration ipaddress and netmask for network interface $1 success" !
ci_m=1
fi
fi
if [ ! "${4}" == "" ]
then
if [ ! "${curr_gateway}" == "" ]
then
if [ ! "${curr_gateway}" == "${4}" ]
then
sed -e "/defaultrouter/d" /etc/rc.conf > $tmp
echo defaultrouter=\"${4}\" >> $tmp
cat $tmp > /etc/rc.conf
echo "configuration gateway for network interface $1 success" !
ci_m=1
fi
echo "${1}:current gateway is ${curr_gateway}" !
else
echo defaultrouter=\"${4}\" >> /etc/rc.conf
echo "configuration gateway for network interface $1 success" !
ci_m=1
fi
fi
return 0
}
network ()
{
if [ ! $IP_MOD == "" ] && [ ! $MS_MOD == "" ] && [ ! $GW_MOD == "" ]
then
ip_wlan=`echo $IP_MOD | awk -F "," '{printf $1}'`
ip_lan=`echo $IP_MOD | awk -F "," '{printf $2}'`
netmask_wlan=`echo $MS_MOD | awk -F "," '{printf $1}'`
netmask_lan=`echo $MS_MOD | awk -F "," '{printf $2}'`
network_parameter em0 $ip_wlan $netmask_wlan $GW_MOD
echo "configuration network interface em0 success" !
if [ $(/usr/local/bin/lspci | grep -c -i net) -ge 2 ]
then
network_parameter em1 $ip_lan $netmask_lan
echo "configuration network interface em1 success" !
static_route $ip_lan
echo "configuration static route success" !
else
echo "less then two net cards" !
fi
fi
}
hostname ()
{
if [ ! "$HN_MOD" == "" ]
then
HN_PRI=`grep "^hostname" /etc/rc.conf | awk -F'=' '{ print $2 }' | sed -e 's/"//g'`
if [ ! "$HN_PRI" == "" ]
then
if [ "$HN_PRI" != "$HN_MOD" ]
then
sed -e "/hostname/d" /etc/rc.conf > $tmp
echo hostname=\"$HN_MOD\" >> $tmp
cat $tmp > /etc/rc.conf
ch_m=1
echo "hostname changed" !
fi
else
echo hostname=\"$HN_MOD\" >> /etc/rc.conf
ch_m=1
echo "hostname changed" !
fi
else
echo "hostname not change" !
fi
}
dnsserver ()
{
if [ ! $DN_MOD == "" ]
then
dns1=`echo $DN_MOD | awk -F "," '{ print $1 }'`
dns2=`echo $DN_MOD | awk -F "," '{ print $2 }'`
dns3=`echo $DN_MOD | awk -F "," '{ print $3 }'`
if [ ! $dns1 == "" ] && [ $(sed -n '/^nameserver/p' /etc/resolv.conf | sed -n 1p | grep -c $dns1) -eq 0 ]
then
echo "nameserver $dns1" > /etc/resolv.conf
echo "configuration dnsserver1 success" !
else
echo "dnserver1 not change"
fi
if [ ! $dns2 == "" ]
then
if [ $(grep -c "^nameserver" /etc/resolv.conf) -ge 2 ]
then
if [ $(sed -n '/^nameserver/p' /etc/resolv.conf | sed -n 2p | grep -c $dns2) -eq 0 ]
then
curr_nameserver2=`grep "^nameserver" /etc/resolv.conf | sed -n 2p | awk '{ print $2}'`
sed -e "/$curr_nameserver2/d" /etc/resolv.conf > $tmp
echo "nameserver $dns2" >> $tmp
cat $tmp > /etc/resolv.conf
echo "nameserver2 $curr_nameserver2 existence" !
echo "configuration dnsserver2 success" !
else
echo "dnserver2 not change"
fi
else
echo "nameserver $dns2" >> /etc/resolv.conf
echo "configuration dnsserver2 success" !
fi
fi
if [ ! $dns3 == "" ]
then
if [ $(grep -c "^nameserver" /etc/resolv.conf) -ge 3 ]
then
if [ $(sed -n '/^nameserver/p' /etc/resolv.conf | sed -n 3p | grep -c $dns3) -eq 0 ]
then
curr_nameserver3=`grep "^nameserver" /etc/resolv.conf | sed -n 3p | awk '{ print $2}'`
sed -e "/$curr_nameserver3/d" /etc/resolv.conf > $tmp
echo "nameserver $dns3" >> $tmp
cat $tmp > /etc/resolv.conf
echo "nameserver3 $curr_nameserver3 existence" !
echo "configuration dnsserver3 success" !
else
echo "dnserver3 not change"
fi
else
echo "nameserver $dns3" >> /etc/resolv.conf
echo "configuration dnsserver3 success" !
fi
fi
chmod 644 /etc/resolv.conf
fi
}
password ()
{
bsdpasswd ()
{
/usr/local/bin/expect <<- END
spawn passwd root
expect "New Password:"
sleep 5
send "$PS_MOD\r"
expect "Retype New Password:"
sleep 5
send "$PS_MOD\r"
expect eof
END
}
if [ "$PS_MOD" ]
then
if [ -f $auto_tmp ]
then
. $auto_tmp
if [ "$PS_MOD_LAST" ]
then
if [ "$PS_MOD_LAST" != "$PS_MOD" ]
then
bsdpasswd
echo "password changed" !
else
echo "password not change" !
fi
else
bsdpasswd
echo "password changed" !
fi
else
bsdpasswd
echo "password changed" !
fi
else
echo "password not change" !
fi
}
restart_service ()
{
if [ "$ch_m" -eq 1 ]
then
reboot && echo "`date '+%Y-%m-%d %H:%M:%S'` system will reboot now" !
elif [ "$ci_m" == 1 ]
then
/etc/netstart && echo "`date '+%Y-%m-%d %H:%M:%S'` restart service success" !
fi
}
# parameters: ip_lan
static_route ()
{
if [ ! "$1" == "" ]
then
IP_123=`echo $1 | awk -F "." '{printf("%d.%d.%d.",$1,$2,$3)}'`
IP_4=`echo $1 | awk -F "." '{printf("%d", $4)}'`
if [ $IP_4 -ge 2 ] && [ $IP_4 -le 61 ]
then
IP_4="1"
elif [ $IP_4 -ge 66 ] && [ $IP_4 -le 125 ]
then
IP_4="65"
elif [ $IP_4 -ge 130 ] && [ $IP_4 -le 189 ]
then
IP_4="129"
elif [ $IP_4 -ge 194 ] && [ $IP_4 -le 253 ]
then
IP_4="193"
fi
if [ $(grep -c "-net 10.0.0.0/8" /etc/rc.conf) -eq 0 ]
then
cat >> /etc/rc.conf << EOF
# add static route
static_routes="em1"
route_em1="-net 10.0.0.0/8 $IP_123$IP_4"
EOF
else
echo "static route not change" !
fi
fi
}
datastore ()
{
if [ $(mount | grep -c '/data') -eq 0 ] && [ -c /dev/da1 ] && [ "$FM_MOD" = "YES" ]
then
if [ $(grep -c '^zfs_enable' /etc/rc.conf) -eq 0 ]
then
cat /etc/rc.conf > $tmp
echo 'zfs_enable="YES"' >> $tmp
cat $tmp > /etc/rc.conf
fi
/etc/rc.d/zfs restart
zpool create data /dev/da1
zfs create data/data1
zfs set copies=2 data/data1
zfs set compression=gzip data/data1
fi
}
hostname
datastore
network
dnsserver
password
restart_service
}
# For Ubuntu or Debian System
Ubuntu ()
{
# parameters: nicname ipaddress netmask gateway
network_parameter ()
{
if [ "${1}" ]
then
if [ $(grep -c "^auto ${1}" /etc/network/interfaces) -eq 0 ]
then
sed -i "/auto ${1}/d" /etc/network/interfaces
echo "\nauto ${1}" >> /etc/network/interfaces
fi
if [ $(grep "^iface ${1} inet" /etc/network/interfaces | grep -c 'static') -eq 0 ]
then
sed -i "/iface ${1} inet/d" /etc/network/interfaces
echo "iface ${1} inet static" >> /etc/network/interfaces
fi
fi
currl_ip=`grep "iface ${1} inet static" -A10 /etc/network/interfaces | grep -v ^# | grep '^address' | sed -n 1p | grep -oP '\d+\.\d+\.\d+\.\d+'`
currl_netmask=`grep "iface ${1} inet static" -A10 /etc/network/interfaces | grep -v ^# | grep '^netmask' | sed -n 1p | grep -oP '\d+\.\d+\.\d+\.\d+'`
currl_gateway=`grep "iface ${1} inet static" -A10 /etc/network/interfaces | grep -v ^# | grep '^gateway' | sed -n 1p | grep -oP '\d+\.\d+\.\d+\.\d+'`
if [ "${2}" ]
then
if [ "$currl_ip" ]
then
if [ "$currl_ip" != "${2}" ]
then
sed -i "s/$currl_ip/${2}/" /etc/network/interfaces
echo "configuration ipaddress for network interface $1 success" !
ci_m=1
fi
else
sed -i "/iface ${1} inet static/a\address ${2}" /etc/network/interfaces
echo "configuration ipaddress for network interface $1 success" !
ci_m=1
fi
fi
if [ "${3}" ]
then
if [ "$currl_netmask" ]
then
if [ "$currl_netmask" != "${3}" ]
then
sed -i "s/$currl_netmask/${3}/" /etc/network/interfaces
echo "configuration netmask for network interface $1 success" !
ci_m=1
fi
else
sed -i "/address ${2}/a\netmask ${3}" /etc/network/interfaces
echo "configuration netmask for network interface $1 success" !
ci_m=1
fi
fi
if [ "${4}" ]
then
if [ "$currl_gateway" ]
then
if [ "$currl_gateway" != "${4}" ]
then
sed -i "s/$currl_gateway/${4}/" /etc/network/interfaces
echo "configuration gateway for network interface $1 success" !
ci_m=1
fi
else
sed -i "/netmask ${3}/a\gateway ${4}" /etc/network/interfaces
echo "configuration gateway for network interface $1 success" !
ci_m=1
fi
fi
return 0
}
dnserver ()
{
if [ $DN_MOD ]
then
dns1=`echo $DN_MOD | awk -F "," '{ print $1 }'`
dns2=`echo $DN_MOD | awk -F "," '{ print $2 }'`
dns3=`echo $DN_MOD | awk -F "," '{ print $3 }'`
if [ $dns1 ]
then
if [ $(sed -n '/^nameserver/p' /etc/resolvconf/resolv.conf.d/base | sed -n 1p | grep -c $dns1) -eq 0 ]
then
echo "nameserver $dns1" > /etc/resolvconf/resolv.conf.d/base
echo "configuration dnsserver1 success" !
ci_m=1
else
echo "dnsserver1 not change" !
fi
fi
if [ $dns2 ]
then
if [ $(sed -n '/^nameserver/p' /etc/resolvconf/resolv.conf.d/base | sed -n 2p | grep -c $dns2) -eq 0 ]
then
echo "nameserver $dns2" >> /etc/resolvconf/resolv.conf.d/base
echo "configuration dnsserver2 success" !
ci_m=1
else
echo "dnsserver2 not change" !
fi
fi
if [ $dns3 ]
then
if [ $(sed -n '/^nameserver/p' /etc/resolvconf/resolv.conf.d/base | sed -n 3p | grep -c $dns3) -eq 0 ]
then
echo "nameserver $dns3" >> /etc/resolvconf/resolv.conf.d/base
echo "configuration dnsserver3 success" !
ci_m=1
else
echo "dnsserver3 not change" !
fi
fi
fi
}
hostname ()
{
local old_hostname=`cat /etc/hostname`
if [ "${HN_MOD}" ]
then
if [ "$old_hostname" ]
then
if [ "${HN_MOD}" != "${old_hostname}" ]
then
sed -i "s/$old_hostname/$HN_MOD/g" /etc/hostname
ch_m=1
echo "configuration hostname success" !
else
echo "hostname not change" !
fi
else
echo "${HN_MOD}" > /etc/hostname
ch_m=1
echo "configuration hostname success" !
fi
else
echo "hostname not change" !
fi
}
password ()
{
if [ "$PS_MOD" ]
then
if [ -f $auto_tmp ]
then
. $auto_tmp
if [ "$PS_MOD_LAST" ]
then
if [ "$PS_MOD" != "$PS_MOD_LAST" ]
then
echo "root:$PS_MOD" | chpasswd && echo "password changed success" !
else
echo "password not change" !
fi
else
echo "root:$PS_MOD" | chpasswd && echo "password changed success" !
fi
else
echo "root:$PS_MOD" | chpasswd && echo "password changed success" !
fi
fi
}
restart_service ()
{
if [ "$ci_m" -eq 1 ]
then
service networking restart || ifdown -a;ifup -a
/sbin/resolvconf -u
fi
if [ "$ch_m" = 1 ]
then
reboot && echo "$(date '+%Y-%m-%d %H:%M:%S') system will reboot now" !
fi
echo "service restarted success" !
}