Create persistent swap partition on Azure Linux VM

Create persistent swap partition on Azure Linux VM

Create persistent swap partition on Azure Linux VM


Swap space is storage on a disk device that is used when the system’s RAM (physcial memory) is full. When a Linux system runs out of RAM, inactive memory pages are moved from the RAM to the swap space. Swap space can take the form of either a dedicated swap partition or a swap file. It is recommended that the swap space should reside on a dedicated partition and a swap file should be used as a last resort. This article is not a deep dive on swap space in Linux but we did want to refresh the concept of it. In this post, we will explain how we could create swap space on a Linux VM on Azure cloud that would persist across a reboot.

The swap partition created using the standard methods may not persist after a machine reboot, for a Linux virtual machine hosted in Microsoft Azure environment. Microsoft Azure, however, provides the option to create a swap partition on the VM, using the /dev/sdb partition and WAAGENT service. The WAAGENT service is an Azure Linux agent for Microsoft Azure environment and will be present in Azure Linux virtual machines by default on each VM. The /dev/sdb partition is an ephemeral partition i.e. the data stored in this partition will be lost after each machine reboot. So we can make use of this partition as our swap partition.

To enable swap space in Azure linux VM, you need to do edit the configruation file for the waagent service located at /etc/waagent.conf and update the following entries:

#Create and use swapfile on resource disk.
#Size of the swapfile.

Change the ResourceDisk.EnableSwap=y and ResourceDisk.SwapSizeMB=5120, the value 5120 = 5GB. This will create a /swapfile in the resource disk that will persist across a reboot.
By default, the resource disk in an Azure Virtual Machine will be /mnt/resource(/dev/sdb) and the same will be mentioned in the waagent.conf file as well as shown in the below output.

[sahil@linuxnix ~]$ grep ResourceDisk.MountPoint /etc/waagent.conf

Therefore, the full path of our swapfile will be /mnt/resource/swapfile.

Given below are the updated entries for EnableSwap and SwapsizeMB parameters that we mentioned earlier.

#Create and use swapfile on resource disk.


#Size of the swapfile.


After the change is made, we need to restart the waagent service. For RHEL based systems, we would use the following command to do so:

sudo service waagent restart

After restarting the service, our swap parition should be mounted on the server as shown below.

[sahil@linuxnix ~]$ free -g
      total used free shared buff/cache available
Mem:    27   13   12      0       1        13
Swap:   4    0    4
[sahil@linuxnix ~]$


We hope that you found this post to be useful and we look forward to your suggestions and feedback.

The post Create persistent swap partition on Azure Linux VM appeared first on The Linux Juggernaut.

Administration Basics Cloud/Virtulization Devops LinuxNix swap