[virt-tools-list] [PATCH v2 3/6] tpm: add TPM emulator backend

Stefan Berger stefanb at linux.vnet.ibm.com
Fri Jun 8 21:42:41 UTC 2018


From: Marc-André Lureau <marcandre.lureau at redhat.com>

An emulated backend doesn't require any path, since libvirt will take
care of finding the emulator and managing the storage. However, the
version to emulate can be specified.

Signed-off-by: Marc-André Lureau <marcandre.lureau at redhat.com>
---
 .../compare/virt-install-singleton-config-2.xml    |  6 +++++
 tests/clitest.py                                   |  1 +
 virtinst/cli.py                                    |  1 +
 virtinst/devices/tpm.py                            | 26 ++++++++++++++++++++--
 4 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/tests/cli-test-xml/compare/virt-install-singleton-config-2.xml b/tests/cli-test-xml/compare/virt-install-singleton-config-2.xml
index 8cd92544..1f36bf09 100644
--- a/tests/cli-test-xml/compare/virt-install-singleton-config-2.xml
+++ b/tests/cli-test-xml/compare/virt-install-singleton-config-2.xml
@@ -148,6 +148,9 @@
         <device path="/dev/tpm0"/>
       </backend>
     </tpm>
+    <tpm model="tpm-crb">
+      <backend type="emulator" version="2.0"/>
+    </tpm>
     <graphics type="vnc" port="-1"/>
     <watchdog model="ib700" action="pause"/>
     <memballoon model="virtio"/>
@@ -322,6 +325,9 @@
         <device path="/dev/tpm0"/>
       </backend>
     </tpm>
+    <tpm model="tpm-crb">
+      <backend type="emulator" version="2.0"/>
+    </tpm>
     <graphics type="vnc" port="-1"/>
     <watchdog model="ib700" action="pause"/>
     <memballoon model="virtio"/>
diff --git a/tests/clitest.py b/tests/clitest.py
index 5271935f..01982371 100644
--- a/tests/clitest.py
+++ b/tests/clitest.py
@@ -450,6 +450,7 @@ cache.mode=emulate,cache.level=3 \
 --watchdog ib700,action=pause \
 --tpm passthrough,model=tpm-tis,path=/dev/tpm0 \
 --tpm passthrough,model=tpm-crb,path=/dev/tpm0 \
+--tpm emulator,model=tpm-crb,version=2.0 \
 --rng egd,backend_host=127.0.0.1,backend_service=8000,backend_type=udp,backend_mode=bind,backend_connect_host=foo,backend_connect_service=708 \
 --panic iobase=0x506 \
 """, "singleton-config-2")
diff --git a/virtinst/cli.py b/virtinst/cli.py
index a2624e66..a20cdb23 100644
--- a/virtinst/cli.py
+++ b/virtinst/cli.py
@@ -2440,6 +2440,7 @@ _register_virt_parser(ParserTPM)
 _add_device_address_args(ParserTPM)
 ParserTPM.add_arg("type", "type")
 ParserTPM.add_arg("model", "model")
+ParserTPM.add_arg("version", "version")
 ParserTPM.add_arg("device_path", "path")
 
 
diff --git a/virtinst/devices/tpm.py b/virtinst/devices/tpm.py
index ab7492ed..8d418815 100644
--- a/virtinst/devices/tpm.py
+++ b/virtinst/devices/tpm.py
@@ -16,9 +16,15 @@ from ..xmlbuilder import XMLProperty
 class DeviceTpm(Device):
     XML_NAME = "tpm"
 
+    VERSION_1_2 = "1.2"
+    VERSION_2_0 = "2.0"
+    VERSION_DEFAULT = "default"
+    VERSIONS = [VERSION_1_2, VERSION_2_0]
+
     TYPE_PASSTHROUGH = "passthrough"
+    TYPE_EMULATOR = "emulator"
     TYPE_DEFAULT = "default"
-    TYPES = [TYPE_PASSTHROUGH]
+    TYPES = [TYPE_PASSTHROUGH, TYPE_EMULATOR]
 
     MODEL_TIS = "tpm-tis"
     MODEL_CRB = "tpm-crb"
@@ -29,6 +35,8 @@ class DeviceTpm(Device):
     def get_pretty_type(tpm_type):
         if tpm_type == DeviceTpm.TYPE_PASSTHROUGH:
             return _("Passthrough device")
+        if tpm_type == DeviceTpm.TYPE_EMULATOR:
+            return _("Emulated device")
         return tpm_type
 
     @staticmethod
@@ -45,6 +53,7 @@ class DeviceTpm(Device):
         """
         users = {
             "device_path": [self.TYPE_PASSTHROUGH],
+            "version": [self.TYPE_EMULATOR],
         }
 
         if users.get(propname):
@@ -54,7 +63,20 @@ class DeviceTpm(Device):
 
     type = XMLProperty("./backend/@type",
                        default_cb=lambda s: s.TYPE_PASSTHROUGH)
+
+    def _get_default_version(self):
+        if not self.supports_property("version"):
+            return None
+        return self.VERSION_1_2
+    version = XMLProperty("./backend/@version",
+                          default_cb=_get_default_version)
     model = XMLProperty("./@model",
                        default_cb=lambda s: s.MODEL_TIS)
+
+
+    def _get_default_device_path(self):
+        if not self.supports_property("device_path"):
+            return None
+        return "/dev/tpm0"
     device_path = XMLProperty("./backend/device/@path",
-                              default_cb=lambda s: "/dev/tpm0")
+                              default_cb=_get_default_device_path)
-- 
2.14.4




More information about the virt-tools-list mailing list