disks: Use information from lsblk for more accuracy

Closes #795.  On a Raspberry Pi, the root partition was show as being
mounted from /dev/root.  Use information from lsblk to more acurately
indentify the device as /dev/mmcblk0p2.

Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Johannes Keyser 2017-04-16 23:44:37 +02:00 committed by Sunil Mohan Adapa
parent 6b5698f673
commit 89146f64ce
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
2 changed files with 37 additions and 3 deletions

View File

@ -48,7 +48,23 @@ def init():
def get_disks():
"""Return the list of disks and free space available."""
"""Returns list of disks by combining information from df and lsblk."""
disks_from_df = _get_disks_from_df()
disks_from_lsblk = _get_disks_from_lsblk()
# Combine both sources of info dicts into one dict, based on mount point;
# note this discards disks without a (current) mount point.
combined_list = []
for disk_from_df in disks_from_df:
for disk_from_lsblk in disks_from_lsblk:
if disk_from_df['mount_point'] == disk_from_lsblk['mountpoint']:
disk_from_df.update(disk_from_lsblk)
combined_list.append(disk_from_df)
return combined_list
def _get_disks_from_df():
"""Return the list of disks and free space available using 'df'."""
command = ['df', '--exclude-type=tmpfs', '--exclude-type=devtmpfs',
'--output=source,target,fstype,size,used,pcent',
'--human-readable']
@ -72,9 +88,27 @@ def get_disks():
return disks
def _get_disks_from_lsblk():
"""Return the list of disks and other information from 'lsblk'."""
command = ['lsblk', '--json', '--output', 'kname,pkname,mountpoint,type']
try:
process = subprocess.run(command, stdout=subprocess.PIPE, check=True)
except subprocess.CalledProcessError as exception:
logger.exception('Error getting disk information: %s', exception)
return []
output = process.stdout.decode()
disks = json.loads(output)['blockdevices']
for disk in disks:
disk['dev_kname'] = '/dev/{0}'.format(disk['kname'])
return disks
def get_root_device(disks):
"""Return the root partition's device from list of partitions."""
devices = [disk['device'] for disk in disks if disk['mount_point'] == '/']
devices = [disk['dev_kname'] for disk in disks
if disk['mountpoint'] == '/' and disk['type'] == 'part']
try:
return devices[0]
except IndexError:

View File

@ -47,7 +47,7 @@
<tbody>
{% for disk in disks %}
<tr>
<td>{{ disk.device }}</td>
<td>{{ disk.dev_kname }}</td>
<td>{{ disk.mount_point }}</td>
<td>{{ disk.file_system_type }}</td>
<td>