summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@vyos.io>2024-06-24 19:13:58 +0200
committerGitHub <noreply@github.com>2024-06-24 19:13:58 +0200
commit4f89e4b7a19a58d893e4e89a6e8491f239f1122e (patch)
treef2a0dcbfb241d86006ac0b0b1c16e886bffb0596 /python
parent6fbe91ea8ea639e3933b3e23f99ce32204cebfe0 (diff)
parent7b65f3d819fe682456120b980effed7f5b3e9cf8 (diff)
downloadvyos-1x-4f89e4b7a19a58d893e4e89a6e8491f239f1122e.tar.gz
vyos-1x-4f89e4b7a19a58d893e4e89a6e8491f239f1122e.zip
Merge pull request #3718 from dmbaturin/T6514-system-storage-raw
op-mode: T6514: rework the "show system storage" code to handle live CD systems correctly
Diffstat (limited to 'python')
-rw-r--r--python/vyos/utils/disk.py51
1 files changed, 50 insertions, 1 deletions
diff --git a/python/vyos/utils/disk.py b/python/vyos/utils/disk.py
index ee540b107..d4271ebe1 100644
--- a/python/vyos/utils/disk.py
+++ b/python/vyos/utils/disk.py
@@ -1,4 +1,4 @@
-# Copyright 2023 VyOS maintainers and contributors <maintainers@vyos.io>
+# Copyright 2023-2024 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
@@ -21,3 +21,52 @@ def device_from_id(id):
for device in path.iterdir():
if device.name.endswith(id):
return device.readlink().stem
+
+def get_storage_stats(directory, human_units=True):
+ """ Return basic storage stats for given directory """
+ from re import sub as re_sub
+ from vyos.utils.process import cmd
+ from vyos.utils.convert import human_to_bytes
+
+ # XXX: using `df -h` and converting human units to bytes
+ # may seem pointless, but there's a reason.
+ # df uses different header field names with `-h` and without it ("Size" vs "1K-blocks")
+ # and outputs values in 1K blocks without `-h`,
+ # so some amount of conversion is needed anyway.
+ # Using `df -h` by default seems simpler.
+ #
+ # This is what the output looks like, as of Debian Buster/Bullseye:
+ # $ df -h -t ext4 --output=source,size,used,avail,pcent
+ # Filesystem Size Used Avail Use%
+ # /dev/sda1 16G 7.6G 7.3G 51%
+
+ out = cmd(f"df -h --output=source,size,used,avail,pcent {directory}")
+ lines = out.splitlines()
+ lists = [l.split() for l in lines]
+ res = {lists[0][i]: lists[1][i] for i in range(len(lists[0]))}
+
+ convert = (lambda x: x) if human_units else human_to_bytes
+
+ stats = {}
+
+ stats["filesystem"] = res["Filesystem"]
+ stats["size"] = convert(res["Size"])
+ stats["used"] = convert(res["Used"])
+ stats["avail"] = convert(res["Avail"])
+ stats["use_percentage"] = re_sub(r'%', '', res["Use%"])
+
+ return stats
+
+def get_persistent_storage_stats(human_units=True):
+ from os.path import exists as path_exists
+
+ persistence_dir = "/usr/lib/live/mount/persistence"
+ if path_exists(persistence_dir):
+ stats = get_storage_stats(persistence_dir, human_units=human_units)
+ else:
+ # If the persistence path doesn't exist,
+ # the system is running from a live CD
+ # and the concept of persistence storage stats is not applicable
+ stats = None
+
+ return stats