This file is indexed.

/etc/vz/dists/scripts/redhat-add_ip.sh is in vzctl 4.9.4-5.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/bin/bash
#  Copyright (C) 2000-2009, Parallels, Inc. All rights reserved.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
# Adds IP address(es) in a container running Red Hat-like distro.

VENET_DEV=venet0
VENET_DEV_CFG=ifcfg-$VENET_DEV
IFCFG_DIR=/etc/sysconfig/network-scripts
IFCFG=${IFCFG_DIR}/ifcfg-${VENET_DEV}
NETFILE=/etc/sysconfig/network
HOSTFILE=/etc/hosts
NETWORKRESTART=

function fix_ifup()
{
	file="/sbin/ifup"

	[ -f "${file}" ] || return 0
	[ "x${VE_STATE}" != "xstarting" ] && return 0

	if grep -q 'if \[ "\${DEVICE}" = "lo" \]; then' ${file} 2>/dev/null
	then
		${CP} ${file} ${file}.$$ || return 1
		/bin/sed -e 's/if \[ "\${DEVICE}" = "lo" \]; then/if \[ "${IPADDR}" = "127.0.0.1" \]; then/g' < ${file} > ${file}.$$ &&
			mv -f ${file}.$$ ${file}
		rm -f ${file}.$$ 2>/dev/null
	fi
}

function setup_network()
{
	# Set up venet0 main interface as 127.0.0.1
	mkdir -p ${IFCFG_DIR}
	echo "DEVICE=${VENET_DEV}
BOOTPROTO=static
ONBOOT=yes
ARPCHECK=\"no\"
IPADDR=127.0.0.1
NETMASK=255.255.255.255
BROADCAST=0.0.0.0
ARPCHECK=\"no\"" > $IFCFG || error "Can't write to file $IFCFG" $VZ_FS_NO_DISK_SPACE

	# Set /etc/sysconfig/network
	put_param $NETFILE NETWORKING yes
	# Set default route to venet0
	put_param $NETFILE GATEWAYDEV ${VENET_DEV}
	# Remove old (obsoleted) fake route
	del_param $NETFILE GATEWAY
	# setup ipv6
	setup6_network

	# Set up /etc/hosts
	if [ ! -f ${HOSTFILE} ]; then
		echo "127.0.0.1 localhost.localdomain localhost" > $HOSTFILE
	fi
	fix_ifup
}

function setup6_network()
{
	[ "${IPV6}" != "yes" ] && return 0

	if ! grep -q 'IPV6INIT="yes"' ${IFCFG}; then
		put_param ${IFCFG} IPV6INIT yes
	fi
	if ! grep -q 'NETWORKING_IPV6="yes"' ${NETFILE}; then
		put_param ${NETFILE} NETWORKING_IPV6 yes
		put_param ${NETFILE} IPV6_DEFAULTDEV ${VENET_DEV}
		NETWORKRESTART=yes
	fi
}

function create_config()
{
	local ip=$1
	local netmask=$2
	local ifnum=$3
	local file=${IFCFG_DIR}/bak/${VENET_DEV_CFG}:${ifnum}

	echo "DEVICE=${VENET_DEV}:${ifnum}
ONBOOT=yes
ARPCHECK=\"no\"
IPADDR=${ip}
NETMASK=${netmask}" > $file ||
	error "Can't write to file $file" ${VZ_FS_NO_DISK_SPACE}
}

function add_ip6()
{
	[ "${IPV6}" != "yes" ] && return 0
	if ! grep -qw "$1" ${IFCFG} 2>/dev/null; then
		setup6_network
		add_param ${IFCFG} IPV6ADDR_SECONDARIES "$1/$2"
		if have_ifconfig; then
			ifconfig ${VENET_DEV} add "$1/$2"
		else
			ip a a $1/$2 dev ${VENET_DEV}
		fi
	fi
}

function get_all_aliasid()
{
	IFNUM=-1

	cd ${IFCFG_DIR} || return 1
	IFNUMLIST=`ls -1 bak/${VENET_DEV_CFG}:* 2>/dev/null |
		sed "s/.*${VENET_DEV_CFG}://"`
}

