]>
Commit | Line | Data |
---|---|---|
88d20d00 TP |
1 | #!/bin/bash |
2 | # Copyright (C) 2000-2007 SWsoft. All rights reserved. | |
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 2 of the License, or | |
7 | # (at your option) any later version. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | # | |
18 | # | |
19 | # This script configure IP alias(es) inside RedHat like VE. | |
20 | # | |
21 | # Parameters are passed in environment variables. | |
22 | # Required parameters: | |
23 | # IP_ADDR - IP address(es) to add | |
24 | # (several addresses should be divided by space) | |
25 | # Optional parameters: | |
26 | # VE_STATE - state of VE; could be one of: | |
27 | # starting | stopping | running | stopped | |
28 | # IPDELALL - delete all old interfaces | |
29 | # | |
30 | ||
31 | VENET_DEV=venet0 | |
32 | VENET_DEV_CFG=ifcfg-$VENET_DEV | |
33 | ||
34 | ||
35 | IFCFG_DIR=/etc/sysconfig/interfaces | |
36 | IFCFG=${IFCFG_DIR}/ifcfg-${VENET_DEV} | |
37 | NETFILE=/etc/sysconfig/network | |
38 | HOSTFILE=/etc/hosts | |
39 | ROUTE=/etc/sysconfig/network | |
40 | NETWORKRESTART= | |
41 | ||
42 | function fix_ifup | |
43 | { | |
44 | file="/sbin/ifup" | |
45 | ||
46 | [ -f "${file}" ] || return 0 | |
47 | [ "x${VE_STATE}" != "xstarting" ] && return 0 | |
48 | ||
49 | if grep -q 'if \[ "\${DEVICE}" = "lo" \]; then' ${file} 2>/dev/null | |
50 | then | |
51 | ${CP} ${file} ${file}.$$ || return 1 | |
52 | /bin/sed -e 's/if \[ "\${DEVICE}" = "lo" \]; then/if \[ "${IPADDR}" = "127.0.0.1" \]; then/g' < ${file} > ${file}.$$ && | |
53 | mv -f ${file}.$$ ${file} | |
54 | rm -f ${file}.$$ 2>/dev/null | |
55 | fi | |
56 | } | |
57 | ||
58 | function setup_network | |
59 | { | |
60 | # Set up venet0 main interface as 127.0.0.1 | |
61 | mkdir -p ${IFCFG_DIR} | |
62 | echo "DEVICE=${VENET_DEV} | |
63 | BOOTPROTO=static | |
64 | ONBOOT=yes | |
65 | IPADDR=127.0.0.1 | |
66 | NETMASK=255.255.255.255 | |
67 | BROADCAST=0.0.0.0" > $IFCFG || error "Can't write to file $IFCFG" $VZ_FS_NO_DISK_SPACE | |
68 | ||
69 | ||
70 | remove_fake_old_route ${ROUTE} | |
71 | # if ! grep -q "${FAKEGATEWAYNET}/24 dev ${VENET_DEV}" ${ROUTE} 2>/dev/null; then | |
72 | # echo "${FAKEGATEWAYNET}/24 dev ${VENET_DEV} scope host | |
73 | #default via ${FAKEGATEWAY}" >> ${ROUTE} || error "Can't create ${ROUTE}" ${VZ_FS_NO_DISK_SPACE} | |
74 | # fi | |
75 | # Set /etc/sysconfig/network | |
76 | put_param $NETFILE NETWORKING yes | |
77 | put_param $NETFILE GATEWAY ${FAKEGATEWAY} | |
78 | ||
79 | # setup ipv6 | |
80 | setup6_network | |
81 | ||
82 | # Set up /etc/hosts | |
83 | if [ ! -f ${HOSTFILE} ]; then | |
84 | echo "127.0.0.1 localhost.localdomain localhost" > $HOSTFILE | |
85 | fi | |
86 | fix_ifup | |
87 | } | |
88 | ||
89 | function setup6_network | |
90 | { | |
91 | [ "${IPV6}" != "yes" ] && return 0 | |
92 | ||
93 | if ! grep -q 'IPV6INIT="yes"' ${IFCFG}; then | |
94 | put_param ${IFCFG} IPV6INIT yes | |
95 | fi | |
96 | if ! grep -q 'NETWORKING_IPV6="yes"' ${NETFILE}; then | |
97 | put_param ${NETFILE} NETWORKING_IPV6 yes | |
98 | put_param ${NETFILE} IPV6_DEFAULTDEV ${VENET_DEV} | |
99 | NETWORKRESTART=yes | |
100 | fi | |
101 | } | |
102 | ||
103 | function create_config | |
104 | { | |
105 | local ip=$1 | |
106 | local ifnum=$2 | |
107 | ||
108 | echo "DEVICE=${VENET_DEV}:${ifnum} | |
109 | BOOTPROTO=static | |
110 | ONBOOT=yes | |
111 | IPADDR=${ip} | |
112 | NETMASK=255.255.255.255" > ${IFCFG_DIR}/bak/${VENET_DEV_CFG}:${ifnum} || | |
113 | error "Unable to create interface config file" ${VZ_FS_NO_DISK_SPACE} | |
114 | } | |
115 | ||
116 | function add_ip6 | |
117 | { | |
118 | [ "${IPV6}" != "yes" ] && return | |
119 | if ! grep -qw "$1" ${IFCFG} 2>/dev/null; then | |
120 | setup6_network | |
121 | add_param ${IFCFG} IPV6ADDR_SECONDARIES "$1/128" | |
122 | ifconfig ${VENET_DEV} add "$1/128" | |
123 | fi | |
124 | } | |
125 | ||
126 | function get_all_aliasid | |
127 | { | |
128 | IFNUM=-1 | |
129 | ||
130 | cd ${IFCFG_DIR} || return 1 | |
131 | IFNUMLIST=`ls -1 bak/${VENET_DEV_CFG}:* 2>/dev/null | | |
132 | sed "s/.*${VENET_DEV_CFG}://"` | |
133 | } | |
134 | ||
135 | function get_aliasid_by_ip | |
136 | { | |
137 | local ip=$1 | |
138 | local idlist | |
139 | ||
140 | cd ${IFCFG_DIR} || return 1 | |
141 | IFNUM=`grep -l "IPADDR=${ip}$" ${VENET_DEV_CFG}:* | head -n 1 | | |
142 | sed -e 's/.*:\([0-9]*\)$/\1/'` | |
143 | } | |
144 | ||
145 | function get_free_aliasid | |
146 | { | |
147 | local found= | |
148 | ||
149 | [ -z "${IFNUMLIST}" ] && get_all_aliasid | |
150 | while test -z ${found}; do | |
151 | let IFNUM=IFNUM+1 | |
152 | echo "${IFNUMLIST}" | grep -q -E "^${IFNUM}$" 2>/dev/null || | |
153 | found=1 | |
154 | done | |
155 | } | |
156 | ||
157 | function backup_configs | |
158 | { | |
159 | local delall=$1 | |
160 | ||
161 | rm -rf ${IFCFG_DIR}/bak/ >/dev/null 2>&1 | |
162 | mkdir -p ${IFCFG_DIR}/bak | |
163 | [ -n "${delall}" ] && return 0 | |
164 | ||
165 | cd ${IFCFG_DIR} || return 1 | |
166 | if ls ${VENET_DEV_CFG}:* > /dev/null 2>&1; then | |
167 | ${CP} ${VENET_DEV_CFG}:* ${IFCFG_DIR}/bak/ || | |
168 | error "Unable to backup intrface config files" ${VZ_FS_NO_DISK_SPACE} | |
169 | fi | |
170 | } | |
171 | ||
172 | function move_configs | |
173 | { | |
174 | cd ${IFCFG_DIR} || return 1 | |
175 | rm -rf ${VENET_DEV_CFG}:* | |
176 | mv -f bak/* ${IFCFG_DIR}/ >/dev/null 2>&1 | |
177 | rm -rf ${IFCFG_DIR}/bak | |
178 | } | |
179 | ||
180 | function add_ip | |
181 | { | |
182 | local ip | |
183 | local new_ips | |
184 | local if_restart= | |
185 | ||
186 | # In case we are starting VE | |
187 | if [ "x${VE_STATE}" = "xstarting" ]; then | |
188 | # Remove all VENET config files | |
189 | rm -f ${IFCFG} ${IFCFG}:* >/dev/null 2>&1 | |
190 | fi | |
191 | if [ ! -f "${IFCFG}" ]; then | |
192 | setup_network | |
193 | if_restart=1 | |
194 | fi | |
195 | backup_configs ${IPDELALL} | |
196 | new_ips="${IP_ADDR}" | |
197 | if [ "x${IPDELALL}" = "xyes" ]; then | |
198 | new_ips= | |
199 | for ip in ${IP_ADDR}; do | |
200 | get_aliasid_by_ip "${ip}" | |
201 | if [ -n "${IFNUM}" ]; then | |
202 | # ip already exists just create it in bak | |
203 | create_config "${ip}" "${IFNUM}" | |
204 | else | |
205 | new_ips="${new_ips} ${ip}" | |
206 | fi | |
207 | done | |
208 | fi | |
209 | for ip in ${new_ips}; do | |
210 | if [ "${ip#*:}" = "${ip}" ]; then | |
211 | get_free_aliasid | |
212 | create_config "${ip}" "${IFNUM}" | |
213 | else | |
214 | if [ "x${IPDELALL}" = "xyes" ]; then | |
215 | del_param ${IFCFG} IPV6ADDR_SECONDARIES "" | |
216 | fi | |
217 | add_ip6 "${ip}" | |
218 | fi | |
219 | done | |
220 | move_configs | |
221 | if [ "x${VE_STATE}" = "xrunning" ]; then | |
222 | # synchronyze config files & interfaces | |
223 | if [ "${NETWORKRESTART}" = "yes" ]; then | |
224 | /etc/init.d/network restart | |
225 | echo "Tutaj 2" | |
226 | elif [ -n "${if_restart}" ]; then | |
227 | ifup ${VENET_DEV}:${IFNUM} | |
228 | echo "Tutaj 3: ${VENET_DEV}:${IFNUM}"; | |
229 | else | |
230 | cd /etc/sysconfig/network-scripts && | |
231 | ./ifup-aliases ${VENET_DEV} | |
232 | ifup ${VENET_DEV}:${IFNUM} | |
233 | echo "Tutaj 4: ${VENET_DEV}:${IFNUM}"; | |
234 | fi | |
235 | fi | |
236 | } | |
237 | ||
238 | add_ip | |
239 | exit 0 | |
240 | # end of script |