Gentoo Wiki


This article is part of the Tips & Tricks series.
Terminals / Shells Network X Window System Portage System Filesystems Kernel Other



With Linux, the swap is a dedicated partition in most cases. But what to do when you want to increase the size of this swap in a already installed system? The solution is to create a dynamic swap. Its data will be in a big file. It can be any file, but the blocks of this file must be contiguous.

There are at least 3 cases where it is not necessary to have a dedicated partition for the swap(s):

Warning: A dynamic swap is somewhat slower as a regular swap in a dedicated partition. In consequence, for the first case to work well, you must be sure that you have enough RAM memory (2GB or more must be a good figure). So, you must be in the second case too. Otherwise, it will be better to partition again your drive even if it will implies to re-install all from the scratch.

Creation of the dynamic swap

The creation of a swap zone in a file is done with the following sequence of instructions:

Code: Creation of the swap file
# dd if=/dev/zero of=/monswap bs=1024 count=131072
131072+0 records in
131072+0 records out
134217728 bytes (134 MB) copied, 4.08474 s, 32.9 MB/s

We will not necessarily get a file with continuous blocks with such a command like cp, so we use dd instead. The count= parameter can be used to set the size of the swap. of= is used for the file name and path.

Code: Creation of the swap structure in the file
# mkswap /monswap
Setting up swapspace version 1, size = 134213 kB
no label, UUID=c9d06273-00c8-44e6-a060-9ee59536c98b
Code: Setup of the file protection
# chmod 600 /monswap

This is needed because we don't want to get warning messages from swapon, and we don't want that this file will be accidentally read or removed.

Code: Drives synchronization
# sync
Code: Swap activation
# swapon -v /monswap
swapon on /monswap

We can verify that this swap is active with free that will report the total size of the swap, or by reading /proc/swaps:

Code: Verification
# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/monswap                                file            131064  208     -1
/mnt/data/monswap2                      file            131064  0       -2

We can see at I have 2 swap files in my system, one on each hard disk in my system. I also have a little bit more as 2BG RAM, and the system is using only a few blocks on the first swap. The system was heavily loaded at that time with several programs running: revdep-rebuild, claws-mail, firefox and amule was only a few of them.

We can suppress this swap with swapoff (the file will not be removed or modified):

Code: Swap suppression
# swapoff /monswap

In order to automatize the activation of this swap, we can add it in fstab:

Code: /etc/fstab
# <fs>      <mountpoint>    <type>          <opts>      <dump/pass>
/monswap    swap            swap            defaults    0    0

The only precaution is to be sure that the order by which the swapon -a and mount -a commands are issued when booting the system is correct. The file system on which the swap file reside must be mounted before the swapon -a command is issued.

If the correct order cannot be granted with fstab, you can add a swapon command and a swapoff command as stated above in /etc/conf.d/local.start and /etc/conf.d/local.stop respectively.


This is a translation - adaptation of O'Reilly: Augmenter la taille de swap sans utiliser de partition dédiée


Retrieved from ""

Last modified: Sat, 04 Oct 2008 13:49:00 +0000 Hits: 889