defect 4426: support persistent route setting for Ubuntu/Debian in routeop postscript
This commit is contained in:
parent
641be270e9
commit
cdacb9a85f
@ -6,7 +6,7 @@
|
||||
#=head2 routeop is called by makeroutes command and setuproutes postscript to
|
||||
# setup a route on a node.
|
||||
# The syntax is:
|
||||
# routeop add/delete net mask gateway ifnamea #NOTE: the add/delete will be
|
||||
# routeop add/delete net mask gateway ifname #NOTE: the add/delete will be
|
||||
# obsoleted, using 'replace' is recommended.
|
||||
# routeop replace net mask gateway ifname #NOTE: it only works for sles so far
|
||||
# net - IP of net like 192.168.1.0. The keyword
|
||||
@ -204,12 +204,6 @@ replace_persistent_route()
|
||||
OS_name="debian"
|
||||
fi
|
||||
|
||||
# The replace operation does not support debain so far
|
||||
if [ "$OS_name" != "redhat" ] && [ "$OS_name" != "sles" ]; then
|
||||
echo "Warning: replace operation only supports to add persistent route for sles and redhat by now."
|
||||
return
|
||||
fi
|
||||
|
||||
# set the destination of the route for searching in the route configuration file
|
||||
if [ "$net" = "default" ]; then
|
||||
routedest="default"
|
||||
@ -221,105 +215,106 @@ replace_persistent_route()
|
||||
|
||||
case $OS_name in
|
||||
sles)
|
||||
filename="/etc/sysconfig/network/routes";
|
||||
if echo $net | grep : 2>&1 1>/dev/null
|
||||
then
|
||||
# for ipv6
|
||||
if [ "$gw" = "" -o "$gw" = "::" ] ; then
|
||||
route="$net/$mask :: - $ifname"
|
||||
route1="$net\/$mask :: - $ifname";
|
||||
else
|
||||
route="$net/$mask $gw - -"
|
||||
route1="$net\/$mask $gw - -";
|
||||
fi
|
||||
else
|
||||
# for ipv4
|
||||
if [ "$gw" = "" -o "$gw" = "0.0.0.0" ] ; then
|
||||
if [ "$net" = "default" ]; then
|
||||
route="default - - $ifname";
|
||||
route1="default - - $ifname";
|
||||
filename="/etc/sysconfig/network/routes";
|
||||
if echo $net | grep : 2>&1 1>/dev/null
|
||||
then
|
||||
# for ipv6
|
||||
if [ "$gw" = "" -o "$gw" = "::" ] ; then
|
||||
route="$net/$mask :: - $ifname"
|
||||
route1="$net\/$mask :: - $ifname";
|
||||
else
|
||||
route="$net/$mask - - $ifname";
|
||||
route1="$net\/$mask - - $ifname";
|
||||
route="$net/$mask $gw - -"
|
||||
route1="$net\/$mask $gw - -";
|
||||
fi
|
||||
else
|
||||
if [ "$net" = "default" ]; then
|
||||
route="default $gw - $ifname";
|
||||
route1="default $gw - $ifname";
|
||||
# for ipv4
|
||||
if [ "$gw" = "" -o "$gw" = "0.0.0.0" ] ; then
|
||||
if [ "$net" = "default" ]; then
|
||||
route="default - - $ifname";
|
||||
route1="default - - $ifname";
|
||||
else
|
||||
route="$net/$mask - - $ifname";
|
||||
route1="$net\/$mask - - $ifname";
|
||||
fi
|
||||
else
|
||||
route="$net/$mask $gw - $ifname";
|
||||
route1="$net\/$mask $gw - $ifname";
|
||||
if [ "$net" = "default" ]; then
|
||||
route="default $gw - $ifname";
|
||||
route1="default $gw - $ifname";
|
||||
else
|
||||
route="$net/$mask $gw - $ifname";
|
||||
route1="$net\/$mask $gw - $ifname";
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
if [ -f $filename ]; then
|
||||
egrep "^$routedest" $filename 2>&1 1>/dev/null
|
||||
if [ $? -ne 0 ]; then #route does not exist
|
||||
echo $route >> $filename
|
||||
if [ -f $filename ]; then
|
||||
egrep "^$routedest" $filename 2>&1 1>/dev/null
|
||||
if [ $? -ne 0 ]; then #route does not exist
|
||||
echo $route >> $filename
|
||||
echo "Persistent route \"$route\" has been added in $filename."
|
||||
else
|
||||
# replace it
|
||||
sed -i -e "s/$routedest1.*/$route1/g" $filename
|
||||
echo "Persistent route \"$route\" has been replaced in $filename."
|
||||
fi
|
||||
else
|
||||
echo "$route" > $filename
|
||||
echo "Persistent route \"$route\" has been added in $filename."
|
||||
else
|
||||
# replace it
|
||||
sed -i -e "s/$routedest1.*/$route1/g" $filename
|
||||
echo "Persistent route \"$route\" has been replaced in $filename."
|
||||
fi
|
||||
else
|
||||
echo "$route" > $filename
|
||||
echo "Persistent route \"$route\" has been added in $filename."
|
||||
fi
|
||||
;;
|
||||
|
||||
redhat)
|
||||
#echo "rh/fedora/centos"
|
||||
if [ -z "$ifname" ]; then
|
||||
echo "Error: the device name is necessary to configure static route."
|
||||
return
|
||||
fi
|
||||
|
||||
if echo $net | grep : 2>&1 1>/dev/null
|
||||
then
|
||||
# ipv6
|
||||
filename="/etc/sysconfig/network-scripts/route6-$ifname"
|
||||
if [ "$gw" = "" -o "$gw" = "::" ] ; then
|
||||
route="$net/$mask dev $ifname"
|
||||
route1="$net\/$mask dev $ifname"
|
||||
else
|
||||
route="$net/$mask via $gw"
|
||||
route1="$net\/$mask via $gw"
|
||||
#echo "rh/fedora/centos"
|
||||
if [ -z "$ifname" ]; then
|
||||
echo "Error: the device name is necessary to configure static route."
|
||||
return
|
||||
fi
|
||||
else
|
||||
# ipv4
|
||||
filename="/etc/sysconfig/network-scripts/route-$ifname"
|
||||
if [ "$gw" = "" -o "$gw" = "0.0.0.0" ] ; then
|
||||
route="$net/$mask dev $ifname"
|
||||
route1="$net\/$mask dev $ifname"
|
||||
|
||||
if echo $net | grep : 2>&1 1>/dev/null
|
||||
then
|
||||
# ipv6
|
||||
filename="/etc/sysconfig/network-scripts/route6-$ifname"
|
||||
if [ "$gw" = "" -o "$gw" = "::" ] ; then
|
||||
route="$net/$mask dev $ifname"
|
||||
route1="$net\/$mask dev $ifname"
|
||||
else
|
||||
route="$net/$mask via $gw"
|
||||
route1="$net\/$mask via $gw"
|
||||
fi
|
||||
else
|
||||
route="$net/$mask via $gw"
|
||||
route1="$net\/$mask via $gw"
|
||||
# ipv4
|
||||
filename="/etc/sysconfig/network-scripts/route-$ifname"
|
||||
if [ "$gw" = "" -o "$gw" = "0.0.0.0" ] ; then
|
||||
route="$net/$mask dev $ifname"
|
||||
route1="$net\/$mask dev $ifname"
|
||||
else
|
||||
route="$net/$mask via $gw"
|
||||
route1="$net\/$mask via $gw"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
if [ -f $filename ]; then
|
||||
egrep "^$routedest" $filename 2>&1 1>/dev/null
|
||||
if [ $? -ne 0 ]; then #route does not exist
|
||||
echo $route >> $filename
|
||||
if [ -f $filename ]; then
|
||||
egrep "^$routedest" $filename 2>&1 1>/dev/null
|
||||
if [ $? -ne 0 ]; then #route does not exist
|
||||
echo $route >> $filename
|
||||
echo "Persistent route \"$route\" has been added in $filename."
|
||||
else
|
||||
# replace it
|
||||
sed -i -e "s/$routedest1.*/$route1/g" $filename
|
||||
echo "Persistent route \"$route\" has been replaced in $filename."
|
||||
fi
|
||||
else
|
||||
echo "$route" > $filename
|
||||
echo "Persistent route \"$route\" has been added in $filename."
|
||||
else
|
||||
# replace it
|
||||
sed -i -e "s/$routedest1.*/$route1/g" $filename
|
||||
echo "Persistent route \"$route\" has been replaced in $filename."
|
||||
fi
|
||||
else
|
||||
echo "$route" > $filename
|
||||
echo "Persistent route \"$route\" has been added in $filename."
|
||||
fi
|
||||
;;
|
||||
|
||||
debian)
|
||||
debianpreconf
|
||||
matchstr=""
|
||||
v6flag=0
|
||||
#on debian/ubuntu need the network device name
|
||||
if [ ! $ifname ];then
|
||||
ifname=`netstat -nr | grep "$net" | awk '{print $8}' | head -1`
|
||||
# on debian/ubuntu need the network device name
|
||||
if [ -z "$ifname" ]; then
|
||||
echo "Error: the device name is necessary to configure static route."
|
||||
return
|
||||
fi
|
||||
filename="/etc/network/interfaces.d/$ifname"
|
||||
|
||||
@ -327,83 +322,47 @@ replace_persistent_route()
|
||||
echo "auto $ifname" > $filename
|
||||
echo "iface $ifname inet dhcp" >> $filename
|
||||
fi
|
||||
echo $net | grep : 2>&1 1>/dev/null
|
||||
#ipv6
|
||||
if [ $? -eq 0 ];then
|
||||
if [ "$gw" = "" -o "$gw" = "::" ] ; then
|
||||
matchstr="$net/$mask dev $ifname"
|
||||
else
|
||||
matchstr="$net/$mask gw $gw"
|
||||
fi
|
||||
v6flag=1
|
||||
else
|
||||
if [ "$gw" = "" -o "$gw" = "0.0.0.0" ] ; then
|
||||
matchstr="net $net netmask $mask dev $ifname"
|
||||
else
|
||||
matchstr="net $net netmask $mask gw $gw"
|
||||
fi
|
||||
fi
|
||||
|
||||
grep "$matchstr" $filename 2>&1 1>/dev/null
|
||||
if [ $? -ne 0 ];then
|
||||
foundflag=0
|
||||
tempfile="/etc/network/interfaces.d/tmp"
|
||||
while read LINE
|
||||
do
|
||||
echo $LINE | grep "iface" 2>&1 1>/dev/null
|
||||
if [ $? -eq 0 -a $foundflag -eq 1 ];then
|
||||
foundflag=0
|
||||
if [ $v6flag -eq 1 ];then
|
||||
if [ "$gw" = "" -o "$gw" = "::" ] ; then
|
||||
echo " up route -A inet6 add $net/$mask dev $ifname" >> $tempfile
|
||||
echo " down route -A inet6 del $net/$mask dev $ifname" >> $tempfile
|
||||
else
|
||||
echo " up route -A inet6 add $net/$mask gw $gw" >> $tempfile
|
||||
echo " down route -A inet6 del $net/$mask gw $gw" >> $tempfile
|
||||
fi
|
||||
else
|
||||
if [ "$gw" = "" -o "$gw" = "0.0.0.0" ] ; then
|
||||
echo " up route add -net $net netmask $mask dev $ifname" >> $tempfile
|
||||
echo " down route del -net $net netmask $mask dev $ifname" >> $tempfile
|
||||
else
|
||||
echo " up route add -net $net netmask $mask gw $gw" >> $tempfile
|
||||
echo " down route del -net $net netmask $mask gw $gw" >> $tempfile
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
echo $LINE | grep "iface $ifname " 2>&1 1>/dev/null
|
||||
#this is the last line of the device
|
||||
if [ $? -eq 0 ];then
|
||||
foundflag=1
|
||||
fi
|
||||
# if the route entry for a target net has been added in the if cfg file, just replace it
|
||||
cmd="post-up $cmd"
|
||||
#cmd1=${cmd//\//\\\/}
|
||||
#sed -i -e "s/.*$routedest1.*/$cmd1/g" $filename
|
||||
|
||||
echo $LINE >> $tempfile
|
||||
done < $filename
|
||||
#the insert place is the last line of the config file
|
||||
if [ $foundflag -eq 1 ];then
|
||||
if [ $v6flag -eq 1 ];then
|
||||
if [ "$gw" = "" -o "$gw" = "::" ] ; then
|
||||
echo " up route -A inet6 add $net/$mask dev $ifname" >> $tempfile
|
||||
echo " down route -A inet6 del $net/$mask dev $ifname" >> $tempfile
|
||||
else
|
||||
echo " up route -A inet6 add $net/$mask gw $gw" >> $tempfile
|
||||
echo " down route -A inet6 del $net/$mask gw $gw" >> $tempfile
|
||||
fi
|
||||
else
|
||||
if [ "$gw" = "" -o "$gw" = "0.0.0.0" ] ; then
|
||||
echo " up route add -net $net netmask $mask dev $ifname" >> $tempfile
|
||||
echo " down route del -net $net netmask $mask dev $ifname" >> $tempfile
|
||||
else
|
||||
echo " up route add -net $net netmask $mask gw $gw" >> $tempfile
|
||||
echo " down route del -net $net netmask $mask gw $gw" >> $tempfile
|
||||
fi
|
||||
fi
|
||||
# for the case to add a new cmd for certain interface
|
||||
foundflag=0
|
||||
setflag=0
|
||||
tempfile="/etc/network/interfaces.d/tmp"
|
||||
>$tempfile
|
||||
|
||||
while read LINE
|
||||
do
|
||||
# replace the existed setting
|
||||
echo $LINE | grep "$routedest1" 2>&1 1>/dev/null
|
||||
if [ $? -eq 0 -a $foundflag -eq 1 ];then
|
||||
LINE=$cmd
|
||||
setflag=1
|
||||
fi
|
||||
mv -f $tempfile $filename
|
||||
echo "Persistent route \"$matchstr\" added in $filename."
|
||||
else
|
||||
echo "Persisten route \"$match\" already exists in $filename"
|
||||
|
||||
echo $LINE | grep "iface" 2>&1 1>/dev/null
|
||||
if [ $? -eq 0 -a $foundflag -eq 1 -a $setflag -eq 0 ];then
|
||||
foundflag=0
|
||||
echo "$cmd" >> $tempfile
|
||||
fi
|
||||
echo $LINE | grep "iface $ifname " 2>&1 1>/dev/null
|
||||
#this is the last line of the device
|
||||
if [ $? -eq 0 ];then
|
||||
foundflag=1
|
||||
fi
|
||||
|
||||
echo $LINE >> $tempfile
|
||||
done < $filename
|
||||
|
||||
#the insert place is the last line of the config file
|
||||
if [ $foundflag -eq 1 -a $setflag -eq 0 ];then
|
||||
echo "$cmd" >> $tempfile
|
||||
fi
|
||||
mv -f $tempfile $filename
|
||||
echo "Persistent route \"$cmd\" is added in $filename.; "
|
||||
;;
|
||||
|
||||
esac
|
||||
@ -925,7 +884,7 @@ elif [ "$op" = "replace" ]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "Adding temporary route: $cmd"
|
||||
echo "Adding temporary route: $cmd; "
|
||||
result=`$cmd 2>&1`
|
||||
code=$?
|
||||
if [ $code -ne 0 ]; then
|
||||
@ -935,7 +894,9 @@ elif [ "$op" = "replace" ]; then
|
||||
fi
|
||||
|
||||
#replace the persistent route
|
||||
replace_persistent_route $net $mask $gw $ifname
|
||||
# the $cmd param is used for Ubuntu since it needs to run the specific cmd to enable
|
||||
# the route during the up of the device
|
||||
replace_persistent_route $net $mask $gw $ifname
|
||||
fi
|
||||
|
||||
exit 0
|
||||
|
Loading…
Reference in New Issue
Block a user