Lack of Swap Space in Azure Linux VM

I’ve recently started using the CentOS Linux VM’s in Azure for some work that requires Linux. One thing I noticed quickly after starting to use them was that these VM’s do not come with any swap space configured by default.

You can go ahead and create a swap partition using the standard methods, but when you reboot the VM it won’t perisist, which was a bit infuriating. That was until I discovered that you can make this change in the Azure Linux agent that is installed by default on each VM and it will take care of it for you.

All you need to do is edit the file that is located at /etc/waagent.conf and locate the two lines below

 

#Create and use swapfile on resource disk. 
ResourceDisk.EnableSwap=n 
#Size of the swapfile. 
ResourceDisk.SwapSizeMB=0

 

To add in some Swap space all you need to do is alter this set to true and your desired space, and restart the VM.

# Create and use swapfile on resource disk. 
ResourceDisk.EnableSwap=y 
# Size of the swapfile. 
ResourceDisk.SwapSizeMB=5000

If you want this to be enabled by default you are going to need to create an image based on the standard image, but with these settings changed.