Page MenuHomeVyOS Platform

No OneTemporary

Size
19 KB
Referenced Files
None
Subscribers
None
diff --git a/python/vyos/system/grub.py b/python/vyos/system/grub.py
index 0ac16af9a..4ebf229a0 100644
--- a/python/vyos/system/grub.py
+++ b/python/vyos/system/grub.py
@@ -1,340 +1,342 @@
# Copyright 2023 VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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/>.
from pathlib import Path
from re import MULTILINE, compile as re_compile
from typing import Union
from uuid import uuid5, NAMESPACE_URL, UUID
from vyos.template import render
from vyos.utils.process import cmd
from vyos.system import disk
# Define variables
GRUB_DIR_MAIN: str = '/boot/grub'
GRUB_CFG_MAIN: str = f'{GRUB_DIR_MAIN}/grub.cfg'
GRUB_DIR_VYOS: str = f'{GRUB_DIR_MAIN}/grub.cfg.d'
CFG_VYOS_HEADER: str = f'{GRUB_DIR_VYOS}/00-vyos-header.cfg'
CFG_VYOS_MODULES: str = f'{GRUB_DIR_VYOS}/10-vyos-modules-autoload.cfg'
CFG_VYOS_VARS: str = f'{GRUB_DIR_VYOS}/20-vyos-defaults-autoload.cfg'
CFG_VYOS_COMMON: str = f'{GRUB_DIR_VYOS}/25-vyos-common-autoload.cfg'
CFG_VYOS_PLATFORM: str = f'{GRUB_DIR_VYOS}/30-vyos-platform-autoload.cfg'
CFG_VYOS_MENU: str = f'{GRUB_DIR_VYOS}/40-vyos-menu-autoload.cfg'
CFG_VYOS_OPTIONS: str = f'{GRUB_DIR_VYOS}/50-vyos-options.cfg'
GRUB_DIR_VYOS_VERS: str = f'{GRUB_DIR_VYOS}/vyos-versions'
TMPL_VYOS_VERSION: str = 'grub/grub_vyos_version.j2'
TMPL_GRUB_VARS: str = 'grub/grub_vars.j2'
TMPL_GRUB_MAIN: str = 'grub/grub_main.j2'
TMPL_GRUB_MENU: str = 'grub/grub_menu.j2'
TMPL_GRUB_MODULES: str = 'grub/grub_modules.j2'
TMPL_GRUB_OPTS: str = 'grub/grub_options.j2'
TMPL_GRUB_COMMON: str = 'grub/grub_common.j2'
# prepare regexes
REGEX_GRUB_VARS: str = r'^set (?P<variable_name>.+)=[\'"]?(?P<variable_value>.*)(?<![\'"])[\'"]?$'
REGEX_GRUB_MODULES: str = r'^insmod (?P<module_name>.+)$'
REGEX_KERNEL_CMDLINE: str = r'^BOOT_IMAGE=/(?P<boot_type>boot|live)/((?P<image_version>.+)/)?vmlinuz.*$'
def install(drive_path: str, boot_dir: str, efi_dir: str, id: str = 'VyOS') -> None:
"""Install GRUB for both BIOS and EFI modes (hybrid boot)
Args:
drive_path (str): path to a drive where GRUB must be installed
boot_dir (str): a path to '/boot' directory
efi_dir (str): a path to '/boot/efi' directory
"""
commands: list[str] = [
f'grub-install --no-floppy --target=i386-pc --boot-directory={boot_dir} \
{drive_path} --force',
f'grub-install --no-floppy --recheck --target=x86_64-efi \
--force-extra-removable --boot-directory={boot_dir} \
--efi-directory={efi_dir} --bootloader-id="{id}" \
--no-uefi-secure-boot'
]
for command in commands:
cmd(command)
def gen_version_uuid(version_name: str) -> str:
"""Generate unique ID from version name
Use UUID5 / NAMESPACE_URL with prefix `uuid5-`
Args:
version_name (str): version name
Returns:
str: generated unique ID
"""
ver_uuid: UUID = uuid5(NAMESPACE_URL, version_name)
ver_id: str = f'uuid5-{ver_uuid}'
return ver_id
def version_add(version_name: str,
root_dir: str = '',
boot_opts: str = '') -> None:
"""Add a new VyOS version to GRUB loader configuration
Args:
vyos_version (str): VyOS version name
root_dir (str): an optional path to the root directory.
Defaults to empty.
boot_opts (str): an optional boot options for Linux kernel.
Defaults to empty.
"""
if not root_dir:
root_dir = disk.find_persistence()
version_config: str = f'{root_dir}/{GRUB_DIR_VYOS_VERS}/{version_name}.cfg'
render(
version_config, TMPL_VYOS_VERSION, {
'version_name': version_name,
'version_uuid': gen_version_uuid(version_name),
'boot_opts': boot_opts
})
def version_del(vyos_version: str, root_dir: str = '') -> None:
"""Delete a VyOS version from GRUB loader configuration
Args:
vyos_version (str): VyOS version name
root_dir (str): an optional path to the root directory.
Defaults to empty.
"""
if not root_dir:
root_dir = disk.find_persistence()
version_config: str = f'{root_dir}/{GRUB_DIR_VYOS_VERS}/{vyos_version}.cfg'
Path(version_config).unlink(missing_ok=True)
def version_list(root_dir: str = '') -> list[str]:
"""Generate a list with installed VyOS versions
Args:
root_dir (str): an optional path to the root directory.
Defaults to empty.
Returns:
list: A list with versions names
"""
if not root_dir:
root_dir = disk.find_persistence()
versions_files = Path(f'{root_dir}/{GRUB_DIR_VYOS_VERS}').glob('*.cfg')
versions_list: list[str] = []
for file in versions_files:
versions_list.append(file.stem)
+ versions_list.sort()
+
return versions_list
def read_env(env_file: str = '') -> dict[str, str]:
"""Read GRUB environment
Args:
env_file (str, optional): a path to grub environment file.
Defaults to empty.
Returns:
dict: dictionary with GRUB environment
"""
if not env_file:
root_dir: str = disk.find_persistence()
env_file = f'{root_dir}/{GRUB_DIR_MAIN}/grubenv'
env_content: str = cmd(f'grub-editenv {env_file} list').splitlines()
regex_filter = re_compile(r'^(?P<variable_name>.*)=(?P<variable_value>.*)$')
env_dict: dict[str, str] = {}
for env_item in env_content:
search_result = regex_filter.fullmatch(env_item)
if search_result:
search_result_dict: dict[str, str] = search_result.groupdict()
variable_name: str = search_result_dict.get('variable_name', '')
variable_value: str = search_result_dict.get('variable_value', '')
if variable_name and variable_value:
env_dict.update({variable_name: variable_value})
return env_dict
def get_cfg_ver(root_dir: str = '') -> int:
"""Get current version of GRUB configuration
Args:
root_dir (str, optional): an optional path to the root directory.
Defaults to empty.
Returns:
int: a configuration version
"""
if not root_dir:
root_dir = disk.find_persistence()
cfg_ver: str = vars_read(f'{root_dir}/{CFG_VYOS_HEADER}').get(
'VYOS_CFG_VER')
if cfg_ver:
cfg_ver_int: int = int(cfg_ver)
else:
cfg_ver_int: int = 0
return cfg_ver_int
def write_cfg_ver(cfg_ver: int, root_dir: str = '') -> None:
"""Write version number of GRUB configuration
Args:
cfg_ver (int): a version number to write
root_dir (str, optional): an optional path to the root directory.
Defaults to empty.
Returns:
int: a configuration version
"""
if not root_dir:
root_dir = disk.find_persistence()
vars_file: str = f'{root_dir}/{CFG_VYOS_HEADER}'
vars_current: dict[str, str] = vars_read(vars_file)
vars_current['VYOS_CFG_VER'] = str(cfg_ver)
vars_write(vars_file, vars_current)
def vars_read(grub_cfg: str) -> dict[str, str]:
"""Read variables from a GRUB configuration file
Args:
grub_cfg (str): a path to the GRUB config file
Returns:
dict: a dictionary with variables and values
"""
vars_dict: dict[str, str] = {}
regex_filter = re_compile(REGEX_GRUB_VARS)
try:
config_text: list[str] = Path(grub_cfg).read_text().splitlines()
except FileNotFoundError:
return vars_dict
for line in config_text:
search_result = regex_filter.fullmatch(line)
if search_result:
search_dict = search_result.groupdict()
variable_name: str = search_dict.get('variable_name', '')
variable_value: str = search_dict.get('variable_value', '')
if variable_name and variable_value:
vars_dict.update({variable_name: variable_value})
return vars_dict
def modules_read(grub_cfg: str) -> list[str]:
"""Read modules list from a GRUB configuration file
Args:
grub_cfg (str): a path to the GRUB config file
Returns:
list: a list with modules to load
"""
mods_list: list[str] = []
regex_filter = re_compile(REGEX_GRUB_MODULES, MULTILINE)
try:
config_text = Path(grub_cfg).read_text()
except FileNotFoundError:
return mods_list
mods_list = regex_filter.findall(config_text)
return mods_list
def modules_write(grub_cfg: str, mods_list: list[str]) -> None:
"""Write modules list to a GRUB configuration file (overwrite everything)
Args:
grub_cfg (str): a path to GRUB configuration file
mods_list (list): a list with modules to load
"""
render(grub_cfg, TMPL_GRUB_MODULES, {'mods_list': mods_list})
def vars_write(grub_cfg: str, grub_vars: dict[str, str]) -> None:
"""Write variables to a GRUB configuration file (overwrite everything)
Args:
grub_cfg (str): a path to GRUB configuration file
grub_vars (dict): a dictionary with new variables
"""
render(grub_cfg, TMPL_GRUB_VARS, {'vars': grub_vars})
def set_default(version_name: str, root_dir: str = '') -> None:
"""Set version as default boot entry
Args:
version_name (str): versio name
root_dir (str, optional): an optional path to the root directory.
Defaults to empty.
"""
if not root_dir:
root_dir = disk.find_persistence()
vars_file = f'{root_dir}/{CFG_VYOS_VARS}'
vars_current = vars_read(vars_file)
vars_current['default'] = gen_version_uuid(version_name)
vars_write(vars_file, vars_current)
def common_write(root_dir: str = '', grub_common: dict[str, str] = {}) -> None:
"""Write common GRUB configuration file (overwrite everything)
Args:
root_dir (str, optional): an optional path to the root directory.
Defaults to empty.
"""
if not root_dir:
root_dir = disk.find_persistence()
common_config = f'{root_dir}/{CFG_VYOS_COMMON}'
render(common_config, TMPL_GRUB_COMMON, grub_common)
def create_structure(root_dir: str = '') -> None:
"""Create GRUB directories structure
Args:
root_dir (str, optional): an optional path to the root directory.
Defaults to ''.
"""
if not root_dir:
root_dir = disk.find_persistence()
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:
"""Write default console type to GRUB configuration
Args:
console_type (str): a default console type
root_dir (str, optional): an optional path to the root directory.
Defaults to empty.
"""
if not root_dir:
root_dir = disk.find_persistence()
vars_file: str = f'{root_dir}/{CFG_VYOS_VARS}'
vars_current: dict[str, str] = vars_read(vars_file)
vars_current['console_type'] = str(console_type)
vars_write(vars_file, vars_current)
def set_raid(root_dir: str = '') -> None:
pass
diff --git a/src/op_mode/image_manager.py b/src/op_mode/image_manager.py
index 76e414239..e75485f9f 100755
--- a/src/op_mode/image_manager.py
+++ b/src/op_mode/image_manager.py
@@ -1,212 +1,210 @@
#!/usr/bin/env python3
#
# Copyright 2023 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This file is part of VyOS.
#
# VyOS is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# VyOS 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 General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# VyOS. If not, see <https://www.gnu.org/licenses/>.
from argparse import ArgumentParser, Namespace
from pathlib import Path
from shutil import rmtree
from sys import exit
from typing import Optional
from vyos.system import disk, grub, image, compat
from vyos.utils.io import ask_yes_no, select_entry
SET_IMAGE_LIST_MSG: str = 'The following images are available:'
SET_IMAGE_PROMPT_MSG: str = 'Select an image to set as default:'
DELETE_IMAGE_LIST_MSG: str = 'The following images are installed:'
DELETE_IMAGE_PROMPT_MSG: str = 'Select an image to delete:'
MSG_DELETE_IMAGE_RUNNING: str = 'Currently running image cannot be deleted; reboot into another image first'
MSG_DELETE_IMAGE_DEFAULT: str = 'Default image cannot be deleted; set another image as default first'
@compat.grub_cfg_update
def delete_image(image_name: Optional[str] = None,
no_prompt: bool = False) -> None:
"""Remove installed image files and boot entry
Args:
image_name (str): a name of image to delete
"""
available_images: list[str] = grub.version_list()
if image_name is None:
if no_prompt:
exit('An image name is required for delete action')
else:
image_name = select_entry(available_images,
DELETE_IMAGE_LIST_MSG,
DELETE_IMAGE_PROMPT_MSG)
if image_name == image.get_running_image():
exit(MSG_DELETE_IMAGE_RUNNING)
if image_name == image.get_default_image():
exit(MSG_DELETE_IMAGE_DEFAULT)
if image_name not in available_images:
exit(f'The image "{image_name}" cannot be found')
persistence_storage: str = disk.find_persistence()
if not persistence_storage:
exit('Persistence storage cannot be found')
if (not no_prompt and
not ask_yes_no(f'Do you really want to delete the image {image_name}?',
default=False)):
exit()
# remove files and menu entry
version_path: Path = Path(f'{persistence_storage}/boot/{image_name}')
try:
rmtree(version_path)
grub.version_del(image_name, persistence_storage)
print(f'The image "{image_name}" was successfully deleted')
except Exception as err:
exit(f'Unable to remove the image "{image_name}": {err}')
@compat.grub_cfg_update
def set_image(image_name: Optional[str] = None,
prompt: bool = True) -> None:
"""Set default boot image
Args:
image_name (str): an image name
"""
available_images: list[str] = grub.version_list()
if image_name is None:
if not prompt:
exit('An image name is required for set action')
else:
image_name = select_entry(available_images,
SET_IMAGE_LIST_MSG,
SET_IMAGE_PROMPT_MSG)
if image_name == image.get_default_image():
exit(f'The image "{image_name}" already configured as default')
if image_name not in available_images:
exit(f'The image "{image_name}" cannot be found')
persistence_storage: str = disk.find_persistence()
if not persistence_storage:
exit('Persistence storage cannot be found')
# set default boot image
try:
grub.set_default(image_name, persistence_storage)
print(f'The image "{image_name}" is now default boot image')
except Exception as err:
exit(f'Unable to set default image "{image_name}": {err}')
@compat.grub_cfg_update
def rename_image(name_old: str, name_new: str) -> None:
"""Rename installed image
Args:
name_old (str): old name
name_new (str): new name
"""
if name_old == image.get_running_image():
exit('Currently running image cannot be renamed')
available_images: list[str] = grub.version_list()
if name_old not in available_images:
exit(f'The image "{name_old}" cannot be found')
if name_new in available_images:
exit(f'The image "{name_new}" already exists')
if not image.validate_name(name_new):
exit(f'The image name "{name_new}" is not allowed')
persistence_storage: str = disk.find_persistence()
if not persistence_storage:
exit('Persistence storage cannot be found')
if not ask_yes_no(
f'Do you really want to rename the image {name_old} '
f'to the {name_new}?',
default=False):
exit()
try:
# replace default boot item
if name_old == image.get_default_image():
grub.set_default(name_new, persistence_storage)
# rename files and dirs
old_path: Path = Path(f'{persistence_storage}/boot/{name_old}')
new_path: Path = Path(f'{persistence_storage}/boot/{name_new}')
old_path.rename(new_path)
# replace boot item
grub.version_del(name_old, persistence_storage)
grub.version_add(name_new, persistence_storage)
print(f'The image "{name_old}" was renamed to "{name_new}"')
except Exception as err:
exit(f'Unable to rename image "{name_old}" to "{name_new}": {err}')
def list_images() -> None:
"""Print list of available images for CLI hints"""
images_list: list[str] = grub.version_list()
- images_list.sort()
-
for image_name in images_list:
print(image_name)
def parse_arguments() -> Namespace:
"""Parse arguments
Returns:
Namespace: a namespace with parsed arguments
"""
parser: ArgumentParser = ArgumentParser(description='Manage system images')
parser.add_argument('--action',
choices=['delete', 'set', 'rename', 'list'],
required=True,
help='action to perform with an image')
parser.add_argument('--no-prompt', action='store_true',
help='perform action non-interactively')
parser.add_argument(
'--image-name',
help=
'a name of an image to add, delete, install, rename, or set as default')
parser.add_argument('--image-new-name', help='a new name for image')
args: Namespace = parser.parse_args()
# Validate arguments
if args.action == 'rename' and (not args.image_name or
not args.image_new_name):
exit('Both old and new image names are required for rename action')
return args
if __name__ == '__main__':
try:
args: Namespace = parse_arguments()
if args.action == 'delete':
delete_image(args.image_name, args.no_prompt)
if args.action == 'set':
set_image(args.image_name)
if args.action == 'rename':
rename_image(args.image_name, args.image_new_name)
if args.action == 'list':
list_images()
exit()
except KeyboardInterrupt:
print('Stopped by Ctrl+C')
exit()
except Exception as err:
exit(f'{err}')

File Metadata

Mime Type
text/x-diff
Expires
Thu, Nov 26, 6:53 PM (8 h, 19 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4919236
Default Alt Text
(19 KB)

Event Timeline