Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make swap size configurable #3882

Merged
merged 1 commit into from
Feb 19, 2025
Merged

Make swap size configurable #3882

merged 1 commit into from
Feb 19, 2025

Conversation

sairon
Copy link
Member

@sairon sairon commented Feb 19, 2025

Allow configuration of the swap size via /etc/default/haos-swapfile file. By setting the SWAPSIZE variable in this file, swapfile get recreated on the next reboot to the defined size. Size can be either in bytes or with optional units (B/K/M/G, accepting some variations but always interpreted as power of 10). The size is then rounded to 4k block size. If no override is defined or the value can't be parsed, it falls back to previously used 33% of system RAM.

Fixes #968

Summary by CodeRabbit

  • New Features

    • Established a persistent mount for the system’s default configuration directory.
    • Enhanced swap file management with dynamic sizing, improved safeguards for low-size scenarios, and refined service dependency ordering.
  • Tests

    • Added tests to verify custom swap configuration and ensure proper swap deactivation.

Allow configuration of the swap size via /etc/default/haos-swapfile file. By
setting the SWAPSIZE variable in this file, swapfile get recreated on the next
reboot to the defined size. Size can be either in bytes or with optional units
(B/K/M/G, accepting some variations but always interpreted as power of 10). The
size is then rounded to 4k block size. If no override is defined or the value
can't be parsed, it falls back to previously used 33% of system RAM.

Fixes #968
@sairon sairon added the os label Feb 19, 2025
@sairon sairon requested a review from agners February 19, 2025 11:22
Copy link

coderabbitai bot commented Feb 19, 2025

📝 Walkthrough

Walkthrough

This pull request introduces a new systemd mount unit to bind /mnt/overlay/etc/default to /etc/default, updates the swapfile service to depend on and start after the new mount unit, and adds a condition in the swap file unit for file non-emptiness. The haos-swapfile script is refactored to include a size conversion function and improved logic for calculating and managing swap file sizes. Additionally, two smoke tests have been added to verify swap file resizing and removal after configuration changes and system reboots.

Changes

File(s) Change Summary
buildroot-external/rootfs-overlay/usr/lib/systemd/system/etc-default.mount
buildroot-external/rootfs-overlay/usr/lib/systemd/system/haos-swapfile.service
buildroot-external/rootfs-overlay/usr/lib/systemd/system/mnt-data-swapfile.swap
• Added a new mount unit for /etc/default with a bind mount from /mnt/overlay/etc/default.
• Updated the swapfile service dependencies to require etc-default.mount and adjusted the startup order (after etc-default.mount and before mnt-data.mount).
• Added a condition in the swap file unit to activate only if /mnt/data/swapfile is not empty.
buildroot-external/rootfs-overlay/usr/libexec/haos-swapfile Refactored the swapfile management script by adding the size2kilobytes() function, recalculating swap sizes using SWAPSIZE and SWAPSIZE_BLOCKS, and introducing checks and logging for swap file creation or removal when the size is below the minimum threshold.
tests/smoke_test/test_basic.py Added two smoke tests (test_custom_swap_size and test_no_swap) to verify that swap file resizing and deletion work as expected following configuration changes and system reboots.

Sequence Diagram(s)

sequenceDiagram
    participant O as mnt-overlay.mount
    participant ED as etc-default.mount
    participant DD as mnt-data.mount
    participant HSS as haos-swapfile.service
    participant GFS as [email protected]

    O->>ED: Activate dependency
    ED->>HSS: Required for service startup
    DD->>HSS: Must be active
    HSS->>GFS: Executes after mounts are active
Loading
sequenceDiagram
    participant HS as haos-swapfile (script)
    participant Config as /etc/default/haos-swapfile
    participant FS as /mnt/data/swapfile

    HS->>Config: Source swap configuration
    HS->>HS: Compute swap size using size2kilobytes()
    HS->>FS: Check if swap file exists and its size
    alt Swap file missing or size below minimum
        HS->>FS: Create new swap file or remove existing one
    else
        HS->>FS: Use existing swap file
    end
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (5)
buildroot-external/rootfs-overlay/usr/libexec/haos-swapfile (2)

22-29: Consider adding upper and lower bounds for swap size.

While the script handles small sizes (<40kB) by disabling swap, there's no upper limit check. Consider adding a maximum size limit to prevent excessive swap allocation.

 if [ -z "${SWAPSIZE}" ] || [ "${SWAPSIZE}" = "-1" ]; then
 	# Default to 33% of total memory
 	SWAPSIZE="$(awk '/MemTotal/{ print int($2 * 0.33) }' /proc/meminfo)"
 	echo "[INFO] Using default swapsize of 33% RAM (${SWAPSIZE} kB)"
