How to add swap file to Azure Linux VM

  • fennng 

Login to your VM

Run the following command to edit the config file

 sudo vi /etc/waagent.conf


I set the swap file to 4000M as my vm only 4000M available

use df -h to check avaialbe disk space on the temp drive /dev/sdb1

df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            952M     0  952M   0% /dev
tmpfs           193M  1.2M  192M   1% /run
/dev/sda1        29G  3.1G   26G  11% /
tmpfs           964M  992K  963M   1% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           964M     0  964M   0% /sys/fs/cgroup
/dev/sda15      105M  3.6M  101M   4% /boot/efi
/dev/sdb1       3.9G  3.9G     0 100% /mnt
tmpfs           193M     0  193M   0% /run/user/1000
/dev/loop0       90M   90M     0 100% /snap/core/6818
/dev/loop1       98M   98M     0 100% /snap/docker/372

Ah, forgot to mention, after changing the config file.

systemctl restart walinuxagent

to make it take effect, you don’t need to restart your VM.

Then run free -m to check your new swap file

 free -m
              total        used        free      shared  buff/cache   available
Mem:           1927         930         211           1         786         759
Swap:          3934          16        3917

use cat /var/log/waagent.log
to check the log

if you see the following log, means you need to reduce your swap file size as there is no enough space on the temp drive.

2019/05/07 02:10:37.103906 INFO Daemon fallocate unsuccessful, falling back to dd
2019/05/07 02:11:57.305341 INFO Daemon Azure Linux Agent Version:
2019/05/07 02:11:57.344161 INFO Daemon OS: ubuntu 18.04
2019/05/07 02:11:57.363692 INFO Daemon Python: 3.6.7
2019/05/07 02:11:57.397202 INFO Daemon Add daemon process pid 31642 to walinuxagent systemd cgroup
2019/05/07 02:11:57.425906 INFO Daemon CGroups: ok
2019/05/07 02:11:57.496881 INFO Daemon Run daemon
2019/05/07 02:11:57.507329 INFO Daemon Activate resource disk
2019/05/07 02:11:57.614996 INFO Daemon Resource disk [/dev/sdb1] is already mounted [/mnt]
2019/05/07 02:11:57.642478 WARNING Daemon Failed to write data loss warning:[Errno 1] Operation not permitted: '/mnt/DATALOSS_WARNING_README.txt'
2019/05/07 02:11:57.670093 INFO Daemon Enable swap
2019/05/07 02:11:57.707961 INFO Daemon Remove old swap file
2019/05/07 02:11:58.531786 INFO Daemon Create swap file
2019/05/07 02:11:58.595572 ERROR Daemon Command: [umask 0077 && fallocate -l 4294967296 '/mnt/swapfile'], return code: [1], result: [fallocate: fallocate failed: No space left on device