-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostonly-ifup
executable file
·66 lines (58 loc) · 1.85 KB
/
hostonly-ifup
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
#!/bin/bash
# Author: www.byte-edu.com
# Description: To Create a Host-Only network model for the virtual machine instances.
BridgeName=v-switch0
HostVirtIP=172.16.10.254/24
VirtNic=$1
# 安装 btctl 工具
InstallBrctl(){
which brctl > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "brctl has been installed."
else
yum install bridge-utils -y > /dev/null 2>&1
[ $? -eq 0 ] && echo "brctl was successfully installed" || (echo "brctl installation failed" && exit 1)
fi
}
# 创建虚拟桥
CreateSwitch(){
ip link show ${BridgeName} > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "The Bridge:[${BridgeName}] is exist."
else
brctl addbr ${BridgeName} && echo "The Bridge:[${BridgeName}] was created successfully." || (echo "The Bridge:[${BridgeName}] was created failed." && exit 1)
ip link set ${BridgeName} up
fi
}
# 添加虚拟机实例网卡到虚拟桥
AttachVirNic(){
if [ "${VirtNic}" ];then
ip link set ${VirtNic} up
sleep 0.5s
brctl addif ${BridgeName} ${VirtNic}
[ $? -eq 0 ]&& echo "Attach Tap nic to ${BridgeName} succeeded." || (echo "Attach Tap nic to ${BridgeName} failed." && exit 1)
else
echo "The tap nic is not exist"
exit 1
fi
}
# 添加宿主机虚拟网卡地址到虚拟桥
AttachVirHostNic(){
BridgeIP=$(ip addr show ${BridgeName} | grep -w 'inet' |awk {'print $2'})
if [ "${BridgeIP}" == "${HostVirtIP}" ];then
echo "The Bridge:[$BridgeName] has IP. It's Host-Only network"
elif [ !${BridgeIP} ];then
ifconfig ${BridgeName} ${HostVirtIP} up && echo "To Create Host-Only network succeeded." || (echo "To Create Host-Only network failed." && exit 1)
else
echo "The bridge already has an IP:[${BridgeIP}], but it does not match the address you specified [${HostVirtIP}]."
exit 1
fi
}
# 配置主函数
MAIN(){
InstallBrctl
CreateSwitch
AttachVirNic
AttachVirHostNic
}
MAIN