Enabling swap on Ubuntu Core

Ubuntu Core images come with everything needed to enable swap, but it’s disabled by default. To enable it:

  1. edit /etc/default/swapfile and set SIZE to a non-zero value (e.g. 512 for 512MB).
  2. sudo systemctl enable --now swapfile.service

That’s all that’s needed.

Now some more details:

  • /etc/default/swapfile is writable, but some editors might be confused by its directory being readonly. The provided vi is fine with it though.
  • If SIZE is zero, even if the service is enabled and started, no swapfile is created (see the /usr/bin/mkswapfile shell script for details).
  • If you set FILE to something on a different partition, you should add an appropriate After= to the service file.
  • To add that After=, edit /etc/systemd/system/swapfile.service, and systemctl daemon-reload before you enable and start the service.

That’s it.

4 Likes
  • It’s not clear what a “core image” is. Is it Ubuntu Core, or something else?
  • Why is swap disabled by default? (In other words, are there any drawbacks to enabling it?)

Yes, the “image” refers to Ubuntu Core (nothing of the above would work on a classic setup anyway).

Typical Ubuntu Core installs run from block devices that are not particulary safe from wearing out through writing to (and deleting) the same block on disk all the time (SD cards, EMMC flash disks, SSDs) … which is what swap usage induces.

So enabling swap is an optional task (but would surely be nicer to handle if there was just a core config option you can set).

In that case, this should be in Ubuntu Core docs, not Snapcraft docs.

These aren’t the snapcraft docs, but, yes this would probably be better in Ubuntu Core docs. I don’t know if those exist, if they are current, how “findable” those are, nor how to edit or otherwise update them.

I’ll update this to explicitly say Ubuntu Core though (you could’ve done it as well, as it’s a wiki).

The “docs” category in ths forum is a wiki for all of snaps, snapcraft, snapd and Ubuntu Core, so unless we start fragmenting the docs category into little pieces, i dont think the post is wrong in this place…

Also, since the described procedure isnt really a core option you can set but requires to edit files on a running system i dont think it qualifies for the official documentation either (something like “snap set core swapsize=50M” that performs the steps above in the backend would qualify IMHO, but that is not implemented atm.)

I first encountered this article on the docs prototype site and it seemed out of place, hence my first question.

Most people building snaps are not using Ubuntu Core. There are separate sites for the snap platform docs and the Ubuntu Core docs. A few documents may belong on both sites, such as the assertions reference. But most, like this post, would not.

So, if one forum will be used to write material for both, there needs to be a way to distinguish the two. That’s all. Sorry for the noise.

but all people using Ubuntu Core need to build/use snaps :wink:

we could simply start using tags to distinguish if this is really required (i dont think it is, but you seem to feel the other way)

Hello: does this have any relation with lxd containers not having any access to swap space?

manager@lxc:~$ snap list
Name    Version    Rev    Tracking       Publisher   Notes
core    16-2.45.2  9665   latest/stable  canonical✓  core
core18  20200724   1885   latest/stable  canonical✓  base
lxd     4.4        16530  latest/stable  canonical✓  -

manager@lxc:~$ sudo swapon --show
NAME      TYPE SIZE USED PRIO
/swap.img file   2G   0B   -2

manager@lxc:~$ free -h
              total        used        free      shared  buff/cache   available
Mem:           1.9G        251M        753M        1.2M        987M        1.6G
Swap:          2.0G          0B        2.0G

manager@lxc:~$ lxc exec ct1 -- free -h
              total        used        free      shared  buff/cache   available
Mem:           1.9G         41M        1.8G        156K        121M        1.9G
Swap:            0B          0B          0B

Thanks!!