OpenWrt Forum Archive

Topic: [Howto] Enable swap

The content of this topic has been archived on 6 May 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

[Howto] Enable swap

Tested on a ASUS WL-500g Premium with Kamikaze 7.07 (brcm-2.4).

1. Install kmod-loop, losetup and swap-utils packages

ipkg install kmod-loop losetup swap-utils

2. Create the UCI configuration file (/etc/config/swap)

config swap
        option path     '/tmp'
        option filename 'swapfile'
        option size     '2000'

3. Create the init script to initialize the swap space on every boot

#!/bin/sh /etc/rc.common
# Copyright (C) 2007 OpenWrt.org
START=98
sleep 5

start_service () {
        local section="$1"
        config_get path     "$section" path
        config_get filename "$section" filename
        config_get size     "$section" size

        dd if=/dev/zero of=/$path/$filename count=$size
        losetup /dev/loop/0 /$path/$filename
        mkswap /dev/loop/0
        swapon /dev/loop/0
}

start() {
        config_load "swap"
        config_foreach start_service swap
}

stop() {
        swapoff /dev/loop/0
}

4. Make the init script executable

chmod +x /etc/init.d/swap

5. Enable the swap space, by default a swap space with 1MiB on /tmp in the ramdisk will be created.

/etc/init.d/swap enable
/etc/init.d/swap start

6. You can change a few parameters using UCI.

OPTION      DEFAULT VALUE    DESCRIPTION
path        /tmp             Location where swap file will be stored.
filename    swapfile         Name of the swap file.
size        2000             Size in blocks: 1000 blocks = 512 Kbytes | 1 Megabyte = 2000 blocks

E.g. to change to location of the swap file to your USB pen drive and change the size of the swap space to 256MiB do:

uci set swap.cfg1.path=/mnt/usb
uci set swap.cfg1.size=512000
uci commit swap
/etc/init.d/swap restart

7. With the 'free' command you can check the swap space usage...

              total         used         free       shared      buffers
  Mem:        30512        13856        16656            0         1316
 Swap:          992            0          992
Total:        31504        13856        17648

Finally, if someone like they can package the two files in a ipk package which automatically installs the other required packages as the dependencies...

Have fun smile

(Last edited by forum2006 on 7 Sep 2007, 12:43)

The idea is good. Thanks for sharing this.

But it is too limited and shaky.
Your config should be rather:

config swap "icandoonlyoneswaploop0"
    option ...

and forget config_foreach when using the hardcoded loop device.

The start part should check whether the swap file has not been already mounted.
It should also check whether the swap file exists (and maybe the size). The creation takes the time and the swap file does not have to be in the ramdisk.
The loop device must be dynamic (losetup -f). It would be good idea to save the state similarly to network scripts to be able to umount it properly.
You should also delete the loop device when umounting it.

Do you know that the rc.common enables you to pass parameters to rc functions (7.07 and higher)? It would be great to name sections and to have the possibility to mount or umount only the particular swap file.
Would it be good to extend it to swap partitions?

I am sorry for only the critical remarks but I do not have a device to verify my ideas.

To integrate this feel free to post patches in this thread here.

Btw. Critical comments are always welcome.

Edit: The above solution works for me and I don't feel to do all the work. A start is there now were you and others can continue from...

Some more comments from nbd  on IRC about this script:

<nbd> - don't use mkswap on every init
<nbd> only if swapon fails
<nbd> - don't hardcode use of /dev/loop
<nbd> some people may want to use it on real devices
<nbd> why add path and filename, if you don't need them to be separate?

So, it's up to you what you make out of it smile

(Last edited by forum2006 on 7 Sep 2007, 14:11)

Could anyone give the right way to enable swap then.
Personally I think this is very important to everyone who use sticks or hard drives.

Ugh... is there any special reason for going through a loop device? I just used
mkswap /path/to/file
swapon /path/to/file
and it worked fine.

My init script for whiterussion RC9

#!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.org

HD=/dev/scsi/host0/bus0/target0/lun0/part1

start() {
        if [ -e $HD ]
        then
                swapon $HD
        fi
}
stop () {
        swapoff $HD
}

Basicly it checks if my harddrive is connected and else it does nothing.

(Last edited by belrpr on 22 Sep 2007, 13:22)

Hello and excuse my ignorance

I have created a dedicated swap partition on an external (usb2.0) hdd... The results of fdisk -l look like this :

root@OpenWrt:/# fdisk -l

Disk /dev/scsi/host0/bus0/target0/lun0/disc: 10.2 GB, 10242892800 bytes
255 heads, 63 sectors/track, 1245 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

                                 Device Boot      Start         End      Blocks   Id  System
/dev/scsi/host0/bus0/target0/lun0/part1               1        1200     9638968+  83  Linux
/dev/scsi/host0/bus0/target0/lun0/part2            1201        1245      361462+  82  Linux swap / Solaris

