Linux SWAP

From DD-WRT Wiki

Jump to: navigation, search

Contents

[edit] Introduction

[edit] What is SWAP?

Swap space is an auxiliary storage, such as a portion of a hard-disk, which can be used as memory by the operating system when system RAM is insufficient. This is especially useful on systems with very little system RAM, such as most DD-WRT compatible routers, as it helps prevent the system from running out of memory when multiple background processes are installed.

[edit] Do I need SWAP?

Swap can be helpful in situations where dd-wrt:

  • runs memory intensive applications (Optware)
  • runs multiple background processes (again, Optware)
  • runs on a router with less than 32 MB of RAM

Swap won't really be needed if:

  • you only use the router for its basic gateway/AP functions
  • you only run a few additional services or applications with modest memory requirements
  • your system has a lot of RAM (such as 64+ MB)

It is strongly advised that you do not enable swapping on flash-based drives (internal flash, SD/MMC, USB sticks)

The above statement is not correct for SDHC cards with built-in wear-leveling (e.g. via an USB SDHC-adaptor) - but keep the swapiness to minimum:

e.g.:

  • MicroSD(HC) cards are a sort of a SSD: December 7, 2008, robert.penz.name: No SWAP Partition, Journaling Filesystems, … on a SSD? Quote: "...They assume perfect wear leveling...We stay also with the 2 million cycles and assume a 16GB SSD *With 50 MByte/sec we get 20 years! *With 2 MByte/sec we get 519 years! *And even if we reduce the write cycles to 100.000 and write with 2 MByte/sec all the time we’re at 26 years!!...1. Never choose to use a journaling file system on the SSD partitions: Bullshit, you’re just risking data security. Stay with ext3...7. One more thing to consider is that flash-devices handle their space in blocks. The blocksize typically varies between 16KB and 512 KB. Therefore writing one byte may cause erase and rewrite of up to 512KB..."

[edit] Requirements

In order to use paging, you first need an external storage. This means that users with neutered routers (like the WRT54G v5) that only have 2MB of flash and run the Micro build of DD-WRT cannot enable swapping in any way. For everyone else, it's recommended to usb-attached HDDs or SSDs for swap space, as opposed to SD/MMC/flash disks. The reason is that swap is read/write intensive and will lead to flash wear.

The utilities required to enable swapping require your router to have Optware support. Follow the tutorial on installing ipkg-opt. Once you have ipkg-opt properly installed, connect to the router via telnet/ssh and issue the following commands (be advised as you may have problems if you didn't install optware support correctly!):

/opt/bin/ipkg-opt update
/opt/bin/ipkg-opt install busybox
 I've installed swap (on a CIFS share, but it should work on a USB device the same way) 
 without needing optware by using this method - why install an entire second busybox 
 when all you need is the swap-utils ipkg (and optionally kmod-loop and kmod-ext2 if you're 
 going to access the swap via a CIFS share):
 
 
 ipkg install swap-utils
 ipkg install kmod-loop
 ipkg install kmod-ext2
 insmod /jffs/lib/modules/2.4.30/loop.o
 insmod /jffs/lib/modules/2.4.30/ext2.o 
 dd if=/dev/zero of=/tmp/smbshare/swap.ext2 bs=1 seek=64M echo y | mke2fs -L optware /tmp/smbshare/swap.ext2
 mkdir /tmp/swap
 mount -o loop /tmp/smbshare/swap.ext2 /tmp/swap
 dd if=/dev/zero of=/tmp/swap/swapfile bs=1024 count=65535
 mkswap /tmp/swap/swapfile
 swapon -p1 /tmp/swap/swapfile
 
 Test if the swap is working with swapon -s
--Azuretech 07:50, 29 August 2009 (CEST)

[edit] Enabling swap

[edit] Using a swap file

To enable swapping using a swap file, you need to first create the file, prepare it for swapping and then enable the swapping on it.

[edit] Creating a swap file

First of, make sure the file system the disk you wish to swap on is properly mounted. For the purposes of this tutorial we will assume the disk is mounted as /mnt and we want to use the file /mnt/myswap.swp for swapping.

Use the following command to create a 16MB file that we will use for swapping:

dd if=/dev/zero of=/mnt/myswap.swp bs=1k count=16000

[edit] Preparing the swap file

Before we enable the swap file we must first set it up. The following command accomplishes just that:

/opt/bin/busybox mkswap /mnt/myswap.swp

[edit] Enabling the swap file

Now that the swap file has been created and set up, we need to enable swapping on it, using swapon:

/opt/bin/busybox swapon /mnt/myswap.swp

[edit] Using a swap partition

Setting up a swap partition is a bit more difficult, as the partition must be first created then formatted using the linux-swap file system. Once that is done, assuming the swap partition is at /dev/scsi/host0/bus0/target0/lun0/part5 (common for v24 USB drives with mutiple partitions), the only command that needs be issued is:

/opt/bin/busybox swapon /dev/scsi/host0/bus0/target0/lun0/part5

[edit] Checking swap

The easiest way to check if swap is working is to use the free command. Below is a sample output of the free command on a system with 32MB RAM and a 64 MB swap partition.

root@MyRouter:/opt/bin# free
                  total         used          free        shared      buffers
Mem:        29340        27652         1688            0         1184
Swap:       64220          300          63920
Total:        93560        27952        65608
root@MyRouter:/opt/bin#


Alternatively, if you have syslog enabled, you can check the file /var/log/messages for any system-generated swap-related messages.


[edit] ADVISORY

Do NOT use this method if using USB storage with MULTIPLE partitions. Refer to USB storage for further information.