Score:1

Ubuntu 20.04 mounting swap even if disabled

ua flag

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
Pilot6 avatar
cn flag
Please add output of `swapon -s`
Raffa avatar
jp flag
Related: [disable and remove install created /swapfile](https://askubuntu.com/q/1416971)
Pilot6 avatar
cn flag
Also please add contents of `fstab`.
proxx avatar
ua flag
@Raffa Thanks but not resolved by that thread.
proxx avatar
ua flag
@Pilot6 Added info.
Pilot6 avatar
cn flag
Is this the full content?
proxx avatar
ua flag
Certainly, I removed everything else and rebooted.
Pilot6 avatar
cn flag
And why would you keep a swap partition if you don't plan to use it?
proxx avatar
ua flag
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/142371/discussion-between-proxx-and-pilot6).
Raffa avatar
jp flag
Please add the output of `systemctl status "*swap*"`
proxx avatar
ua flag
@raffa its actually in there, below "As I suspected systemd is the culprit."
Raffa avatar
jp flag
I see ... Please add the output of `systemctl --type swap`
Raffa avatar
jp flag
Mask that unit `sudo systemctl mask dev-dm\x2d0.swap` then reboot and see how it goes.
Score:1
jp flag

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.

proxx avatar
ua flag
I tried this and a few similar things to no avail: Unit dev-dm0.swap does not exist, proceeding anyway.
Raffa avatar
jp flag
@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?
proxx avatar
ua flag
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 avatar
ua flag
Actually it is stupid enough to just create it anyway, I did a reboot, same result.
Raffa avatar
jp flag
@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.
proxx avatar
ua flag
I appreciate your help @Raffa have a nice day sir!
Score:0
ua flag

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.

Score:0
cn flag

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 
proxx avatar
ua flag
systemctl disable systemd-gpt-auto-generator Failed to disable unit: Unit file systemd-gpt-auto-generator.service does not exist.
Pilot6 avatar
cn flag
I am fixing the answer.
proxx avatar
ua flag
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.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.