ONLamp.com    
 Published on ONLamp.com (http://www.onlamp.com/)
 See this if you're having trouble printing code examples


Big Scary Daemons Expanding Small NetBSD Systems

by Michael W. Lucas
09/25/2003

In the previous article, we installed NetBSD on a HP Jornada 728 palmtop. Anyone who has worked with any BSD knows that the base operating system doesn't include many programs that most people use in day-to-day work. Unless you only need vi(1) and ssh(1), you must install additional software to make your palmtop useful and comfortable. If you're running a palmtop — or, indeed, any sort of small hardware with NetBSD installed — you'll probably want to expand the system.

NetBSD uses a standard BSD-style ports and packages system, making installing generic software painless. You could just download packages from the NetBSD FTP site and install them — this works exactly as it does on any other architecture NetBSD supports. I like to customize my packages, however, and often the latest and greatest versions of software are not available as packages. That leaves the ports system, which is also easy to use — if you have sufficient robust disk space. Flash cards can only withstand a limited number of writes before wearing out, which makes them anything but robust. While I expect flash disks to last for a while, there's no point in pushing my luck. Even though we're running on a palmtop, we have a full UNIX system available, giving us an obvious option for additional write-friendly disk space: NFS.

Enabling NFS

As NFS maps user accounts by UID, make certain to synchronize the user IDs on the palmtop with those on the server. This is very simple on a handheld device; just use vipw(8) to edit the password file and change the UID of your regular user, then use chown(1) to change the ownership of your home directory to the new UID. If you've done a lot of customization on your palmtop already, be sure to track down any lingering remnants of your old UID as well. If, for some reason, you cannot change your UIDs to match your server, or if you wish to mount files from two different NFS servers, mount_umap(8) will allow you to map arbitrary UIDs on your palmtop.

Also in Big Scary Daemons:

Running Commercial Linux Software on FreeBSD

Building Detailed Network Reports with Netflow

Visualizing Network Traffic with Netflow and FlowScan

Monitoring Network Traffic with Netflow

Information Security with Colin Percival

To enable NFS services on a NetBSD server, set the following in /etc/rc.conf. Any NFS server can provide remote filesystems for your palmtop, but as this is an article about NetBSD, we'll use a NetBSD server as an example.

rpcbind=YES
nfs_server=YES

You also must configure /etc/exports to export your palmtop's remote filesystems. On the NFS server, I support my palmtop in the directory /home/mwlucas/palmtop. To configure a NetBSD NFS server to export the home directory tree to a palmtop at 192.168.1.55, use the following line in /etc/exports. Again, tweak this as appropriate for whatever operating system you're using as a NFS server.

/home -alldirs -maproot=root: 192.168.1.55

Installing pkgsrc

The two requirements for building packages on the palmtop are swap space and the pkgsrc tree. The pkgsrc tree is easy to install; just download the pkgsrc.tar.gz file from ftp.netbsd.org or a mirror and extract it in the proper place.

# cd /home/mwlucas/palmtop
# tar -xzvf pkgsrc.tar.gz

Enabling Swap Space

Swap space is only slightly more complicated. Most UNIX-like operating systems let you use a file as swap space. We're going to create an empty file of the proper size, and then configure the palmtop to use that file as swap space. Use dd(1) to create a swap file.

# dd if=/dev/zero of=/home/mwlucas/palmtop/swap bs=4k count=32k

Also set the permissions on your swap file so that nobody but you can read it. While your palmtop is a single-user system, you don't want another user on the NFS server to be able to extract sensitive data from your palmtop's swap space.

# chmod 600 /home/mwlucas/palmtop/swap

Configuring Your Palmtop

Now that the server is ready, configure the palmtop. We'll need mount points for our pkgsrc tree and the swap space. Swap is not normally "mounted," but as we're swapping to a file, the file must be visible to the system.

# mkdir /usr/pkgsrc
# mkdir /swap

We need to have the pkgsrc directory mounted rw (read/write) to build the software. You could use various /etc/mk.conf options to have a read-only pkgsrc tree, but in that case, you'll need another NFS-mounted partition on which to build the packages. Additionally, using the soft and intr options gives the palmtop a chance to recover gracefully, should the network connection go away. (This isn't guaranteed to work, but it's better than having no chance whatsoever for graceful recovery.) Finally, be sure that you use the noauto option so that the palmtop will boot without a live network connection! All of this combines to create the following /etc/fstab entry for /usr/pkgsrc:

nfsserver:/home/mwlucas/palmtop/pkgsrc /usr/pkgsrc nfs  rw,soft,intr,noauto 0 0

A simple mount /usr/pkgsrc will now make the package source tree appear on your system.

The swap file entry is a little different. NetBSD can mount a swap file directly, rather than requiring a directory containing just the swap file. The mount point must be a directory, but it magically becomes a file when you activate it. The boot process will not hang if the network is not available, so we can skip the noauto function. Just specify the mount point as an option:

nfsserver:/home/mwlucas/palmtop/swap   none     swap    sw,nfsmntpt=/swap

You could just hardcode the swap file into /etc/rc.conf and have the palmtop use it no matter what, but the network might not be available at boot. Even if you have a wireless network card, an advantage of a palmtop is that you can carry it everywhere with you. You need to be able to enable and disable swap at will, turning it on when you want to do something computer-intensive, such as building packages, and turning it off when you're checking your shopping list or playing DOOM. Use swapctl(8) for this.

To start, list all of your configured swap devices with swapctl -l.

# swapctl -l
#

As your palmtop booted without swap space configured, this is empty. Use the -A option to tell swapctl(8) to enable all swap listed in /etc/fstab.

# swapctl -A
swapctl: adding nfsserver:/home/mwlucas/palmtop/swap as swap device
	at priority 0
#

The -l option now gives quite different results.

# swapctl -l
Device      512-blocks     Used    Avail Capacity  Priority
/swap           262144        0   262144     0%    0
#

Our swap space is idle, but once we start building packages it'll pick up quickly.

Managing NFS

Once you integrate NFS into your palmtop, you need manage it carefully. NFS requires a live network connection. If you are accessing a NFS filesystem and unplug the network card, your system can hang or freeze. With the soft,intr NFS mount option your palmtop should eventually recover from /usr/pkgsrc disappearing, but if swap is in use, that's another story. Always remove your swap space and unmount your NFS shares before removing the network card! Disconnect the swap space by using swapctl -d and the full path to the swap device.

# swapctl -d /swap
#

While you're no longer swapping to that file, df(1) will show that the /swap file is still mounted. Before disconnecting the network, be sure to unmount all of your NFS-mounted shares.

# umount /swap
# umount /usr/pkgsrc

This all quickly becomes routine. A shell script that would parse /etc/fstab and automatically enable any NFS partitions and swap partitions it finds would be a nice addition for small systems, especially if it could be used to easily unmount and disconnect those partitions in preparation for a network disconnect. Any takers?

Conclusion

Given swap space and the pkgsrc tree, you can now configure your small NetBSD system in any way you desire, from a game machine to a simple security scanner to a quite unique Internet server.

Michael W. Lucas


Read more Big Scary Daemons columns.

Return to the BSD DevCenter.

Copyright © 2009 O'Reilly Media, Inc.