Configuring the network

Next, let's look at some basic network configurations so that we can communicate with the outside world. I am assuming that there is an Ethernet interface, eth0, and that we only need a simple IP v4 configuration.

These examples use the network utilities that are part of BusyBox, and are sufficient for a simple use case, using the old-but-reliable ifup and ifdown programs. You can read the man pages on both for more details. The main network configuration is stored in /etc/network/interfaces. You will need to create these directories in the staging directory:

etc/network
etc/network/if-pre-up.d
etc/network/if-up.d
var/run

For a static IP address, etc/network/interfaces would look like this:

auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
  address 10.0.0.42
  netmask 255.255.255.0
  network 10.0.0.0

For a dynamic IP address allocated using DHCP, etc/network/interfaces would look like this:

auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp

You will also have to configure a DHCP client program. BusyBox has one named udchpcd. It needs a shell script that should go in /usr/share/udhcpc/default.script. There is a suitable default in the BusyBox source code in the directory examples//udhcp/simple.script.

Network components for glibc

glibc uses a mechanism known as the name service switch (NSS) to control the way that names are resolved to numbers for networking and users. User names, for example, may be resolved to UIDs via the file /etc/passwd; network services such as HTTP can be resolved to the service port number via /etc/services, and so on. All this is configured by /etc/nsswitch.conf, see the manual page, nss(5) for full details. Here is a simple example that will suffice for most embedded Linux implementations:

passwd:      files
group:       files
shadow:      files
hosts:       files dns
networks:    files
protocols:   files
services:    files

Everything is resolved by the correspondingly named file in /etc, except for the host names, which may additionally be resolved by a DNS lookup.

To make this work, you need to populate /etc with those files. Networks, protocols, and services are the same across all Linux systems, so they can be copied from /etc in your development PC. /etc/hosts should, at least contain, the loopback address:

127.0.0.1 localhost

We will come to the others, passwd, group, and shadow, later.

The last piece of the jigsaw is the libraries that perform the name resolution. They are plugins that are loaded as needed based on the contents of nsswitch.conf, meaning that they do not show up as dependencies if you use readelf or similar. You will simply have to copy them from the toolchain's sysroot:

$ cd ~/rootfs
$ cp -a $TOOLCHAIN_SYSROOT/lib/libnss* lib
$ cp -a $TOOLCHAIN_SYSROOT/lib/libresolv* lib
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.151.164