Swap: Difference between revisions

From OLPC
Jump to navigation Jump to search
(wiki markup, link to Secure Digital card, corruption bug is <trac>6532, etc.)
Line 1: Line 1:
[[Swap]] refers to using storage for data that does not fit into physical random-access memory.
Swap on SD cards is 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 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.
The SD suspend bug(s) are fixed in Joyride releases 2263 and later (maybe others) and in Build 710 or later of the preceding

releases. 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.
== Swap broken in early software releases ==

Swap on [[Secure Digital card]]s 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_notes/8.1.2|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!
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 ==
== How to swap (page) to an SD card ==


Get a [[Secure Digital card]].
Use a recent joyride software release (this requires getting your XO a [[Activation and developer keys|developer key]], which is a good idea anyway since it unlocks your machine and puts you in control of it). Get a throwaway 1GB SD card. Available for
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
$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
will tend to burn it up faster than its "usual" lifetime for photos
Line 20: Line 27:
larger).
larger).


Go to a terminal (either the Terminal activity, or Ctrl-Alt-F1). Become root.
In the [[Terminal activity]] or a [[console]], become [[root]].
Type "<tt>mount</tt>", make sure the SD card is mounted at <tt>/dev/mmcblk0p1</tt>,
Type <tt>mount</tt> (then press Enter after this and all other commands), make sure the SD card is mounted at <tt>/dev/mmcblk0p1</tt>,
in a "vfat" filesystem.
in a "vfat" filesystem.


Go into the Journal, find the SD card hiding behind the Frame at the
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.
bottom, hover over it, pick Unmount. Go back to the terminal.


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


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


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


Number Start End Size Type File system Flags
Number Start End Size Type File system Flags
Line 46: Line 52:
2 512MB 1018MB 506MB primary linux-swap
2 512MB 1018MB 506MB primary linux-swap


Type "<tt>quit</tt>". Now you're back to the shell. Type "<tt>/sbin/swapon /dev/mmcblk0p2</tt>". You're done.
Type <tt>quit</tt> to exit parted.

Now you're back to the shell.

Type <tt>/sbin/swapon /dev/mmcblk0p2</tt> to add this second partition as a swap device. You're done.


The Hal daemon is smart enough to mount filesystems when it sees an SD
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
card appear, but it's not smart enough to start using freshly
available swap space. For the moment, you'll have to do "<tt>/sbin/swapon
available swap space. For the moment, you'll have to enter <tt>/sbin/swapon
/dev/mmcblk0p2</tt>" each time after you reboot the XO. Similarly, it
/dev/mmcblk0p2</tt> each time after you reboot the XO. Similarly, it
won't automatically do a "<tt>/sbin/swapoff</tt>" if you try to eject it. I'm sure
won't automatically do a <tt>/sbin/swapoff</tt> if you try to eject the SD card. I'm sure
somebody will eventually come up with a Hal script or something to
somebody will eventually come up with a Hal script or something to
automate those parts.
automate those parts.


== Swap to Network Block Device ==
== 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.
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.
Line 103: Line 113:
fi
fi


[[Category:Howto]]
[[Category:HowTo]]
[[Category:FAQ]]

Revision as of 07:21, 12 March 2009

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. You're done.

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 reboot the XO. Similarly, it won't automatically do a /sbin/swapoff if you try to eject the SD card. I'm sure somebody will eventually come up with a Hal script or something to automate those parts.

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