Block device listing
Module for processing output of the lsblk command. Different information
is provided by the lsblk command depending upon the options. Parsers
included here are:
LSBlock - Command lsblk
The LSBlock class parses output of the lsblk command with no options.
LSBlockPairs - Command lsblk -P -o [columns...]
The LSBlockPairs class parses output of the lsblk -P -o [columns...]
command.
These classes based on BlockDevices which implements all of the
functionality except the parsing of command specific information.
Information is stored in the attribute self.rows which is a list of
BlockDevice objects.
Each BlockDevice object provides the functionality for one row of data from the
command output. Data in a BlockDevice object is accessible by multiple methods.
For example the NAME field can be accessed in the following four ways:
lsblk_info.rows[0].data['NAME']
lsblk_info.rows[0].NAME
lsblk_info.rows[0].name
lsblk_info.rows[0].get('NAME')
Sample output of the lsblk command looks like:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 9G 0 disk
|-vda1 252:1 0 500M 0 part /boot
`-vda2 252:2 0 8.5G 0 part
|-rhel-root 253:0 0 7.6G 0 lvm /
|-rhel-swap 253:1 0 924M 0 lvm [SWAP]
sda 8:0 0 500G 0 disk
`-sda1 8:1 0 500G 0 part /data
Note the hierarchy demonstrated in the name column. For instance vda1 and
vda2 are children of vda. Likewise, rhel-root and rhel-swap
are children of vda2. This relationship is demonstrated in the
PARENT_NAMES key, which is only present if the row is a child row. For
example PARENT_NAMES value for rhel-root will be ['vda', 'vda2']
meaning that vda2 is the immediate parent and vda is parent of
vda2.
Also note that column names that are not valid Python property names been
changed. For example MAJ:MIN has been changed to MAJ_MIN.
Examples
>>> lsblk_info = shared[LSBlock]
>>> lsblk_info
<insights.parsers.lsblk.LSBlock object at 0x7f1f6a422d50>
>>> lsblk_info.rows
[disk:vda,
part:vda1(/boot),
part:vda2,
lvm:rhel-root(/),
lvm:rhel-swap([SWAP]),
disk:sda,
part:sda1(/data)]
>>> lsblk_info.rows[0]
disk:vda
>>> lsblk_info.rows[0].data
{'READ_ONLY': False, 'NAME': 'vda', 'REMOVABLE': False, 'MAJ_MIN': '252:0',
'TYPE': 'disk', 'SIZE': '9G'}
>>> lsblk_info.rows[0].data['NAME']
'vda'
>>> lsblk_info.rows[0].NAME
'vda'
>>> lsblk_info.rows[0].name
'vda'
>>> lsblk_info.rows[0].data['MAJ_MIN']
'252:0'
>>> lsblk_info.rows[0].MAJ_MIN
'252:0'
>>> lsblk_info.rows[0].maj_min
'252:0'
>>> lsblk_info.rows[0].removable
False
>>> lsblk_info.rows[0].read_only
False
>>> lsblk_info.rows[2].data
{'READ_ONLY': False, 'PARENT_NAMES': ['vda'], 'NAME': 'vda2',
'REMOVABLE': False, 'MAJ_MIN': '252:2', 'TYPE': 'part', 'SIZE': '8.5G'}
>>> lsblk_info.rows[2].parent_names
['vda']
>>> lsblk_info.rows[3].parent_names
['vda', 'vda2']
>>> lsblk_info.device_data['vda'] # Access devices by name
'disk:vda'
>>> lsblk_info.search(NAME='vda2')
[{'READ_ONLY': False, 'PARENT_NAMES': ['vda'], 'NAME': 'vda2',
'REMOVABLE': False, 'MAJ_MIN': '252:2', 'TYPE': 'part', 'SIZE': '8.5G'}]
- class insights.parsers.lsblk.BlockDevice(data)[source]
Bases:
objectClass to contain one line of
lsblkcommand information.Contains all of the fields for a single line of
lsblkoutput. Computed values are the column names except where the column name is an invalid variable name in Python such as MAJ:MIN. Thegetmethod is provided to access any value, including those that are not valid names in Python. All other valid names may be accessed asobj.column_name.
- class insights.parsers.lsblk.BlockDevices(context, extra_bad_lines=None)[source]
Bases:
CommandParserClass to contain all information from
lsblkcommand.Output of the
lsblkcommand is contained in this base class. Data may be accessed via the iterator and each item represents a row of output from the command in dict format.- rows
List of
BlockDeviceobjects for each row of the input. Input column name matches key name except any ‘-’ is replaced with ‘_’ and the following names are changed:Column Name Key Name MAJ:MIN MAJ_MIN RM REMOVABLE RO READD_ONLY
- Type:
list of BlockDevice
- device_data
A dictionary of
BlockDeviceobjects keyed on the ‘NAME’ column (e.g.sdaorrhel-swap)- Type:
dict of BlockDevice
- search(**kwargs)[source]
Returns a list of the block devices (in order) matching the given criteria. Keys are searched for directly - see the
insights.parsers.keyword_search()utility function for more details. If no search parameters are given, no rows are returned. Keys need to be in all upper case, as they appear in the source data.Examples
>>> blockdevs.search(NAME='sda1') [{'NAME': '/dev/sda1', 'TYPE': 'disk', 'SIZE', '80G', ...}] >>> blockdevs.search(TYPE='lvm') [{'NAME': 'volgrp01-root', 'TYPE': 'lvm', 'SIZE', '15G', ...}...]
- Parameters:
**kwargs (dict) -- Dictionary of key-value pairs to search for.
- Returns:
The list of mount points matching the given criteria.
- Return type:
(list)
- class insights.parsers.lsblk.LSBlock(context, extra_bad_lines=None)[source]
Bases:
BlockDevicesParse output of the
lsblkcommand.The specific lsblk commands are
/bin/lsblkand/usr/bin/lsblk. Typical content of thelsblkcommand output looks like:NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 80G 0 disk |-sda1 8:1 0 256M 0 part /boot `-sda2 8:2 0 79.8G 0 part |-volgrp01-root (dm-0) 253:0 0 15G 0 lvm / `-volgrp01-swap (dm-1) 253:1 0 8G 0 lvm [SWAP]
Note
See the discussion of the key
PARENT_NAMESabove.
- class insights.parsers.lsblk.LSBlockPairs(context, extra_bad_lines=None)[source]
Bases:
BlockDevicesParse output of the
lsblk -P -ocommand.lsblkcommand with-P -ooptions provides explicit selection of output columns in keyword=value pairs.The specific lsblk commands are
/bin/lsblk -P -o column_namesand/usr/bin/lsblk -P -o column_names. Typical content of thelsblkcommand output looks like:ALIGNMENT="0" DISC-ALN="0" DISC-GRAN="0B" DISC-MAX="0B" DISC-ZERO="0" FSTYPE="" GROUP="cdrom" KNAME="sr0" LABEL="" LOG-SEC="512" MAJ:MIN="11:0" MIN-IO="512" MODE="brw-rw----" MODEL="DVD+-RW DVD8801 " MOUNTPOINT="" NAME="sr0" OPT-IO="0" OWNER="root" PHY-SEC="512" RA="128" RM="1" RO="0" ROTA="1" RQ-SIZE="128" SCHED="cfq" SIZE="1024M" STATE="running" TYPE="rom" UUID="" ALIGNMENT="0" DISC-ALN="0" DISC-GRAN="0B" DISC-MAX="0B" DISC-ZERO="0" FSTYPE="" GROUP="disk" KNAME="sda" LABEL="" LOG-SEC="512" MAJ:MIN="8:0" MIN-IO="512" MODE="brw-rw----" MODEL="WDC WD1600JS-75N" MOUNTPOINT="" NAME="sda" OPT-IO="0" OWNER="root" PHY-SEC="512" RA="128" RM="0" RO="0" ROTA="1" RQ-SIZE="128" SCHED="cfq" SIZE="149G" STATE="running" TYPE="disk" UUID="" ALIGNMENT="0" DISC-ALN="0" DISC-GRAN="0B" DISC-MAX="0B" DISC-ZERO="0" FSTYPE="ext4" GROUP="disk" KNAME="sda1" LABEL="" LOG-SEC="512" MAJ:MIN="8:1" MIN-IO="512" MODE="brw-rw----" MODEL="" MOUNTPOINT="/boot" NAME="sda1" OPT-IO="0" OWNER="root" PHY-SEC="512" RA="128" RM="0" RO="0" ROTA="1" RQ-SIZE="128" SCHED="cfq" SIZE="500M" STATE="" TYPE="part" UUID="c7c4c016-8b00-4ded-bffb-5cc4719b7d45"
- rows
List of
BlockDeviceobjects for each row of the input. Input column name matches key name except that any ‘-’, ‘:’, or ‘.’ is replaced with ‘_’ and the following names are changed:Column Name Key Name RM removable RO read_only
- Type:
list of BlockDevice
- failed_device_paths
Set of device names that failed to get device path
- Type:
set
Note
PARENT_NAMESis not available as a key because it is not listed in theLsBlockPairsoutput and cannot always be correctly inferred from the other data present.