diff options
| author | Christian Breunig <christian@breunig.cc> | 2026-04-26 19:38:17 +0200 |
|---|---|---|
| committer | Christian Breunig <christian@breunig.cc> | 2026-04-26 19:38:17 +0200 |
| commit | e5c61bdfef7a060120625ff50e302923753b2ef3 (patch) | |
| tree | 3ee0ffdfb49573b6ba3ee30e20347c3563c1c9b6 | |
| parent | 0cc2bbfc54370cce2d2c7540fb155e464207f1be (diff) | |
| download | vyos-1x-e5c61bdfef7a060120625ff50e302923753b2ef3.tar.gz vyos-1x-e5c61bdfef7a060120625ff50e302923753b2ef3.zip | |
utils: T8541: add write-config-file-value helper
Introduce src/helpers/write-config-file-value.py to patch a saved config.boot
from scripts by writing a quoted CLI path with an optional value (omit --value
to create a valueless node).
Extend vyos.utils.config.write_saved_value() to safely update saved configs:
create missing parent nodes before tagging (ConfigTree.set_tag requires nodes
to exist) and then write the requested value/valueless leaf back to disk.
Remove the legacy serial console activation script which is now replaced by the
helper-based approach. The helper is run once during ISO Image assembly and will
inject the approprate serial console defintiions into config.boot.default.
| -rw-r--r-- | python/vyos/utils/config.py | 29 | ||||
| -rwxr-xr-x | src/activation-scripts/05-serial_console.py | 43 | ||||
| -rw-r--r-- | src/helpers/write-config-file-value.py | 48 |
3 files changed, 77 insertions, 43 deletions
diff --git a/python/vyos/utils/config.py b/python/vyos/utils/config.py index 1f067e91e..5bba69a61 100644 --- a/python/vyos/utils/config.py +++ b/python/vyos/utils/config.py @@ -44,6 +44,35 @@ def read_saved_value(path: list): return ' '.join(res) return res +def write_saved_value(path: list, value=None, config_path: str=config_file): + """Write or replace a node in a saved configuration. + + - If value is None, the node is treated as valueless. + - Tag nodes in the path are detected via XML reference tree and marked on + the ConfigTree prior to setting the node. + """ + if not isinstance(path, list) or not path: + raise ValueError('path must be a non-empty list') + + from vyos.configtree import ConfigTree + from vyos.utils.file import read_file + from vyos.utils.file import write_file + + config_string = read_file(config_path) + ct = ConfigTree(config_string) + + # ConfigTree.set_tag() requires the node to exist. + # Create missing nodes along the path so tag marking works even when + # writing a completely new subtree into config.boot. + for target in flag(path): + if not ct.exists(target): + ct.create_node(target) + + set_tags(ct, path) + + ct.set(path, value=value, replace=True) + write_file(config_path, ct.to_string()) + def flag(l: list) -> list: res = [l[0:i] for i,_ in enumerate(l, start=1)] return res diff --git a/src/activation-scripts/05-serial_console.py b/src/activation-scripts/05-serial_console.py deleted file mode 100755 index ccda2ac4e..000000000 --- a/src/activation-scripts/05-serial_console.py +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright (C) VyOS Inc. -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with this library. If not, see <http://www.gnu.org/licenses/>. - -from vyos.configtree import ConfigTree -from vyos.system.image import is_live_boot -from vyos.utils.kernel import get_kernel_serial_console -from vyos.utils.serial import is_tty - -base = ['system', 'console', 'device'] - -def activate(config: ConfigTree): - # Configure the kernel serial console only once during live boot. During - # installation, the user can define the console type (VT TTY or serial - # TTY). If this is not limited to live boot, the serial interface will - # always be re-added during system boot, even if it was removed from - # config.boot. - if not is_live_boot(): - return - - # Parse current kernel cmdline and continue only for valid serial console - # data. Prevent writing incomplete/invalid console settings to config.boot. - console_type, console_num, console_speed = get_kernel_serial_console() - device = f'{console_type}{console_num}' - if not is_tty(device) or not console_speed: - return - - # The kernel was booted with a configured serial console, but no console - # is configured via CLI; align/fix the CLI configuration. - if not config.exists(base + [device]): - config.set(base + [device, 'speed'], value=console_speed) - config.set_tag(base) diff --git a/src/helpers/write-config-file-value.py b/src/helpers/write-config-file-value.py new file mode 100644 index 000000000..f3993223c --- /dev/null +++ b/src/helpers/write-config-file-value.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +# +# Copyright (C) VyOS Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +from argparse import ArgumentParser +from shlex import split as shlex_split + +from vyos.utils.config import write_saved_value + +def _split_quoted(s: str) -> list[str]: + parts = shlex_split(s) + if not parts: + raise ValueError('empty string') + return parts + +if __name__ == '__main__': + parser = ArgumentParser() + parser.add_argument( + '--path', + required=True, + help='Quoted CLI path, e.g. "system console device ttyS1 speed"', + ) + parser.add_argument( + '--value', + required=False, + help='Value for the node, e.g. "9600". If omitted, creates a valueless node.', + ) + parser.add_argument( + '--config-file', + required=True, + help=f'Path to saved config.boot', + ) + args = parser.parse_args() + + path = _split_quoted(args.path) + write_saved_value(path, value=args.value, config_path=args.config_file) |
