Skip to content

Commit

Permalink
Libvirt: Implement _get_environment_information
Browse files Browse the repository at this point in the history
Add following fields as information.
* Host Distro
* Host Kernel Version
* Libvirt Version
* VMM (qemu/ch) Version
  • Loading branch information
pupacha authored and squirrelsc committed Jul 22, 2022
1 parent 8c59a62 commit afec5a0
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
18 changes: 17 additions & 1 deletion lisa/sut_orchestrator/libvirt/ch_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under the MIT license.

import os
import re
import xml.etree.ElementTree as ET # noqa: N817
from pathlib import Path
from typing import List, Type
Expand All @@ -15,12 +16,14 @@
from lisa.sut_orchestrator.libvirt.context import NodeContext, get_node_context
from lisa.sut_orchestrator.libvirt.platform import BaseLibvirtPlatform
from lisa.tools import QemuImg
from lisa.util.logger import Logger
from lisa.util.logger import Logger, filter_ansi_escape

from .. import CLOUD_HYPERVISOR
from .console_logger import QemuConsoleLogger
from .schema import BaseLibvirtNodeSchema, CloudHypervisorNodeSchema, DiskImageFormat

CH_VERSION_PATTERN = re.compile(r"cloud-hypervisor (?P<ch_version>.+)")


class CloudHypervisorPlatform(BaseLibvirtPlatform):
@classmethod
Expand Down Expand Up @@ -193,3 +196,16 @@ def _create_node_os_disk(
expected_exit_code=0,
expected_exit_code_failure_message="Failed to copy os disk image",
)

def _get_vmm_version(self) -> str:
result = "Unknown"
if self.host_node:
output = self.host_node.execute(
"cloud-hypervisor --version",
shell=True,
).stdout
output = filter_ansi_escape(output)
match = re.search(CH_VERSION_PATTERN, output.strip())
if match:
result = match.group("ch_version")
return result
54 changes: 52 additions & 2 deletions lisa/sut_orchestrator/libvirt/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
from lisa.feature import Feature
from lisa.node import Node, RemoteNode, local_node_connect
from lisa.platform_ import Platform
from lisa.tools import Iptables, QemuImg
from lisa.tools import Iptables, QemuImg, Uname
from lisa.util import LisaException, constants, get_public_key_data
from lisa.util.logger import Logger
from lisa.util.logger import Logger, filter_ansi_escape

from . import libvirt_events_thread
from .console_logger import QemuConsoleLogger
Expand All @@ -46,6 +46,12 @@
)
from .serial_console import SerialConsole

# Host environment information fields
KEY_HOST_DISTRO = "host_distro"
KEY_HOST_KERNEL = "host_kernel_version"
KEY_LIBVIRT_VERSION = "libvirt_version"
KEY_VMM_VERSION = "vmm_version"


class _HostCapabilities:
def __init__(self) -> None:
Expand All @@ -65,6 +71,13 @@ def __init__(self, runbook: schema.Platform) -> None:
self.host_node: Node
self.vm_disks_dir: str

self._host_environment_information_hooks = {
KEY_HOST_DISTRO: self._get_host_distro,
KEY_HOST_KERNEL: self._get_host_kernel_version,
KEY_LIBVIRT_VERSION: self._get_libvirt_version,
KEY_VMM_VERSION: self._get_vmm_version,
}

@classmethod
def type_name(cls) -> str:
return ""
Expand Down Expand Up @@ -1072,3 +1085,40 @@ def __node_runbook_type(self) -> type:
node_runbook_type: type = type(self).node_runbook_type()
assert issubclass(node_runbook_type, BaseLibvirtNodeSchema)
return node_runbook_type

def _get_host_distro(self) -> str:
result = self.host_node.os.information.full_version if self.host_node else ""
return result

def _get_host_kernel_version(self) -> str:
result = ""
if self.host_node:
uname = self.host_node.tools[Uname]
result = uname.get_linux_information().kernel_version_raw
return result

def _get_libvirt_version(self) -> str:
result = ""
if self.host_node:
result = self.host_node.execute("libvirtd --version", shell=True).stdout
result = filter_ansi_escape(result)
return result

def _get_vmm_version(self) -> str:
return "Unknown"

def _get_environment_information(self, environment: Environment) -> Dict[str, str]:
information: Dict[str, str] = {}

if self.host_node:
node: Node = self.host_node
for key, method in self._host_environment_information_hooks.items():
node.log.debug(f"detecting {key} ...")
try:
value = method()
if value:
information[key] = value
except Exception as identifier:
node.log.exception(f"error on get {key}.", exc_info=identifier)

return information
18 changes: 17 additions & 1 deletion lisa/sut_orchestrator/libvirt/qemu_platform.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

import re
from typing import List, Type

from lisa.environment import Environment
Expand All @@ -9,11 +10,13 @@
from lisa.sut_orchestrator.libvirt.context import get_node_context
from lisa.sut_orchestrator.libvirt.platform import BaseLibvirtPlatform
from lisa.tools import QemuImg
from lisa.util.logger import Logger
from lisa.util.logger import Logger, filter_ansi_escape

from .. import QEMU
from .schema import QemuNodeSchema

QEMU_VERSION_PATTERN = re.compile(r"QEMU emulator version (?P<qemu_version>.+)\s")


class QemuPlatform(BaseLibvirtPlatform):
@classmethod
Expand All @@ -39,3 +42,16 @@ def _create_node_os_disk(
self.host_node.tools[QemuImg].create_diff_qcow2(
node_context.os_disk_file_path, node_context.os_disk_base_file_path
)

def _get_vmm_version(self) -> str:
result = "Unknown"
if self.host_node:
output = self.host_node.execute(
"qemu-system-x86_64 --version",
shell=True,
).stdout
output = filter_ansi_escape(output)
match = re.search(QEMU_VERSION_PATTERN, output.strip())
if match:
result = match.group("qemu_version")
return result

0 comments on commit afec5a0

Please sign in to comment.