Skip to content

fix: switch the swap file on during the play - #34

Merged
MarkProminic merged 1 commit into
mainfrom
fix/swap-enable-during-play-30
Oct 28, 2026
Merged

MarkProminic merged 1 commit into
mainfrom
fix/swap-enable-during-play-30

Conversation

@solaechea

Copy link
Copy Markdown
Contributor

Written with Claude Opus 5.5

Fixes #30.

Change

The swap role created the swap file and its fstab entry, but swap only became active after the next reboot:

  • Nothing notified the "Running swapon on the swap file" handler, so it never ran.
  • mkswap was a handler too. A play that failed before its end left the file unformatted for good, because later runs saw that the file existed (creates:) and notified nothing.

Both steps are now tasks in tasks/enable.yml, run right after the file is created and its permissions set:

  • Formatting: runs whenever the file's header lacks the SWAPSPACE2 signature, the same check the molecule verifier uses. A second run therefore changes nothing, and an unformatted file left by a failed run is repaired.
  • Enabling: runs whenever the file isn't listed in /proc/swaps. It's still skipped under swap_test_mode.

The two unused handlers are removed. Permissions are now set before mkswap, so the file is never formatted while it's world-readable.

Testing

  • Container, swap_test_mode: true, as in the molecule scenario:
    • First run: the file is created, gets mode 600 and is formatted (SWAPSPACE2).
    • Second run: no changes.
    • A zeroed, never-formatted file: formatted again.
    • The run stops at the later swappiness task only because that container has no sysctl, which is unrelated to this change.
  • swapon itself can't run in a container. On a Debian 12 EC2 instance where the old role had left /swapfile unformatted and inactive, running the same mkswap and swapon commands enabled 4 GB of swap.
  • prettier --check and yamllint pass.

Nothing notified the swapon handler, so swap only became active after the
next reboot, and mkswap was a handler too, so a play that failed before its
end left the file unformatted for good: later runs saw the file and notified
nothing. Format the file whenever it lacks the swap signature and enable it
whenever it isn't active, as tasks right after it is created, so the steps
that follow get the memory and an interrupted run is repaired.

@MarkProminic MarkProminic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that a Swap was already included in the base STARTcloud Image:

https://github.com/STARTcloud/vagrant_box_template_creator/blob/5c0363b3c67a0b7f39d8be1cdb955bc389f137a9/builder/provisioners/preseed/server/debian12-server/preseed.cfg#L105

Please confirm this is not going to override or break the work done in the templating process for existing projects that would use the swap role(I can't think of any, but there may be)

@MarkProminic

Copy link
Copy Markdown
Contributor

Confirmed: the role only manages the swap file at swap_file_path and never touches the partition the base image carries, so this adds a second swap area rather than replacing anything; that is already the state after the first reboot today, the change only moves mkswap/swapon into the play. The /proc/swaps and dd checks match what disable.yml and the molecule verifier already do. One note: the 4086 offset assumes 4 KiB pages, same as the verifier; fine for every amd64 box here, just be aware of it. Approving.

@MarkProminic
MarkProminic merged commit ab8a8d8 into main Oct 28, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

swap: the swap file is never switched on during the play

2 participants