-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovfenv
182 lines (162 loc) · 4.79 KB
/
ovfenv
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
#!/bin/bash
#
# /etc/rc.d/init.d/ovfenv
#
# Get ovfenv properties and process them.
#
# chkconfig: 345 09 26
# description: Every operation here is allowed, for now, it is just
# about network configuration but anything can be retrieved
# from properties to setup os, db, as configuration...
#
# processname: none
# Source function library.
. /etc/rc.d/init.d/functions
LOCK_FILE=/var/lock/subsys/ovfenv
# Get the ovfenv.xml file from vmware tools or cdrom if vmtools failed.
get_ovfenv()
{
OVFENV=/etc/sysconfig/ovfenv.xml
/usr/sbin/vmtoolsd --cmd "info-get guestinfo.ovfenv" > $OVFENV
if [ $? -ne 0 ]
then
mount -o ro /dev/cdrom /media
cp -f /media/ovfenv.xml $OVFENV
umount /media
fi
}
set_network()
{
ETH0_FILE=/etc/sysconfig/network-scripts/ifcfg-eth0
XMLNS="setns oe=http://schemas.dmtf.org/ovf/environment/1\n"
GET="cat /oe:Environment/oe:PropertySection/oe:Property"
IP_KEY="[@oe:key='ip']"
NETMASK_KEY="[@oe:key='netmask']"
GATEWAY_KEY="[@oe:key='gateway']"
DOMAIN_KEY="[@oe:key='domain']"
DNS1_KEY="[@oe:key='dns1']"
DNS2_KEY="[@oe:key='dns2']"
ip=$(echo -e "${XMLNS}${GET}${IP_KEY}" | xmllint --shell $OVFENV | grep -oE "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")
netmask=$(echo -e "${XMLNS}${GET}${NETMASK_KEY}" | xmllint --shell $OVFENV | grep -oE "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")
gateway=$(echo -e "${XMLNS}${GET}${GATEWAY_KEY}" | xmllint --shell $OVFENV | grep -oE "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")
domain=$(echo -e "${XMLNS}${GET}${DOMAIN_KEY}" | xmllint --shell $OVFENV | grep value | sed -r 's/.*value="(.*)".*/\1/')
dns1=$(echo -e "${XMLNS}${GET}${DNS1_KEY}" | xmllint --shell $OVFENV | grep -oE "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")
dns2=$(echo -e "${XMLNS}${GET}${DNS2_KEY}" | xmllint --shell $OVFENV | grep -oE "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")
hwaddr=$(ifconfig eth0 | awk '$4 == "HWaddr" {print $5}')
cat > ${ETH0_FILE} << EOF
# Set by ovfenv service.
# Don't change manually value will be reseted at reboot
DEVICE=eth0
NAME=eth0
BOOTPROTO=none
IPADDR=$ip
NETMASK=$netmask
HWADDR=$hwaddr
ONBOOT=yes
EOF
echo "IP;${ip};${ETH0_FILE}" >> $LOCK_FILE
echo "Netmask;${netmask};${ETH0_FILE}" >> $LOCK_FILE
echo "Mac;${hwaddr};${ETH0_FILE}" >> $LOCK_FILE
set_dns $domain $dns1 $dns2
set_default_gateway $gateway
set_hosts $ip
}
# Configure DNS settings
set_dns()
{
DNS_FILE=/etc/resolv.conf
cat > $DNS_FILE << EOF
# Set by ovfenv
search $1
nameserver $2
nameserver $3
EOF
echo "Domain;$1;$DNS_FILE" >> $LOCK_FILE
echo "Dns1;$2;$DNS_FILE" >> $LOCK_FILE
echo "Dns2;$3;$DNS_FILE" >> $LOCK_FILE
}
# Adjust /etc/hosts entry
set_hosts()
{
ip=$1
FQDN=$(hostname)
SDN=$(hostname -s)
HOSTS_FILE=/etc/hosts
if [ "$(grep -i $FQDN $HOSTS_FILE)" ]
then
sed -i -r 's/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(.*'$FQDN'.*)$/'$ip'\1/' $HOSTS_FILE
elif [ "$(grep -i $SDN $HOSTS_FILE)" ]
then
sed -i -r 's/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(.*'$SDN'.*)$/'$ip'\1/' $HOSTS_FILE
else
echo "${ip} ${FQDN} ${SDN}" >> $HOSTS_FILE
fi
echo "HostsFile;${ip};${HOSTS_FILE}" >> $LOCK_FILE
}
# Set or replace the system default gateway.
set_default_gateway()
{
SYSTEM_NETWORK_FILE=/etc/sysconfig/network
gateway=$1
if [ "$(grep GATEWAY /etc/sysconfig/network)" ]
then
sed -i -r 's/(GATEWAY=).*/\1'${gateway}'/' $SYSTEM_NETWORK_FILE
else
echo "GATEWAY=$gateway" >> $SYSTEM_NETWORK_FILE
fi
echo "Gateway;${gateway};${SYSTEM_NETWORK_FILE}" >> $LOCK_FILE
}
check_ovfenv_properties_set()
{
RETVAL=0
for line in $(cat $LOCK_FILE)
do
key=$(echo $line|cut -d";" -f1)
value=$(echo $line|cut -d";" -f2)
file=$(echo $line|cut -d";" -f3)
if [ "$file" ]
then
if [ -z "$(grep "${value}" ${file})" ]
then
echo -n "$key not set on $file: "
failure
echo
RETVAL=1
fi
fi
done
[ $RETVAL -eq 0 ] && echo -n "OVF environment correctly applied" && success && echo
exit $RETVAL
}
case "$1" in
start)
# Clean up lock file first
rm -f $LOCK_FILE
touch $LOCK_FILE
get_ovfenv
set_network
check_ovfenv_properties_set
;;
stop)
# echo -n "Shutting down ovfenv services: "
rm -f $LOCK_FILE
;;
status)
if [ -f $LOCK_FILE ]
then
check_ovfenv_properties_set
else
echo -n "No lock file found, ovfenv not launched."
failure
echo
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: ovfenv {start|stop|status|restart"
exit 1
;;
esac