# Howto Start ETH0 with static or dhcp mode using runlevels.

## FunkyChicken

Hi,

Does anyone know how i can boot up my machines eth0 card in DHCP or with a static ip addresses using RUNLEVELS from GRUB. I have the RUNLEVELS figured out for starting different services depending where the laptop is, i.e (at work, home, Gaming LAN, etc) so thats not a problem. But the one thing that has me confused is that /etc/init/net.eth0 has to have the variables as one or the other. i.e dhcp or static ip.

Thanks in advance on any help.

----------

## pjp

Moved from Hardware & Laptops.

My first thought would be to add some code to net.eth0 to decide what is running.  The how, I'm not sure of yet (not clean anyway).

----------

## Sven Vermeulen

Checkout https://forums.gentoo.org/viewtopic.php?t=10455&highlight=ping, I've posted a possible, but untested solution over there.

----------

## Praxxus

On edit: Fixed "netselect" script

Okay . . .

1) Make a copy of your /etc/conf.d/net for every custom runlevel + "default," using the runlevel name as the file extension:

```
cp /etc/conf.d/net /etc/conf.d/net.default

cp /etc/conf.d/net /etc/conf.d/net.work

cp /etc/conf.d/net /etc/conf.d/net.home

. . . etc.
```

2) Create the /etc/init.d/netselect script:

```
#!/sbin/runscript

# Copyright 1999-2002 Gentoo Technologies, Inc.

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

DEFAULT="default"

start() {

        ebegin "Setting network configuration for $mylevel runlevel"

        if [ -f /etc/conf.d/net.$mylevel ]; then

           /bin/cp -f /etc/conf.d/net.$mylevel /etc/conf.d/net > /dev/null 2>&1

        elif [ -f /etc/conf.d/net.$DEFAULT ] ; then

           /bin/cp -f /etc/conf.d/net.$DEFAULT /etc/conf.d/net > /dev/null 2>&1

           echo "       Copying $DEFAULT configuration file"

        else echo "     Nothing to do"

        fi

        eend 0

}

stop() {

        ebegin "Restoring default network configuration"

        if [ -f /etc/conf.d/net.$DEFAULT ]; then

           /bin/cp -f /etc/conf.d/net.$DEFAULT /etc/conf.d/net > /dev/null 2>&1

        else echo "     Nothing to do"

        fi

        eend 0

}
```

3)  Assumes "netselect" is owned by root

```
chmod 0755 /etc/init.d/netselect
```

4)  Add netselect to all appropriate runlevels.  In the example below, I only used three for example.  The first part (before the semicolon) is just a space-delimited list, so "for i in default work home LAN school aunt uncle dog spoon fish ; do ..." would add netselect to those 10 runlevels:

```
for i in default work home ; do rc-update add netselect $i; done
```

5)  Add netselect as a (moderate) dependency for "/etc/init.d/net.eth0"  Note that I have "use pcmcia" commented out.  Don't take that as an "instruction," that's just the way I'm set up.   :Wink: 

```
#!/sbin/runscript

# Copyright 1999-2002 Gentoo Technologies, Inc.

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

# $Header: /home/cvsroot/gentoo-src/rc-scripts/init.d/net.eth0,v 1.22 2002/11/18 13:07:02 azarah Exp $

#NB: Config is in /etc/conf.d/net

# For pcmcia users. note that pcmcia must be added to the same

# runlevel as the net.* script that needs it.

depend() {

# use pcmcia

    after netselect

}
```

6)  Configure all the different net.runlevel scripts you created in step #1.

That should do it!

----------

## Sven Vermeulen

Wouldn't this always run the default (net.default)? If you don't want that to happen, you should add the necessary lines to your bootloader so that one can choose at the beginning which runlevel it wants to use, f.i.

```

   Linux 2.4.20 default

   Linux 2.4.20 home

   Linux 2.4.20 work

Grub: _

```

