From 82475522f9fd6faabdfdc68f6fdf2fa7088128e6 Mon Sep 17 00:00:00 2001 From: Johannes Keyser Date: Tue, 29 Aug 2017 01:13:58 +0200 Subject: [PATCH] disks: Add low disk space warning (Closes: #985) Reviewed-by: Sunil Mohan Adapa --- plinth/modules/disks/views.py | 50 +++++++++++++++++++++++++++++++++++ plinth/views.py | 3 +++ 2 files changed, 53 insertions(+) diff --git a/plinth/modules/disks/views.py b/plinth/modules/disks/views.py index 9a91b35eb..f169eed30 100644 --- a/plinth/modules/disks/views.py +++ b/plinth/modules/disks/views.py @@ -68,6 +68,56 @@ def expand_partition(request, device): messages.success(request, _('Partition expanded successfully.')) +def warn_about_insufficient_root_space(request): + """Warn about insufficient space on root partition.""" + disks = disks_module.get_disks() + list_root = [disk for disk in disks if disk['mountpoint'] == '/'] + perc_used = list_root[0]['percentage_used'] if list_root else -1 + size_str = list_root[0]['size'] if list_root else '-1' + size_Bytes = _interpret_size_string(size_str) + free_Bytes = size_Bytes * (100 - perc_used) / 100 + free_GiB = free_Bytes / 1024 ** 3 + free_str = _format_bytes(free_Bytes) + + if perc_used < 0 or free_GiB < 0: + # FIXME: Log read error. + return + + msg_str = _('Warning: Low disk space on root partition ({percent_used}%' + ' used, {free_space} free). FIXME: Link to disk module.').format( + percent_used=perc_used, free_space=free_str) + + # FIXME: Match with existing coloring in disk module. + if perc_used > 90 or free_GiB < 3: + messages.error(request, msg_str) + + elif perc_used > 80 or free_GiB < 2: + messages.warning(request, msg_str) + + +def _interpret_size_string(size_str): + """Convert size string to number of bytes.""" + if size_str is None or not size_str: + return -1 + + if size_str[-1] in '-10123456789': + return float(size_str[:-1]) + + if size_str[-1] == 'K': + return float(size_str[:-1]) * 1024 + + if size_str[-1] == 'M': + return float(size_str[:-1]) * 1024 ** 2 + + if size_str[-1] == 'G': + return float(size_str[:-1]) * 1024 ** 3 + + if size_str[-1] == 'T': + return float(size_str[:-1]) * 1024 ** 4 + + return -1 + + def _format_bytes(size): """Return human readable disk size from bytes.""" if not size: diff --git a/plinth/views.py b/plinth/views.py index 4ce9747ba..dcc665294 100644 --- a/plinth/views.py +++ b/plinth/views.py @@ -31,6 +31,7 @@ import time from . import forms, frontpage import plinth from plinth import actions +from plinth.modules.disks import views as disk_views @public @@ -45,6 +46,8 @@ def index(request): details_label = frontpage.shortcuts[selection]['label'] configure_url = frontpage.shortcuts[selection]['configure_url'] + disk_views.warn_about_insufficient_root_space(request) + return TemplateResponse(request, 'index.html', {'title': _('FreedomBox'), 'shortcuts': shortcuts,