+else
+	# Maximum swap size: 32GB (in kB)
+	MAX_SWAP=$((32 * 1024 * 1024))
+	if [ "${SWAPSIZE}" -gt "${MAX_SWAP}" ]; then
+		echo "[WARN] Requested swap size exceeds 32GB, capping at maximum"
+		SWAPSIZE="${MAX_SWAP}"
+	fi
 fi

45-55: Enhance error handling for swap file creation.

The swap file creation process could benefit from additional error handling and logging:

  1. The dd command should use status=progress for better feedback
  2. Consider adding error handling for the dd command
 echo "[INFO] Creating swapfile of size ${SWAPSIZE} kB (rounded to ${SWAPSIZE_BLOCKS} blocks)"
 umask 0077
-dd if=/dev/zero of="${SWAPFILE}" bs=4k count="${SWAPSIZE_BLOCKS}"
+if ! dd if=/dev/zero of="${SWAPFILE}" bs=4k count="${SWAPSIZE_BLOCKS}" status=progress; then
+    echo "[ERROR] Failed to create swapfile"
+    rm -f "${SWAPFILE}"
+    exit 1
+fi
buildroot-external/rootfs-overlay/usr/lib/systemd/system/mnt-data-swapfile.swap (1)

1-4: Consider adding additional conditions for swap activation.

The current condition checks if the swap file is not empty, but we might want to add more conditions to ensure system stability:

  1. Check if there's enough memory available
  2. Verify the filesystem is not read-only
 [Unit]
 Description=HAOS swap file
 ConditionFileNotEmpty=/mnt/data/swapfile
+ConditionPathIsReadWrite=/mnt/data
+AssertPathExists=/proc/meminfo
tests/smoke_test/test_basic.py (2)

81-92: LGTM! Consider adding additional verifications for robustness.

The test correctly verifies that the swap size can be customized. However, consider adding these checks for improved robustness:

  1. Verify that the swap file exists before testing
  2. Verify that the swap is active using swapon --show
  3. Add a timeout for the reboot to prevent test flakiness
 @pytest.mark.dependency(depends=["test_init"])
 def test_custom_swap_size(shell, target):
