summaryrefslogtreecommitdiff
path: root/src/migration-scripts/system/31-to-32
blob: 89b604a7f6064bbc890f7dfc79ac6ba4585e29a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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.flavor import get_image_serial_console
from vyos.system import disk
from vyos.system.grub import CFG_VYOS_VARS
from vyos.system.grub import vars_read

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}'

    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:
        if config.exists(base + ['device', serial_console]):
            config.set(base + ['device', serial_console, 'kernel'])