Table of Contents
/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
ordd
if missing. - Initializes it securely with
mkswap
and setschmod 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# Clone the repository git clone https://github.com/Ryadel/azure-tempdisk-swapfile.git cd azure-tempdisk-swapfile # Install the script sudo install -D -m 0755 scripts/azure-tempdisk-swapfile.sh \ /usr/local/sbin/azure-tempdisk-swapfile.sh # Install the systemd service sudo install -D -m 0644 services/azure-tempdisk-swapfile.service \ /etc/systemd/system/azure-tempdisk-swapfile.service # (Optional) Install default configuration sudo install -D -m 0644 defaults/azure-tempdisk-swapfile \ /etc/default/azure-tempdisk-swapfile # Enable and start sudo systemctl daemon-reload sudo systemctl enable --now azure-tempdisk-swapfile.service |
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:
1 2 3 4 |
SWAP_SIZE=16G # Desired size (e.g. 8G, 16G, 32768M) SWAP_FILE=/mnt/swapfile WAIT_SECS=60 # Seconds to wait for /mnt |
If you change the size, the script will automatically recreate the swapfile at the next boot.
Verify
Check that the swapfile is active:
1 2 3 |
swapon --show free -h |
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.