# 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 . # Serial from vyos.configtree import ConfigTree from vyos.flavor import get_image_serial_console from vyos.system import compat from vyos.system import disk from vyos.system import image from vyos.system.grub import CFG_VYOS_VARS from vyos.system.grub import vars_read from vyos.utils.kernel import get_kernel_primary_console base = ['system', 'console'] def migrate(config: ConfigTree) -> None: if not config.exists(base): return # The serial console device is defined by the image flavor - amd64 uses the # 8250 UART at ttyS0 while arm64 systems expose the PL011 UART as ttyAMA0 console_type, console_num, _ = get_image_serial_console() if not console_type: # image flavor carries no serial console data, assume 8250 UART console_type, console_num = 'ttyS', '0' serial_console = f'{console_type}{console_num}' if not config.exists(base + ['device', serial_console]): return # A container shares the kernel command line of its host, which says # nothing about the console of VyOS itself if image.is_running_as_container(): return # An installed legacy image keeps GRUB in compat mode. The GRUB structure # update then rewrites the GRUB vars on every boot before migration runs, # so they may not reflect the boot console - use the console the kernel # booted with instead if compat.mode(): kernel_type, kernel_num, _ = get_kernel_primary_console() if kernel_type is not None: if f'{kernel_type}{kernel_num}' == serial_console: config.set(base + ['device', serial_console, 'kernel']) return root_dir = disk.find_persistence() vars_file: str = f'{root_dir}/{CFG_VYOS_VARS}' vars_current: dict[str, str] = vars_read(vars_file) # A system without persistent GRUB storage - like VyOS running inside a # container - has no boot console we could detect. Neither has one whose # GRUB vars carry no console_type, e.g. a flavor that bakes no console data if 'console_type' not in vars_current: return # Check if VyOS installation uses serial boot console if vars_current['console_type'] == console_type: config.set(base + ['device', serial_console, 'kernel'])