All pastes #442835 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-04-16 14:37 UTC
rendered paste body
#!/bin/bash
#
# Startup script for the virt-network
#
# chkconfig: - 97 01
# description: Local private network for xen guests

PATH=/usr/bin:/sbin:/bin:/usr:/sbin
export PATH


# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
if [ "$NETWORKING" = "no" ]; then
	logger -s -p daemon.warning  "virt-network: Networking disabled"
	exit 0
fi

# Discover default interface
GATEWAYDEV=`/sbin/ip route | grep default | awk {'print $5'}`
if [ "$GATEWAYDEV" = "link" ]; then
	GATEWAYDEV=`/sbin/ip route | grep default | awk {'print $3'}`
fi	

start() {
	# Clear any existing NAT rules
	/sbin/iptables -F -t nat
	/sbin/iptables -F
	if test -z "$GATEWAYDEV"; then
		echo "No gateway device found"
                logger -s -p daemon.warning  "virt-network: No gateway device found"
		
	else
		echo "Masquerading using $GATEWAYDEV"
		logger  p daemon.warning "virt-network: Masquerading using $GATEWAYDEV "
		/sbin/iptables -t nat -A POSTROUTING -o $GATEWAYDEV -j MASQUERADE
		echo 1 > /proc/sys/net/ipv4/ip_forward

	fi
	return 0
}
stop() {
	# Clear any existing NAT rules
	/sbin/iptables -F -t nat
	echo "Stop masquerading"
	logger  p daemon.warning "virt-network: Stopping masquerading"
	echo 0 > /proc/sys/net/ipv4/ip_forward
	return 0
}
	
status() {

	MASQDEV=`/sbin/iptables -L -t nat -v | grep MASQUERADE | awk {'print $7'}`
	IPFORWARD=`cat /proc/sys/net/ipv4/ip_forward`
	RETCODE=0
	if [ "$IPFORWARD" = "1" ]; then
		IPSTATUS="enabled"
	else
		IPSTATUS="disabled"
		RETCODE=1
	fi
 
	if test -z "$MASQDEV"; then
		MASQDEV="Not found"
		RETCODE=1
	fi
	
	echo "NAT device    : $MASQDEV"
	echo "IP Forwarding : $IPSTATUS"
	 
	return $RETCODE
}


case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
	
  status)
	status
	;;

  restart)
        stop
	start
	;;

*)
echo "Usage: $0 {start | stop | restart | status}"
;;
esac