with each line configured in (in this example with grub) /boot/grub/menu.lst

```

/kernel vmlinuz-2.4.20 root=/dev/hdXY default

...

/kernel vmlinuz-2.4.20 root=/dev/hdXY home

...

/kernel vmlinuz-2.4.20 root=/dev/hdXY work

```

----------

## Praxxus

 *Quote:*   

> Wouldn't this always run the default (net.default)?

 

Yes, if you booted into "default" all the time, it would.  FunkyChicken had already said that he had "RUNLEVELS" in GRUB figured out, so I was working with that.

Thanks for your post, though.  I don't have runlevels in GRUB figured out, and was going to ask for more information.   :Wink: 

----------

## Sven Vermeulen

Personally I think it would be easier if both networks provide DHCP. The network admin has more control (f.i. only approve access to laptops with known MAC-adresses) and you learn how to set up a DHCP-server  :Smile: 

----------

## pjp

Which is an entirely different issue though.

----------

## FunkyChicken

 *Praxxus wrote:*   

> 
> 
> start() {
> 
>         ebegin "Setting network configuration for $mylevel runlevel"
> ...

 

Praxxus, i understand what you have done, but i have come across a problem and i was wondering if you have come across this? From the netselect script, it copies the /etc/conf.d/net.$mylevel --> /etc/conf.d/net for the /etc/init.d/net.eth0 to pickup the appropriate variables for the interface.

But unfortunately the file /etc/conf.d/net.$mylevel is not being copied over for some reason ?

I have gone through 2-3 times checking everything to see if it is ok, and as far as i can tell everything Look's Good.

Any Ideas

----------

## Praxxus

Do you have the net.xxx scripts in /etc/conf.d/?  (by "xxx" --or "$mylevel" in the script-- I mean the name of your custom runlevel)

And, just to abide by the script, there should be an /etc/conf.d/net.default script, too.

----------

## FunkyChicken

Here are my runlevels

In /etc/runlevels

boot (always needed)

default (this starts local,net.eth0,netselect,pcmcia,vcron)

lan-server  (this starts net.eth0,netselect,samba,proftpd,apache,sshd,firewall,dhcp) for basic file server, dhcpd server at LANS, etc

lan-static-ip  (this starts net.eth0, netselect)

lan-wireless-dhcp (this starts pcmcia)

single (nothing starts here)

standalone (this start netselect,net.eth0)

and here are the resulting net.xxx from /etc/conf.d

net.default  (with settings for iface_eth0="dhcp")

net.lan-server  (with settings for iface_eth0="10.0.0.127 broadcast 10.0.0.255 netmask 255.255.255.0")

net.lan-static-ip  (with settings for iface_eth0="XXX.XXX.XXX.XXX broadcast XXX.XXX.XXX.XXX netmask 255.255.255.0")

net.lan-wireless-dhcp  (with settings for iface_eth1="dhcp")

net.standalone  (with settings for iface_eth0="127.0.0.1 broadcast 127.0.0.1 netmask 255.255.255.0")

So everything looks ok, but when i boot into a runlevel the appropriate /etc/conf.d/net.xxx is not copied to /etc/conf.d/net for some reason, and when i shutdown the PC, the /etc/conf.d/net.default is not copied /etc/conf.d/net as in the script. All file permissions are 0755 (root,root) on the net & net.xxx files, so permissions looks ok.

Note:

When i copied the /etc/conf.d/net.xxx /etc/conf.d/net manually and rebooted it worked according to the /etc/conf.d/net file i had copied.

I hope this makes sense

FunkyChicken

----------

## Praxxus

1)  Make sure /etc/init.d/netselect is in all of the necessary runlevels (rc-update).

2)  Make sure you have your runlevels defined in /etc/inittab.  I was just playing around, testing the "netselect" script, and came to the realization (duh) that naming a runlevel in grub.conf is not enough.  In order to get my test system to boot to the "fish" runlevel, I tweaked /etc/inittab:

