Sunday, June 21, 2009

Configuring network profiles in Gentoo

I use my laptop at home and at work. At home, network is configured as static IP address and at work it is using DHCP. So, I needed solution to switch between the two easily. Here is what I came up with.
The contents of my "/etc/conf.d/net" is shown below.

modules_eth0=( "dhclient" )

config_work=( "dhcp" )

config_home="192.168.1.254 netmask 255.255.255.0 brd 192.168.0.255"
routes_home="default via 192.168.1.1"
dns_servers_home="127.0.0.1 192.168.1.1"

cmdline_opt() {
if [ $# -ne 1 ] || ! [ -r /proc/cmdline ]; then
return 1
fi

for opt in $(cat /proc/cmdline) ; do
[ "${opt}" = "${1}" ] && return 0
done

return 1
}

preup() {
if [ "${IFACE}" = "eth0" ] ; then
if cmdline_opt location=home ; then
einfo "Setting network profile: home"
_configure_variables "home"
elif cmdline_opt location=work ; then
einfo "Setting network profile: work"
_configure_variables "work"
else
return 1
fi
fi

# Remember to return 0 on success
return 0
}

I pass "location" as kernel parameter when I boot the machine. So, I added it to the kernel boot line in "grub.conf". I choose the appropriate kernel to boot from the grub menu depending on whether I am at home or work. The relevant portions of my "grub.conf" is shown below.

title Gentoo Vanilla Sources (2.6.28-ab1) (Home)
root (hd0,4)
kernel /boot/kernel-genkernel-x86-2.6.28-ab1 ro real_root=/dev/sda5 location=home

title Gentoo Vanilla Sources (2.6.28-ab1) (Work)
root (hd0,4)
kernel /boot/kernel-genkernel-x86-2.6.28-ab1 ro real_root=/dev/sda5 location=work


If there are any easier/cleaner solutions I would like to hear about them.

No comments: