• 3 min read

SWAP space in Linux VM’s on Windows Azure – Part 2

This article was written by Piyush Ranjan (MSFT) from the Azure CAT team. In a previous post, SWAP space in Linux VM’s on Windows Azure Part 1, I discussed how by default the Linux VM’s…

This article was written by Piyush Ranjan (MSFT) from the Azure CAT team.

In a previous post, SWAP space in Linux VM’s on Windows Azure Part 1, I discussed how by default the Linux VM’s provisioned in Azure IaaS from the gallery images do not have swap space configured. The post then provided a simple set of steps with which one could configure a file based swap space on the resource disk (/mnt/resource). The key point to note, however, is that the steps described there are for a VM that has already been provisioned and is running. Ideally, one would want to have the swap space configured automatically right at the time of the VM provisioning itself, rather than having to wait for a later time and then running a bunch of commands manually.

The trick to automating the swap space configuration at the time of VM provisioning is to use the Windows Azure Linux Agent (waagent). Most people are somewhat vaguely aware that there is an agent running in the Linux VM, but most people also find it a bit too obscure and ignore it, even though the Azure portal actually has a nice documentation on waagent.  See Windows Azure Linux Agent user Guide. There is one other point that needs to be mentioned before drilling down into the details of waagent and how it may be used for the task at hand. That point is that this approach works well if you have a customized Linux VM of your own and are exporting it as a reusable image for provisioning of Linux VM’s in future. There is no way of tapping into the capabilities of the waagent when using a raw base image of Linux from the Azure gallery. This is not really a limitation since the use case scenario that I find most useful is one where I start out with a VM that is provisioned using a gallery image, and is then customized for things I like to have; for example, I like to have Standard Java instead of the open-jdk Java; or I may want to install Hadoop binaries on the VM so that the image can then be used later on toward a multi-node cluster. In such a scenario, it is just as easy to configure the waagent to do a few of the additional things that I want done automatically through the provisioning process.

As discussed in the Windows Azure Linux Agent user’s guide, the agent can be configured to do many things, among which are:

Resource disk management

Formatting and mounting the resource disk

Configuring swap space

The waagent is already installed in the VM provisioned from a gallery image and one needs to simply edit its configuration file located at “/etc/waagent.conf” where the configuration looks like the following:

Change the two lines shown above in the configuration file to enable swap, by setting as follows:

Set ResourceDisk.EnableSwap=y

Set ResourceDisk.SwapSizeMB=5120

The overall process, therefore, is the following:

Provision a Linux VM in IaaS as usual using one of the images in the gallery.

Customize the VM to your liking by installing or removing software components that you need.

Edit the “/etc/waagent.conf” file to set the swap related lines, as shown above. Adjust the size of the swap file (the above is setting it to 5 GB).

Capture a reusable image of the VM using instructions described here.

Provision new Linux VM’s using the image just exported. These VM’s will have the swap space automatically enabled.

While we are on the subject of Windows Azure Linux Agent, it turns out that it provides yet another interesting capability – that of executing an arbitrary, user-specified script through the Role.StateConsumer property in the same configuration file “/etc/waagent.conf”. For example, one can create a shell script “do-cfg.sh” as follows:

Then, in the configuration file set Role.StateConsumer=/home/scripts/do-cfg.sh or whatever is the path to your script. The waagent execute the script just before sending a “Ready” signal to Azure fabric when provisioning a VM. It passes a command-line argument “Ready” to the custom script which can be tested within the script, as shown above to do some custom initialization. Likewise, the waagent executes the same script at the time of VM shutdown and passes a command-line argument “Shutdown” to the script which can be tested for and some custom cleanup task can be run in the VM.