diff --git a/salt/modules/parted.py b/salt/modules/parted.py index 3de952ddfe..81ed6ab25e 100644 --- a/salt/modules/parted.py +++ b/salt/modules/parted.py @@ -109,13 +109,6 @@ def list_(device, unit=None): salt '*' partition.list /dev/sda unit=s salt '*' partition.list /dev/sda unit=kB ''' - if device: - dev = device.replace('/dev/', '') - if dev not in os.listdir('/dev'): - raise CommandExecutionError( - 'Invalid device passed to partition.part_list' - ) - if unit: if unit not in VALID_UNITS: raise CommandExecutionError( @@ -129,37 +122,42 @@ def list_(device, unit=None): ret = {'info': {}, 'partitions': {}} mode = 'info' for line in out: - if line.startswith('BYT'): + if line in ('BYT;', 'CHS;', 'CYL;'): continue - comps = line.replace(';', '').split(':') + cols = line.replace(';', '').split(':') if mode == 'info': - if 7 <= len(comps) <= 8: + if 7 <= len(cols) <= 8: ret['info'] = { - 'disk': comps[0], - 'size': comps[1], - 'interface': comps[2], - 'logical sector': comps[3], - 'physical sector': comps[4], - 'partition table': comps[5], - 'model': comps[6]} - try: - ret['info']['disk flags'] = comps[7] - except IndexError: + 'disk': cols[0], + 'size': cols[1], + 'interface': cols[2], + 'logical sector': cols[3], + 'physical sector': cols[4], + 'partition table': cols[5], + 'model': cols[6]} + if len(cols) == 8: + ret['info']['disk flags'] = cols[7] # Older parted (2.x) doesn't show disk flags in the 'print' # output, and will return a 7-column output for the info # line. In these cases we just leave this field out of the # return dict. - pass mode = 'partitions' + else: + raise CommandExecutionError( + 'Problem encountered while parsing output from parted') else: - ret['partitions'][comps[0]] = { - 'number': comps[0], - 'start': comps[1], - 'end': comps[2], - 'size': comps[3], - 'type': comps[4], - 'file system': comps[5], - 'flags': comps[6]} + if len(cols) == 7: + ret['partitions'][cols[0]] = { + 'number': cols[0], + 'start': cols[1], + 'end': cols[2], + 'size': cols[3], + 'type': cols[4], + 'file system': cols[5], + 'flags': cols[6]} + else: + raise CommandExecutionError( + 'Problem encountered while parsing output from parted') return ret @@ -176,12 +174,6 @@ def align_check(device, part_type, partition): salt '*' partition.align_check /dev/sda minimal 1 ''' - dev = device.replace('/dev/', '') - if dev not in os.listdir('/dev'): - raise CommandExecutionError( - 'Invalid device passed to partition.align_check' - ) - if part_type not in set(['minimal', 'optimal']): raise CommandExecutionError( 'Invalid part_type passed to partition.align_check' @@ -213,10 +205,6 @@ def check(device, minor): salt '*' partition.check 1 ''' - dev = device.replace('/dev/', '') - if dev not in os.listdir('/dev'): - raise CommandExecutionError('Invalid device passed to partition.check') - try: int(minor) except Exception: @@ -243,10 +231,6 @@ def cp(device, from_minor, to_minor): # pylint: disable=C0103 salt '*' partition.cp /dev/sda 2 3 ''' - dev = device.replace('/dev/', '') - if dev not in os.listdir('/dev'): - raise CommandExecutionError('Invalid device passed to partition.cp') - try: int(from_minor) int(to_minor) @@ -279,10 +263,6 @@ def get_id(device, minor): salt '*' partition.get_id /dev/sda 1 ''' - dev = device.replace('/dev/', '') - if dev not in os.listdir('/dev'): - raise CommandExecutionError('Invalid device passed to partition.get_id') - try: int(minor) except Exception: @@ -314,10 +294,6 @@ def set_id(device, minor, system_id): salt '*' partition.set_id /dev/sda 1 83 ''' - dev = device.replace('/dev/', '') - if dev not in os.listdir('/dev'): - raise CommandExecutionError('Invalid device passed to partition.set_id') - try: int(minor) except Exception: @@ -371,10 +347,6 @@ def mkfs(device, fs_type): salt '*' partition.mkfs /dev/sda2 fat32 ''' - dev = device.replace('/dev/', '') - if dev not in os.listdir('/dev'): - raise CommandExecutionError('Invalid device passed to partition.mkfs') - if fs_type not in set(['ext2', 'fat32', 'fat16', 'linux-swap', 'reiserfs', 'hfs', 'hfs+', 'hfsx', 'NTFS', 'ufs']): raise CommandExecutionError('Invalid fs_type passed to partition.mkfs') @@ -401,12 +373,6 @@ def mklabel(device, label_type): salt '*' partition.mklabel /dev/sda msdos ''' - dev = device.replace('/dev/', '') - if dev not in os.listdir('/dev'): - raise CommandExecutionError( - 'Invalid device passed to partition.mklabel' - ) - if label_type not in set(['aix', 'amiga', 'bsd', 'dvh', 'gpt', 'loop', 'mac', 'msdos', 'pc98', 'sun']): raise CommandExecutionError( @@ -450,12 +416,6 @@ def mkpart(device, part_type, fs_type=None, start=None, end=None): 'partition.mkpart requires a start and an end' ) - dev = device.replace('/dev/', '') - if dev not in os.listdir('/dev'): - raise CommandExecutionError( - 'Invalid device passed to partition.mkpart' - ) - if part_type not in set(['primary', 'logical', 'extended']): raise CommandExecutionError( 'Invalid part_type passed to partition.mkpart' @@ -499,12 +459,6 @@ def mkpartfs(device, part_type, fs_type, start, end): salt '*' partition.mkpartfs /dev/sda logical ext2 440 670 ''' - dev = device.replace('/dev/', '') - if dev not in os.listdir('/dev'): - raise CommandExecutionError( - 'Invalid device passed to partition.mkpartfs' - ) - if part_type not in set(['primary', 'logical', 'extended']): raise CommandExecutionError( 'Invalid part_type passed to partition.mkpartfs' @@ -539,10 +493,6 @@ def name(device, partition, name): salt '*' partition.name /dev/sda 1 'My Documents' ''' - dev = device.replace('/dev/', '') - if dev not in os.listdir('/dev'): - raise CommandExecutionError('Invalid device passed to partition.name') - try: int(partition) except Exception: @@ -576,10 +526,6 @@ def rescue(device, start, end): salt '*' partition.rescue /dev/sda 0 8056 ''' - dev = device.replace('/dev/', '') - if dev not in os.listdir('/dev'): - raise CommandExecutionError('Invalid device passed to partition.rescue') - _validate_partition_boundary(start) _validate_partition_boundary(end) @@ -604,10 +550,6 @@ def resize(device, minor, start, end): salt '*' partition.resize /dev/sda 3 200 850 ''' - dev = device.replace('/dev/', '') - if dev not in os.listdir('/dev'): - raise CommandExecutionError('Invalid device passed to partition.resize') - try: int(minor) except Exception: @@ -638,10 +580,6 @@ def rm(device, minor): # pylint: disable=C0103 salt '*' partition.rm /dev/sda 5 ''' - dev = device.replace('/dev/', '') - if dev not in os.listdir('/dev'): - raise CommandExecutionError('Invalid device passed to partition.rm') - try: int(minor) except Exception: @@ -668,10 +606,6 @@ def set_(device, minor, flag, state): salt '*' partition.set /dev/sda 1 boot on ''' - dev = device.replace('/dev/', '') - if dev not in os.listdir('/dev'): - raise CommandExecutionError('Invalid device passed to partition.set') - try: int(minor) except Exception: @@ -703,10 +637,6 @@ def toggle(device, partition, flag): salt '*' partition.name /dev/sda 1 boot ''' - dev = device.replace('/dev/', '') - if dev not in os.listdir('/dev'): - raise CommandExecutionError('Invalid device passed to partition.toggle') - try: int(partition) except Exception: @@ -756,11 +686,6 @@ def get_block_device(): salt '*' partition.get_block_device ''' - ret = [] - cmd = '/bin/lsblk -n -io KNAME -d -e 1,7,11 -l' + cmd = 'lsblk -n -io KNAME -d -e 1,7,11 -l' devs = __salt__['cmd.run'](cmd).splitlines() - for dev in devs: - if dev not in os.listdir('/dev'): - continue - ret.append(dev) - return ret + return devs