How To Create a NFS Swap partition (OpenSlug version)
(see below for OpenDebianSlug notes)
First off, here's a shout-out to my friends (dyoung and robbat2) helping me out late at night (my timezone) and getting this job done. robbat2 is from the gentoo-embedded crew and explained this recipe to me. So no more delay... Here's the secret sauce.
Why do you want an NFS swap partition
I haven't run this on Unslung. It works on OpenSlug (I'll be testing some more on today's build 05/06/2005). While the slug is a great little workhorse, it's short on memory. Sometimes having a swap device comes in handy -- often while natively compiling. Sometimes one doesn't have a harddisk attached or any device attached, but a little extra memory would be great for performance or just to allow stuff to work.
How To do it
I'm assuming you are a little familiar with NFS and have an NFS server running.
- Export a directory you want to have the swapfile in from the NFS server.
- Change directory on the NFS server to that directory and create a swapfile. The following command creates a zeroed 128MB file called swapfile. As always be careful when using dd, and *double* check the command before you hit enter
dd if=/dev/zero of=swapfile bs=1M count=128
- Start the NFS server and portmap on the server machine if it's not already started.
- On the TARGET (slug) NFS mount the partition. In my case it looked something like the following (192.168.1.5 is the NFS server IP address):
mkdir /mnt/swap
mount -t nfs 192.168.1.5:/exported_NFS_directory /mnt/swap
- On the TARGET, initialize the swapfile partition. N.B. I spent about 1 hour chasing down the fact that my Host and Target systems are opposite endian. So the Host created swapfile had a tiny little error when mounting the swap device. This is avoided by initializing the swapfile partition from the TARGET. The command to do this is
mkswap /mnt/swap/swapfile
- scp the loopback kernel module to the slug if you don't already have it. I'll be adding this as a default module pretty soon, but in the meantime you can just (from you build box find the loop.ko kernel module in build/tmp) and
ipkg install kernel-module-loop
Note: My build machine and my NFS server are the same device. Yours may be different.
- From the slug target install the kernel module if one needs to:
modprobe loop
- loopmount the swap file as a loop device:
losetup /dev/loop0 /mnt/swap/swapfile
- Setup the device for swap usage:
mkswap /dev/loop0
- Mount the swap partition on the local loop device:
swapon /dev/loop0
- Enjoy!
- cat /proc/meminfo to see the swap space is avialable to the system
cat /proc/meminfo
Making it permanent
This is all good, but it can get a bit annoying doing that on every boot.
First, create the file /etc/init.d/swap (you may have to modify this a bit):
cat > /etc/init.d/swap
#!/bin/sh
set -e
case "$1" in
start)
echo -n "Starting swap: "
modprobe loop
losetup /dev/loop0 /swapfile
swapon /dev/loop0
echo "done"
;;
stop)
echo -n "Stopping swap: "
swapoff /dev/loop0
losetup -d /dev/loop0
echo "done"
;;
restart)
swapoff /dev/loop0
swapon /dev/loop0
;;
*)
echo "Usage: swap { start | stop | restart }" >&2
exit 1
;;
esac
exit 0
^D (that is pressing Ctrl+D)
Then make it executable:
chmod +x /etc/init.d/swap
Then make it start:
update-rc.d swap defaults
Done. On the next reboot it should mount everything.
Getting the loopback module in OpenDebianSlug is a challenge (finding the equivalent of ipkg install kernel-loop-module). There may be easier ways, but one option is to do the following (different package for versions other than 2.7beta). Ideally you would do this in the initial install.