2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2021, quidame <quidame@poivron.org>
|
|
|
|
# Copyright (c) 2013, Alexander Bulimov <lazywolf0@gmail.com>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2021-05-18 06:46:45 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
author:
|
2021-05-18 06:46:45 +02:00
|
|
|
- Alexander Bulimov (@abulimov)
|
2021-07-10 16:37:31 +02:00
|
|
|
- quidame (@quidame)
|
2020-03-09 10:11:07 +01:00
|
|
|
module: filesystem
|
|
|
|
short_description: Makes a filesystem
|
|
|
|
description:
|
|
|
|
- This module creates a filesystem.
|
2023-02-24 09:25:15 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
2020-10-26 19:43:01 +01:00
|
|
|
state:
|
|
|
|
description:
|
2023-06-15 15:46:44 +02:00
|
|
|
- If O(state=present), the filesystem is created if it doesn't already
|
|
|
|
exist, that is the default behaviour if O(state) is omitted.
|
|
|
|
- If O(state=absent), filesystem signatures on O(dev) are wiped if it
|
2021-05-18 06:46:45 +02:00
|
|
|
contains a filesystem (as known by C(blkid)).
|
2023-06-15 15:46:44 +02:00
|
|
|
- When O(state=absent), all other options but O(dev) are ignored, and the
|
|
|
|
module does not fail if the device O(dev) doesn't actually exist.
|
2020-10-26 19:43:01 +01:00
|
|
|
type: str
|
|
|
|
choices: [ present, absent ]
|
|
|
|
default: present
|
|
|
|
version_added: 1.3.0
|
2020-03-09 10:11:07 +01:00
|
|
|
fstype:
|
2021-07-10 16:37:31 +02:00
|
|
|
choices: [ btrfs, ext2, ext3, ext4, ext4dev, f2fs, lvm, ocfs2, reiserfs, xfs, vfat, swap, ufs ]
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2021-05-18 06:46:45 +02:00
|
|
|
- Filesystem type to be created. This option is required with
|
2023-06-15 15:46:44 +02:00
|
|
|
O(state=present) (or if O(state) is omitted).
|
2021-07-10 16:37:31 +02:00
|
|
|
- ufs support has been added in community.general 3.4.0.
|
2020-10-26 19:43:01 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
aliases: [type]
|
|
|
|
dev:
|
|
|
|
description:
|
2021-07-10 16:37:31 +02:00
|
|
|
- Target path to block device (Linux) or character device (FreeBSD) or
|
|
|
|
regular file (both).
|
|
|
|
- When setting Linux-specific filesystem types on FreeBSD, this module
|
|
|
|
only works when applying to regular files, aka disk images.
|
2023-06-15 15:46:44 +02:00
|
|
|
- Currently V(lvm) (Linux-only) and V(ufs) (FreeBSD-only) do not support
|
|
|
|
a regular file as their target O(dev).
|
2021-07-10 16:37:31 +02:00
|
|
|
- Support for character devices on FreeBSD has been added in community.general 3.4.0.
|
2020-10-26 19:43:01 +01:00
|
|
|
type: path
|
2022-08-24 19:59:01 +02:00
|
|
|
required: true
|
2020-03-09 10:11:07 +01:00
|
|
|
aliases: [device]
|
|
|
|
force:
|
|
|
|
description:
|
2023-06-15 15:46:44 +02:00
|
|
|
- If V(true), allows to create new filesystem on devices that already has filesystem.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
2022-08-24 19:59:01 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
resizefs:
|
|
|
|
description:
|
2023-06-15 15:46:44 +02:00
|
|
|
- If V(true), if the block device and filesystem size differ, grow the filesystem into the space.
|
2022-04-13 07:07:51 +02:00
|
|
|
- Supported for C(btrfs), C(ext2), C(ext3), C(ext4), C(ext4dev), C(f2fs), C(lvm), C(xfs), C(ufs) and C(vfat) filesystems.
|
2021-05-18 06:46:45 +02:00
|
|
|
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
|
|
|
|
not supported on FreeBSD systems.
|
2022-09-06 07:31:25 +02:00
|
|
|
- vFAT will likely fail if C(fatresize < 1.04).
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
2022-08-24 19:59:01 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
opts:
|
|
|
|
description:
|
2022-09-06 07:31:25 +02:00
|
|
|
- List of options to be passed to C(mkfs) command.
|
2020-10-26 19:43:01 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
requirements:
|
2023-06-15 15:46:44 +02:00
|
|
|
- Uses specific tools related to the O(fstype) for creating or resizing a
|
2021-07-10 16:37:31 +02:00
|
|
|
filesystem (from packages e2fsprogs, xfsprogs, dosfstools, and so on).
|
|
|
|
- Uses generic tools mostly related to the Operating System (Linux or
|
|
|
|
FreeBSD) or available on both, as C(blkid).
|
|
|
|
- On FreeBSD, either C(util-linux) or C(e2fsprogs) package is required.
|
2020-03-09 10:11:07 +01:00
|
|
|
notes:
|
2023-06-15 15:46:44 +02:00
|
|
|
- Potential filesystems on O(dev) are checked using C(blkid). In case C(blkid)
|
2021-07-10 16:37:31 +02:00
|
|
|
is unable to detect a filesystem (and in case C(fstyp) on FreeBSD is also
|
|
|
|
unable to detect a filesystem), this filesystem is overwritten even if
|
2023-06-15 15:46:44 +02:00
|
|
|
O(force) is V(false).
|
2021-07-10 16:37:31 +02:00
|
|
|
- On FreeBSD systems, both C(e2fsprogs) and C(util-linux) packages provide
|
|
|
|
a C(blkid) command that is compatible with this module. However, these
|
|
|
|
packages conflict with each other, and only the C(util-linux) package
|
2023-06-15 15:46:44 +02:00
|
|
|
provides the command required to not fail when O(state=absent).
|
2021-07-10 16:37:31 +02:00
|
|
|
seealso:
|
|
|
|
- module: community.general.filesize
|
|
|
|
- module: ansible.posix.mount
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Create a ext2 filesystem on /dev/sdb1
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.filesystem:
|
2020-03-09 10:11:07 +01:00
|
|
|
fstype: ext2
|
|
|
|
dev: /dev/sdb1
|
|
|
|
|
|
|
|
- name: Create a ext4 filesystem on /dev/sdb1 and check disk blocks
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.filesystem:
|
2020-03-09 10:11:07 +01:00
|
|
|
fstype: ext4
|
|
|
|
dev: /dev/sdb1
|
|
|
|
opts: -cc
|
2020-10-26 19:43:01 +01:00
|
|
|
|
|
|
|
- name: Blank filesystem signature on /dev/sdb1
|
|
|
|
community.general.filesystem:
|
|
|
|
dev: /dev/sdb1
|
|
|
|
state: absent
|
2021-07-10 16:37:31 +02:00
|
|
|
|
|
|
|
- name: Create a filesystem on top of a regular file
|
|
|
|
community.general.filesystem:
|
|
|
|
dev: /path/to/disk.img
|
|
|
|
fstype: vfat
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
import re
|
|
|
|
import stat
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-12-24 18:34:48 +01:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
class Device(object):
|
|
|
|
def __init__(self, module, path):
|
|
|
|
self.module = module
|
|
|
|
self.path = path
|
|
|
|
|
|
|
|
def size(self):
|
|
|
|
""" Return size in bytes of device. Returns int """
|
|
|
|
statinfo = os.stat(self.path)
|
|
|
|
if stat.S_ISBLK(statinfo.st_mode):
|
|
|
|
blockdev_cmd = self.module.get_bin_path("blockdev", required=True)
|
2021-05-18 06:46:45 +02:00
|
|
|
dummy, out, dummy = self.module.run_command([blockdev_cmd, "--getsize64", self.path], check_rc=True)
|
|
|
|
devsize_in_bytes = int(out)
|
2021-07-10 16:37:31 +02:00
|
|
|
elif stat.S_ISCHR(statinfo.st_mode) and platform.system() == 'FreeBSD':
|
|
|
|
diskinfo_cmd = self.module.get_bin_path("diskinfo", required=True)
|
|
|
|
dummy, out, dummy = self.module.run_command([diskinfo_cmd, self.path], check_rc=True)
|
|
|
|
devsize_in_bytes = int(out.split()[2])
|
2020-03-09 10:11:07 +01:00
|
|
|
elif os.path.isfile(self.path):
|
2021-05-18 06:46:45 +02:00
|
|
|
devsize_in_bytes = os.path.getsize(self.path)
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
|
|
|
self.module.fail_json(changed=False, msg="Target device not supported: %s" % self)
|
|
|
|
|
2021-05-18 06:46:45 +02:00
|
|
|
return devsize_in_bytes
|
|
|
|
|
2020-06-23 15:04:55 +02:00
|
|
|
def get_mountpoint(self):
|
|
|
|
"""Return (first) mountpoint of device. Returns None when not mounted."""
|
|
|
|
cmd_findmnt = self.module.get_bin_path("findmnt", required=True)
|
|
|
|
|
|
|
|
# find mountpoint
|
2021-02-16 08:03:51 +01:00
|
|
|
rc, mountpoint, dummy = self.module.run_command([cmd_findmnt, "--mtab", "--noheadings", "--output",
|
|
|
|
"TARGET", "--source", self.path], check_rc=False)
|
2020-06-23 15:04:55 +02:00
|
|
|
if rc != 0:
|
|
|
|
mountpoint = None
|
|
|
|
else:
|
|
|
|
mountpoint = mountpoint.split('\n')[0]
|
|
|
|
|
|
|
|
return mountpoint
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
def __str__(self):
|
|
|
|
return self.path
|
|
|
|
|
|
|
|
|
|
|
|
class Filesystem(object):
|
|
|
|
|
|
|
|
MKFS = None
|
2021-05-18 06:46:45 +02:00
|
|
|
MKFS_FORCE_FLAGS = []
|
|
|
|
INFO = None
|
|
|
|
GROW = None
|
|
|
|
GROW_MAX_SPACE_FLAGS = []
|
|
|
|
GROW_MOUNTPOINT_ONLY = False
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
LANG_ENV = {'LANG': 'C', 'LC_ALL': 'C', 'LC_MESSAGES': 'C'}
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
|
|
|
|
@property
|
|
|
|
def fstype(self):
|
|
|
|
return type(self).__name__
|
|
|
|
|
|
|
|
def get_fs_size(self, dev):
|
2021-05-18 06:46:45 +02:00
|
|
|
"""Return size in bytes of filesystem on device (integer).
|
|
|
|
Should query the info with a per-fstype command that can access the
|
|
|
|
device whenever it is mounted or not, and parse the command output.
|
|
|
|
Parser must ensure to return an integer, or raise a ValueError.
|
|
|
|
"""
|
2020-03-09 10:11:07 +01:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def create(self, opts, dev):
|
|
|
|
if self.module.check_mode:
|
|
|
|
return
|
|
|
|
|
|
|
|
mkfs = self.module.get_bin_path(self.MKFS, required=True)
|
2021-05-18 06:46:45 +02:00
|
|
|
cmd = [mkfs] + self.MKFS_FORCE_FLAGS + opts + [str(dev)]
|
2020-03-09 10:11:07 +01:00
|
|
|
self.module.run_command(cmd, check_rc=True)
|
|
|
|
|
2020-10-26 19:43:01 +01:00
|
|
|
def wipefs(self, dev):
|
|
|
|
if self.module.check_mode:
|
|
|
|
return
|
|
|
|
|
|
|
|
# wipefs comes with util-linux package (as 'blockdev' & 'findmnt' above)
|
2021-05-18 06:46:45 +02:00
|
|
|
# that is ported to FreeBSD. The use of dd as a portable fallback is
|
2020-10-26 19:43:01 +01:00
|
|
|
# not doable here if it needs get_mountpoint() (to prevent corruption of
|
2021-05-18 06:46:45 +02:00
|
|
|
# a mounted filesystem), since 'findmnt' is not available on FreeBSD,
|
|
|
|
# even in util-linux port for this OS.
|
2020-10-26 19:43:01 +01:00
|
|
|
wipefs = self.module.get_bin_path('wipefs', required=True)
|
2021-05-18 06:46:45 +02:00
|
|
|
cmd = [wipefs, "--all", str(dev)]
|
2020-10-26 19:43:01 +01:00
|
|
|
self.module.run_command(cmd, check_rc=True)
|
|
|
|
|
2021-05-18 06:46:45 +02:00
|
|
|
def grow_cmd(self, target):
|
|
|
|
"""Build and return the resizefs commandline as list."""
|
|
|
|
cmdline = [self.module.get_bin_path(self.GROW, required=True)]
|
|
|
|
cmdline += self.GROW_MAX_SPACE_FLAGS + [target]
|
|
|
|
return cmdline
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def grow(self, dev):
|
|
|
|
"""Get dev and fs size and compare. Returns stdout of used command."""
|
|
|
|
devsize_in_bytes = dev.size()
|
|
|
|
|
|
|
|
try:
|
|
|
|
fssize_in_bytes = self.get_fs_size(dev)
|
|
|
|
except NotImplementedError:
|
2021-05-18 06:46:45 +02:00
|
|
|
self.module.fail_json(msg="module does not support resizing %s filesystem yet" % self.fstype)
|
|
|
|
except ValueError as err:
|
|
|
|
self.module.warn("unable to process %s output '%s'" % (self.INFO, to_native(err)))
|
|
|
|
self.module.fail_json(msg="unable to process %s output for %s" % (self.INFO, dev))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if not fssize_in_bytes < devsize_in_bytes:
|
|
|
|
self.module.exit_json(changed=False, msg="%s filesystem is using the whole device %s" % (self.fstype, dev))
|
|
|
|
elif self.module.check_mode:
|
2021-05-18 06:46:45 +02:00
|
|
|
self.module.exit_json(changed=True, msg="resizing filesystem %s on device %s" % (self.fstype, dev))
|
|
|
|
|
|
|
|
if self.GROW_MOUNTPOINT_ONLY:
|
|
|
|
mountpoint = dev.get_mountpoint()
|
|
|
|
if not mountpoint:
|
|
|
|
self.module.fail_json(msg="%s needs to be mounted for %s operations" % (dev, self.fstype))
|
|
|
|
grow_target = mountpoint
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
2021-05-18 06:46:45 +02:00
|
|
|
grow_target = str(dev)
|
|
|
|
|
|
|
|
dummy, out, dummy = self.module.run_command(self.grow_cmd(grow_target), check_rc=True)
|
|
|
|
return out
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Ext(Filesystem):
|
2021-05-18 06:46:45 +02:00
|
|
|
MKFS_FORCE_FLAGS = ['-F']
|
|
|
|
INFO = 'tune2fs'
|
2020-03-09 10:11:07 +01:00
|
|
|
GROW = 'resize2fs'
|
|
|
|
|
|
|
|
def get_fs_size(self, dev):
|
2021-05-18 06:46:45 +02:00
|
|
|
"""Get Block count and Block size and return their product."""
|
|
|
|
cmd = self.module.get_bin_path(self.INFO, required=True)
|
|
|
|
dummy, out, dummy = self.module.run_command([cmd, '-l', str(dev)], check_rc=True, environ_update=self.LANG_ENV)
|
|
|
|
|
|
|
|
block_count = block_size = None
|
|
|
|
for line in out.splitlines():
|
2020-03-09 10:11:07 +01:00
|
|
|
if 'Block count:' in line:
|
|
|
|
block_count = int(line.split(':')[1].strip())
|
|
|
|
elif 'Block size:' in line:
|
|
|
|
block_size = int(line.split(':')[1].strip())
|
2021-05-18 06:46:45 +02:00
|
|
|
if None not in (block_size, block_count):
|
|
|
|
break
|
|
|
|
else:
|
2022-05-22 17:20:30 +02:00
|
|
|
raise ValueError(repr(out))
|
2021-05-18 06:46:45 +02:00
|
|
|
|
|
|
|
return block_size * block_count
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Ext2(Ext):
|
|
|
|
MKFS = 'mkfs.ext2'
|
|
|
|
|
|
|
|
|
|
|
|
class Ext3(Ext):
|
|
|
|
MKFS = 'mkfs.ext3'
|
|
|
|
|
|
|
|
|
|
|
|
class Ext4(Ext):
|
|
|
|
MKFS = 'mkfs.ext4'
|
|
|
|
|
|
|
|
|
|
|
|
class XFS(Filesystem):
|
|
|
|
MKFS = 'mkfs.xfs'
|
2021-05-18 06:46:45 +02:00
|
|
|
MKFS_FORCE_FLAGS = ['-f']
|
|
|
|
INFO = 'xfs_info'
|
2020-03-09 10:11:07 +01:00
|
|
|
GROW = 'xfs_growfs'
|
2021-05-18 06:46:45 +02:00
|
|
|
GROW_MOUNTPOINT_ONLY = True
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def get_fs_size(self, dev):
|
2021-05-18 06:46:45 +02:00
|
|
|
"""Get bsize and blocks and return their product."""
|
|
|
|
cmdline = [self.module.get_bin_path(self.INFO, required=True)]
|
|
|
|
|
|
|
|
# Depending on the versions, xfs_info is able to get info from the
|
|
|
|
# device, whenever it is mounted or not, or only if unmounted, or
|
|
|
|
# only if mounted, or not at all. For any version until now, it is
|
|
|
|
# able to query info from the mountpoint. So try it first, and use
|
|
|
|
# device as the last resort: it may or may not work.
|
2021-02-03 07:54:04 +01:00
|
|
|
mountpoint = dev.get_mountpoint()
|
|
|
|
if mountpoint:
|
2021-05-18 06:46:45 +02:00
|
|
|
cmdline += [mountpoint]
|
2021-02-03 07:54:04 +01:00
|
|
|
else:
|
2021-05-18 06:46:45 +02:00
|
|
|
cmdline += [str(dev)]
|
|
|
|
dummy, out, dummy = self.module.run_command(cmdline, check_rc=True, environ_update=self.LANG_ENV)
|
2020-06-23 15:04:55 +02:00
|
|
|
|
2021-05-18 06:46:45 +02:00
|
|
|
block_size = block_count = None
|
2021-02-03 07:54:04 +01:00
|
|
|
for line in out.splitlines():
|
2020-03-09 10:11:07 +01:00
|
|
|
col = line.split('=')
|
|
|
|
if col[0].strip() == 'data':
|
2021-05-18 06:46:45 +02:00
|
|
|
if col[1].strip() == 'bsize':
|
|
|
|
block_size = int(col[2].split()[0])
|
|
|
|
if col[2].split()[1] == 'blocks':
|
|
|
|
block_count = int(col[3].split(',')[0])
|
|
|
|
if None not in (block_size, block_count):
|
|
|
|
break
|
|
|
|
else:
|
2022-05-22 17:20:30 +02:00
|
|
|
raise ValueError(repr(out))
|
2020-06-23 15:04:55 +02:00
|
|
|
|
2021-05-18 06:46:45 +02:00
|
|
|
return block_size * block_count
|
2020-06-23 15:04:55 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
class Reiserfs(Filesystem):
|
|
|
|
MKFS = 'mkfs.reiserfs'
|
2021-05-18 06:46:45 +02:00
|
|
|
MKFS_FORCE_FLAGS = ['-q']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Btrfs(Filesystem):
|
|
|
|
MKFS = 'mkfs.btrfs'
|
2022-04-13 07:07:51 +02:00
|
|
|
INFO = 'btrfs'
|
|
|
|
GROW = 'btrfs'
|
|
|
|
GROW_MAX_SPACE_FLAGS = ['filesystem', 'resize', 'max']
|
|
|
|
GROW_MOUNTPOINT_ONLY = True
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
super(Btrfs, self).__init__(module)
|
2021-05-18 06:46:45 +02:00
|
|
|
mkfs = self.module.get_bin_path(self.MKFS, required=True)
|
|
|
|
dummy, stdout, stderr = self.module.run_command([mkfs, '--version'], check_rc=True)
|
2020-03-09 10:11:07 +01:00
|
|
|
match = re.search(r" v([0-9.]+)", stdout)
|
|
|
|
if not match:
|
|
|
|
# v0.20-rc1 use stderr
|
|
|
|
match = re.search(r" v([0-9.]+)", stderr)
|
|
|
|
if match:
|
|
|
|
# v0.20-rc1 doesn't have --force parameter added in following version v3.12
|
|
|
|
if LooseVersion(match.group(1)) >= LooseVersion('3.12'):
|
2021-05-18 06:46:45 +02:00
|
|
|
self.MKFS_FORCE_FLAGS = ['-f']
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
|
|
|
# assume version is greater or equal to 3.12
|
2021-05-18 06:46:45 +02:00
|
|
|
self.MKFS_FORCE_FLAGS = ['-f']
|
2020-03-09 10:11:07 +01:00
|
|
|
self.module.warn('Unable to identify mkfs.btrfs version (%r, %r)' % (stdout, stderr))
|
|
|
|
|
2022-04-13 07:07:51 +02:00
|
|
|
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])
|
2022-05-22 17:20:30 +02:00
|
|
|
raise ValueError(repr(stdout))
|
2022-04-13 07:07:51 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
class Ocfs2(Filesystem):
|
|
|
|
MKFS = 'mkfs.ocfs2'
|
2021-05-18 06:46:45 +02:00
|
|
|
MKFS_FORCE_FLAGS = ['-Fx']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class F2fs(Filesystem):
|
|
|
|
MKFS = 'mkfs.f2fs'
|
2021-05-18 06:46:45 +02:00
|
|
|
INFO = 'dump.f2fs'
|
2020-03-09 10:11:07 +01:00
|
|
|
GROW = 'resize.f2fs'
|
|
|
|
|
2021-05-18 06:46:45 +02:00
|
|
|
def __init__(self, module):
|
|
|
|
super(F2fs, self).__init__(module)
|
2020-03-09 10:11:07 +01:00
|
|
|
mkfs = self.module.get_bin_path(self.MKFS, required=True)
|
2021-05-18 06:46:45 +02:00
|
|
|
dummy, out, dummy = self.module.run_command([mkfs, os.devnull], check_rc=False, environ_update=self.LANG_ENV)
|
2020-03-09 10:11:07 +01:00
|
|
|
# Looking for " F2FS-tools: mkfs.f2fs Ver: 1.10.0 (2018-01-30)"
|
|
|
|
# mkfs.f2fs displays version since v1.2.0
|
|
|
|
match = re.search(r"F2FS-tools: mkfs.f2fs Ver: ([0-9.]+) \(", out)
|
|
|
|
if match is not None:
|
|
|
|
# Since 1.9.0, mkfs.f2fs check overwrite before make filesystem
|
|
|
|
# before that version -f switch wasn't used
|
|
|
|
if LooseVersion(match.group(1)) >= LooseVersion('1.9.0'):
|
2021-05-18 06:46:45 +02:00
|
|
|
self.MKFS_FORCE_FLAGS = ['-f']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def get_fs_size(self, dev):
|
2021-05-18 06:46:45 +02:00
|
|
|
"""Get sector size and total FS sectors and return their product."""
|
|
|
|
cmd = self.module.get_bin_path(self.INFO, required=True)
|
|
|
|
dummy, out, dummy = self.module.run_command([cmd, str(dev)], check_rc=True, environ_update=self.LANG_ENV)
|
|
|
|
sector_size = sector_count = None
|
|
|
|
for line in out.splitlines():
|
2020-03-09 10:11:07 +01:00
|
|
|
if 'Info: sector size = ' in line:
|
|
|
|
# expected: 'Info: sector size = 512'
|
|
|
|
sector_size = int(line.split()[4])
|
|
|
|
elif 'Info: total FS sectors = ' in line:
|
|
|
|
# expected: 'Info: total FS sectors = 102400 (50 MB)'
|
|
|
|
sector_count = int(line.split()[5])
|
|
|
|
if None not in (sector_size, sector_count):
|
|
|
|
break
|
|
|
|
else:
|
2022-05-22 17:20:30 +02:00
|
|
|
raise ValueError(repr(out))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
return sector_size * sector_count
|
|
|
|
|
|
|
|
|
|
|
|
class VFAT(Filesystem):
|
2021-05-18 06:46:45 +02:00
|
|
|
INFO = 'fatresize'
|
2020-03-09 10:11:07 +01:00
|
|
|
GROW = 'fatresize'
|
2021-05-18 06:46:45 +02:00
|
|
|
GROW_MAX_SPACE_FLAGS = ['-s', 'max']
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
super(VFAT, self).__init__(module)
|
|
|
|
if platform.system() == 'FreeBSD':
|
|
|
|
self.MKFS = 'newfs_msdos'
|
|
|
|
else:
|
|
|
|
self.MKFS = 'mkfs.vfat'
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def get_fs_size(self, dev):
|
2021-05-18 06:46:45 +02:00
|
|
|
"""Get and return size of filesystem, in bytes."""
|
|
|
|
cmd = self.module.get_bin_path(self.INFO, required=True)
|
|
|
|
dummy, out, dummy = self.module.run_command([cmd, '--info', str(dev)], check_rc=True, environ_update=self.LANG_ENV)
|
|
|
|
fssize = None
|
|
|
|
for line in out.splitlines()[1:]:
|
2022-05-22 17:20:30 +02:00
|
|
|
parts = line.split(':', 1)
|
|
|
|
if len(parts) < 2:
|
|
|
|
continue
|
|
|
|
param, value = parts
|
|
|
|
if param.strip() in ('Size', 'Cur size'):
|
2021-05-18 06:46:45 +02:00
|
|
|
fssize = int(value.strip())
|
|
|
|
break
|
|
|
|
else:
|
2022-05-22 17:20:30 +02:00
|
|
|
raise ValueError(repr(out))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-05-18 06:46:45 +02:00
|
|
|
return fssize
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class LVM(Filesystem):
|
|
|
|
MKFS = 'pvcreate'
|
2021-05-18 06:46:45 +02:00
|
|
|
MKFS_FORCE_FLAGS = ['-f']
|
|
|
|
INFO = 'pvs'
|
2020-03-09 10:11:07 +01:00
|
|
|
GROW = 'pvresize'
|
|
|
|
|
|
|
|
def get_fs_size(self, dev):
|
2021-05-18 06:46:45 +02:00
|
|
|
"""Get and return PV size, in bytes."""
|
|
|
|
cmd = self.module.get_bin_path(self.INFO, required=True)
|
2021-02-16 08:03:51 +01:00
|
|
|
dummy, size, dummy = self.module.run_command([cmd, '--noheadings', '-o', 'pv_size', '--units', 'b', '--nosuffix', str(dev)], check_rc=True)
|
2021-05-18 06:46:45 +02:00
|
|
|
pv_size = int(size)
|
|
|
|
return pv_size
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Swap(Filesystem):
|
|
|
|
MKFS = 'mkswap'
|
2021-05-18 06:46:45 +02:00
|
|
|
MKFS_FORCE_FLAGS = ['-f']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
2021-07-10 16:37:31 +02:00
|
|
|
class UFS(Filesystem):
|
|
|
|
MKFS = 'newfs'
|
|
|
|
INFO = 'dumpfs'
|
|
|
|
GROW = 'growfs'
|
|
|
|
GROW_MAX_SPACE_FLAGS = ['-y']
|
|
|
|
|
|
|
|
def get_fs_size(self, dev):
|
|
|
|
"""Get providersize and fragment size and return their product."""
|
|
|
|
cmd = self.module.get_bin_path(self.INFO, required=True)
|
|
|
|
dummy, out, dummy = self.module.run_command([cmd, str(dev)], check_rc=True, environ_update=self.LANG_ENV)
|
|
|
|
|
|
|
|
fragmentsize = providersize = None
|
|
|
|
for line in out.splitlines():
|
|
|
|
if line.startswith('fsize'):
|
|
|
|
fragmentsize = int(line.split()[1])
|
|
|
|
elif 'providersize' in line:
|
|
|
|
providersize = int(line.split()[-1])
|
|
|
|
if None not in (fragmentsize, providersize):
|
|
|
|
break
|
|
|
|
else:
|
2022-05-22 17:20:30 +02:00
|
|
|
raise ValueError(repr(out))
|
2021-07-10 16:37:31 +02:00
|
|
|
|
|
|
|
return fragmentsize * providersize
|
|
|
|
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
FILESYSTEMS = {
|
|
|
|
'ext2': Ext2,
|
|
|
|
'ext3': Ext3,
|
|
|
|
'ext4': Ext4,
|
|
|
|
'ext4dev': Ext4,
|
|
|
|
'f2fs': F2fs,
|
|
|
|
'reiserfs': Reiserfs,
|
|
|
|
'xfs': XFS,
|
|
|
|
'btrfs': Btrfs,
|
|
|
|
'vfat': VFAT,
|
|
|
|
'ocfs2': Ocfs2,
|
|
|
|
'LVM2_member': LVM,
|
|
|
|
'swap': Swap,
|
2021-07-10 16:37:31 +02:00
|
|
|
'ufs': UFS,
|
2020-03-09 10:11:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
friendly_names = {
|
|
|
|
'lvm': 'LVM2_member',
|
|
|
|
}
|
|
|
|
|
|
|
|
fstypes = set(FILESYSTEMS.keys()) - set(friendly_names.values()) | set(friendly_names.keys())
|
|
|
|
|
|
|
|
# There is no "single command" to manipulate filesystems, so we map them all out and their options
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2020-10-26 19:43:01 +01:00
|
|
|
state=dict(type='str', default='present', choices=['present', 'absent']),
|
|
|
|
fstype=dict(type='str', aliases=['type'], choices=list(fstypes)),
|
|
|
|
dev=dict(type='path', required=True, aliases=['device']),
|
|
|
|
opts=dict(type='str'),
|
2020-03-09 10:11:07 +01:00
|
|
|
force=dict(type='bool', default=False),
|
|
|
|
resizefs=dict(type='bool', default=False),
|
|
|
|
),
|
2020-10-26 19:43:01 +01:00
|
|
|
required_if=[
|
|
|
|
('state', 'present', ['fstype'])
|
|
|
|
],
|
2020-03-09 10:11:07 +01:00
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
2020-10-26 19:43:01 +01:00
|
|
|
state = module.params['state']
|
2020-03-09 10:11:07 +01:00
|
|
|
dev = module.params['dev']
|
|
|
|
fstype = module.params['fstype']
|
|
|
|
opts = module.params['opts']
|
|
|
|
force = module.params['force']
|
|
|
|
resizefs = module.params['resizefs']
|
|
|
|
|
2021-05-18 06:46:45 +02:00
|
|
|
mkfs_opts = []
|
|
|
|
if opts is not None:
|
|
|
|
mkfs_opts = opts.split()
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = False
|
|
|
|
|
|
|
|
if not os.path.exists(dev):
|
2020-10-26 19:43:01 +01:00
|
|
|
msg = "Device %s not found." % dev
|
|
|
|
if state == "present":
|
|
|
|
module.fail_json(msg=msg)
|
|
|
|
else:
|
|
|
|
module.exit_json(msg=msg)
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
dev = Device(module, dev)
|
|
|
|
|
2021-07-10 16:37:31 +02:00
|
|
|
# In case blkid/fstyp isn't able to identify an existing filesystem, device
|
|
|
|
# is considered as empty, then this existing filesystem would be overwritten
|
|
|
|
# even if force isn't enabled.
|
2020-03-09 10:11:07 +01:00
|
|
|
cmd = module.get_bin_path('blkid', required=True)
|
2021-05-18 06:46:45 +02:00
|
|
|
rc, raw_fs, err = module.run_command([cmd, '-c', os.devnull, '-o', 'value', '-s', 'TYPE', str(dev)])
|
2020-03-09 10:11:07 +01:00
|
|
|
fs = raw_fs.strip()
|
2021-07-10 16:37:31 +02:00
|
|
|
if not fs and platform.system() == 'FreeBSD':
|
|
|
|
cmd = module.get_bin_path('fstyp', required=True)
|
|
|
|
rc, raw_fs, err = module.run_command([cmd, str(dev)])
|
|
|
|
fs = raw_fs.strip()
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-10-26 19:43:01 +01:00
|
|
|
if state == "present":
|
|
|
|
if fstype in friendly_names:
|
|
|
|
fstype = friendly_names[fstype]
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-10-26 19:43:01 +01:00
|
|
|
try:
|
|
|
|
klass = FILESYSTEMS[fstype]
|
|
|
|
except KeyError:
|
|
|
|
module.fail_json(changed=False, msg="module does not support this filesystem (%s) yet." % fstype)
|
|
|
|
|
|
|
|
filesystem = klass(module)
|
|
|
|
|
|
|
|
same_fs = fs and FILESYSTEMS.get(fs) == FILESYSTEMS[fstype]
|
|
|
|
if same_fs and not resizefs and not force:
|
|
|
|
module.exit_json(changed=False)
|
|
|
|
elif same_fs and resizefs:
|
|
|
|
if not filesystem.GROW:
|
|
|
|
module.fail_json(changed=False, msg="module does not support resizing %s filesystem yet." % fstype)
|
|
|
|
|
|
|
|
out = filesystem.grow(dev)
|
|
|
|
|
|
|
|
module.exit_json(changed=True, msg=out)
|
|
|
|
elif fs and not force:
|
2022-08-24 19:59:01 +02:00
|
|
|
module.fail_json(msg="'%s' is already used as %s, use force=true to overwrite" % (dev, fs), rc=rc, err=err)
|
2020-10-26 19:43:01 +01:00
|
|
|
|
|
|
|
# create fs
|
2021-05-18 06:46:45 +02:00
|
|
|
filesystem.create(mkfs_opts, dev)
|
2020-10-26 19:43:01 +01:00
|
|
|
changed = True
|
|
|
|
|
|
|
|
elif fs:
|
|
|
|
# wipe fs signatures
|
|
|
|
filesystem = Filesystem(module)
|
|
|
|
filesystem.wipefs(dev)
|
|
|
|
changed = True
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
module.exit_json(changed=changed)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|