Skip to content

Commit

Permalink
cc_mounts: Fix swapfile not working on btrfs (#4319)
Browse files Browse the repository at this point in the history
To make a swapfile work on btrfs, we need to create an empty file
and add the "no copy-on-write" attribute before making it a swapfile.

See https://btrfs.readthedocs.io/en/latest/Swapfile.html

Fixes GH-3713
LP: #1884127
  • Loading branch information
bin456789 committed Aug 14, 2023
1 parent 7949bb3 commit 11a4fd1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cloudinit/config/cc_mounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ def create_swap(fname, size, method):

fstype = util.get_mount_info(swap_dir)[1]

if fstype == "btrfs":
subp.subp(["truncate", "-s", "0", fname])
subp.subp(["chattr", "+C", fname])

if (
fstype == "xfs" and util.kernel_version() < (4, 18)
) or fstype == "btrfs":
Expand Down
2 changes: 2 additions & 0 deletions tests/unittests/config/test_cc_mounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ def test_swap_creation_method_btrfs(
cc_mounts.handle(None, self.cc, self.mock_cloud, [])
self.m_subp_subp.assert_has_calls(
[
mock.call(["truncate", "-s", "0", self.swap_path]),
mock.call(["chattr", "+C", self.swap_path]),
mock.call(
[
"dd",
Expand Down
1 change: 1 addition & 0 deletions tools/.github-cla-signers
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bdrung
beantaxi
beezly
berolinux
bin456789
bipinbachhao
BirknerAlex
bmhughes
Expand Down

0 comments on commit 11a4fd1

Please sign in to comment.