From 736fde9e784587cb2ef344bbf31d876eafea7741 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Mon, 30 Aug 2021 20:51:19 -0700 Subject: [PATCH] add support for newer versions of sfdisk --- jc/parsers/sfdisk.py | 127 +++++++++++++++++++++------ tests/fixtures/centos-8/sfdisk-F.out | 9 ++ tests/fixtures/centos-8/sfdisk-l.out | 24 +++++ tests/fixtures/debian10/sfdisk-F.out | 3 + tests/fixtures/debian10/sfdisk-l.out | 12 +++ 5 files changed, 147 insertions(+), 28 deletions(-) create mode 100644 tests/fixtures/centos-8/sfdisk-F.out create mode 100644 tests/fixtures/centos-8/sfdisk-l.out create mode 100644 tests/fixtures/debian10/sfdisk-F.out create mode 100644 tests/fixtures/debian10/sfdisk-l.out diff --git a/jc/parsers/sfdisk.py b/jc/parsers/sfdisk.py index 873901ac..edd0c1c0 100644 --- a/jc/parsers/sfdisk.py +++ b/jc/parsers/sfdisk.py @@ -2,11 +2,12 @@ Supports the following `sfdisk` options: - `-l` -- `-d` -- `-uM` -- `-uC` -- `-uS` -- `-uB` +- `-F` +- `-d` (deprecated - only for older versions of util-linux) +- `-uM` (deprecated - only for older versions of util-linux) +- `-uC` (deprecated - only for older versions of util-linux) +- `-uS` (deprecated - only for older versions of util-linux) +- `-uB` (deprecated - only for older versions of util-linux) Usage (cli): @@ -25,24 +26,38 @@ Schema: [ { - "disk": string, - "cylinders": integer, - "heads": integer, - "sectors_per_track": integer, - "units": string, + "disk": string, + "disk_size": string, + "free_disk_size": string, + "bytes": integer, + "free_bytes": integer, + "sectors": integer, + "free_sectors": integer, + "cylinders": integer, + "heads": integer, + "sectors_per_track": integer, + "units": string, + "logical_sector_size": integer, + "physical_sector_size": integer, + "min_io_size": integer, + "optimal_io_size": integer, + "disk_label_type": string, + "disk_identifier": string, + "disk_model": string, "partitions": [ { - "device": string, - "boot": boolean, - "start": integer, - "end": integer, - "size": integer, - "cyls": integer, - "mib": integer, - "blocks": integer, - "sectors": integer, - "id": string, - "system": string + "device": string, + "boot": boolean, + "start": integer, + "end": integer, + "size": integer, + "cyls": integer, + "mib": integer, + "blocks": integer, + "sectors": integer, + "id": string, + "system": string, + "type": string } ] } @@ -186,7 +201,7 @@ import jc.parsers.universal class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.0' + version = '1.1' description = '`sfdisk` command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -210,7 +225,8 @@ def _process(proc_data): List of Dictionaries. Structured to conform to the schema. """ int_list = ['cylinders', 'heads', 'sectors_per_track', 'start', 'end', 'size', 'cyls', 'mib', - 'blocks', 'sectors'] + 'blocks', 'sectors', 'bytes', 'logical_sector_size', 'physical_sector_size', + 'min_io_size', 'optimal_io_size', 'free_bytes', 'free_sectors'] bool_list = ['boot'] for entry in proc_data: @@ -255,6 +271,8 @@ def parse(data, raw=False, quiet=False): if jc.utils.has_data(data): for line in data.splitlines(): + + # deprecated - only for older versions of util-linux if line.startswith('# partition table of'): if item: raw_output.append(item) @@ -265,6 +283,7 @@ def parse(data, raw=False, quiet=False): item['disk'] = line.split()[4] continue + # deprecated - only for older versions of util-linux if option == 'd': if line.startswith('unit: '): item['units'] = line.split()[1] @@ -284,23 +303,75 @@ def parse(data, raw=False, quiet=False): continue else: - if line.startswith('Disk '): + # older versions of util-linux + # Disk /dev/sda: 2610 cylinders, 255 heads, 63 sectors/track + if line.startswith('Disk ') and 'sectors/track' in line: if item: raw_output.append(item) item = {} partitions = [] line = line.replace(':', '').replace(',', '') - item['disk'] = line.split()[1] - item['cylinders'] = line.split()[2] - item['heads'] = line.split()[4] - item['sectors_per_track'] = line.split()[6] + fields = line.split() + item['disk'] = fields[1] + item['cylinders'] = fields[2] + item['heads'] = fields[4] + item['sectors_per_track'] = fields[6] + continue + + # util-linux v2.32.0+ (?) + # Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors + if line.startswith('Disk ') and line.endswith('sectors'): + if item: + raw_output.append(item) + + item = {} + partitions = [] + line = line.replace(':', '').replace(',', '') + fields = line.split() + item['disk'] = fields[1] + item['disk_size'] = ' '.join(fields[2:4]) + item['bytes'] = fields[4] + item['sectors'] = fields[6] + continue + + if line.startswith('Disk model: '): + item['disk_model'] = line.split(':', maxsplit=1)[1].strip() + continue + + if line.startswith('Sector size (logical/physical)'): + fields = line.split() + item['logical_sector_size'] = fields[3] + item['physical_sector_size'] = fields[6] + continue + + if line.startswith('I/O size (minimum/optimal)'): + fields = line.split() + item['min_io_size'] = fields[3] + item['optimal_io_size'] = fields[6] + continue + + if line.startswith('Disklabel type'): + item['disk_label_type'] = line.split(':', maxsplit=1)[1].strip() + continue + + if line.startswith('Disk identifier'): + item['disk_identifier'] = line.split(':', maxsplit=1)[1].strip() continue if line.startswith('Units: '): item['units'] = line.split(':')[1].strip() continue + if line.startswith('Unpartitioned space'): + line = line.replace(':', '').replace(',', '') + fields = line.split() + item['disk'] = fields[2] + item['free_disk_size'] = ' '.join(fields[3:5]) + item['free_bytes'] = fields[5] + item['free_sectors'] = fields[7] + continue + if 'Device' in line and 'Boot' in line and 'Start' in line and 'End' in line: section = 'partitions' partitions.append(line.lower().replace('#', ' ')) diff --git a/tests/fixtures/centos-8/sfdisk-F.out b/tests/fixtures/centos-8/sfdisk-F.out new file mode 100644 index 00000000..f6936699 --- /dev/null +++ b/tests/fixtures/centos-8/sfdisk-F.out @@ -0,0 +1,9 @@ +Unpartitioned space /dev/sda: 0 B, 0 bytes, 0 sectors +Units: sectors of 1 * 512 = 512 bytes +Sector size (logical/physical): 512 bytes / 512 bytes + + + + + + diff --git a/tests/fixtures/centos-8/sfdisk-l.out b/tests/fixtures/centos-8/sfdisk-l.out new file mode 100644 index 00000000..97fd27fa --- /dev/null +++ b/tests/fixtures/centos-8/sfdisk-l.out @@ -0,0 +1,24 @@ +Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors +Units: sectors of 1 * 512 = 512 bytes +Sector size (logical/physical): 512 bytes / 512 bytes +I/O size (minimum/optimal): 512 bytes / 512 bytes +Disklabel type: dos +Disk identifier: 0x94988ac4 + +Device Boot Start End Sectors Size Id Type +/dev/sda1 * 2048 2099199 2097152 1G 83 Linux +/dev/sda2 2099200 41943039 39843840 19G 8e Linux LVM + + + + +Disk /dev/mapper/cl-root: 17 GiB, 18249416704 bytes, 35643392 sectors +Units: sectors of 1 * 512 = 512 bytes +Sector size (logical/physical): 512 bytes / 512 bytes +I/O size (minimum/optimal): 512 bytes / 512 bytes + + +Disk /dev/mapper/cl-swap: 2 GiB, 2147483648 bytes, 4194304 sectors +Units: sectors of 1 * 512 = 512 bytes +Sector size (logical/physical): 512 bytes / 512 bytes +I/O size (minimum/optimal): 512 bytes / 512 bytes diff --git a/tests/fixtures/debian10/sfdisk-F.out b/tests/fixtures/debian10/sfdisk-F.out new file mode 100644 index 00000000..ab69e34a --- /dev/null +++ b/tests/fixtures/debian10/sfdisk-F.out @@ -0,0 +1,3 @@ +Unpartitioned space /dev/sda: 0 B, 0 bytes, 0 sectors +Units: sectors of 1 * 512 = 512 bytes +Sector size (logical/physical): 512 bytes / 512 bytes diff --git a/tests/fixtures/debian10/sfdisk-l.out b/tests/fixtures/debian10/sfdisk-l.out new file mode 100644 index 00000000..630d8e1f --- /dev/null +++ b/tests/fixtures/debian10/sfdisk-l.out @@ -0,0 +1,12 @@ +Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors +Disk model: VMware Virtual S +Units: sectors of 1 * 512 = 512 bytes +Sector size (logical/physical): 512 bytes / 512 bytes +I/O size (minimum/optimal): 512 bytes / 512 bytes +Disklabel type: dos +Disk identifier: 0x3719ef0f + +Device Boot Start End Sectors Size Id Type +/dev/sda1 * 2048 39942143 39940096 19G 83 Linux +/dev/sda2 39944190 41940991 1996802 975M 5 Extended +/dev/sda5 39944192 41940991 1996800 975M 82 Linux swap / Solaris