2013-03-18 05:06:52 +08:00
|
|
|
#
|
2013-10-28 04:59:46 +08:00
|
|
|
# Copyright 2009, 2013 Red Hat, Inc.
|
2013-03-18 05:06:52 +08:00
|
|
|
# Cole Robinson <crobinso@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
|
2013-10-28 04:59:47 +08:00
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2013-03-18 05:06:52 +08:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2013-08-09 08:47:17 +08:00
|
|
|
from virtinst import VirtualDevice
|
2013-07-14 06:56:09 +08:00
|
|
|
from virtinst.xmlbuilder import XMLProperty
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2013-07-16 21:14:37 +08:00
|
|
|
class _VirtualCharDevice(VirtualDevice):
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
|
|
|
Base class for all character devices. Shouldn't be instantiated
|
|
|
|
directly.
|
|
|
|
"""
|
|
|
|
|
2013-07-16 21:14:37 +08:00
|
|
|
TYPE_PTY = "pty"
|
|
|
|
TYPE_DEV = "dev"
|
|
|
|
TYPE_STDIO = "stdio"
|
|
|
|
TYPE_PIPE = "pipe"
|
|
|
|
TYPE_FILE = "file"
|
|
|
|
TYPE_VC = "vc"
|
|
|
|
TYPE_NULL = "null"
|
|
|
|
TYPE_TCP = "tcp"
|
|
|
|
TYPE_UDP = "udp"
|
|
|
|
TYPE_UNIX = "unix"
|
|
|
|
TYPE_SPICEVMC = "spicevmc"
|
2013-10-05 23:07:32 +08:00
|
|
|
# We don't list the non-UI friendly types here
|
|
|
|
_TYPES_FOR_ALL = [TYPE_PTY, TYPE_DEV, TYPE_FILE,
|
|
|
|
TYPE_TCP, TYPE_UDP, TYPE_UNIX]
|
2013-08-08 07:04:18 +08:00
|
|
|
_TYPES_FOR_CHANNEL = [TYPE_SPICEVMC]
|
|
|
|
TYPES = _TYPES_FOR_ALL
|
2013-07-16 21:14:37 +08:00
|
|
|
|
|
|
|
MODE_CONNECT = "connect"
|
|
|
|
MODE_BIND = "bind"
|
|
|
|
MODES = [MODE_CONNECT, MODE_BIND]
|
|
|
|
|
|
|
|
PROTOCOL_RAW = "raw"
|
|
|
|
PROTOCOL_TELNET = "telnet"
|
|
|
|
PROTOCOLS = [PROTOCOL_RAW, PROTOCOL_TELNET]
|
|
|
|
|
|
|
|
CHANNEL_TARGET_GUESTFWD = "guestfwd"
|
|
|
|
CHANNEL_TARGET_VIRTIO = "virtio"
|
|
|
|
CHANNEL_TARGETS = [CHANNEL_TARGET_GUESTFWD,
|
|
|
|
CHANNEL_TARGET_VIRTIO]
|
|
|
|
|
|
|
|
CONSOLE_TARGET_SERIAL = "serial"
|
|
|
|
CONSOLE_TARGET_UML = "uml"
|
|
|
|
CONSOLE_TARGET_XEN = "xen"
|
|
|
|
CONSOLE_TARGET_VIRTIO = "virtio"
|
|
|
|
CONSOLE_TARGETS = [CONSOLE_TARGET_SERIAL, CONSOLE_TARGET_UML,
|
|
|
|
CONSOLE_TARGET_XEN, CONSOLE_TARGET_VIRTIO]
|
|
|
|
|
2013-10-06 01:27:11 +08:00
|
|
|
CHANNEL_NAME_SPICE = "com.redhat.spice.0"
|
|
|
|
CHANNEL_NAME_QEMUGA = "org.qemu.guest_agent.0"
|
|
|
|
CHANNEL_NAME_LIBGUESTFS = "org.libguestfs.channel.0"
|
|
|
|
CHANNEL_NAMES = [CHANNEL_NAME_SPICE,
|
|
|
|
CHANNEL_NAME_QEMUGA,
|
|
|
|
CHANNEL_NAME_LIBGUESTFS]
|
|
|
|
|
2013-10-06 02:04:49 +08:00
|
|
|
@staticmethod
|
|
|
|
def pretty_channel_name(val):
|
|
|
|
if val == _VirtualCharDevice.CHANNEL_NAME_SPICE:
|
|
|
|
return "spice"
|
|
|
|
if val == _VirtualCharDevice.CHANNEL_NAME_QEMUGA:
|
|
|
|
return "qemu-ga"
|
|
|
|
if val == _VirtualCharDevice.CHANNEL_NAME_LIBGUESTFS:
|
|
|
|
return "libguestfs"
|
|
|
|
return None
|
|
|
|
|
2013-07-16 21:14:37 +08:00
|
|
|
@staticmethod
|
|
|
|
def pretty_type(ctype):
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
|
|
|
Return a human readable description of the passed char type
|
|
|
|
"""
|
|
|
|
desc = ""
|
|
|
|
|
2013-07-16 21:14:37 +08:00
|
|
|
if ctype == _VirtualCharDevice.TYPE_PTY:
|
2013-03-18 05:06:52 +08:00
|
|
|
desc = _("Pseudo TTY")
|
2013-07-16 21:14:37 +08:00
|
|
|
elif ctype == _VirtualCharDevice.TYPE_DEV:
|
2013-03-18 05:06:52 +08:00
|
|
|
desc = _("Physical host character device")
|
2013-07-16 21:14:37 +08:00
|
|
|
elif ctype == _VirtualCharDevice.TYPE_STDIO:
|
2013-03-18 05:06:52 +08:00
|
|
|
desc = _("Standard input/output")
|
2013-07-16 21:14:37 +08:00
|
|
|
elif ctype == _VirtualCharDevice.TYPE_PIPE:
|
2013-03-18 05:06:52 +08:00
|
|
|
desc = _("Named pipe")
|
2013-07-16 21:14:37 +08:00
|
|
|
elif ctype == _VirtualCharDevice.TYPE_FILE:
|
2013-03-18 05:06:52 +08:00
|
|
|
desc = _("Output to a file")
|
2013-07-16 21:14:37 +08:00
|
|
|
elif ctype == _VirtualCharDevice.TYPE_VC:
|
2013-03-18 05:06:52 +08:00
|
|
|
desc = _("Virtual console")
|
2013-07-16 21:14:37 +08:00
|
|
|
elif ctype == _VirtualCharDevice.TYPE_NULL:
|
2013-03-18 05:06:52 +08:00
|
|
|
desc = _("Null device")
|
2013-07-16 21:14:37 +08:00
|
|
|
elif ctype == _VirtualCharDevice.TYPE_TCP:
|
2013-03-18 05:06:52 +08:00
|
|
|
desc = _("TCP net console")
|
2013-07-16 21:14:37 +08:00
|
|
|
elif ctype == _VirtualCharDevice.TYPE_UDP:
|
2013-03-18 05:06:52 +08:00
|
|
|
desc = _("UDP net console")
|
2013-07-16 21:14:37 +08:00
|
|
|
elif ctype == _VirtualCharDevice.TYPE_UNIX:
|
2013-03-18 05:06:52 +08:00
|
|
|
desc = _("Unix socket")
|
2013-07-16 21:14:37 +08:00
|
|
|
elif ctype == _VirtualCharDevice.TYPE_SPICEVMC:
|
2013-03-18 05:06:52 +08:00
|
|
|
desc = _("Spice agent")
|
|
|
|
|
|
|
|
return desc
|
|
|
|
|
2013-07-16 21:14:37 +08:00
|
|
|
@staticmethod
|
|
|
|
def pretty_mode(char_mode):
|
2013-03-18 05:06:52 +08:00
|
|
|
"""
|
|
|
|
Return a human readable description of the passed char type
|
|
|
|
"""
|
|
|
|
desc = ""
|
|
|
|
|
2013-07-16 21:14:37 +08:00
|
|
|
if char_mode == _VirtualCharDevice.MODE_CONNECT:
|
2013-03-18 05:06:52 +08:00
|
|
|
desc = _("Client mode")
|
2013-07-16 21:14:37 +08:00
|
|
|
elif char_mode == _VirtualCharDevice.MODE_BIND:
|
2013-03-18 05:06:52 +08:00
|
|
|
desc = _("Server mode")
|
|
|
|
|
|
|
|
return desc
|
|
|
|
|
|
|
|
def supports_property(self, propname, ro=False):
|
|
|
|
"""
|
|
|
|
Whether the character dev type supports the passed property name
|
|
|
|
"""
|
|
|
|
users = {
|
2013-07-16 21:14:37 +08:00
|
|
|
"source_path" : [self.TYPE_FILE, self.TYPE_UNIX,
|
|
|
|
self.TYPE_DEV, self.TYPE_PIPE],
|
|
|
|
"source_mode" : [self.TYPE_UNIX, self.TYPE_TCP],
|
|
|
|
"source_host" : [self.TYPE_TCP, self.TYPE_UDP],
|
|
|
|
"source_port" : [self.TYPE_TCP, self.TYPE_UDP],
|
|
|
|
"protocol" : [self.TYPE_TCP],
|
|
|
|
"bind_host" : [self.TYPE_UDP],
|
|
|
|
"bind_port" : [self.TYPE_UDP],
|
2013-10-05 23:28:41 +08:00
|
|
|
}
|
2013-03-18 05:06:52 +08:00
|
|
|
|
|
|
|
if ro:
|
2013-07-16 21:14:37 +08:00
|
|
|
users["source_path"] += [self.TYPE_PTY]
|
2013-03-18 05:06:52 +08:00
|
|
|
|
|
|
|
if users.get(propname):
|
2013-07-16 21:14:37 +08:00
|
|
|
return self.type in users[propname]
|
2013-03-18 05:06:52 +08:00
|
|
|
return hasattr(self, propname)
|
|
|
|
|
2013-07-16 21:14:37 +08:00
|
|
|
|
2013-07-25 02:37:07 +08:00
|
|
|
_XML_PROP_ORDER = ["type", "_has_mode_bind", "_has_mode_connect",
|
2013-07-16 21:14:37 +08:00
|
|
|
"bind_host", "bind_port",
|
2013-09-24 21:25:05 +08:00
|
|
|
"source_mode", "_source_path",
|
2013-07-16 21:14:37 +08:00
|
|
|
"source_host", "source_port",
|
|
|
|
"target_type", "target_name"]
|
|
|
|
|
|
|
|
type = XMLProperty(
|
2013-03-18 05:06:52 +08:00
|
|
|
doc=_("Method used to expose character device in the host."),
|
|
|
|
xpath="./@type")
|
|
|
|
|
2013-09-24 21:25:05 +08:00
|
|
|
_tty = XMLProperty("./@tty")
|
|
|
|
_source_path = XMLProperty(xpath="./source/@path",
|
|
|
|
doc=_("Host input path to attach to the guest."))
|
|
|
|
|
|
|
|
def _get_source_path(self):
|
|
|
|
source = self._source_path
|
|
|
|
if source is None and self._tty:
|
|
|
|
return self._tty
|
|
|
|
return source
|
|
|
|
def _set_source_path(self, val):
|
|
|
|
self._source_path = val
|
|
|
|
source_path = property(_get_source_path, _set_source_path)
|
2013-07-16 21:14:37 +08:00
|
|
|
|
|
|
|
def _get_default_source_mode(self):
|
|
|
|
if self.type == self.TYPE_UDP:
|
|
|
|
return self.MODE_CONNECT
|
|
|
|
if not self.supports_property("source_mode"):
|
|
|
|
return None
|
|
|
|
return self.MODE_BIND
|
2013-09-24 21:25:05 +08:00
|
|
|
def _make_sourcemode_xpath(self):
|
2013-07-16 21:14:37 +08:00
|
|
|
if self.type == self.TYPE_UDP:
|
2013-03-18 05:06:52 +08:00
|
|
|
return "./source[@mode='connect']/@mode"
|
|
|
|
return "./source/@mode"
|
2013-07-16 21:14:37 +08:00
|
|
|
source_mode = XMLProperty(name="char sourcemode",
|
|
|
|
doc=_("Target connect/listen mode."),
|
2013-09-24 21:25:05 +08:00
|
|
|
make_xpath_cb=_make_sourcemode_xpath,
|
2013-07-16 21:14:37 +08:00
|
|
|
default_cb=_get_default_source_mode)
|
|
|
|
|
|
|
|
def _get_default_sourcehost(self):
|
|
|
|
if not self.supports_property("source_host"):
|
|
|
|
return None
|
|
|
|
return "127.0.0.1"
|
|
|
|
def _set_source_validate(self, val):
|
|
|
|
if val is None or self.type != self.TYPE_UDP:
|
|
|
|
return val
|
|
|
|
if not self._has_mode_connect:
|
|
|
|
self._has_mode_connect = self.MODE_CONNECT
|
|
|
|
return val
|
2013-09-24 21:25:05 +08:00
|
|
|
def _make_sourcehost_xpath(self):
|
2013-07-16 21:14:37 +08:00
|
|
|
mode = self.source_mode
|
|
|
|
if self.type == self.TYPE_UDP:
|
|
|
|
mode = "connect"
|
|
|
|
return "./source[@mode='%s']/@host" % mode
|
|
|
|
source_host = XMLProperty(name="char sourcehost",
|
|
|
|
doc=_("Address to connect/listen to."),
|
2013-09-24 21:25:05 +08:00
|
|
|
make_xpath_cb=_make_sourcehost_xpath,
|
2013-07-16 21:14:37 +08:00
|
|
|
default_cb=_get_default_sourcehost,
|
|
|
|
set_converter=_set_source_validate)
|
|
|
|
|
2013-09-24 21:25:05 +08:00
|
|
|
def _make_sourceport_xpath(self):
|
2013-03-18 05:06:52 +08:00
|
|
|
return "./source[@mode='%s']/@service" % self.source_mode
|
2013-07-16 21:14:37 +08:00
|
|
|
source_port = XMLProperty(name="char sourceport",
|
|
|
|
doc=_("Port on target host to connect/listen to."),
|
2013-09-24 21:25:05 +08:00
|
|
|
make_xpath_cb=_make_sourceport_xpath,
|
2013-07-17 21:57:15 +08:00
|
|
|
set_converter=_set_source_validate, is_int=True)
|
2013-07-16 21:14:37 +08:00
|
|
|
|
2013-09-20 01:27:30 +08:00
|
|
|
_has_mode_connect = XMLProperty("./source[@mode='connect']/@mode")
|
|
|
|
_has_mode_bind = XMLProperty("./source[@mode='bind']/@mode")
|
2013-07-16 21:14:37 +08:00
|
|
|
|
|
|
|
def _set_bind_validate(self, val):
|
|
|
|
if val is None:
|
|
|
|
return None
|
|
|
|
if not self._has_mode_bind:
|
|
|
|
self._has_mode_bind = self.MODE_BIND
|
|
|
|
return val
|
2013-09-20 01:27:30 +08:00
|
|
|
bind_host = XMLProperty("./source[@mode='bind']/@host",
|
2013-10-24 21:07:06 +08:00
|
|
|
doc=_("Host address to bind to."),
|
2013-07-16 21:14:37 +08:00
|
|
|
set_converter=_set_bind_validate)
|
2013-09-20 01:27:30 +08:00
|
|
|
bind_port = XMLProperty("./source[@mode='bind']/@service",
|
2013-07-16 21:14:37 +08:00
|
|
|
doc=_("Host port to bind to."),
|
2013-07-17 21:57:15 +08:00
|
|
|
set_converter=_set_bind_validate, is_int=True)
|
2013-07-16 21:14:37 +08:00
|
|
|
|
|
|
|
def _get_default_protocol(self):
|
|
|
|
if not self.supports_property("protocol"):
|
|
|
|
return None
|
|
|
|
return self.PROTOCOL_RAW
|
2013-09-20 01:27:30 +08:00
|
|
|
protocol = XMLProperty("./protocol/@type",
|
2013-07-16 21:14:37 +08:00
|
|
|
doc=_("Format used when sending data."),
|
|
|
|
default_cb=_get_default_protocol)
|
|
|
|
|
|
|
|
def _get_default_target_type(self):
|
2013-08-08 07:04:18 +08:00
|
|
|
if self.virtual_device_type == "channel":
|
2013-07-16 21:14:37 +08:00
|
|
|
return self.CHANNEL_TARGET_VIRTIO
|
|
|
|
return None
|
2013-09-20 01:27:30 +08:00
|
|
|
target_type = XMLProperty("./target/@type",
|
2013-07-16 21:14:37 +08:00
|
|
|
doc=_("Channel type as exposed in the guest."),
|
|
|
|
default_cb=_get_default_target_type)
|
|
|
|
|
2013-09-20 01:27:30 +08:00
|
|
|
target_address = XMLProperty("./target/@address",
|
2013-07-16 21:14:37 +08:00
|
|
|
doc=_("Guest forward channel address in the guest."))
|
|
|
|
|
2013-09-20 01:27:30 +08:00
|
|
|
target_port = XMLProperty("./target/@port", is_int=True,
|
2013-07-16 21:14:37 +08:00
|
|
|
doc=_("Guest forward channel port in the guest."))
|
|
|
|
|
|
|
|
def _default_target_name(self):
|
|
|
|
if self.type == self.TYPE_SPICEVMC:
|
2013-10-06 01:27:11 +08:00
|
|
|
return self.CHANNEL_NAME_SPICE
|
2013-07-16 21:14:37 +08:00
|
|
|
return None
|
2013-09-20 01:27:30 +08:00
|
|
|
target_name = XMLProperty("./target/@name",
|
2013-03-18 05:06:52 +08:00
|
|
|
doc=_("Sysfs name of virtio port in the guest"),
|
2013-07-16 21:14:37 +08:00
|
|
|
default_cb=_default_target_name)
|
2013-04-14 02:34:52 +08:00
|
|
|
|
|
|
|
|
2013-07-16 21:14:37 +08:00
|
|
|
class VirtualConsoleDevice(_VirtualCharDevice):
|
|
|
|
virtual_device_type = "console"
|
2013-10-05 23:28:41 +08:00
|
|
|
TYPES = [_VirtualCharDevice.TYPE_PTY]
|
2013-04-14 02:34:52 +08:00
|
|
|
|
|
|
|
|
2013-07-16 21:14:37 +08:00
|
|
|
class VirtualSerialDevice(_VirtualCharDevice):
|
|
|
|
virtual_device_type = "serial"
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2013-07-16 21:14:37 +08:00
|
|
|
class VirtualParallelDevice(_VirtualCharDevice):
|
|
|
|
virtual_device_type = "parallel"
|
2013-04-14 02:34:52 +08:00
|
|
|
|
|
|
|
|
2013-07-16 21:14:37 +08:00
|
|
|
class VirtualChannelDevice(_VirtualCharDevice):
|
|
|
|
virtual_device_type = "channel"
|
2013-10-05 23:07:32 +08:00
|
|
|
TYPES = (_VirtualCharDevice._TYPES_FOR_CHANNEL +
|
|
|
|
_VirtualCharDevice._TYPES_FOR_ALL)
|
2013-07-24 20:46:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
VirtualConsoleDevice.register_type()
|
|
|
|
VirtualSerialDevice.register_type()
|
|
|
|
VirtualParallelDevice.register_type()
|
|
|
|
VirtualChannelDevice.register_type()
|