How to create a swap for Azure Ubuntu VM?

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:

  • https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-how-to-inject-custom-data/
  • https://wiki.ubuntu.com/AzureSwapPartitions
  • https://bugs.launchpad.net/ubuntu/+source/cloud-init/+bug/1374166

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?


Solution 1:

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

Solution 2:

Bruno's answer is a great starting point, but it only worked after I rebooted and gave it another minute after booting.

a. Enable swap in /etc/waagent.conf, 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.

b. Do the following as root, which includes rebooting your machine:

umount /mnt
service walinuxagent restart
reboot

c. After booting it will still take some time before the swap is actually enabled. You can check it with swapon -s.

Solution 3:

I believe the right way to do this so that both cloud-init and the waagent play 'nice' together (from Cloud-Init Azure docs) is to keep these values set to this

# disabling provisioning turns off all 'Provisioning.*' function
Provisioning.Enabled=n
# this is currently not handled by cloud-init, so let walinuxagent do it.
ResourceDisk.Format=y
ResourceDisk.MountPoint=/mnt

I tried changing the mountpoint but it didn't seem to work properly so the docs are probably accurate about the values

And then you can customize the swap options as you want

# Create and use swapfile on resource disk.
ResourceDisk.EnableSwap=y

# Size of the swapfile.
ResourceDisk.SwapSizeMB=8192

A basic restart picks up the new swap fine

sudo service walinuxagent restart

free -m
             total       used       free     shared    buffers     cached
Mem:          3944        882       3061         44         29        163
-/+ buffers/cache:        689       3255
Swap:         8192          0       8192