November 4, 2013

Adding A Swapfile

During installation I did not create a swap partition.As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether.

To create a swap file two different commands can be used : dd or fallocate.

With dd ( swap file 2 GB in /media/Data, you can choose another location. I choose /media/Data, because my root file system is small and /media/Data is mounted at boottime ) :
sudo dd if=/dev/zero of=/media/Data/swap bs=1M count=2048

With fallocate :
sudo fallocate -l 2048M /media/Data/swap
fallocate: /media/Data/swap: fallocate failed: Text file busy........
Another try :
cd /media/Data
sudo fallocate -l 2048M swap
Done !

ls -l swap
-rw-r--r-- 1 root root 536870912 Nov  4 12:07 swap
Set the right permissions (a world-readable swap file is a huge local vulnerability)
sudo chmod 600 swap
ls -l swap
-rw------- 1 root root 536870912 Nov  4 12:07 swap
Better !

After creating the correctly-sized file, format it to swap:
sudo mkswap swap

Activate the swapfile:
sudo swapon swap 

You can verify this yourself with the swapon command :
swapon -s 
Filename                                 Type        Size             Used    Priority
/media/Data/swap                 file           2097148    0            -1


Edit /etc/fstab and add an entry for the swap file:
sudo nano /etc/fstab  
/media/Data/swap   none   swap   sw   0   0
Your location would be different....After the next reboot the swap will be used automatically.

To remove a swap file, the current swap file must be turned off.
sudo swapoff -a
sudo rm -f swap 

The Linux kernel assigns priorities to all swap containers. The system will use swap areas of higher priority before using swap areas of lower priority. Priorities can be assigned in fstab via the pri parameter or with sudo swapon -p <value>.
I'm using just one swap area, so priority is of no concern...

The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap area.
cat /proc/sys/vm/swappiness will show a number between 0 and 100. Default is 60.
Setting this parameter to a low value will reduce swapping from RAM, and is known to improve responsiveness on many systems.
To  change the system swappiness value, open /etc/sysctl.conf as root :
sudo nano /etc/syscl.conf
and add these lines :
vm.swappiness=0
vm.vfs_cache_pressure=50


Reboot for the change to take effect. You can also clear your swap by running swapoff -a and then swapon -a as root instead of rebooting to achieve the same effect.

vm.vfs_cache_pressure controls inode/dentry (i.e. filesystem) cache vs. other caches, i.e. we want to keep filesystem meta-data in RAM if possible ( link ).

Links :
https://wiki.archlinux.org/index.php/swap#Swap_file_creation
http://askubuntu.com/questions/103915/how-do-i-configure-swappiness
http://askubuntu.com/questions/157793/why-is-swap-being-used-even-though-i-have-plenty-of-free-ram
https://help.ubuntu.com/community/SwapFaq
http://unix.stackexchange.com/questions/4402/what-is-a-superblock-inode-dentry-and-a-file 

There is also a package dphys-swapfile that will autogenerate a swap file. After the package has been installed whenever you boot you'll have /var/swap created.

Using a memory stick/memory card as swap memory on Linux

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.