CentOS 7.x adding swap space using file
From Notes_Wiki
<yambe:breadcrumb>CentOS_7.x_file_system_management|CentOS 7.x file system management</yambe:breadcrumb>
CentOS 7.x adding swap space using file
In CentOS 7.x swap can be configured via disk partitions. It can also be configured from file located on any of the existing file-systems. To create a file and configure it as swap use following steps
- Install qemu-img package to create swap file. We can also create file using dd.
- yum -y install qemu-img
- Use qemu-img to create swap file of desired size
- qemu-img create -f raw -o preallocation=full /swap.raw 2G
-
- The file can be created anywhere instead of /swap.raw suggested in these steps
- Set correct file permissions on the swap file
- chmod 0600 /swap.raw
- Format file as swap
- mkswap /swap.raw
- Add entry in /etc/fstab for the file using following contents
- /swap.raw none swap defaults 0 0
- Mount all partitions and enable swap
- mount -a
- swapon -a
- Check whether additional swap is visible
- free -m
These steps can be used to add swap to cloud (Amazon AWS or Azure) etc. VMs as they do not come with swap. However, various cloud providers recommend not enabling swap and increasing ram instead.
<yambe:breadcrumb>CentOS_7.x_file_system_management|CentOS 7.x file system management</yambe:breadcrumb>