Using a swapfile on linux

As of version 2.6, swapfiles are apparently no slower than a swap partition. This is great news, because swap files are far more flexible than whole partitions.

Take very careful note of the performance claims in that LKML posting, though: "Create the swapfile when the filesystem is young and empty, it'll be nice and contiguous." and "In 2.6 partitions and swapfiles have the same reliability and they will have the same performance unless the swapfile is badly fragmented" (my emphasis). In other words, retrofitting a swap file onto an existing system can have negative performance implications, particularly at boot/swapon time. We've seen a swapfile on one shared hosting server, for instance, take 16 minutes to run swapon /data/swapfile, which doesn't bode well for a quick, clean boot.

Outline

  1. Create a file
  2. Format it
  3. Add fstab entry
  4. Activate it with swapon

  5. Exclude it from backups

Making a swapfile

A swapfile can be any file you want. We use dd to create and zero it out. Zeroing it out is important, as the file must not be sparse.

2x $amount_of_physical_memory isn't a bad guideline.

# this is for a 1GiB swapfile
dd if=/dev/zero of=/data/swap bs=1M count=1024

Make it read-write only by root, otherwise users could in theory read private data that's been swapped out.

chmod 600 /data/swap

Format it

You need to put some magic numbers in the swap file.

mkswap /data/swap

If you're curious, here's what hexdump has to say about a fresh 10MiB swapfile, it's not terribly exciting.

nanoha:~# hexdump -C  swap                      
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000400  01 00 00 00 ff 09 00 00  00 00 00 00 86 42 cf 73  |.............B.s|
00000410  da 04 44 c5 bb 83 47 4d  0e 83 2e 87 00 00 00 00  |..D...GM........|
00000420  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000ff0  00 00 00 00 00 00 53 57  41 50 53 50 41 43 45 32  |......SWAPSPACE2|
00001000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00a00000

Add to fstab

# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/data/swap      swap            swap    defaults        0       0

Mount it

Now that you have an entry in fstab, you can check it's correct by letting the system try to activate it.

  1. Check what's active, probably nothing right now

    nanoha:~# cat /proc/swaps
    nanoha:~#
  2. Activate all system swaps

    nanoha:~# swapon -a
  3. Check that it's using the swapfile now

    nanoha:~# cat /proc/swaps
    Filename                                Type            Size    Used    Priority
    /data/swap                              file            1048568 0       -1

That's super!

Exclude it from backups

Now that you've created this monster, you don't want to back it up. Adjust the rdiff-backup or amanda exclusions as necessary (it's what we use on our systems).

  • For amanda, you'll want to add an entry to /data/.amandaexclude

    ./swap
  • For rdiffs, you need to edit the exclusion list in config-management and roll it out - /backup/$SERVERNAME-exclude

    /data/swap

That's it!


References/External Links