Skip to content

LowMem extension: Fix swapfile creation on btrfs; Fix permission race - #10298

Open
dlitz wants to merge 2 commits into
armbian:mainfrom
dlitz:fix-lowmem-on-btrfs
Open

LowMem extension: Fix swapfile creation on btrfs; Fix permission race#10298
dlitz wants to merge 2 commits into
armbian:mainfrom
dlitz:fix-lowmem-on-btrfs

Conversation

@dlitz

@dlitz dlitz commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Description

This PR includes a fix for the creation of the /swapfile when building with ENABLE_EXTENSIONS=lowmem and a btrfs root filesystem. Specifically, it ensures that the swapfile is created with the No_COW attribute (chattr +C). Otherwise, swapon fails.

This PR also includes a one-line fix (as a separate commit) to set the umask so that there is no moment where /swapfile is world-readable.

How Has This Been Tested?

  • BOARD=qemu-uefi-x86 without this patch
  • BOARD=qemu-uefi-x86 with this patch
  • BOARD=nanopi-r3s-lts without this patch
  • BOARD=nanopi-r3s-lts with this patch

Common build options: ./compile.sh RELEASE=trixie BRANCH=edge BUILD_MINIMAL=yes KERNEL_CONFIGURE=no ROOTFS_TYPE=btrfs BTRFS_COMPRESSION=zstd ENABLE_EXTENSIONS=lowmem CLEAN_LEVEL=debs

Notes:

  • CLEAN_LEVEL=debs seems to be needed to prevent the build process from including an old, cached copy of the lowmem-mkswap.sh script.
  • BTRFS_COMPRESSION=zstd or BTRFS_COMPRESSION=none seems to be needed on nanopi-r3s-lts because u-boot apparently can't properly read /boot/armbianEnv.txt from a zlib-compressed btrfs.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas

Summary by CodeRabbit

  • Bug Fixes
    • Strengthened swapfile setup by starting with restrictive permissions (umask 077).
    • Ensured the swapfile target is reset to empty before allocation.
    • Improved behavior on copy-on-write filesystems by attempting to disable CoW on the swapfile (without failing if unsupported).

Signed-off-by: Darsey Litzenberger <dlitz@dlitz.net>
@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The swapfile setup script now sets a restrictive umask, truncates the target, and attempts to disable copy-on-write before allocation and activation.

Changes

Swapfile initialization hardening

Layer / File(s) Summary
Prepare swapfile before allocation
packages/bsp/armbian-lowmem/lowmem-mkswap.sh
The script applies umask 077, truncates the swapfile, and attempts chattr +C before the existing allocation and activation steps.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • armbian/build#10065: Both PRs modify the swapfile creation workflow, including sizing, setup, and activation.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: improving LowMem swapfile creation on Btrfs and fixing a permissions race.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added size/small PR with less then 50 lines 08 Milestone: Third quarter release Needs review Seeking for review BSP Board Support Packages labels Jul 27, 2026

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/bsp/armbian-lowmem/lowmem-mkswap.sh`:
- Around line 29-35: Update the swapfile creation flow around SWAPFILE_PATH so
failures after truncate, including chattr, fallocate, mkswap, or swapon, remove
the incomplete swapfile before exiting. Use transactional staging or equivalent
validation/repair for existing incomplete files, while preserving valid existing
swapfile handling.
- Around line 34-35: The swapfile setup currently ignores failures from chattr
in the low-memory swap creation flow. Update the chattr +C step after
REAL_SWAPFILE_PATH is resolved to stop immediately with an actionable error when
it fails, before preallocation, mkswap, or swapon proceed; retain normal
continuation when No_COW setup succeeds.
- Around line 32-35: Update the swapfile setup flow to use REAL_SWAPFILE_PATH,
the canonical path resolved by readlink, for free-space checks, df/dirname
lookups, and swapfile allocation operations; retain SWAPFILE_PATH only as the
configurable input and ensure all filesystem calculations target the resolved
path.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 7f9feebd-0b10-4195-9ab1-9e9715e4f913

📥 Commits

Reviewing files that changed from the base of the PR and between 0538504 and 0aecfa5.

📒 Files selected for processing (1)
  • packages/bsp/armbian-lowmem/lowmem-mkswap.sh

Comment thread packages/bsp/armbian-lowmem/lowmem-mkswap.sh Outdated
Comment thread packages/bsp/armbian-lowmem/lowmem-mkswap.sh Outdated
Comment thread packages/bsp/armbian-lowmem/lowmem-mkswap.sh Outdated
This ensures the swapfile is created with the No_COW attribute.
Otherwise, swapon fails with "Invalid argument" and the kernel logs a
warning:

    BTRFS warning (device mmcblk1p1): swapfile must not be copy-on-write

Signed-off-by: Darsey Litzenberger <dlitz@dlitz.net>
@dlitz
dlitz force-pushed the fix-lowmem-on-btrfs branch from 0aecfa5 to 61a7dd0 Compare July 28, 2026 00:44
@EvilOlaf
EvilOlaf requested a review from vidplace7 July 28, 2026 03:27
@github-actions

Copy link
Copy Markdown
Contributor

✅ This PR has been reviewed and approved — all set for merge!

@github-actions github-actions Bot added Ready to merge Reviewed, tested and ready for merge and removed Needs review Seeking for review labels Jul 28, 2026
@igorpecovnik igorpecovnik removed the Ready to merge Reviewed, tested and ready for merge label Jul 29, 2026
@EvilOlaf

Copy link
Copy Markdown
Member

CLEAN_LEVEL=debs seems to be needed to prevent the build process from including an old, cached copy of the lowmem-mkswap.sh script.

So after merge global rootfs recreation seems necessary

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

Labels

08 Milestone: Third quarter release BSP Board Support Packages size/small PR with less then 50 lines

Development

Successfully merging this pull request may close these issues.

3 participants