/etc/vz/dists/scripts/debian-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 | #!/bin/sh
# Copyright (C) 2000-2011, 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 Debian-like distro.
VENET_DEV=venet0
LOOPBACK=lo
CFGFILE=/etc/network/interfaces
HOSTFILE=/etc/hosts
need_restart=
fix_networking_conf()
{
local cfg="/etc/init/networking.conf"
local str="local-filesystems"
test -f ${cfg} || return 0
fgrep -q "udevtrigger" ${cfg} 2>/dev/null || return 0
fgrep -v "udevtrigger" ${cfg} | \
sed "s,(${str},${str},g" > ${cfg}.$$ && \
mv -f ${cfg}.$$ ${cfg}
}
# Set up loopback device
setup_lo()
{
grep -qw lo ${CFGFILE} 2>/dev/null && return 0
cat << EOF >> ${CFGFILE}
# Auto generated ${LOOPBACK} interface
auto ${LOOPBACK}
iface ${LOOPBACK} inet loopback
EOF
}
setup_network()
{
echo "# This configuration file is auto-generated.
#
# WARNING: Do not edit this file, your changes will be lost.
# Please create/edit $CFGFILE.head and
# $CFGFILE.tail instead, their contents will be
# inserted at the beginning and at the end of this file, respectively.
#
# NOTE: it is NOT guaranteed that the contents of $CFGFILE.tail
# will be at the very end of this file.
#
" > ${CFGFILE}
if [ -f ${CFGFILE}.head ]; then
cat ${CFGFILE}.head >> ${CFGFILE}
fi
if [ -f ${CFGFILE}.template ]; then
cat ${CFGFILE}.template >> ${CFGFILE}
fi
setup_lo
# Set up /etc/hosts
if [ ! -f $HOSTFILE ]; then
echo "127.0.0.1 localhost.localdomain localhost" > $HOSTFILE
if [ "${IPV6}" = "yes" ]; then
echo "::1 localhost.localdomain localhost" >> $HOSTFILE
fi
fi
if [ -n "${IP_ADDR}" ]; then
# Set up venet0
echo "
# Auto generated ${VENET_DEV} interface
auto ${VENET_DEV}
iface ${VENET_DEV} inet manual
up ifconfig ${VENET_DEV} up
up ifconfig ${VENET_DEV} 127.0.0.2
up route add default dev ${VENET_DEV}
down route del default dev ${VENET_DEV}
down ifconfig ${VENET_DEV} down
" >> ${CFGFILE}
if [ "${IPV6}" = "yes" ]; then
echo "
iface ${VENET_DEV} inet6 manual
up route -A inet6 add default dev ${VENET_DEV}
down route -A inet6 del default dev ${VENET_DEV}
" >> ${CFGFILE}
fi
fi
if [ -f ${CFGFILE}.tail ]; then
cat ${CFGFILE}.tail >> ${CFGFILE}
fi
fix_networking_conf
}
create_config()
{
local ip=$1
local netmask=$2
local mask=$3
local ifnum=$4
if [ "${ip#*:}" = "${ip}" ]; then
echo "auto ${VENET_DEV}:${ifnum}
iface ${VENET_DEV}:${ifnum} inet static
address ${ip}
netmask ${netmask}
" >> ${CFGFILE}.bak
else
sed -i -e "s/iface ${VENET_DEV} inet6 manual/iface ${VENET_DEV} inet6 manual\n\tup ifconfig ${VENET_DEV} add ${ip}\/${mask}\n\tdown ifconfig ${VENET_DEV} del ${ip}\/${mask}/" ${CFGFILE}.bak
need_restart=yes
fi
}
get_all_aliasid()
{
IFNUM=-1
IFNUMLIST=$(grep -e "^auto ${VENET_DEV}:.*$" 2>/dev/null \
${CFGFILE}.bak | sed "s/.*${VENET_DEV}://")
}
get_free_aliasid()
{
local found=
[ -z "${IFNUMLIST}" ] && get_all_aliasid
while test -z ${found}; do
IFNUM=$((IFNUM+1))
echo "${IFNUMLIST}" | grep -q -E "^${IFNUM}$" 2>/dev/null ||
found=1
done
}
add_ip()
{
local ipm
local add
local iface
if [ "x${VE_STATE}" = "xstarting" ]; then
if test -n "$IP_ADDR"; then
setup_network
else
# IP_ADDR empty, do we need to remove old ones?
if grep -v "^#" ${CFGFILE} | grep -q -F -w "${VENET_DEV}"; then
setup_network
else
setup_lo
fi
fi
elif ! grep -q -E "^auto ${VENET_DEV}([^:]|$)" ${CFGFILE} 2>/dev/null; then
setup_network
fi
if [ "${IPDELALL}" = "yes" ]; then
ifdown ${VENET_DEV} >/dev/null 2>&1
if [ -z "${IP_ADDR}" ]; then
# No new IPs to assign, remove venet0 completely
remove_debian_interface "${VENET_DEV}.*" ${CFGFILE}
sed -i -e 's/^# Auto generated venet0 interface$//' ${CFGFILE}
else
# Will add new IPs below, only remove venet0 aliases and IPv6
remove_debian_interface "${VENET_DEV}:[0-9]*" ${CFGFILE}
# Remove IPv6 addresses (which are not aliases)
# Note the actual tab character in grep expression
grep -v "^ up ifconfig ${VENET_DEV} add " ${CFGFILE} > ${CFGFILE}.bak
grep -v "^ down ifconfig ${VENET_DEV} del " ${CFGFILE}.bak > ${CFGFILE}
fi
fi
if [ -n "${IP_ADDR}" ]; then
cp -f ${CFGFILE} ${CFGFILE}.bak
for ipm in ${IP_ADDR}; do
ip_conv $ipm
if grep -w "${_IP}" >/dev/null 2>&1 ${CFGFILE}.bak; then
continue
fi
get_free_aliasid
create_config "${_IP}" "${_NETMASK}" "${_MASK}" "${IFNUM}"
done
mv -f ${CFGFILE}.bak ${CFGFILE}
fi
if [ "x${VE_STATE}" = "xrunning" ]; then
[ -n "${need_restart}" ] && /sbin/ifdown venet0 2>/dev/null
/sbin/ifup -a --force 2>/dev/null
fi
}
add_ip
exit 0
|