function get_aliasid_by_ip()
{
	local ip=$1
	local idlist

	cd ${IFCFG_DIR} || return 1
	IFNUM=`grep -l "IPADDR=${ip}$" ${VENET_DEV_CFG}:* 2>/dev/null | \
		head -n 1 | sed -e 's/.*:\([0-9]*\)$/\1/'`
}

function get_free_aliasid()
{
	local found=

	[ -z "${IFNUMLIST}" ] && get_all_aliasid
	while test -z ${found}; do
		let IFNUM=IFNUM+1
		echo "${IFNUMLIST}" | grep -q -E "^${IFNUM}$" 2>/dev/null ||
			found=1
	done
}

function backup_configs()
{
	local delall=$1

	rm -rf ${IFCFG_DIR}/bak/ >/dev/null 2>&1
	mkdir -p ${IFCFG_DIR}/bak
	[ -n "${delall}" ] && return 0

	cd ${IFCFG_DIR} || return 1
	if ls ${VENET_DEV_CFG}:* > /dev/null 2>&1; then
		${CP} ${VENET_DEV_CFG}:* ${IFCFG_DIR}/bak/ ||
			error "Unable to backup interface config files" ${VZ_FS_NO_DISK_SPACE}
	fi
}

function move_configs()
{
	cd ${IFCFG_DIR} || return 1
	rm -rf ${VENET_DEV_CFG}:*
	mv -f bak/* ${IFCFG_DIR}/ >/dev/null 2>&1
	rm -rf ${IFCFG_DIR}/bak
}

function check_running()
{
	if have_ifconfig; then
		if ifconfig ${VENET_DEV} | grep -qFw RUNNING 2>/dev/null; then
			return 1
		fi
	else
		if ip l l ${VENET_DEV} | grep -qFw UP 2>/dev/null; then
			return 1
		fi
	fi

	return 0
}

function add_ip()
{
	local ipm
	local new_ips
	local if_restart=

	# In case we are starting CT
	if [ "x${VE_STATE}" = "xstarting" ]; then
		# Remove all VENET config files
		rm -f ${IFCFG} ${IFCFG}:* >/dev/null 2>&1
		if [ -z "${IP_ADDR}" ]; then
			# Just enable networking
			put_param $NETFILE NETWORKING yes
			return 0
		fi
	fi
	if [ ! -f "${IFCFG}" ]; then
		setup_network
		if_restart=1
	fi
	backup_configs ${IPDELALL}
	new_ips="${IP_ADDR}"
	if [ "x${IPDELALL}" = "xyes" ]; then
		new_ips=
		for ipm in ${IP_ADDR}; do
			ip_conv $ipm
			get_aliasid_by_ip "${_IP}"
			if [ -n "${IFNUM}" ]; then
				# ip already exists just create it in bak
				create_config "${_IP}" "${_NETMASK}" "${IFNUM}"
			else
				new_ips="${new_ips} ${ipm}"
			fi
		done
	fi
	for ipm in ${new_ips}; do
		ip_conv $ipm
		if [ -z "$_IPV6ADDR" ]; then
			get_free_aliasid
			create_config "${_IP}" "${_NETMASK}" "${IFNUM}"
		else
			if [ "x${IPDELALL}" = "xyes" ]; then
				del_param ${IFCFG} IPV6ADDR_SECONDARIES ""
			fi
			add_ip6 "${_IP}" "${_MASK}"
		fi
	done
	move_configs
	if [ "x${VE_STATE}" = "xrunning" ]; then
		# synchronyze config files & interfaces
		if [ "${NETWORKRESTART}" = "yes" ]; then
			/etc/init.d/network restart
		elif check_running; then
			# check_running return 0; so not running; restart
			/etc/init.d/network restart
		elif [ -n "${if_restart}" ]; then
			ifup ${VENET_DEV}
		else
			cd /etc/sysconfig/network-scripts &&
				./ifup-aliases ${VENET_DEV}
		fi
	fi
}

add_ip
exit 0
# end of script