+    # Verify swap file exists and is active
+    assert shell.run_check("test -f /mnt/data/swapfile")
+    assert shell.run_check("swapon --show")
+
     output = shell.run_check("stat -c '%s' /mnt/data/swapfile")
     # set new swap size to half of the previous size - round to 4k blocks
     new_swap_size = (int(output[0]) // 2 // 4096) * 4096
     shell.run_check(f"echo 'SWAPSIZE={new_swap_size/1024/1024}M' > /etc/default/haos-swapfile; reboot")
+
+    # Wait for reboot with timeout
+    sleep(30)  # Adjust timeout as needed
+
     # reactivate ShellDriver to handle login again
     target.deactivate(shell)
     target.activate(shell)
+
+    # Verify swap is active with new size
     output = shell.run_check("stat -c '%s' /mnt/data/swapfile")
     assert int(output[0]) == new_swap_size, f"Incorrect swap size {new_swap_size}B: {output}"
+    assert shell.run_check("swapon --show"), "Swap is not active"

94-104: LGTM! Consider adding additional verifications for robustness.

The test correctly verifies that the swap can be disabled. However, consider adding these checks for improved robustness:

  1. Verify that the swap file is actually removed
  2. Add a timeout for the reboot to prevent test flakiness
 @pytest.mark.dependency(depends=["test_custom_swap_size"])
 def test_no_swap(shell, target):
     output = shell.run_check("echo 'SWAPSIZE=0' > /etc/default/haos-swapfile; reboot")
+
+    # Wait for reboot with timeout
+    sleep(30)  # Adjust timeout as needed
+
     # reactivate ShellDriver to handle login again
     target.deactivate(shell)
     target.activate(shell)
+
     output = shell.run_check("systemctl --no-pager -l list-units --state=failed")
     assert "0 loaded units listed." in output, f"Some units failed:\n{"\n".join(output)}"
+
     swapon = shell.run_check("swapon --show")
     assert swapon == [], f"Swapfile still exists: {swapon}"
+    assert not shell.run_check("test -f /mnt/data/swapfile"), "Swap file not removed"
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dc7b693 and e4d10a0.

📒 Files selected for processing (5)
  • buildroot-external/rootfs-overlay/usr/lib/systemd/system/etc-default.mount (1 hunks)
  • buildroot-external/rootfs-overlay/usr/lib/systemd/system/haos-swapfile.service (1 hunks)
  • buildroot-external/rootfs-overlay/usr/lib/systemd/system/mnt-data-swapfile.swap (1 hunks)
  • buildroot-external/rootfs-overlay/usr/libexec/haos-swapfile (1 hunks)
  • tests/smoke_test/test_basic.py (1 hunks)
🔇 Additional comments (4)
buildroot-external/rootfs-overlay/usr/libexec/haos-swapfile (2)

4-14: LGTM: Well-implemented size conversion function.

The size2kilobytes function effectively handles various size formats (B, K, M, G) with case-insensitive matching and proper power-of-10 conversions. The implementation using awk is robust and handles edge cases well.


16-19:

❓ Verification inconclusive

Verify configuration file permissions.

The script sources /etc/default/haos-swapfile but doesn't verify its permissions. Since this file affects system-wide swap settings, we should ensure it has appropriate permissions.


🏁 Script executed:

#!/bin/bash
# Check permissions of the config file in the rootfs overlay
find buildroot-external/rootfs-overlay/etc/default -type f -name "haos-swapfile" -ls 2>/dev/null

# Check if there's a default config file with preset permissions
find buildroot-external/package -type f -name "haos-swapfile" -ls 2>/dev/null

Length of output: 150


Action Required: Manually Verify Configuration File Permissions

The script sources /etc/default/haos-swapfile without checking its file permissions, and our automated search did not return any instance of this file. This leaves ambiguity about whether a default configuration file is present and, if so, whether it has appropriate permissions.

  • Manual Check: Please verify on your system if /etc/default/haos-swapfile exists.
  • Permission Verification: If it exists, ensure that its permissions are properly restricted (e.g., owner-readable and non-writable by unauthorized users) to avoid unintentional system-wide swap configuration changes.
buildroot-external/rootfs-overlay/usr/lib/systemd/system/etc-default.mount (1)

12-14: LGTM: Appropriate target dependency.

The mount unit is correctly configured to be wanted by hassos-bind.target, ensuring proper integration with the system's mount hierarchy.

buildroot-external/rootfs-overlay/usr/lib/systemd/system/haos-swapfile.service (1)

4-5: LGTM! Dependencies are correctly ordered.

The service dependencies have been properly updated to ensure that /etc/default is mounted before the swap file service starts, which is necessary for reading the swap size configuration.

Copy link
Member

@agners agners left a comment

Choose a reason for hiding this comment

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

LGTM! Very slick implementation, and even with tests 🤩 !

@sairon sairon merged commit d42e34f into dev Feb 19, 2025
3 checks passed
@sairon sairon deleted the configurable-swap-size branch February 19, 2025 14:33
sairon added a commit to home-assistant/os-agent that referenced this pull request Feb 28, 2025
Add /io/hass/os/Config/Swap object that exposes interface with
properties controlling the swap size and swappiness configuration added
in [1] and [2]. There are no checks whether this supported on the target
system (while swappiness would probably have effect also on Supervised
installs, swap size can't be controlled there). These checks should be
implemented on upper layer (i.e. Supervisor).

[1] home-assistant/operating-system#3882
[2] home-assistant/operating-system#3884
sairon added a commit to home-assistant/os-agent that referenced this pull request Feb 28, 2025
Add /io/hass/os/Config/Swap object that exposes interface with
properties controlling the swap size and swappiness configuration added
in [1] and [2]. There are no checks whether this supported on the target
system (while swappiness would probably have effect also on Supervised
installs, swap size can't be controlled there). These checks should be
implemented on upper layer (i.e. Supervisor).

[1] home-assistant/operating-system#3882
[2] home-assistant/operating-system#3884
sairon added a commit to home-assistant/os-agent that referenced this pull request Feb 28, 2025
* Add DBus node for swap size and swappiness config

Add /io/hass/os/Config/Swap object that exposes interface with
properties controlling the swap size and swappiness configuration added
in [1] and [2]. There are no checks whether this supported on the target
system (while swappiness would probably have effect also on Supervised
installs, swap size can't be controlled there). These checks should be
implemented on upper layer (i.e. Supervisor).

[1] home-assistant/operating-system#3882
[2] home-assistant/operating-system#3884

* Read default/current swappiness from procfs
@sairon sairon mentioned this pull request Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How can i increase my swap file size?
2 participants