[virt-tools-list] [virt-manager PATCH v2 1/4] Add VirtualTPMDevice class to virtinst

Stefan Berger stefanb at linux.vnet.ibm.com
Wed Jun 26 01:45:06 UTC 2013


Add VirtualTPMDevice class and TPM test cases to xmlparse.py

Signed-off-by: Stefan Berger <stefanb at linux.vnet.ibm.com>
---
 tests/xmlparse-xml/change-tpm-in.xml  |  63 ++++++++++++++++
 tests/xmlparse-xml/change-tpm-out.xml |  63 ++++++++++++++++
 tests/xmlparse.py                     |  13 ++++
 virtinst/Guest.py                     |   1 +
 virtinst/VirtualDevice.py             |   4 +-
 virtinst/VirtualTPMDevice.py          | 136 ++++++++++++++++++++++++++++++++++
 virtinst/__init__.py                  |   1 +
 7 files changed, 280 insertions(+), 1 deletion(-)
 create mode 100644 tests/xmlparse-xml/change-tpm-in.xml
 create mode 100644 tests/xmlparse-xml/change-tpm-out.xml
 create mode 100644 virtinst/VirtualTPMDevice.py

diff --git a/tests/xmlparse-xml/change-tpm-in.xml b/tests/xmlparse-xml/change-tpm-in.xml
new file mode 100644
index 0000000..4b8af17
--- /dev/null
+++ b/tests/xmlparse-xml/change-tpm-in.xml
@@ -0,0 +1,63 @@
+<domain type='kvm'>
+  <name>TestGuest</name>
+  <currentMemory>204800</currentMemory>
+  <memory>409600</memory>
+  <uuid>12345678-1234-1234-1234-123456789012</uuid>
+  <os>
+    <type arch='i686' machine='foobar'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <features>
+    <acpi/><apic/>
+  </features>
+  <cpu match='exact'>
+    <model>footest</model>
+    <vendor>Intel</vendor>
+    <topology sockets='4' cores='5' threads='2'/>
+    <feature policy='force' name='x2apic'/>
+    <feature policy='forbid' name='lahf_lm'/>
+  </cpu>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>restart</on_crash>
+  <vcpu cpuset='1-3'>5</vcpu>
+  <devices>
+    <emulator>/usr/lib/xen/bin/qemu-dm</emulator>
+    <disk type='file' device='floppy'>
+      <driver name='qemu' type='qcow2'/>
+      <source file='/default-pool/testvol1.img'/>
+      <target dev='fda' bus='fdc'/>
+    </disk>
+    <disk type='file' device='disk'>
+      <driver name='qemu' type='raw'/>
+      <source file='/tmp/test.img'/>
+      <target dev='vda' bus='virtio'/>
+    </disk>
+    <disk type='block' device='disk'>
+      <driver name='qemu'/>
+      <source dev='/dev/loop0'/>
+      <target dev='vdb' bus='virtio'/>
+    </disk>
+    <disk type='block' device='cdrom'>
+      <target dev='hdc' bus='ide'/>
+      <readonly/>
+    </disk>
+    <interface type='network'>
+      <source network='default'/>
+      <mac address='22:22:33:44:55:66'/>
+      <model type='virtio'/>
+    </interface>
+    <graphics type='sdl' display=':3.4' xauth='/tmp/.Xauthority'/>
+    <console type='pty'/>
+    <tpm model='tpm-tis'>
+      <backend type='passthrough'>
+        <backend path='/dev/tpm0'/>
+      </backend>
+    </tpm>
+  </devices>
+  <seclabel type='static' model='selinux'>
+    <label>foolabel</label>
+    <imagelabel>imagelabel</imagelabel>
+  </seclabel>
+</domain>
diff --git a/tests/xmlparse-xml/change-tpm-out.xml b/tests/xmlparse-xml/change-tpm-out.xml
new file mode 100644
index 0000000..093e0ac
--- /dev/null
+++ b/tests/xmlparse-xml/change-tpm-out.xml
@@ -0,0 +1,63 @@
+<domain type="kvm">
+  <name>TestGuest</name>
+  <currentMemory>204800</currentMemory>
+  <memory>409600</memory>
+  <uuid>12345678-1234-1234-1234-123456789012</uuid>
+  <os>
+    <type arch="i686" machine="foobar">hvm</type>
+    <boot dev="hd"/>
+  </os>
+  <features>
+    <acpi/><apic/>
+  </features>
+  <cpu match="exact">
+    <model>footest</model>
+    <vendor>Intel</vendor>
+    <topology sockets="4" cores="5" threads="2"/>
+    <feature policy="force" name="x2apic"/>
+    <feature policy="forbid" name="lahf_lm"/>
+  </cpu>
+  <clock offset="utc"/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>restart</on_crash>
+  <vcpu cpuset="1-3">5</vcpu>
+  <devices>
+    <emulator>/usr/lib/xen/bin/qemu-dm</emulator>
+    <disk type="file" device="floppy">
+      <driver name="qemu" type="qcow2"/>
+      <source file="/default-pool/testvol1.img"/>
+      <target dev="fda" bus="fdc"/>
+    </disk>
+    <disk type="file" device="disk">
+      <driver name="qemu" type="raw"/>
+      <source file="/tmp/test.img"/>
+      <target dev="vda" bus="virtio"/>
+    </disk>
+    <disk type="block" device="disk">
+      <driver name="qemu"/>
+      <source dev="/dev/loop0"/>
+      <target dev="vdb" bus="virtio"/>
+    </disk>
+    <disk type="block" device="cdrom">
+      <target dev="hdc" bus="ide"/>
+      <readonly/>
+    </disk>
+    <interface type="network">
+      <source network="default"/>
+      <mac address="22:22:33:44:55:66"/>
+      <model type="virtio"/>
+    </interface>
+    <graphics type="sdl" display=":3.4" xauth="/tmp/.Xauthority"/>
+    <console type="pty"/>
+    <tpm model="tpm-tis">
+      <backend type="passthrough">
+        <backend path="/dev/tpm0"/>
+      </backend>
+    </tpm>
+  </devices>
+  <seclabel type="static" model="selinux">
+    <label>foolabel</label>
+    <imagelabel>imagelabel</imagelabel>
+  </seclabel>
+</domain>
diff --git a/tests/xmlparse.py b/tests/xmlparse.py
index 7dfea3d..84eb090 100644
--- a/tests/xmlparse.py
+++ b/tests/xmlparse.py
@@ -724,6 +724,19 @@ class XMLParseTest(unittest.TestCase):
 
         self._alter_compare(guest.get_xml_config(), outfile)
 
