diff options
Diffstat (limited to 'python/vyos/version.py')
-rw-r--r-- | python/vyos/version.py | 66 |
1 files changed, 52 insertions, 14 deletions
diff --git a/python/vyos/version.py b/python/vyos/version.py index d51a940d6..a524b36ea 100644 --- a/python/vyos/version.py +++ b/python/vyos/version.py @@ -34,9 +34,17 @@ import json import vyos.defaults +from vyos.util import read_file +from vyos.util import read_json +from vyos.util import popen +from vyos.util import run +from vyos.util import DEVNULL + + version_file = os.path.join(vyos.defaults.directories['data'], 'version.json') -def get_version_data(file=version_file): + +def get_version_data(fname=version_file): """ Get complete version data @@ -52,20 +60,50 @@ def get_version_data(file=version_file): is an implementation detail and may change in the future, while the interface of this module will stay the same. """ - try: - with open(file, 'r') as f: - version_data = json.load(f) - return version_data - except FileNotFoundError: - return {} + return read_json(fname, {}) -def get_version(file=None): + +def get_version(fname=version_file): """ Get the version number, or an empty string if it could not be determined """ - version_data = None - if file: - version_data = get_version_data(file=file) - else: - version_data = get_version_data() - return version_data.get('version','') + return get_version_data(fname=fname).get('version', '') + + +def get_full_version_data(fname=version_file): + version_data = get_version_data(fname) + + # Get system architecture (well, kernel architecture rather) + version_data['system_arch'], _ = popen('uname -m', stderr=DEVNULL) + + # Get hypervisor name, if any + try: + hypervisor, _ = popen('hvinfo', stderr=DEVNULL) + version_data['system_type'] = f"{hypervisor} guest" + except OSError: + # hvinfo returns 1 if it cannot detect any hypervisor + version_data['system_type'] = 'bare metal' + + # Get boot type, it can be livecd, installed image, or, possible, a system installed + # via legacy "install system" mechanism + # In installed images, the squashfs image file is named after its image version, + # while on livecd it's just "filesystem.squashfs", that's how we tell a livecd boot + # from an installed image + boot_via = "installed image" + if run(""" grep -e '^overlay.*/filesystem.squashfs' /proc/mounts >/dev/null""") == 0: + boot_via = "livecd" + elif run(""" grep '^overlay /' /proc/mounts >/dev/null """) != 0: + boot_via = "legacy non-image installation" + version_data['boot_via'] = boot_via + + # Get hardware details from DMI + dmi = '/sys/class/dmi/id' + version_data['hardware_vendor'] = read_file(dmi + '/sys_vendor', 'Unknown') + version_data['hardware_model'] = read_file(dmi +'/product_name','Unknown') + + # These two assume script is run as root, normal users can't access those files + subsystem = '/sys/class/dmi/id/subsystem/id' + version_data['hardware_serial'] = read_file(subsystem + '/product_serial','Unknown') + version_data['hardware_uuid'] = read_file(subsystem + '/product_uuid', 'Unknown') + + return version_data |