Search code examples
swapcloud-init

How do you configure a swap partition using cloud-init?


We have an instance that uses cloudinit for initial instantiation, and this instance and cloudinit work great.

We want to add swap to this instance, and have correctly configured a suitable disk, however we cannot figure out how to get cloudinit to initialise the swap disks, like cloudinit does with all the other disks on the machine.

Our configuration of our disks, including swap, is as follows:

fs_setup:
  - label: vidi
    device: /dev/xvde
    filesystem: ext4
  - label: swap
    device: /dev/xvdg
    filesystem: swap
mounts:
- [ /dev/xvde, /var/lib/vidispine, ext4, defaults, 0, 0 ]
- [ /dev/xvdg, none, swap, sw, 0, 0 ]

This results in an /etc/fstab as follows:

LABEL=cloudimg-rootfs   /    ext4   defaults,discard    0 0
/dev/xvde   /var/lib/vidispine  ext4    defaults,comment=cloudconfig    0   0
/dev/xvdg   none    swap    sw,comment=cloudconfig  0   0

The disk /dev/xvde is formatted correctly on startup. The disk /dev/xvdg is ignored.

What additional steps are required for cloudinit to "mkswap" and "swapon" the /dev/xvdg disk?


Solution

  • In response to "What additional steps are required for cloudinit to 'mkswap' and 'swapon' the /dev/xvdg disk?", the short answer is "nothing".

    The longer answer is that you need to be running a version of cloud-init with the following bugfix applied:

    https://github.com/canonical/cloud-init/pull/143

    Which fixes the following error when running mkswap:

    mkswap: invalid block count argument: ''
    

    Most specifically, Ubuntu Bionic images 20200131 and newer work properly.

    Older versions of cloudinit require the following added to the runcmd scripts on boot to work around the bug above:

    - mkswap /dev/xvdg
    - swapon -a