From 6c45d006560c574da3651f636af3f884fdd2ce66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20=C5=A0karvada?= Date: Fri, 19 Sep 2014 13:32:59 +0200 Subject: [PATCH] moved profile autodetection from post install script to tuned daemon MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The file system image containing tuned can be prepared offline, thus we need to check the configuration on the first run. This is also more clean solution than the previous post install hack. Resolves: rhbz#1144067 Signed-off-by: Jaroslav Škarvada --- tuned.spec | 8 -------- tuned/daemon/daemon.py | 27 +++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/tuned.spec b/tuned.spec index b8907d3..8aa6841 100644 --- a/tuned.spec +++ b/tuned.spec @@ -80,14 +80,6 @@ sed -i 's/\(dynamic_tuning[ \t]*=[ \t]*\).*/\10/' %{buildroot}%{_sysconfdir}/tun %post %systemd_post tuned.service -# try to autodetect the best profile for the system in case there is none preset -if [ ! -f /etc/tuned/active_profile -o -z "`cat /etc/tuned/active_profile 2>/dev/null`" ] -then - PROFILE=`/usr/sbin/tuned-adm recommend 2>/dev/null` - [ "$PROFILE" ] || PROFILE=balanced - /usr/sbin/tuned-adm profile "$PROFILE" 2>/dev/null || echo -n "$PROFILE" > /etc/tuned/active_profile -fi - # convert active_profile from full path to name (if needed) sed -i 's|.*/\([^/]\+\)/[^\.]\+\.conf|\1|' /etc/tuned/active_profile diff --git a/tuned/daemon/daemon.py b/tuned/daemon/daemon.py index 6b20fa6..7409418 100644 --- a/tuned/daemon/daemon.py +++ b/tuned/daemon/daemon.py @@ -1,4 +1,5 @@ import os +import errno import threading import tuned.logs from tuned.exceptions import TunedException @@ -104,16 +105,34 @@ class Daemon(object): def _save_active_profile(self, profile_name): try: with open(consts.ACTIVE_PROFILE_FILE, "w") as f: - f.write(profile_name) + f.write(profile_name + "\n") except (OSError,IOError) as e: log.error("Cannot write active profile into %s: %s" % (consts.ACTIVE_PROFILE_FILE, str(e))) + def _set_recommended_profile(self): + log.info("no profile preset, checking what is recommended for your configuration") + profile = tuned.utils.commands.recommend_profile() + log.info("using '%s' profile and setting it as active" % profile) + self._save_active_profile(profile) + return profile + def _get_active_profile(self): try: with open(consts.ACTIVE_PROFILE_FILE, "r") as f: - return f.read().strip() - except (OSError, IOError, EOFError) as e: - log.error("Cannot read active profile, setting default.") + profile = f.read().strip() + if profile == "": + profile = self._set_recommended_profile() + return profile + except IOError as e: + if e.errno == errno.ENOENT: + # No such file or directory + profile = self._set_recommended_profile() + else: + profile = consts.DEFAULT_PROFILE + log.error("error reading active profile from '%s', falling back to '%s' profile" % (consts.ACTIVE_PROFILE_FILE, profile)) + return profile + except (OSError, EOFError) as e: + log.error("cannot read active profile, falling back to '%s' profile" % consts.DEFAULT_PROFILE) return consts.DEFAULT_PROFILE def is_enabled(self):