maas-ui
maas-ui copied to clipboard
[ui] provide a way to disable swap files added by default to all deployed machines
Bug originally filed by dmitriis at https://bugs.launchpad.net/bugs/1793831
Currently all ubuntu installations have swap enabled by default even if there was no swap partition configured in the node storage configuration. For bionic this is somewhat consistent with [1] and [2], however, MAAS uses curtin to perform the installation and not d-i or subiquity.
It would make sense to have a UI option and a way to disable swap via API for individual nodes, by tag or completely for all deployed nodes.
This is relevant, for example, on hypervisor nodes where guest (QEMU process) pages can be otherwise swapped out by the host or storage nodes where certain designs prohibit swap usage completely making a compromise between trashing vs OOM killer behavior. There are other applications where this can be relevant as well.
The curtin logic is pretty straightforward: setup a swap file if swap size is not provided (None) and it is set to 'None' if there is no swap config. And the default curtin config generated by MAAS (see the attachment) does not contain a swap section.
https://github.com/CanonicalLtd/curtin/blob/ubuntu/18.1-52-g5f0082d1-0ubuntu1/curtin/swap.py#L54-L63 def setup_swapfile(target, fstab=None, swapfile=None, size=None, maxsize=None): if size is None: size = suggested_swapsize(fsys=target, maxsize=maxsize)
if size == 0:
LOG.debug("Not creating swap: suggested size was 0")
return
if swapfile is None:
swapfile = "/swap.img"
https://github.com/CanonicalLtd/curtin/blob/18.1-52-g5f0082d1-0ubuntu1/curtin/commands/curthooks.py#L517-L520 def add_swap(cfg, target, fstab): if 'swap' in cfg and not cfg.get('swap'): LOG.debug("disabling 'add_swap' due to config") return swapcfg = cfg.get('swap', {}) fname = swapcfg.get('filename', None) size = swapcfg.get('size', None) maxsize = swapcfg.get('maxsize', None) # ... swap.setup_swapfile(target=target, fstab=fstab, swapfile=fname, size=size, maxsize=maxsize)
https://github.com/CanonicalLtd/curtin/blob/ubuntu/18.1-52-g5f0082d1-0ubuntu1/curtin/commands/curthooks.py#L1163-L1167 def builtin_curthooks(cfg, target, state):
...
with events.ReportEventStack(
name=stack_prefix + '/setting-up-swap',
reporting_enabled=True, level="INFO",
description="setting up swap"):
add_swap(cfg, target, state.get('fstab'))
https://curtin.readthedocs.io/en/latest/topics/config.html#swap "size: <Size string> Configure the exact size of the swapfile. Setting size to 0 will disable swap."
maas-vhost1/2018-09-21/messages:Sep 21 19:06:03 maas-vhost1 cloud-init[1025]: Running command ['sh', '-c', 'rm -f "$1" && umask 0066 && { fallocate -l "${2}M" "$1" || dd if=/dev/zero "of=$1" bs=1M "count=$2"; } && mkswap "$1" || { r=$?; rm -f "$1"; exit $r; }', 'setup_swap', '/tmp/tmptm16lan8/target//swap.img', '2048'] with allowed return codes [0] (capture=False) maas-vhost1/2018-09-21/messages:Sep 21 19:06:03 maas-vhost1 cloud-init[1025]: Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) maas-vhost1/2018-09-21/messages:Sep 21 19:06:03 maas-vhost1 cloud-init[1025]: creating swap file '/swap.img' of 2048MB took 0.122 seconds maas-vhost1/2018-09-21/messages:Sep 21 19:06:03 maas-vhost1 cloud-init[1025]: finish: cmd-install/stage-curthooks/builtin/cmd-curthooks/setting-up-swap: SUCCESS: setting up swap
swap is on, whether you need it or not
free -m total used free shared buff/cache available Mem: 1993 90 1677 0 225 1756 Swap: 2047 0 2047
One could set vm.swappiness=0 [3], however, this is not ideal because it does not entirely disable swap [3]: "A value of 0 instructs the kernel not to initiate swap until the amount of free and file-backed pages is less than the high water mark in a zone."
The default value for vm.swappiness is 60 [4] and has to be changed explicitly as well.
[1] https://wiki.ubuntu.com/ZestyZapus/ReleaseNotes#Swap [2] http://blog.surgut.co.uk/2016/12/swapfiles-by-default-in-ubuntu.html Starting from 17.04 Zesty Zapus release, instead of creating swap partitions, swapfiles will be used by default for non-lvm based installations. [3] https://www.kernel.org/doc/Documentation/sysctl/vm.txt [4] http://kernel.ubuntu.com/git/ubuntu/ubuntu-xenial.git/tree/mm/vmscan.c?h=Ubuntu-hwe-4.15.0-35.38_16.04.1#n152