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.)
No edit summary
 
(38 intermediate revisions by 3 users not shown)
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 volatile data that does not fit into 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.


= Introduction =
''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 XO-1 has only 256 MiB of memory (see [[Hardware specifications]]); adding swap space may allow running more simultaneous instances of more complicated applications.
== Swap broken in early software releases ==


The XO-1.5, XO-1.75 and XO-4 have more memory, so swap space is rarely needed, except for special situations.
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!

== How to swap (page) to an SD card ==


Get a [[Secure Digital card]].
Get a [[Secure Digital card]].
You can use a throwaway 1GB SD card, these are available for
You can use a throwaway SD card, these are available for
$3-$20 depending where you go. 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 half a gig on the card for
and such. You will still be able to use the rest of the card for
file storage; the other half will be for swap space. Plug it into the
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
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
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
remove a USB drive 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
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
it, throw it away, and put in a new SD card.
larger).


= How much swap? =
In the [[Terminal activity]] or a [[console]], become [[root]].
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 the examples below we use 64 MiB of swap. This is a reasonable amount for ordinary interactive use of an XO-1, and can substantially improve user experience when demand is high.
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.


As an example of the improvement, on 13.2.0 one can open many Sugar activities without closing the previous activity, and then switch between them with <tt>alt-tab</tt>. Without swap this is impractical.
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 part of the OLPC system software (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
current configuration. On my 1GB SD card it looked like this:


Special situations may require more swap.
Number Start End Size Type File system Flags
1 127kB 1018MB 1018MB primary fat16


The device you use may have ample space, so using more is reasonable.
Type <tt>resize 1 0 512</tt> 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 <tt>mkpartfs
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
size, adjust these numbers to give you larger or smaller partitions that fit.) Type <tt>print</tt>
and for a 1GB card it should look roughly like this:


= Swap to a file on a device =
Number Start End Size Type File system Flags
A file on a filesystem on an SD card or USB drive can be used for swap.
1 32.3kB 512MB 512MB primary fat16
2 512MB 1018MB 506MB primary linux-swap


Advantages:
Type <tt>quit</tt> to exit parted.
* avoids dedicating a partition on external device,
* avoids repartitioning,


Disadvantages:
Now you're back to the shell.
* the XO may hang if the device is removed without first turning off swap<ref name="hang">once the system is using a resource for swap, it relies on it heavily, and if the resource is lost the system may hang, so you have to make sure it isn't lost, or if it is lost use the power button to reboot.</ref>,
* slightly less efficient<ref>swap to a file on a filesystem is less efficient than swap to partition, but not notably so; the extra CPU cost of remapping the swap I/O is minimal, and at the time the swap is used the system is already under demand.</ref>.


