2013-03-17 17:06:52 -04:00
#
2013-10-27 21:59:46 +01:00
# Copyright 2009, 2013 Red Hat, Inc.
2013-03-17 17:06:52 -04:00
#
2018-04-04 14:35:41 +01:00
# This work is licensed under the GNU GPLv2 or later.
2018-03-20 15:00:02 -04:00
# See the COPYING file in the top-level directory.
2013-03-17 17:06:52 -04:00
2018-03-20 12:18:35 -04:00
from . device import Device
2018-03-20 12:27:37 -04:00
from . . xmlbuilder import XMLProperty
2013-03-17 17:06:52 -04:00
2013-04-13 14:34:52 -04:00
2018-03-20 12:18:35 -04:00
class _DeviceChar ( Device ) :
2013-03-17 17:06:52 -04:00
"""
Base class for all character devices . Shouldn ' t be instantiated
directly .
"""
2013-07-16 09:14:37 -04: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 "
2014-03-25 15:42:33 +01:00
TYPE_SPICEPORT = " spiceport "
2016-08-24 16:37:36 -04:00
TYPE_NMDM = " nmdm "
2014-03-25 15:42:33 +01:00
2013-10-05 13:27:11 -04:00
CHANNEL_NAME_SPICE = " com.redhat.spice.0 "
CHANNEL_NAME_QEMUGA = " org.qemu.guest_agent.0 "
CHANNEL_NAME_LIBGUESTFS = " org.libguestfs.channel.0 "
2014-03-25 15:42:33 +01:00
CHANNEL_NAME_SPICE_WEBDAV = " org.spice-space.webdav.0 "
2013-10-05 13:27:11 -04:00
CHANNEL_NAMES = [ CHANNEL_NAME_SPICE ,
CHANNEL_NAME_QEMUGA ,
2014-03-25 15:42:33 +01:00
CHANNEL_NAME_LIBGUESTFS ,
CHANNEL_NAME_SPICE_WEBDAV ]
2013-10-05 13:27:11 -04:00
2018-09-06 12:43:08 -04:00
@classmethod
def get_recommended_types ( cls , _guest ) :
if cls . XML_NAME == " console " :
return [ cls . TYPE_PTY ]
ret = [ cls . TYPE_PTY , cls . TYPE_FILE , cls . TYPE_UNIX ]
if cls . XML_NAME == " channel " :
ret = [ cls . TYPE_SPICEVMC , cls . TYPE_SPICEPORT ] + ret
return ret
2013-10-05 14:04:49 -04:00
@staticmethod
def pretty_channel_name ( val ) :
2018-03-20 12:18:35 -04:00
if val == _DeviceChar . CHANNEL_NAME_SPICE :
2013-10-05 14:04:49 -04:00
return " spice "
2018-03-20 12:18:35 -04:00
if val == _DeviceChar . CHANNEL_NAME_QEMUGA :
2013-10-05 14:04:49 -04:00
return " qemu-ga "
2018-03-20 12:18:35 -04:00
if val == _DeviceChar . CHANNEL_NAME_LIBGUESTFS :
2013-10-05 14:04:49 -04:00
return " libguestfs "
2018-03-20 12:18:35 -04:00
if val == _DeviceChar . CHANNEL_NAME_SPICE_WEBDAV :
2014-03-25 15:42:33 +01:00
return " spice-webdav "
2013-10-05 14:04:49 -04:00
return None
2013-07-16 09:14:37 -04:00
@staticmethod
def pretty_type ( ctype ) :
2013-03-17 17:06:52 -04:00
"""
Return a human readable description of the passed char type
"""
desc = " "
2018-03-20 12:18:35 -04:00
if ctype == _DeviceChar . TYPE_PTY :
2013-03-17 17:06:52 -04:00
desc = _ ( " Pseudo TTY " )
2018-03-20 12:18:35 -04:00
elif ctype == _DeviceChar . TYPE_DEV :
2013-03-17 17:06:52 -04:00
desc = _ ( " Physical host character device " )
2018-03-20 12:18:35 -04:00
elif ctype == _DeviceChar . TYPE_STDIO :
2013-03-17 17:06:52 -04:00
desc = _ ( " Standard input/output " )
2018-03-20 12:18:35 -04:00
elif ctype == _DeviceChar . TYPE_PIPE :
2013-03-17 17:06:52 -04:00
desc = _ ( " Named pipe " )
2018-03-20 12:18:35 -04:00
elif ctype == _DeviceChar . TYPE_FILE :
2013-03-17 17:06:52 -04:00
desc = _ ( " Output to a file " )
2018-03-20 12:18:35 -04:00
elif ctype == _DeviceChar . TYPE_VC :
2013-03-17 17:06:52 -04:00
desc = _ ( " Virtual console " )
2018-03-20 12:18:35 -04:00
elif ctype == _DeviceChar . TYPE_NULL :
2013-03-17 17:06:52 -04:00
desc = _ ( " Null device " )
2018-03-20 12:18:35 -04:00
elif ctype == _DeviceChar . TYPE_TCP :
2013-03-17 17:06:52 -04:00
desc = _ ( " TCP net console " )
2018-03-20 12:18:35 -04:00
elif ctype == _DeviceChar . TYPE_UDP :
2013-03-17 17:06:52 -04:00
desc = _ ( " UDP net console " )
2018-03-20 12:18:35 -04:00
elif ctype == _DeviceChar . TYPE_UNIX :
2013-03-17 17:06:52 -04:00
desc = _ ( " Unix socket " )
2018-03-20 12:18:35 -04:00
elif ctype == _DeviceChar . TYPE_SPICEVMC :
2013-03-17 17:06:52 -04:00
desc = _ ( " Spice agent " )
2018-03-20 12:18:35 -04:00
elif ctype == _DeviceChar . TYPE_SPICEPORT :
2014-03-25 15:42:33 +01:00
desc = _ ( " Spice port " )
2013-03-17 17:06:52 -04:00
return desc
2014-01-19 13:56:06 -05:00
def _set_host_helper ( self , hostparam , portparam , val ) :
def parse_host ( val ) :
host , ignore , port = ( val or " " ) . partition ( " : " )
return host or None , port or None
host , port = parse_host ( val )
2015-09-05 16:27:27 -04:00
if not host :
host = " 127.0.0.1 "
2014-01-19 13:56:06 -05:00
if host :
setattr ( self , hostparam , host )
if port :
setattr ( self , portparam , port )
2019-05-13 11:31:18 -04:00
def set_friendly_connect ( self , val ) :
self . _set_host_helper ( " connect_host " , " connect_service " , val )
2014-01-19 13:56:06 -05:00
def set_friendly_bind ( self , val ) :
2019-05-13 11:31:18 -04:00
self . _set_host_helper ( " bind_host " , " bind_service " , val )
2014-01-19 13:56:06 -05:00
def set_friendly_target ( self , val ) :
self . _set_host_helper ( " target_address " , " target_port " , val )
2013-07-16 09:14:37 -04:00
2018-09-03 16:18:03 -04:00
_XML_PROP_ORDER = [ " type " ,
2019-05-13 11:31:18 -04:00
" bind_host " , " bind_service " ,
" source_mode " , " connect_host " , " connect_service " ,
2019-05-13 11:59:57 -04:00
" source_path " , " source_channel " ,
2018-07-04 16:10:38 +08:00
" target_type " , " target_name " , " target_state " ]
2013-07-16 09:14:37 -04:00
2018-02-22 20:44:09 -05:00
type = XMLProperty ( " ./@type " )
2019-05-13 11:31:18 -04:00
2019-05-13 11:59:57 -04:00
source_path = XMLProperty ( " ./source/@path " )
2019-05-13 11:31:18 -04:00
source_channel = XMLProperty ( " ./source/@channel " )
source_master = XMLProperty ( " ./source/@master " )
source_slave = XMLProperty ( " ./source/@slave " )
source_mode = XMLProperty ( " ./source/@mode " )
2013-09-24 09:25:05 -04:00
2019-05-13 11:31:18 -04:00
target_address = XMLProperty ( " ./target/@address " )
target_port = XMLProperty ( " ./target/@port " , is_int = True )
target_type = XMLProperty ( " ./target/@type " )
target_name = XMLProperty ( " ./target/@name " )
target_state = XMLProperty ( " ./target/@state " )
protocol = XMLProperty ( " ./protocol/@type " )
log_file = XMLProperty ( " ./log/@file " )
log_append = XMLProperty ( " ./log/@append " , is_onoff = True )
# Convenience source helpers for setting connect/bind host and service
connect_host = XMLProperty ( " ./source[@mode= ' connect ' ]/@host " )
connect_service = XMLProperty (
2018-09-03 16:18:03 -04:00
" ./source[@mode= ' connect ' ]/@service " , is_int = True )
bind_host = XMLProperty ( " ./source[@mode= ' bind ' ]/@host " )
2019-05-13 11:31:18 -04:00
bind_service = XMLProperty ( " ./source[@mode= ' bind ' ]/@service " , is_int = True )
2015-09-05 16:27:27 -04:00
#######################
# Remaining XML props #
#######################
2013-07-16 09:14:37 -04:00
2013-04-13 14:34:52 -04:00
2018-09-03 16:18:03 -04:00
##################
# Default config #
##################
def set_defaults ( self , _guest ) :
2019-05-13 13:18:41 -04:00
if ( not self . source_mode and
self . type in [ self . TYPE_UNIX , self . TYPE_TCP ] ) :
2018-09-06 12:43:08 -04:00
self . source_mode = " bind "
2018-09-03 16:18:03 -04:00
if not self . target_type and self . DEVICE_TYPE == " channel " :
2018-09-06 11:49:09 -04:00
self . target_type = " virtio "
2018-09-03 16:18:03 -04:00
if not self . target_name and self . type == self . TYPE_SPICEVMC :
self . target_name = self . CHANNEL_NAME_SPICE
2018-03-20 12:18:35 -04:00
class DeviceConsole ( _DeviceChar ) :
2018-03-21 10:53:34 -04:00
XML_NAME = " console "
2013-04-13 14:34:52 -04:00
2018-03-20 12:18:35 -04:00
class DeviceSerial ( _DeviceChar ) :
2018-03-21 10:53:34 -04:00
XML_NAME = " serial "
2013-03-17 17:06:52 -04:00
2013-04-13 14:34:52 -04:00
2018-03-20 12:18:35 -04:00
class DeviceParallel ( _DeviceChar ) :
2018-03-21 10:53:34 -04:00
XML_NAME = " parallel "
2013-04-13 14:34:52 -04:00
2018-03-20 12:18:35 -04:00
class DeviceChannel ( _DeviceChar ) :
2018-03-21 10:53:34 -04:00
XML_NAME = " channel "