-
Notifications
You must be signed in to change notification settings - Fork 235
transformers: add host kernel boot parameters transformer #4502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vyadavmsft
wants to merge
1
commit into
main
Choose a base branch
from
feature/host-kernel-boot-parameters
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| import re | ||
| import shlex | ||
| from dataclasses import dataclass, field | ||
| from typing import Any, Dict, List, Tuple, Type | ||
|
|
||
| from dataclasses_json import dataclass_json | ||
|
|
||
| from lisa import schema | ||
| from lisa.tools import GrubConfig | ||
| from lisa.transformers.deployment_transformer import ( | ||
| DeploymentTransformer, | ||
| DeploymentTransformerSchema, | ||
| ) | ||
| from lisa.util import LisaException, field_metadata | ||
|
|
||
| _KERNEL_ARG_NAME_PATTERN = re.compile(r"^[A-Za-z0-9_.-]+$") | ||
| _KERNEL_ARG_VALUE_PATTERN = re.compile(r"^[A-Za-z0-9_.,:/+%@=-]*$") | ||
|
|
||
|
|
||
| @dataclass_json | ||
| @dataclass | ||
| class HostKernelBootParametersTransformerSchema(DeploymentTransformerSchema): | ||
| parameters: str = field(default="", metadata=field_metadata(required=False)) | ||
| reboot_timeout: int = field(default=900, metadata=field_metadata(required=False)) | ||
|
|
||
|
|
||
| class HostKernelBootParameters(DeploymentTransformer): | ||
| @classmethod | ||
| def type_name(cls) -> str: | ||
| return "host_kernel_boot_parameters" | ||
|
|
||
| @classmethod | ||
| def type_schema(cls) -> Type[schema.TypedSchema]: | ||
| return HostKernelBootParametersTransformerSchema | ||
|
|
||
| @property | ||
| def _output_names(self) -> List[str]: | ||
| return [] | ||
|
|
||
| def _internal_run(self) -> Dict[str, Any]: | ||
| runbook: HostKernelBootParametersTransformerSchema = self.runbook | ||
| kernel_parameters = self._parse_parameters(runbook.parameters) | ||
|
|
||
| if not kernel_parameters: | ||
| self._log.info("No host kernel boot parameters were provided") | ||
| return {} | ||
|
|
||
| self._log.info( | ||
| "Applying host kernel boot parameters: " | ||
| f"{self._format_parameters(kernel_parameters)}" | ||
| ) | ||
|
|
||
| grub_config = self._node.tools[GrubConfig] | ||
| for parameter_name, parameter_value in kernel_parameters: | ||
| self._log.info( | ||
| f"Setting host kernel boot parameter " | ||
| f"'{parameter_name}={parameter_value}'" | ||
| ) | ||
| grub_config.set_kernel_cmdline_arg(parameter_name, parameter_value) | ||
|
|
||
| self._log.info( | ||
| f"Rebooting after setting host kernel boot parameters " | ||
| f"(timeout: {runbook.reboot_timeout}s)" | ||
| ) | ||
| self._node.reboot(time_out=runbook.reboot_timeout) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check whether the parameters are set properly? |
||
| return {} | ||
|
|
||
| def _parse_parameters(self, raw_parameters: str) -> List[Tuple[str, str]]: | ||
| raw_parameters = raw_parameters.strip() | ||
| if not raw_parameters: | ||
| return [] | ||
|
|
||
| try: | ||
| parameter_tokens = shlex.split(raw_parameters) | ||
| except ValueError as identifier: | ||
| raise LisaException( | ||
| f"Failed to parse host kernel boot parameters: {identifier}" | ||
| ) from identifier | ||
|
|
||
| kernel_parameters: List[Tuple[str, str]] = [] | ||
| for parameter_token in parameter_tokens: | ||
| if "=" not in parameter_token: | ||
| raise LisaException( | ||
| "Host kernel boot parameters must be whitespace-separated " | ||
| f"name=value pairs. Invalid parameter: '{parameter_token}'" | ||
| ) | ||
|
|
||
| parameter_name, parameter_value = parameter_token.split("=", 1) | ||
| if not _KERNEL_ARG_NAME_PATTERN.fullmatch(parameter_name): | ||
| raise LisaException( | ||
| "Host kernel boot parameter names may contain only letters, " | ||
| "numbers, underscores, dots, and hyphens. Invalid name: " | ||
| f"'{parameter_name}'" | ||
| ) | ||
| if not _KERNEL_ARG_VALUE_PATTERN.fullmatch(parameter_value): | ||
| raise LisaException( | ||
| "Host kernel boot parameter values may contain only letters, " | ||
| "numbers, underscores, dots, commas, colons, slashes, plus " | ||
| "signs, percent signs, at signs, equals signs, and hyphens. " | ||
| f"Invalid value for '{parameter_name}': '{parameter_value}'" | ||
| ) | ||
|
|
||
| kernel_parameters.append((parameter_name, parameter_value)) | ||
|
|
||
| return kernel_parameters | ||
|
|
||
| def _format_parameters(self, kernel_parameters: List[Tuple[str, str]]) -> str: | ||
| return " ".join( | ||
| f"{parameter_name}={parameter_value}" | ||
| for parameter_name, parameter_value in kernel_parameters | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.