#!/bin/bash # Copyright (C) 2000-2007 SWsoft. 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 # # # This script configure IP alias(es) inside Magellan like VE. # # Parameters are passed in environment variables. # Required parameters: # IP_ADDR - IP address(es) to add # (several addresses should be divided by space) # Optional parameters: # VE_STATE - state of VE; could be one of: # starting | stopping | running | stopped # IPDELALL - delete all old interfaces # VENET_DEV=venet0 IFCFG_DIR=/etc/conf.d IFCFG=${IFCFG_DIR}/net.${VENET_DEV} ROUTESCFG=${IFCFG_DIR}/net.routes SCRIPT=/etc/runlevels/default/net.${VENET_DEV} HOSTFILE=/etc/hosts function setup_network() { cat > ${IFCFG} << EOF ONBOOT=yes NETWORKING=static IP=127.0.0.1 NETMASK=255.255.255.255 BROADCAST=0.0.0.0 EOF # setup routes echo "-net ${FAKEGATEWAY} netmask 255.255.255.255 dev ${VENET_DEV}" > ${ROUTESCFG} echo "default gw ${FAKEGATEWAY}" >> ${ROUTESCFG} # Set up /etc/hosts if [ ! -f ${HOSTFILE} ]; then echo "127.0.0.1 localhost.localdomain localhost" > $HOSTFILE fi } function get_all_aliasid() { IFNUM=-1 IFNUMLIST=$(for i in $(find ${IFCFG_DIR} -name net.${VENET_DEV}:*); do echo $i | sed "s/.*${VENET_DEV}://"; done) } 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 create_config() { local ip=$1 local ifnum=$2 echo -e "# auto-generated configuration for ${VENET_DEV}:${ifnum} ONBOOT=yes NETWORKING=static IP=${ip} NETMASK=255.255.255.255 BROADCAST=0.0.0.0 " > ${IFCFG}:${ifnum} } function add_ip() { local ip local new_ips # In case we are starting VE if [ "x${VE_STATE}" = "xstarting" ]; then setup_network fi if [ "x${IPDELALL}" = "xyes" ]; then rm -f ${IFCFG} rm -f ${IFCFG}:[0-9]* fi for ip in ${IP_ADDR}; do found= if grep -e "\\<${ip}\\>" >/dev/null 2>&1 ${IFCFG}:*; then continue fi get_free_aliasid create_config ${ip} ${IFNUM} done if [ "x${VE_STATE}" = "xrunning" ]; then # synchronyze config files & interfaces /etc/init.d/network restart 2>/dev/null 1>/dev/null fi } add_ip exit 0 # end of script