[virt-tools-list] [RFC PATCH 1/3] virtinst: add support for virtio-rng devices

Giuseppe Scrivano gscrivan at redhat.com
Wed Sep 18 13:29:28 UTC 2013


The virtio-rng device is supported by libvirt since version 1.0.3.

Signed-off-by: Giuseppe Scrivano <gscrivan at redhat.com>
---
 tests/testdriver.xml                         |  7 +++
 tests/xmlparse-xml/change-rng-egd-in.xml     | 64 +++++++++++++++++++
 tests/xmlparse-xml/change-rng-egd-out.xml    | 64 +++++++++++++++++++
 tests/xmlparse-xml/change-rng-random-in.xml  | 61 ++++++++++++++++++
 tests/xmlparse-xml/change-rng-random-out.xml | 61 ++++++++++++++++++
 tests/xmlparse.py                            | 30 +++++++++
 virtinst/__init__.py                         |  1 +
 virtinst/device.py                           |  4 +-
 virtinst/devicerng.py                        | 94 ++++++++++++++++++++++++++++
 9 files changed, 385 insertions(+), 1 deletion(-)
 create mode 100644 tests/xmlparse-xml/change-rng-egd-in.xml
 create mode 100644 tests/xmlparse-xml/change-rng-egd-out.xml
 create mode 100644 tests/xmlparse-xml/change-rng-random-in.xml
 create mode 100644 tests/xmlparse-xml/change-rng-random-out.xml
 create mode 100644 virtinst/devicerng.py

diff --git a/tests/testdriver.xml b/tests/testdriver.xml
index 195afe1..cd70ad8 100644
--- a/tests/testdriver.xml
+++ b/tests/testdriver.xml
@@ -298,6 +298,13 @@
       </backend>
     </tpm>
 
+    <!-- rng devices -->
+    <rng model='virtio'>
+      <rate period='2000' bytes='1234'/>
+      <backend model='egd' type='tcp'>
+        <source mode='connect' host='1.2.3.4' service='1234'/>
+      </backend>
+    </rng>
   </devices>
 </domain>
 
diff --git a/tests/xmlparse-xml/change-rng-egd-in.xml b/tests/xmlparse-xml/change-rng-egd-in.xml
new file mode 100644
index 0000000..74db226
--- /dev/null
+++ b/tests/xmlparse-xml/change-rng-egd-in.xml
@@ -0,0 +1,64 @@
+<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='/dev/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'/>
+    <rng model='virtio'>
+      <rate period='2000' bytes='1234'/>
+      <backend model='egd' type='udp'>
+        <source mode='connect' host='1.2.3.4' service='1234'/>
+      </backend>
+    </rng>
+  </devices>
+  <seclabel type='static' model='selinux'>
+    <label>foolabel</label>
+    <imagelabel>imagelabel</imagelabel>
+  </seclabel>
+</domain>
diff --git a/tests/xmlparse-xml/change-rng-egd-out.xml b/tests/xmlparse-xml/change-rng-egd-out.xml
new file mode 100644
index 0000000..c4bd71e
--- /dev/null
+++ b/tests/xmlparse-xml/change-rng-egd-out.xml
@@ -0,0 +1,64 @@
+<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="/dev/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"/>
+    <rng model="virtio">
+      <rate period="2001" bytes="4321"/>
+      <backend model="egd" type="tcp">
+        <source mode="bind" host="1.2.3.5" service="1235"/>
+      </backend>
+    </rng>
+  </devices>
+  <seclabel type="static" model="selinux">
+    <label>foolabel</label>
+    <imagelabel>imagelabel</imagelabel>
+  </seclabel>
+</domain>
diff --git a/tests/xmlparse-xml/change-rng-random-in.xml b/tests/xmlparse-xml/change-rng-random-in.xml
new file mode 100644
index 0000000..fb9367d
--- /dev/null
+++ b/tests/xmlparse-xml/change-rng-random-in.xml
@@ -0,0 +1,61 @@
+<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='/dev/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'/>
+    <rng model='virtio'>
+      <backend model='random'>/dev/random</backend>
+    </rng>
+  </devices>
+  <seclabel type='static' model='selinux'>
+    <label>foolabel</label>
+    <imagelabel>imagelabel</imagelabel>
+  </seclabel>
+</domain>
diff --git a/tests/xmlparse-xml/change-rng-random-out.xml b/tests/xmlparse-xml/change-rng-random-out.xml
new file mode 100644
index 0000000..8c3eba4
--- /dev/null
+++ b/tests/xmlparse-xml/change-rng-random-out.xml
@@ -0,0 +1,61 @@
+<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="/dev/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"/>
+    <rng model="virtio">
+      <backend model="random">/dev/hwrng</backend>
+    </rng>
+  </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 db67949..14fb91a 100644
--- a/tests/xmlparse.py
+++ b/tests/xmlparse.py
@@ -733,6 +733,36 @@ class XMLParseTest(unittest.TestCase):
 
         self._alter_compare(guest.get_xml_config(), outfile)
 
+    def testAlterRNG_EGD(self):
+        guest, outfile = self._get_test_content("change-rng-egd")
+
+        dev1 = guest.get_devices("rng")[0]
+
+        check = self._make_checker(dev1)
+        check("type", "egd")
+        check("backend_type", "udp", "tcp")
+
+        check("backend_source_host", "1.2.3.4", "1.2.3.5")
+        check("backend_source_service", "1234", "1235")
+        check("backend_source_mode", "connect", "bind")
+
+        check("rate_bytes", "1234", "4321")
+        check("rate_period", "2000", "2001")
+
+        self._alter_compare(guest.get_xml_config(), outfile)
+
+    def testAlterRNG_Random(self):
+        guest, outfile = self._get_test_content("change-rng-random")
+
+        dev1 = guest.get_devices("rng")[0]
+
+        check = self._make_checker(dev1)
+        check("type", "random", "random")
+        check("model", "virtio", "virtio")
+        check("device", "/dev/random", "/dev/hwrng")
+
+        self._alter_compare(guest.get_xml_config(), outfile)
+
     def testConsoleCompat(self):
         guest, outfile = self._get_test_content("console-compat")
 
