Skip to content

Commit

Permalink
Add support for emulated TPM into virtual provision plugin
Browse files Browse the repository at this point in the history
Based on code introduced in #2078,
but honoring HW requirements.
  • Loading branch information
happz committed Feb 19, 2024
1 parent b7d75ce commit f9842b2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 17 deletions.
2 changes: 2 additions & 0 deletions spec/hardware/tpm.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ example:

link:
- implemented-by: /tmt/steps/provision/artemis.py
- implemented-by: /tmt/steps/provision/testcloud.py
note: "``version: 2.0`` only"
92 changes: 75 additions & 17 deletions tmt/steps/provision/testcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,32 @@ def normalize_disk_size(key_address: str, value: Any, logger: tmt.log.Logger) ->
raise tmt.utils.NormalizationError(key_address, value, 'an integer')


def _report_hw_requirement_support(constraint: tmt.hardware.Constraint[Any]) -> bool:
components = constraint.expand_name()

if components.name == 'memory' \
and constraint.operator in (tmt.hardware.Operator.EQ,
tmt.hardware.Operator.GTE,
tmt.hardware.Operator.LTE):
return True

if components.name == 'disk' \
and components.child_name == 'size' \
and constraint.operator in (tmt.hardware.Operator.EQ,
tmt.hardware.Operator.GTE,
tmt.hardware.Operator.LTE):
return True

if components.name == 'tpm' \
and components.child_name == 'version' \
and constraint.value == '2.0' \
and constraint.operator in (tmt.hardware.Operator.EQ,
tmt.hardware.Operator.GTE):
return True

return False


@dataclasses.dataclass
class TestcloudGuestData(tmt.steps.provision.GuestSshData):
# Override parent class with our defaults
Expand Down Expand Up @@ -512,6 +538,53 @@ def _combine_hw_disk_size(self) -> None:

self.hardware.and_(disk_size_constraint)

def _apply_hw_tpm(self, domain: 'DomainConfiguration') -> None:
""" Apply ``tpm`` constraint to given VM domain """

if not self.hardware or not self.hardware.constraint:
self.debug(
'tpm.version',
"not included because of no constraints",
level=4)

return

variant = self.hardware.constraint.variant()

tpm_constraints = [
constraint
for constraint in variant
if isinstance(constraint, tmt.hardware.TextConstraint)
and constraint.expand_name().name == 'tpm'
and constraint.expand_name().child_name == 'version']

if not tpm_constraints:
self.debug(
'tpm.version',
"not included because of no 'tpm.version' constraints",
level=4)

return

for constraint in tpm_constraints:
if constraint.operator != tmt.hardware.Operator.EQ:
self.warn(
f"Cannot apply hardware requirement '{constraint}', operator not supported.")
return

if constraint.value != '2.0':
self.warn(
f"Cannot apply hardware requirement '{constraint}',"
" TPM version not supported.")
return

self.debug(
'.version',
f"set to '{constraint.value}' because of '{constraint}'",
level=4)

domain.tpm_configuration = TPMConfiguration()

def _apply_hw_memory(self, domain: 'DomainConfiguration') -> None:
""" Apply ``memory`` constraint to given VM domain """

Expand Down Expand Up @@ -671,6 +744,7 @@ def start(self) -> None:

self._apply_hw_memory(self._domain)
storage_image = self._apply_hw_disk_size(self._domain)
self._apply_hw_tpm(self._domain)

self.debug('final domain memory', str(self._domain.memory_size))
self.debug('final domain disk size', str(storage_image.size))
Expand Down Expand Up @@ -915,23 +989,7 @@ def go(self) -> None:
data.show(verbose=self.verbosity_level, logger=self._logger)

if data.hardware and data.hardware.constraint:
def _report_support(constraint: tmt.hardware.Constraint[Any]) -> bool:
if constraint.expand_name().name == 'memory' \
and constraint.operator in (tmt.hardware.Operator.EQ,
tmt.hardware.Operator.GTE,
tmt.hardware.Operator.LTE):
return True

if constraint.expand_name().name == 'disk' \
and constraint.expand_name().child_name == 'size' \
and constraint.operator in (tmt.hardware.Operator.EQ,
tmt.hardware.Operator.GTE,
tmt.hardware.Operator.LTE):
return True

return False

data.hardware.report_support(check=_report_support, logger=self._logger)
data.hardware.report_support(check=_report_hw_requirement_support, logger=self._logger)

for line in data.hardware.format_variants():
self._logger.debug('hardware', line, level=4)
Expand Down

0 comments on commit f9842b2

Please sign in to comment.