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

filesystem: add bcachefs support #8126

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/8126-filesystem-bcachefs-support.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- filesystem - add bcachefs support (https://github.com/ansible-collections/community.general/pull/8126).
50 changes: 47 additions & 3 deletions plugins/modules/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@
default: present
version_added: 1.3.0
fstype:
choices: [ btrfs, ext2, ext3, ext4, ext4dev, f2fs, lvm, ocfs2, reiserfs, xfs, vfat, swap, ufs ]
choices: [ bcachefs, btrfs, ext2, ext3, ext4, ext4dev, f2fs, lvm, ocfs2, reiserfs, xfs, vfat, swap, ufs ]
description:
- Filesystem type to be created. This option is required with
O(state=present) (or if O(state) is omitted).
- ufs support has been added in community.general 3.4.0.
- bcachefs support has been added in community.general 8.6.0.
type: str
aliases: [type]
dev:
Expand All @@ -67,7 +68,7 @@
resizefs:
description:
- If V(true), if the block device and filesystem size differ, grow the filesystem into the space.
- Supported for C(btrfs), C(ext2), C(ext3), C(ext4), C(ext4dev), C(f2fs), C(lvm), C(xfs), C(ufs) and C(vfat) filesystems.
- Supported for C(bcachefs), 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
Expand All @@ -86,7 +87,7 @@
- The UUID options specified in O(opts) take precedence over this value.
- See xfs_admin(8) (C(xfs)), tune2fs(8) (C(ext2), C(ext3), C(ext4), C(ext4dev)) for possible values.
- For O(fstype=lvm) the value is ignored, it resets the PV UUID if set.
- Supported for O(fstype) being one of C(ext2), C(ext3), C(ext4), C(ext4dev), C(lvm), or C(xfs).
- Supported for O(fstype) being one of C(bcachefs), C(ext2), C(ext3), C(ext4), C(ext4dev), C(lvm), or C(xfs).
- This is B(not idempotent). Specifying this option will always result in a change.
- Mutually exclusive with O(resizefs).
type: str
Expand Down Expand Up @@ -405,6 +406,48 @@ class Reiserfs(Filesystem):
MKFS_FORCE_FLAGS = ['-q']


class Bcachefs(Filesystem):
MKFS = 'mkfs.bcachefs'
MKFS_FORCE_FLAGS = ['--force']
MKFS_SET_UUID_OPTIONS = ['-U', '--uuid']
INFO = 'bcachefs'
GROW = 'bcachefs'
GROW_MAX_SPACE_FLAGS = ['device', 'resize']

def get_fs_size(self, dev):
"""Return size in bytes of filesystem on device (integer)."""
dummy, stdout, dummy = self.module.run_command([self.module.get_bin_path(self.INFO),
'show-super', str(dev)], check_rc=True)

for line in stdout.splitlines():
if "Size: " in line:
parts = line.split()
unit = parts[2]

base = None
exp = None

units_2 = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
units_10 = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]

try:
exp = units_2.index(unit)
base = 1024
except ValueError:
exp = units_10.index(unit)
base = 1000

if exp == 0:
value = int(parts[1])
else:
value = float(parts[1])

if base is not None and exp is not None:
return int(value * pow(base, exp))

raise ValueError(repr(stdout))


class Btrfs(Filesystem):
MKFS = 'mkfs.btrfs'
INFO = 'btrfs'
Expand Down Expand Up @@ -567,6 +610,7 @@ def get_fs_size(self, dev):


FILESYSTEMS = {
'bcachefs': Bcachefs,
'ext2': Ext2,
'ext3': Ext3,
'ext4': Ext4,
Expand Down
1 change: 1 addition & 0 deletions tests/integration/targets/filesystem/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tested_filesystems:
# - 1.7.0 requires at least 30Mo
# - 1.10.0 requires at least 38Mo
# - resizefs asserts when initial fs is smaller than 60Mo and seems to require 1.10.0
bcachefs: {fssize: 20, grow: true, new_uuid: null}
ext4: {fssize: 10, grow: true, new_uuid: 'random'}
ext4dev: {fssize: 10, grow: true, new_uuid: 'random'}
ext3: {fssize: 10, grow: true, new_uuid: 'random'}
Expand Down
6 changes: 5 additions & 1 deletion tests/integration/targets/filesystem/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
# Not available: btrfs, lvm, f2fs, ocfs2
# All BSD systems use swap fs, but only Linux needs mkswap
# Supported: ext2/3/4 (e2fsprogs), xfs (xfsprogs), reiserfs (progsreiserfs), vfat
- 'not (ansible_system == "FreeBSD" and item.0.key in ["btrfs", "f2fs", "swap", "lvm", "ocfs2"])'
- 'not (ansible_system == "FreeBSD" and item.0.key in ["bcachefs", "btrfs", "f2fs", "swap", "lvm", "ocfs2"])'
# Available on FreeBSD but not on testbed (util-linux conflicts with e2fsprogs): wipefs, mkfs.minix
- 'not (ansible_system == "FreeBSD" and item.1 in ["overwrite_another_fs", "remove_fs"])'

Expand All @@ -46,6 +46,10 @@

# Other limitations and corner cases

# bcachefs only on Alpine > 3.18 and Arch Linux for now
# other distributions have too old versions of bcachefs-tools and/or util-linux (blkid for UUID tests)
- 'ansible_distribution == "Alpine" and ansible_distribution_version is version("3.18", ">") and item.0.key == "bcachefs"'
- 'ansible_distribution == "Archlinux" and item.0.key == "bcachefs"'
# f2fs-tools and reiserfs-utils packages not available with RHEL/CentOS on CI
- 'not (ansible_distribution in ["CentOS", "RedHat"] and item.0.key in ["f2fs", "reiserfs"])'
- 'not (ansible_os_family == "RedHat" and ansible_distribution_major_version is version("8", ">=") and
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/targets/filesystem/tasks/setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
- e2fsprogs
- xfsprogs

- name: "Install bcachefs tools"
ansible.builtin.package:
name: bcachefs-tools
state: present
when:
# bcachefs only on Alpine > 3.18 and Arch Linux for now
# other distributions have too old versions of bcachefs-tools and/or util-linux (blkid for UUID tests)
- ansible_distribution == "Alpine" and ansible_distribution_version is version("3.18", ">")
- ansible_distribution == "Archlinux"

- name: "Install btrfs progs"
ansible.builtin.package:
name: btrfs-progs
Expand Down
Loading