How to add a Linux swap on Azure

Andy

Administrator
Creative Team
User ID
1
Joined
7 Jan 2019
Messages
1,121
Reaction score
57
Points
48
Azure instances have temporary storage, on Linux instances, it's not mounted by default.

For example, let's have a look at B1ms:
Azure-B1ls.png


B1ls is available only on Linux for the best customer experience. Windows is not supported because the minimum recommended memory for the Windows OS is larger than what B1ls offers.

Open an SSH session to your Azure Linux instance and run these commands as root or using sudo:
  1. Test the available disk space for your instance:
    • andy@thelounge:~$ df -h
  2. Edit the Azure Linux Agent configuration file:
    • andy@thelounge:~$ sudo nano /etc/waagent.conf
  3. Update these parameters:
    • ResourceDisk.Format=y
    • ResourceDisk.EnableSwap=y
    • ResourceDisk.SwapSizeMB=4096
  4. Set the “ResourceDisk.SwapSizeMB” value to the number of megabytes to allocate to the swap file. Usually, this value can be up to the size of the installed system memory. Make sure you have sufficient available space on the disk to do this.
  5. Press <Ctrl>+<X>, <Y> and then <Enter> to exit the nano editor and save the “waagent.conf” file.
  6. Restart the Azure Linux Agent service:
    • andy@thelounge:~$ sudo service waagent restart
  7. Verify that your swap file is available:
    • andy@thelounge:~$ swapon -s
If you followed the instructions but the swap file isn’t available at this point, you can try restarting the virtual machine using:
Code:
shutdown -r now

Azure-B1ls-4GBswap.png

Code:
andy@thelounge:/mnt$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            176M     0  176M   0% /dev
tmpfs            39M  936K   38M   3% /run
/dev/sdb1        29G  1.8G   28G   7% /
tmpfs           194M     0  194M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           194M     0  194M   0% /sys/fs/cgroup
/dev/loop0       55M   55M     0 100% /snap/core18/1754
/dev/loop1       38M   38M     0 100% /snap/thelounge/91
/dev/loop2       28M   28M     0 100% /snap/snapd/7264
/dev/sdb15      105M  3.6M  101M   4% /boot/efi
/dev/sda1       3.9G  3.9G     0 100% /mnt
tmpfs            39M     0   39M   0% /run/user/1000
andy@thelounge:/mnt$ ls -alh
total 3.9G
drwxr-xr-x  3 root root 4.0K May 24 09:04 .
drwxr-xr-x 23 root root 4.0K May 24 09:04 ..
-r--r--r--  1 root root  639 May 23 19:02 DATALOSS_WARNING_README.txt
drwx------  2 root root  16K May 23 19:02 lost+found
-rw-------  1 root root 3.9G May 24 09:07 swapfile
andy@thelounge:/mnt$ free -h
              total        used        free      shared  buff/cache   available
Mem:           386M        236M         29M        868K        120M        137M
Swap:          3.8G         10M        3.8G
andy@thelounge:/mnt$
And some words of caution:
  • Ensure you don’t assign more space to the swap file than available on disk, otherwise, you can run into abnormal system behaviour, including not being able to log in to your instance.
  • Also, be careful when resizing your VM, as the available disk space may decrease to the point of causing issues stated before.
I tested this on Azure Ubuntu Server 18.04 LTS and it works like a charm 😃

I hope this tutorial been useful for you, feel free to post your questions or comments down below 👇
 
 Short URL:
Back
Top