How to set Linux swapfile on an Azure VM temp disk A lightweight service + script tool to create and activate a swapfile on Azure’s temporary disk (/mnt) at every boot

How to create an Azure SQL Managed Instance
Fork me on GitHub
If you’ve already read my article about azure-tempdisk-bootstrap, you know how useful it is to take advantage of Azure’s ephemeral resource disk to store temp and cache data for Nginx, PHP, and .NET apps. That project was all about improving web server performance by relocating temporary workloads to /mnt.

This time, I want to introduce you to a complementary project that focuses on another perfect use case for the Azure temporary disk: the swapfile. The project is called azure-tempdisk-swapfile, and its full source code is freely available on GitHub. Its job is simple but critical: it ensures that your VM always has an active swapfile on /mnt, even after reboots or deallocations, when the ephemeral disk is wiped clean.

Why swap belongs on /mnt

The Azure local temporary disk is physically attached to the VM host, usually SSD or NVMe, which makes it very fast. Since swap doesn’t need persistence, it’s an ideal candidate for /mnt. The only catch is that the disk’s contents vanish after deallocation, leaving you without swap unless you recreate it. That’s where this project comes in.

azure-tempdisk-swapfile automates the whole process. At every boot, it waits for /mnt to mount, checks for the swapfile, recreates it if missing or resized, runs mkswap, and activates it with swapon. Once installed, you don’t have to worry about swap anymore.

How it works

  • Runs at startup via a systemd oneshot service.
  • Waits until /mnt is mounted (up to a configurable timeout).
  • Creates the swapfile with fallocate or dd if missing.
  • Initializes it securely with mkswap and sets chmod 600.
  • Activates it with swapon, idempotently (does nothing if already correct).

Installation

The project repository is here: github.com/Ryadel/azure-tempdisk-swapfile.

Clone the repo and install it step by step:

From now on, the service will automatically ensure your swapfile is recreated and activated at boot.

Configuration

Edit /etc/default/azure-tempdisk-swapfile to adjust settings:

If you change the size, the script will automatically recreate the swapfile at the next boot.

Verify

Check that the swapfile is active:

You should see /mnt/swapfile among the active swap devices.

Why I wrote it

Running multiple WordPress and .NET sites on Azure VMs, I’ve often hit memory pressure. Scaling up RAM helps, but having a reliable swapfile on /mnt acts as a safety net. It’s not a replacement for real memory, but it can prevent crashes during spikes. I wanted a clean, reliable, and automated way to set it up—and that’s what azure-tempdisk-swapfile delivers.

Conclusion

If you’re already using azure-tempdisk-bootstrap to optimize your temp directories on Azure, azure-tempdisk-swapfile is the perfect companion to complete the picture. Together, they help you get the most out of Azure’s ephemeral disk—boosting performance where it counts, while keeping your setup safe and automated.

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.

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