Swap: Difference between revisions

From OLPC
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
[[Swap]] refers to using storage for data that does not fit into physical random-access memory.
[[Swap]] refers to using storage for data that does not fit into physical random-access memory.

== Introduction ==


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 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.
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]].
Get a [[Secure Digital card]].
You can use a throwaway SD card, these are available for
You can use a throwaway SD card, these are available for
very little. I say throwaway because swapping to it
very little. We 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
and such. You will still be able to use the rest of the card for
and such. You will still be able to use the rest of the card for
Line 19: Line 19:
it, throw it away, and put in a new SD card.
it, throw it away, and put in a new SD card.


=== Swapping to a partition ===
== Swap to a partition on a device ==
You can dedicate a partition on a USB flash drive or SD card to use as swap.
A partition on an SD card or USB drive can be used for swap.

Disadvantages:
* need to repartition the device,


In the [[Terminal activity]] or a [[console]], become [[root]].
In the [[Terminal activity]] or a [[console]], become [[root]].
Line 59: Line 62:
won't automatically do a <tt>/sbin/swapoff</tt> 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.
won't automatically do a <tt>/sbin/swapoff</tt> 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 ==
== Swap to a file on a device ==
A file can be used for swap.
A file on a filesystem on an SD card or USB drive can be used for swap.


Advantages:
Advantages:
Line 93: Line 96:
* repeat the steps each time you reboot,
* repeat the steps each time you reboot,


Last tested with 13.2.0.
== Swap to network block device ==

== Swap to a file on another system, using the network block device ==


Swap can be added as a file on another system, with access over network connection using the network block device driver, which is available as a module.
Swap can be added as a file on another system, with access over network connection using the network block device driver, which is available as a module.
Line 103: Line 108:


Disadvantages:
Disadvantages:
* the XO may hang if the other system is shut down,
* the XO may hang if the other system is shutdown, or the network connection is lost, without first turning off swap,
* the XO may hang if the network connection is lost,
* the network may be loaded,
* the network may be loaded,
* disk space is needed on the other system.
* disk space is needed on the other system.
Line 138: Line 142:
Last tested with 13.2.0.
Last tested with 13.2.0.


== XO-1 swap to loopback block device ==
== Swap to a file on internal device ==


XO-1 only. Swap won't attach to a file on internal storage, because of a limitation of the filesystem driver, but this can be bypassed using a loopback block device.
Swap won't attach to a file on internal storage on XO-1, because of a limitation of the filesystem driver, but this can be bypassed using a loopback block device.


Advantages:
Advantages:
Line 146: Line 150:


Disadvantages:
Disadvantages:
* the internal storage is rated for 100,000 writes per cell, the filesystem will try to spread that across the whole device, so this will shorten the life of the internal storage, and it will become permanently slower much earlier than planned by the designers.
* the XO-1 internal storage is rated for 100,000 writes per cell, the filesystem will try to spread that across the whole device, so this will shorten the life of the internal storage, and it will become permanently slower much earlier than planned by the designers.


Recipe:
Recipe:

Revision as of 01:40, 10 August 2013

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

Introduction

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.

Get a Secure Digital card. You can use a throwaway SD card, these are available for very little. We 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.

Swap to a partition on a device

A partition on an SD card or USB drive can be used for swap.

Disadvantages:

  • need to repartition the device,

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.

Swap to a file on a device

A file on a filesystem on an SD card or USB drive can be used for swap.

Advantages:

  • avoid dedicating a partition on external device,

Disadvantages:

  • the XO may hang if the device is removed without first turning off swap,
  • 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.

Here's a way to make a 256 MB swap file on an external filesystem.

  • remove all other SD cards or USB drives, (to make the following simplest),
  • insert the SD card or USB drive,
  • in Terminal, as Root, copy and paste the commands:
 # find the SD card or USB drive and make a file name for it
 D=/run/media/olpc/
 E="$D$(ls $D)"
 F=$E/swap
 
 # create a 256 MB file
 dd if=/dev/zero of="$F" bs=1M count=256
 
 # mark the file as a swap file
 sudo mkswap "$F"
 
 # start using the file
 sudo swapon $F
  • to verify the swapfile is being used:
 sudo swapon -s
  • to safely remove the device,
 sudo swapoff -a
  • repeat the steps each time you reboot,

Last tested with 13.2.0.

Swap to a file on another system, using the network block device

Swap can be added as a file on another system, with access over network connection using the network block device driver, which is available as a module.

Advantages:

  • swap space can be added when needed rather than always available,
  • additional swap space can be added at any time,
  • no damage to endurance of SD card or USB drive,

Disadvantages:

  • the XO may hang if the other system is shutdown, or the network connection is lost, without first turning off swap,
  • the network may be loaded,
  • disk space is needed on the other system.

Setup the other system:

 # install the nbd package
 yum install -y nbd
 
 # create a 512Mb empty file
 dd if=/dev/zero of=/root/xo-swap bs=1048576 count=512
 
 # provide it over network block device protocol on port 1234
 nbd-server 1234 /root/xo-swap

Setup the XO:

 # install the nbd package
 yum install -y nbd
 
 # load the network block device module
 modprobe nbd
 
 # attach the network block device to the remote system
 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

Last tested with 13.2.0.

Swap to a file on internal device

Swap won't attach to a file on internal storage on XO-1, because of a limitation of the filesystem driver, but this can be bypassed using a loopback block device.

Advantages:

  • no additional hardware or network required.

Disadvantages:

  • the XO-1 internal storage is rated for 100,000 writes per cell, the filesystem will try to spread that across the whole device, so this will shorten the life of the internal storage, and it will become permanently slower much earlier than planned by the designers.

Recipe:

 # set the name of the swap file
 FILE=/var/swap
 
 # create the file and preallocate the space
 dd if=/dev/zero of=$FILE count=32768
 
 # write the swap signature pattern into the file
 /sbin/mkswap $FILE
 
 # create a loopback block device representing the file
 LOOP=$(losetup --show --find $FILE)
 
 # turn on swap through the loopback device
 swapon $LOOP

Last tested on 13.2.0.