udiskie: Merge into storage module

udiskie is now an essential module that will be installed along with storage.

Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Joseph Nuthalapati authored and James Valleroy committed 2018-09-24 18:04:50 -04:00
1 parent f172925d9d
commit a307476634
11 files changed
+163 -304

No files matched your search

+47 -7
View File
@@ -17,21 +17,32 @@
"""
FreedomBox app to manage storage.
"""
import json
import logging
import subprocess
from django.utils.translation import ugettext_lazy as _
from plinth import actions
from plinth import service as service_module
from plinth import action_utils, actions, cfg
from plinth.menu import main_menu
from plinth.utils import format_lazy
version = 2
version = 3
name = _('Storage')
description = []
managed_services = ['freedombox-udiskie']
managed_packages = ['udiskie', 'gir1.2-udisks-2.0']
description = [
format_lazy(
_('This module allows you to manage storage media attached to your '
'{box_name}. You can view the storage media currently in use, mount '
'and unmount removable media, expand the root partition etc.'),
box_name=_(cfg.box_name))
]
service = None
@@ -81,8 +92,8 @@ def _get_disks_from_df():
disks = []
for line in output.splitlines()[1:]:
parts = line.split(maxsplit=6)
keys = ('device', 'file_system_type', 'size', 'used',
'free', 'percent_used', 'mount_point')
keys = ('device', 'file_system_type', 'size', 'used', 'free',
'percent_used', 'mount_point')
disk = dict(zip(keys, parts))
disk['percent_used'] = int(disk['percent_used'].rstrip('%'))
disk['size'] = int(disk['size'])
@@ -168,8 +179,37 @@ def format_bytes(size):
return _('{disk_size:.1f} TiB').format(disk_size=size)
def is_running():
"""Return whether the service is running."""
return action_utils.service_is_running('freedombox-udiskie')
def is_enabled():
"""Return whether the module is enabled."""
return action_utils.service_is_enabled('freedombox-udiskie')
def enable():
"""Enable the module."""
actions.superuser_run('udiskie', ['enable'])
def disable():
"""Disable the module."""
actions.superuser_run('udiskie', ['disable'])
def setup(helper, old_version=None):
"""Expand root parition on first setup."""
"""Install and configure the module."""
helper.install(managed_packages, skip_recommends=True)
helper.call('post', actions.superuser_run, 'udiskie', ['enable'])
global service
if service is None:
service = service_module.Service(
managed_services[0], name, ports=[], is_external=True,
is_enabled=is_enabled, enable=enable, disable=disable,
is_running=is_running)
helper.call('post', service.notify_enabled, None, True)
disks = get_disks()
root_device = get_root_device(disks)
if is_expandable(root_device):
+67 -10
View File
@@ -36,6 +36,12 @@
<h2>{{ title }}</h2>
{% endblock %}
{% block description %}
{% for paragraph in description %}
<p>{{ paragraph|safe }}</p>
{% endfor %}
{% endblock %}
{% if manual_page %}
<p class="manual-page">
<a href="{% url 'help:manual-page' manual_page %}">
@@ -44,7 +50,7 @@
</p>
{% endif %}
<p>{% trans "The following disks are in use:" %}</p>
<p>{% trans "The following storage devices are in use:" %}</p>
<table class="table table-bordered table-condensed table-striped">
<thead>
@@ -66,17 +72,17 @@
{% if disk.percent_used < 75 %}
<div class="progress-bar progress-bar-striped progress-bar-success"
{% elif disk.percent_used < 90 %}
<div class="progress-bar progress-bar-striped progress-bar-warning"
<div class="progress-bar progress-bar-striped progress-bar-warning"
{% else %}
<div class="progress-bar progress-bar-striped progress-bar-danger"
<div class="progress-bar progress-bar-striped progress-bar-danger"
{% endif %}
role="progressbar" aria-valuenow="disk.percent_used"
aria-valuemin="0" aria-valuemax="100"
style="width: {{ disk.percent_used }}%;">
{{ disk.percent_used }}%
</div>
</div>
<div>{{ disk.used_str }} / {{ disk.size_str }}</div>
role="progressbar" aria-valuenow="disk.percent_used"
aria-valuemin="0" aria-valuemax="100"
style="width: {{ disk.percent_used }}%;">
{{ disk.percent_used }}%
</div>
</div>
<div>{{ disk.used_str }} / {{ disk.size_str }}</div>
</td>
</tr>
{% endfor %}
@@ -100,4 +106,55 @@
</p>
{% endif %}
{% block status %}
{{ block.super }}
<h3>{% trans "Removable Devices" %}</h3>
{% if not devices %}
<p>
{% blocktrans trimmed %}
There are no additional storage devices attached.
{% endblocktrans %}
</p>
{% else %}
<table class="table table-bordered table-condensed table-striped">
<thead>
<tr>
<th>{% trans "Device" %}</th>
<th>{% trans "Label" %}</th>
<th>{% trans "Mount Point" %}</th>
<th>{% trans "Type" %}</th>
<th>{% trans "Size" %}</th>
<th>{% trans "Actions" %}</th>
</tr>
</thead>
<tbody>
{% for device in devices %}
<tr>
<td>{{ device.device }}</td>
<td>{{ device.label }}</td>
<td>{{ device.mount_points|join:', ' }}</td>
<td>{{ device.filesystem_type }}</td>
<td>{{ device.size }}</td>
<td>
{% if device.mount_points %}
<form class="form" method="post"
action="{% url 'storage:eject' device.device|urlencode:"" %}">
{% csrf_token %}
<button type="submit"
class="btn btn-sm btn-default glyphicon glyphicon-eject">
</button>
</form>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% endblock %}
{% endblock %}
+175
View File
@@ -0,0 +1,175 @@
#
# This file is part of FreedomBox.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Library for interacting with udisks2 D-Bus service.
"""
from django.utils.translation import ugettext as _
from plinth import utils
from plinth.modules.storage import format_bytes
def _get_options():
"""Return the common options used for udisks2 operations."""
glib = utils.import_from_gi('GLib', '2.0')
options = glib.Variant(
'a{sv}', {'auth.no_user_interaction': glib.Variant('b', True)})
return options
def list_devices():
"""List devices that can be ejected."""
udisks = utils.import_from_gi('UDisks', '2.0')
client = udisks.Client.new_sync()
object_manager = client.get_object_manager()
block = None
devices = []
for obj in object_manager.get_objects():
if not obj.get_block():
continue
block = obj.get_block()
if block.props.id_usage != 'filesystem' or \
block.props.hint_system:
continue
device_name = block.props.device
if not device_name:
continue
device = {
'device': block.props.device,
'label': block.props.id_label,
'size': format_bytes(block.props.size),
'filesystem_type': block.props.id_type
}
try:
device['mount_points'] = obj.get_filesystem().props.mount_points
except Exception:
pass
devices.append(device)
return devices
def eject_drive_of_device(device_path):
"""Eject a device after unmounting all of its partitions.
Return the details (model, vendor) of drives ejected.
"""
udisks = utils.import_from_gi('UDisks', '2.0')
client = udisks.Client.new_sync()
object_manager = client.get_object_manager()
found_objects = [
obj for obj in object_manager.get_objects()
if obj.get_block() and obj.get_block().props.device == device_path
]
if not found_objects:
raise ValueError(
_('No such device - {device_path}').format(
device_path=device_path))
obj = found_objects[0]
# Unmount filesystems
block_device = obj.get_block()
drive_object_path = block_device.props.drive
if drive_object_path != '/':
umount_all_filesystems_of_drive(drive_object_path)
else:
# Block device has not associated drive
umount_filesystem(obj.get_filesystem())
# Eject the drive
drive = client.get_drive_for_block(block_device)
if drive:
drive.call_eject_sync(_get_options(), None)
return {
'vendor': drive.props.vendor,
'model': drive.props.model,
}
return None
def umount_filesystem(filesystem):
"""Unmount a filesystem """
if filesystem and filesystem.props.mount_points:
filesystem.call_unmount_sync(_get_options())
def umount_all_filesystems_of_drive(drive_object_path):
"""Unmount all filesystems on block devices of a drive."""
udisks = utils.import_from_gi('UDisks', '2.0')
client = udisks.Client.new_sync()
object_manager = client.get_object_manager()
for obj in object_manager.get_objects():
block_device = obj.get_block()
if not block_device or block_device.props.drive != drive_object_path:
continue
umount_filesystem(obj.get_filesystem())
def get_error_message(error):
"""Return an error message given an exception."""
udisks = utils.import_from_gi('UDisks', '2.0')
if error.matches(udisks.Error.quark(), udisks.Error.FAILED):
message = _('The operation failed.')
elif error.matches(udisks.Error.quark(), udisks.Error.CANCELLED):
message = _('The operation was cancelled.')
elif error.matches(udisks.Error.quark(), udisks.Error.ALREADY_UNMOUNTING):
message = _('The device is already unmounting.')
elif error.matches(udisks.Error.quark(), udisks.Error.NOT_SUPPORTED):
message = _('The operation is not supported due to '
'missing driver/tool support.')
elif error.matches(udisks.Error.quark(), udisks.Error.TIMED_OUT):
message = _('The operation timed out.')
elif error.matches(udisks.Error.quark(), udisks.Error.WOULD_WAKEUP):
message = _('The operation would wake up a disk that is '
'in a deep-sleep state.')
elif error.matches(udisks.Error.quark(), udisks.Error.DEVICE_BUSY):
message = _('Attempting to unmount a device that is busy.')
elif error.matches(udisks.Error.quark(), udisks.Error.ALREADY_CANCELLED):
message = _('The operation has already been cancelled.')
elif error.matches(udisks.Error.quark(), udisks.Error.NOT_AUTHORIZED) or \
error.matches(udisks.Error.quark(),
udisks.Error.NOT_AUTHORIZED_CAN_OBTAIN) or \
error.matches(udisks.Error.quark(),
udisks.Error.NOT_AUTHORIZED_DISMISSED):
message = _('Not authorized to perform the requested operation.')
elif error.matches(udisks.Error.quark(), udisks.Error.ALREADY_MOUNTED):
message = _('The device is already mounted.')
elif error.matches(udisks.Error.quark(), udisks.Error.NOT_MOUNTED):
message = _('The device is not mounted.')
elif error.matches(udisks.Error.quark(),
udisks.Error.OPTION_NOT_PERMITTED):
message = _('Not permitted to use the requested option.')
elif error.matches(udisks.Error.quark(),
udisks.Error.MOUNTED_BY_OTHER_USER):
message = _('The device is mounted by another user.')
else:
message = error.message
return message
+2 -2
View File
@@ -14,7 +14,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
URLs for the disks module.
"""
@@ -23,8 +22,9 @@ from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^sys/storage/$', views.index, name='index'),
url(r'^sys/storage/expand$', views.expand, name='expand'),
url(r'^sys/storage/eject/(?P<device_path>[\w%]+)/$', views.eject,
name='eject')
]
+44 -3
View File
@@ -19,16 +19,20 @@ Views for storage module.
"""
import logging
import urllib.parse
from django.contrib import messages
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils.translation import ugettext as _
from django.views.decorators.http import require_POST
from plinth.modules import storage
from plinth.utils import format_lazy, is_user_admin
from . import udisks2
logger = logging.getLogger(__name__)
@@ -44,7 +48,9 @@ def index(request):
return TemplateResponse(
request, 'storage.html', {
'title': _('Storage'),
'description': storage.description,
'disks': disks,
'devices': udisks2.list_devices(),
'manual_page': storage.manual_page,
'expandable_root_size': expandable_root_size
})
@@ -73,9 +79,10 @@ def expand_partition(request, device):
try:
storage.expand_partition(device)
except Exception as exception:
messages.error(request,
_('Error expanding partition: {exception}')
.format(exception=exception))
messages.error(
request,
_('Error expanding partition: {exception}')
.format(exception=exception))
else:
messages.success(request, _('Partition expanded successfully.'))
@@ -106,3 +113,37 @@ def warn_about_low_disk_space(request):
messages.error(request, message)
elif percent_used > 75 or free_gib < 2:
messages.warning(request, message)
@require_POST
def eject(request, device_path):
"""Eject a device, given its path.
Device path is quoted with slashes written as %2F.
"""
device_path = urllib.parse.unquote(device_path)
try:
drive = udisks2.eject_drive_of_device(device_path)
if drive:
messages.success(
request,
_('{drive_vendor} {drive_model} can be safely unplugged.')
.format(drive_vendor=drive['vendor'],
drive_model=drive['model']))
else:
messages.success(request, _('Device can be safely unplugged.'))
except Exception as exception:
try:
message = udisks2.get_error_message(exception)
except AttributeError:
message = str(exception)
logger.exception('Error ejecting device - %s', message)
messages.error(
request,
_('Error ejecting device: {error_message}').format(
error_message=message))
return redirect(reverse('storage:index'))