From 26d5409a8730682c7057afdfa4183c01ac24ed3d Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 13 Apr 2022 11:16:27 +0000 Subject: [PATCH] Implement btrfs resize support (#4465) (#4498) * Implement btrfs resize support * Add changelog fragment for btrfs resize support Co-authored-by: Fabian Klemp (cherry picked from commit 8ccc4d1fbb9b6da835201262e75195503336093f) Co-authored-by: elara-leitstellentechnik --- changelogs/fragments/4465-btrfs-resize.yml | 3 +++ plugins/modules/system/filesystem.py | 19 ++++++++++++++++++- .../targets/filesystem/defaults/main.yml | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/4465-btrfs-resize.yml diff --git a/changelogs/fragments/4465-btrfs-resize.yml b/changelogs/fragments/4465-btrfs-resize.yml new file mode 100644 index 0000000000..1cdeea16c8 --- /dev/null +++ b/changelogs/fragments/4465-btrfs-resize.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - filesystem - add support for resizing btrfs (https://github.com/ansible-collections/community.general/issues/4465). diff --git a/plugins/modules/system/filesystem.py b/plugins/modules/system/filesystem.py index 2245d341ce..9fb7810249 100644 --- a/plugins/modules/system/filesystem.py +++ b/plugins/modules/system/filesystem.py @@ -59,7 +59,7 @@ options: resizefs: description: - If C(yes), if the block device and filesystem size differ, grow the filesystem into the space. - - Supported for C(ext2), C(ext3), C(ext4), C(ext4dev), C(f2fs), C(lvm), C(xfs), C(ufs) and C(vfat) filesystems. + - Supported for C(btrfs), C(ext2), C(ext3), C(ext4), C(ext4dev), C(f2fs), C(lvm), C(xfs), C(ufs) and C(vfat) filesystems. Attempts to resize other filesystem types will fail. - XFS Will only grow if mounted. Currently, the module is based on commands from C(util-linux) package to perform operations, so resizing of XFS is @@ -331,6 +331,10 @@ class Reiserfs(Filesystem): class Btrfs(Filesystem): MKFS = 'mkfs.btrfs' + INFO = 'btrfs' + GROW = 'btrfs' + GROW_MAX_SPACE_FLAGS = ['filesystem', 'resize', 'max'] + GROW_MOUNTPOINT_ONLY = True def __init__(self, module): super(Btrfs, self).__init__(module) @@ -349,6 +353,19 @@ class Btrfs(Filesystem): self.MKFS_FORCE_FLAGS = ['-f'] self.module.warn('Unable to identify mkfs.btrfs version (%r, %r)' % (stdout, stderr)) + def get_fs_size(self, dev): + """Return size in bytes of filesystem on device (integer).""" + mountpoint = dev.get_mountpoint() + if not mountpoint: + self.module.fail_json(msg="%s needs to be mounted for %s operations" % (dev, self.fstype)) + + dummy, stdout, dummy = self.module.run_command([self.module.get_bin_path(self.INFO), + 'filesystem', 'usage', '-b', mountpoint], check_rc=True) + for line in stdout.splitlines(): + if "Device size" in line: + return int(line.split()[-1]) + raise ValueError(stdout) + class Ocfs2(Filesystem): MKFS = 'mkfs.ocfs2' diff --git a/tests/integration/targets/filesystem/defaults/main.yml b/tests/integration/targets/filesystem/defaults/main.yml index 27672bbea6..8b2928012b 100644 --- a/tests/integration/targets/filesystem/defaults/main.yml +++ b/tests/integration/targets/filesystem/defaults/main.yml @@ -16,7 +16,7 @@ tested_filesystems: ext3: {fssize: 10, grow: True} ext2: {fssize: 10, grow: True} xfs: {fssize: 20, grow: False} # grow requires a mounted filesystem - btrfs: {fssize: 150, grow: False} # grow not implemented + btrfs: {fssize: 150, grow: False} # grow requires a mounted filesystem reiserfs: {fssize: 33, grow: False} # grow not implemented vfat: {fssize: 20, grow: True} ocfs2: {fssize: '{{ ocfs2_fssize }}', grow: False} # grow not implemented