Dec 12

How to: Install a swap file in a Windows Azure Ubuntu VM

How to: Install a swap file in a Windows Azure Ubuntu VM

Unless you made your own Ubuntu image, configured the Azure connector/integration to create a swap file and uploaded it to Windows Azure chances are your VM does not come with a swap file/memory. Afterall Microsoft recommends that all Linux machines that are uploaded to the cloud come without a swap file to avoid issues. The application you install to configure your VM (think of sysprep for Linux) has a parameter to allow a swap file to be automatically created when you deploy the VM. However, most of us would just deploy a clean VM from the cloud and Windows Azure Linux VMs do not come with a swap file by default.

So, I’ve prepared a few commands that will allow you to deploy a swap file for Windows Azure Linux VMs:

fallocate -l 5g /mnt/swapfile
chown root:root /mnt/swapfile
chmod 600 /mnt/swapfile
mkswap /mnt/swapfile
swapon /mnt/swapfile
cat /proc/swaps
echo “/mnt/swapfile none swap sw 0 0” >> /etc/fstab
nano /etc/fstab

I recommend reading this article too: How to: Configure Swappiness in Ubuntu. This is very important if you care about the performance of your VM after you have enabled swap memory.

Going step by step here is what the commands are doing:

  1. The easy way is to open bash with super user rights
    • sudo bash
  2. Then we need to allocate the swap file
    • fallocate -l 5g /mnt/swapfile (or) (this one is faster)
    • dd if=/dev/zero of=/mnt/swapfile bs=1024 count=4096k
  3. Then we give root ownership of the swap file and configure the file permissions
    • chown root:root /mnt/swapfile (and)
    • chmod 600 /mnt/swapfile
  4. Then we indicate the system to prepare that file as swap file
    • mkswap /mnt/swapfile
  5. After that we indicate the system to use the file as a swap file
    • swapon /mnt/swapfile
  6. Next we make sure the system acknowledges the new swap file by listing all swap files in the system
    • cat /proc/swaps
  7. Finally, we tell the system to make this file a swap file on reboot
    • echo “/mnt/swapfile none swap sw 0 0” >> /etc/fstab
  8. We make sure our change took place by inspecting the file
    • nano /etc/fstab

One important consideration made here is that the /mnt/ mountpoint belongs to the temporary storage you are provided by Windows Azure on your VM. This temporary storage is a disk you can access (and as far as I know you are not billed for the storage capacity you use on this drive) but every reboot / poweroff that disk is gone. When you start again your machine any and all files will be gone. Literally it is temporary storage. It will persist as long as your VM is on. If for some reason the power goes off on your VM you will lose this data so don’t bank on the no reboot yay more space thought.

Alternative Options

I recommend using the /mnt mount as it uses the temporary disk you are provided which results on no billing for storage (and perhpaps, I´m not sure, added IO as it is a new disk on the system). The problem is if you want to create a sub-directory on that drive and use it to allocate your swap file. Because of the nature of this temporary storage space, if you shutdown and turn your computer back on even the file structure will be lost and you won’t be able to mount the swap file as the folder will not exist. IF you are dead set on having it on a separate subfolder I have the following alternative for you:

The best way to address this is to have an init.d script that will generate the folder and swap file for you… but before you freak out there is an available package that will get the job 95% done! Another great advantage of using it is that it will size your swap file automatically on every restart. As you probably could imagine VMs change RAM size from time to time. This program will deploy a swap file that is a twice the size of the RAM up to 2gb but you could apply your own logic. This is great when you are resizing machines frequently:

  1. Configure the settings (so when you install and setup it’s ready).
  2. Install dphys-swapfile.
  3. Reboot your server to test it.
  4. Configure your swappiness!

If you want to learn more about the commands of dphys-swapfile you can see the General Commands Manual: dphys-swapfile.

I. Initial Configuration

When you install dphys-swapfile it will perform an initial configuration and if you haven’t pre-configured it you might end up with a swap file on your main drive which you will have to get rid of later on… because it is not what we want. You need to edit the user configuration file to instruct dphys-swapfile to use a different file for swap and to create the needed folder:

$ nano /etc/dphys-swapfile

Once in there type the following but make the adjustments you need to establish the file location.

CONF_SWAPFILE=/mnt/swap/swapfile

# Create the directory
[ -d /mnt/swap ] || mkdir /mnt/swap

Remember we are only doing this as temporary storage will be cleared every now and then. If you do this on a persistent drive like the system drive the folder won’t be wiped.

The first line configures where we want the swap file to reside. The next instructions tells the system to look for the folder and if does not exist to create it. If you decide to change the folder name you have to change it on both lines not just one.

II. Install dphys-swapfile

As simple as: sudo apt-get install dphys-swapfile. It will perform a setup step and use the file we previously created. I recommend at this point verifying everything is the way we want it. Once it is installed if it hasn’t already performed this task go ahead and run it with the swapon parameter and then execute swapon -s to see if it worked. You should see something like:

$ swapon -s
Filename Type Size Used Priority
/mnt/swap/swapfile file 2097148 0 -1

III. Reboot to test

All that is left is to reboot your server (perhaps change your RAM size) and see what happens. Remember you probably need to turn it off and back on for your temporary drive to get wiped and truly test it. Check the file exists and using swapon -s and free -m make sure swap is working properly

IV. Configure your swappiness

Visit: How to: Configure Swappiness in Ubuntu to learn how to properly configure your swappiness which is important to ensure optimal performance.

Enhanced by Zemanta

Leave a Reply

%d bloggers like this: