disks: Add low disk space warning (Closes: #985)

Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Johannes Keyser 2017-08-29 01:13:58 +02:00 committed by Sunil Mohan Adapa
parent 1f0a878b2e
commit 82475522f9
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
2 changed files with 53 additions and 0 deletions

View File

@ -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:

View File

@ -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,