5 Oct 2004 lgs   » (Master)

System V init scripts

I got tired of editing my /etc/hosts by hand to adapt my network configuration depending on if I'm at home or at the office. So I decided to hack something on the initscripts:

  1. Edit /boot/grub/menu.lst and duplicate some entries adding a LOCATION={home|office} parameter:
    title Fedora Core (2.6.8-1.521) Office
            root (hd0,0)
            kernel /boot/vmlinuz-2.6.8-1.521 ro root=LABEL=/ rhgb quiet LOCATION=office
            initrd /boot/initrd-2.6.8-1.521.img
    
    

    title Fedora Core (2.6.8-1.521) Home root (hd0,0) kernel /boot/vmlinuz-2.6.8-1.521 ro root=LABEL=/ rhgb quiet LOCATION=home initrd /boot/initrd-2.6.8-1.521.img

  2. Write a System V script (actually copying an existing one and adapting) called location which copies the right /etc/hosts. Note the chkconfig lines at the beginning of the file:
    #!/bin/bash
    #
    # chkconfig: 2345 90 10
    # description: little script to configure some stuff depending if the laptop \
    #              is at the office or at home
    
    

    # source function library . /etc/init.d/functions

    start () { echo $"$LOCATION" if [ -e /etc/hosts.$LOCATION ]; then cp /etc/hosts.$LOCATION /etc/hosts else echo $"localized hosts file not found" fi }

    stop () { echo "Have a good day" }

    case "$1" in start) start ;; stop) stop ;; restart|reload) stop start ;; *) echo $"Usage: $0 {start|stop|restart}" exit 1 esac

    exit 0

  3. Install the location script with chkconfig:
    [root@i8100] chkconfig location on

  4. Write /etc/hosts.home and /etc/home.office

That's all. Next time I reboot I will have my right network configuration just by selecting one grub menu item. :-)

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!