virtinst: add support for virtio-rng devices

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

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano 2013-09-18 15:29:28 +02:00 committed by Cole Robinson
parent 3861069f99
commit 8abcae5073
9 changed files with 385 additions and 1 deletions

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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")

View File

@ -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)

View File

@ -103,6 +103,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
@ -123,7 +124,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 = {}

94
virtinst/devicerng.py Normal file
View File

@ -0,0 +1,94 @@
# coding=utf-8
#
# Copyright 2013 Red Hat, Inc.
# Giuseppe Scrivano <gscrivan@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()