1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

parted: Allow passing negative numbers to specify partition boundary relative to disk end (#129)

* Allow passing negative numbers to specify partition boundary relative to disk end
Fixes: https://github.com/ansible/ansible/issues/43369

* parted: unit test case, create partition with part_start: -1GiB

* fs_type parameter is not really optional for negative part_start parameter

* Revert "fs_type parameter is not really optional for negative part_start parameter"

This reverts commit 800b1cb00b.

Instead: added notes and documentation about netagive part_start and fs_type.

* include fs_type in negative part_start example
This commit is contained in:
Robert Osowiecki 2020-06-23 14:02:35 +02:00 committed by GitHub
parent 4399759cf3
commit ced14746a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 5 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- "parted - accept negative numbers in ``part_start`` and ``part_end``"

View file

@ -66,15 +66,18 @@ options:
part_start: part_start:
description: description:
- Where the partition will start as offset from the beginning of the disk, - Where the partition will start as offset from the beginning of the disk,
that is, the "distance" from the start of the disk. that is, the "distance" from the start of the disk. Negative numbers
specify distance from the end of the disk.
- The distance can be specified with all the units supported by parted - The distance can be specified with all the units supported by parted
(except compat) and it is case sensitive, e.g. C(10GiB), C(15%). (except compat) and it is case sensitive, e.g. C(10GiB), C(15%).
- Using negative values may require setting of C(fs_type) (see notes).
type: str type: str
default: 0% default: 0%
part_end : part_end:
description: description:
- Where the partition will end as offset from the beginning of the disk, - Where the partition will end as offset from the beginning of the disk,
that is, the "distance" from the start of the disk. that is, the "distance" from the start of the disk. Negative numbers
specify distance from the end of the disk.
- The distance can be specified with all the units supported by parted - The distance can be specified with all the units supported by parted
(except compat) and it is case sensitive, e.g. C(10GiB), C(15%). (except compat) and it is case sensitive, e.g. C(10GiB), C(15%).
type: str type: str
@ -96,6 +99,7 @@ options:
fs_type: fs_type:
description: description:
- If specified and the partition does not exist, will set filesystem type to given partition. - If specified and the partition does not exist, will set filesystem type to given partition.
- Parameter optional, but see notes below about negative negative C(part_start) values.
type: str type: str
version_added: '0.2.0' version_added: '0.2.0'
notes: notes:
@ -103,6 +107,9 @@ notes:
installed on the system is before version 3.1, the module queries the kernel installed on the system is before version 3.1, the module queries the kernel
through C(/sys/) to obtain disk information. In this case the units CHS and through C(/sys/) to obtain disk information. In this case the units CHS and
CYL are not supported. CYL are not supported.
- Negative C(part_start) start values were rejected if C(fs_type) was not given.
This bug was fixed in parted 3.2.153. If you want to use negative C(part_start),
specify C(fs_type) as well or make sure your system contains newer parted.
''' '''
RETURN = r''' RETURN = r'''
@ -180,6 +187,14 @@ EXAMPLES = r'''
state: present state: present
part_start: 1GiB part_start: 1GiB
- name: Create a new primary partition with a size of 1GiB at disk's end
parted:
device: /dev/sdb
number: 3
state: present
fs_type: ext3
part_start: -1GiB
# Example on how to read info and reuse it in subsequent task # Example on how to read info and reuse it in subsequent task
- name: Read device information (always use unit when probing) - name: Read device information (always use unit when probing)
parted: device=/dev/sdb unit=MiB parted: device=/dev/sdb unit=MiB
@ -208,9 +223,9 @@ parted_units = units_si + units_iec + ['s', '%', 'cyl', 'chs', 'compact']
def parse_unit(size_str, unit=''): def parse_unit(size_str, unit=''):
""" """
Parses a string containing a size of information Parses a string containing a size or boundary information
""" """
matches = re.search(r'^([\d.]+)([\w%]+)?$', size_str) matches = re.search(r'^(-?[\d.]+)([\w%]+)?$', size_str)
if matches is None: if matches is None:
# "<cylinder>,<head>,<sector>" format # "<cylinder>,<head>,<sector>" format
matches = re.search(r'^(\d+),(\d+),(\d+)$', size_str) matches = re.search(r'^(\d+),(\d+),(\d+)$', size_str)

View file

@ -198,6 +198,17 @@ class TestParted(ModuleTestCase):
with patch('ansible_collections.community.general.plugins.modules.system.parted.get_device_info', return_value=parted_dict1): with patch('ansible_collections.community.general.plugins.modules.system.parted.get_device_info', return_value=parted_dict1):
self.execute_module(changed=True, script='unit KiB mkpart primary 0% 1GiB') self.execute_module(changed=True, script='unit KiB mkpart primary 0% 1GiB')
def test_create_new_partition_minus_1G(self):
set_module_args({
'device': '/dev/sdb',
'number': 4,
'state': 'present',
'fs_type': 'ext2',
'part_start': '-1GiB',
})
with patch('ansible_collections.community.general.plugins.modules.system.parted.get_device_info', return_value=parted_dict1):
self.execute_module(changed=True, script='unit KiB mkpart primary ext2 -1GiB 100%')
def test_remove_partition_number_1(self): def test_remove_partition_number_1(self):
set_module_args({ set_module_args({
'device': '/dev/sdb', 'device': '/dev/sdb',