Skip to content

Instantly share code, notes, and snippets.

@sata
Created March 11, 2012 16:26
Show Gist options
  • Save sata/2017017 to your computer and use it in GitHub Desktop.
Save sata/2017017 to your computer and use it in GitHub Desktop.
additional configuration for network in openvz, used primarly to be able to set up bridges between container and host node
#!/usr/bin/env bash
# /usr/sbin/vznetcfg.custom
# originally from: http://wiki.openvz.org/Using_private_IPs_for_Hardware_Nodes
# a script to bring up bridged network interfaces (veth's) in a container
# modified 2012-03-11 to work with dhcp configured containers
GLOBALCONFIGFILE=/etc/vz/vz.conf
CTCONFIGFILE=/etc/vz/conf/$VEID.conf
vzctl=/usr/sbin/vzctl
brctl=/usr/sbin/brctl
ip=/sbin/ip
ifconfig=/sbin/ifconfig
# sourcing configs
. $GLOBALCONFIGFILE
. $CTCONFIGFILE
NETIF_OPTIONS=`echo $NETIF | sed 's/,/\n/g'`
for str in $NETIF_OPTIONS; do \
# getting 'ifname' parameter value
if echo "$str" | grep -o "^ifname=" ; then
# remove the parameter name from the string (along with '=')
CTIFNAME=${str#*=};
fi
# getting 'host_ifname' parameter value
if echo "$str" | grep -o "^host_ifname=" ; then
# remove the parameter name from the string (along with '=')
VZHOSTIF=${str#*=};
fi
done
# this is uncommented since it's not a requirement to have VETH_IP_ADDRESS
# for instance, when using dhcp incontainer
#if [ ! -n "$VETH_IP_ADDRESS" ]; then
# echo "According to $CONFIGFILE CT$VEID has no veth IPs configured."
# exit 1
#fi
if [ ! -n "$VZHOSTIF" ]; then
echo "According to $CONFIGFILE CT$VEID has no veth interface configured."
exit 1
fi
if [ ! -n "$CTIFNAME" ]; then
echo "Corrupted $CONFIGFILE: no 'ifname' defined for host_ifname $VZHOSTIF."
exit 1
fi
echo "Initializing interface $VZHOSTIF for CT$VEID."
$ifconfig $VZHOSTIF 0
CTROUTEDEV=$VZHOSTIF
if [ -n "$BRIDGEDEV" ]; then
echo "Adding interface $VZHOSTIF to the bridge $BRIDGEDEV."
CTROUTEDEV=$BRIDGEDEV
$brctl addif $BRIDGEDEV $VZHOSTIF
fi
# Up the interface $CTIFNAME link in CT$VEID
$vzctl exec $VEID $ip link set $CTIFNAME up
for IP in $VETH_IP_ADDRESS; do
echo "Adding an IP $IP to the $CTIFNAME for CT$VEID."
$vzctl exec $VEID $ip address add $IP dev $CTIFNAME
# removing the netmask
IP_STRIP=${IP%%/*};
echo "Adding a route from CT0 to CT$VEID using $IP_STRIP."
$ip route add $IP_STRIP dev $CTROUTEDEV
done
if [ -n "$CT0_IP" ]; then
echo "Adding a route from CT$VEID to CT0."
$vzctl exec $VEID $ip route add $CT0_IP dev $CTIFNAME
fi
if [ -n "$VE_DEFAULT_GATEWAY" ]; then
echo "Setting $VE_DEFAULT_GATEWAY as a default gateway for CT$VEID."
$vzctl exec $VEID \
$ip route add default via $VE_DEFAULT_GATEWAY dev $CTIFNAME
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment