summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Breunig <christian@breunig.cc>2026-04-14 14:24:09 +0000
committerGitHub <noreply@github.com>2026-04-14 14:24:09 +0000
commit96573c5c3fa4a619819e808c4f8e7a8996cf7fe6 (patch)
treebfb63f8c4d25632a90cb3b58b69c137511656eea /src
parent73f743d176f96affceab004c92d19b561f71a24e (diff)
parent35db941bcf30f3fac5b1358aa1124f34f9808950 (diff)
downloadvyos-1x-96573c5c3fa4a619819e808c4f8e7a8996cf7fe6.tar.gz
vyos-1x-96573c5c3fa4a619819e808c4f8e7a8996cf7fe6.zip
Merge pull request #5092 from c-po/kernel-serial-activation
serial: T8375: use boot activation script to define a serial console on the CLI
Diffstat (limited to 'src')
-rwxr-xr-xsrc/activation-scripts/05-serial_console.py43
-rwxr-xr-xsrc/conf_mode/system_console.py35
-rwxr-xr-xsrc/init/vyos-router6
-rw-r--r--src/migration-scripts/system/31-to-3238
-rwxr-xr-xsrc/op_mode/image_installer.py79
5 files changed, 174 insertions, 27 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..ccda2ac4e
--- /dev/null
+++ b/src/activation-scripts/05-serial_console.py
@@ -0,0 +1,43 @@
+# 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/conf_mode/system_console.py b/src/conf_mode/system_console.py
index 4411257e3..50e5e607b 100755
--- a/src/conf_mode/system_console.py
+++ b/src/conf_mode/system_console.py
@@ -30,6 +30,8 @@ airbag.enable()
by_bus_dir = '/dev/serial/by-bus'
+default_tty_console = ('tty', '0', '')
+
def get_config(config=None):
if config:
conf = config
@@ -57,7 +59,8 @@ def verify(console):
if not console or 'device' not in console:
return None
- for device in console['device']:
+ kernel_consoles: list = []
+ for device, device_config in console['device'].items():
if device.startswith('usb'):
# It is much easiert to work with the native ttyUSBn name when using
# getty, but that name may change across reboots - depending on the
@@ -68,8 +71,13 @@ def verify(console):
# and it can not be used as a serial interface
if not os.path.isdir(by_bus_dir) or not os.path.exists(by_bus_device):
raise ConfigError(f'Device {device} does not support being used as tty')
- if not is_tty(device):
+ if not is_tty(device, warning=True):
Warning(f'Device "{device}" used for console is not a TTY!')
+ if 'kernel' in device_config:
+ kernel_consoles.append(device)
+
+ if len(kernel_consoles) > 1:
+ raise ConfigError('Only one device can be used as Kernel output console!')
return None
@@ -82,6 +90,7 @@ def generate(console):
os.unlink(os.path.join(root, basename))
if not console or 'device' not in console:
+ grub_util.update_serial_console(*default_tty_console)
return None
# replace keys in the config for ttyUSB items to use them in `apply()` later
@@ -101,6 +110,7 @@ def generate(console):
else:
raise ConfigError(f'Device {device} does not support being used as tty')
+ use_serial_console = False
for device, device_config in console['device'].items():
# Do not render getty configuration if specified device is not a TTY.
if not is_tty(device):
@@ -112,14 +122,15 @@ def generate(console):
render(config_file, 'getty/serial-getty.service.j2', device_config)
os.symlink(config_file, getty_wants_symlink)
- # GRUB
- # For existing serial line change speed (if necessary)
- # Only applies to ttyS0
- if 'ttyS0' not in console['device']:
- return None
+ if 'kernel' in device_config:
+ # get console type ("ttyS" or "ttyAMA") from device (e.g. "ttyS0")
+ console_type = device.rstrip('0123456789')
+ console_num = device[len(console_type):]
+ grub_util.update_serial_console(console_type, console_num, device_config['speed'])
+ use_serial_console = True
- speed = console['device']['ttyS0']['speed']
- grub_util.update_console_speed(speed)
+ if not use_serial_console:
+ grub_util.update_serial_console(*default_tty_console)
return None
@@ -127,9 +138,9 @@ def apply(console):
# Reset screen blanking
call('/usr/bin/setterm -blank 0 -powersave off -powerdown 0 -term linux </dev/tty1 >/dev/tty1 2>&1')
- # Service control moved to vyos.utils.serial to unify checks and prompts.
- # If users are connected, we want to show an informational message on completing
- # the process, but not halt configuration processing with an interactive prompt.
+ # Service control moved to vyos.utils.serial to unify checks and prompts.
+ # If users are connected, we want to show an informational message on completing
+ # the process, but not halt configuration processing with an interactive prompt.
restart_login_consoles(prompt_user=False, quiet=False)
if not console:
diff --git a/src/init/vyos-router b/src/init/vyos-router
index 234f9b9b9..600892c93 100755
--- a/src/init/vyos-router
+++ b/src/init/vyos-router
@@ -194,10 +194,10 @@ migrate_bootfile ()
}
# configure system-specific settings
-system_config ()
+system_activate ()
{
if [ -x $vyos_libexec_dir/run-config-activation.py ]; then
- log_progress_msg system
+ log_progress_msg activate
sg ${GROUP} -c "$vyos_libexec_dir/run-config-activation.py $BOOTFILE"
fi
}
@@ -613,7 +613,7 @@ start ()
update_interface_config || overall_status=1
- disabled system_config || system_config || overall_status=1
+ disabled system_activate || system_activate || overall_status=1
for s in ${subinit[@]} ; do
if ! disabled $s; then
diff --git a/src/migration-scripts/system/31-to-32 b/src/migration-scripts/system/31-to-32
new file mode 100644
index 000000000..1688b44ed
--- /dev/null
+++ b/src/migration-scripts/system/31-to-32
@@ -0,0 +1,38 @@
+# 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/>.
+
+# Serial
+
+from vyos.configtree import ConfigTree
+from vyos.system import disk
+from vyos.system.grub import CFG_VYOS_VARS
+from vyos.system.grub import vars_read
+
+serial_console = 'ttyS0'
+base = ['system', 'console']
+
+def migrate(config: ConfigTree) -> None:
+ if not base:
+ return
+
+ root_dir = disk.find_persistence()
+ vars_file: str = f'{root_dir}/{CFG_VYOS_VARS}'
+ vars_current: dict[str, str] = vars_read(vars_file)
+ # Check if VyOS installation uses serial boot console
+ if vars_current['console_type'] == 'ttyS':
+ # In the past we only supported ttyS0 as boot console, that's why we
+ # can hardcode it ...
+ if config.exists(base + ['device', serial_console]):
+ config.set(base + ['device', serial_console, 'kernel'])
diff --git a/src/op_mode/image_installer.py b/src/op_mode/image_installer.py
index cb3aa7b6c..27d8214c3 100755
--- a/src/op_mode/image_installer.py
+++ b/src/op_mode/image_installer.py
@@ -17,9 +17,14 @@
# You should have received a copy of the GNU General Public License along with
# VyOS. If not, see <https://www.gnu.org/licenses/>.
-from argparse import ArgumentParser, Namespace
+from argparse import ArgumentParser
+from argparse import Namespace
from pathlib import Path
-from shutil import copy, chown, rmtree, copytree, disk_usage
+from shutil import copy
+from shutil import chown
+from shutil import rmtree
+from shutil import copytree
+from shutil import disk_usage
from glob import glob
from sys import exit
from os import environ
@@ -38,8 +43,10 @@ from psutil import disk_partitions
from vyos.base import Warning
from vyos.configtree import ConfigTree
+from vyos.config_mgmt import unsaved_commits
from vyos.defaults import base_dir
from vyos.defaults import directories
+from vyos.flavor import get_image_serial_console
from vyos.remote import download
from vyos.system import disk
from vyos.system import grub
@@ -55,13 +62,16 @@ from vyos.utils.auth import evaluate_strength
from vyos.utils.auth import get_local_users
from vyos.utils.auth import get_user_home_dir
from vyos.utils.dict import dict_search
-from vyos.utils.io import ask_input, ask_yes_no, select_entry
+from vyos.utils.io import ask_input
+from vyos.utils.io import ask_yes_no
+from vyos.utils.io import select_entry
from vyos.utils.file import chmod_2775
from vyos.utils.file import read_file
from vyos.utils.file import write_file
-from vyos.utils.process import cmd, run, rc_cmd
+from vyos.utils.process import cmd
+from vyos.utils.process import run
+from vyos.utils.process import rc_cmd
from vyos.version import get_version_data
-from vyos.config_mgmt import unsaved_commits
# define text messages
MSG_ERR_NOT_LIVE: str = 'The system is already installed. Please use "add system image" instead.'
@@ -135,16 +145,17 @@ ISO_DOWNLOAD_PATH: str = ''
external_download_script: str = f'{base_dir}/simple-download.py'
external_latest_image_url_script: str = f'{base_dir}/latest-image-url.py'
+(flavor_sercon_type, flavor_sercon_num, flavor_sercon_speed) = get_image_serial_console()
+
# default boot variables
DEFAULT_BOOT_VARS: dict[str, str] = {
'timeout': '5',
'console_type': 'tty',
- 'console_num': '0',
- 'console_speed': '115200',
+ 'console_num': flavor_sercon_num,
+ 'console_speed': flavor_sercon_speed,
'bootmode': 'normal'
}
-
def bytes_to_gb(size: int) -> float:
"""Convert Bytes to GBytes, rounded to 1 decimal number
@@ -606,6 +617,49 @@ def configure_authentication(config_file: str, password: str) -> None:
with open(config_file, 'w') as f:
f.write(config.to_string())
+def configure_serial_console(config_file: str, console_type: str) -> None:
+ """Apply serial console settings to config.boot from kernel cmdline.
+
+ This overlaps with 05-serial_console.py activation logic, but that script
+ only runs during live boot. During installation, the user may pick a
+ different source config, so serial console settings must be written to
+ the final target config explicitly.
+
+ Behavior:
+ - Reads the kernel serial console device/speed from the current boot cmdline.
+ - If the detected device is a valid tty, writes:
+ system console device <TTY> speed <rate>
+ - If "console_type == 'S'", also writes:
+ system console device <TTY> kernel
+
+ Args:
+ config_file (str): path of target config file
+ console_type (str): 'K' (KVM/tty) or 'S' (serial)
+ """
+ from vyos.utils.serial import is_tty
+ from vyos.utils.kernel import get_kernel_serial_console
+
+ # Parse current kernel cmdline and continue only for valid serial console
+ # data. Prevent writing incomplete/invalid console settings to config.boot.
+ k_console_type, k_console_num, k_console_speed = get_kernel_serial_console()
+ device = f'{k_console_type}{k_console_num}'
+ if not is_tty(device) or not k_console_speed:
+ return
+
+ base = ['system', 'console', 'device']
+ config_string = read_file(config_file)
+ config = ConfigTree(config_string)
+ config.set(base + [device, 'speed'], value=k_console_speed)
+ config.set_tag(base)
+
+ # Only mark this device as kernel boot console when console_type 'S' for
+ # serial was defined by user.
+ if console_type == 'S':
+ config.set(base + [device, 'kernel'])
+
+ with open(config_file, 'w') as f:
+ f.write(config.to_string())
+
def validate_signature(file_path: str, sign_type: str) -> None:
"""Validate a file by signature and delete a signature file
@@ -759,12 +813,11 @@ def console_hint() -> str:
path = '/dev/tty'
name = Path(path).name
- if name in ['ttyS0', 'ttyAMA0']:
+ if name.startswith(('ttyS', 'ttyAMA')):
return 'S'
else:
return 'K'
-
def cleanup(mounts: list[str] = [], remove_items: list[str] = []) -> None:
"""Clean up after installation
@@ -913,10 +966,10 @@ def install_image() -> None:
print(MSG_WARN_PASSWORD_CONFIRM)
# ask for default console
+ console_dict: dict[str, str] = {'K': 'tty', 'S': flavor_sercon_type}
console_type: str = ask_input(MSG_INPUT_CONSOLE_TYPE,
default=console_hint(),
- valid_responses=['K', 'S'])
- console_dict: dict[str, str] = {'K': 'tty', 'S': 'ttyS'}
+ valid_responses=console_dict.keys())
config_boot_list = [f'{DIR_CONFIG}/config.boot',
'/opt/vyatta/etc/config.boot.default']
@@ -962,6 +1015,8 @@ def install_image() -> None:
copy(default_config, f'{target_config_dir}/config.boot')
configure_authentication(f'{target_config_dir}/config.boot',
user_password)
+ configure_serial_console(f'{target_config_dir}/config.boot',
+ console_type)
Path(f'{target_config_dir}/.vyatta_config').touch()
# create a persistence.conf