diff options
| author | Christian Breunig <christian@breunig.cc> | 2026-03-31 15:35:33 +0200 |
|---|---|---|
| committer | Christian Breunig <christian@breunig.cc> | 2026-04-09 20:54:06 +0200 |
| commit | 51922535b79529d603c3b7d52cde9da54c069d42 (patch) | |
| tree | 2bcaca4b3877a598a5f2f0229ac0329cb87f3ab6 /src/activation-scripts | |
| parent | d2ad852ab4393862e9f27b2bc3bd7d382feb4c5a (diff) | |
| download | vyos-1x-51922535b79529d603c3b7d52cde9da54c069d42.tar.gz vyos-1x-51922535b79529d603c3b7d52cde9da54c069d42.zip | |
serial: T8375: use boot activation script to define a serial console on the CLI
Required during first-boot of a system. We have images form amd64 and arm64
CPUs which also tend to have different serial interfaces (ttyS vs. ttyAMA).
The images which are installed have the correct serial setting for GRUB (ttyS0
or ttyAMA0) and the activation script will probe the Kernel command-line. If a
serial interface is defined, we will include it in the VyOS CLI configuration.
Diffstat (limited to 'src/activation-scripts')
| -rwxr-xr-x | src/activation-scripts/05-serial_console.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/activation-scripts/05-serial_console.py b/src/activation-scripts/05-serial_console.py new file mode 100755 index 000000000..93e381d17 --- /dev/null +++ b/src/activation-scripts/05-serial_console.py @@ -0,0 +1,57 @@ +# 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/>. + +import re + +from typing import Optional +from typing import Tuple + +from vyos.configtree import ConfigTree +from vyos.utils.file import read_file +from vyos.system.image import is_live_boot + +base = ['system', 'console', 'device'] + +def get_kernel_serial_console() -> Tuple[Optional[str], Optional[str]]: + """ + Extract the serial console device and speed setting from the kernel + command line. + """ + device = speed = None + CMDLINE_CONSOLE_RE = re.compile( + r'(?:^|\s)console=(?P<device>tty(?:S|AMA)\d+),(?P<speed>\d+)(?=\s|$)' + ) + kernel_cmdline = read_file('/proc/cmdline') + if m := CMDLINE_CONSOLE_RE.search(kernel_cmdline): + device = m.group("device") # "ttyS0" + speed = m.group("speed") # Baud rate/speed, e.g. "115200" + + return (device, speed) + +def activate(config: ConfigTree): + # Configure the kernel serial console only once during live boot. + # During installation, the user can define the console. 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 + + (device, speed) = get_kernel_serial_console() + if device and speed: + # The kernel was booted with a configured serial console, but no + # console is configured in the CLI; align/fix the CLI configuration. + if not config.exists(base + [device]): + config.set(base + [device, 'speed'], value=speed) + config.set_tag(base) |
