Swap

From OLPC
Revision as of 19:55, 9 August 2013 by Quozl (talk | contribs) (nobody should be using 8.2.1, it has security vulnerabilities from being so old, so remove the advice specific to 8.2.1)
Jump to: navigation, search

Swap refers to using storage for data that does not fit into physical random-access memory.

The XO-1 has only 256 MiB of DRAM (see Hardware specifications); adding swap space may allow running more simultaneous instances of more complicated applications.

The XO-1.5, XO-1.75 and XO-4 have more DRAM, so swap space is rarely needed, except for special situations.

How to swap (page) to an SD card

Get a Secure Digital card. You can use a throwaway SD card, these are available for very little. I say throwaway because swapping to it will tend to burn it up faster than its "usual" lifetime for photos and such. You will still be able to use the rest of the card for file storage; about 512 MB will be for swap space. Plug it into the SD card slot on the XO. You'll have to keep it plugged in the whole time while you're swapping to it; you can't remove it the way you remove a USB stick or a non-swap SD card. When it starts to slow down after a few years, you can copy any still-interesting user files off it, throw it away, and put in a new SD card.

Swapping to a partition

You can dedicate a partition on a USB flash drive or SD card to use as swap.

In the Terminal activity or a console, become root. Type mount (and press Enter after this and all other commands), make sure the SD card is mounted at /dev/mmcblk0p1, in a "vfat" filesystem.

Go into the Journal, find the SD card at the lower left (it may be hidden by the Frame), hover over it, choose Unmount. Go back to the terminal.

Type mount, make sure /dev/mmcblk0p1 is not mounted any more. Type yum install parted since the partition editor is not part of the OLPC system software (you'll need to be on the Internet to do this). Run /sbin/parted /dev/mmcblk0. Type print to see the current configuration. On my 1GB SD card it looked like this:

Number  Start  End    Size    Type     File system  Flags
 1      127kB  1018MB 1018MB  primary  fat16

Type resize 1 0 512 to shrink this filesystem down to 512MB. If it asks you whether to use FAT32, say no if your card is 2GB or less. Then type mkpartfs primary linux-swap 512 1018. That'll make a second partition for swapping to, and format it as a Linux swap partition. (If your SD card is a different size, adjust these numbers to give you larger or smaller partitions that fit.) Type print and for a 1GB card it should look roughly like this:

Number  Start  End     Size   Type     File system  Flags
 1      32.3kB 512MB   512MB  primary  fat16
 2      512MB  1018MB  506MB  primary  linux-swap

Type quit to exit parted.

Now you're back to the shell.

Type /sbin/swapon /dev/mmcblk0p2 to add this second partition as a swap device. To have this partition used as swap when the system boots, add this line to /etc/rc.local.

The Hal daemon is smart enough to mount filesystems when it sees an SD card appear, but it's not smart enough to start using freshly available swap space. For the moment, you'll have to enter /sbin/swapon /dev/mmcblk0p2 each time after you insert the card. (Similarly, it won't automatically do a /sbin/swapoff if you try to eject the SD card, but removing swap from a running system is not a good idea.) I'm sure somebody will eventually come up with a Hal script or something to automate this.

Swapping to a file

A file can be used for swap. Here's a way to make a 256 MB swap file on an external filesystem.

  • insert the SD card or USB drive,
  • hover over the Journal's icon for the drive at the lower left and the Journal will show the path to where it's mounted, something like /mount/VOLUME_NAME
  • in a terminal, enter the commands
dd if=/dev/zero of=/media/VOLUME_NAME/swapfile bs=1M count=256
sudo mkswap /media/VOLUME_NAME/swapfile
sudo swapon /media/VOLUME_NAME/swapfile
  • enter swapon -s to verify the swapfile is being used.

As in the guide for using a swap partition, you'll have to enter sudo swapon /media/VOLUME_NAME/swapfile each time you start, and you mustn't remove the drive without entering sudo swapoff /media/VOLUME_NAME/swapfile.

The jffs2 filesystem can't be used for swap, an error "Invalid argument" occurs.

Swapping to a file on a file system instead of a partition is less efficient, but not notably so. The extra CPU cost of remapping the swap I/O is minimal.

Swap to network block device

The network block device driver is available as a module. In unusual situations that demand this, it is possible to swap to a file on another system over network connection. Last tested with 13.2.0.

Setup the other system:

# install the nbd package
sudo yum install -y nbd
# create a 512Mb empty file
sudo dd if=/dev/zero of=/root/xo-swap bs=1048576 count=512

# provide it over network block device protocol
sudo nbd-server 1234 /root/xo-swap

Setup the XO:

# install the nbd package
sudo yum install -y nbd

# load the network block device module
sudo modprobe nbd

# attach the network block device to the remote system
# (you may need to install the nbd package)
sudo nbd-client 10.0.0.1 1234 /dev/nbd0

# make swap space on the block device
sudo mkswap /dev/nbd0

# enable swap space
sudo swapon /dev/nbd0

Caution: the XO may hang if the network connection is lost.

XO-1 swap to loopback block device

XO-1 only. Swap won't attach to a file on internal storage, but this can be worked around using a loopback device. The internal storage is rated for 100,000 writes per cell. The filesystem will try to spread that across the whole chip. Swap on internal storage will shorten the life of the internal storage.

su
dd if=/dev/zero of=/var/swap count=128000
/sbin/mkswap /var/swap
#!/bin/sh
if [ `cat /proc/swaps | wc -l` == 1 ] ;then
    loopdev=`su -c "/sbin/losetup -f"`
    su -c "/sbin/losetup $loopdev /var/swap"
    su -c "/sbin/swapon $loopdev"
fi