Ubuntu – How to create a swap for Azure Ubuntu VM

azurecloud-initUbuntu

I have read quite a few posts on this, but I am still unsure about the correct approach, assuming:

  1. I have a default Ubuntu 14.04 LTS VM created by and running on Azure, which doesn't come with a swap

  2. I would like to create a swap using existing VM storage, instead of create a new disk using additional storage

Posts I have read:

Many solutions were discussed but I can't seem to find one that will persists across server reboots (probably due to cloud-init has it's own idea about image partitioning), can someone advise me on the best practice?

Best Answer

Assuming that you have Linux Agent installed. All you have to do is to enable swap under /etc/waagent.conf. These are the relevant lines:

ResourceDisk.Format=y                   # Format if unformatted. If 'n', resour$
ResourceDisk.Filesystem=ext4            # Typically ext3 or ext4. FreeBSD image$
ResourceDisk.MountPoint=/mnt/resource   #
ResourceDisk.EnableSwap=y               # Create and use swapfile on resource d$
ResourceDisk.SwapSizeMB=2048            # Size of the swapfile.

It will automatically use the resource disk (which comes with every VM) to create the swap. There's no need to create a disk for it.

Update: You also need to execute the steps below in order to create the swapfile:

umount /mnt
service walinuxagent restart