<s> """<STR_LIT>""" <EOL> import errno <EOL> import os <EOL> import os . path <EOL> import re <EOL> import tempfile <EOL> import time <EOL> import logging <EOL> import pwd <EOL> import shutil <EOL> import urllib2 <EOL> from bitarray import bitarray <EOL> try : <EOL> import psutil <EOL> except ImportError : <EOL> psutil = None <EOL> try : <EOL> import fdsend <EOL> except ImportError : <EOL> fdsend = None <EOL> from ganeti import utils <EOL> from ganeti import constants <EOL> from ganeti import errors <EOL> from ganeti import serializer <EOL> from ganeti import objects <EOL> from ganeti import uidpool <EOL> from ganeti import ssconf <EOL> from ganeti import netutils <EOL> from ganeti import pathutils <EOL> from ganeti . hypervisor import hv_base <EOL> from ganeti . utils import wrapper as utils_wrapper <EOL> from ganeti . hypervisor . hv_kvm . monitor import QmpConnection , QmpMessage , MonitorSocket <EOL> from ganeti . hypervisor . hv_kvm . netdev import OpenTap <EOL> _KVM_NETWORK_SCRIPT = pathutils . CONF_DIR + "<STR_LIT>" <EOL> _KVM_START_PAUSED_FLAG = "<STR_LIT>" <EOL> _SPICE_ADDITIONAL_PARAMS = frozenset ( [ <EOL> constants . HV_KVM_SPICE_IP_VERSION , <EOL> constants . HV_KVM_SPICE_PASSWORD_FILE , <EOL> constants . HV_KVM_SPICE_LOSSLESS_IMG_COMPR , <EOL> constants . HV_KVM_SPICE_JPEG_IMG_COMPR , <EOL> constants . HV_KVM_SPICE_ZLIB_GLZ_IMG_COMPR , <EOL> constants . HV_KVM_SPICE_STREAMING_VIDEO_DETECTION , <EOL> constants . HV_KVM_SPICE_USE_TLS , <EOL> ] ) <EOL> _KVM_NICS_RUNTIME_INDEX = <NUM_LIT:1> <EOL> _KVM_DISKS_RUNTIME_INDEX = <NUM_LIT:3> <EOL> _DEVICE_RUNTIME_INDEX = { <EOL> constants . HOTPLUG_TARGET_DISK : _KVM_DISKS_RUNTIME_INDEX , <EOL> constants . HOTPLUG_TARGET_NIC : _KVM_NICS_RUNTIME_INDEX <EOL> } <EOL> _FIND_RUNTIME_ENTRY = { <EOL> constants . HOTPLUG_TARGET_NIC : <EOL> lambda nic , kvm_nics : [ n for n in kvm_nics if n . uuid == nic . uuid ] , <EOL> constants . HOTPLUG_TARGET_DISK : <EOL> lambda disk , kvm_disks : [ ( d , l , u ) for ( d , l , u ) in kvm_disks <EOL> if d . uuid == disk . uuid ] <EOL> } <EOL> _RUNTIME_DEVICE = { <EOL> constants . HOTPLUG_TARGET_NIC : lambda d : d , <EOL> constants . HOTPLUG_TARGET_DISK : lambda ( d , e , _ ) : d <EOL> } <EOL> _RUNTIME_ENTRY = { <EOL> constants . HOTPLUG_TARGET_NIC : lambda d , e : d , <EOL> constants . HOTPLUG_TARGET_DISK : lambda d , e : ( d , e [ <NUM_LIT:0> ] , e [ <NUM_LIT:1> ] ) <EOL> } <EOL> _DEVICE_TYPE = { <EOL> constants . HOTPLUG_TARGET_NIC : lambda hvp : hvp [ constants . HV_NIC_TYPE ] , <EOL> constants . HOTPLUG_TARGET_DISK : lambda hvp : hvp [ constants . HV_DISK_TYPE ] , <EOL> } <EOL> _DEVICE_DRIVER = { <EOL> constants . HOTPLUG_TARGET_NIC : <EOL> lambda ht : "<STR_LIT>" if ht == constants . HT_NIC_PARAVIRTUAL else ht , <EOL> constants . HOTPLUG_TARGET_DISK : <EOL> lambda ht : "<STR_LIT>" if ht == constants . HT_DISK_PARAVIRTUAL else ht , <EOL> } <EOL> _DEVICE_BUS = { <EOL> constants . HOTPLUG_TARGET_NIC : <EOL> lambda _ : _PCI_BUS , <EOL> constants . HOTPLUG_TARGET_DISK : <EOL> lambda ht : _SCSI_BUS if ht in constants . HT_SCSI_DEVICE_TYPES else _PCI_BUS <EOL> } <EOL> _HOTPLUGGABLE_DEVICE_TYPES = { <EOL> constants . HOTPLUG_TARGET_NIC : [ <EOL> constants . HT_NIC_E1000 , <EOL> constants . HT_NIC_I82551 , <EOL> constants . HT_NIC_I8259ER , <EOL> constants . HT_NIC_I85557B , <EOL> constants . HT_NIC_NE2K_PCI , <EOL> constants . HT_NIC_PARAVIRTUAL , <EOL> constants . HT_NIC_PCNET , <EOL> constants . HT_NIC_RTL8139 , <EOL> ] , <EOL> constants . HOTPLUG_TARGET_DISK : [ <EOL> constants . HT_DISK_PARAVIRTUAL , <EOL> constants . HT_DISK_SCSI_BLOCK , <EOL> constants . HT_DISK_SCSI_GENERIC , <EOL> constants . HT_DISK_SCSI_HD , <EOL> constants . HT_DISK_SCSI_CD , <EOL> ] <EOL> } <EOL> _PCI_BUS = "<STR_LIT>" <EOL> _SCSI_BUS = "<STR_LIT>" <EOL> _MIGRATION_CAPS_DELIM = "<STR_LIT::>" <EOL> def _with_qmp ( fn ) : <EOL> """<STR_LIT>""" <EOL> def wrapper ( self , instance , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not getattr ( self , "<STR_LIT>" , None ) : <EOL> filename = self . _InstanceQmpMonitor ( instance . name ) <EOL> self . qmp = QmpConnection ( filename ) <EOL> return fn ( self , instance , * args , ** kwargs ) <EOL> return wrapper <EOL> def _GetDriveURI ( disk , link , uri ) : <EOL> """<STR_LIT>""" <EOL> access_mode = disk . params . get ( constants . LDP_ACCESS , <EOL> constants . DISK_KERNELSPACE ) <EOL> if ( uri and access_mode == constants . DISK_USERSPACE ) : <EOL> drive_uri = uri <EOL> else : <EOL> drive_uri = link <EOL> return drive_uri <EOL> def _GenerateDeviceKVMId ( dev_type , dev ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( dev_type . lower ( ) , dev . uuid . rsplit ( "<STR_LIT:->" , <NUM_LIT:2> ) [ <NUM_LIT:0> ] ) <EOL> def _GenerateDeviceHVInfoStr ( hvinfo ) : <EOL> """<STR_LIT>""" <EOL> d = dict ( hvinfo ) <EOL> hvinfo_str = d . pop ( "<STR_LIT>" ) <EOL> for k , v in d . items ( ) : <EOL> hvinfo_str += "<STR_LIT>" % ( k , v ) <EOL> return hvinfo_str <EOL> def _GenerateDeviceHVInfo ( dev_type , kvm_devid , hv_dev_type , bus_slots ) : <EOL> """<STR_LIT>""" <EOL> driver = _DEVICE_DRIVER [ dev_type ] ( hv_dev_type ) <EOL> bus = _DEVICE_BUS [ dev_type ] ( hv_dev_type ) <EOL> slots = bus_slots [ bus ] <EOL> slot = utils . GetFreeSlot ( slots , reserve = True ) <EOL> hvinfo = { <EOL> "<STR_LIT>" : driver , <EOL> "<STR_LIT:id>" : kvm_devid , <EOL> "<STR_LIT>" : bus , <EOL> } <EOL> if bus == _PCI_BUS : <EOL> hvinfo . update ( { <EOL> "<STR_LIT>" : hex ( slot ) , <EOL> } ) <EOL> elif bus == _SCSI_BUS : <EOL> hvinfo . update ( { <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : slot , <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> } ) <EOL> return hvinfo <EOL> def _GetExistingDeviceInfo ( dev_type , device , runtime ) : <EOL> """<STR_LIT>""" <EOL> index = _DEVICE_RUNTIME_INDEX [ dev_type ] <EOL> found = _FIND_RUNTIME_ENTRY [ dev_type ] ( device , runtime [ index ] ) <EOL> if not found : <EOL> raise errors . HotplugError ( "<STR_LIT>" % <EOL> ( dev_type , device . uuid ) ) <EOL> return found [ <NUM_LIT:0> ] <EOL> def _UpgradeSerializedRuntime ( serialized_runtime ) : <EOL> """<STR_LIT>""" <EOL> loaded_runtime = serializer . Load ( serialized_runtime ) <EOL> kvm_cmd , serialized_nics , hvparams = loaded_runtime [ : <NUM_LIT:3> ] <EOL> if len ( loaded_runtime ) >= <NUM_LIT:4> : <EOL> serialized_disks = loaded_runtime [ <NUM_LIT:3> ] <EOL> else : <EOL> serialized_disks = [ ] <EOL> def update_hvinfo ( dev , dev_type ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT>" not in dev : <EOL> dev [ "<STR_LIT>" ] = { } <EOL> uuid = dev [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in dev : <EOL> dev [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] = "<STR_LIT>" % ( dev_type . lower ( ) , <EOL> uuid . split ( "<STR_LIT:->" ) [ <NUM_LIT:0> ] , <EOL> "<STR_LIT>" , <EOL> dev [ "<STR_LIT>" ] ) <EOL> dev [ "<STR_LIT>" ] [ "<STR_LIT>" ] = hex ( dev [ "<STR_LIT>" ] ) <EOL> dev [ "<STR_LIT>" ] [ "<STR_LIT>" ] = _PCI_BUS <EOL> del dev [ "<STR_LIT>" ] <EOL> for nic in serialized_nics : <EOL> if "<STR_LIT>" not in nic : <EOL> nic [ "<STR_LIT>" ] = utils . NewUUID ( ) <EOL> update_hvinfo ( nic , constants . HOTPLUG_TARGET_NIC ) <EOL> for disk_entry in serialized_disks : <EOL> update_hvinfo ( disk_entry [ <NUM_LIT:0> ] , constants . HOTPLUG_TARGET_DISK ) <EOL> return kvm_cmd , serialized_nics , hvparams , serialized_disks <EOL> def _AnalyzeSerializedRuntime ( serialized_runtime ) : <EOL> """<STR_LIT>""" <EOL> kvm_cmd , serialized_nics , hvparams , serialized_disks = _UpgradeSerializedRuntime ( serialized_runtime ) <EOL> kvm_nics = [ objects . NIC . FromDict ( snic ) for snic in serialized_nics ] <EOL> kvm_disks = [ ( objects . Disk . FromDict ( sdisk ) , link , uri ) <EOL> for sdisk , link , uri in serialized_disks ] <EOL> return ( kvm_cmd , kvm_nics , hvparams , kvm_disks ) <EOL> class HeadRequest ( urllib2 . Request ) : <EOL> def get_method ( self ) : <EOL> return "<STR_LIT>" <EOL> def _CheckUrl ( url ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> urllib2 . urlopen ( HeadRequest ( url ) ) <EOL> return True <EOL> except urllib2 . URLError : <EOL> return False <EOL> class KVMHypervisor ( hv_base . BaseHypervisor ) : <EOL> """<STR_LIT>""" <EOL> CAN_MIGRATE = True <EOL> _ROOT_DIR = pathutils . RUN_DIR + "<STR_LIT>" <EOL> _PIDS_DIR = _ROOT_DIR + "<STR_LIT>" <EOL> _UIDS_DIR = _ROOT_DIR + "<STR_LIT>" <EOL> _CTRL_DIR = _ROOT_DIR + "<STR_LIT>" <EOL> _CONF_DIR = _ROOT_DIR + "<STR_LIT>" <EOL> _NICS_DIR = _ROOT_DIR + "<STR_LIT>" <EOL> _KEYMAP_DIR = _ROOT_DIR + "<STR_LIT>" <EOL> _CHROOT_DIR = _ROOT_DIR + "<STR_LIT>" <EOL> _CHROOT_QUARANTINE_DIR = _ROOT_DIR + "<STR_LIT>" <EOL> _DIRS = [ _ROOT_DIR , _PIDS_DIR , _UIDS_DIR , _CTRL_DIR , _CONF_DIR , _NICS_DIR , <EOL> _CHROOT_DIR , _CHROOT_QUARANTINE_DIR , _KEYMAP_DIR ] <EOL> PARAMETERS = { <EOL> constants . HV_KVM_PATH : hv_base . REQ_FILE_CHECK , <EOL> constants . HV_KERNEL_PATH : hv_base . OPT_FILE_CHECK , <EOL> constants . HV_INITRD_PATH : hv_base . OPT_FILE_CHECK , <EOL> constants . HV_ROOT_PATH : hv_base . NO_CHECK , <EOL> constants . HV_KERNEL_ARGS : hv_base . NO_CHECK , <EOL> constants . HV_ACPI : hv_base . NO_CHECK , <EOL> constants . HV_SERIAL_CONSOLE : hv_base . NO_CHECK , <EOL> constants . HV_SERIAL_SPEED : hv_base . NO_CHECK , <EOL> constants . HV_VNC_BIND_ADDRESS : hv_base . NO_CHECK , <EOL> constants . HV_VNC_TLS : hv_base . NO_CHECK , <EOL> constants . HV_VNC_X509 : hv_base . OPT_DIR_CHECK , <EOL> constants . HV_VNC_X509_VERIFY : hv_base . NO_CHECK , <EOL> constants . HV_VNC_PASSWORD_FILE : hv_base . OPT_FILE_CHECK , <EOL> constants . HV_KVM_SPICE_BIND : hv_base . NO_CHECK , <EOL> constants . HV_KVM_SPICE_IP_VERSION : <EOL> ( False , lambda x : ( x == constants . IFACE_NO_IP_VERSION_SPECIFIED or <EOL> x in constants . VALID_IP_VERSIONS ) , <EOL> "<STR_LIT>" , <EOL> None , None ) , <EOL> constants . HV_KVM_SPICE_PASSWORD_FILE : hv_base . OPT_FILE_CHECK , <EOL> constants . HV_KVM_SPICE_LOSSLESS_IMG_COMPR : <EOL> hv_base . ParamInSet ( <EOL> False , constants . HT_KVM_SPICE_VALID_LOSSLESS_IMG_COMPR_OPTIONS ) , <EOL> constants . HV_KVM_SPICE_JPEG_IMG_COMPR : <EOL> hv_base . ParamInSet ( <EOL> False , constants . HT_KVM_SPICE_VALID_LOSSY_IMG_COMPR_OPTIONS ) , <EOL> constants . HV_KVM_SPICE_ZLIB_GLZ_IMG_COMPR : <EOL> hv_base . ParamInSet ( <EOL> False , constants . HT_KVM_SPICE_VALID_LOSSY_IMG_COMPR_OPTIONS ) , <EOL> constants . HV_KVM_SPICE_STREAMING_VIDEO_DETECTION : <EOL> hv_base . ParamInSet ( <EOL> False , constants . HT_KVM_SPICE_VALID_VIDEO_STREAM_DETECTION_OPTIONS ) , <EOL> constants . HV_KVM_SPICE_AUDIO_COMPR : hv_base . NO_CHECK , <EOL> constants . HV_KVM_SPICE_USE_TLS : hv_base . NO_CHECK , <EOL> constants . HV_KVM_SPICE_TLS_CIPHERS : hv_base . NO_CHECK , <EOL> constants . HV_KVM_SPICE_USE_VDAGENT : hv_base . NO_CHECK , <EOL> constants . HV_KVM_FLOPPY_IMAGE_PATH : hv_base . OPT_FILE_CHECK , <EOL> constants . HV_CDROM_IMAGE_PATH : hv_base . OPT_FILE_OR_URL_CHECK , <EOL> constants . HV_KVM_CDROM2_IMAGE_PATH : hv_base . OPT_FILE_OR_URL_CHECK , <EOL> constants . HV_BOOT_ORDER : <EOL> hv_base . ParamInSet ( True , constants . HT_KVM_VALID_BO_TYPES ) , <EOL> constants . HV_NIC_TYPE : <EOL> hv_base . ParamInSet ( True , constants . HT_KVM_VALID_NIC_TYPES ) , <EOL> constants . HV_DISK_TYPE : <EOL> hv_base . ParamInSet ( True , constants . HT_KVM_VALID_DISK_TYPES ) , <EOL> constants . HV_KVM_SCSI_CONTROLLER_TYPE : <EOL> hv_base . ParamInSet ( True , constants . HT_KVM_VALID_SCSI_CONTROLLER_TYPES ) , <EOL> constants . HV_KVM_CDROM_DISK_TYPE : <EOL> hv_base . ParamInSet ( False , constants . HT_KVM_VALID_DISK_TYPES ) , <EOL> constants . HV_USB_MOUSE : <EOL> hv_base . ParamInSet ( False , constants . HT_KVM_VALID_MOUSE_TYPES ) , <EOL> constants . HV_KEYMAP : hv_base . NO_CHECK , <EOL> constants . HV_MIGRATION_PORT : hv_base . REQ_NET_PORT_CHECK , <EOL> constants . HV_MIGRATION_BANDWIDTH : hv_base . REQ_NONNEGATIVE_INT_CHECK , <EOL> constants . HV_MIGRATION_DOWNTIME : hv_base . REQ_NONNEGATIVE_INT_CHECK , <EOL> constants . HV_MIGRATION_MODE : hv_base . MIGRATION_MODE_CHECK , <EOL> constants . HV_USE_LOCALTIME : hv_base . NO_CHECK , <EOL> constants . HV_DISK_CACHE : <EOL> hv_base . ParamInSet ( True , constants . HT_VALID_CACHE_TYPES ) , <EOL> constants . HV_KVM_DISK_AIO : <EOL> hv_base . ParamInSet ( False , constants . HT_KVM_VALID_AIO_TYPES ) , <EOL> constants . HV_SECURITY_MODEL : <EOL> hv_base . ParamInSet ( True , constants . HT_KVM_VALID_SM_TYPES ) , <EOL> constants . HV_SECURITY_DOMAIN : hv_base . NO_CHECK , <EOL> constants . HV_KVM_FLAG : <EOL> hv_base . ParamInSet ( False , constants . HT_KVM_FLAG_VALUES ) , <EOL> constants . HV_VHOST_NET : hv_base . NO_CHECK , <EOL> constants . HV_VIRTIO_NET_QUEUES : hv_base . OPT_VIRTIO_NET_QUEUES_CHECK , <EOL> constants . HV_KVM_USE_CHROOT : hv_base . NO_CHECK , <EOL> constants . HV_KVM_USER_SHUTDOWN : hv_base . NO_CHECK , <EOL> constants . HV_MEM_PATH : hv_base . OPT_DIR_CHECK , <EOL> constants . HV_REBOOT_BEHAVIOR : <EOL> hv_base . ParamInSet ( True , constants . REBOOT_BEHAVIORS ) , <EOL> constants . HV_CPU_MASK : hv_base . OPT_MULTI_CPU_MASK_CHECK , <EOL> constants . HV_WORKER_CPU_MASK : hv_base . OPT_MULTI_CPU_MASK_CHECK , <EOL> constants . HV_CPU_TYPE : hv_base . NO_CHECK , <EOL> constants . HV_CPU_CORES : hv_base . OPT_NONNEGATIVE_INT_CHECK , <EOL> constants . HV_CPU_THREADS : hv_base . OPT_NONNEGATIVE_INT_CHECK , <EOL> constants . HV_CPU_SOCKETS : hv_base . OPT_NONNEGATIVE_INT_CHECK , <EOL> constants . HV_SOUNDHW : hv_base . NO_CHECK , <EOL> constants . HV_USB_DEVICES : hv_base . NO_CHECK , <EOL> constants . HV_VGA : hv_base . NO_CHECK , <EOL> constants . HV_KVM_EXTRA : hv_base . NO_CHECK , <EOL> constants . HV_KVM_MACHINE_VERSION : hv_base . NO_CHECK , <EOL> constants . HV_KVM_MIGRATION_CAPS : hv_base . NO_CHECK , <EOL> constants . HV_KVM_PCI_RESERVATIONS : <EOL> ( False , lambda x : ( x >= <NUM_LIT:0> and x <= constants . QEMU_PCI_SLOTS ) , <EOL> "<STR_LIT>" % <EOL> constants . QEMU_PCI_SLOTS , <EOL> None , None ) , <EOL> constants . HV_VNET_HDR : hv_base . NO_CHECK , <EOL> } <EOL> _VIRTIO = "<STR_LIT>" <EOL> _VIRTIO_NET_PCI = "<STR_LIT>" <EOL> _VIRTIO_BLK_PCI = "<STR_LIT>" <EOL> _MIGRATION_STATUS_RE = re . compile ( r"<STR_LIT>" , <EOL> re . M | re . I ) <EOL> _MIGRATION_PROGRESS_RE = re . compile ( r"<STR_LIT>" <EOL> r"<STR_LIT>" <EOL> r"<STR_LIT>" , re . I ) <EOL> _MIGRATION_INFO_MAX_BAD_ANSWERS = <NUM_LIT:5> <EOL> _MIGRATION_INFO_RETRY_DELAY = <NUM_LIT:2> <EOL> _VERSION_RE = re . compile ( r"<STR_LIT>" ) <EOL> _CPU_INFO_RE = re . compile ( r"<STR_LIT>" , re . I ) <EOL> _CPU_INFO_CMD = "<STR_LIT>" <EOL> _CONT_CMD = "<STR_LIT>" <EOL> _DEFAULT_MACHINE_VERSION_RE = re . compile ( r"<STR_LIT>" , re . M ) <EOL> _CHECK_MACHINE_VERSION_RE = staticmethod ( lambda x : re . compile ( r"<STR_LIT>" % x , re . M ) ) <EOL> _QMP_RE = re . compile ( r"<STR_LIT>" , re . M ) <EOL> _SPICE_RE = re . compile ( r"<STR_LIT>" , re . M ) <EOL> _VHOST_RE = re . compile ( r"<STR_LIT>" , re . M ) <EOL> _VIRTIO_NET_QUEUES_RE = re . compile ( r"<STR_LIT>" , re . M ) <EOL> _ENABLE_KVM_RE = re . compile ( r"<STR_LIT>" , re . M ) <EOL> _DISABLE_KVM_RE = re . compile ( r"<STR_LIT>" , re . M ) <EOL> _NETDEV_RE = re . compile ( r"<STR_LIT>" , re . M ) <EOL> _DISPLAY_RE = re . compile ( r"<STR_LIT>" , re . M ) <EOL> _MACHINE_RE = re . compile ( r"<STR_LIT>" , re . M ) <EOL> _DEVICE_DRIVER_SUPPORTED = staticmethod ( lambda drv , devlist : <EOL> re . compile ( r"<STR_LIT>" % drv , re . M ) . search ( devlist ) ) <EOL> _BOOT_RE = re . compile ( r"<STR_LIT>" , re . M | re . S ) <EOL> _UUID_RE = re . compile ( r"<STR_LIT>" , re . M ) <EOL> _INFO_VERSION_RE = re . compile ( r'<STR_LIT>' , re . M ) <EOL> _INFO_VERSION_CMD = "<STR_LIT>" <EOL> _DEFAULT_PCI_RESERVATIONS = "<STR_LIT>" <EOL> _DEFAULT_SCSI_RESERVATIONS = "<STR_LIT>" <EOL> ANCILLARY_FILES = [ <EOL> _KVM_NETWORK_SCRIPT , <EOL> ] <EOL> ANCILLARY_FILES_OPT = [ <EOL> _KVM_NETWORK_SCRIPT , <EOL> ] <EOL> _KVMOPT_HELP = "<STR_LIT>" <EOL> _KVMOPT_MLIST = "<STR_LIT>" <EOL> _KVMOPT_DEVICELIST = "<STR_LIT>" <EOL> _KVMOPTS_CMDS = { <EOL> _KVMOPT_HELP : ( [ "<STR_LIT>" ] , False ) , <EOL> _KVMOPT_MLIST : ( [ "<STR_LIT>" , "<STR_LIT:?>" ] , False ) , <EOL> _KVMOPT_DEVICELIST : ( [ "<STR_LIT>" , "<STR_LIT:?>" ] , True ) , <EOL> } <EOL> def __init__ ( self ) : <EOL> hv_base . BaseHypervisor . __init__ ( self ) <EOL> dirs = [ ( dname , constants . RUN_DIRS_MODE ) for dname in self . _DIRS ] <EOL> utils . EnsureDirs ( dirs ) <EOL> self . qmp = None <EOL> @ staticmethod <EOL> def VersionsSafeForMigration ( src , target ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> @ classmethod <EOL> def _InstancePidFile ( cls , instance_name ) : <EOL> """<STR_LIT>""" <EOL> return utils . PathJoin ( cls . _PIDS_DIR , instance_name ) <EOL> @ classmethod <EOL> def _InstanceUidFile ( cls , instance_name ) : <EOL> """<STR_LIT>""" <EOL> return utils . PathJoin ( cls . _UIDS_DIR , instance_name ) <EOL> @ classmethod <EOL> def _InstancePidInfo ( cls , pid ) : <EOL> """<STR_LIT>""" <EOL> alive = utils . IsProcessAlive ( pid ) <EOL> if not alive : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % pid ) <EOL> cmdline_file = utils . PathJoin ( "<STR_LIT>" , str ( pid ) , "<STR_LIT>" ) <EOL> try : <EOL> cmdline = utils . ReadFile ( cmdline_file ) <EOL> except EnvironmentError , err : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % <EOL> ( pid , err ) ) <EOL> instance = None <EOL> memory = <NUM_LIT:0> <EOL> vcpus = <NUM_LIT:0> <EOL> arg_list = cmdline . split ( "<STR_LIT:\x00>" ) <EOL> while arg_list : <EOL> arg = arg_list . pop ( <NUM_LIT:0> ) <EOL> if arg == "<STR_LIT>" : <EOL> instance = arg_list . pop ( <NUM_LIT:0> ) <EOL> elif arg == "<STR_LIT>" : <EOL> memory = int ( arg_list . pop ( <NUM_LIT:0> ) ) <EOL> elif arg == "<STR_LIT>" : <EOL> vcpus = int ( arg_list . pop ( <NUM_LIT:0> ) . split ( "<STR_LIT:U+002C>" ) [ <NUM_LIT:0> ] ) <EOL> if instance is None : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" % pid ) <EOL> return ( instance , memory , vcpus ) <EOL> @ classmethod <EOL> def _InstancePidAlive ( cls , instance_name ) : <EOL> """<STR_LIT>""" <EOL> pidfile = cls . _InstancePidFile ( instance_name ) <EOL> pid = utils . ReadPidFile ( pidfile ) <EOL> alive = False <EOL> try : <EOL> cmd_instance = cls . _InstancePidInfo ( pid ) [ <NUM_LIT:0> ] <EOL> alive = ( cmd_instance == instance_name ) <EOL> except errors . HypervisorError : <EOL> pass <EOL> return ( pidfile , pid , alive ) <EOL> @ classmethod <EOL> def _CheckDown ( cls , instance_name ) : <EOL> """<STR_LIT>""" <EOL> alive = cls . _InstancePidAlive ( instance_name ) [ <NUM_LIT:2> ] <EOL> if alive : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % <EOL> ( instance_name , "<STR_LIT>" ) ) <EOL> @ classmethod <EOL> def _InstanceMonitor ( cls , instance_name ) : <EOL> """<STR_LIT>""" <EOL> return utils . PathJoin ( cls . _CTRL_DIR , "<STR_LIT>" % instance_name ) <EOL> @ classmethod <EOL> def _InstanceSerial ( cls , instance_name ) : <EOL> """<STR_LIT>""" <EOL> return utils . PathJoin ( cls . _CTRL_DIR , "<STR_LIT>" % instance_name ) <EOL> @ classmethod <EOL> def _InstanceQmpMonitor ( cls , instance_name ) : <EOL> """<STR_LIT>""" <EOL> return utils . PathJoin ( cls . _CTRL_DIR , "<STR_LIT>" % instance_name ) <EOL> @ classmethod <EOL> def _InstanceKvmdMonitor ( cls , instance_name ) : <EOL> """<STR_LIT>""" <EOL> return utils . PathJoin ( cls . _CTRL_DIR , "<STR_LIT>" % instance_name ) <EOL> @ classmethod <EOL> def _InstanceShutdownMonitor ( cls , instance_name ) : <EOL> """<STR_LIT>""" <EOL> return utils . PathJoin ( cls . _CTRL_DIR , "<STR_LIT>" % instance_name ) <EOL> @ staticmethod <EOL> def _SocatUnixConsoleParams ( ) : <EOL> """<STR_LIT>""" <EOL> if constants . SOCAT_USE_ESCAPE : <EOL> return "<STR_LIT>" % constants . SOCAT_ESCAPE_CODE <EOL> else : <EOL> return "<STR_LIT>" <EOL> @ classmethod <EOL> def _InstanceKVMRuntime ( cls , instance_name ) : <EOL> """<STR_LIT>""" <EOL> return utils . PathJoin ( cls . _CONF_DIR , "<STR_LIT>" % instance_name ) <EOL> @ classmethod <EOL> def _InstanceChrootDir ( cls , instance_name ) : <EOL> """<STR_LIT>""" <EOL> return utils . PathJoin ( cls . _CHROOT_DIR , instance_name ) <EOL> @ classmethod <EOL> def _InstanceNICDir ( cls , instance_name ) : <EOL> """<STR_LIT>""" <EOL> return utils . PathJoin ( cls . _NICS_DIR , instance_name ) <EOL> @ classmethod <EOL> def _InstanceNICFile ( cls , instance_name , seq ) : <EOL> """<STR_LIT>""" <EOL> return utils . PathJoin ( cls . _InstanceNICDir ( instance_name ) , str ( seq ) ) <EOL> @ classmethod <EOL> def _InstanceKeymapFile ( cls , instance_name ) : <EOL> """<STR_LIT>""" <EOL> return utils . PathJoin ( cls . _KEYMAP_DIR , instance_name ) <EOL> @ classmethod <EOL> def _TryReadUidFile ( cls , uid_file ) : <EOL> """<STR_LIT>""" <EOL> if os . path . exists ( uid_file ) : <EOL> try : <EOL> uid = int ( utils . ReadOneLineFile ( uid_file ) ) <EOL> return uid <EOL> except EnvironmentError : <EOL> logging . warning ( "<STR_LIT>" , exc_info = True ) <EOL> except ( TypeError , ValueError ) : <EOL> logging . warning ( "<STR_LIT>" , exc_info = True ) <EOL> return None <EOL> @ classmethod <EOL> def _RemoveInstanceRuntimeFiles ( cls , pidfile , instance_name ) : <EOL> """<STR_LIT>""" <EOL> utils . RemoveFile ( pidfile ) <EOL> utils . RemoveFile ( cls . _InstanceMonitor ( instance_name ) ) <EOL> utils . RemoveFile ( cls . _InstanceSerial ( instance_name ) ) <EOL> utils . RemoveFile ( cls . _InstanceQmpMonitor ( instance_name ) ) <EOL> utils . RemoveFile ( cls . _InstanceKVMRuntime ( instance_name ) ) <EOL> utils . RemoveFile ( cls . _InstanceKeymapFile ( instance_name ) ) <EOL> uid_file = cls . _InstanceUidFile ( instance_name ) <EOL> uid = cls . _TryReadUidFile ( uid_file ) <EOL> utils . RemoveFile ( uid_file ) <EOL> if uid is not None : <EOL> uidpool . ReleaseUid ( uid ) <EOL> try : <EOL> shutil . rmtree ( cls . _InstanceNICDir ( instance_name ) ) <EOL> except OSError , err : <EOL> if err . errno != errno . ENOENT : <EOL> raise <EOL> try : <EOL> chroot_dir = cls . _InstanceChrootDir ( instance_name ) <EOL> utils . RemoveDir ( chroot_dir ) <EOL> except OSError , err : <EOL> if err . errno == errno . ENOTEMPTY : <EOL> new_chroot_dir = tempfile . mkdtemp ( dir = cls . _CHROOT_QUARANTINE_DIR , <EOL> prefix = "<STR_LIT>" % <EOL> ( instance_name , <EOL> utils . TimestampForFilename ( ) ) ) <EOL> logging . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> instance_name , new_chroot_dir ) <EOL> utils . RenameFile ( chroot_dir , new_chroot_dir ) <EOL> else : <EOL> raise <EOL> @ staticmethod <EOL> def _ConfigureNIC ( instance , seq , nic , tap ) : <EOL> """<STR_LIT>""" <EOL> hv_base . ConfigureNIC ( [ pathutils . KVM_IFUP , tap ] , instance , seq , nic , tap ) <EOL> @ classmethod <EOL> def _SetProcessAffinity ( cls , process_id , cpus ) : <EOL> """<STR_LIT>""" <EOL> if psutil is None : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> target_process = psutil . Process ( process_id ) <EOL> if cpus == constants . CPU_PINNING_OFF : <EOL> target_process . set_cpu_affinity ( range ( psutil . cpu_count ( ) ) ) <EOL> else : <EOL> target_process . set_cpu_affinity ( cpus ) <EOL> for p in target_process . get_children ( recursive = True ) : <EOL> p . set_cpu_affinity ( cpus ) <EOL> @ classmethod <EOL> def _AssignCpuAffinity ( cls , cpu_mask , worker_cpu_mask , process_id , <EOL> thread_dict ) : <EOL> """<STR_LIT>""" <EOL> worker_cpu_list = utils . ParseCpuMask ( worker_cpu_mask ) <EOL> cls . _SetProcessAffinity ( process_id , worker_cpu_list ) <EOL> cpu_list = utils . ParseMultiCpuMask ( cpu_mask ) <EOL> if len ( cpu_list ) == <NUM_LIT:1> : <EOL> all_cpu_mapping = cpu_list [ <NUM_LIT:0> ] <EOL> if all_cpu_mapping != constants . CPU_PINNING_OFF : <EOL> for vcpu in thread_dict : <EOL> cls . _SetProcessAffinity ( thread_dict [ vcpu ] , all_cpu_mapping ) <EOL> else : <EOL> assert len ( thread_dict ) == len ( cpu_list ) <EOL> for i , vcpu in enumerate ( cpu_list ) : <EOL> cls . _SetProcessAffinity ( thread_dict [ i ] , vcpu ) <EOL> def _GetVcpuThreadIds ( self , instance_name ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> output = self . _CallMonitorCommand ( instance_name , self . _CPU_INFO_CMD ) <EOL> for line in output . stdout . splitlines ( ) : <EOL> match = self . _CPU_INFO_RE . search ( line ) <EOL> if not match : <EOL> continue <EOL> grp = map ( int , match . groups ( ) ) <EOL> result [ grp [ <NUM_LIT:0> ] ] = grp [ <NUM_LIT:1> ] <EOL> return result <EOL> def _ExecuteCpuAffinity ( self , instance_name , cpu_mask , worker_cpu_mask ) : <EOL> """<STR_LIT>""" <EOL> _ , pid , _ = self . _InstancePidAlive ( instance_name ) <EOL> thread_dict = self . _GetVcpuThreadIds ( instance_name ) <EOL> self . _AssignCpuAffinity ( cpu_mask , worker_cpu_mask , pid , thread_dict ) <EOL> def ListInstances ( self , hvparams = None ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> for name in os . listdir ( self . _PIDS_DIR ) : <EOL> if self . _InstancePidAlive ( name ) [ <NUM_LIT:2> ] : <EOL> result . append ( name ) <EOL> return result <EOL> @ classmethod <EOL> def _IsUserShutdown ( cls , instance_name ) : <EOL> return os . path . exists ( cls . _InstanceShutdownMonitor ( instance_name ) ) <EOL> @ classmethod <EOL> def _ClearUserShutdown ( cls , instance_name ) : <EOL> utils . RemoveFile ( cls . _InstanceShutdownMonitor ( instance_name ) ) <EOL> def GetInstanceInfo ( self , instance_name , hvparams = None ) : <EOL> """<STR_LIT>""" <EOL> _ , pid , alive = self . _InstancePidAlive ( instance_name ) <EOL> if not alive : <EOL> if self . _IsUserShutdown ( instance_name ) : <EOL> return ( instance_name , - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , hv_base . HvInstanceState . SHUTDOWN , <NUM_LIT:0> ) <EOL> else : <EOL> return None <EOL> _ , memory , vcpus = self . _InstancePidInfo ( pid ) <EOL> istat = hv_base . HvInstanceState . RUNNING <EOL> times = <NUM_LIT:0> <EOL> try : <EOL> qmp = QmpConnection ( self . _InstanceQmpMonitor ( instance_name ) ) <EOL> qmp . connect ( ) <EOL> vcpus = len ( qmp . Execute ( "<STR_LIT>" ) ) <EOL> mem_bytes = qmp . Execute ( "<STR_LIT>" ) [ qmp . ACTUAL_KEY ] <EOL> memory = mem_bytes / <NUM_LIT> <EOL> except errors . HypervisorError : <EOL> pass <EOL> return ( instance_name , pid , memory , vcpus , istat , times ) <EOL> def GetAllInstancesInfo ( self , hvparams = None ) : <EOL> """<STR_LIT>""" <EOL> data = [ ] <EOL> for name in os . listdir ( self . _PIDS_DIR ) : <EOL> try : <EOL> info = self . GetInstanceInfo ( name ) <EOL> except errors . HypervisorError : <EOL> continue <EOL> if info : <EOL> data . append ( info ) <EOL> return data <EOL> def _GenerateKVMBlockDevicesOptions ( self , up_hvp , kvm_disks , <EOL> kvmhelp , devlist ) : <EOL> """<STR_LIT>""" <EOL> kernel_path = up_hvp [ constants . HV_KERNEL_PATH ] <EOL> if kernel_path : <EOL> boot_disk = False <EOL> else : <EOL> boot_disk = up_hvp [ constants . HV_BOOT_ORDER ] == constants . HT_BO_DISK <EOL> needs_boot_flag = self . _BOOT_RE . search ( kvmhelp ) <EOL> dev_opts = [ ] <EOL> disk_type = up_hvp [ constants . HV_DISK_TYPE ] <EOL> if disk_type == constants . HT_DISK_PARAVIRTUAL : <EOL> driver = self . _VIRTIO_BLK_PCI <EOL> iface = self . _VIRTIO <EOL> else : <EOL> driver = iface = disk_type <EOL> if self . _DEVICE_DRIVER_SUPPORTED ( driver , devlist ) : <EOL> if_val = "<STR_LIT>" <EOL> device_driver = driver <EOL> else : <EOL> if_val = "<STR_LIT>" % iface <EOL> device_driver = None <EOL> aio_mode = up_hvp [ constants . HV_KVM_DISK_AIO ] <EOL> if aio_mode == constants . HT_KVM_AIO_NATIVE : <EOL> aio_val = "<STR_LIT>" % aio_mode <EOL> else : <EOL> aio_val = "<STR_LIT>" <EOL> disk_cache = up_hvp [ constants . HV_DISK_CACHE ] <EOL> for cfdev , link_name , uri in kvm_disks : <EOL> if cfdev . dev_type in constants . DTS_EXT_MIRROR : <EOL> if disk_cache != "<STR_LIT:none>" : <EOL> logging . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> disk_cache ) <EOL> cache_val = "<STR_LIT>" <EOL> elif disk_cache != constants . HT_CACHE_DEFAULT : <EOL> cache_val = "<STR_LIT>" % disk_cache <EOL> else : <EOL> cache_val = "<STR_LIT>" <EOL> if cfdev . mode != constants . DISK_RDWR : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> boot_val = "<STR_LIT>" <EOL> if boot_disk : <EOL> dev_opts . extend ( [ "<STR_LIT>" , "<STR_LIT:c>" ] ) <EOL> boot_disk = False <EOL> if needs_boot_flag and disk_type != constants . HT_DISK_IDE : <EOL> boot_val = "<STR_LIT>" <EOL> drive_uri = _GetDriveURI ( cfdev , link_name , uri ) <EOL> drive_val = "<STR_LIT>" % ( drive_uri , if_val , boot_val , cache_val , aio_val ) <EOL> if device_driver is not None : <EOL> kvm_devid = cfdev . hvinfo [ "<STR_LIT:id>" ] <EOL> drive_val += "<STR_LIT>" % kvm_devid <EOL> dev_val = _GenerateDeviceHVInfoStr ( cfdev . hvinfo ) <EOL> dev_val += "<STR_LIT>" % kvm_devid <EOL> dev_opts . extend ( [ "<STR_LIT>" , dev_val ] ) <EOL> dev_opts . extend ( [ "<STR_LIT>" , drive_val ] ) <EOL> return dev_opts <EOL> @ staticmethod <EOL> def _CdromOption ( kvm_cmd , cdrom_disk_type , cdrom_image , cdrom_boot , <EOL> needs_boot_flag ) : <EOL> """<STR_LIT>""" <EOL> if utils . IsUrl ( cdrom_image ) and not _CheckUrl ( cdrom_image ) : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % <EOL> cdrom_image ) <EOL> if utils . IsUrl ( cdrom_image ) : <EOL> options = "<STR_LIT>" <EOL> else : <EOL> options = "<STR_LIT>" <EOL> if cdrom_boot : <EOL> if_val = "<STR_LIT>" + constants . HT_DISK_IDE <EOL> elif cdrom_disk_type == constants . HT_DISK_PARAVIRTUAL : <EOL> if_val = "<STR_LIT>" <EOL> else : <EOL> if_val = "<STR_LIT>" + cdrom_disk_type <EOL> boot_val = "<STR_LIT>" <EOL> if cdrom_boot : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , "<STR_LIT:d>" ] ) <EOL> if needs_boot_flag : <EOL> boot_val = "<STR_LIT>" <EOL> drive_val = "<STR_LIT>" % ( cdrom_image , options , if_val , boot_val ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , drive_val ] ) <EOL> def _GenerateKVMRuntime ( self , instance , block_devices , startup_paused , <EOL> kvmhelp ) : <EOL> """<STR_LIT>""" <EOL> hvp = instance . hvparams <EOL> self . ValidateParameters ( hvp ) <EOL> pidfile = self . _InstancePidFile ( instance . name ) <EOL> kvm = hvp [ constants . HV_KVM_PATH ] <EOL> kvm_cmd = [ kvm ] <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , instance . name ] ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , instance . beparams [ constants . BE_MAXMEM ] ] ) <EOL> smp_list = [ "<STR_LIT:%s>" % instance . beparams [ constants . BE_VCPUS ] ] <EOL> if hvp [ constants . HV_CPU_CORES ] : <EOL> smp_list . append ( "<STR_LIT>" % hvp [ constants . HV_CPU_CORES ] ) <EOL> if hvp [ constants . HV_CPU_THREADS ] : <EOL> smp_list . append ( "<STR_LIT>" % hvp [ constants . HV_CPU_THREADS ] ) <EOL> if hvp [ constants . HV_CPU_SOCKETS ] : <EOL> smp_list . append ( "<STR_LIT>" % hvp [ constants . HV_CPU_SOCKETS ] ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , "<STR_LIT:U+002C>" . join ( smp_list ) ] ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , pidfile ] ) <EOL> bus_slots = self . _GetBusSlots ( hvp ) <EOL> if hvp [ constants . HV_SOUNDHW ] : <EOL> soundhw = hvp [ constants . HV_SOUNDHW ] <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , soundhw ] ) <EOL> if hvp [ constants . HV_DISK_TYPE ] in constants . HT_SCSI_DEVICE_TYPES : <EOL> kvm_cmd . extend ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" % hvp [ constants . HV_KVM_SCSI_CONTROLLER_TYPE ] <EOL> ] ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" ] ) <EOL> if not instance . hvparams [ constants . HV_ACPI ] : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" ] ) <EOL> if instance . hvparams [ constants . HV_REBOOT_BEHAVIOR ] == constants . INSTANCE_REBOOT_EXIT : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" ] ) <EOL> mversion = hvp [ constants . HV_KVM_MACHINE_VERSION ] <EOL> if not mversion : <EOL> mversion = self . _GetDefaultMachineVersion ( kvm ) <EOL> if self . _MACHINE_RE . search ( kvmhelp ) : <EOL> if ( hvp [ constants . HV_KVM_FLAG ] == constants . HT_KVM_ENABLED ) : <EOL> specprop = "<STR_LIT>" <EOL> else : <EOL> specprop = "<STR_LIT>" <EOL> machinespec = "<STR_LIT>" % ( mversion , specprop ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , machinespec ] ) <EOL> else : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , mversion ] ) <EOL> if ( hvp [ constants . HV_KVM_FLAG ] == constants . HT_KVM_ENABLED and <EOL> self . _ENABLE_KVM_RE . search ( kvmhelp ) ) : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" ] ) <EOL> elif ( hvp [ constants . HV_KVM_FLAG ] == constants . HT_KVM_DISABLED and <EOL> self . _DISABLE_KVM_RE . search ( kvmhelp ) ) : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" ] ) <EOL> kernel_path = hvp [ constants . HV_KERNEL_PATH ] <EOL> if kernel_path : <EOL> boot_cdrom = boot_floppy = boot_network = False <EOL> else : <EOL> boot_cdrom = hvp [ constants . HV_BOOT_ORDER ] == constants . HT_BO_CDROM <EOL> boot_floppy = hvp [ constants . HV_BOOT_ORDER ] == constants . HT_BO_FLOPPY <EOL> boot_network = hvp [ constants . HV_BOOT_ORDER ] == constants . HT_BO_NETWORK <EOL> if startup_paused : <EOL> kvm_cmd . extend ( [ _KVM_START_PAUSED_FLAG ] ) <EOL> if boot_network : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , "<STR_LIT:n>" ] ) <EOL> disk_type = hvp [ constants . HV_DISK_TYPE ] <EOL> cdrom_disk_type = hvp [ constants . HV_KVM_CDROM_DISK_TYPE ] <EOL> if not cdrom_disk_type : <EOL> cdrom_disk_type = disk_type <EOL> cdrom_image1 = hvp [ constants . HV_CDROM_IMAGE_PATH ] <EOL> if cdrom_image1 : <EOL> needs_boot_flag = self . _BOOT_RE . search ( kvmhelp ) <EOL> self . _CdromOption ( kvm_cmd , cdrom_disk_type , cdrom_image1 , boot_cdrom , <EOL> needs_boot_flag ) <EOL> cdrom_image2 = hvp [ constants . HV_KVM_CDROM2_IMAGE_PATH ] <EOL> if cdrom_image2 : <EOL> self . _CdromOption ( kvm_cmd , cdrom_disk_type , cdrom_image2 , False , False ) <EOL> floppy_image = hvp [ constants . HV_KVM_FLOPPY_IMAGE_PATH ] <EOL> if floppy_image : <EOL> options = "<STR_LIT>" <EOL> if boot_floppy : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , "<STR_LIT:a>" ] ) <EOL> options = "<STR_LIT>" % options <EOL> if_val = "<STR_LIT>" <EOL> options = "<STR_LIT>" % ( options , if_val ) <EOL> drive_val = "<STR_LIT>" % ( floppy_image , options ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , drive_val ] ) <EOL> if kernel_path : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , kernel_path ] ) <EOL> initrd_path = hvp [ constants . HV_INITRD_PATH ] <EOL> if initrd_path : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , initrd_path ] ) <EOL> root_append = [ "<STR_LIT>" % hvp [ constants . HV_ROOT_PATH ] , <EOL> hvp [ constants . HV_KERNEL_ARGS ] ] <EOL> if hvp [ constants . HV_SERIAL_CONSOLE ] : <EOL> serial_speed = hvp [ constants . HV_SERIAL_SPEED ] <EOL> root_append . append ( "<STR_LIT>" % serial_speed ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , "<STR_LIT:U+0020>" . join ( root_append ) ] ) <EOL> mem_path = hvp [ constants . HV_MEM_PATH ] <EOL> if mem_path : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , mem_path , "<STR_LIT>" ] ) <EOL> monitor_dev = ( "<STR_LIT>" % <EOL> self . _InstanceMonitor ( instance . name ) ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , monitor_dev ] ) <EOL> if hvp [ constants . HV_SERIAL_CONSOLE ] : <EOL> serial_dev = ( "<STR_LIT>" % <EOL> self . _InstanceSerial ( instance . name ) ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , serial_dev ] ) <EOL> else : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , "<STR_LIT:none>" ] ) <EOL> mouse_type = hvp [ constants . HV_USB_MOUSE ] <EOL> vnc_bind_address = hvp [ constants . HV_VNC_BIND_ADDRESS ] <EOL> spice_bind = hvp [ constants . HV_KVM_SPICE_BIND ] <EOL> spice_ip_version = None <EOL> kvm_cmd . extend ( [ "<STR_LIT>" ] ) <EOL> if mouse_type : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , mouse_type ] ) <EOL> elif vnc_bind_address : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , constants . HT_MOUSE_TABLET ] ) <EOL> if vnc_bind_address : <EOL> if netutils . IsValidInterface ( vnc_bind_address ) : <EOL> if_addresses = netutils . GetInterfaceIpAddresses ( vnc_bind_address ) <EOL> if_ip4_addresses = if_addresses [ constants . IP4_VERSION ] <EOL> if len ( if_ip4_addresses ) < <NUM_LIT:1> : <EOL> logging . error ( "<STR_LIT>" , <EOL> vnc_bind_address ) <EOL> else : <EOL> vnc_bind_address = if_ip4_addresses [ <NUM_LIT:0> ] <EOL> if netutils . IP4Address . IsValid ( vnc_bind_address ) : <EOL> if instance . network_port > constants . VNC_BASE_PORT : <EOL> display = instance . network_port - constants . VNC_BASE_PORT <EOL> if vnc_bind_address == constants . IP4_ADDRESS_ANY : <EOL> vnc_arg = "<STR_LIT>" % ( display ) <EOL> else : <EOL> vnc_arg = "<STR_LIT>" % ( vnc_bind_address , display ) <EOL> else : <EOL> logging . error ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> instance . network_port , constants . VNC_BASE_PORT ) <EOL> vnc_arg = "<STR_LIT:none>" <EOL> vnc_append = "<STR_LIT>" <EOL> if hvp [ constants . HV_VNC_TLS ] : <EOL> vnc_append = "<STR_LIT>" % vnc_append <EOL> if hvp [ constants . HV_VNC_X509_VERIFY ] : <EOL> vnc_append = "<STR_LIT>" % ( vnc_append , <EOL> hvp [ constants . HV_VNC_X509 ] ) <EOL> elif hvp [ constants . HV_VNC_X509 ] : <EOL> vnc_append = "<STR_LIT>" % ( vnc_append , <EOL> hvp [ constants . HV_VNC_X509 ] ) <EOL> if hvp [ constants . HV_VNC_PASSWORD_FILE ] : <EOL> vnc_append = "<STR_LIT>" % vnc_append <EOL> vnc_arg = "<STR_LIT>" % ( vnc_arg , vnc_append ) <EOL> else : <EOL> vnc_arg = "<STR_LIT>" % ( vnc_bind_address , instance . name ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , vnc_arg ] ) <EOL> elif spice_bind : <EOL> if netutils . IsValidInterface ( spice_bind ) : <EOL> addresses = netutils . GetInterfaceIpAddresses ( spice_bind ) <EOL> spice_ip_version = hvp [ constants . HV_KVM_SPICE_IP_VERSION ] <EOL> if spice_ip_version != constants . IFACE_NO_IP_VERSION_SPECIFIED : <EOL> if not addresses [ spice_ip_version ] : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( spice_ip_version , <EOL> spice_bind ) ) <EOL> elif ( addresses [ constants . IP4_VERSION ] and <EOL> addresses [ constants . IP6_VERSION ] ) : <EOL> cluster_family = ssconf . SimpleStore ( ) . GetPrimaryIPFamily ( ) <EOL> spice_ip_version = netutils . IPAddress . GetVersionFromAddressFamily ( cluster_family ) <EOL> elif addresses [ constants . IP4_VERSION ] : <EOL> spice_ip_version = constants . IP4_VERSION <EOL> elif addresses [ constants . IP6_VERSION ] : <EOL> spice_ip_version = constants . IP6_VERSION <EOL> else : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( spice_bind ) ) <EOL> spice_address = addresses [ spice_ip_version ] [ <NUM_LIT:0> ] <EOL> else : <EOL> spice_address = spice_bind <EOL> spice_arg = "<STR_LIT>" % spice_address <EOL> if hvp [ constants . HV_KVM_SPICE_USE_TLS ] : <EOL> spice_arg = ( "<STR_LIT>" % <EOL> ( spice_arg , instance . network_port , <EOL> pathutils . SPICE_CACERT_FILE ) ) <EOL> spice_arg = ( "<STR_LIT>" % <EOL> ( spice_arg , pathutils . SPICE_CERT_FILE , <EOL> pathutils . SPICE_CERT_FILE ) ) <EOL> tls_ciphers = hvp [ constants . HV_KVM_SPICE_TLS_CIPHERS ] <EOL> if tls_ciphers : <EOL> spice_arg = "<STR_LIT>" % ( spice_arg , tls_ciphers ) <EOL> else : <EOL> spice_arg = "<STR_LIT>" % ( spice_arg , instance . network_port ) <EOL> if not hvp [ constants . HV_KVM_SPICE_PASSWORD_FILE ] : <EOL> spice_arg = "<STR_LIT>" % spice_arg <EOL> if spice_ip_version : <EOL> spice_arg = "<STR_LIT>" % ( spice_arg , spice_ip_version ) <EOL> img_lossless = hvp [ constants . HV_KVM_SPICE_LOSSLESS_IMG_COMPR ] <EOL> img_jpeg = hvp [ constants . HV_KVM_SPICE_JPEG_IMG_COMPR ] <EOL> img_zlib_glz = hvp [ constants . HV_KVM_SPICE_ZLIB_GLZ_IMG_COMPR ] <EOL> if img_lossless : <EOL> spice_arg = "<STR_LIT>" % ( spice_arg , img_lossless ) <EOL> if img_jpeg : <EOL> spice_arg = "<STR_LIT>" % ( spice_arg , img_jpeg ) <EOL> if img_zlib_glz : <EOL> spice_arg = "<STR_LIT>" % ( spice_arg , img_zlib_glz ) <EOL> video_streaming = hvp [ constants . HV_KVM_SPICE_STREAMING_VIDEO_DETECTION ] <EOL> if video_streaming : <EOL> spice_arg = "<STR_LIT>" % ( spice_arg , video_streaming ) <EOL> if not hvp [ constants . HV_KVM_SPICE_AUDIO_COMPR ] : <EOL> spice_arg = "<STR_LIT>" % spice_arg <EOL> if not hvp [ constants . HV_KVM_SPICE_USE_VDAGENT ] : <EOL> spice_arg = "<STR_LIT>" % spice_arg <EOL> else : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> kvm_cmd . extend ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> logging . info ( "<STR_LIT>" , instance . network_port ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , spice_arg ] ) <EOL> else : <EOL> if self . _DISPLAY_RE . search ( kvmhelp ) : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , "<STR_LIT:none>" ] ) <EOL> else : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" ] ) <EOL> if hvp [ constants . HV_USE_LOCALTIME ] : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" ] ) <EOL> if hvp [ constants . HV_KVM_USE_CHROOT ] : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , self . _InstanceChrootDir ( instance . name ) ] ) <EOL> if hvp [ constants . HV_CPU_TYPE ] : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , hvp [ constants . HV_CPU_TYPE ] ] ) <EOL> if hvp [ constants . HV_VGA ] : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , hvp [ constants . HV_VGA ] ] ) <EOL> elif spice_bind : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> if hvp [ constants . HV_USB_DEVICES ] : <EOL> for dev in hvp [ constants . HV_USB_DEVICES ] . split ( "<STR_LIT:U+002C>" ) : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , dev ] ) <EOL> if self . _UUID_RE . search ( kvmhelp ) : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , instance . uuid ] ) <EOL> if hvp [ constants . HV_KVM_EXTRA ] : <EOL> kvm_cmd . extend ( hvp [ constants . HV_KVM_EXTRA ] . split ( "<STR_LIT:U+0020>" ) ) <EOL> def _generate_kvm_device ( dev_type , dev ) : <EOL> """<STR_LIT>""" <EOL> kvm_devid = _GenerateDeviceKVMId ( dev_type , dev ) <EOL> hv_dev_type = _DEVICE_TYPE [ dev_type ] ( hvp ) <EOL> dev . hvinfo = _GenerateDeviceHVInfo ( dev_type , kvm_devid , <EOL> hv_dev_type , bus_slots ) <EOL> kvm_disks = [ ] <EOL> for disk , link_name , uri in block_devices : <EOL> _generate_kvm_device ( constants . HOTPLUG_TARGET_DISK , disk ) <EOL> kvm_disks . append ( ( disk , link_name , uri ) ) <EOL> kvm_nics = [ ] <EOL> for nic in instance . nics : <EOL> _generate_kvm_device ( constants . HOTPLUG_TARGET_NIC , nic ) <EOL> kvm_nics . append ( nic ) <EOL> hvparams = hvp <EOL> return ( kvm_cmd , kvm_nics , hvparams , kvm_disks ) <EOL> def _WriteKVMRuntime ( self , instance_name , data ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> utils . WriteFile ( self . _InstanceKVMRuntime ( instance_name ) , <EOL> data = data ) <EOL> except EnvironmentError , err : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % err ) <EOL> def _ReadKVMRuntime ( self , instance_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> file_content = utils . ReadFile ( self . _InstanceKVMRuntime ( instance_name ) ) <EOL> except EnvironmentError , err : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % err ) <EOL> return file_content <EOL> def _SaveKVMRuntime ( self , instance , kvm_runtime ) : <EOL> """<STR_LIT>""" <EOL> kvm_cmd , kvm_nics , hvparams , kvm_disks = kvm_runtime <EOL> serialized_nics = [ nic . ToDict ( ) for nic in kvm_nics ] <EOL> serialized_disks = [ ( blk . ToDict ( ) , link , uri ) <EOL> for blk , link , uri in kvm_disks ] <EOL> serialized_form = serializer . Dump ( ( kvm_cmd , serialized_nics , hvparams , <EOL> serialized_disks ) ) <EOL> self . _WriteKVMRuntime ( instance . name , serialized_form ) <EOL> def _LoadKVMRuntime ( self , instance , serialized_runtime = None ) : <EOL> """<STR_LIT>""" <EOL> if not serialized_runtime : <EOL> serialized_runtime = self . _ReadKVMRuntime ( instance . name ) <EOL> return _AnalyzeSerializedRuntime ( serialized_runtime ) <EOL> def _RunKVMCmd ( self , name , kvm_cmd , tap_fds = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> result = utils . RunCmd ( kvm_cmd , noclose_fds = tap_fds ) <EOL> finally : <EOL> for fd in tap_fds : <EOL> utils_wrapper . CloseFdNoError ( fd ) <EOL> if result . failed : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % <EOL> ( name , result . fail_reason , result . output ) ) <EOL> if not self . _InstancePidAlive ( name ) [ <NUM_LIT:2> ] : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % name ) <EOL> @ staticmethod <EOL> def _GenerateKvmTapName ( nic ) : <EOL> """<STR_LIT>""" <EOL> if nic . name is None or not nic . name . startswith ( constants . INSTANCE_COMMUNICATION_NIC_PREFIX ) : <EOL> return "<STR_LIT>" <EOL> return hv_base . GenerateTapName ( ) <EOL> def _GetNetworkDeviceFeatures ( self , up_hvp , devlist , kvmhelp ) : <EOL> """<STR_LIT>""" <EOL> nic_type = up_hvp [ constants . HV_NIC_TYPE ] <EOL> nic_extra_str = "<STR_LIT>" <EOL> tap_extra_str = "<STR_LIT>" <EOL> features = { <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : ( False , <NUM_LIT:1> ) <EOL> } <EOL> update_features = { } <EOL> if nic_type == constants . HT_NIC_PARAVIRTUAL : <EOL> if self . _DEVICE_DRIVER_SUPPORTED ( self . _VIRTIO_NET_PCI , devlist ) : <EOL> nic_model = self . _VIRTIO_NET_PCI <EOL> update_features [ "<STR_LIT>" ] = up_hvp [ constants . HV_VNET_HDR ] <EOL> else : <EOL> nic_model = self . _VIRTIO <EOL> if up_hvp [ constants . HV_VHOST_NET ] : <EOL> if self . _VHOST_RE . search ( kvmhelp ) : <EOL> update_features [ "<STR_LIT>" ] = True <EOL> tap_extra_str = "<STR_LIT>" <EOL> else : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> virtio_net_queues = up_hvp . get ( constants . HV_VIRTIO_NET_QUEUES , <NUM_LIT:1> ) <EOL> if virtio_net_queues > <NUM_LIT:1> : <EOL> if self . _VIRTIO_NET_QUEUES_RE . search ( kvmhelp ) : <EOL> nic_extra_str = "<STR_LIT>" % ( <NUM_LIT:2> * virtio_net_queues + <NUM_LIT:1> ) <EOL> update_features [ "<STR_LIT>" ] = ( True , virtio_net_queues ) <EOL> else : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> nic_model = nic_type <EOL> update_features [ "<STR_LIT>" ] = nic_model <EOL> features . update ( update_features ) <EOL> return features , tap_extra_str , nic_extra_str <EOL> def _ExecuteKVMRuntime ( self , instance , kvm_runtime , kvmhelp , incoming = None ) : <EOL> """<STR_LIT>""" <EOL> conf_hvp = instance . hvparams <EOL> name = instance . name <EOL> self . _CheckDown ( name ) <EOL> self . _ClearUserShutdown ( instance . name ) <EOL> self . _StartKvmd ( instance . hvparams ) <EOL> temp_files = [ ] <EOL> kvm_cmd , kvm_nics , up_hvp , kvm_disks = kvm_runtime <EOL> kvm_path = kvm_cmd [ <NUM_LIT:0> ] <EOL> up_hvp = objects . FillDict ( conf_hvp , up_hvp ) <EOL> security_model = conf_hvp [ constants . HV_SECURITY_MODEL ] <EOL> if security_model == constants . HT_SM_USER : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , conf_hvp [ constants . HV_SECURITY_DOMAIN ] ] ) <EOL> keymap = conf_hvp [ constants . HV_KEYMAP ] <EOL> if keymap : <EOL> keymap_path = self . _InstanceKeymapFile ( name ) <EOL> utils . WriteFile ( keymap_path , data = "<STR_LIT>" % keymap ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , keymap_path ] ) <EOL> tapfds = [ ] <EOL> taps = [ ] <EOL> devlist = self . _GetKVMOutput ( kvm_path , self . _KVMOPT_DEVICELIST ) <EOL> if not kvm_nics : <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , "<STR_LIT:none>" ] ) <EOL> else : <EOL> features , tap_extra , nic_extra = self . _GetNetworkDeviceFeatures ( up_hvp , devlist , kvmhelp ) <EOL> nic_model = features [ "<STR_LIT>" ] <EOL> kvm_supports_netdev = self . _NETDEV_RE . search ( kvmhelp ) <EOL> for nic_seq , nic in enumerate ( kvm_nics ) : <EOL> tapname , nic_tapfds , nic_vhostfds = OpenTap ( features = features , name = self . _GenerateKvmTapName ( nic ) ) <EOL> tapfds . extend ( nic_tapfds ) <EOL> tapfds . extend ( nic_vhostfds ) <EOL> taps . append ( tapname ) <EOL> tapfd = "<STR_LIT>" % ( "<STR_LIT>" if len ( nic_tapfds ) > <NUM_LIT:1> else "<STR_LIT>" , <EOL> "<STR_LIT::>" . join ( str ( fd ) for fd in nic_tapfds ) ) <EOL> if nic_vhostfds : <EOL> vhostfd = "<STR_LIT>" % ( "<STR_LIT>" <EOL> if len ( nic_vhostfds ) > <NUM_LIT:1> else "<STR_LIT>" , <EOL> "<STR_LIT::>" . join ( str ( fd ) for fd in nic_vhostfds ) ) <EOL> else : <EOL> vhostfd = "<STR_LIT>" <EOL> if kvm_supports_netdev : <EOL> if "<STR_LIT:id>" in nic . hvinfo : <EOL> nic_val = _GenerateDeviceHVInfoStr ( nic . hvinfo ) <EOL> netdev = nic . hvinfo [ "<STR_LIT:id>" ] <EOL> else : <EOL> nic_val = "<STR_LIT:%s>" % nic_model <EOL> netdev = "<STR_LIT>" % nic_seq <EOL> nic_val += ( "<STR_LIT>" % ( netdev , nic . mac , nic_extra ) ) <EOL> tap_val = ( "<STR_LIT>" % <EOL> ( netdev , tapfd , vhostfd , tap_extra ) ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , tap_val , "<STR_LIT>" , nic_val ] ) <EOL> else : <EOL> nic_val = "<STR_LIT>" % ( nic_seq , <EOL> nic . mac , nic_model ) <EOL> tap_val = "<STR_LIT>" % ( nic_seq , tapfd ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , tap_val , "<STR_LIT>" , nic_val ] ) <EOL> if incoming : <EOL> target , port = incoming <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , "<STR_LIT>" % ( target , port ) ] ) <EOL> vnc_pwd_file = conf_hvp [ constants . HV_VNC_PASSWORD_FILE ] <EOL> vnc_pwd = None <EOL> if vnc_pwd_file : <EOL> try : <EOL> vnc_pwd = utils . ReadOneLineFile ( vnc_pwd_file , strict = True ) <EOL> except EnvironmentError , err : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> % ( vnc_pwd_file , err ) ) <EOL> if conf_hvp [ constants . HV_KVM_USE_CHROOT ] : <EOL> utils . EnsureDirs ( [ ( self . _InstanceChrootDir ( name ) , <EOL> constants . SECURE_DIR_MODE ) ] ) <EOL> if self . _QMP_RE . search ( kvmhelp ) : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , "<STR_LIT>" % <EOL> self . _InstanceQmpMonitor ( instance . name ) ] ) <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , "<STR_LIT>" % <EOL> self . _InstanceKvmdMonitor ( instance . name ) ] ) <EOL> for nic_seq , nic in enumerate ( kvm_nics ) : <EOL> if ( incoming and <EOL> nic . nicparams [ constants . NIC_MODE ] == constants . NIC_MODE_ROUTED ) : <EOL> continue <EOL> self . _ConfigureNIC ( instance , nic_seq , nic , taps [ nic_seq ] ) <EOL> bdev_opts = self . _GenerateKVMBlockDevicesOptions ( up_hvp , <EOL> kvm_disks , <EOL> kvmhelp , <EOL> devlist ) <EOL> kvm_cmd . extend ( bdev_opts ) <EOL> start_kvm_paused = not ( _KVM_START_PAUSED_FLAG in kvm_cmd ) and not incoming <EOL> if start_kvm_paused : <EOL> kvm_cmd . extend ( [ _KVM_START_PAUSED_FLAG ] ) <EOL> cpu_pinning = False <EOL> if up_hvp . get ( constants . HV_CPU_MASK , None ) : <EOL> cpu_pinning = True <EOL> if security_model == constants . HT_SM_POOL : <EOL> ss = ssconf . SimpleStore ( ) <EOL> uid_pool = uidpool . ParseUidPool ( ss . GetUidPool ( ) , separator = "<STR_LIT:\n>" ) <EOL> all_uids = set ( uidpool . ExpandUidPool ( uid_pool ) ) <EOL> uid = uidpool . RequestUnusedUid ( all_uids ) <EOL> try : <EOL> username = pwd . getpwuid ( uid . GetUid ( ) ) . pw_name <EOL> kvm_cmd . extend ( [ "<STR_LIT>" , username ] ) <EOL> self . _RunKVMCmd ( name , kvm_cmd , tapfds ) <EOL> except : <EOL> uidpool . ReleaseUid ( uid ) <EOL> raise <EOL> else : <EOL> uid . Unlock ( ) <EOL> utils . WriteFile ( self . _InstanceUidFile ( name ) , data = uid . AsStr ( ) ) <EOL> else : <EOL> self . _RunKVMCmd ( name , kvm_cmd , tapfds ) <EOL> utils . EnsureDirs ( [ ( self . _InstanceNICDir ( instance . name ) , <EOL> constants . RUN_DIRS_MODE ) ] ) <EOL> for nic_seq , tap in enumerate ( taps ) : <EOL> utils . WriteFile ( self . _InstanceNICFile ( instance . name , nic_seq ) , <EOL> data = tap ) <EOL> if vnc_pwd : <EOL> change_cmd = "<STR_LIT>" % vnc_pwd <EOL> self . _CallMonitorCommand ( instance . name , change_cmd ) <EOL> spice_password_file = conf_hvp [ constants . HV_KVM_SPICE_PASSWORD_FILE ] <EOL> if spice_password_file : <EOL> spice_pwd = "<STR_LIT>" <EOL> try : <EOL> spice_pwd = utils . ReadOneLineFile ( spice_password_file , strict = True ) <EOL> except EnvironmentError , err : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> % ( spice_password_file , err ) ) <EOL> qmp = QmpConnection ( self . _InstanceQmpMonitor ( instance . name ) ) <EOL> qmp . connect ( ) <EOL> arguments = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:password>" : spice_pwd , <EOL> } <EOL> qmp . Execute ( "<STR_LIT>" , arguments ) <EOL> for filename in temp_files : <EOL> utils . RemoveFile ( filename ) <EOL> if cpu_pinning : <EOL> self . _ExecuteCpuAffinity ( instance . name , up_hvp [ constants . HV_CPU_MASK ] , <EOL> up_hvp [ constants . HV_WORKER_CPU_MASK ] ) <EOL> start_memory = self . _InstanceStartupMemory ( instance ) <EOL> if start_memory < instance . beparams [ constants . BE_MAXMEM ] : <EOL> self . BalloonInstanceMemory ( instance , start_memory ) <EOL> if start_kvm_paused : <EOL> self . _CallMonitorCommand ( instance . name , self . _CONT_CMD ) <EOL> @ staticmethod <EOL> def _StartKvmd ( hvparams ) : <EOL> """<STR_LIT>""" <EOL> if hvparams is None or not hvparams [ constants . HV_KVM_USER_SHUTDOWN ] or utils . IsDaemonAlive ( constants . KVMD ) : <EOL> return <EOL> result = utils . RunCmd ( [ pathutils . DAEMON_UTIL , "<STR_LIT:start>" , constants . KVMD ] ) <EOL> if result . failed : <EOL> raise errors . HypervisorError ( "<STR_LIT>" ) <EOL> def StartInstance ( self , instance , block_devices , startup_paused ) : <EOL> """<STR_LIT>""" <EOL> self . _CheckDown ( instance . name ) <EOL> kvmpath = instance . hvparams [ constants . HV_KVM_PATH ] <EOL> kvmhelp = self . _GetKVMOutput ( kvmpath , self . _KVMOPT_HELP ) <EOL> kvm_runtime = self . _GenerateKVMRuntime ( instance , block_devices , <EOL> startup_paused , kvmhelp ) <EOL> self . _SaveKVMRuntime ( instance , kvm_runtime ) <EOL> self . _ExecuteKVMRuntime ( instance , kvm_runtime , kvmhelp ) <EOL> @ classmethod <EOL> def _CallMonitorCommand ( cls , instance_name , command , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> if timeout is not None : <EOL> timeout_cmd = "<STR_LIT>" % ( timeout , ) <EOL> else : <EOL> timeout_cmd = "<STR_LIT>" <EOL> socat = ( "<STR_LIT>" % <EOL> ( utils . ShellQuote ( command ) , <EOL> timeout_cmd , <EOL> constants . SOCAT_PATH , <EOL> utils . ShellQuote ( cls . _InstanceMonitor ( instance_name ) ) ) ) <EOL> result = utils . RunCmd ( socat ) <EOL> if result . failed : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( command , instance_name , result . fail_reason , result . output ) ) <EOL> raise errors . HypervisorError ( msg ) <EOL> return result <EOL> @ _with_qmp <EOL> def VerifyHotplugSupport ( self , instance , action , dev_type ) : <EOL> """<STR_LIT>""" <EOL> runtime = self . _LoadKVMRuntime ( instance ) <EOL> device_type = _DEVICE_TYPE [ dev_type ] ( runtime [ <NUM_LIT:2> ] ) <EOL> if device_type not in _HOTPLUGGABLE_DEVICE_TYPES [ dev_type ] : <EOL> msg = "<STR_LIT>" % device_type <EOL> raise errors . HypervisorError ( msg ) <EOL> if dev_type == constants . HOTPLUG_TARGET_DISK : <EOL> if action == constants . HOTPLUG_ACTION_ADD : <EOL> self . qmp . CheckDiskHotAddSupport ( ) <EOL> if dev_type == constants . HOTPLUG_TARGET_NIC : <EOL> if action == constants . HOTPLUG_ACTION_ADD : <EOL> self . qmp . CheckNicHotAddSupport ( ) <EOL> def HotplugSupported ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> output = self . _CallMonitorCommand ( instance . name , self . _INFO_VERSION_CMD ) <EOL> except errors . HypervisorError : <EOL> raise errors . HotplugError ( "<STR_LIT>" ) <EOL> match = self . _INFO_VERSION_RE . search ( output . stdout ) <EOL> if not match : <EOL> raise errors . HotplugError ( "<STR_LIT>" ) <EOL> v_major , v_min , _ , _ = match . groups ( ) <EOL> if ( int ( v_major ) , int ( v_min ) ) < ( <NUM_LIT:1> , <NUM_LIT:7> ) : <EOL> raise errors . HotplugError ( "<STR_LIT>" ) <EOL> def _GetBusSlots ( self , hvp = None , runtime = None ) : <EOL> """<STR_LIT>""" <EOL> bus_slots = { <EOL> _PCI_BUS : bitarray ( self . _DEFAULT_PCI_RESERVATIONS ) , <EOL> _SCSI_BUS : bitarray ( self . _DEFAULT_SCSI_RESERVATIONS ) , <EOL> } <EOL> if hvp and constants . HV_KVM_PCI_RESERVATIONS in hvp : <EOL> res = hvp [ constants . HV_KVM_PCI_RESERVATIONS ] <EOL> pci = bitarray ( constants . QEMU_PCI_SLOTS ) <EOL> pci . setall ( False ) <EOL> pci [ <NUM_LIT:0> : res : <NUM_LIT:1> ] = True <EOL> bus_slots [ _PCI_BUS ] = pci <EOL> if runtime : <EOL> _ , nics , _ , disks = runtime <EOL> disks = [ d for d , _ , _ in disks ] <EOL> for d in disks + nics : <EOL> if not d . hvinfo or "<STR_LIT>" not in d . hvinfo : <EOL> continue <EOL> bus = d . hvinfo [ "<STR_LIT>" ] <EOL> slots = bus_slots [ bus ] <EOL> if bus == _PCI_BUS : <EOL> slot = d . hvinfo [ "<STR_LIT>" ] <EOL> slots [ int ( slot , <NUM_LIT:16> ) ] = True <EOL> elif bus == _SCSI_BUS : <EOL> slot = d . hvinfo [ "<STR_LIT>" ] <EOL> slots [ slot ] = True <EOL> return bus_slots <EOL> @ _with_qmp <EOL> def _VerifyHotplugCommand ( self , _instance , kvm_devid , should_exist ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> found = self . qmp . HasDevice ( kvm_devid ) <EOL> logging . info ( "<STR_LIT>" , i , found ) <EOL> if found and should_exist : <EOL> break <EOL> if not found and not should_exist : <EOL> break <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> if found and not should_exist : <EOL> msg = "<STR_LIT>" % kvm_devid <EOL> raise errors . HypervisorError ( msg ) <EOL> if not found and should_exist : <EOL> msg = "<STR_LIT>" % kvm_devid <EOL> raise errors . HypervisorError ( msg ) <EOL> logging . info ( "<STR_LIT>" , kvm_devid ) <EOL> @ _with_qmp <EOL> def HotAddDevice ( self , instance , dev_type , device , extra , seq ) : <EOL> """<STR_LIT>""" <EOL> kvm_devid = _GenerateDeviceKVMId ( dev_type , device ) <EOL> runtime = self . _LoadKVMRuntime ( instance ) <EOL> up_hvp = runtime [ <NUM_LIT:2> ] <EOL> device_type = _DEVICE_TYPE [ dev_type ] ( up_hvp ) <EOL> bus_state = self . _GetBusSlots ( up_hvp , runtime ) <EOL> if not device . hvinfo : <EOL> device . hvinfo = _GenerateDeviceHVInfo ( dev_type , kvm_devid , <EOL> device_type , bus_state ) <EOL> if dev_type == constants . HOTPLUG_TARGET_DISK : <EOL> uri = _GetDriveURI ( device , extra [ <NUM_LIT:0> ] , extra [ <NUM_LIT:1> ] ) <EOL> def drive_add_fn ( filename ) : <EOL> """<STR_LIT>""" <EOL> cmd = "<STR_LIT>" % ( filename , kvm_devid ) <EOL> self . _CallMonitorCommand ( instance . name , cmd ) <EOL> self . qmp . HotAddDisk ( device , kvm_devid , uri , drive_add_fn ) <EOL> elif dev_type == constants . HOTPLUG_TARGET_NIC : <EOL> kvmpath = instance . hvparams [ constants . HV_KVM_PATH ] <EOL> kvmhelp = self . _GetKVMOutput ( kvmpath , self . _KVMOPT_HELP ) <EOL> devlist = self . _GetKVMOutput ( kvmpath , self . _KVMOPT_DEVICELIST ) <EOL> features , _ , _ = self . _GetNetworkDeviceFeatures ( up_hvp , devlist , kvmhelp ) <EOL> ( tap , tapfds , vhostfds ) = OpenTap ( features = features ) <EOL> self . _ConfigureNIC ( instance , seq , device , tap ) <EOL> self . qmp . HotAddNic ( device , kvm_devid , tapfds , vhostfds , features ) <EOL> utils . WriteFile ( self . _InstanceNICFile ( instance . name , seq ) , data = tap ) <EOL> self . _VerifyHotplugCommand ( instance , kvm_devid , True ) <EOL> index = _DEVICE_RUNTIME_INDEX [ dev_type ] <EOL> entry = _RUNTIME_ENTRY [ dev_type ] ( device , extra ) <EOL> runtime [ index ] . append ( entry ) <EOL> self . _SaveKVMRuntime ( instance , runtime ) <EOL> @ _with_qmp <EOL> def HotDelDevice ( self , instance , dev_type , device , _ , seq ) : <EOL> """<STR_LIT>""" <EOL> runtime = self . _LoadKVMRuntime ( instance ) <EOL> entry = _GetExistingDeviceInfo ( dev_type , device , runtime ) <EOL> kvm_device = _RUNTIME_DEVICE [ dev_type ] ( entry ) <EOL> kvm_devid = _GenerateDeviceKVMId ( dev_type , kvm_device ) <EOL> if dev_type == constants . HOTPLUG_TARGET_DISK : <EOL> self . qmp . HotDelDisk ( kvm_devid ) <EOL> command = "<STR_LIT>" % kvm_devid <EOL> self . _CallMonitorCommand ( instance . name , command ) <EOL> elif dev_type == constants . HOTPLUG_TARGET_NIC : <EOL> self . qmp . HotDelNic ( kvm_devid ) <EOL> utils . RemoveFile ( self . _InstanceNICFile ( instance . name , seq ) ) <EOL> self . _VerifyHotplugCommand ( instance , kvm_devid , False ) <EOL> index = _DEVICE_RUNTIME_INDEX [ dev_type ] <EOL> runtime [ index ] . remove ( entry ) <EOL> self . _SaveKVMRuntime ( instance , runtime ) <EOL> return kvm_device . hvinfo <EOL> def HotModDevice ( self , instance , dev_type , device , _ , seq ) : <EOL> """<STR_LIT>""" <EOL> if dev_type == constants . HOTPLUG_TARGET_NIC : <EOL> device . hvinfo = self . HotDelDevice ( instance , dev_type , device , _ , seq ) <EOL> self . HotAddDevice ( instance , dev_type , device , _ , seq ) <EOL> @ classmethod <EOL> def _ParseKVMVersion ( cls , text ) : <EOL> """<STR_LIT>""" <EOL> match = cls . _VERSION_RE . search ( text . splitlines ( ) [ <NUM_LIT:0> ] ) <EOL> if not match : <EOL> raise errors . HypervisorError ( "<STR_LIT>" ) <EOL> v_all = match . group ( <NUM_LIT:0> ) <EOL> v_maj = int ( match . group ( <NUM_LIT:1> ) ) <EOL> v_min = int ( match . group ( <NUM_LIT:2> ) ) <EOL> if match . group ( <NUM_LIT:4> ) : <EOL> v_rev = int ( match . group ( <NUM_LIT:4> ) ) <EOL> else : <EOL> v_rev = <NUM_LIT:0> <EOL> return ( v_all , v_maj , v_min , v_rev ) <EOL> @ classmethod <EOL> def _GetKVMOutput ( cls , kvm_path , option ) : <EOL> """<STR_LIT>""" <EOL> assert option in cls . _KVMOPTS_CMDS , "<STR_LIT>" <EOL> optlist , can_fail = cls . _KVMOPTS_CMDS [ option ] <EOL> result = utils . RunCmd ( [ kvm_path ] + optlist ) <EOL> if result . failed and not can_fail : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % <EOL> "<STR_LIT:U+0020>" . join ( optlist ) ) <EOL> return result . output <EOL> @ classmethod <EOL> def _GetKVMVersion ( cls , kvm_path ) : <EOL> """<STR_LIT>""" <EOL> return cls . _ParseKVMVersion ( cls . _GetKVMOutput ( kvm_path , cls . _KVMOPT_HELP ) ) <EOL> @ classmethod <EOL> def _GetDefaultMachineVersion ( cls , kvm_path ) : <EOL> """<STR_LIT>""" <EOL> output = cls . _GetKVMOutput ( kvm_path , cls . _KVMOPT_MLIST ) <EOL> match = cls . _DEFAULT_MACHINE_VERSION_RE . search ( output ) <EOL> if match : <EOL> return match . group ( <NUM_LIT:1> ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> @ classmethod <EOL> def _StopInstance ( cls , instance , force = False , name = None , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> assert ( timeout is None or force is not None ) <EOL> if name is not None and not force : <EOL> raise errors . HypervisorError ( "<STR_LIT>" ) <EOL> if name is None : <EOL> name = instance . name <EOL> acpi = instance . hvparams [ constants . HV_ACPI ] <EOL> else : <EOL> acpi = False <EOL> _ , pid , alive = cls . _InstancePidAlive ( name ) <EOL> if pid > <NUM_LIT:0> and alive : <EOL> if force or not acpi : <EOL> utils . KillProcess ( pid ) <EOL> else : <EOL> cls . _CallMonitorCommand ( name , "<STR_LIT>" , timeout ) <EOL> cls . _ClearUserShutdown ( instance . name ) <EOL> def StopInstance ( self , instance , force = False , retry = False , name = None , <EOL> timeout = None ) : <EOL> """<STR_LIT>""" <EOL> self . _StopInstance ( instance , force , name = name , timeout = timeout ) <EOL> def CleanupInstance ( self , instance_name ) : <EOL> """<STR_LIT>""" <EOL> pidfile , pid , alive = self . _InstancePidAlive ( instance_name ) <EOL> if pid > <NUM_LIT:0> and alive : <EOL> raise errors . HypervisorError ( "<STR_LIT>" ) <EOL> self . _RemoveInstanceRuntimeFiles ( pidfile , instance_name ) <EOL> self . _ClearUserShutdown ( instance_name ) <EOL> def RebootInstance ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> _ , _ , alive = self . _InstancePidAlive ( instance . name ) <EOL> if not alive : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" % instance . name ) <EOL> kvm_runtime = self . _LoadKVMRuntime ( instance ) <EOL> if not self . StopInstance ( instance ) : <EOL> self . StopInstance ( instance , force = True ) <EOL> self . _SaveKVMRuntime ( instance , kvm_runtime ) <EOL> kvmpath = instance . hvparams [ constants . HV_KVM_PATH ] <EOL> kvmhelp = self . _GetKVMOutput ( kvmpath , self . _KVMOPT_HELP ) <EOL> self . _ExecuteKVMRuntime ( instance , kvm_runtime , kvmhelp ) <EOL> def MigrationInfo ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> return self . _ReadKVMRuntime ( instance . name ) <EOL> def AcceptInstance ( self , instance , info , target ) : <EOL> """<STR_LIT>""" <EOL> kvm_runtime = self . _LoadKVMRuntime ( instance , serialized_runtime = info ) <EOL> incoming_address = ( target , instance . hvparams [ constants . HV_MIGRATION_PORT ] ) <EOL> kvmpath = instance . hvparams [ constants . HV_KVM_PATH ] <EOL> kvmhelp = self . _GetKVMOutput ( kvmpath , self . _KVMOPT_HELP ) <EOL> self . _ExecuteKVMRuntime ( instance , kvm_runtime , kvmhelp , <EOL> incoming = incoming_address ) <EOL> def FinalizeMigrationDst ( self , instance , info , success ) : <EOL> """<STR_LIT>""" <EOL> if success : <EOL> kvm_runtime = self . _LoadKVMRuntime ( instance , serialized_runtime = info ) <EOL> kvm_nics = kvm_runtime [ <NUM_LIT:1> ] <EOL> for nic_seq , nic in enumerate ( kvm_nics ) : <EOL> if nic . nicparams [ constants . NIC_MODE ] != constants . NIC_MODE_ROUTED : <EOL> continue <EOL> try : <EOL> tap = utils . ReadFile ( self . _InstanceNICFile ( instance . name , nic_seq ) ) <EOL> except EnvironmentError , err : <EOL> logging . warning ( "<STR_LIT>" , <EOL> instance . name , nic_seq , str ( err ) ) <EOL> continue <EOL> try : <EOL> self . _ConfigureNIC ( instance , nic_seq , nic , tap ) <EOL> except errors . HypervisorError , err : <EOL> logging . warning ( str ( err ) ) <EOL> self . _WriteKVMRuntime ( instance . name , info ) <EOL> else : <EOL> self . StopInstance ( instance , force = True ) <EOL> def MigrateInstance ( self , cluster_name , instance , target , live ) : <EOL> """<STR_LIT>""" <EOL> instance_name = instance . name <EOL> port = instance . hvparams [ constants . HV_MIGRATION_PORT ] <EOL> _ , _ , alive = self . _InstancePidAlive ( instance_name ) <EOL> if not alive : <EOL> raise errors . HypervisorError ( "<STR_LIT>" ) <EOL> if not live : <EOL> self . _CallMonitorCommand ( instance_name , "<STR_LIT>" ) <EOL> migrate_command = ( "<STR_LIT>" % <EOL> instance . hvparams [ constants . HV_MIGRATION_BANDWIDTH ] ) <EOL> self . _CallMonitorCommand ( instance_name , migrate_command ) <EOL> migrate_command = ( "<STR_LIT>" % <EOL> instance . hvparams [ constants . HV_MIGRATION_DOWNTIME ] ) <EOL> self . _CallMonitorCommand ( instance_name , migrate_command ) <EOL> migration_caps = instance . hvparams [ constants . HV_KVM_MIGRATION_CAPS ] <EOL> if migration_caps : <EOL> for c in migration_caps . split ( _MIGRATION_CAPS_DELIM ) : <EOL> migrate_command = ( "<STR_LIT>" % c ) <EOL> self . _CallMonitorCommand ( instance_name , migrate_command ) <EOL> migrate_command = "<STR_LIT>" % ( target , port ) <EOL> self . _CallMonitorCommand ( instance_name , migrate_command ) <EOL> def FinalizeMigrationSource ( self , instance , success , live ) : <EOL> """<STR_LIT>""" <EOL> if success : <EOL> pidfile , pid , _ = self . _InstancePidAlive ( instance . name ) <EOL> utils . KillProcess ( pid ) <EOL> self . _RemoveInstanceRuntimeFiles ( pidfile , instance . name ) <EOL> elif live : <EOL> self . _CallMonitorCommand ( instance . name , self . _CONT_CMD ) <EOL> self . _ClearUserShutdown ( instance . name ) <EOL> def GetMigrationStatus ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> info_command = "<STR_LIT>" <EOL> for _ in range ( self . _MIGRATION_INFO_MAX_BAD_ANSWERS ) : <EOL> result = self . _CallMonitorCommand ( instance . name , info_command ) <EOL> match = self . _MIGRATION_STATUS_RE . search ( result . stdout ) <EOL> if not match : <EOL> if not result . stdout : <EOL> logging . info ( "<STR_LIT>" ) <EOL> else : <EOL> logging . warning ( "<STR_LIT>" , <EOL> result . stdout ) <EOL> else : <EOL> status = match . group ( <NUM_LIT:1> ) <EOL> if status in constants . HV_KVM_MIGRATION_VALID_STATUSES : <EOL> migration_status = objects . MigrationStatus ( status = status ) <EOL> match = self . _MIGRATION_PROGRESS_RE . search ( result . stdout ) <EOL> if match : <EOL> migration_status . transferred_ram = match . group ( "<STR_LIT>" ) <EOL> migration_status . total_ram = match . group ( "<STR_LIT>" ) <EOL> return migration_status <EOL> logging . warning ( "<STR_LIT>" , status ) <EOL> time . sleep ( self . _MIGRATION_INFO_RETRY_DELAY ) <EOL> return objects . MigrationStatus ( status = constants . HV_MIGRATION_FAILED ) <EOL> def BalloonInstanceMemory ( self , instance , mem ) : <EOL> """<STR_LIT>""" <EOL> self . _CallMonitorCommand ( instance . name , "<STR_LIT>" % mem ) <EOL> def GetNodeInfo ( self , hvparams = None ) : <EOL> """<STR_LIT>""" <EOL> result = self . GetLinuxNodeInfo ( ) <EOL> kvmpath = constants . KVM_PATH <EOL> if hvparams is not None : <EOL> kvmpath = hvparams . get ( constants . HV_KVM_PATH , constants . KVM_PATH ) <EOL> _ , v_major , v_min , v_rev = self . _GetKVMVersion ( kvmpath ) <EOL> result [ constants . HV_NODEINFO_KEY_VERSION ] = ( v_major , v_min , v_rev ) <EOL> return result <EOL> @ classmethod <EOL> def GetInstanceConsole ( cls , instance , primary_node , node_group , <EOL> hvparams , beparams ) : <EOL> """<STR_LIT>""" <EOL> if hvparams [ constants . HV_SERIAL_CONSOLE ] : <EOL> cmd = [ pathutils . KVM_CONSOLE_WRAPPER , <EOL> constants . SOCAT_PATH , utils . ShellQuote ( instance . name ) , <EOL> utils . ShellQuote ( cls . _InstanceMonitor ( instance . name ) ) , <EOL> "<STR_LIT>" % cls . _SocatUnixConsoleParams ( ) , <EOL> "<STR_LIT>" % cls . _InstanceSerial ( instance . name ) ] <EOL> ndparams = node_group . FillND ( primary_node ) <EOL> return objects . InstanceConsole ( instance = instance . name , <EOL> kind = constants . CONS_SSH , <EOL> host = primary_node . name , <EOL> port = ndparams . get ( constants . ND_SSH_PORT ) , <EOL> user = constants . SSH_CONSOLE_USER , <EOL> command = cmd ) <EOL> vnc_bind_address = hvparams [ constants . HV_VNC_BIND_ADDRESS ] <EOL> if vnc_bind_address and instance . network_port > constants . VNC_BASE_PORT : <EOL> display = instance . network_port - constants . VNC_BASE_PORT <EOL> return objects . InstanceConsole ( instance = instance . name , <EOL> kind = constants . CONS_VNC , <EOL> host = vnc_bind_address , <EOL> port = instance . network_port , <EOL> display = display ) <EOL> spice_bind = hvparams [ constants . HV_KVM_SPICE_BIND ] <EOL> if spice_bind : <EOL> return objects . InstanceConsole ( instance = instance . name , <EOL> kind = constants . CONS_SPICE , <EOL> host = spice_bind , <EOL> port = instance . network_port ) <EOL> return objects . InstanceConsole ( instance = instance . name , <EOL> kind = constants . CONS_MESSAGE , <EOL> message = ( "<STR_LIT>" % <EOL> instance . name ) ) <EOL> def Verify ( self , hvparams = None ) : <EOL> """<STR_LIT>""" <EOL> msgs = [ ] <EOL> kvmpath = constants . KVM_PATH <EOL> if hvparams is not None : <EOL> kvmpath = hvparams . get ( constants . HV_KVM_PATH , constants . KVM_PATH ) <EOL> if not os . path . exists ( kvmpath ) : <EOL> msgs . append ( "<STR_LIT>" % kvmpath ) <EOL> if not os . path . exists ( constants . SOCAT_PATH ) : <EOL> msgs . append ( "<STR_LIT>" % <EOL> constants . SOCAT_PATH ) <EOL> return self . _FormatVerifyResults ( msgs ) <EOL> @ classmethod <EOL> def CheckParameterSyntax ( cls , hvparams ) : <EOL> """<STR_LIT>""" <EOL> super ( KVMHypervisor , cls ) . CheckParameterSyntax ( hvparams ) <EOL> kernel_path = hvparams [ constants . HV_KERNEL_PATH ] <EOL> if kernel_path : <EOL> if not hvparams [ constants . HV_ROOT_PATH ] : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if ( hvparams [ constants . HV_VNC_X509_VERIFY ] and <EOL> not hvparams [ constants . HV_VNC_X509 ] ) : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % <EOL> ( constants . HV_VNC_X509 , <EOL> constants . HV_VNC_X509_VERIFY ) ) <EOL> if hvparams [ constants . HV_SERIAL_CONSOLE ] : <EOL> serial_speed = hvparams [ constants . HV_SERIAL_SPEED ] <EOL> valid_speeds = constants . VALID_SERIAL_SPEEDS <EOL> if not serial_speed or serial_speed not in valid_speeds : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> utils . CommaJoin ( valid_speeds ) ) <EOL> boot_order = hvparams [ constants . HV_BOOT_ORDER ] <EOL> if ( boot_order == constants . HT_BO_CDROM and <EOL> not hvparams [ constants . HV_CDROM_IMAGE_PATH ] ) : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> security_model = hvparams [ constants . HV_SECURITY_MODEL ] <EOL> if security_model == constants . HT_SM_USER : <EOL> if not hvparams [ constants . HV_SECURITY_DOMAIN ] : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif ( security_model == constants . HT_SM_NONE or <EOL> security_model == constants . HT_SM_POOL ) : <EOL> if hvparams [ constants . HV_SECURITY_DOMAIN ] : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> spice_bind = hvparams [ constants . HV_KVM_SPICE_BIND ] <EOL> spice_ip_version = hvparams [ constants . HV_KVM_SPICE_IP_VERSION ] <EOL> if spice_bind : <EOL> if spice_ip_version != constants . IFACE_NO_IP_VERSION_SPECIFIED : <EOL> if ( netutils . IP4Address . IsValid ( spice_bind ) and <EOL> spice_ip_version != constants . IP4_VERSION ) : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( spice_bind , spice_ip_version ) ) <EOL> if ( netutils . IP6Address . IsValid ( spice_bind ) and <EOL> spice_ip_version != constants . IP6_VERSION ) : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( spice_bind , spice_ip_version ) ) <EOL> else : <EOL> for param in _SPICE_ADDITIONAL_PARAMS : <EOL> if hvparams [ param ] : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % <EOL> ( param , constants . HV_KVM_SPICE_BIND ) ) <EOL> @ classmethod <EOL> def ValidateParameters ( cls , hvparams ) : <EOL> """<STR_LIT>""" <EOL> super ( KVMHypervisor , cls ) . ValidateParameters ( hvparams ) <EOL> kvm_path = hvparams [ constants . HV_KVM_PATH ] <EOL> security_model = hvparams [ constants . HV_SECURITY_MODEL ] <EOL> if security_model == constants . HT_SM_USER : <EOL> username = hvparams [ constants . HV_SECURITY_DOMAIN ] <EOL> try : <EOL> pwd . getpwnam ( username ) <EOL> except KeyError : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> % username ) <EOL> vnc_bind_address = hvparams [ constants . HV_VNC_BIND_ADDRESS ] <EOL> if vnc_bind_address : <EOL> bound_to_addr = netutils . IP4Address . IsValid ( vnc_bind_address ) <EOL> is_interface = netutils . IsValidInterface ( vnc_bind_address ) <EOL> is_path = utils . IsNormAbsPath ( vnc_bind_address ) <EOL> if not bound_to_addr and not is_interface and not is_path : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> constants . HV_KVM_SPICE_BIND ) <EOL> spice_bind = hvparams [ constants . HV_KVM_SPICE_BIND ] <EOL> if spice_bind : <EOL> if hvparams [ constants . HV_VNC_BIND_ADDRESS ] : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> kvmhelp = cls . _GetKVMOutput ( kvm_path , cls . _KVMOPT_HELP ) <EOL> if not cls . _SPICE_RE . search ( kvmhelp ) : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> bound_to_addr = ( netutils . IP4Address . IsValid ( spice_bind ) or <EOL> netutils . IP6Address . IsValid ( spice_bind ) ) <EOL> if not bound_to_addr and not netutils . IsValidInterface ( spice_bind ) : <EOL> raise errors . HypervisorError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> constants . HV_KVM_SPICE_BIND ) <EOL> machine_version = hvparams [ constants . HV_KVM_MACHINE_VERSION ] <EOL> if machine_version : <EOL> output = cls . _GetKVMOutput ( kvm_path , cls . _KVMOPT_MLIST ) <EOL> if not cls . _CHECK_MACHINE_VERSION_RE ( machine_version ) . search ( output ) : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % <EOL> machine_version ) <EOL> @ classmethod <EOL> def PowercycleNode ( cls , hvparams = None ) : <EOL> """<STR_LIT>""" <EOL> cls . LinuxPowercycle ( ) </s>
