CentOS 7.x adding swap space using file

From Notes_Wiki
Revision as of 07:40, 28 July 2018 by Saurabh (talk | contribs) (Created page with "<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 c...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<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

  1. Install qemu-img package to create swap file. We can also create file using dd.
    yum -y install qemu-img
  2. 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
  3. Set correct file permissions on the swap file
    chmod 0600 /swap.raw
  4. Format file as swap
    mkswap /swap.raw
  5. Add entry in /etc/fstab for the file using following contents
    /swap.raw none swap defaults 0 0
  6. Mount all partitions and enable swap
    mount -a
    swapon -a
  7. 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>