```
# System initialization (runs when system boots).

#DE:S12345:respawn:/sbin/devfsd /dev -fg

# Script to run when going single user (runlevel 1).

~~:S:wait:/sbin/sulogin

si:S:sysinit:/sbin/rc boot

l0:0:wait:/sbin/rc shutdown

l1:1:wait:/sbin/rc single

l2:2:wait:/sbin/rc nonetwork

l3:3:wait:/sbin/rc fish

l4:4:wait:/sbin/rc default

l5:5:wait:/sbin/rc default

l6:6:wait:/sbin/rc reboot

z6:6:respawn:/sbin/sulogin
```

----------

## FunkyChicken

Yeah checked that ... here is mine

```

# System initialization (runs when system boots).

#DE:S1234567:respawn:/sbin/devfsd /dev -fg

# Script to run when going single user (runlevel 1).

~~:S:wait:/sbin/sulogin

si:S:sysinit:/sbin/rc boot

l0:0:wait:/sbin/rc shutdown 

l1:1:wait:/sbin/rc single

l2:2:wait:/sbin/rc standalone

l3:3:wait:/sbin/rc default

l4:4:wait:/sbin/rc lan-static-ip

l5:5:wait:/sbin/rc lan-server

# Level 6 has to remain as the Reboot RUNLEVEL - See 'man init'

# Levels 7 - 9 Can be used still though, although not commonly used

l6:6:wait:/sbin/rc reboot

z6:6:respawn:/sbin/sulogin

l7:7:wait:/sbin/rc lan-wireless-dhcp

```

and my grub config (menu.lst)

```

default 0

timeout 30

splashimage=(hd0,4)/boot/grub/splash.xpm.gz

title=Windows XP

root (hd0,0)

chainloader +1

title=Gentoo Linux (Network [DHCP])

root (hd0,4)

kernel /boot/bzImage root=/dev/hda6 vga=791 3

title=Gentoo Linux (Network [Static IP])

root (hd0,4)

kernel /boot/bzImage root=/dev/hda6 vga=791 4

title=Gentoo Linux (Wireless Network [DHCP])

root (hd0,4)

kernel /boot/bzImage root=/dev/hda6 vga=791 7

title=Gentoo Linux (Standalone)

root (hd0,4)

kernel /boot/bzImage root=/dev/hda6 vga=791 2

title=Gentoo Linux (DHCP SERVER & FILE,WEB,FTP Server)

root (hd0,4)

kernel /boot/bzImage root=/dev/hda6 vga=791 5

title=Gentoo Linux (Single User)

root (hd0,4)

kernel /boot/bzImage root=/dev/hda6 vga=791 1

```

the only thing stopping it is for some reason it will not copy the /etc/init.d/net.xxx across to /etc/init.d/net .... got me stumped ... 

p.s I checked the runlevels, and they all (the ones that need it) have the netselect script

 :Confused: 

FunkyChicken

----------

## Praxxus

Okay,  replace the three instances of:

```
2>1 /dev/null 
```

with:

```
> /dev/null 2>&1
```

in /etc/init.d/netselect and see if that helps.

 :Embarassed: 

----------

## FunkyChicken

Praxxus,

That fixed the file copy problem, but know when i try and boot with one of the static ip configs, the system tries to use dhcp to configure eth0. But the wierd thing is the correct file is being copied over i.e /etc/conf.d/net.lan-static-ip /etc/conf.d/net ( the iface_eth0 is set as a static setting), but some how /etc/init.d/net.eth0 is getting the settings as dhcp.

 :Rolling Eyes: 

Got any ideas ....

Funky Chicken

----------

## Praxxus

Does /etc/conf.d/net.eth0 exist?  If so, /etc/init.d/net.eth0 might be getting its initial information from it.  Remove the config file, and /etc/init.d/net.eth0 will default to getting its information from /etc/conf.d/net

----------

