Wednesday, May 18, 2011

Creating SWAP space from a file

If you don't have SWAP on your system and you need it, you can create it from a file.


SWAP is a storage place, which is used by operating system to store files and data which are actually not used at the moment or not needed to be stored in RAM so that there will be enough free memory in the system. Some programs may use SWAP rather than RAM.


It is recommended to create SWAP partition when you have small RAM capacity. For common user with 2GB of RAM there is no need to create this partition.
Note than SWAP is used for system hibernation, if you don't have SWAP partition, this feature will not work for you. Be aware that hibernating to swap created from file will also not work properly.


How to create swap from file:
1. First wi will create an empty file:
mkdir -p /var/lib/swap  - placement of SWAP directory
dd if=/dev/zero of=/var/lib/swap/swapfile bs=1M count=128 - this will create actual file, in this case 128MB large

2. Next step is to create swap file:
mkswap /var/lib/swap/swapfile

3. Set swap to be used by the system:
swapon /var/lib/swap/swapfile
For disabling use command:
swapoff /var/lib/swap/swapfile

4. Now you can check if the SWAP was added to the system:
cat /proc/swaps

NOTE: this SWAP is only temporary, it will be not connected when the system reboots.
You can add following line into the file /etc/fstab for swap mounting during the start:
/var/lib/swap/swapfile swap swap defaults 0 0

No comments:

Post a Comment