mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-09-19 04:59:01 +00:00
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:
parent
6b5698f673
commit
89146f64ce
@ -48,7 +48,23 @@ def init():
|
|||||||
|
|
||||||
|
|
||||||
def get_disks():
|
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',
|
command = ['df', '--exclude-type=tmpfs', '--exclude-type=devtmpfs',
|
||||||
'--output=source,target,fstype,size,used,pcent',
|
'--output=source,target,fstype,size,used,pcent',
|
||||||
'--human-readable']
|
'--human-readable']
|
||||||
@ -72,9 +88,27 @@ def get_disks():
|
|||||||
return 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):
|
def get_root_device(disks):
|
||||||
"""Return the root partition's device from list of partitions."""
|
"""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:
|
try:
|
||||||
return devices[0]
|
return devices[0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
|
|||||||
@ -47,7 +47,7 @@
|
|||||||
<tbody>
|
<tbody>
|
||||||
{% for disk in disks %}
|
{% for disk in disks %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ disk.device }}</td>
|
<td>{{ disk.dev_kname }}</td>
|
||||||
<td>{{ disk.mount_point }}</td>
|
<td>{{ disk.mount_point }}</td>
|
||||||
<td>{{ disk.file_system_type }}</td>
|
<td>{{ disk.file_system_type }}</td>
|
||||||
<td>
|
<td>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user