TIP_Dynamic_swap
| Terminals / Shells • Network • X Window System • Portage • System • Filesystems • Kernel • Other |
Contents |
Introduction
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):
- You forget the swap when partitioning your drive and don't want to change the partition scheme.
- You have a lot of RAM memory and your system is almost never using the swap.
- You want to increase the size of your swap.
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.
Credit
This is a translation - adaptation of O'Reilly: Augmenter la taille de swap sans utiliser de partition dédiée
TODO
- Check my English, any volunteer?
- Translate and adapt the second part of O'Reilly's article about dynamic swap on a windows partition. I don't want to do this because I have no way to test it.
Created by NickStallman.net, Luxury Homes Australia
Real estate agents should be using interactive floor plans and list their apartments, townhouses and units.
