From a1b9e606449715202b4feccf8b6de23e278b776f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20=C5=A0karvada?= Date: Tue, 4 Apr 2017 15:11:45 +0200 Subject: [PATCH] builtin functions: added virt_check function and support expansion in include MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The virt_check builtin function expands to the first argument if running inside virtual machine, otherwise it expands to the second argument (when on bare metal or in case of error). It uses virt-what as a backend. So now the following can be used to lower fragmentation of Tuned profiles: [main] include=virtual-${f:virt_check:guest:host} So it will include virtual-guest if running inside virtual machine, otherwise it includes virtual-host. Resolves: rhbz#1426654 Signed-off-by: Jaroslav Škarvada --- .../profiles/functions/function_virt_check.py | 22 +++++++++++++++++++ tuned/profiles/loader.py | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tuned/profiles/functions/function_virt_check.py diff --git a/tuned/profiles/functions/function_virt_check.py b/tuned/profiles/functions/function_virt_check.py new file mode 100644 index 0000000..2caba3f --- /dev/null +++ b/tuned/profiles/functions/function_virt_check.py @@ -0,0 +1,22 @@ +import os +import tuned.logs +import base +from tuned.utils.commands import commands + +class virt_check(base.Function): + """ + Checks whether running inside virtual machine (VM) or on bare metal. + If running inside VM expands to argument 1, otherwise expands to + argument 2 (even on error). + """ + def __init__(self): + # 2 arguments + super(self.__class__, self).__init__("virt_check", 2) + + def execute(self, args): + if not super(self.__class__, self).execute(args): + return None + (ret, out) = self._cmd.execute(["virt-what"]) + if ret == 0 and len(out) > 0: + return args[0] + return args[1] diff --git a/tuned/profiles/loader.py b/tuned/profiles/loader.py index 5ee7bed..0c7520c 100644 --- a/tuned/profiles/loader.py +++ b/tuned/profiles/loader.py @@ -72,7 +72,7 @@ class Loader(object): config = self._load_config_data(filename) profile = self._profile_factory.create(name, config) if "include" in profile.options: - include_name = profile.options.pop("include") + include_name = self._variables.expand(profile.options.pop("include")) self._load_profile([include_name], profiles, processed_files) profiles.append(profile)