X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/3c286190a94c93de556e82fd2d84362b9e52a8d3..a8e8c0c6afd0a8c124571d47b07b1998d2aefc78:/lib/hypervisor/hv_kvm.py diff --git a/lib/hypervisor/hv_kvm.py b/lib/hypervisor/hv_kvm.py index da8c183..98b271f 100644 --- a/lib/hypervisor/hv_kvm.py +++ b/lib/hypervisor/hv_kvm.py @@ -68,6 +68,17 @@ IFF_TAP = 0x0002 IFF_NO_PI = 0x1000 IFF_VNET_HDR = 0x4000 +#: SPICE parameters which depend on L{constants.HV_KVM_SPICE_BIND} +_SPICE_ADDITIONAL_PARAMS = frozenset([ + constants.HV_KVM_SPICE_IP_VERSION, + constants.HV_KVM_SPICE_PASSWORD_FILE, + constants.HV_KVM_SPICE_LOSSLESS_IMG_COMPR, + constants.HV_KVM_SPICE_JPEG_IMG_COMPR, + constants.HV_KVM_SPICE_ZLIB_GLZ_IMG_COMPR, + constants.HV_KVM_SPICE_STREAMING_VIDEO_DETECTION, + constants.HV_KVM_SPICE_USE_TLS, + ]) + def _ProbeTapVnetHdr(fd): """Check whether to enable the IFF_VNET_HDR flag. @@ -136,6 +147,31 @@ def _OpenTap(vnet_hdr=True): return (ifname, tapfd) +def _BuildNetworkEnv(name, network, gateway, network6, gateway6, + network_type, mac_prefix, tags, env): + """Build environment variables concerning a Network. + + """ + if name: + env["NETWORK_NAME"] = name + if network: + env["NETWORK_SUBNET"] = network + if gateway: + env["NETWORK_GATEWAY"] = gateway + if network6: + env["NETWORK_SUBNET6"] = network6 + if gateway6: + env["NETWORK_GATEWAY6"] = gateway6 + if mac_prefix: + env["NETWORK_MAC_PREFIX"] = mac_prefix + if network_type: + env["NETWORK_TYPE"] = network_type + if tags: + env["NETWORK_TAGS"] = " ".join(tags) + + return env + + class QmpMessage: """QEMU Messaging Protocol (QMP) message. @@ -424,12 +460,14 @@ class KVMHypervisor(hv_base.BaseHypervisor): _CHROOT_DIR, _CHROOT_QUARANTINE_DIR, _KEYMAP_DIR] PARAMETERS = { + constants.HV_KVM_PATH: hv_base.REQ_FILE_CHECK, constants.HV_KERNEL_PATH: hv_base.OPT_FILE_CHECK, constants.HV_INITRD_PATH: hv_base.OPT_FILE_CHECK, constants.HV_ROOT_PATH: hv_base.NO_CHECK, constants.HV_KERNEL_ARGS: hv_base.NO_CHECK, constants.HV_ACPI: hv_base.NO_CHECK, constants.HV_SERIAL_CONSOLE: hv_base.NO_CHECK, + constants.HV_SERIAL_SPEED: hv_base.NO_CHECK, constants.HV_VNC_BIND_ADDRESS: (False, lambda x: (netutils.IP4Address.IsValid(x) or utils.IsNormAbsPath(x)), @@ -477,8 +515,8 @@ class KVMHypervisor(hv_base.BaseHypervisor): hv_base.ParamInSet(False, constants.HT_KVM_VALID_MOUSE_TYPES), constants.HV_KEYMAP: hv_base.NO_CHECK, constants.HV_MIGRATION_PORT: hv_base.REQ_NET_PORT_CHECK, - constants.HV_MIGRATION_BANDWIDTH: hv_base.NO_CHECK, - constants.HV_MIGRATION_DOWNTIME: hv_base.NO_CHECK, + constants.HV_MIGRATION_BANDWIDTH: hv_base.REQ_NONNEGATIVE_INT_CHECK, + constants.HV_MIGRATION_DOWNTIME: hv_base.REQ_NONNEGATIVE_INT_CHECK, constants.HV_MIGRATION_MODE: hv_base.MIGRATION_MODE_CHECK, constants.HV_USE_LOCALTIME: hv_base.NO_CHECK, constants.HV_DISK_CACHE: @@ -495,6 +533,14 @@ class KVMHypervisor(hv_base.BaseHypervisor): hv_base.ParamInSet(True, constants.REBOOT_BEHAVIORS), constants.HV_CPU_MASK: hv_base.OPT_MULTI_CPU_MASK_CHECK, constants.HV_CPU_TYPE: hv_base.NO_CHECK, + constants.HV_CPU_CORES: hv_base.OPT_NONNEGATIVE_INT_CHECK, + constants.HV_CPU_THREADS: hv_base.OPT_NONNEGATIVE_INT_CHECK, + constants.HV_CPU_SOCKETS: hv_base.OPT_NONNEGATIVE_INT_CHECK, + constants.HV_SOUNDHW: hv_base.NO_CHECK, + constants.HV_USB_DEVICES: hv_base.NO_CHECK, + constants.HV_VGA: hv_base.NO_CHECK, + constants.HV_KVM_EXTRA: hv_base.NO_CHECK, + constants.HV_KVM_MACHINE_VERSION: hv_base.NO_CHECK, } _MIGRATION_STATUS_RE = re.compile("Migration\s+status:\s+(\w+)", @@ -513,6 +559,21 @@ class KVMHypervisor(hv_base.BaseHypervisor): _CPU_INFO_CMD = "info cpus" _CONT_CMD = "cont" + _DEFAULT_MACHINE_VERSION_RE = re.compile(r"^(\S+).*\(default\)", re.M) + _CHECK_MACHINE_VERSION_RE = \ + staticmethod(lambda x: re.compile(r"^(%s)[ ]+.*PC" % x, re.M)) + + _QMP_RE = re.compile(r"^-qmp\s", re.M) + _SPICE_RE = re.compile(r"^-spice\s", re.M) + _VHOST_RE = re.compile(r"^-net\s.*,vhost=on|off", re.M) + _ENABLE_KVM_RE = re.compile(r"^-enable-kvm\s", re.M) + _DISABLE_KVM_RE = re.compile(r"^-disable-kvm\s", re.M) + _NETDEV_RE = re.compile(r"^-netdev\s", re.M) + # match -drive.*boot=on|off on different lines, but in between accept only + # dashes not preceeded by a new line (which would mean another option + # different than -drive is starting) + _BOOT_RE = re.compile(r"^-drive\s([^-]|(?= (0, 13): + # check for vhost_net support + if self._VHOST_RE.search(kvmhelp): tap_extra = ",vhost=on" else: raise errors.HypervisorError("vhost_net is configured" @@ -1432,7 +1511,7 @@ class KVMHypervisor(hv_base.BaseHypervisor): tapname, tapfd = _OpenTap(vnet_hdr) tapfds.append(tapfd) taps.append(tapname) - if (v_major, v_min) >= (0, 12): + if self._NETDEV_RE.search(kvmhelp): nic_val = "%s,mac=%s,netdev=netdev%s" % (nic_model, nic.mac, nic_seq) tap_val = "type=tap,id=netdev%s,fd=%d%s" % (nic_seq, tapfd, tap_extra) kvm_cmd.extend(["-netdev", tap_val, "-device", nic_val]) @@ -1463,7 +1542,7 @@ class KVMHypervisor(hv_base.BaseHypervisor): constants.SECURE_DIR_MODE)]) # Automatically enable QMP if version is >= 0.14 - if (v_major, v_min) >= (0, 14): + if self._QMP_RE.search(kvmhelp): logging.debug("Enabling QMP") kvm_cmd.extend(["-qmp", "unix:%s,server,nowait" % self._InstanceQmpMonitor(instance.name)]) @@ -1562,10 +1641,12 @@ class KVMHypervisor(hv_base.BaseHypervisor): """ self._CheckDown(instance.name) + kvmpath = instance.hvparams[constants.HV_KVM_PATH] + kvmhelp = self._GetKVMHelpOutput(kvmpath) kvm_runtime = self._GenerateKVMRuntime(instance, block_devices, - startup_paused) + startup_paused, kvmhelp) self._SaveKVMRuntime(instance, kvm_runtime) - self._ExecuteKVMRuntime(instance, kvm_runtime) + self._ExecuteKVMRuntime(instance, kvm_runtime, kvmhelp) def _CallMonitorCommand(self, instance_name, command): """Invoke a command on the instance monitor. @@ -1609,17 +1690,52 @@ class KVMHypervisor(hv_base.BaseHypervisor): return (v_all, v_maj, v_min, v_rev) @classmethod - def _GetKVMVersion(cls): + def _GetKVMHelpOutput(cls, kvm_path): + """Return the KVM help output. + + @return: output of kvm --help + @raise errors.HypervisorError: when the KVM help output cannot be retrieved + + """ + result = utils.RunCmd([kvm_path, "--help"]) + if result.failed: + raise errors.HypervisorError("Unable to get KVM help output") + return result.output + + @classmethod + def _GetKVMVersion(cls, kvm_path): """Return the installed KVM version. @return: (version, v_maj, v_min, v_rev) @raise errors.HypervisorError: when the KVM version cannot be retrieved """ - result = utils.RunCmd([constants.KVM_PATH, "--help"]) + return cls._ParseKVMVersion(cls._GetKVMHelpOutput(kvm_path)) + + @classmethod + def _GetKVMSupportedMachinesOutput(cls, kvm_path): + """Return the output regarding supported machine versions. + + @return: output of kvm -M ? + @raise errors.HypervisorError: when the KVM help output cannot be retrieved + + """ + result = utils.RunCmd([kvm_path, "-M", "?"]) if result.failed: - raise errors.HypervisorError("Unable to get KVM version") - return cls._ParseKVMVersion(result.output) + raise errors.HypervisorError("Unable to get kvm -M ? output") + return result.output + + @classmethod + def _GetDefaultMachineVersion(cls, kvm_path): + """Return the default hardware revision (e.g. pc-1.1) + + """ + output = cls._GetKVMSupportedMachinesOutput(kvm_path) + match = cls._DEFAULT_MACHINE_VERSION_RE.search(output) + if match: + return match.group(1) + else: + return "pc" def StopInstance(self, instance, force=False, retry=False, name=None): """Stop an instance. @@ -1667,7 +1783,9 @@ class KVMHypervisor(hv_base.BaseHypervisor): self.StopInstance(instance, force=True) # ...and finally we can save it again, and execute it... self._SaveKVMRuntime(instance, kvm_runtime) - self._ExecuteKVMRuntime(instance, kvm_runtime) + kvmpath = instance.hvparams[constants.HV_KVM_PATH] + kvmhelp = self._GetKVMHelpOutput(kvmpath) + self._ExecuteKVMRuntime(instance, kvm_runtime, kvmhelp) def MigrationInfo(self, instance): """Get instance information to perform a migration. @@ -1693,7 +1811,10 @@ class KVMHypervisor(hv_base.BaseHypervisor): """ kvm_runtime = self._LoadKVMRuntime(instance, serialized_runtime=info) incoming_address = (target, instance.hvparams[constants.HV_MIGRATION_PORT]) - self._ExecuteKVMRuntime(instance, kvm_runtime, incoming=incoming_address) + kvmpath = instance.hvparams[constants.HV_KVM_PATH] + kvmhelp = self._GetKVMHelpOutput(kvmpath) + self._ExecuteKVMRuntime(instance, kvm_runtime, kvmhelp, + incoming=incoming_address) def FinalizeMigrationDst(self, instance, info, success): """Finalize the instance migration on the target node. @@ -1840,7 +1961,10 @@ class KVMHypervisor(hv_base.BaseHypervisor): """ result = self.GetLinuxNodeInfo() - _, v_major, v_min, v_rev = self._GetKVMVersion() + # FIXME: this is the global kvm version, but the actual version can be + # customized as an hv parameter. we should use the nodegroup's default kvm + # path parameter here. + _, v_major, v_min, v_rev = self._GetKVMVersion(constants.KVM_PATH) result[constants.HV_NODEINFO_KEY_VERSION] = (v_major, v_min, v_rev) return result @@ -1888,6 +2012,9 @@ class KVMHypervisor(hv_base.BaseHypervisor): Check that the binary exists. """ + # FIXME: this is the global kvm version, but the actual version can be + # customized as an hv parameter. we should use the nodegroup's default kvm + # path parameter here. if not os.path.exists(constants.KVM_PATH): return "The kvm binary ('%s') does not exist." % constants.KVM_PATH if not os.path.exists(constants.SOCAT_PATH): @@ -1916,6 +2043,14 @@ class KVMHypervisor(hv_base.BaseHypervisor): (constants.HV_VNC_X509, constants.HV_VNC_X509_VERIFY)) + if hvparams[constants.HV_SERIAL_CONSOLE]: + serial_speed = hvparams[constants.HV_SERIAL_SPEED] + valid_speeds = constants.VALID_SERIAL_SPEEDS + if not serial_speed or serial_speed not in valid_speeds: + raise errors.HypervisorError("Invalid serial console speed, must be" + " one of: %s" % + utils.CommaJoin(valid_speeds)) + boot_order = hvparams[constants.HV_BOOT_ORDER] if (boot_order == constants.HT_BO_CDROM and not hvparams[constants.HV_CDROM_IMAGE_PATH]): @@ -1953,16 +2088,7 @@ class KVMHypervisor(hv_base.BaseHypervisor): else: # All the other SPICE parameters depend on spice_bind being set. Raise an # error if any of them is set without it. - spice_additional_params = frozenset([ - constants.HV_KVM_SPICE_IP_VERSION, - constants.HV_KVM_SPICE_PASSWORD_FILE, - constants.HV_KVM_SPICE_LOSSLESS_IMG_COMPR, - constants.HV_KVM_SPICE_JPEG_IMG_COMPR, - constants.HV_KVM_SPICE_ZLIB_GLZ_IMG_COMPR, - constants.HV_KVM_SPICE_STREAMING_VIDEO_DETECTION, - constants.HV_KVM_SPICE_USE_TLS, - ]) - for param in spice_additional_params: + for param in _SPICE_ADDITIONAL_PARAMS: if hvparams[param]: raise errors.HypervisorError("spice: %s requires %s to be set" % (param, constants.HV_KVM_SPICE_BIND)) @@ -1978,6 +2104,8 @@ class KVMHypervisor(hv_base.BaseHypervisor): """ super(KVMHypervisor, cls).ValidateParameters(hvparams) + kvm_path = hvparams[constants.HV_KVM_PATH] + security_model = hvparams[constants.HV_SECURITY_MODEL] if security_model == constants.HT_SM_USER: username = hvparams[constants.HV_SECURITY_DOMAIN] @@ -1995,11 +2123,11 @@ class KVMHypervisor(hv_base.BaseHypervisor): " only one of them can be used at a" " given time.") - # KVM version should be >= 0.14.0 - _, v_major, v_min, _ = cls._GetKVMVersion() - if (v_major, v_min) < (0, 14): + # check that KVM supports SPICE + kvmhelp = cls._GetKVMHelpOutput(kvm_path) + if not cls._SPICE_RE.search(kvmhelp): raise errors.HypervisorError("spice is configured, but it is not" - " available in versions of KVM < 0.14") + " supported according to kvm --help") # if spice_bind is not an IP address, it must be a valid interface bound_to_addr = (netutils.IP4Address.IsValid(spice_bind) @@ -2009,6 +2137,13 @@ class KVMHypervisor(hv_base.BaseHypervisor): " a valid IP address or interface name" % constants.HV_KVM_SPICE_BIND) + machine_version = hvparams[constants.HV_KVM_MACHINE_VERSION] + if machine_version: + output = cls._GetKVMSupportedMachinesOutput(kvm_path) + if not cls._CHECK_MACHINE_VERSION_RE(machine_version).search(output): + raise errors.HypervisorError("Unsupported machine version: %s" % + machine_version) + @classmethod def PowercycleNode(cls): """KVM powercycle, just a wrapper over Linux powercycle.