From 629ff86edb9e5f68dd5849c6300a2ad97aaed4a0 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Wed, 24 Sep 2025 18:07:59 -0500 Subject: T7709: add utils write_file_sync and write_file_atomic write_file_sync does an explicit fsync of file and directory on write. write_file_atomic calls write_file_sync on a temp file and uses os.rename to provide an 'atomic' write. --- python/vyos/utils/file.py | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) (limited to 'python') diff --git a/python/vyos/utils/file.py b/python/vyos/utils/file.py index 1e2de2b39..491cc6547 100644 --- a/python/vyos/utils/file.py +++ b/python/vyos/utils/file.py @@ -14,6 +14,8 @@ # License along with this library. If not, see . import os +import tempfile + from vyos.utils.permission import chown def makedir(path, user=None, group=None): @@ -185,3 +187,84 @@ def wait_for_file_write_complete(file_path, pre_hook=None, timeout=None, sleep_i """ Waits for a process to close a file after opening it in write mode. """ wait_for_inotify(file_path, event_type='IN_CLOSE_WRITE', pre_hook=pre_hook, timeout=timeout, sleep_interval=sleep_interval) + + +def copy_chown(source, target): + # pylint: disable=import-outside-toplevel + import shutil + import stat + + shutil.copy2(source, target) + st = os.stat(source) + os.chown(target, st[stat.ST_UID], st[stat.ST_GID]) + + +def write_file_sync(file_path, data: str, mode='w'): + """Write file with explicit sync of file and directory""" + # pylint: disable=consider-using-with + file_dir = os.path.dirname(file_path) + + # write and sync file + try: + file = open(file_path, mode) + file.write(data) + file.flush() + os.fsync(file.fileno()) + file.close() + except OSError as e: + try: + file.close() + except OSError: + pass + raise e + + # sync directory entry + try: + fd = os.open(file_dir, os.O_DIRECTORY | os.O_RDONLY) + os.fsync(fd) + os.close(fd) + except OSError as e: + try: + os.close(fd) + except OSError: + pass + raise e + + +def write_file_atomic(file_path, data: str, mode='w'): + """Use os.rename for 'atomic' write. + + Note that this requires an euid/egid of that of the source file for the + chown operation. + + Note that this calls write_file_sync, above. + """ + # pylint: disable=consider-using-with,raise-missing-from + file_dir = os.path.dirname(file_path) + temp_file = tempfile.NamedTemporaryFile(delete=False, dir=file_dir).name + + def cleanup(): + if os.path.exists(temp_file): + try: + os.unlink(temp_file) + except OSError: + pass + + if os.path.exists(file_path): + try: + copy_chown(file_path, temp_file) + except OSError as e: + cleanup() + raise OSError(f'copy_chown {e}') + + try: + write_file_sync(temp_file, data, mode=mode) + except OSError as e: + cleanup() + raise OSError(f'write_file_sync {e}') + + try: + os.rename(temp_file, file_path) + except OSError as e: + cleanup() + raise OSError(f'rename {e}') -- cgit v1.2.3 From fbdf1fb8914a6b0ff3a64f4f359eaf3abd8be031 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Wed, 24 Sep 2025 18:27:08 -0500 Subject: T7709: use write_file_sync/atomic in vyos-save-config.py script Config save is provided by the helper script in both CLI and configsession (hence also in the http api). Use utilities write_file_sync and write_file_atomic, in accordance with permissions and location: If the target is in /opt/vyatta/etc/config or /config, use write_file_sync; if, moreover, the caller has permissions, use write_file_atomic. Otherwise, fall back to util write_file. --- python/vyos/component_version.py | 10 ++++++++ src/helpers/vyos-save-config.py | 52 +++++++++++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 9 deletions(-) (limited to 'python') diff --git a/python/vyos/component_version.py b/python/vyos/component_version.py index 136bd36e8..13fb8333f 100644 --- a/python/vyos/component_version.py +++ b/python/vyos/component_version.py @@ -209,6 +209,16 @@ def version_info_prune_component(x: VersionInfo, y: VersionInfo) -> VersionInfo: x.component = {k: v for k, v in x.component.items() if k in y.component} +def add_system_version_string(config_str: str = None) -> str: + """Wrap config string with system version and return string.""" + version_info = version_info_from_system() + if config_str is not None: + version_info.update_config_body(config_str) + version_info.update_footer() + + return version_info.write_string() + + def add_system_version(config_str: str = None, out_file: str = None): """Wrap config string with system version and write to out_file. diff --git a/src/helpers/vyos-save-config.py b/src/helpers/vyos-save-config.py index adf62b71d..208fb8ae8 100755 --- a/src/helpers/vyos-save-config.py +++ b/src/helpers/vyos-save-config.py @@ -23,15 +23,22 @@ from argparse import ArgumentParser from vyos.config import Config from vyos.remote import urlc -from vyos.component_version import add_system_version +from vyos.component_version import add_system_version_string from vyos.defaults import directories +from vyos.utils.file import write_file +from vyos.utils.file import write_file_sync +from vyos.utils.file import write_file_atomic +from vyos.utils.file import file_is_persistent DEFAULT_CONFIG_PATH = os.path.join(directories['config'], 'config.boot') remote_save = None parser = ArgumentParser(description='Save configuration') parser.add_argument('file', type=str, nargs='?', help='Save configuration to file') -parser.add_argument('--write-json-file', type=str, help='Save JSON of configuration to file') +parser.add_argument( + '--write-json-file', type=str, help='Save JSON of configuration to file' +) + args = parser.parse_args() file = args.file json_file = args.write_json_file @@ -47,16 +54,43 @@ if re.match(r'\w+:/', save_file): except ValueError as e: sys.exit(e) + config = Config() ct = config.get_config_tree(effective=True) +# The effective config is None before boot configuration is complete. +# Nothing to write, nor do we want to invite saving an empty string: +# exit gracefully. +if ct is None: + sys.exit() + +config_str = ct.to_string() +versioned_config_str = add_system_version_string(config_str) + # pylint: disable=consider-using-with -write_file = save_file if remote_save is None else NamedTemporaryFile(delete=False).name +file_to_write = ( + save_file if remote_save is None else NamedTemporaryFile(delete=False).name +) -# config_tree is None before boot configuration is complete; -# automated saves should check boot_configuration_complete -config_str = None if ct is None else ct.to_string() -add_system_version(config_str, write_file) +if file_is_persistent(file_to_write): + if os.geteuid() == 0: + try: + write_file_atomic(file_to_write, versioned_config_str) + except OSError as e: + print(f'failed to write config file, write_file_atomic: {e}') + sys.exit(1) + else: + try: + write_file_sync(file_to_write, versioned_config_str) + except OSError as e: + print(f'failed to write config file, write_file_sync: {e}') + sys.exit(1) +else: + try: + write_file(file_to_write, versioned_config_str) + except Exception as e: # pylint: disable=broad-exception-caught + print(f'failed to write config file, write_file: {e}') + sys.exit(1) if json_file is not None and ct is not None: try: @@ -67,6 +101,6 @@ if json_file is not None and ct is not None: if remote_save is not None: try: - remote_save.upload(write_file) + remote_save.upload(file_to_write) finally: - os.remove(write_file) + os.remove(file_to_write) -- cgit v1.2.3 From c8dde333d96c08e4d61ed373dedc2f12da8e4fe1 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Wed, 24 Sep 2025 18:54:03 -0500 Subject: T7709: use standard script vyos-save-config.py for vyconf Take advantage of the helper script in the vyconf context. --- python/vyos/configsession.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'python') diff --git a/python/vyos/configsession.py b/python/vyos/configsession.py index 462a028bb..f0a22d930 100644 --- a/python/vyos/configsession.py +++ b/python/vyos/configsession.py @@ -392,13 +392,7 @@ class ConfigSession(object): return out def save_config(self, file_path): - if self._vyconf_session is None: - out = self.__run_command(SAVE_CONFIG + [file_path]) - else: - out, _ = self._vyconf_session.save_config( - file=file_path, append_version=True - ) - + out = self.__run_command(SAVE_CONFIG + [file_path]) return out def install_image(self, url): -- cgit v1.2.3