Posted On March 31, 2019

Linux Swapfile

kimconnect 0 comments
blog.KimConnect.com >> Linux >> Linux Swapfile

There’s a more updated post here.

Setup Swap on CentOS Localhost:

# Check memory & swap file
free -m

# Check available disk space
df -h

# Allocate swapfile, set appropriate permissions, create swapfile
sudo dd if=/dev/zero of=/swapfile count=4096 bs=1MiB #allocate
chmod 600 /swapfile #secure the directory
mkswap /swapfile #make swapfile in the /swapfile directory
swapon /swapfile #configure system to use /swapfile

# Check swapfile settings
swapon -s

# Make permanent
vim /etc/fstab
# add this line
/swapfile none swap defaults 0 0
Ansible Playbook

---
# need to be root
- debug: msg="{{ ansible_memtotal_mb }}M swapfile size"

- stat: path=/swapfile
register: swap

- debug: var=swap.stat.exists

- command: fallocate -l {{ ansible_memtotal_mb }}M /swapfile
when: swap.stat.exists == false

- file: path=/swapfile mode=0600
when: swap.stat.exists == false or (swap.stat.exists == true and swap.stat.mode != 0600)

- command: mkswap /swapfile
when: swap.stat.exists == false

- command: swapon /swapfile
when: swap.stat.exists == false

- lineinfile: dest=/etc/fstab
regexp='^/swapfile'
line='/swapfile none swap sw 0 0'
when: swap.stat.exists == false

- sysctl: name=vm.swappiness value=1 state=present

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

How To Install Ubuntu Server 20.04

Overview: As of January 2021, the free version of Redhat, namely CentOS 8 has been…

How To Configure Alternative Storage for a Kubernetes (K8s) Worker Node

The below illustration is assuming that one has a local RAID mount being added to…

How to Remove Apps in CentOS/Redhat

Check the repo directory: [root@localhost ~]# ls /etc/yum.repos.d CentOS-Base.repo CentOS-Sources.repo CentOS-CR.repo CentOS-Vault.repo CentOS-Debuginfo.repo docker-ce.repo CentOS-fasttrack.repo…
Other project: DragonCoin.com