From: Julien Fortin Date: Fri, 11 Jan 2019 04:00:39 +0000 (+0800) Subject: statemanager: configure state_dir via ifupdown2.conf X-Git-Tag: 1.2.5-1~6 X-Git-Url: https://git.proxmox.com/?p=mirror_ifupdown2.git;a=commitdiff_plain;h=9f98f3604e0c34b3e73b7cfda20255ba546f7126 statemanager: configure state_dir via ifupdown2.conf ifupdown2 used /var/tmp/network/ to store its state file upstream users reported that when /var/tmp is not mounted before network configuration ifupdown2 fails. We now let user define which location they want to use for the state file. closes: #918832 Reported-by: Maximilian Wilhelm Signed-off-by: Julien Fortin --- diff --git a/debian/changelog b/debian/changelog index 6063939..cd62e83 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,10 @@ +ifupdown2 (1.2.4-1) unstable; urgency=medium + + * Fix: statemanager directory path customization via ifupdown2.conf + (closes: #918832) + + -- Julien Fortin Fri, 11 Jan 2019 23:42:42 +0000 + ifupdown2 (1.2.3-1) unstable; urgency=medium * Fix: log: use stderr if syslog initialization fails (closes: #917534) diff --git a/etc/network/ifupdown2/ifupdown2.conf b/etc/network/ifupdown2/ifupdown2.conf index e1035d6..e05c35f 100644 --- a/etc/network/ifupdown2/ifupdown2.conf +++ b/etc/network/ifupdown2/ifupdown2.conf @@ -80,3 +80,9 @@ ifaceobj_squash=0 # based on the physical interface they are running on top of. # set this flag to 0 to disable this behaviour adjust_logical_dev_mtu=1 + +# directory where the state file is stored +# if this directory doesn't exists ifupdown2 will create it +# if directory creation fails or state_dir variable is empty +# state_dir will default to /run/network/ +state_dir=/run/network/ diff --git a/ifupdown2/ifupdown/ifupdownmain.py b/ifupdown2/ifupdown/ifupdownmain.py index 0633dc3..f520994 100644 --- a/ifupdown2/ifupdown/ifupdownmain.py +++ b/ifupdown2/ifupdown/ifupdownmain.py @@ -287,6 +287,11 @@ class ifupdownMain(ifupdownBase): self._cache_no_repeats = {} + # initialize global config object with config passed by the user + # This makes config available to addon modules + ifupdownConfig.config = self.config + statemanager.statemanager_api.init() + if self.flags.STATEMANAGER_ENABLE: self.statemanager = statemanager.statemanager_api try: @@ -323,10 +328,6 @@ class ifupdownMain(ifupdownBase): self._ifaceobj_squash_internal = True if self.config.get( 'ifaceobj_squash_internal', '1') == '1' else False - # initialize global config object with config passed by the user - # This makes config available to addon modules - ifupdownConfig.config = self.config - self.validate_keywords = { '': self._keyword_mac, '': self._keyword_text, diff --git a/ifupdown2/ifupdown/statemanager.py b/ifupdown2/ifupdown/statemanager.py index 0615456..5e8f1c7 100644 --- a/ifupdown2/ifupdown/statemanager.py +++ b/ifupdown2/ifupdown/statemanager.py @@ -15,10 +15,12 @@ try: from ifupdown2.ifupdown.iface import * import ifupdown2.ifupdown.exceptions as exceptions + import ifupdown2.ifupdown.ifupdownconfig as ifupdownConfig except ImportError: from ifupdown.iface import * import ifupdown.exceptions as exceptions + import ifupdown.ifupdownconfig as ifupdownConfig class pickling(): @@ -61,8 +63,7 @@ class stateManager(): """ - state_dir = '/var/tmp/network/' - """directory where the state file is stored """ + __DEFAULT_STATE_DIR = "/run/network/" state_filename = 'ifstatenew' """name of the satefile """ @@ -78,14 +79,43 @@ class stateManager(): which includes a dictionary of last pickled iface objects """ + self.state_dir = None + self.state_file = None self.ifaceobjdict = OrderedDict() self.logger = logging.getLogger('ifupdown.' + self.__class__.__name__) - if not os.path.exists(self.state_dir): - os.mkdir(self.state_dir) + + def init(self): + self.state_dir = ifupdownConfig.config.get("state_dir") + used_default = False + + if not self.state_dir: + self.logger.debug("statemanager: state_dir not defined in config file, using default: %s" % self.__DEFAULT_STATE_DIR) + self.state_dir = self.__DEFAULT_STATE_DIR + used_default = True + + try: + self._init_makedirs_state_dir() + except Exception as e: + if used_default: + # if the default path was used but still throws an exception... + raise + self.logger.info("statemanager: %s: using default directory: %s" % (e, self.__DEFAULT_STATE_DIR)) + self.state_dir = self.__DEFAULT_STATE_DIR + try: + self._init_makedirs_state_dir() + except Exception as e: + raise Exception("statemanager: unable to create required directory: %s" % str(e)) + if not os.path.exists(self.state_rundir): - os.mkdir(self.state_rundir) - self.state_file = self.state_dir + self.state_filename + os.makedirs(self.state_rundir) + + self.state_file = "%s/%s" % (self.state_dir, self.state_filename) + + def _init_makedirs_state_dir(self): + if not os.path.exists(self.state_dir): + os.makedirs(self.state_dir) + def save_ifaceobj(self, ifaceobj): self.ifaceobjdict.setdefault(ifaceobj.name,