summaryrefslogtreecommitdiff
path: root/src/migration-scripts/system/31-to-32
blob: 4f537cd41b358966004635bccd61182142933858 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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 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'])