If you need to plug-in your notebook into various networks, then imagine a setup wherein you power on your notebook and are presented with a list of client names on boot up. You select a client, press Enter and voila, you boot with an up and running networked computer, with the network settings corresponding to the selected client. If your notebook runs Linux, especially, PCQLinux 2004, then this article will explain how to achieve this. We will use the GRUB's boot loader screen to display the list of clients. Let's start with setting
up GRUB.
Set up GRUB
The configuration file for GRUB is grub.conf found under /etc directory. Open the file in a Linux text editor. You should be able to see the following lines.
title PCQLinux (2.4.22-1.2115.nptl)
root (hd0,6)
kernel /vmlinuz-2.4.22-1.2115.nptl ro root=LABEL=/ hdd=ide-scsi rhgb
initrd /initrd-2.4.22-1.2115.nptl.img
|
Note that the above lines may not match exactly with those in your grub.conf. Copy these lines to the end of the file and substitute 'PCQLinux (2.4.22-1.2115.nptl)' with the name of a client (say
Client 1) and add 'client=client1' to the third line such that it looks as follows.
title Client 1
root (hd0,6)
kernel /vmlinuz-2.4.22-1.2115.nptl client=client1 ro root=LABEL=/ hdd=ide-scsi rhgb
initrd /initrd-2.4.22-1.2115.nptl.img
For another client (say Client 2), copy the same lines to the end of the file and substitute with Client 2 and 'client=client2' as shown below.
title Client 2
root (hd0,6)
kernel /vmlinuz-2.4.22-1.2115.nptl client=client2 ro root=LABEL=/ hdd=ide-scsi rhgb
initrd /initrd-2.4.22-1.2115.nptl.img
Create network configuration files
Next, we need to create network configuration files for client 1 and client 2 and place it under the directory /etc/sysconfig/networking/ devices. Following is the config file for client 1:
DEVICE=eth0
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
PEERDNS=no
BOOTPROTO=none
NETMASK=255.255.255.0
IPADDR=192.168.0.251
GATEWAY=192.168.0.1
Substitute 192.168.0.251, 255. 255.255.0 and 192.168.0.1 with the values for the IP address, netmask and gateway of client1. Now save the file as client1.ifcfg-eth0 under /etc/sysconfig/networking/devices. Similarly create another file for client 2.
DEVICE=eth0
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
PEERDNS=no
BOOTPROTO=none
NETMASK=255.255.255.0
IPADDR=172.16.0.222
GATEWAY=172.16.0.254
Save it as client2.ifcfg-eth0. If the client's network is running a DHCP server, use the following lines instead of the ones specified above:
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
Write a small script
Finally we need a script, which can detect which client was selected on the boot screen and set up the corresponding configuration. Such a script will look as follows (refer to the section below for an explanation of the script).
#!/bin/sh
client=`(cat /proc/cmdline; echo) | /bin/cut -d" " -f1 | /bin/cut -d= -f2`
case "$client" in
client1)
cp /etc/sysconfig/networking/devices/client1.ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0
;;
client2)
cp /etc/sysconfig/networking/devices/client2.ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0
;;
esac
Save the above script as setprofile.sh in /etc directory. Assign it executable permissions using the following command:
chmod +x setprofile.sh
Now we want this script to be executed upon each reboot. To accomplish this, open the file named rc.sysinit found in /etc directory and append the following to it:
/etc/setprofile.sh
Reboot the machine and you would see 'Client 1' and 'Client 2' on the boot screen to select from. Selecting Client 1 will boot the machine with the network settings specified in client1.ifcfg-eth0, and selecting Client 2 will use the settings in client2.ifcfg-eth0. After booting, cross-check if things are working, by issuing the ifconfig command which will show the IP address setup by the scripts.
Under the hood
The logic behind this setup is explained here. We added new entries in grub.conf, which shows up on the boot screen. Take up the following line in
grub.conf:
kernel /vmlinuz-2.4.22-1.2115.nptl client=client1 ro root=LABEL=/ hdd=ide-scsi rhgb
In the above line, whatever is present after 'kernel /vmlinuz-2.4.22-1.2115.nptl' is treated as arguments by the Grub boot loader and stored in a file called /proc/cmdline. This means if you select 'Client 1', then /proc/cmdline will have the following:
client=client1 ro root=LABEL=/ hdd=ide-scsi rhgb
Therefore, if we are able to extract the first argument, ie, client= client1 from this file, we can detect which entry was selected on the boot screen. The following line in setnwprofile.sh does exactly the same.
client=`(cat /proc/cmdline; echo) | /bin/cut -d" " -f1 | /bin/cut
-d= -f2`
In fact, the above line not only extracts client=client1, but it also pipes the result out further to another cut command (namely cut -d= -f2) such that the variable named client only contains client 1 (or client 2 if Client 2 was selected on the boot screen). Depending on the value of the client variable, a case statement is used to copy the network configuration file (client1 .ifcfg-eth0 if client 1 is selected) to /etc/sysconfig/network-scripts/ ifcfg-eth0. The latter file is used by PCQLinux 2004 to determine the network settings. If client 2 was selected, client2.ifcfg-eth0 will be copied to /etc/ sysconfig/network-scripts/ifcfg-eth0. The setnwprofile.sh scripts executes at the Linux startup, and, hence you get an up and running network on the login.
Shekhar Govindarajan, IT4Enterprise