Swap

From OLPC
Revision as of 23:33, 30 January 2010 by Normanshulman (talk | contribs) (How to swap (page) to an SD card)
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.

Note: It's confusing because besides its 256 of RAM memory, the XO-1 also uses flash memory as storage: in the XO-1's 1 GB of built-in NAND Flash and on an external USB flash drive or SD card.

Swap broken in early software releases

Swap on Secure Digital cards was broken in many XO software releases. When the bug is present, the XO forcibly unmounts the SD card at suspend, which loses the swap file, and breaks all applications that touched the swap, most notoriously X11. It also often overwrites the first blocks of your SD card with zeros or gibberish, which makes them no longer work until you reformat them entirely. NOT FUN!

The SD suspend bug(s) (<trac>6532</trac>)was fixed in Joyride releases 2263 and later and in Build 710 or later which corresponds to Release 8.1.2. Having the bug fixed allows an SD card to be used as swap space (for demand paging), making your small RAM seem larger and your XO more responsive.

The recipe below WILL NOT WORK if you have the XO's original software release, or the "Update.1" release. Use at your own risk!

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).

In the Terminal activity or a console, become root. Type mount (then 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.

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