summaryrefslogtreecommitdiff
path: root/python/vyos/system
diff options
context:
space:
mode:
Diffstat (limited to 'python/vyos/system')
-rw-r--r--python/vyos/system/compat.py22
-rw-r--r--python/vyos/system/grub.py38
2 files changed, 54 insertions, 6 deletions
diff --git a/python/vyos/system/compat.py b/python/vyos/system/compat.py
index 37b834ad6..1b487c1d2 100644
--- a/python/vyos/system/compat.py
+++ b/python/vyos/system/compat.py
@@ -198,11 +198,11 @@ def update_cfg_ver(root_dir:str = '') -> int:
return cfg_version
-def get_default(menu_entries: list, root_dir: str = '') -> Union[int, None]:
+def get_default(data: dict, root_dir: str = '') -> Union[int, None]:
"""Translate default version to menuentry index
Args:
- menu_entries (list): list of dicts of installed version boot data
+ data (dict): boot data
root_dir (str): an optional path to the root directory
Returns:
@@ -213,10 +213,22 @@ def get_default(menu_entries: list, root_dir: str = '') -> Union[int, None]:
grub_cfg_main = f'{root_dir}/{grub.GRUB_CFG_MAIN}'
+ menu_entries = data.get('versions', [])
+ console_type = data.get('console_type', 'tty')
+ console_num = data.get('console_num', '0')
image_name = image.get_default_image()
- sublist = list(filter(lambda x: x.get('version') == image_name,
- menu_entries))
+ sublist = list(filter(lambda x: (x.get('version') == image_name and
+ x.get('console_type') == console_type and
+ x.get('console_num') == console_num and
+ x.get('bootmode') == 'normal'),
+ menu_entries))
+ # legacy images added with legacy tools omitted 'ttyUSB'; if entry not
+ # available, default to initial entry of version
+ if not sublist:
+ sublist = list(filter(lambda x: x.get('version') == image_name,
+ menu_entries))
+
if sublist:
return menu_entries.index(sublist[0])
@@ -291,7 +303,7 @@ def grub_cfg_fields(root_dir: str = '') -> dict:
menu_entries = update_version_list(root_dir)
fields['versions'] = menu_entries
- default = get_default(menu_entries, root_dir)
+ default = get_default(fields, root_dir)
if default is not None:
fields['default'] = default
diff --git a/python/vyos/system/grub.py b/python/vyos/system/grub.py
index 2e8b20972..e56f0bec8 100644
--- a/python/vyos/system/grub.py
+++ b/python/vyos/system/grub.py
@@ -17,6 +17,7 @@ import platform
from pathlib import Path
from re import MULTILINE, compile as re_compile
+from shutil import copy2
from typing import Union
from uuid import uuid5, NAMESPACE_URL, UUID
@@ -373,7 +374,7 @@ def create_structure(root_dir: str = '') -> None:
if not root_dir:
root_dir = disk.find_persistence()
- Path(f'{root_dir}/GRUB_DIR_VYOS_VERS').mkdir(parents=True, exist_ok=True)
+ Path(f'{root_dir}/{GRUB_DIR_VYOS_VERS}').mkdir(parents=True, exist_ok=True)
def set_console_type(console_type: str, root_dir: str = '') -> None:
@@ -422,3 +423,38 @@ def set_kernel_cmdline_options(cmdline_options: str, version_name: str,
version_add(version_name=version_name, root_dir=root_dir,
boot_opts_config=cmdline_options)
+
+
+def sort_inodes(dir_path: str) -> None:
+ """Sort inodes for files inside a folder
+ Regenerate inodes for each file to get the same order for both inodes
+ and file names
+
+ GRUB iterates files by inodes, not alphabetically. Therefore, if we
+ want to read them in proper order, we need to sort inodes for all
+ config files in a folder.
+
+ Args:
+ dir_path (str): a path to directory
+ """
+ dir_content: list[Path] = sorted(Path(dir_path).iterdir())
+ temp_list_old: list[Path] = []
+ temp_list_new: list[Path] = []
+
+ # create a copy of all files, to get new inodes
+ for item in dir_content:
+ # skip directories
+ if item.is_dir():
+ continue
+ # create a new copy of file with a temporary name
+ copy_path = Path(f'{item.as_posix()}_tmp')
+ copy2(item, Path(copy_path))
+ temp_list_old.append(item)
+ temp_list_new.append(copy_path)
+
+ # delete old files and rename new ones
+ for item in temp_list_old:
+ item.unlink()
+ for item in temp_list_new:
+ new_name = Path(f'{item.as_posix()[0:-4]}')
+ item.rename(new_name)