When an Ubuntu instance launches in Windows Azure, cloud-init is called to perform the set-up of the machine. We are going to use custom data to pass instructions to cloud-init so that it creates the swap partition for us. The Windows Azure documentation has more information about injecting custom data and custom data and cloud-init.
You will need to instruct cloud-init to do the following things to create a swap partition:
- Partition (part of) the ephemeral disk as swap
- Initialise the created swap partition
- Mount the initialised swap partition as swap
N.B. This wiki page explains how to configure swap partitions on Ubuntu instances in Windows Azure. If you are looking to create a swap file, see the example in the cloud-init documentation.
To partition the disk appropriately, you will need to use the disk_setup configuration stanza. To partition all of the ephemeral disk as swap, you would use the following (82 is the 'Linux swap' disk type):
disk_setup: ephemeral0: table_type: mbr layout: [[100, 82]] overwrite: True
To partition only the last third as swap, you would use the following:
disk_setup: ephemeral0: table_type: mbr layout: [66, [33, 82]] overwrite: True
Once you have configured the partitioning of the disk, you then need to instruct cloud-init to initialise the swap partition.
To configure filesystems on the partitions, we will use the fs_setup configuration stanza.
Each partition specified for ephemeral0 gets a reference of the form ephemeral0.X (where X is 1 for the first partition, 2 for the second etc.). We will use this reference to instruct cloud-init to initialise the swap partition.
If you were using the whole disk for swap (in a single partition), you would do:
fs_setup: - device: ephemeral0.1 filesystem: swap
If you were using only the second partition of the disk for swap, you would do:
fs_setup: - device: ephemeral0.2 filesystem: swap
If the first partition of the disk is not specified, cloud-init will default to making an ext4 filesystem on that partition; this means that the second example given above does not need to specify ephemeral0.1. If you did want to, however, it would look like this:
fs_setup: - device: ephemeral0.1 filesystem: ext4 - device: ephemeral0.2 filesystem: swap
In order to actually be able to use the swap partition that we have created and initialised, we will need to instruct cloud-init to mount this partition as swap using the mounts configuration stanza.
The mounts stanza is, quite simply, a list of entries that should end up in /etc/fstab, with any missing values filled in from a default line.
In order to mount a full-disk swap partition, we would use the following:
mounts: - ["ephemeral0.1", "none", "swap", "sw", "0", "0"]
If we were using the first partition as ext4 and the second as swap, we would use the following:
mounts: - ["ephemeral0.1", "/mnt"] - ["ephemeral0.2", "none", "swap", "sw", "0", "0"]
cloud-init does mount ephemeral0.1 on /mnt by default, but specifying any values for the mounts configuration option overrides this default. As such, you must ensure that you specify how to mount all partitions on ephemeral0 if you are specifying how to mount any of them.
Putting It All Together
Important: You must prefix your custom data with a line containing just #cloud-config for cloud-init to recognise it.
With that in mind, we can put all of the above together. The following custom data would create two partitions on the ephemeral disk. The first would take up two-thirds of the disk, be formatted (by default) as ext4 and mounted at /mnt. The second would be a swap partition taking up the remainder of the disk, which could be used as swap from boot:
#cloud-config disk_setup: ephemeral0: table_type: mbr layout: [66, [33, 82]] overwrite: True fs_setup: - device: ephemeral0.1 filesystem: ext4 - device: ephemeral0.2 filesystem: swap mounts: - ["ephemeral0.1", "/mnt"] - ["ephemeral0.2", "none", "swap", "sw", "0", "0"]