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

Add disk_set and disk_toggle functions, and update valid partition flags #50834

Merged
merged 2 commits into from
Dec 12, 2018
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
90 changes: 84 additions & 6 deletions salt/modules/parted.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
VALID_UNITS = set(['s', 'B', 'kB', 'MB', 'MiB', 'GB', 'GiB', 'TB', 'TiB', '%',
'cyl', 'chs', 'compact'])

VALID_DISK_FLAGS = set(['cylinder_alignment', 'pmbr_boot',
'implicit_partition_table'])

VALID_PARTITION_FLAGS = set(['boot', 'root', 'swap', 'hidden', 'raid',
'lvm', 'lba', 'hp-service', 'palo',
'prep', 'msftres', 'bios_grub', 'atvrecv',
'diag', 'legacy_boot', 'msftdata', 'irst',
'esp', 'type'])


def __virtual__():
'''
Expand Down Expand Up @@ -641,8 +650,25 @@ def set_(device, minor, flag, state):
:ref:`YAML Idiosyncrasies <yaml-idiosyncrasies>`). Some or all of these
flags will be available, depending on what disk label you are using.

Valid flags are: bios_grub, legacy_boot, boot, lba, root, swap, hidden, raid,
LVM, PALO, PREP, DIAG
Valid flags are:
* boot
* root
* swap
* hidden
* raid
* lvm
* lba
* hp-service
* palo
* prep
* msftres
* bios_grub
* atvrecv
* diag
* legacy_boot
* msftdata
* irst
* esp type

CLI Example:

Expand All @@ -659,8 +685,7 @@ def set_(device, minor, flag, state):
'Invalid minor number passed to partition.set'
)

if flag not in set(['bios_grub', 'legacy_boot', 'boot', 'lba', 'root',
'swap', 'hidden', 'raid', 'LVM', 'PALO', 'PREP', 'DIAG']):
if flag not in VALID_PARTITION_FLAGS:
raise CommandExecutionError('Invalid flag passed to partition.set')

if state not in set(['on', 'off']):
Expand Down Expand Up @@ -691,15 +716,68 @@ def toggle(device, partition, flag):
'Invalid partition number passed to partition.toggle'
)

if flag not in set(['bios_grub', 'legacy_boot', 'boot', 'lba', 'root',
'swap', 'hidden', 'raid', 'LVM', 'PALO', 'PREP', 'DIAG']):
if flag not in VALID_PARTITION_FLAGS:
raise CommandExecutionError('Invalid flag passed to partition.toggle')

cmd = 'parted -m -s {0} toggle {1} {2}'.format(device, partition, flag)
out = __salt__['cmd.run'](cmd).splitlines()
return out


def disk_set(device, flag, state):
'''
Changes a flag on selected device.

A flag can be either "on" or "off" (make sure to use proper
quoting, see :ref:`YAML Idiosyncrasies
<yaml-idiosyncrasies>`). Some or all of these flags will be
available, depending on what disk label you are using.

Valid flags are:
* cylinder_alignment
* pmbr_boot
* implicit_partition_table

CLI Example:

.. code-block:: bash

salt '*' partition.disk_set /dev/sda pmbr_boot '"on"'
'''
_validate_device(device)

if flag not in VALID_DISK_FLAGS:
raise CommandExecutionError('Invalid flag passed to partition.disk_set')

if state not in set(['on', 'off']):
raise CommandExecutionError('Invalid state passed to partition.disk_set')

cmd = ['parted', '-m', '-s', device, 'disk_set', flag, state]
out = __salt__['cmd.run'](cmd).splitlines()
return out


def disk_toggle(device, flag):
'''
Toggle the state of <flag> on <device>. Valid flags are the same
as the disk_set command.

CLI Example:

.. code-block:: bash

salt '*' partition.disk_toggle /dev/sda pmbr_boot
'''
_validate_device(device)

if flag not in VALID_DISK_FLAGS:
raise CommandExecutionError('Invalid flag passed to partition.disk_toggle')

cmd = ['parted', '-m', '-s', device, 'disk_toggle', flag]
out = __salt__['cmd.run'](cmd).splitlines()
return out


def exists(device=''):
'''
Check to see if the partition exists
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/modules/test_parted.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,20 @@ def test_list__valid_unit_valid_legacy_cmd_output(self):
}
}
self.assertEqual(output, expected)

def test_disk_set(self):
with patch('salt.modules.parted._validate_device', MagicMock()):
self.cmdrun.return_value = ''
output = parted.disk_set('/dev/sda', 'pmbr_boot', 'on')
self.cmdrun.assert_called_once_with(
['parted', '-m', '-s', '/dev/sda', 'disk_set',
'pmbr_boot', 'on'])
assert output == []

def test_disk_toggle(self):
with patch('salt.modules.parted._validate_device', MagicMock()):
self.cmdrun.return_value = ''
output = parted.disk_toggle('/dev/sda', 'pmbr_boot')
self.cmdrun.assert_called_once_with(
['parted', '-m', '-s', '/dev/sda', 'disk_toggle', 'pmbr_boot'])
assert output == []