How to: Install a swap file in a Windows Azure Ubuntu VM 2.0

How to: Install a swap file in a Windows Azure Ubuntu VM

Unless you made your own Ubuntu image, configured the Azure connector/integration to create a swap file and uploaded it to Windows Azure chances are your VM does not come with a swap file/memory. Afterall Microsoft recommends that all Linux machines that are uploaded to the cloud come without a swap file to avoid issues. The application you install to configure your VM (think of sysprep for Linux) has a parameter to allow a swap file to be automatically created when you deploy the VM. The nice thing about it, is that now you can modify the configuration file and upon the restart of the service or reboot of the computer it will take care of creating the swap file for you! Isn’t that amazing!

BTW, I recommend reading this article too: How to: Configure Swappiness in Ubuntu. This is very important if you care about the performance of your VM after you have enabled swap memory.

So let’s get started!


  1. Edit the /etc/waagent.conf file:
    1. To create a swap file in the directory that’s defined by the ResourceDisk.MountPoint parameter, you can update the following three parameters:
      ResourceDisk.Format=y
      ResourceDisk.EnableSwap=y
      ResourceDisk.SwapSizeMB=xx

      Note The xx placeholder represents the desired number of megabytes (MB) for the swap file. As mentioned above, execute df -h to see how much available space you have on /mnt or /mnt/resouces. I say this because for example the 4GB temp storage disk after formatting and what not turns out to be around 3.7 GB. In that case I used 3.5GB or 3584 above to be on the safe side.

  2. Restart the WALinuxAgent service by running one of the following commands, depending on the system in question:
    Ubuntu: service walinuxagent restart
    Red Hat/Centos: service waagent restart
  3. Run one of the following commands to show the new swap apace that’s being used after the restart:
    dmesg | grep swap
    swapon -s
    cat /proc/swaps
    file /mnt/resource/swapfile
    free| grep -i swap
  4. If the swap file isn’t created, you can restart the virtual machine by using one of the following commands:
    shutdown -r now
    init 6

Personally I would strongly suggest you make sure you don’t assign more MB to the swap file than there are available otherwise you will run into system issues to the point you might not be able to log in to the server any more. Careful when resizing your VMs as well, as the available disk space may go up or down. Turn of swap files before resizing your VM and turn them back on when done. Obviously if you swap is say 2 GB then your temporary hard drive would never be smaller than that so you’re always safe. Generally you don’t need to reboot to observer the swap file being created and properly used. If it doesn’t work with the service restart double/triple check everything and use a reboot as a last resource. Good luck!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.