Skip to content

3008.x: Fix "mount.swap" activation when using UUID and introduce "resolve_canonical" argument - #70201

Open
meaksh wants to merge 8 commits into
saltstack:3008.xfrom
meaksh:3008.x-fix-mount-swap-check-canonical-names
Open

3008.x: Fix "mount.swap" activation when using UUID and introduce "resolve_canonical" argument#70201
meaksh wants to merge 8 commits into
saltstack:3008.xfrom
meaksh:3008.x-fix-mount-swap-check-canonical-names

Conversation

@meaksh

@meaksh meaksh commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR fixes an issue on mount.swap state, when a device is passed using UUID or other labels instead of its canonical name. The state execution fails when trying to activate the swap.

For example, given the following context:

# blkid | grep swap
/dev/vda3: UUID="75c7c0e8-d6c7-4061-afe7-28468efbc282" TYPE="swap" PARTUUID="02cf4e13-0e71-424e-aece-e4053133f7ac"

# cat /etc/fstab | grep swap
UUID=75c7c0e8-d6c7-4061-afe7-28468efbc282		none	swap	defaults	0 0

# swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/vda3 partition   2G   0B   -2

And given the following SLS:

# cat /srv/salt/swap_test2.sls 
prueba_swap:
  mount.swap:
    - name: "UUID=75c7c0e8-d6c7-4061-afe7-28468efbc282"

The state execution fails with:

# salt-call --local state.apply swap_test2
[...]
[ERROR   ] Command 'swapon' failed with return code: 255
[ERROR   ] stdout: swapon: /dev/vda3: swapon failed: Device or resource busy
[ERROR   ] retcode: 255
[ERROR   ] Command 'swapon' failed with return code: 255
[ERROR   ] output: swapon: /dev/vda3: swapon failed: Device or resource busy
[ERROR   ] Swap UUID=75c7c0e8-d6c7-4061-afe7-28468efbc282 failed to activate
local:
----------
          ID: prueba_swap
    Function: mount.swap
        Name: UUID=75c7c0e8-d6c7-4061-afe7-28468efbc282
      Result: False
     Comment: Swap UUID=75c7c0e8-d6c7-4061-afe7-28468efbc282 failed to activate
     Started: 17:11:10.932588
    Duration: 10.284 ms
     Changes:   

Summary for local
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:  10.284 ms

Additionally to fixing the above issue, this PR introduces the resolve_canonical argument (Default: False) for the following mount functions:

  • mount.set_fstab
  • mount.rm_fstab
  • mount.mounted (state)
  • mount.unmounted (state)
  • mount.swap (state)
  • mount.fstab_present (state)
  • mount.fstab_absent (state)

When resolve_canonical is set to True, then the mechanism to detect existing entries will always resolve the device to its canonical name, preventing the creation of duplicate entries when using canonical names and your existing entries are using UUID references (or viceversa)

What issues does this PR fix or reference?

Fixes #16084
Fixes openSUSE/salt#679

Merge requirements satisfied?

[NOTICE] Bug fixes or features added to Salt require tests.

Commits signed with GPG?

Yes

@meaksh
meaksh requested a review from a team as a code owner September 1, 2026 15:24
bmwiedemann and others added 5 commits September 1, 2026 16:37
fstab(5) allows a swap to be identified by a TAG=value specification
instead of a device path, and swapon(8) resolves those tags.  The state
did not: it compared the configured name against the output of
mount.swaps, which is read from /proc/swaps and therefore only ever
contains device paths.

The comparison could never match, so the state called mount.swapon on
every run and then reported

    Swap UUID=... failed to activate

even though the swap was active all along.

Resolve TAG=value specifications to a device path with blkid before
looking them up, reusing the _convert_to() helper that fstab_present
already uses for the same purpose.  Specifications that no block device
matches are left untouched, so a genuine failure is still reported with
the name the user configured.
/proc/swaps, and therefore mount.swaps, reports the path the kernel
arrived at when the swap was activated, which need not be the name used
to activate it: swapon(8) follows symlinks, so a swap activated as
/dev/mapper/system-swap is reported as /dev/dm-0.

That is not a corner case for the TAG=value specifications resolved in
the previous commit, it is the normal result for LVM and dm-crypt
setups.  blkid names those devices through /dev/mapper:

    # blkid -t UUID=42fbd303-5ed5-4c40-b382-d1bef97e5d88
    /dev/mapper/cr_home: UUID="42fbd303-..." TYPE="ext4"

so the resolved device still would not have matched what the kernel
reports.  The same applies to a swap configured as a symlink, such as
/dev/disk/by-uuid/<uuid>.

Resolve both the configured name and the keys of the active swaps with
os.path.realpath() before comparing them.  This also replaces the
hand-rolled symlink resolution, which only followed a single level and
assumed that a relative link target lived in /dev.

While here, use the resolved device for the "is set to be added to the
fstab and to be activated" test-mode check as well; it looked up the
unresolved name and so never fired for a symlinked device.
Adjust code after moving functions to salt.utils.mount
Co-authored-by: Yeray Gutiérrez Cedrés <yeray.gutierrez@suse.com>
@meaksh
meaksh force-pushed the 3008.x-fix-mount-swap-check-canonical-names branch 2 times, most recently from f249103 to 4dde1c8 Compare September 1, 2026 15:39
twangboy
twangboy previously approved these changes Sep 1, 2026
@twangboy

twangboy commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Looks like you have some pre-commit failures

twangboy
twangboy previously approved these changes Sep 2, 2026
@meaksh

meaksh commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

@twangboy I just pushed a small variable rename to make the name really consistent with its purpose. Sorry because this has dismissed your previous approval.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test:full Run the full test suite

Projects

None yet

3 participants