In this page I describe how to enable a swap file on a router (Asus RT-AC56U)
- we use an Entware installation, so please
refer to this page on how to install Entware on a router.
The swap file is used to increase the available physical memory (RAM) with a
virtual memory (SWAP) in order to have more resources and to let services have
more available memory to be executed. A swap file is created on an external USB
drive. We choose two different location where we can store and manage this swap
file. One location is inside "/opt" folder (this is the root folder of an
Entware installation), the other one is inside the root folder of the
mounted USB external drive (the root folder location is stored inside the
settings of the router). To create the swap file we use scripts. We give a look
to the first script that creates the swap file in "/opt" folder and enable it:
#!/bin/sh DIR_DL="/opt/swap" # look for file existence if [ ! -e "$DIR_DL" ]; then # create a 256MB swap file ("count" is in Kilobytes) dd if=/dev/zero of=$DIR_DL bs=1k count=262144 # set up the swap file mkswap $DIR_DL fi # enable swap swapon $DIR_DL
Inside the above-mentioned script, the file "swap" (the filename of the swap
file), together with path "/opt", is put inside variable "DIR_DL". We check if
this file exists, if it does not exist we create an empty file with 256 MB size.
The "mkswap" instruction is used to make the file a swap file, in this
way the operative system knows that this file is to be used as swap file.
The last instruction "swapon" just enables the swap file.
An alternate script to create the swap file is the following one:
#!/bin/sh SWAP_FOLDER=`nvram get apps_mounted_path` DIR_DL="$SWAP_FOLDER/swap" # look for file existence if [ ! -e "$DIR_DL" ]; then # create a 256MB swap file ("count" is in Kilobytes) dd if=/dev/zero of=$DIR_DL bs=1k count=262144 # set up the swap file mkswap $DIR_DL fi # enable swap swapon $DIR_DL
The above-mentioned script is similar to the first one, but in this case we
read the settings of the router using "nvram" instruction in order to achieve
the root folder of the mounted USB external drive. This folder name usually
starts with "/tmp/mnt" as this folder is the root folder of all mounted USB
external drive. The rest of the script is the same as the first one.
After creating the script to create and enable the swap file, we have to execute
it whenever we insert the external USB drive, please read the section "How
to install Entware on a router" on how to execute a script whenever an
external USB drive is mounted.
We need also to create another script to disable the swap file whenever we
remove the external USB drive. In this case we just use the instruction "swapoff"
to disable the swap file.
The first script file uses the "/opt" folder to disable the swap file:
#!/bin/sh DIR_DL="/opt/swap" # disable swap swapoff $DIR_DL
The second script file reads the router settings in order to know where is the swap file:
#!/bin/sh SWAP_FOLDER=`nvram get apps_mounted_path` DIR_DL="$SWAP_FOLDER/swap" # disable swap swapoff $DIR_DL
There is an command, inside the router, to know how much swap memory is used and free, this command is named "free", this is a sample output of this command:
free total used free shared buffers Mem: 255892 54432 201460 0 168 Swap: 262140 0 262140 Total: 518032 54432 463600
We can also use the command "cat /proc/swaps" to know all informations about the swap file, this is a sample output of this command:
cat /proc/swaps Filename Type Size Used Priority /opt/swap file 262140 59256 -1