Skip to content

Commit 3a62b20

Browse files
authored
Update optimization.md
Adding corrections for swap configuration in Linux.
1 parent 6a8111e commit 3a62b20

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

articles/virtual-machines/linux/optimization.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,32 @@ By default when you create a VM, Azure provides you with an OS disk (**/dev/sda*
5151
## Linux Swap File
5252
If your Azure VM is from an Ubuntu or CoreOS image, then you can use CustomData to send a cloud-config to cloud-init. If you [uploaded a custom Linux image](upload-vhd.md?toc=%2fazure%2fvirtual-machines%2flinux%2ftoc.json) that uses cloud-init, you also configure swap partitions using cloud-init.
5353

54-
On Ubuntu Cloud Images, you must use cloud-init to configure the swap partition. For more information, see [AzureSwapPartitions](https://wiki.ubuntu.com/AzureSwapPartitions).
54+
For all images that are being provisioned and supported by cloud-init, which can all be found [here](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/using-cloud-init). You cannot use the **/etc/waagent.conf** to manage swap.
55+
56+
The easiest method when using cloud-init is to follow the steps below:
57+
58+
Create a file called **create_swapfile.sh** under **/var/lib/cloud/scripts/per-boot** folder.
59+
**$ sudo touch /var/lib/cloud/scripts/per-boot/create_swapfile.sh**
60+
61+
Add the following lines to the file:
62+
**$ sudo vi /var/lib/cloud/scripts/per-boot/create_swapfile.sh**
63+
64+
```
65+
#!/bin/sh
66+
if [ ! -f '/mnt/swapfile' ]; then
67+
fallocate --length 2GiB /mnt/swapfile
68+
chmod 600 /mnt/swapfile
69+
mkswap /mnt/swapfile
70+
swapon /mnt/swapfile
71+
swapon -a ; fi
72+
```
73+
**NOTE**: You can change the value according to your need and also based on the available space in your resource disk which is different based on the VM size being used.
74+
75+
Make the file executable by using:
76+
**$ sudo chmod +x /var/lib/cloud/scripts/per-boot/create_swapfile.sh**
77+
78+
You can execute the script right after the last step for the swapfile to be created right away:
79+
**$ sudo /var/lib/cloud/scripts/per-boot/./create_swapfile.sh**
5580

5681
For images without cloud-init support, VM images deployed from the Azure Marketplace have a VM Linux Agent integrated with the OS. This agent allows the VM to interact with various Azure services. Assuming you have deployed a standard image from the Azure Marketplace, you would need to do the following to correctly configure your Linux swap file settings:
5782

0 commit comments

Comments
 (0)