Swap

From OLPC
Revision as of 22:36, 29 July 2013 by Quozl (talk | contribs) (remove old warning, long since fixed)
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.

How to swap (page) to an SD card

Get a Secure Digital card. You can use a throwaway 1GB SD card, these are available for $3-$20 depending where you go. 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 half a gig on the card for file storage; the other half 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. If/when it starts to fail after a few years, you can copy any still-interesting user files off it, throw it away, and put in a new $1 1GB SD card (or something larger).

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,
  • on 8.2.1 and earlier, open the Journal, which "mounts" the USB drive to make its filesystem available (later versions do this automatically),
  • 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

Joyride 2311, and most other builds, contain the network block device driver as a module. In unusual situations that demand this, it is possible to swap to a file on another system over network connection.

Setup the other system:

# create 512Mb empty file
dd if=/dev/zero of=/tmp/xo-swap bs=1048576 count=512

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

Setup the XO:

# load the network block device module
modprobe nbd

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

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

# enable swap space
swapon /dev/nbd0

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


Earlier commentary about swapping

Swapping to a partition would overcome this???

Swap on the internal memory doesn't work either, it causes system freezes. Swap doesn't even like attaching to a jffs2 file, but can be forced by using a loopback device. The internal flash chip is only rated for 100,000 writes/cell. JFFS2 will try to spread that across the whole chip, even then swap write patterns are likely bad for it. Try the following at your own risk.

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