Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions data/config.boot.default
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ system {
config-management {
commit-revisions "100"
}
console {
device ttyS0 {
speed "115200"
}
}
host-name "vyos"
login {
operator-group default {
Expand Down
2 changes: 1 addition & 1 deletion interface-definitions/include/version/system-version.xml.i
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- include start from include/version/system-version.xml.i -->
<syntaxVersion component='system' version='31'></syntaxVersion>
<syntaxVersion component='system' version='32'></syntaxVersion>
<!-- include end -->
16 changes: 13 additions & 3 deletions interface-definitions/system_console.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
<properties>
<help>Serial console device name</help>
<completionHelp>
<script>ls -1 /dev | grep -e ttyS -e hvc</script>
<script>ls -1 /dev | grep -e ttyS -e ttyAMA -e hvc</script>
<script>if [ -d /dev/serial/by-bus ]; then ls -1 /dev/serial/by-bus; fi</script>
</completionHelp>
<valueHelp>
<format>ttySN</format>
<description>TTY device name, regular serial port</description>
<description>TTY device name, ttyS based</description>
</valueHelp>
<valueHelp>
<format>ttyAMAN</format>
<description>TTY device name, ttyAMA based</description>
</valueHelp>
<valueHelp>
<format>usbNbXpY</format>
Expand All @@ -28,10 +32,16 @@
<description>Xen console</description>
</valueHelp>
<constraint>
<regex>(ttyS[0-9]+|ttyAMA[0-9]+|hvc[0-9]+|usb[0-9]+b.*)</regex>
<regex>(ttyS[0-9]+|ttyAMA[0-9]+|hvc[0-9]+|usb[0-9]+b.*)</regex>
</constraint>
</properties>
<children>
<leafNode name="kernel">
<properties>
<help>Use the console as an output for kernel messages</help>
<valueless/>
</properties>
</leafNode>
<leafNode name="speed">
<properties>
<help>Console baud rate</help>
Expand Down
69 changes: 69 additions & 0 deletions python/vyos/flavor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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/>.

"""
VyOS flavor data access library.

VyOS stores its flavor specific data in a JSON file. This module provides a
convenient interface to reading it.

Example of the version data dict::
{
'console_type': 'ttyS',
'console_num': '0',
'console_speed': '115200'
}
"""

import os
import vyos.defaults

from vyos.utils.file import read_json

flavor_file = os.path.join(vyos.defaults.directories['data'], 'flavor.json')

def get_flavor_data(fname=flavor_file):
"""
Get complete flavor data

Args:
file (str): path to the flavor file

Returns:
dict: flavor data, if it can not be found and empty dict

The optional ``file`` argument comes in handy in upgrade scripts
that need to retrieve information from images other than the running image.
It should not be used on a running system since the location of that file
is an implementation detail and may change in the future, while the interface
of this module will stay the same.
"""
return read_json(flavor_file, {})

def get_image_serial_console(fname=flavor_file):
"""
Get serial console parameters baked into the image flavor.

Args:
file (str): path to the flavor file

Returns:
dict: serial interface data baked into the image flavor. Example:
{"console_type": "ttyS", "console_speed": "115200", "console_num":"0"}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong order.

"""
console_type = get_flavor_data(fname=fname).get('console_type', '')
console_num = get_flavor_data(fname=fname).get('console_num', '')
console_speed = get_flavor_data(fname=fname).get('console_speed', '')
Comment on lines +66 to +68
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we read the file once?

return (console_type, console_num, console_speed)
16 changes: 13 additions & 3 deletions python/vyos/system/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@
# along with this library. If not, see <http://www.gnu.org/licenses/>.

from pathlib import Path
from re import compile, MULTILINE, DOTALL
from re import compile
from re import MULTILINE
from re import DOTALL
from functools import wraps
from copy import deepcopy
from typing import Union

from vyos.system import disk, grub, image, SYSTEM_CFG_VER
from vyos.flavor import get_image_serial_console
from vyos.system import disk
from vyos.system import grub
from vyos.system import image
from vyos.system import SYSTEM_CFG_VER
from vyos.template import render

TMPL_GRUB_COMPAT: str = 'grub/grub_compat.j2'
Expand Down Expand Up @@ -128,11 +134,15 @@ def parse_entry(entry: tuple) -> dict:
entry_dict['bootmode'] = 'pw_reset'
else:
entry_dict['bootmode'] = 'normal'
(_, _, default_speed) = get_image_serial_console()
# find console type and number
regex_filter = compile(REGEX_CONSOLE)
entry_dict.update(regex_filter.match(entry[1]).groupdict())
# Set new or default console speed - this line must always be present to
# keep backward compatibility. It is needed to boot into old images and
# use the serial console speed
speed = entry_dict.get('console_speed', None)
entry_dict['console_speed'] = speed if speed is not None else '115200'
entry_dict['console_speed'] = speed if speed is not None else default_speed
entry_dict['boot_opts'] = sanitize_boot_opts(entry[1])

return entry_dict
Expand Down
26 changes: 19 additions & 7 deletions python/vyos/system/grub.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@
import platform

from pathlib import Path
from re import MULTILINE, compile as re_compile
from re import MULTILINE
from re import compile as re_compile
from shutil import copy2
from uuid import uuid5, NAMESPACE_URL, UUID
from uuid import uuid5
from uuid import NAMESPACE_URL
from uuid import UUID

from vyos.template import render
from vyos.utils.process import cmd, rc_cmd
from vyos.flavor import get_image_serial_console
from vyos.system import disk
from vyos.template import render
from vyos.utils.process import cmd
from vyos.utils.process import rc_cmd

# Define variables
GRUB_DIR_MAIN: str = '/boot/grub'
Expand Down Expand Up @@ -385,7 +390,7 @@ def set_console_type(console_type: str, root_dir: str = '') -> None:
"""Write default console type to GRUB configuration

Args:
console_type (str): a default console type
console_type (str): GRUB default console type, e.g. tty, ttyS or ttyAMA
root_dir (str, optional): an optional path to the root directory.
Defaults to empty.
"""
Expand All @@ -397,20 +402,27 @@ def set_console_type(console_type: str, root_dir: str = '') -> None:
vars_current['console_type'] = str(console_type)
vars_write(vars_file, vars_current)

def set_console_speed(console_speed: str, root_dir: str = '') -> None:
def set_serial_console(console_type: str, console_num: str,
console_speed: str, root_dir: str = '') -> None:
"""Write default console speed to GRUB configuration

Args:
console_type (str): console device, e.g. 'ttyS' or 'ttyAMA'
console_num (str): console instance, e.g. '0'
console_speed (str): default console speed
root_dir (str, optional): an optional path to the root directory.
Defaults to empty.
"""
if not root_dir:
root_dir = disk.find_persistence()

(default_type, default_num, default_speed) = get_image_serial_console()

vars_file: str = f'{root_dir}/{CFG_VYOS_VARS}'
vars_current: dict[str, str] = vars_read(vars_file)
vars_current['console_speed'] = str(console_speed)
vars_current['console_type'] = console_type if console_type else default_type
vars_current['console_num'] = console_num if console_num else default_num
vars_current['console_speed'] = console_speed if console_speed else default_speed
vars_write(vars_file, vars_current)

def set_kernel_cmdline_options(cmdline_options: str, version_name: str,
Expand Down
25 changes: 18 additions & 7 deletions python/vyos/system/grub_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
# 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.system import disk, grub, image, compat
from vyos.system import disk
from vyos.system import grub
from vyos.system import image
from vyos.system import compat

@compat.grub_cfg_update
def set_console_speed(console_speed: str, root_dir: str = '') -> None:
def set_serial_console(console_type: str, console_num: str,
console_speed: str, root_dir: str = '') -> None:
"""Write default console speed to GRUB configuration

Args:
Expand All @@ -27,20 +31,27 @@ def set_console_speed(console_speed: str, root_dir: str = '') -> None:
if not root_dir:
root_dir = disk.find_persistence()

grub.set_console_speed(console_speed, root_dir)
grub.set_serial_console(console_type, console_num, console_speed, root_dir)

@image.if_not_live_boot
def update_console_speed(console_speed: str, root_dir: str = '') -> None:
def update_serial_console(console_type: str, console_num: str,
console_speed: str, root_dir: str = '') -> None:
"""Update console_speed if different from current value"""

if not root_dir:
root_dir = disk.find_persistence()

vars_file: str = f'{root_dir}/{grub.CFG_VYOS_VARS}'
vars_current: dict[str, str] = grub.vars_read(vars_file)
console_speed_current = vars_current.get('console_speed', None)
if console_speed != console_speed_current:
set_console_speed(console_speed, root_dir)

console_type_current = vars_current.get('console_type')
console_num_current = vars_current.get('console_num')
console_speed_current = vars_current.get('console_speed')

if console_type != console_type_current or \
console_num != console_num_current or \
console_speed != console_speed_current:
set_serial_console(console_type, console_num, console_speed, root_dir)

@compat.grub_cfg_update
def set_kernel_cmdline_options(cmdline_options: str, version: str = '',
Expand Down
26 changes: 24 additions & 2 deletions python/vyos/utils/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
# License along with this library. If not, see <http://www.gnu.org/licenses/>.

import os
from typing import Tuple
from typing import Optional

# A list of used Kernel constants
# https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/drivers/net/wireguard/messages.h?h=linux-6.6.y#n45
WIREGUARD_REKEY_AFTER_TIME = 120


def load_module(name: str, quiet: bool = True, dry_run: bool = False) -> int:
"""Load a kernel module via modprobe.

Expand All @@ -39,7 +40,6 @@ def load_module(name: str, quiet: bool = True, dry_run: bool = False) -> int:
cmd.append(name)
return run(cmd)


def unload_module(name: str) -> int:
"""Unload a kernel module via rmmod.

Expand Down Expand Up @@ -150,3 +150,25 @@ def lsmod():
for m in list_loaded_modules():
mods_data.append(get_module_data(m))
return mods_data

def get_kernel_serial_console() -> Tuple[Optional[str], Optional[str], Optional[str]]:
"""
Extract the serial console type, number, and speed setting from the kernel
command line which was used during system boot.
"""
import re
from vyos.utils.file import read_file

cmdline_console_re = re.compile(
r'(?:^|\s)console=(?P<console_type>tty(?:S|AMA))(?P<console_num>\d+),(?P<console_speed>\d+)(?=\s|$)'
)

kernel_cmdline = read_file('/proc/cmdline')
if m := cmdline_console_re.search(kernel_cmdline):
return (
m.group('console_type'),
m.group('console_num'),
m.group('console_speed'),
)

return (None, None, None)
9 changes: 7 additions & 2 deletions python/vyos/utils/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
# 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 os, re, json
import os
import re
import json
from typing import List

from vyos.base import Warning
Expand Down Expand Up @@ -117,7 +119,7 @@ def restart_login_consoles(prompt_user=False, quiet=True, devices: List[str]=[])

return True

def is_tty(name: str) -> bool:
def is_tty(name: str, warning=False) -> bool:
""" Check if a given device file (e.g. /dev/ttyS0) is a TTY (teletypewriter)
device in Linux
"""
Expand All @@ -128,4 +130,7 @@ def is_tty(name: str) -> bool:
fd = f.fileno()
# True if filename is a TTY
return os.isatty(fd)
elif warning:
from vyos.base import Warning
Warning(f'Device "{name}" does not exist!')
return False
1 change: 1 addition & 0 deletions smoketest/configs/assert/basic-api-service
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ set system conntrack modules pptp
set system conntrack modules sip
set system conntrack modules sqlnet
set system conntrack modules tftp
set system console device ttyS0 kernel
set system console device ttyS0 speed '115200'
set system host-name 'vyos'
set system login user vyos authentication encrypted-password '$6$2Ta6TWHd/U$NmrX0x9kexCimeOcYK1MfhMpITF9ELxHcaBU/znBq.X2ukQOj61fVI2UYP/xBzP4QtiTcdkgs7WOQMHWsRymO/'
Expand Down
1 change: 1 addition & 0 deletions smoketest/configs/assert/basic-haproxy
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ set system conntrack modules pptp
set system conntrack modules sip
set system conntrack modules sqlnet
set system conntrack modules tftp
set system console device ttyS0 kernel
set system console device ttyS0 speed '115200'
set system host-name 'vyos'
set system login user vyos authentication encrypted-password '$6$O5gJRlDYQpj$MtrCV9lxMnZPMbcxlU7.FI793MImNHznxGoMFgm3Q6QP3vfKJyOSRCt3Ka/GzFQyW1yZS4NS616NLHaIPPFHc0'
Expand Down
1 change: 1 addition & 0 deletions smoketest/configs/assert/basic-ipv6
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ set system login user vyos authentication plaintext-password ''
set system name-server '2001:db8::1'
set system name-server '2001:db8::2'
set system syslog local facility all level 'debug'
set system console device ttyS0 kernel
set system console device ttyS0 speed '115200'
1 change: 1 addition & 0 deletions smoketest/configs/assert/basic-syslog
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set interfaces ethernet eth1 address '172.16.33.154/24'
set interfaces ethernet eth1 duplex 'auto'
set interfaces ethernet eth1 speed 'auto'
set interfaces ethernet eth1 vrf 'red'
set system console device ttyS0 kernel
set system console device ttyS0 speed '115200'
set system domain-name 'vyos-ci-test.net'
set system host-name 'vyos'
Expand Down
2 changes: 2 additions & 0 deletions smoketest/configs/assert/basic-system-ip-protocol
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
set system console device ttyS0 kernel
set system console device ttyS0 speed '115200'
set system ip protocol any route-map 'stub'
set system ip protocol babel route-map 'stub'
set system ip protocol bgp route-map 'stub'
Expand Down
1 change: 1 addition & 0 deletions smoketest/configs/assert/basic-vyos
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ set system conntrack modules pptp
set system conntrack modules sip
set system conntrack modules sqlnet
set system conntrack modules tftp
set system console device ttyS0 kernel
set system console device ttyS0 speed '115200'
set system host-name 'vyos'
set system login user vyos authentication encrypted-password '$6$O5gJRlDYQpj$MtrCV9lxMnZPMbcxlU7.FI793MImNHznxGoMFgm3Q6QP3vfKJyOSRCt3Ka/GzFQyW1yZS4NS616NLHaIPPFHc0'
Expand Down
Loading
Loading