+    def testAlterTPM(self):
+        infile  = "tests/xmlparse-xml/change-tpm-in.xml"
+        outfile = "tests/xmlparse-xml/change-tpm-out.xml"
+        guest = virtinst.Guest(conn=conn,
+                               parsexml=file(infile).read())
+
+        dev1 = guest.get_devices("tpm")[0]
+
+        check = self._make_checker(dev1)
+        check("model", "tpm-tis", "tpm-tis")
+
+        self._alter_compare(guest.get_xml_config(), outfile)
+
     def testConsoleCompat(self):
         infile  = "tests/xmlparse-xml/console-compat-in.xml"
         outfile = "tests/xmlparse-xml/console-compat-out.xml"
diff --git a/virtinst/Guest.py b/virtinst/Guest.py
index 6702444..81c4b4e 100644
--- a/virtinst/Guest.py
+++ b/virtinst/Guest.py
@@ -675,6 +675,7 @@ class Guest(XMLBuilderDomain.XMLBuilderDomain):
             "smartcard" : virtinst.VirtualSmartCardDevice,
             "redirdev"  : virtinst.VirtualRedirDevice,
             "memballoon": virtinst.VirtualMemballoon,
+            "tpm"       : virtinst.VirtualTPMDevice,
        }
 
         # Hand off all child element parsing to relevant classes
diff --git a/virtinst/VirtualDevice.py b/virtinst/VirtualDevice.py
index bbc32a6..a1e5aef 100644
--- a/virtinst/VirtualDevice.py
+++ b/virtinst/VirtualDevice.py
@@ -45,6 +45,7 @@ class VirtualDevice(XMLBuilderDomain):
     VIRTUAL_DEV_SMARTCARD       = "smartcard"
     VIRTUAL_DEV_REDIRDEV        = "redirdev"
     VIRTUAL_DEV_MEMBALLOON      = "memballoon"
+    VIRTUAL_DEV_TPM             = "tpm"
 
     # Ordering in this list is important: it will be the order the
     # Guest class outputs XML. So changing this may upset the test suite
@@ -64,7 +65,8 @@ class VirtualDevice(XMLBuilderDomain):
                             VIRTUAL_DEV_WATCHDOG,
                             VIRTUAL_DEV_SMARTCARD,
                             VIRTUAL_DEV_REDIRDEV,
-                            VIRTUAL_DEV_MEMBALLOON]
+                            VIRTUAL_DEV_MEMBALLOON,
+                            VIRTUAL_DEV_TPM]
 
     # General device type (disk, interface, etc.)
     _virtual_device_type = None
