OpenRC notebook roaming How-To

From Gentoo Wiki
Jump to: navigation, search

Abstract

This article shows how to set up OpenRC to act as a convenient, transparent and flexible networking client which supports roaming as an alternative to other solutions often used on notebooks.

Requirements

The currently stable version of these packages are all that is needed (no *kit software required)

Configuration and setup

Before you start configuring the interfaces, make sure that they work (meaning you have the required drivers installed) and that you know their names. A convenient way is to make a Udev rule to give them human-readable names like ethernet0, wireless0.

OpenRC service management

For each interface an init script must be created for OpenRC to manage that interface by making a symlink to net.lo

root # ln -s /etc/init.d/net.lo /etc/init.d/net.${INTERFACE_NAME}

where ${INTERFACE_NAME} is the name of the interface, e.g. eth0, wlan0, ethernet0, wireless0

If simple roaming is required, any network interface will satisfy the virtual net service. Therefore it is recommended to set rc_depend_strict="NO" in /etc/rc.conf.

File/etc/rc.conf

rc_depend_strict="NO"

The services responsible for managing the interfaces must be added to one or more runlevel. This makes it possible to have network profiles by having most services in the default runlevel and then adding the interfaces to stacked runlevels and having different configuration files in /etc/conf.d/ for each runlevel as /etc/conf.d/net.${RUNELEVEL} The options and syntax of /etc/conf.d/net is decribed in the Gentoo Handbook which shows how to set up more complicated networks (this how-to assumes that all connections are initiated via DHCP)

Suspend to RAM example

With most services in the default runlevel and network services in a runlevel "network", which is stacked on top of the default runlevel, a PM hook for sys-power/pm-utils can be crated which transparently stops network services before suspending, thus preventing timeouts and other problems.

root # mkdir /etc/runlevels/network/
root #
rc-update --stack add default network # include init scripts form the default runlevel in the netewokr runelvel
root #
rc-update del net.ethernet0 default # remove networking scripts from the default runlevel
root #
rc-update add net.ethernet0 network # and add them to the network runlevel
root #
rc-update del net.wireless0 default
root #
rc-update add net.wireless0 network
root #
rc-update del netmount default # remove dependent networking services from the default runlevel
root #
rc-update add netmount network # which would start the networking scripts because of "need net" stanza
File/etc/pm/sleep.d/00networkPM hook example

#!/bin/bash

# properly stop and start the network

if [ -r "${PM_FUNCTIONS}" ]; then
    . "${PM_FUNCTIONS}" 
elif [ -r "${FUNCTIONS}" ]; then
    . "${FUNCTIONS}"
else
# pm-utils version is too old, or something else is wrong
    exit $NA
fi


case "$1" in
   	hibernate|suspend)
		exec rc default
		;;
	thaw|resume)
		exec rc network
		;;
	*) exit $NA
		;;
esac

The hook must be executable

root # chmod a+x /etc/pm/sleep.d/00network

To boot into the network runlevel rather than the default one, /etc/inittab must be changed as explained in the Handbook

File/etc/inittab

...

# Default runlevel.
id:4:initdefault:         #was 3 - can still be 3, it only depends which runlevel is assigned to network below

...

l0:0:wait:/sbin/rc shutdown 
l0s:0:wait:/sbin/halt -dhp
l1:1:wait:/sbin/rc single
l2:2:wait:/sbin/rc nonetwork
l3:3:wait:/sbin/rc default
l4:4:wait:/sbin/rc network      # was default
l5:5:wait:/sbin/rc default
l6:6:wait:/sbin/rc reboot
l6r:6:wait:/sbin/reboot -dk
...

The bootloader can also support multiple options for different setups

File/boot/grub/grub.confGrub v1 example

default 0
timeout 30

title Gentoo Linux 2.6.24-r5 with network
root (hd0,0)
kernel /boot/kernel-genkernel-x86-2.6.24-gentoo-r5 root=/dev/ram0 real_root=/dev/sda3
initrd /boot/initramfs-genkernel-x86-2.6.24-gentoo-r5

title Gentoo Linux 2.6.24-r5 no network
root (hd0,0)
kernel /boot/kernel-genkernel-x86-2.6.24-gentoo-r5 root=/dev/ram0 real_root=/dev/sda3 3
initrd /boot/initramfs-genkernel-x86-2.6.24-gentoo-r5

Exclusive network interface example

It may be desirable to have always only one networking interface configured. This can be done by the postup or postdown functions defined in /etc/conf.d/net[1]

File/etc/conf.d/net

postup_wireless0() {
     rc-service net.ethernet0 stop
}

postup_ethernet0() {
     rc-service net.wireless0 stop
}

postdown_wireless0() {
     rc-service net.ethernet0 start
}

postdown_ethernet0() {
     rc-service net.wireless0 start
}

Network specific log-in procedure example

The postup or postup_${ESSID} (this function would be triggered only after successful connection to the given ${ESSID}) functions defined in /etc/conf.d/net[1] can be used to submit data to e.g. a web log-in form

File/etc/conf.d/net

postup_someESSID() {
     wget --save-cookies cookies.txt \
                        --post-data 'user=foo&password=bar' \
                        http://server.com/auth.php
}

Wi-Fi management

A minimal /etc/wpa_supplicant/wpa_cupplicant.conf is required to begin with. The permissions for the control interface should reflect who can configure the wireless interface. This minimal example assumes only users in the wheel group are allowed to configure the interface and that the configuration file can be updated by clients.

File/etc/wpa_supplicant/wpa_cupplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=wheel
update_config=1

The use of the wpa_supplicant daemon on the interface may have to be enforced in /etc/conf.d/net[2]. It is recommended to have dhcpcd watch the wireless interface (this is by default if no special networking setup is defined) and obtain and address when a carrier is acquired. Hotplugging via ifplugd is possible but isn't necessary as dhcpcd notices the change itself and ifplugd would slow down its reaction.

Clients

The wpa_supplicant daemon can be controlled via clients

  • wpa_cli - command-line client
  • wpa_gui - Qt4 based GUI client (only with the qt4 USE flag)
    • can be started in the system tray in the Xorg startup file .xinitrc
File~/.xinitrc

wpa_gui -t &   # -t : immediately minimize to system tray

Ethernet management

OpenRC automatically hotplugs all interfaces if configured in /etc/rc.conf

File/etc/rc.conf

rc_hotplug="*"

so the respective services will be started if that interface becomes available and ifplugd will be automatically started for every ethernet interface to ensure that the device gets configured when a carrier is acquired (e.g. a cable is plugged in). Ifplugd will emit beeps on state change which can be disabled in /etc/conf.d/net

File/etc/conf.d/netdisabling ifplugd beeping for interface ethernet0

ipfplugd_ethernet0="--no-beep"

References

  1. 1.0 1.1 Gentoo Handbook, Adding Functionality
  2. Gentoo Handbook, Wireless Networking