# Init script for adding arbitrary routes using net config..

## rett

I am new to the forums  :Very Happy:   but I have noticed there doesn't seem to be a really good way to create arbitrary routes with init scripts other than putting route commands in a startup file so I decided to try my hand at it.

First of all, the scripts are created as /etc/init.d/net.routename init files.

This can be added to runlevels or started manually and what it does is check in /etc/conf.d/net for route_routename and route_routename_dev statements.  The specific format is just like a route statement without the "route add" or "route del":

```

route_example="10.1.0.0 netmask 255.255.0.0 gw 10.250.1.1"

route_example_dev="eth1"

```

Continuing with this example, you would create a file in init.d called net.example and either run it with start and stop or add it to a runlevel with rc-update.

I am very interested in feedback because this is filling a specific need of mine and have noticed other posts with similar interests.

I just wanted to make this clear: this is my FIRST init script.  I'm sure there are better ways to do this.  Would someone possibly take over and do it right so everyone can benefit?

[Edited to elimate confusion]

(Scroll down to see script)Last edited by rett on Thu Dec 04, 2003 7:59 pm; edited 6 times in total

----------

## pato

Thanks for the great script. This is exactly what I have been looking for. Previously, I was adding my route commands to /etc/conf.d/local.start (which I suppose is still appropriate). However, I like the elegance of your method a lot better. I made a few minor mods that you can either use or not use:

```
depend() {

        need net

}

```

I also changed the name of the init script to route.foo instead of net.foo. I thought it might be less confusing since net.foo is usually associated with network interfaces. This is purely subjective, though.

I'd like to see this script added to the Gentoo FAQ, since I banged my head for hours trying to figure out the Gentoo way of adding static routes. I always prefer a sanctioned method over a hack.  :Smile: 

Thanks again.

----------

## rett

You probably figured it out but just in case, you can't change the name from net.* to route.* without making changes to runscript.sh because in order for it to get it's route info from /etc/conf.d/net it has to be named that way.

I struggled with that myself until I dug into runscript.sh and found a special statement for net.* scripts and found the need to name it that way.

Also, I just made a slight modification to the script (there was an error)

Thanks, and I hope it helps more than just myself!

----------

## Mnemia

This is pretty nice, good job   :Very Happy: 

I really wish that Gentoo had a standard way of setting static routes at boot though. Something like this solution should be adopted ASAP - I think this is one of the only major holes in the otherwise excellent init system.

Maybe try filing a bug with your solution to get it some developer attention?

----------

## rett

I just revamped the init script and the way it works.

Before, it didn't actually guarantee the route would be activated after the appropriate interface had been started.  Now you can specify which interface it is associated with and it won't start until after it has been started.

/etc/conf.d/net syntax:

```

iface_eth0="172.16.1.1 broadcast 172.16.255.255 netmask 255.255.0.0"

route_netname="161.223.0.0 netmask 255.255.0.0 gw 161.223.142.20"

route_netname_dev="eth0"

```

And here's the init script (/etc/init.d/net.netname):

```

#!/sbin/runscript

# Distributed under the terms of the GNU General Public License v2

depend() {

        after net."$(eval echo \$route_${myservice##*.}_dev)"

}

                                                                                                                                 

setup_env() {

        route_net=${myservice##*.}

        route_string="$(eval echo \$route_${myservice##*.})"

        route_dev="$(eval echo \$route_${myservice##*.}_dev)"

}

                                                                                                                                 

checkconfig() {

        if [ -z "${route_string}" ]

        then

                eerror "Please make sure that /etc/conf.d/net has route_${route_net} set"

                return 1

        fi

}

                                                                                                                                 

start() {

        local retval=0

        setup_env

        checkconfig || return 1

        ebegin "Adding route to ${route_net}"

                                                                                                                                 

        /sbin/route add -net $route_string dev $route_dev 2> /dev/null || {

                retval=$?

                eend ${retval} "Failed to establish route to ${route_net}"

                return ${retval}

        }

        eend 0

}

                                                                                                                                 

stop() {

        local retval=0

        setup_env

        checkconfig || return 1

        ebegin "Removing route to ${route_net}"

                                                                                                                                 

        /sbin/route del -net $route_string 2> /dev/null

                                                                                                                                 

        eend 0

}

restart() {

        svc_stop

        svc_start

}

```

Please, if errors are found, post them here.  I'm trying to learn and help is really appriciated.   :Smile: Last edited by rett on Thu Dec 04, 2003 8:01 pm; edited 1 time in total

----------

## ova

I think that gentoo developers should modify this script bit and add it into init.d.

The name "*route*" should bee greate.

----------

## nick_downing

Thanx people, I also discovered the same problem just now.  Searching the forums revealed this neat looking script.  I'll let you know how I go with it.

cheers,

Nick

----------

## crowbert

Another thanks from someone who was looking for the solution to the same problem.  Has a bug report been filed?  If so, could someone please post the number in this thread?

----------

## MikeP

I guess https://bugs.gentoo.org/show_bug.cgi?id=19695 is what you are looking for.

I've encountered this problem too and found that, im gonna try the attached patches..

I agree that something like this should be added to the default gentoo distro.

----------

