Information Technology Grimoire

Version .0.0.1

IT Notes from various projects because I forget, and hopefully they help you too.

swapfile

Have a Swap File?

(1:483)# free -m
               total        used        free      shared  buff/cache   available
Mem:            1963         291         295           4        1377        1475
Swap:              0           0           0
(1:485)# swapon --show

Have Space for a Swapfile?

(1:484)# df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           197M  1.0M  196M   1% /run
/dev/vda1        58G  2.9G   56G   5% /
tmpfs           982M  216K  982M   1% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/vda15      105M  6.1M   99M   6% /boot/efi
tmpfs           197M  4.0K  197M   1% /run/user/0
294/1963MB      0.44 0.16 0.06 1/183 528716

Allocate Swapfile

This should be the same as your total memory

(1:487)# fallocate -l 2G /swapfile

Set Permissions

(1:489)# chmod 600 /swapfile
(1:490)# ls -lh /swapfile
-rw------- 1 root root 2.0G Oct  5 06:40 /swapfile

Convert to Swap Usage

(1:491)# mkswap /swapfile
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=000f1cc9-775f-4cb8-8454-acea027074dc

(1:492)# swapon /swapfile

Have a Swap File now?

(1:493)# swapon --show
NAME      TYPE SIZE USED PRIO
/swapfile file   2G   0B   -2

(1:494)# free -m
               total        used        free      shared  buff/cache   available
Mem:            1963         293         291           4        1379        1473
Swap:           2047           0        2047

Make it Permanent

(1:495)# cp /etc/fstab /etc/fstab.bak
(1:496)# echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab

Tune the Swap Usage

(1:497)# cat /proc/sys/vm/swappiness
60

(1:498)# sysctl vm.swappiness=10
vm.swappiness = 10

(1:499)# cat /proc/sys/vm/vfs_cache_pressure
100

(1:500)# sysctl vm.vfs_cache_pressure=50
vm.vfs_cache_pressure = 50

Make tunings permanent

(1:501)# vim /etc/sysctl.conf
vm.swappiness=10
vm.vfs_cache_pressure=50