storage: Use UDisks information as primary source

Rename get_disks() to get_mounts() and use it in for backups and samba shares.

Create a new get_disks() similar to get_mounts() but use df information only for
showing free space. This inverts the importance of 'df' and UDisks. Use UDisks
as primary source of information for showing list of disks and then use df to
fill in the free space information.

- Retrieve all the mount points of a device and return them as part of
get_disks() in an extra 'mount_points' property.

- For storage listing, this fixes showing up of /.snapshots as separate disk and
showing of vboxsf, network mounts etc. Only shows mounts that are related to
block devices.

- Update various uses of get_disks() within storage module to use
'mounts_points' instead of 'mount_point' to be accurate in cases where there are
multiple mounts for a given device. Use get_mounts() where appropriate instead.

- Display all the mount points against a devices in multiple lines.

- Also show devices that are not currently mounted.

Tests performed:

- Filling up a disk shows a disk space warning properly. Warning contains the
  free disk space correctly.

- Calling get_root_device(get_disks()) return the correct root device.

- In Deluge, the download directory contains a list of all samba current shares.
  If a disk with samba share is unmouted, it does not show up in the list.

- In the Samba app page, all disks are shown properly. Root disk is shown as
  'disk'. All other mount points such as .snapshots and /vagrant also show up.

- In the Samba app page, unavailable shares list shows up when a disk with a
  share is unmounted.

- Upload a backup, warning on the form shows available disk space properly.

- When adding a backup location. The list includes all mount points. Duplicated
  mount points are not shown. Root disk is not shown in the list. When all the
  disks are used up for backup location, a warning that no additional disks are
  available is shown.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2020-05-29 17:38:31 -07:00 committed by James Valleroy
parent 955dfea866
commit 10b46f1968
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
7 changed files with 46 additions and 14 deletions

View File

@ -15,7 +15,7 @@ from django.core.validators import (FileExtensionValidator,
from django.utils.translation import ugettext
from django.utils.translation import ugettext_lazy as _
from plinth.modules.storage import get_disks
from plinth.modules.storage import get_mounts
from plinth.utils import format_lazy
from . import api, split_path
@ -158,7 +158,7 @@ def get_disk_choices():
if repository.storage_type == 'disk'
]
choices = []
for device in get_disks():
for device in get_mounts():
if device['mount_point'] == '/':
continue

View File

@ -88,11 +88,11 @@ def make_request(request, view, **kwargs):
def test_samba_shares_view(rf):
"""Test that a share list has correct view data."""
with patch('plinth.views.AppView.get_context_data',
return_value={'is_enabled': True}), patch(
'plinth.modules.samba.get_users',
return_value=USERS), patch(
'plinth.modules.storage.get_disks', return_value=DISKS):
with patch('plinth.views.AppView.get_context_data', return_value={
'is_enabled': True
}), patch('plinth.modules.samba.get_users',
return_value=USERS), patch('plinth.modules.storage.get_mounts',
return_value=DISKS):
view = views.SambaAppView.as_view()
response, _ = make_request(rf.get(''), view)

View File

@ -28,7 +28,7 @@ class SambaAppView(views.AppView):
def get_context_data(self, *args, **kwargs):
"""Return template context data."""
context = super().get_context_data(*args, **kwargs)
disks = storage.get_disks()
disks = storage.get_mounts()
shares = samba.get_shares()
for disk in disks:

View File

@ -75,7 +75,36 @@ def init():
def get_disks():
"""Returns list of disks by combining information from df and udisks."""
"""Returns list of disks and their free space.
The primary source of information is UDisks' list of block devices.
Information from df is used for free space available.
"""
disks_from_df = _get_disks_from_df()
disks = udisks2.get_disks()
for disk in disks:
disk['size'] = format_bytes(disk['size'])
# Add info from df to the disks from udisks based on mount point.
for disk in disks:
for disk_from_df in disks_from_df:
if disk_from_df['mount_point'] in disk['mount_points']:
disk['mount_point'] = disk_from_df['mount_point']
for key in ('percent_used', 'size', 'used', 'free', 'size_str',
'used_str', 'free_str'):
disk[key] = disk_from_df[key]
return sorted(disks, key=lambda disk: disk['device'])
def get_mounts():
"""Return list of mounts by combining information from df and UDisks.
The primary source of information is the df command. Information from
UDisks is used for labels.
"""
disks = _get_disks_from_df()
disks_from_udisks = udisks2.get_disks()
for disk in disks_from_udisks:
@ -130,7 +159,7 @@ def get_filesystem_type(mount_point='/'):
def get_disk_info(mount_point):
"""Get information about the free space of a drive"""
disks = get_disks()
list_root = [disk for disk in disks if disk['mount_point'] == mount_point]
list_root = [disk for disk in disks if mount_point in disk['mount_points']]
if not list_root:
raise PlinthError('Mount point {} not found.'.format(mount_point))
@ -147,8 +176,9 @@ def get_disk_info(mount_point):
def get_root_device(disks):
"""Return the root partition's device from list of partitions."""
for disk in disks:
if disk['mount_point'] == '/':
if '/' in disk['mount_points']:
return disk['device']
return None

View File

@ -21,7 +21,7 @@ def get_available_samba_shares():
samba_shares = json.loads(
actions.superuser_run('samba', ['get-shares']))
if samba_shares:
disks = storage.get_disks()
disks = storage.get_mounts()
for share in samba_shares:
for disk in disks:
if share['mount_point'] == disk['mount_point']:

View File

@ -35,7 +35,7 @@
<tr>
<td>{{ disk.device }}</td>
<td>{{ disk.label|default_if_none:"" }}</td>
<td>{{ disk.mount_point }}</td>
<td>{{ disk.mount_points|join:"<br>" }}</td>
<td>{{ disk.filesystem_type }}</td>
<td>
<div class="progress">

View File

@ -143,13 +143,15 @@ def get_disks():
'label': block.id_label,
'size': block.size,
'filesystem_type': block.id_type,
'is_removable': not block.hint_system
'is_removable': not block.hint_system,
'mount_points': [],
}
try:
file_system = Filesystem(object_)
device['mount_points'] = file_system.mount_points
except Exception:
continue
devices.append(device)
return devices