diff --git a/virtinst/__init__.py b/virtinst/__init__.py
index 603e2dc..1cf5c55 100644
--- a/virtinst/__init__.py
+++ b/virtinst/__init__.py
@@ -53,6 +53,7 @@ from virtinst.devicesmartcard import VirtualSmartCardDevice
 from virtinst.deviceredirdev import VirtualRedirDevice
 from virtinst.devicememballoon import VirtualMemballoon
 from virtinst.devicetpm import VirtualTPMDevice
+from virtinst.devicerng import VirtualRNGDevice
 
 from virtinst.installer import (ContainerInstaller, ImportInstaller,
                                 LiveCDInstaller, PXEInstaller, Installer)
diff --git a/virtinst/device.py b/virtinst/device.py
index f7dce38..73e3d2d 100644
--- a/virtinst/device.py
+++ b/virtinst/device.py
@@ -45,6 +45,7 @@ class VirtualDevice(XMLBuilder):
     VIRTUAL_DEV_REDIRDEV        = "redirdev"
     VIRTUAL_DEV_MEMBALLOON      = "memballoon"
     VIRTUAL_DEV_TPM             = "tpm"
+    VIRTUAL_DEV_RNG             = "rng"
 
     # Ordering in this list is important: it will be the order the
     # Guest class outputs XML. So changing this may upset the test suite
@@ -65,7 +66,8 @@ class VirtualDevice(XMLBuilder):
                             VIRTUAL_DEV_SMARTCARD,
                             VIRTUAL_DEV_REDIRDEV,
                             VIRTUAL_DEV_MEMBALLOON,
-                            VIRTUAL_DEV_TPM]
+                            VIRTUAL_DEV_TPM,
+                            VIRTUAL_DEV_RNG]
 
     virtual_device_classes = {}
 
diff --git a/virtinst/devicerng.py b/virtinst/devicerng.py
new file mode 100644
index 0000000..c28dde0
--- /dev/null
+++ b/virtinst/devicerng.py
@@ -0,0 +1,94 @@
+# coding=utf-8
+#
+# Copyright 2013  Red Hat, Inc.
+# Giuseppe Scrivano <gscrivan at redhat.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 import VirtualDevice
+from virtinst.xmlbuilder import XMLProperty
+
+
+class VirtualRNGDevice(VirtualDevice):
+
+    virtual_device_type = VirtualDevice.VIRTUAL_DEV_RNG
+
+    TYPE_RANDOM = "random"
+    TYPE_EGD = "egd"
+    TYPES = [TYPE_RANDOM, TYPE_EGD]
+
+    BACKEND_TYPE_UDP = "udp"
+    BACKEND_TYPE_TCP = "tcp"
+    BACKEND_TYPES = [BACKEND_TYPE_UDP, BACKEND_TYPE_TCP]
+
+    BACKEND_MODE_BIND = "bind"
+    BACKEND_MODE_CONNECT = "connect"
+    BACKEND_MODES = [BACKEND_MODE_BIND, BACKEND_MODE_CONNECT]
+
+    @staticmethod
+    def get_pretty_type(rng_type):
+        if rng_type == VirtualRNGDevice.TYPE_RANDOM:
+            return _("Random")
+        if rng_type == VirtualRNGDevice.TYPE_EGD:
+            return _("Entropy Gathering Daemon")
+        return rng_type
+
+    @staticmethod
+    def get_pretty_backend_type(backend_type):
+        return {"udp" : "UDP",
+                "tcp": "TCP"}.get(backend_type) or backend_type
+
+    @staticmethod
+    def get_pretty_mode(mode):
+        return {"bind" : "Bind",
+                "connect": "Connect"}.get(mode) or mode
+
+    def supports_property(self, propname):
+        """
+        Whether the rng dev type supports the passed property name
+        """
+        users = {
+            "type"                   : [self.TYPE_EGD, self.TYPE_RANDOM],
+
+            "model"                  : [self.TYPE_EGD, self.TYPE_RANDOM],
+            "backend_source_host"    : [self.TYPE_EGD],
+            "backend_source_mode"    : [self.TYPE_EGD],
+            "backend_source_service" : [self.TYPE_EGD],
+            "backend_type"           : [self.TYPE_EGD],
+            "device"                 : [self.TYPE_RANDOM],
+            "rate_bytes"             : [self.TYPE_EGD, self.TYPE_RANDOM],
+            "rate_period"            : [self.TYPE_EGD, self.TYPE_RANDOM],
+        }
+        if users.get(propname):
+            return self.type in users[propname]
+
+        return hasattr(self, propname)
+
+    type = XMLProperty(xpath="./backend/@model")
+    model = XMLProperty(xpath="./@model",
+                        default_cb=lambda s: "virtio")
+
+    backend_type = XMLProperty(xpath="./backend/@type")
+    backend_source_host = XMLProperty(xpath="./backend/source/@host")
+    backend_source_service = XMLProperty(xpath="./backend/source/@service")
+    backend_source_mode = XMLProperty(xpath="./backend/source/@mode")
+
+    rate_bytes = XMLProperty(xpath="./rate/@bytes")
+    rate_period = XMLProperty(xpath="./rate/@period")
+
+    device = XMLProperty(xpath="./backend")
+
+VirtualRNGDevice.register_type()
-- 
1.8.3.1




More information about the virt-tools-list mailing list