Recipe for Sugar:
Type <tt>/sbin/swapon /dev/mmcblk0p2</tt> to add this second partition as a swap device. You're done.
* download [http://dev.laptop.org/~quozl/MakeSwap-3.xo MakeSwap-3.xo],
* insert an SD card<ref name="covering">if a child is to be using the laptop with swap, then it may be appropriate to cover the slot with adhesive so that the card is not easily removed.</ref> with a FAT filesystem<ref>MakeSwap does not work with anything other than FAT formatted cards. SD cards are FAT formatted and optimised by the manufacturer, so if you have reformatted them you will need to use other instructions.</ref>,
* run the activity, wait for it to say it is done, then quit,
* the activity can be safely run again (it does nothing new), or erased (swap continues to be available).


Recipe for GNOME:
The Hal daemon is smart enough to mount filesystems when it sees an SD
* remove all other SD cards or USB drives, (to make the following simplest),
card appear, but it's not smart enough to start using freshly
* insert the SD card or USB drive<ref name="sd_better">it is best to use an SD card rather than a USB drive</ref>,
available swap space. For the moment, you'll have to enter <tt>/sbin/swapon
* in Terminal, as [[Root]], copy and paste the commands:
/dev/mmcblk0p2</tt> each time after you reboot the XO. Similarly, it
# find the SD card or USB drive and make a file name for it
won't automatically do a <tt>/sbin/swapoff</tt> if you try to eject the SD card. I'm sure
D=/run/media/olpc/
somebody will eventually come up with a Hal script or something to
E="$D$(ls $D)"
automate those parts.
F=$E/swap
# create a 64 MiB file
dd if=/dev/zero of="$F" bs=1M count=64
# 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.1. Does not work exactly the same way on earlier releases.
== Swap to network block device ==


= Swap to an entire 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.
An entire SD card or USB drive can be used for swap.

Advantages:
* suited to use with a child<ref name="covering" />,

Disadvantages:
* need to repartition the device, which loses all files, and may affect performance of the device,
* the device should not be removed once inserted<ref name="hang" />.

Recipe:
* in the [[Terminal activity]], become [[root]], then install the gparted package:
yum install -y gparted
* insert the SD card or USB drive, and use gparted (Applications -> System Tools -> GParted Partition Editor) to delete the manufacturer's partition and add a linux-swap partition, then add it to the filesystem table:
eval $(blkid -o export /dev/mmcblk1p1) # get the UUID
echo UUID=$UUID swap swap defaults 0 0 >> /etc/fstab # add to table
swapon -a -v # turn on swap

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<ref name="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:
Setup the other system:


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


Setup the XO:
Setup the XO:


# install the nbd package
# load the network block device module
yum install -y nbd
modprobe nbd
# attach the network block device to the remote system
# load the network block device module
modprobe nbd
# (you may need to install the nbd client package)
nbd-client 10.0.0.1 1234 /dev/nbd0
# 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
# make swap space on the block device
mkswap /dev/nbd0
# enable swap space
swapon /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:
Caution: the XO may hang if the network connection is lost.
* no additional hardware or network required.


Disadvantages:
* the XO-1 will become slower over months instead of years<ref>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 using the internal storage as swap will shorten the life of the internal storage, and it will become ''permanently'' slower much earlier than planned by the designers.</ref>.


Recipe:
== Earlier commentary about swapping ==
# set the name of the swap file
FILE=/var/swap
# create the file and preallocate the space
dd if=/dev/zero of=$FILE bs=1M count=64
# 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.
:Swapping to a partition would overcome this???


= Monitoring =
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.


* the {{Code|swapon -a}} and {{Code|free}} commands are helpful for confirming that swap is available,
su
* using a USB drive with an indicator is helpful for assessing when swap is being accessed,
dd if=/dev/zero of=/var/swap count=128000
* the {{Code|top}} command is helpful for seeing how much swap and memory demand is happening.
/sbin/mkswap /var/swap


= Notes =
#!/bin/sh
{{reflist}}
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


[[Category:HowTo]]
[[Category:HowTo]]

Latest revision as of 01:28, 17 July 2014

Swap refers to using storage for volatile data that does not fit into memory.

Introduction

The XO-1 has only 256 MiB of memory (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 memory, 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 drive 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.

How much swap?

In the examples below we use 64 MiB of swap. This is a reasonable amount for ordinary interactive use of an XO-1, and can substantially improve user experience when demand is high.

As an example of the improvement, on 13.2.0 one can open many Sugar activities without closing the previous activity, and then switch between them with alt-tab. Without swap this is impractical.

Special situations may require more swap.

The device you use may have ample space, so using more is reasonable.

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:

  • avoids dedicating a partition on external device,
  • avoids repartitioning,

Disadvantages:

  • the XO may hang if the device is removed without first turning off swap<ref name="hang">once the system is using a resource for swap, it relies on it heavily, and if the resource is lost the system may hang, so you have to make sure it isn't lost, or if it is lost use the power button to reboot.</ref>,
  • slightly less efficient<ref>swap to a file on a filesystem is less efficient than swap to partition, but not notably so; the extra CPU cost of remapping the swap I/O is minimal, and at the time the swap is used the system is already under demand.</ref>.

Recipe for Sugar:

  • download MakeSwap-3.xo,
  • insert an SD card<ref name="covering">if a child is to be using the laptop with swap, then it may be appropriate to cover the slot with adhesive so that the card is not easily removed.</ref> with a FAT filesystem<ref>MakeSwap does not work with anything other than FAT formatted cards. SD cards are FAT formatted and optimised by the manufacturer, so if you have reformatted them you will need to use other instructions.</ref>,
  • run the activity, wait for it to say it is done, then quit,
  • the activity can be safely run again (it does nothing new), or erased (swap continues to be available).

Recipe for GNOME:

  • remove all other SD cards or USB drives, (to make the following simplest),
  • insert the SD card or USB drive<ref name="sd_better">it is best to use an SD card rather than a USB drive</ref>,
  • 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 64 MiB file
 dd if=/dev/zero of="$F" bs=1M count=64
 
 # 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.1. Does not work exactly the same way on earlier releases.

Swap to an entire device

An entire SD card or USB drive can be used for swap.

Advantages:

  • suited to use with a child<ref name="covering" />,

Disadvantages:

  • need to repartition the device, which loses all files, and may affect performance of the device,
  • the device should not be removed once inserted<ref name="hang" />.

Recipe:

yum install -y gparted
  • insert the SD card or USB drive, and use gparted (Applications -> System Tools -> GParted Partition Editor) to delete the manufacturer's partition and add a linux-swap partition, then add it to the filesystem table:
 eval $(blkid -o export /dev/mmcblk1p1)               # get the UUID
 echo UUID=$UUID swap swap defaults 0 0 >> /etc/fstab # add to table
 swapon -a -v                                         # turn on swap

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<ref name="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 64 MiB empty file
 dd if=/dev/zero of=/root/xo-swap bs=1048576 count=64
 
 # 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 will become slower over months instead of years<ref>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 using the internal storage as swap will shorten the life of the internal storage, and it will become permanently slower much earlier than planned by the designers.</ref>.

Recipe:

 # set the name of the swap file
 FILE=/var/swap
 
 # create the file and preallocate the space
 dd if=/dev/zero of=$FILE bs=1M count=64
 
 # 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.

Monitoring

  • the swapon -a and free commands are helpful for confirming that swap is available,
  • using a USB drive with an indicator is helpful for assessing when swap is being accessed,
  • the top command is helpful for seeing how much swap and memory demand is happening.

Notes

<references group=""></references>