diff --git a/virtinst/VirtualTPMDevice.py b/virtinst/VirtualTPMDevice.py
new file mode 100644
index 0000000..c4e70fc
--- /dev/null
+++ b/virtinst/VirtualTPMDevice.py
@@ -0,0 +1,136 @@
+# coding=utf-8
+#
+# Copyright 2011  Red Hat, Inc.
+# Cole Robinson <crobinso at redhat.com>
+# Marc-André Lureau <marcandre.lureau at redhat.com>
+#
+# Copyright 2013  IBM Corporation
+# Author: Stefan Berger <stefanb at linux.vnet.ibm.com>
+#
+# This program 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 2 of the License, or
+# (at your option)  any later version.
+#
+# This program 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 this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301 USA.
+
+from virtinst.VirtualDevice import VirtualDevice
+from virtinst.XMLBuilderDomain import _xml_property
+
+
+class VirtualTPMDevice(VirtualDevice):
+
+    _virtual_device_type = VirtualDevice.VIRTUAL_DEV_TPM
+
+    # backend types
+    TPM_PASSTHROUGH = "passthrough"
+
+    # device models
+    TPM_TIS = "tpm-tis"
+
+    # Default backend type and list of choices
+    TYPE_DEFAULT = TPM_PASSTHROUGH
+    _types = [TPM_PASSTHROUGH]
+
+    # Default device model and list of choices
+    MODEL_DEFAULT = TPM_TIS
+    _models = [TPM_TIS]
+
+    def get_dev_instance(conn, tpm_type):
+        """
+        Set up the class attributes for the passed tpm_type
+        """
+
+        if tpm_type == VirtualTPMDevice.TPM_PASSTHROUGH:
+            c = VirtualTPMPassthroughDevice
+        else:
+            raise ValueError(_("Unknown TPM device type '%s'.") %
+                             tpm_type)
+
+        return c(conn, tpm_type)
+    get_dev_instance = staticmethod(get_dev_instance)
+
+    def __init__(self, conn, typ=TYPE_DEFAULT,
+                 parsexml=None, parsexmlnode=None, caps=None):
+        VirtualDevice.__init__(self, conn, parsexml, parsexmlnode, caps)
+
+        self._type = None
+        self._model = self.TPM_TIS
+        self._device_path = None
+
+        if self._is_parse():
+            return
+
+        self.type = typ
+
+    def get_types(self):
+        return self._types[:]
+    types = property(get_types)
+
+    def get_type(self):
+        return self._type
+    def set_type(self, val):
+        if val not in self.types:
+            raise ValueError(_("Unknown TPM type '%s'") % val)
+        self._type = val
+    type = _xml_property(get_type, set_type,
+                         xpath="./backend/@type")
+
+    def get_models(self):
+        return self._models[:]
+    models = property(get_models)
+
+    def get_model(self):
+        return self._model
+    def set_model(self, val):
+        if val not in self.models:
+            raise ValueError(_("Unknown TPM model '%s'") % val)
+        self._model = val
+    model = _xml_property(get_model, set_model,
+                          xpath="./@model")
+
+    def get_device_path(self):
+        return self._device_path
+    def set_device_path(self, val):
+        self._device_path = val
+    device_path = _xml_property(get_device_path, set_device_path,
+                                xpath="./backend/device/@path")
+
+    def supports_property(self, propname):
+        """
+        Whether the TPM dev type supports the passed property name
+        """
+        users = {
+            "device_path"     : [self.TPM_PASSTHROUGH],
+        }
+
+        if users.get(propname):
+            return self.type in users[propname]
+
+        return hasattr(self, propname)
+
+    def _get_xml_config(self):
+        device = "/dev/tpm0"
+        if self._device_path is not None:
+            device = self._device_path
+
+        xml  = "    <tpm model='%s'>\n" % self.model
+        xml += "      <backend type='%s'>\n" % self.type
+        if self.type == "passthrough":
+            xml += "        <device path='%s'/>\n" % device
+        xml += "      </backend>\n"
+        xml += "    </tpm>"
+
+        return xml
+
+
+class VirtualTPMPassthroughDevice(VirtualTPMDevice):
+    _tpm_type = VirtualTPMDevice.TPM_PASSTHROUGH
diff --git a/virtinst/__init__.py b/virtinst/__init__.py
index 5e10ca6..b608e5d 100644
--- a/virtinst/__init__.py
+++ b/virtinst/__init__.py
@@ -38,6 +38,7 @@ from virtinst.VirtualFilesystem import VirtualFilesystem
 from virtinst.VirtualSmartCardDevice import VirtualSmartCardDevice
 from virtinst.VirtualRedirDevice import VirtualRedirDevice
 from virtinst.VirtualMemballoon import VirtualMemballoon
+from virtinst.VirtualTPMDevice import VirtualTPMDevice
 from virtinst.DistroInstaller import DistroInstaller
 from virtinst.PXEInstaller import PXEInstaller
 from virtinst.LiveCDInstaller import LiveCDInstaller
-- 
1.8.1.4




More information about the virt-tools-list mailing list