1

TLDR: A fully updated ubuntu 22.04 server is still mounting swap even after removing it from /etc/fstab

I am about to pull my hair over this issue. I've been reading the ubuntu documentation about swap for about 15 times and still can't figure out why this is happening.

I commented out the swap FS in /etc/fstab, did a reboot and my system is still listing a swap drive in /proc/swaps and free -h.

/proc/swaps

Filename                Type        Size    Used    Priority
/dev/dm-0                               partition   2097148 0   -2

As I suspected systemd is the culprit.

 dev-disk-by\x2duuid-8a852d8d\x2d56cf\x2d4478\x2da13e\x2d0405fa7239dc.swap - /dev/disk/by-uuid/8a852d8d-56cf-4648-a13e-0405fa7239dc
 Loaded: loaded (/etc/fstab; generated)
 Active: active since Tue 2023-01-10 12:49:44 CET; 2 weeks 2 days ago
   What: /dev/disk/by-uuid/8a852d8d-56cf-4678-a13e-0405fa7239dc
   Docs: man:fstab(5)
         man:systemd-fstab-generator(8)
  Tasks: 0 (limit: 38460)
 Memory: 200.0K
 CGroup: /system.slice/dev-disk-by\x2duuid-8a852d8d\x2d56cf\x2d4478\x2da13e\x2d0405fa7239dc.swap

After reading a bit about systemd swap I realized that this drive is not mounted through a unit file since there does not appear to be one.

Now I suspect that systemd-gpt-auto-generator automatically mounts the swap drive through discovery. https://man.archlinux.org/man/systemd-gpt-auto-generator.8

What I do not understand is how to safely disable only the swap mounting. I don't really want to throw overboard all of this default configuration since it is running on many systems. And the risk of future updates depending on this and thus causing anomalies.

I could remove the swap partition altogether but this is really the ugliest of fixes and also not very linux like. And I really don't want to have inexperienced admin's messing around with the deletion of partitions, it's just not worth the risk. Another non-solution is to adjust swapiness but I really don't want swap. Someone on stackexchange mentioned doing a swapoff post boot which really isn't a solution either.

rant: Systemd once again fixing what isn't broke and ignoring KISS as if designed by microsoft. These things used to be predictable.

Also: I am running software that really suffers from swap and i'd rather have it crash then being swapped.

Last but not least it is stunning how little information can be found on the subject, I can not possibly be the only one with this problem.

Any input is appreciated! Thanks in advance.

Edit

Per request: output of swapon -s

Filename                Type        Size    Used    Priority
/dev/dm-0                               partition   2097148 2316    -2

Output of /etc/fstab

UUID="f7285d85-d18f-4a10-9580-e54a2cd4581c" / ext4 defaults,errors=remount-ro 0 1

Output of systemctl --type swap

  UNIT             LOAD   ACTIVE SUB    DESCRIPTION
  dev-dm\x2d0.swap loaded active active /dev/dm-0
Artur Meinild
  • 31,385
proxx
  • 19
  • 6

3 Answers3

2

Swap partitions and swap files are mounted by systemd units these day in addition to /etc/fstab entries.

There most likely is a systemd unit that mounts your swap partition ... Find it with:

systemctl --type swap

The output might be something like:

  UNIT         LOAD   ACTIVE SUB    DESCRIPTION
  dev-dm0.swap loaded active active /dev/dm-0

Mask it like so:

sudo systemctl mask dev-dm0.swap

Then reboot.

Raffa
  • 35,113
  • I tried this and a few similar things to no avail: Unit dev-dm0.swap does not exist, proceeding anyway. – proxx Jan 27 '23 at 12:04
  • @proxx It does exist in your output that you added to your question ,,, You need to mask it with the systemd style escapes (exactly as it's shown with systemctl --type swap) i.e. sudo systemctl mask dev-dm\x2d0.swap and not sudo systemctl mask dev-dm0.swap ... Is that how you run it? – Raffa Jan 27 '23 at 12:09
  • Same result, it creates a symlink anyway but the file does not exist. I did a global search for any .swap files but none exist on the system. "Unit dev-dmx2d0.swap does not exist, proceeding anyway." – proxx Jan 27 '23 at 12:12
  • Actually it is stupid enough to just create it anyway, I did a reboot, same result. – proxx Jan 27 '23 at 12:18
  • @proxx I am out of ideas :-) ... Unless you want to delete the swap partition or at least format it carefully and double check beforehand to another filesystem e.g. ext4 instead of swap. – Raffa Jan 27 '23 at 12:21
  • I appreciate your help @Raffa have a nice day sir! – proxx Jan 27 '23 at 12:27
0

You can disable systemd-gpt-auto-generator by

adding systemd.gpt_auto=no boot parameter in /etc/default/grub.

And use only fstab.

But I don't see a reason to keep a swap partition if you are not planning to use it.

Also you can always disable swap in a working system by

sudo swapoff -a 
Pilot6
  • 92,169
  • systemctl disable systemd-gpt-auto-generator Failed to disable unit: Unit file systemd-gpt-auto-generator.service does not exist. – proxx Jan 27 '23 at 11:28
  • I am fixing the answer. – Pilot6 Jan 27 '23 at 11:35
  • Hate to say it but it does not work, I added it to GRUB_CMDLINE_LINUX_DEFAULT and did the update grub thingy but still had a swapdrive on boot. – proxx Jan 27 '23 at 12:26
0

Solved it myself by flipping the 'do not automount' option on the swap partition.

Since Ubuntu uses LVM by default the attribute has to be set on the /dev/LMV0/SWAP instead of on the underlying device.

sgdisk -A 2:set:63 /dev/LVM0/SWAP

On reboot there is no more swap device mounted.

systemd-gpt-auto-generator will not mount the drive automatically when this attribute is set and thus there will be no swap partition.

proxx
  • 19
  • 6