However, it looks like my router doesn't take advantage of the swap partition since issuing the command free gives the following:
root@OpenWrt:/# free
              total         used         free       shared      buffers
  Mem:        30520        21972         8548            0         1724
Swap:            0            0            0
Total:        30520        21972         8548

Is there anything else that I should do, like initializing the swap @ boottime?
Thanks in avance!

I tried with mkswap and swapon, but the swap file is not in use, everything is free...
root@OpenWrt:/opt$ free
              total         used         free       shared      buffers
  Mem:        30516        29664          852            0          212
Swap:       250856            0       250856
Total:       281372        29664       251708
As you can see, there too little ram left... How can I make it swap? Do you need a config file?
Thanks

(Last edited by gixie on 6 Feb 2008, 04:04)

Someone else asked the question but I saw no reply to it - I've tried swap partitions and swap files and a number of cpu-intensive activities, but have yet to see swap actually USED on my rb532 kamikaze instance.

Is there some trick to getting the device to take advantage of swap space, or is CF i/o so slow that swap doesn't really buy you anything in terms of resource utilization?

-d-

I think the swap isn't  used well by open wrt on usb sticks and external harddisks.
I have the same problem. Even if the RAM is almost filled (98 %) the swap isn't used well :
I never saw being used more than 48-56 KB. like in the screen :
the image is taken while I was copying from a lan station to vsftpd server of the Asus wl 500p with openwrt kamikaze 7.09.
I copied to an external hdd Seagate 500 Gb which was mounted as an ext3 filesystem. The rates were good enough : 2,6 MB/sec . which is not quite bad. I transfered large files of 350 MB.
The swap is used from the a usb stick of 512 Mb. I mounted the swap with different priorities :
swapon -p 500 /dev/scsi/host0/bus0/target0/lun0/part2,    but the results were the same.

root/ $ free
                total         used         free       shared      buffers
  Mem:        30512        29620          892            0          388
Swap:       120476           48       120428
Total:       150988        29668       121320

root/ $ free
              total         used         free       shared      buffers
  Mem:        30512        29768          744            0          340
Swap:       120476           48       120428
Total:       150988        29816       121172

root/ $ free
              total         used         free       shared      buffers
  Mem:        30512        29724          788            0          412
Swap:       120476           48       120428
Total:       150988        29772       121216

root / $ free
              total         used         free       shared      buffers
  Mem:        30512        29760          752            0          416
Swap:       120476           44       120432
Total:       150988        29804       121184

root / $ free
              total         used         free       shared      buffers
  Mem:        30512        29904          608            0          340
Swap:       120476           52       120424
Total:       150988        29956       121032

My question is :
Is there someone on this forum who knows what to do in order to use efficiently the swap ? And what must be done for that ? Usb sticks are good ? What is better ? Internal hdd ? On external hdd the problem is that they enter in sleep mode after 10 minutes, and they are not reccomended for swap.
Any ideeas ?

(Last edited by youth4ever on 19 Jun 2008, 10:10)

youth4ever wrote:

I think the swap isn't  used well by open wrt on usb sticks and external harddisks.
I have the same problem. Even if the RAM is almost filled (98 %) the swap isn't used well :
I never saw being used more than 48-56 KB. like in the screen :
the image is taken while I was copying from a lan station to vsftpd server of the Asus wl 500-p with openwrt kamikaze 7.09.

Why should linux use swap, if it isn't absolutely necessary?
On the other hand, linux tries to use as much RAM as possible for buffers/disk cache, so memory usage will be nearly 100% soon.
And vsftpd is not a very memory-consuming program. Maybe you should try squid or mysql server, to see your swap grow :-D

My wgt634u running a lot of services (squid, pure-ftpd, openvpn, lighttpd, pptpd, samba server, print server,  ...):

output of free
              total         used         free       shared      buffers
  Mem:        30028        28400         1628            0          612
 Swap:       514040        32768       481272
Total:       544068        61168       482900

top of "top"
Mem: 28440K used, 1588K free, 0K shrd, 616K buff, 5208K cached

Thank you for your quick answer. Yes, you made the point. smile

Do you use a usb stick for swap ?

thanks.

I do use a LaCie 40GB 2,5" USB mobile harddrive.
It's running non-stop since mid 2005 without any problems :-)
With a USB stick, I would not use a swap partition that large...

I've read that for USBsticks and other solid state memory, a  swap partition is not a good idea.  An example is the Asus EeePC which uses Xandros Linux for an all solid-state mini-laptop -- no swap disk and strongly not recommended.  Apparently solid-state memory's useful life is extended if there are generally less writes and mostly reads.

Swap disk are reputed to perform best when on two separate IDE or SATA controllers. With USB hard disks, that option just isn't usually available.

The discussion might have continued from here.