1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 """KVM hypervisor
32
33 """
34
35 import errno
36 import os
37 import os.path
38 import re
39 import tempfile
40 import time
41 import logging
42 import pwd
43 import struct
44 import fcntl
45 import shutil
46 import socket
47 import stat
48 import StringIO
49 from bitarray import bitarray
50 try:
51 import affinity
52 except ImportError:
53 affinity = None
54 try:
55 import fdsend
56 except ImportError:
57 fdsend = None
58
59 from ganeti import utils
60 from ganeti import constants
61 from ganeti import errors
62 from ganeti import serializer
63 from ganeti import objects
64 from ganeti import uidpool
65 from ganeti import ssconf
66 from ganeti import netutils
67 from ganeti import pathutils
68 from ganeti.hypervisor import hv_base
69 from ganeti.utils import wrapper as utils_wrapper
70
71
72 _KVM_NETWORK_SCRIPT = pathutils.CONF_DIR + "/kvm-vif-bridge"
73 _KVM_START_PAUSED_FLAG = "-S"
74
75
76
77
78 TUNSETIFF = 0x400454ca
79 TUNGETIFF = 0x800454d2
80 TUNGETFEATURES = 0x800454cf
81 IFF_TAP = 0x0002
82 IFF_NO_PI = 0x1000
83 IFF_ONE_QUEUE = 0x2000
84 IFF_VNET_HDR = 0x4000
85
86
87 _SPICE_ADDITIONAL_PARAMS = frozenset([
88 constants.HV_KVM_SPICE_IP_VERSION,
89 constants.HV_KVM_SPICE_PASSWORD_FILE,
90 constants.HV_KVM_SPICE_LOSSLESS_IMG_COMPR,
91 constants.HV_KVM_SPICE_JPEG_IMG_COMPR,
92 constants.HV_KVM_SPICE_ZLIB_GLZ_IMG_COMPR,
93 constants.HV_KVM_SPICE_STREAMING_VIDEO_DETECTION,
94 constants.HV_KVM_SPICE_USE_TLS,
95 ])
96
97
98
99 _AVAILABLE_PCI_SLOT = bitarray("0")
100
101
102
103
104
105 _KVM_NICS_RUNTIME_INDEX = 1
106 _KVM_DISKS_RUNTIME_INDEX = 3
107 _DEVICE_RUNTIME_INDEX = {
108 constants.HOTPLUG_TARGET_DISK: _KVM_DISKS_RUNTIME_INDEX,
109 constants.HOTPLUG_TARGET_NIC: _KVM_NICS_RUNTIME_INDEX
110 }
111 _FIND_RUNTIME_ENTRY = {
112 constants.HOTPLUG_TARGET_NIC:
113 lambda nic, kvm_nics: [n for n in kvm_nics if n.uuid == nic.uuid],
114 constants.HOTPLUG_TARGET_DISK:
115 lambda disk, kvm_disks: [(d, l, u) for (d, l, u) in kvm_disks
116 if d.uuid == disk.uuid]
117 }
118 _RUNTIME_DEVICE = {
119 constants.HOTPLUG_TARGET_NIC: lambda d: d,
120 constants.HOTPLUG_TARGET_DISK: lambda (d, e, _): d
121 }
122 _RUNTIME_ENTRY = {
123 constants.HOTPLUG_TARGET_NIC: lambda d, e: d,
124 constants.HOTPLUG_TARGET_DISK: lambda d, e: (d, e[0], e[1])
125 }
129 """Helper function to get the drive uri to be used in --drive kvm option
130
131 @type disk: L{objects.Disk}
132 @param disk: A disk configuration object
133 @type link: string
134 @param link: The device link as returned by _SymlinkBlockDev()
135 @type uri: string
136 @param uri: The drive uri as returned by _CalculateDeviceURI()
137
138 """
139 access_mode = disk.params.get(constants.LDP_ACCESS,
140 constants.DISK_KERNELSPACE)
141 if (uri and access_mode == constants.DISK_USERSPACE):
142 drive_uri = uri
143 else:
144 drive_uri = link
145
146 return drive_uri
147
150 """Helper function to generate a unique device name used by KVM
151
152 QEMU monitor commands use names to identify devices. Here we use their pci
153 slot and a part of their UUID to name them. dev.pci might be None for old
154 devices in the cluster.
155
156 @type dev_type: sting
157 @param dev_type: device type of param dev
158 @type dev: L{objects.Disk} or L{objects.NIC}
159 @param dev: the device object for which we generate a kvm name
160 @raise errors.HotplugError: in case a device has no pci slot (old devices)
161
162 """
163
164 if not dev.pci:
165 raise errors.HotplugError("Hotplug is not supported for %s with UUID %s" %
166 (dev_type, dev.uuid))
167
168 return "%s-%s-pci-%d" % (dev_type.lower(), dev.uuid.split("-")[0], dev.pci)
169
172 """Helper method to get first available slot in a bitarray
173
174 @type slots: bitarray
175 @param slots: the bitarray to operate on
176 @type slot: integer
177 @param slot: if given we check whether the slot is free
178 @type reserve: boolean
179 @param reserve: whether to reserve the first available slot or not
180 @return: the idx of the (first) available slot
181 @raise errors.HotplugError: If all slots in a bitarray are occupied
182 or the given slot is not free.
183
184 """
185 if slot is not None:
186 assert slot < len(slots)
187 if slots[slot]:
188 raise errors.HypervisorError("Slots %d occupied" % slot)
189
190 else:
191 avail = slots.search(_AVAILABLE_PCI_SLOT, 1)
192 if not avail:
193 raise errors.HypervisorError("All slots occupied")
194
195 slot = int(avail[0])
196
197 if reserve:
198 slots[slot] = True
199
200 return slot
201
204 """Helper function to get an existing device inside the runtime file
205
206 Used when an instance is running. Load kvm runtime file and search
207 for a device based on its type and uuid.
208
209 @type dev_type: sting
210 @param dev_type: device type of param dev
211 @type device: L{objects.Disk} or L{objects.NIC}
212 @param device: the device object for which we generate a kvm name
213 @type runtime: tuple (cmd, nics, hvparams, disks)
214 @param runtime: the runtime data to search for the device
215 @raise errors.HotplugError: in case the requested device does not
216 exist (e.g. device has been added without --hotplug option) or
217 device info has not pci slot (e.g. old devices in the cluster)
218
219 """
220 index = _DEVICE_RUNTIME_INDEX[dev_type]
221 found = _FIND_RUNTIME_ENTRY[dev_type](device, runtime[index])
222 if not found:
223 raise errors.HotplugError("Cannot find runtime info for %s with UUID %s" %
224 (dev_type, device.uuid))
225
226 return found[0]
227
230 """Upgrade runtime data
231
232 Remove any deprecated fields or change the format of the data.
233 The runtime files are not upgraded when Ganeti is upgraded, so the required
234 modification have to be performed here.
235
236 @type serialized_runtime: string
237 @param serialized_runtime: raw text data read from actual runtime file
238 @return: (cmd, nic dicts, hvparams, bdev dicts)
239 @rtype: tuple
240
241 """
242 loaded_runtime = serializer.Load(serialized_runtime)
243 kvm_cmd, serialized_nics, hvparams = loaded_runtime[:3]
244 if len(loaded_runtime) >= 4:
245 serialized_disks = loaded_runtime[3]
246 else:
247 serialized_disks = []
248
249 for nic in serialized_nics:
250
251 if "uuid" not in nic:
252 nic["uuid"] = utils.NewUUID()
253
254 return kvm_cmd, serialized_nics, hvparams, serialized_disks
255
258 """Return runtime entries for a serialized runtime file
259
260 @type serialized_runtime: string
261 @param serialized_runtime: raw text data read from actual runtime file
262 @return: (cmd, nics, hvparams, bdevs)
263 @rtype: tuple
264
265 """
266 kvm_cmd, serialized_nics, hvparams, serialized_disks = \
267 _UpgradeSerializedRuntime(serialized_runtime)
268 kvm_nics = [objects.NIC.FromDict(snic) for snic in serialized_nics]
269 kvm_disks = [(objects.Disk.FromDict(sdisk), link, uri)
270 for sdisk, link, uri in serialized_disks]
271
272 return (kvm_cmd, kvm_nics, hvparams, kvm_disks)
273
276 """Retrieves supported TUN features from file descriptor.
277
278 @see: L{_ProbeTapVnetHdr}
279
280 """
281 req = struct.pack("I", 0)
282 try:
283 buf = _ioctl(fd, TUNGETFEATURES, req)
284 except EnvironmentError, err:
285 logging.warning("ioctl(TUNGETFEATURES) failed: %s", err)
286 return None
287 else:
288 (flags, ) = struct.unpack("I", buf)
289 return flags
290
293 """Check whether to enable the IFF_VNET_HDR flag.
294
295 To do this, _all_ of the following conditions must be met:
296 1. TUNGETFEATURES ioctl() *must* be implemented
297 2. TUNGETFEATURES ioctl() result *must* contain the IFF_VNET_HDR flag
298 3. TUNGETIFF ioctl() *must* be implemented; reading the kernel code in
299 drivers/net/tun.c there is no way to test this until after the tap device
300 has been created using TUNSETIFF, and there is no way to change the
301 IFF_VNET_HDR flag after creating the interface, catch-22! However both
302 TUNGETIFF and TUNGETFEATURES were introduced in kernel version 2.6.27,
303 thus we can expect TUNGETIFF to be present if TUNGETFEATURES is.
304
305 @type fd: int
306 @param fd: the file descriptor of /dev/net/tun
307
308 """
309 flags = _features_fn(fd)
310
311 if flags is None:
312
313 return False
314
315 result = bool(flags & IFF_VNET_HDR)
316
317 if not result:
318 logging.warning("Kernel does not support IFF_VNET_HDR, not enabling")
319
320 return result
321
324 """Open a new tap device and return its file descriptor.
325
326 This is intended to be used by a qemu-type hypervisor together with the -net
327 tap,fd=<fd> command line parameter.
328
329 @type vnet_hdr: boolean
330 @param vnet_hdr: Enable the VNET Header
331 @return: (ifname, tapfd)
332 @rtype: tuple
333
334 """
335 try:
336 tapfd = os.open("/dev/net/tun", os.O_RDWR)
337 except EnvironmentError:
338 raise errors.HypervisorError("Failed to open /dev/net/tun")
339
340 flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE
341
342 if vnet_hdr and _ProbeTapVnetHdr(tapfd):
343 flags |= IFF_VNET_HDR
344
345
346 ifr = struct.pack("16sh", "", flags)
347
348 try:
349 res = fcntl.ioctl(tapfd, TUNSETIFF, ifr)
350 except EnvironmentError, err:
351 raise errors.HypervisorError("Failed to allocate a new TAP device: %s" %
352 err)
353
354
355 ifname = struct.unpack("16sh", res)[0].strip("\x00")
356 return (ifname, tapfd)
357
360 """QEMU Messaging Protocol (QMP) message.
361
362 """
364 """Creates a new QMP message based on the passed data.
365
366 """
367 if not isinstance(data, dict):
368 raise TypeError("QmpMessage must be initialized with a dict")
369
370 self.data = data
371
373 """Get the value of the required field if present, or None.
374
375 Overrides the [] operator to provide access to the message data,
376 returning None if the required item is not in the message
377 @return: the value of the field_name field, or None if field_name
378 is not contained in the message
379
380 """
381 return self.data.get(field_name, None)
382
384 """Set the value of the required field_name to field_value.
385
386 """
387 self.data[field_name] = field_value
388
390 """Return the number of fields stored in this QmpMessage.
391
392 """
393 return len(self.data)
394
396 """Delete the specified element from the QmpMessage.
397
398 """
399 del(self.data[key])
400
401 @staticmethod
403 """Build a QmpMessage from a JSON encoded string.
404
405 @type json_string: str
406 @param json_string: JSON string representing the message
407 @rtype: L{QmpMessage}
408 @return: a L{QmpMessage} built from json_string
409
410 """
411
412 data = serializer.LoadJson(json_string)
413 return QmpMessage(data)
414
418
420
421
422 return self.data == other.data
423
426 _SOCKET_TIMEOUT = 5
427
429 """Instantiates the MonitorSocket object.
430
431 @type monitor_filename: string
432 @param monitor_filename: the filename of the UNIX raw socket on which the
433 monitor (QMP or simple one) is listening
434
435 """
436 self.monitor_filename = monitor_filename
437 self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
438
439
440 self.sock.settimeout(self._SOCKET_TIMEOUT)
441 self._connected = False
442
444 sock_stat = None
445 try:
446 sock_stat = os.stat(self.monitor_filename)
447 except EnvironmentError, err:
448 if err.errno == errno.ENOENT:
449 raise errors.HypervisorError("No monitor socket found")
450 else:
451 raise errors.HypervisorError("Error checking monitor socket: %s",
452 utils.ErrnoOrStr(err))
453 if not stat.S_ISSOCK(sock_stat.st_mode):
454 raise errors.HypervisorError("Monitor socket is not a socket")
455
457 """Make sure that the connection is established.
458
459 """
460 if not self._connected:
461 raise errors.ProgrammerError("To use a MonitorSocket you need to first"
462 " invoke connect() on it")
463
465 """Connects to the monitor.
466
467 Connects to the UNIX socket
468
469 @raise errors.HypervisorError: when there are communication errors
470
471 """
472 if self._connected:
473 raise errors.ProgrammerError("Cannot connect twice")
474
475 self._check_socket()
476
477
478 try:
479 self.sock.connect(self.monitor_filename)
480 except EnvironmentError:
481 raise errors.HypervisorError("Can't connect to qmp socket")
482 self._connected = True
483
485 """Closes the socket
486
487 It cannot be used after this call.
488
489 """
490 self.sock.close()
491
494 """Connection to the QEMU Monitor using the QEMU Monitor Protocol (QMP).
495
496 """
497 _FIRST_MESSAGE_KEY = "QMP"
498 _EVENT_KEY = "event"
499 _ERROR_KEY = "error"
500 _RETURN_KEY = RETURN_KEY = "return"
501 _ACTUAL_KEY = ACTUAL_KEY = "actual"
502 _ERROR_CLASS_KEY = "class"
503 _ERROR_DESC_KEY = "desc"
504 _EXECUTE_KEY = "execute"
505 _ARGUMENTS_KEY = "arguments"
506 _CAPABILITIES_COMMAND = "qmp_capabilities"
507 _MESSAGE_END_TOKEN = "\r\n"
508
512
514 """Connects to the QMP monitor.
515
516 Connects to the UNIX socket and makes sure that we can actually send and
517 receive data to the kvm instance via QMP.
518
519 @raise errors.HypervisorError: when there are communication errors
520 @raise errors.ProgrammerError: when there are data serialization errors
521
522 """
523 super(QmpConnection, self).connect()
524
525
526 greeting = self._Recv()
527 if not greeting[self._FIRST_MESSAGE_KEY]:
528 self._connected = False
529 raise errors.HypervisorError("kvm: QMP communication error (wrong"
530 " server greeting")
531
532
533
534 self._buf = ""
535
536
537
538
539 self.Execute(self._CAPABILITIES_COMMAND)
540
542 """Extract and parse a QMP message from the given buffer.
543
544 Seeks for a QMP message in the given buf. If found, it parses it and
545 returns it together with the rest of the characters in the buf.
546 If no message is found, returns None and the whole buffer.
547
548 @raise errors.ProgrammerError: when there are data serialization errors
549
550 """
551 message = None
552
553
554 pos = buf.find(self._MESSAGE_END_TOKEN)
555 if pos >= 0:
556 try:
557 message = QmpMessage.BuildFromJsonString(buf[:pos + 1])
558 except Exception, err:
559 raise errors.ProgrammerError("QMP data serialization error: %s" % err)
560 buf = buf[pos + 1:]
561
562 return (message, buf)
563
565 """Receives a message from QMP and decodes the received JSON object.
566
567 @rtype: QmpMessage
568 @return: the received message
569 @raise errors.HypervisorError: when there are communication errors
570 @raise errors.ProgrammerError: when there are data serialization errors
571
572 """
573 self._check_connection()
574
575
576 (message, self._buf) = self._ParseMessage(self._buf)
577 if message:
578 return message
579
580 recv_buffer = StringIO.StringIO(self._buf)
581 recv_buffer.seek(len(self._buf))
582 try:
583 while True:
584 data = self.sock.recv(4096)
585 if not data:
586 break
587 recv_buffer.write(data)
588
589 (message, self._buf) = self._ParseMessage(recv_buffer.getvalue())
590 if message:
591 return message
592
593 except socket.timeout, err:
594 raise errors.HypervisorError("Timeout while receiving a QMP message: "
595 "%s" % (err))
596 except socket.error, err:
597 raise errors.HypervisorError("Unable to receive data from KVM using the"
598 " QMP protocol: %s" % err)
599
600 - def _Send(self, message):
601 """Encodes and sends a message to KVM using QMP.
602
603 @type message: QmpMessage
604 @param message: message to send to KVM
605 @raise errors.HypervisorError: when there are communication errors
606 @raise errors.ProgrammerError: when there are data serialization errors
607
608 """
609 self._check_connection()
610 try:
611 message_str = str(message)
612 except Exception, err:
613 raise errors.ProgrammerError("QMP data deserialization error: %s" % err)
614
615 try:
616 self.sock.sendall(message_str)
617 except socket.timeout, err:
618 raise errors.HypervisorError("Timeout while sending a QMP message: "
619 "%s (%s)" % (err.string, err.errno))
620 except socket.error, err:
621 raise errors.HypervisorError("Unable to send data from KVM using the"
622 " QMP protocol: %s" % err)
623
624 - def Execute(self, command, arguments=None):
625 """Executes a QMP command and returns the response of the server.
626
627 @type command: str
628 @param command: the command to execute
629 @type arguments: dict
630 @param arguments: dictionary of arguments to be passed to the command
631 @rtype: dict
632 @return: dictionary representing the received JSON object
633 @raise errors.HypervisorError: when there are communication errors
634 @raise errors.ProgrammerError: when there are data serialization errors
635
636 """
637 self._check_connection()
638 message = QmpMessage({self._EXECUTE_KEY: command})
639 if arguments:
640 message[self._ARGUMENTS_KEY] = arguments
641 self._Send(message)
642
643
644
645 while True:
646 response = self._Recv()
647 err = response[self._ERROR_KEY]
648 if err:
649 raise errors.HypervisorError("kvm: error executing the %s"
650 " command: %s (%s):" %
651 (command,
652 err[self._ERROR_DESC_KEY],
653 err[self._ERROR_CLASS_KEY]))
654
655 elif not response[self._EVENT_KEY]:
656 return response
657
660 """KVM hypervisor interface
661
662 """
663 CAN_MIGRATE = True
664
665 _ROOT_DIR = pathutils.RUN_DIR + "/kvm-hypervisor"
666 _PIDS_DIR = _ROOT_DIR + "/pid"
667 _UIDS_DIR = _ROOT_DIR + "/uid"
668 _CTRL_DIR = _ROOT_DIR + "/ctrl"
669 _CONF_DIR = _ROOT_DIR + "/conf"
670 _NICS_DIR = _ROOT_DIR + "/nic"
671 _KEYMAP_DIR = _ROOT_DIR + "/keymap"
672
673 _CHROOT_DIR = _ROOT_DIR + "/chroot"
674
675
676
677
678
679 _CHROOT_QUARANTINE_DIR = _ROOT_DIR + "/chroot-quarantine"
680 _DIRS = [_ROOT_DIR, _PIDS_DIR, _UIDS_DIR, _CTRL_DIR, _CONF_DIR, _NICS_DIR,
681 _CHROOT_DIR, _CHROOT_QUARANTINE_DIR, _KEYMAP_DIR]
682
683 PARAMETERS = {
684 constants.HV_KVM_PATH: hv_base.REQ_FILE_CHECK,
685 constants.HV_KERNEL_PATH: hv_base.OPT_FILE_CHECK,
686 constants.HV_INITRD_PATH: hv_base.OPT_FILE_CHECK,
687 constants.HV_ROOT_PATH: hv_base.NO_CHECK,
688 constants.HV_KERNEL_ARGS: hv_base.NO_CHECK,
689 constants.HV_ACPI: hv_base.NO_CHECK,
690 constants.HV_SERIAL_CONSOLE: hv_base.NO_CHECK,
691 constants.HV_SERIAL_SPEED: hv_base.NO_CHECK,
692 constants.HV_VNC_BIND_ADDRESS: hv_base.NO_CHECK,
693 constants.HV_VNC_TLS: hv_base.NO_CHECK,
694 constants.HV_VNC_X509: hv_base.OPT_DIR_CHECK,
695 constants.HV_VNC_X509_VERIFY: hv_base.NO_CHECK,
696 constants.HV_VNC_PASSWORD_FILE: hv_base.OPT_FILE_CHECK,
697 constants.HV_KVM_SPICE_BIND: hv_base.NO_CHECK,
698 constants.HV_KVM_SPICE_IP_VERSION:
699 (False, lambda x: (x == constants.IFACE_NO_IP_VERSION_SPECIFIED or
700 x in constants.VALID_IP_VERSIONS),
701 "The SPICE IP version should be 4 or 6",
702 None, None),
703 constants.HV_KVM_SPICE_PASSWORD_FILE: hv_base.OPT_FILE_CHECK,
704 constants.HV_KVM_SPICE_LOSSLESS_IMG_COMPR:
705 hv_base.ParamInSet(
706 False, constants.HT_KVM_SPICE_VALID_LOSSLESS_IMG_COMPR_OPTIONS),
707 constants.HV_KVM_SPICE_JPEG_IMG_COMPR:
708 hv_base.ParamInSet(
709 False, constants.HT_KVM_SPICE_VALID_LOSSY_IMG_COMPR_OPTIONS),
710 constants.HV_KVM_SPICE_ZLIB_GLZ_IMG_COMPR:
711 hv_base.ParamInSet(
712 False, constants.HT_KVM_SPICE_VALID_LOSSY_IMG_COMPR_OPTIONS),
713 constants.HV_KVM_SPICE_STREAMING_VIDEO_DETECTION:
714 hv_base.ParamInSet(
715 False, constants.HT_KVM_SPICE_VALID_VIDEO_STREAM_DETECTION_OPTIONS),
716 constants.HV_KVM_SPICE_AUDIO_COMPR: hv_base.NO_CHECK,
717 constants.HV_KVM_SPICE_USE_TLS: hv_base.NO_CHECK,
718 constants.HV_KVM_SPICE_TLS_CIPHERS: hv_base.NO_CHECK,
719 constants.HV_KVM_SPICE_USE_VDAGENT: hv_base.NO_CHECK,
720 constants.HV_KVM_FLOPPY_IMAGE_PATH: hv_base.OPT_FILE_CHECK,
721 constants.HV_CDROM_IMAGE_PATH: hv_base.OPT_FILE_CHECK,
722 constants.HV_KVM_CDROM2_IMAGE_PATH: hv_base.OPT_FILE_CHECK,
723 constants.HV_BOOT_ORDER:
724 hv_base.ParamInSet(True, constants.HT_KVM_VALID_BO_TYPES),
725 constants.HV_NIC_TYPE:
726 hv_base.ParamInSet(True, constants.HT_KVM_VALID_NIC_TYPES),
727 constants.HV_DISK_TYPE:
728 hv_base.ParamInSet(True, constants.HT_KVM_VALID_DISK_TYPES),
729 constants.HV_KVM_CDROM_DISK_TYPE:
730 hv_base.ParamInSet(False, constants.HT_KVM_VALID_DISK_TYPES),
731 constants.HV_USB_MOUSE:
732 hv_base.ParamInSet(False, constants.HT_KVM_VALID_MOUSE_TYPES),
733 constants.HV_KEYMAP: hv_base.NO_CHECK,
734 constants.HV_MIGRATION_PORT: hv_base.REQ_NET_PORT_CHECK,
735 constants.HV_MIGRATION_BANDWIDTH: hv_base.REQ_NONNEGATIVE_INT_CHECK,
736 constants.HV_MIGRATION_DOWNTIME: hv_base.REQ_NONNEGATIVE_INT_CHECK,
737 constants.HV_MIGRATION_MODE: hv_base.MIGRATION_MODE_CHECK,
738 constants.HV_USE_LOCALTIME: hv_base.NO_CHECK,
739 constants.HV_DISK_CACHE:
740 hv_base.ParamInSet(True, constants.HT_VALID_CACHE_TYPES),
741 constants.HV_SECURITY_MODEL:
742 hv_base.ParamInSet(True, constants.HT_KVM_VALID_SM_TYPES),
743 constants.HV_SECURITY_DOMAIN: hv_base.NO_CHECK,
744 constants.HV_KVM_FLAG:
745 hv_base.ParamInSet(False, constants.HT_KVM_FLAG_VALUES),
746 constants.HV_VHOST_NET: hv_base.NO_CHECK,
747 constants.HV_KVM_USE_CHROOT: hv_base.NO_CHECK,
748 constants.HV_KVM_USER_SHUTDOWN: hv_base.NO_CHECK,
749 constants.HV_MEM_PATH: hv_base.OPT_DIR_CHECK,
750 constants.HV_REBOOT_BEHAVIOR:
751 hv_base.ParamInSet(True, constants.REBOOT_BEHAVIORS),
752 constants.HV_CPU_MASK: hv_base.OPT_MULTI_CPU_MASK_CHECK,
753 constants.HV_CPU_TYPE: hv_base.NO_CHECK,
754 constants.HV_CPU_CORES: hv_base.OPT_NONNEGATIVE_INT_CHECK,
755 constants.HV_CPU_THREADS: hv_base.OPT_NONNEGATIVE_INT_CHECK,
756 constants.HV_CPU_SOCKETS: hv_base.OPT_NONNEGATIVE_INT_CHECK,
757 constants.HV_SOUNDHW: hv_base.NO_CHECK,
758 constants.HV_USB_DEVICES: hv_base.NO_CHECK,
759 constants.HV_VGA: hv_base.NO_CHECK,
760 constants.HV_KVM_EXTRA: hv_base.NO_CHECK,
761 constants.HV_KVM_MACHINE_VERSION: hv_base.NO_CHECK,
762 constants.HV_VNET_HDR: hv_base.NO_CHECK,
763 }
764
765 _VIRTIO = "virtio"
766 _VIRTIO_NET_PCI = "virtio-net-pci"
767 _VIRTIO_BLK_PCI = "virtio-blk-pci"
768
769 _MIGRATION_STATUS_RE = re.compile(r"Migration\s+status:\s+(\w+)",
770 re.M | re.I)
771 _MIGRATION_PROGRESS_RE = \
772 re.compile(r"\s*transferred\s+ram:\s+(?P<transferred>\d+)\s+kbytes\s*\n"
773 r"\s*remaining\s+ram:\s+(?P<remaining>\d+)\s+kbytes\s*\n"
774 r"\s*total\s+ram:\s+(?P<total>\d+)\s+kbytes\s*\n", re.I)
775
776 _MIGRATION_INFO_MAX_BAD_ANSWERS = 5
777 _MIGRATION_INFO_RETRY_DELAY = 2
778
779 _VERSION_RE = re.compile(r"\b(\d+)\.(\d+)(\.(\d+))?\b")
780
781 _CPU_INFO_RE = re.compile(r"cpu\s+\#(\d+).*thread_id\s*=\s*(\d+)", re.I)
782 _CPU_INFO_CMD = "info cpus"
783 _CONT_CMD = "cont"
784
785 _DEFAULT_MACHINE_VERSION_RE = re.compile(r"^(\S+).*\(default\)", re.M)
786 _CHECK_MACHINE_VERSION_RE = \
787 staticmethod(lambda x: re.compile(r"^(%s)[ ]+.*PC" % x, re.M))
788
789 _QMP_RE = re.compile(r"^-qmp\s", re.M)
790 _SPICE_RE = re.compile(r"^-spice\s", re.M)
791 _VHOST_RE = re.compile(r"^-net\s.*,vhost=on|off", re.M)
792 _ENABLE_KVM_RE = re.compile(r"^-enable-kvm\s", re.M)
793 _DISABLE_KVM_RE = re.compile(r"^-disable-kvm\s", re.M)
794 _NETDEV_RE = re.compile(r"^-netdev\s", re.M)
795 _DISPLAY_RE = re.compile(r"^-display\s", re.M)
796 _MACHINE_RE = re.compile(r"^-machine\s", re.M)
797 _VIRTIO_NET_RE = re.compile(r"^name \"%s\"" % _VIRTIO_NET_PCI, re.M)
798 _VIRTIO_BLK_RE = re.compile(r"^name \"%s\"" % _VIRTIO_BLK_PCI, re.M)
799
800
801
802 _BOOT_RE = re.compile(r"^-drive\s([^-]|(?<!^)-)*,boot=on\|off", re.M | re.S)
803 _UUID_RE = re.compile(r"^-uuid\s", re.M)
804
805 _INFO_PCI_RE = re.compile(r'Bus.*device[ ]*(\d+).*')
806 _INFO_PCI_CMD = "info pci"
807 _FIND_PCI_DEVICE_RE = \
808 staticmethod(
809 lambda pci, devid: re.compile(r'Bus.*device[ ]*%d,(.*\n){5,6}.*id "%s"' %
810 (pci, devid), re.M))
811
812 _INFO_VERSION_RE = \
813 re.compile(r'^QEMU (\d+)\.(\d+)(\.(\d+))?.*monitor.*', re.M)
814 _INFO_VERSION_CMD = "info version"
815
816
817 _DEFAULT_PCI_RESERVATIONS = "11100000000000000000000000000000"
818 _SOUNDHW_WITH_PCI_SLOT = ["ac97", "es1370", "hda"]
819
820 ANCILLARY_FILES = [
821 _KVM_NETWORK_SCRIPT,
822 ]
823 ANCILLARY_FILES_OPT = [
824 _KVM_NETWORK_SCRIPT,
825 ]
826
827
828 _KVMOPT_HELP = "help"
829 _KVMOPT_MLIST = "mlist"
830 _KVMOPT_DEVICELIST = "devicelist"
831
832
833
834 _KVMOPTS_CMDS = {
835 _KVMOPT_HELP: (["--help"], False),
836 _KVMOPT_MLIST: (["-M", "?"], False),
837 _KVMOPT_DEVICELIST: (["-device", "?"], True),
838 }
839
846
847 @classmethod
849 """Returns the instance pidfile.
850
851 """
852 return utils.PathJoin(cls._PIDS_DIR, instance_name)
853
854 @classmethod
856 """Returns the instance uidfile.
857
858 """
859 return utils.PathJoin(cls._UIDS_DIR, instance_name)
860
861 @classmethod
863 """Check pid file for instance information.
864
865 Check that a pid file is associated with an instance, and retrieve
866 information from its command line.
867
868 @type pid: string or int
869 @param pid: process id of the instance to check
870 @rtype: tuple
871 @return: (instance_name, memory, vcpus)
872 @raise errors.HypervisorError: when an instance cannot be found
873
874 """
875 alive = utils.IsProcessAlive(pid)
876 if not alive:
877 raise errors.HypervisorError("Cannot get info for pid %s" % pid)
878
879 cmdline_file = utils.PathJoin("/proc", str(pid), "cmdline")
880 try:
881 cmdline = utils.ReadFile(cmdline_file)
882 except EnvironmentError, err:
883 raise errors.HypervisorError("Can't open cmdline file for pid %s: %s" %
884 (pid, err))
885
886 instance = None
887 memory = 0
888 vcpus = 0
889
890 arg_list = cmdline.split("\x00")
891 while arg_list:
892 arg = arg_list.pop(0)
893 if arg == "-name":
894 instance = arg_list.pop(0)
895 elif arg == "-m":
896 memory = int(arg_list.pop(0))
897 elif arg == "-smp":
898 vcpus = int(arg_list.pop(0).split(",")[0])
899
900 if instance is None:
901 raise errors.HypervisorError("Pid %s doesn't contain a ganeti kvm"
902 " instance" % pid)
903
904 return (instance, memory, vcpus)
905
906 @classmethod
908 """Returns the instance pidfile, pid, and liveness.
909
910 @type instance_name: string
911 @param instance_name: instance name
912 @rtype: tuple
913 @return: (pid file name, pid, liveness)
914
915 """
916 pidfile = cls._InstancePidFile(instance_name)
917 pid = utils.ReadPidFile(pidfile)
918
919 alive = False
920 try:
921 cmd_instance = cls._InstancePidInfo(pid)[0]
922 alive = (cmd_instance == instance_name)
923 except errors.HypervisorError:
924 pass
925
926 return (pidfile, pid, alive)
927
928 @classmethod
930 """Raises an error unless the given instance is down.
931
932 """
933 alive = cls._InstancePidAlive(instance_name)[2]
934 if alive:
935 raise errors.HypervisorError("Failed to start instance %s: %s" %
936 (instance_name, "already running"))
937
938 @classmethod
940 """Returns the instance monitor socket name
941
942 """
943 return utils.PathJoin(cls._CTRL_DIR, "%s.monitor" % instance_name)
944
945 @classmethod
947 """Returns the instance serial socket name
948
949 """
950 return utils.PathJoin(cls._CTRL_DIR, "%s.serial" % instance_name)
951
952 @classmethod
954 """Returns the instance serial QMP socket name
955
956 """
957 return utils.PathJoin(cls._CTRL_DIR, "%s.qmp" % instance_name)
958
959 @classmethod
961 """Returns the instance kvm daemon socket name
962
963 """
964 return utils.PathJoin(cls._CTRL_DIR, "%s.kvmd" % instance_name)
965
966 @classmethod
968 """Returns the instance QMP output filename
969
970 """
971 return utils.PathJoin(cls._CTRL_DIR, "%s.shutdown" % instance_name)
972
973 @staticmethod
975 """Returns the correct parameters for socat
976
977 If we have a new-enough socat we can use raw mode with an escape character.
978
979 """
980 if constants.SOCAT_USE_ESCAPE:
981 return "raw,echo=0,escape=%s" % constants.SOCAT_ESCAPE_CODE
982 else:
983 return "echo=0,icanon=0"
984
985 @classmethod
987 """Returns the instance KVM runtime filename
988
989 """
990 return utils.PathJoin(cls._CONF_DIR, "%s.runtime" % instance_name)
991
992 @classmethod
994 """Returns the name of the KVM chroot dir of the instance
995
996 """
997 return utils.PathJoin(cls._CHROOT_DIR, instance_name)
998
999 @classmethod
1001 """Returns the name of the directory holding the tap device files for a
1002 given instance.
1003
1004 """
1005 return utils.PathJoin(cls._NICS_DIR, instance_name)
1006
1007 @classmethod
1009 """Returns the name of the file containing the tap device for a given NIC
1010
1011 """
1012 return utils.PathJoin(cls._InstanceNICDir(instance_name), str(seq))
1013
1014 @classmethod
1016 """Returns the name of the file containing the keymap for a given instance
1017
1018 """
1019 return utils.PathJoin(cls._KEYMAP_DIR, instance_name)
1020
1021 @classmethod
1023 """Try to read a uid file
1024
1025 """
1026 if os.path.exists(uid_file):
1027 try:
1028 uid = int(utils.ReadOneLineFile(uid_file))
1029 return uid
1030 except EnvironmentError:
1031 logging.warning("Can't read uid file", exc_info=True)
1032 except (TypeError, ValueError):
1033 logging.warning("Can't parse uid file contents", exc_info=True)
1034 return None
1035
1036 @classmethod
1038 """Removes an instance's rutime sockets/files/dirs.
1039
1040 """
1041 utils.RemoveFile(pidfile)
1042 utils.RemoveFile(cls._InstanceMonitor(instance_name))
1043 utils.RemoveFile(cls._InstanceSerial(instance_name))
1044 utils.RemoveFile(cls._InstanceQmpMonitor(instance_name))
1045 utils.RemoveFile(cls._InstanceKVMRuntime(instance_name))
1046 utils.RemoveFile(cls._InstanceKeymapFile(instance_name))
1047 uid_file = cls._InstanceUidFile(instance_name)
1048 uid = cls._TryReadUidFile(uid_file)
1049 utils.RemoveFile(uid_file)
1050 if uid is not None:
1051 uidpool.ReleaseUid(uid)
1052 try:
1053 shutil.rmtree(cls._InstanceNICDir(instance_name))
1054 except OSError, err:
1055 if err.errno != errno.ENOENT:
1056 raise
1057 try:
1058 chroot_dir = cls._InstanceChrootDir(instance_name)
1059 utils.RemoveDir(chroot_dir)
1060 except OSError, err:
1061 if err.errno == errno.ENOTEMPTY:
1062
1063 new_chroot_dir = tempfile.mkdtemp(dir=cls._CHROOT_QUARANTINE_DIR,
1064 prefix="%s-%s-" %
1065 (instance_name,
1066 utils.TimestampForFilename()))
1067 logging.warning("The chroot directory of instance %s can not be"
1068 " removed as it is not empty. Moving it to the"
1069 " quarantine instead. Please investigate the"
1070 " contents (%s) and clean up manually",
1071 instance_name, new_chroot_dir)
1072 utils.RenameFile(chroot_dir, new_chroot_dir)
1073 else:
1074 raise
1075
1076 @staticmethod
1125
1126 @staticmethod
1131
1132 @staticmethod
1134 """Create a CPU mask suitable for sched_setaffinity from a list of
1135 CPUs.
1136
1137 See man taskset for more info on sched_setaffinity masks.
1138 For example: [ 0, 2, 5, 6 ] will return 101 (0x65, 0..01100101).
1139
1140 @type cpu_list: list of int
1141 @param cpu_list: list of physical CPU numbers to map to vCPUs in order
1142 @rtype: int
1143 @return: a bit mask of CPU affinities
1144
1145 """
1146 if cpu_list == constants.CPU_PINNING_OFF:
1147 return constants.CPU_PINNING_ALL_KVM
1148 else:
1149 return sum(2 ** cpu for cpu in cpu_list)
1150
1151 @classmethod
1153 """Change CPU affinity for running VM according to given CPU mask.
1154
1155 @param cpu_mask: CPU mask as given by the user. e.g. "0-2,4:all:1,3"
1156 @type cpu_mask: string
1157 @param process_id: process ID of KVM process. Used to pin entire VM
1158 to physical CPUs.
1159 @type process_id: int
1160 @param thread_dict: map of virtual CPUs to KVM thread IDs
1161 @type thread_dict: dict int:int
1162
1163 """
1164
1165 cpu_list = utils.ParseMultiCpuMask(cpu_mask)
1166
1167 if len(cpu_list) == 1:
1168 all_cpu_mapping = cpu_list[0]
1169 if all_cpu_mapping == constants.CPU_PINNING_OFF:
1170
1171 pass
1172 else:
1173
1174
1175 cls._VerifyAffinityPackage()
1176 affinity.set_process_affinity_mask(
1177 process_id, cls._BuildAffinityCpuMask(all_cpu_mapping))
1178 else:
1179
1180
1181
1182 assert len(thread_dict) == len(cpu_list)
1183 cls._VerifyAffinityPackage()
1184
1185
1186 for vcpu, i in zip(cpu_list, range(len(cpu_list))):
1187 affinity.set_process_affinity_mask(thread_dict[i],
1188 cls._BuildAffinityCpuMask(vcpu))
1189
1191 """Get a mapping of vCPU no. to thread IDs for the instance
1192
1193 @type instance_name: string
1194 @param instance_name: instance in question
1195 @rtype: dictionary of int:int
1196 @return: a dictionary mapping vCPU numbers to thread IDs
1197
1198 """
1199 result = {}
1200 output = self._CallMonitorCommand(instance_name, self._CPU_INFO_CMD)
1201 for line in output.stdout.splitlines():
1202 match = self._CPU_INFO_RE.search(line)
1203 if not match:
1204 continue
1205 grp = map(int, match.groups())
1206 result[grp[0]] = grp[1]
1207
1208 return result
1209
1211 """Complete CPU pinning.
1212
1213 @type instance_name: string
1214 @param instance_name: name of instance
1215 @type cpu_mask: string
1216 @param cpu_mask: CPU pinning mask as entered by user
1217
1218 """
1219
1220 _, pid, _ = self._InstancePidAlive(instance_name)
1221
1222 thread_dict = self._GetVcpuThreadIds(instance_name)
1223
1224 self._AssignCpuAffinity(cpu_mask, pid, thread_dict)
1225
1227 """Get the list of running instances.
1228
1229 We can do this by listing our live instances directory and
1230 checking whether the associated kvm process is still alive.
1231
1232 """
1233 result = []
1234 for name in os.listdir(self._PIDS_DIR):
1235 if self._InstancePidAlive(name)[2]:
1236 result.append(name)
1237 return result
1238
1239 @classmethod
1242
1243 @classmethod
1246
1248 """Get instance properties.
1249
1250 @type instance_name: string
1251 @param instance_name: the instance name
1252 @type hvparams: dict of strings
1253 @param hvparams: hypervisor parameters to be used with this instance
1254 @rtype: tuple of strings
1255 @return: (name, id, memory, vcpus, stat, times)
1256
1257 """
1258 _, pid, alive = self._InstancePidAlive(instance_name)
1259 if not alive:
1260 if self._IsUserShutdown(instance_name):
1261 return (instance_name, -1, 0, 0, hv_base.HvInstanceState.SHUTDOWN, 0)
1262 else:
1263 return None
1264
1265 _, memory, vcpus = self._InstancePidInfo(pid)
1266 istat = hv_base.HvInstanceState.RUNNING
1267 times = 0
1268
1269 try:
1270 qmp = QmpConnection(self._InstanceQmpMonitor(instance_name))
1271 qmp.connect()
1272 vcpus = len(qmp.Execute("query-cpus")[qmp.RETURN_KEY])
1273
1274
1275 mem_bytes = qmp.Execute("query-balloon")[qmp.RETURN_KEY][qmp.ACTUAL_KEY]
1276 memory = mem_bytes / 1048576
1277 except errors.HypervisorError:
1278 pass
1279
1280 return (instance_name, pid, memory, vcpus, istat, times)
1281
1283 """Get properties of all instances.
1284
1285 @type hvparams: dict of strings
1286 @param hvparams: hypervisor parameters
1287 @return: list of tuples (name, id, memory, vcpus, stat, times)
1288
1289 """
1290 data = []
1291 for name in os.listdir(self._PIDS_DIR):
1292 try:
1293 info = self.GetInstanceInfo(name)
1294 except errors.HypervisorError:
1295
1296 continue
1297 if info:
1298 data.append(info)
1299 return data
1300
1303 """Generate KVM options regarding instance's block devices.
1304
1305 @type instance: L{objects.Instance}
1306 @param instance: the instance object
1307 @type up_hvp: dict
1308 @param up_hvp: the instance's runtime hypervisor parameters
1309 @type kvm_disks: list of tuples
1310 @param kvm_disks: list of tuples [(disk, link_name, uri)..]
1311 @type kvmhelp: string
1312 @param kvmhelp: output of kvm --help
1313 @type devlist: string
1314 @param devlist: output of kvm -device ?
1315 @rtype: list
1316 @return: list of command line options eventually used by kvm executable
1317
1318 """
1319 kernel_path = up_hvp[constants.HV_KERNEL_PATH]
1320 if kernel_path:
1321 boot_disk = False
1322 else:
1323 boot_disk = up_hvp[constants.HV_BOOT_ORDER] == constants.HT_BO_DISK
1324
1325
1326
1327 needs_boot_flag = self._BOOT_RE.search(kvmhelp)
1328
1329 dev_opts = []
1330 device_driver = None
1331 disk_type = up_hvp[constants.HV_DISK_TYPE]
1332 if disk_type == constants.HT_DISK_PARAVIRTUAL:
1333 if_val = ",if=%s" % self._VIRTIO
1334 try:
1335 if self._VIRTIO_BLK_RE.search(devlist):
1336 if_val = ",if=none"
1337
1338 device_driver = self._VIRTIO_BLK_PCI
1339 except errors.HypervisorError, _:
1340 pass
1341 else:
1342 if_val = ",if=%s" % disk_type
1343
1344 disk_cache = up_hvp[constants.HV_DISK_CACHE]
1345 if instance.disk_template in constants.DTS_EXT_MIRROR:
1346 if disk_cache != "none":
1347
1348 logging.warning("KVM: overriding disk_cache setting '%s' with 'none'"
1349 " to prevent shared storage corruption on migration",
1350 disk_cache)
1351 cache_val = ",cache=none"
1352 elif disk_cache != constants.HT_CACHE_DEFAULT:
1353 cache_val = ",cache=%s" % disk_cache
1354 else:
1355 cache_val = ""
1356 for cfdev, link_name, uri in kvm_disks:
1357 if cfdev.mode != constants.DISK_RDWR:
1358 raise errors.HypervisorError("Instance has read-only disks which"
1359 " are not supported by KVM")
1360
1361 boot_val = ""
1362 if boot_disk:
1363 dev_opts.extend(["-boot", "c"])
1364 boot_disk = False
1365 if needs_boot_flag and disk_type != constants.HT_DISK_IDE:
1366 boot_val = ",boot=on"
1367
1368 drive_uri = _GetDriveURI(cfdev, link_name, uri)
1369
1370 drive_val = "file=%s,format=raw%s%s%s" % \
1371 (drive_uri, if_val, boot_val, cache_val)
1372
1373 if device_driver:
1374
1375
1376
1377 kvm_devid = _GenerateDeviceKVMId(constants.HOTPLUG_TARGET_DISK, cfdev)
1378 drive_val += (",id=%s" % kvm_devid)
1379 drive_val += (",bus=0,unit=%d" % cfdev.pci)
1380 dev_val = ("%s,drive=%s,id=%s" %
1381 (device_driver, kvm_devid, kvm_devid))
1382 dev_val += ",bus=pci.0,addr=%s" % hex(cfdev.pci)
1383 dev_opts.extend(["-device", dev_val])
1384
1385 dev_opts.extend(["-drive", drive_val])
1386
1387 return dev_opts
1388
1391 """Generate KVM information to start an instance.
1392
1393 @type kvmhelp: string
1394 @param kvmhelp: output of kvm --help
1395 @attention: this function must not have any side-effects; for
1396 example, it must not write to the filesystem, or read values
1397 from the current system the are expected to differ between
1398 nodes, since it is only run once at instance startup;
1399 actions/kvm arguments that can vary between systems should be
1400 done in L{_ExecuteKVMRuntime}
1401
1402 """
1403
1404 hvp = instance.hvparams
1405 self.ValidateParameters(hvp)
1406
1407 pidfile = self._InstancePidFile(instance.name)
1408 kvm = hvp[constants.HV_KVM_PATH]
1409 kvm_cmd = [kvm]
1410
1411 kvm_cmd.extend(["-name", instance.name])
1412 kvm_cmd.extend(["-m", instance.beparams[constants.BE_MAXMEM]])
1413
1414 smp_list = ["%s" % instance.beparams[constants.BE_VCPUS]]
1415 if hvp[constants.HV_CPU_CORES]:
1416 smp_list.append("cores=%s" % hvp[constants.HV_CPU_CORES])
1417 if hvp[constants.HV_CPU_THREADS]:
1418 smp_list.append("threads=%s" % hvp[constants.HV_CPU_THREADS])
1419 if hvp[constants.HV_CPU_SOCKETS]:
1420 smp_list.append("sockets=%s" % hvp[constants.HV_CPU_SOCKETS])
1421
1422 kvm_cmd.extend(["-smp", ",".join(smp_list)])
1423
1424 kvm_cmd.extend(["-pidfile", pidfile])
1425
1426 pci_reservations = bitarray(self._DEFAULT_PCI_RESERVATIONS)
1427
1428
1429 if hvp[constants.HV_SOUNDHW]:
1430 soundhw = hvp[constants.HV_SOUNDHW]
1431
1432
1433
1434 if soundhw in self._SOUNDHW_WITH_PCI_SLOT:
1435 _ = _GetFreeSlot(pci_reservations, reserve=True)
1436 kvm_cmd.extend(["-soundhw", soundhw])
1437
1438 if hvp[constants.HV_DISK_TYPE] == constants.HT_DISK_SCSI:
1439
1440 _ = _GetFreeSlot(pci_reservations, reserve=True)
1441
1442
1443 addr = _GetFreeSlot(pci_reservations, reserve=True)
1444 pci_info = ",bus=pci.0,addr=%s" % hex(addr)
1445 kvm_cmd.extend(["-balloon", "virtio,id=balloon%s" % pci_info])
1446 kvm_cmd.extend(["-daemonize"])
1447 if not instance.hvparams[constants.HV_ACPI]:
1448 kvm_cmd.extend(["-no-acpi"])
1449 if instance.hvparams[constants.HV_REBOOT_BEHAVIOR] == \
1450 constants.INSTANCE_REBOOT_EXIT:
1451 kvm_cmd.extend(["-no-reboot"])
1452
1453 mversion = hvp[constants.HV_KVM_MACHINE_VERSION]
1454 if not mversion:
1455 mversion = self._GetDefaultMachineVersion(kvm)
1456 if self._MACHINE_RE.search(kvmhelp):
1457
1458
1459
1460 if (hvp[constants.HV_KVM_FLAG] == constants.HT_KVM_ENABLED):
1461 specprop = ",accel=kvm"
1462 else:
1463 specprop = ""
1464 machinespec = "%s%s" % (mversion, specprop)
1465 kvm_cmd.extend(["-machine", machinespec])
1466 else:
1467 kvm_cmd.extend(["-M", mversion])
1468 if (hvp[constants.HV_KVM_FLAG] == constants.HT_KVM_ENABLED and
1469 self._ENABLE_KVM_RE.search(kvmhelp)):
1470 kvm_cmd.extend(["-enable-kvm"])
1471 elif (hvp[constants.HV_KVM_FLAG] == constants.HT_KVM_DISABLED and
1472 self._DISABLE_KVM_RE.search(kvmhelp)):
1473 kvm_cmd.extend(["-disable-kvm"])
1474
1475 kernel_path = hvp[constants.HV_KERNEL_PATH]
1476 if kernel_path:
1477 boot_cdrom = boot_floppy = boot_network = False
1478 else:
1479 boot_cdrom = hvp[constants.HV_BOOT_ORDER] == constants.HT_BO_CDROM
1480 boot_floppy = hvp[constants.HV_BOOT_ORDER] == constants.HT_BO_FLOPPY
1481 boot_network = hvp[constants.HV_BOOT_ORDER] == constants.HT_BO_NETWORK
1482
1483 if startup_paused:
1484 kvm_cmd.extend([_KVM_START_PAUSED_FLAG])
1485
1486 if boot_network:
1487 kvm_cmd.extend(["-boot", "n"])
1488
1489
1490
1491 needs_boot_flag = self._BOOT_RE.search(kvmhelp)
1492
1493 disk_type = hvp[constants.HV_DISK_TYPE]
1494
1495
1496 cdrom_disk_type = hvp[constants.HV_KVM_CDROM_DISK_TYPE]
1497 if not cdrom_disk_type:
1498 cdrom_disk_type = disk_type
1499
1500 iso_image = hvp[constants.HV_CDROM_IMAGE_PATH]
1501 if iso_image:
1502 options = ",format=raw,media=cdrom"
1503
1504 if boot_cdrom:
1505 actual_cdrom_type = constants.HT_DISK_IDE
1506 elif cdrom_disk_type == constants.HT_DISK_PARAVIRTUAL:
1507 actual_cdrom_type = "virtio"
1508 else:
1509 actual_cdrom_type = cdrom_disk_type
1510 if_val = ",if=%s" % actual_cdrom_type
1511
1512 boot_val = ""
1513 if boot_cdrom:
1514 kvm_cmd.extend(["-boot", "d"])
1515 if needs_boot_flag:
1516 boot_val = ",boot=on"
1517
1518 drive_val = "file=%s%s%s%s" % (iso_image, options, if_val, boot_val)
1519 kvm_cmd.extend(["-drive", drive_val])
1520
1521 iso_image2 = hvp[constants.HV_KVM_CDROM2_IMAGE_PATH]
1522 if iso_image2:
1523 options = ",format=raw,media=cdrom"
1524 if cdrom_disk_type == constants.HT_DISK_PARAVIRTUAL:
1525 if_val = ",if=virtio"
1526 else:
1527 if_val = ",if=%s" % cdrom_disk_type
1528 drive_val = "file=%s%s%s" % (iso_image2, options, if_val)
1529 kvm_cmd.extend(["-drive", drive_val])
1530
1531 floppy_image = hvp[constants.HV_KVM_FLOPPY_IMAGE_PATH]
1532 if floppy_image:
1533 options = ",format=raw,media=disk"
1534 if boot_floppy:
1535 kvm_cmd.extend(["-boot", "a"])
1536 options = "%s,boot=on" % options
1537 if_val = ",if=floppy"
1538 options = "%s%s" % (options, if_val)
1539 drive_val = "file=%s%s" % (floppy_image, options)
1540 kvm_cmd.extend(["-drive", drive_val])
1541
1542 if kernel_path:
1543 kvm_cmd.extend(["-kernel", kernel_path])
1544 initrd_path = hvp[constants.HV_INITRD_PATH]
1545 if initrd_path:
1546 kvm_cmd.extend(["-initrd", initrd_path])
1547 root_append = ["root=%s" % hvp[constants.HV_ROOT_PATH],
1548 hvp[constants.HV_KERNEL_ARGS]]
1549 if hvp[constants.HV_SERIAL_CONSOLE]:
1550 serial_speed = hvp[constants.HV_SERIAL_SPEED]
1551 root_append.append("console=ttyS0,%s" % serial_speed)
1552 kvm_cmd.extend(["-append", " ".join(root_append)])
1553
1554 mem_path = hvp[constants.HV_MEM_PATH]
1555 if mem_path:
1556 kvm_cmd.extend(["-mem-path", mem_path, "-mem-prealloc"])
1557
1558 monitor_dev = ("unix:%s,server,nowait" %
1559 self._InstanceMonitor(instance.name))
1560 kvm_cmd.extend(["-monitor", monitor_dev])
1561 if hvp[constants.HV_SERIAL_CONSOLE]:
1562 serial_dev = ("unix:%s,server,nowait" %
1563 self._InstanceSerial(instance.name))
1564 kvm_cmd.extend(["-serial", serial_dev])
1565 else:
1566 kvm_cmd.extend(["-serial", "none"])
1567
1568 mouse_type = hvp[constants.HV_USB_MOUSE]
1569 vnc_bind_address = hvp[constants.HV_VNC_BIND_ADDRESS]
1570 spice_bind = hvp[constants.HV_KVM_SPICE_BIND]
1571 spice_ip_version = None
1572
1573 kvm_cmd.extend(["-usb"])
1574
1575 if mouse_type:
1576 kvm_cmd.extend(["-usbdevice", mouse_type])
1577 elif vnc_bind_address:
1578 kvm_cmd.extend(["-usbdevice", constants.HT_MOUSE_TABLET])
1579
1580 if vnc_bind_address:
1581 if netutils.IsValidInterface(vnc_bind_address):
1582 if_addresses = netutils.GetInterfaceIpAddresses(vnc_bind_address)
1583 if_ip4_addresses = if_addresses[constants.IP4_VERSION]
1584 if len(if_ip4_addresses) < 1:
1585 logging.error("Could not determine IPv4 address of interface %s",
1586 vnc_bind_address)
1587 else:
1588 vnc_bind_address = if_ip4_addresses[0]
1589 if netutils.IP4Address.IsValid(vnc_bind_address):
1590 if instance.network_port > constants.VNC_BASE_PORT:
1591 display = instance.network_port - constants.VNC_BASE_PORT
1592 if vnc_bind_address == constants.IP4_ADDRESS_ANY:
1593 vnc_arg = ":%d" % (display)
1594 else:
1595 vnc_arg = "%s:%d" % (vnc_bind_address, display)
1596 else:
1597 logging.error("Network port is not a valid VNC display (%d < %d),"
1598 " not starting VNC",
1599 instance.network_port, constants.VNC_BASE_PORT)
1600 vnc_arg = "none"
1601
1602
1603
1604 vnc_append = ""
1605 if hvp[constants.HV_VNC_TLS]:
1606 vnc_append = "%s,tls" % vnc_append
1607 if hvp[constants.HV_VNC_X509_VERIFY]:
1608 vnc_append = "%s,x509verify=%s" % (vnc_append,
1609 hvp[constants.HV_VNC_X509])
1610 elif hvp[constants.HV_VNC_X509]:
1611 vnc_append = "%s,x509=%s" % (vnc_append,
1612 hvp[constants.HV_VNC_X509])
1613 if hvp[constants.HV_VNC_PASSWORD_FILE]:
1614 vnc_append = "%s,password" % vnc_append
1615
1616 vnc_arg = "%s%s" % (vnc_arg, vnc_append)
1617
1618 else:
1619 vnc_arg = "unix:%s/%s.vnc" % (vnc_bind_address, instance.name)
1620
1621 kvm_cmd.extend(["-vnc", vnc_arg])
1622 elif spice_bind:
1623
1624
1625 if netutils.IsValidInterface(spice_bind):
1626
1627
1628 addresses = netutils.GetInterfaceIpAddresses(spice_bind)
1629 spice_ip_version = hvp[constants.HV_KVM_SPICE_IP_VERSION]
1630
1631
1632
1633 if spice_ip_version != constants.IFACE_NO_IP_VERSION_SPECIFIED:
1634 if not addresses[spice_ip_version]:
1635 raise errors.HypervisorError("SPICE: Unable to get an IPv%s address"
1636 " for %s" % (spice_ip_version,
1637 spice_bind))
1638
1639
1640 elif (addresses[constants.IP4_VERSION] and
1641 addresses[constants.IP6_VERSION]):
1642
1643
1644 cluster_family = ssconf.SimpleStore().GetPrimaryIPFamily()
1645 spice_ip_version = \
1646 netutils.IPAddress.GetVersionFromAddressFamily(cluster_family)
1647 elif addresses[constants.IP4_VERSION]:
1648 spice_ip_version = constants.IP4_VERSION
1649 elif addresses[constants.IP6_VERSION]:
1650 spice_ip_version = constants.IP6_VERSION
1651 else:
1652 raise errors.HypervisorError("SPICE: Unable to get an IP address"
1653 " for %s" % (spice_bind))
1654
1655 spice_address = addresses[spice_ip_version][0]
1656
1657 else:
1658
1659
1660 spice_address = spice_bind
1661
1662 spice_arg = "addr=%s" % spice_address
1663 if hvp[constants.HV_KVM_SPICE_USE_TLS]:
1664 spice_arg = ("%s,tls-port=%s,x509-cacert-file=%s" %
1665 (spice_arg, instance.network_port,
1666 pathutils.SPICE_CACERT_FILE))
1667 spice_arg = ("%s,x509-key-file=%s,x509-cert-file=%s" %
1668 (spice_arg, pathutils.SPICE_CERT_FILE,
1669 pathutils.SPICE_CERT_FILE))
1670 tls_ciphers = hvp[constants.HV_KVM_SPICE_TLS_CIPHERS]
1671 if tls_ciphers:
1672 spice_arg = "%s,tls-ciphers=%s" % (spice_arg, tls_ciphers)
1673 else:
1674 spice_arg = "%s,port=%s" % (spice_arg, instance.network_port)
1675
1676 if not hvp[constants.HV_KVM_SPICE_PASSWORD_FILE]:
1677 spice_arg = "%s,disable-ticketing" % spice_arg
1678
1679 if spice_ip_version:
1680 spice_arg = "%s,ipv%s" % (spice_arg, spice_ip_version)
1681
1682
1683 img_lossless = hvp[constants.HV_KVM_SPICE_LOSSLESS_IMG_COMPR]
1684 img_jpeg = hvp[constants.HV_KVM_SPICE_JPEG_IMG_COMPR]
1685 img_zlib_glz = hvp[constants.HV_KVM_SPICE_ZLIB_GLZ_IMG_COMPR]
1686 if img_lossless:
1687 spice_arg = "%s,image-compression=%s" % (spice_arg, img_lossless)
1688 if img_jpeg:
1689 spice_arg = "%s,jpeg-wan-compression=%s" % (spice_arg, img_jpeg)
1690 if img_zlib_glz:
1691 spice_arg = "%s,zlib-glz-wan-compression=%s" % (spice_arg, img_zlib_glz)
1692
1693
1694 video_streaming = hvp[constants.HV_KVM_SPICE_STREAMING_VIDEO_DETECTION]
1695 if video_streaming:
1696 spice_arg = "%s,streaming-video=%s" % (spice_arg, video_streaming)
1697
1698
1699 if not hvp[constants.HV_KVM_SPICE_AUDIO_COMPR]:
1700 spice_arg = "%s,playback-compression=off" % spice_arg
1701 if not hvp[constants.HV_KVM_SPICE_USE_VDAGENT]:
1702 spice_arg = "%s,agent-mouse=off" % spice_arg
1703 else:
1704
1705
1706 addr = _GetFreeSlot(pci_reservations, reserve=True)
1707 pci_info = ",bus=pci.0,addr=%s" % hex(addr)
1708 kvm_cmd.extend(["-device", "virtio-serial-pci,id=spice%s" % pci_info])
1709 kvm_cmd.extend([
1710 "-device",
1711 "virtserialport,chardev=spicechannel0,name=com.redhat.spice.0",
1712 ])
1713 kvm_cmd.extend(["-chardev", "spicevmc,id=spicechannel0,name=vdagent"])
1714
1715 logging.info("KVM: SPICE will listen on port %s", instance.network_port)
1716 kvm_cmd.extend(["-spice", spice_arg])
1717
1718 else:
1719
1720
1721 if self._DISPLAY_RE.search(kvmhelp):
1722 kvm_cmd.extend(["-display", "none"])
1723 else:
1724 kvm_cmd.extend(["-nographic"])
1725
1726 if hvp[constants.HV_USE_LOCALTIME]:
1727 kvm_cmd.extend(["-localtime"])
1728
1729 if hvp[constants.HV_KVM_USE_CHROOT]:
1730 kvm_cmd.extend(["-chroot", self._InstanceChrootDir(instance.name)])
1731
1732
1733 if hvp[constants.HV_CPU_TYPE]:
1734 kvm_cmd.extend(["-cpu", hvp[constants.HV_CPU_TYPE]])
1735
1736
1737
1738 if hvp[constants.HV_VGA]:
1739 kvm_cmd.extend(["-vga", hvp[constants.HV_VGA]])
1740 elif spice_bind:
1741 kvm_cmd.extend(["-vga", "qxl"])
1742
1743
1744 if hvp[constants.HV_USB_DEVICES]:
1745 for dev in hvp[constants.HV_USB_DEVICES].split(","):
1746 kvm_cmd.extend(["-usbdevice", dev])
1747
1748
1749 if self._UUID_RE.search(kvmhelp):
1750 kvm_cmd.extend(["-uuid", instance.uuid])
1751
1752 if hvp[constants.HV_KVM_EXTRA]:
1753 kvm_cmd.extend(hvp[constants.HV_KVM_EXTRA].split(" "))
1754
1755 kvm_disks = []
1756 for disk, link_name, uri in block_devices:
1757 disk.pci = _GetFreeSlot(pci_reservations, disk.pci, True)
1758 kvm_disks.append((disk, link_name, uri))
1759
1760 kvm_nics = []
1761 for nic in instance.nics:
1762 nic.pci = _GetFreeSlot(pci_reservations, nic.pci, True)
1763 kvm_nics.append(nic)
1764
1765 hvparams = hvp
1766
1767 return (kvm_cmd, kvm_nics, hvparams, kvm_disks)
1768
1778
1780 """Read an instance's KVM runtime
1781
1782 """
1783 try:
1784 file_content = utils.ReadFile(self._InstanceKVMRuntime(instance_name))
1785 except EnvironmentError, err:
1786 raise errors.HypervisorError("Failed to load KVM runtime file: %s" % err)
1787 return file_content
1788
1790 """Save an instance's KVM runtime
1791
1792 """
1793 kvm_cmd, kvm_nics, hvparams, kvm_disks = kvm_runtime
1794
1795 serialized_nics = [nic.ToDict() for nic in kvm_nics]
1796 serialized_disks = [(blk.ToDict(), link, uri)
1797 for blk, link, uri in kvm_disks]
1798 serialized_form = serializer.Dump((kvm_cmd, serialized_nics, hvparams,
1799 serialized_disks))
1800
1801 self._WriteKVMRuntime(instance.name, serialized_form)
1802
1804 """Load an instance's KVM runtime
1805
1806 """
1807 if not serialized_runtime:
1808 serialized_runtime = self._ReadKVMRuntime(instance.name)
1809
1810 return _AnalyzeSerializedRuntime(serialized_runtime)
1811
1812 - def _RunKVMCmd(self, name, kvm_cmd, tap_fds=None):
1813 """Run the KVM cmd and check for errors
1814
1815 @type name: string
1816 @param name: instance name
1817 @type kvm_cmd: list of strings
1818 @param kvm_cmd: runcmd input for kvm
1819 @type tap_fds: list of int
1820 @param tap_fds: fds of tap devices opened by Ganeti
1821
1822 """
1823 try:
1824 result = utils.RunCmd(kvm_cmd, noclose_fds=tap_fds)
1825 finally:
1826 for fd in tap_fds:
1827 utils_wrapper.CloseFdNoError(fd)
1828
1829 if result.failed:
1830 raise errors.HypervisorError("Failed to start instance %s: %s (%s)" %
1831 (name, result.fail_reason, result.output))
1832 if not self._InstancePidAlive(name)[2]:
1833 raise errors.HypervisorError("Failed to start instance %s" % name)
1834
1835
1836
1838 """Execute a KVM cmd, after completing it with some last minute data.
1839
1840 @type incoming: tuple of strings
1841 @param incoming: (target_host_ip, port)
1842 @type kvmhelp: string
1843 @param kvmhelp: output of kvm --help
1844
1845 """
1846
1847
1848
1849
1850
1851
1852
1853
1854 conf_hvp = instance.hvparams
1855 name = instance.name
1856 self._CheckDown(name)
1857
1858 self._ClearUserShutdown(instance.name)
1859 self._StartKvmd(instance.hvparams)
1860
1861 temp_files = []
1862
1863 kvm_cmd, kvm_nics, up_hvp, kvm_disks = kvm_runtime
1864
1865 kvm_path = kvm_cmd[0]
1866 up_hvp = objects.FillDict(conf_hvp, up_hvp)
1867
1868
1869
1870 security_model = conf_hvp[constants.HV_SECURITY_MODEL]
1871 if security_model == constants.HT_SM_USER:
1872 kvm_cmd.extend(["-runas", conf_hvp[constants.HV_SECURITY_DOMAIN]])
1873
1874 keymap = conf_hvp[constants.HV_KEYMAP]
1875 if keymap:
1876 keymap_path = self._InstanceKeymapFile(name)
1877
1878
1879
1880
1881 utils.WriteFile(keymap_path, data="include en-us\ninclude %s\n" % keymap)
1882 kvm_cmd.extend(["-k", keymap_path])
1883
1884
1885
1886
1887 tapfds = []
1888 taps = []
1889 devlist = self._GetKVMOutput(kvm_path, self._KVMOPT_DEVICELIST)
1890 if not kvm_nics:
1891 kvm_cmd.extend(["-net", "none"])
1892 else:
1893 vnet_hdr = False
1894 tap_extra = ""
1895 nic_type = up_hvp[constants.HV_NIC_TYPE]
1896 if nic_type == constants.HT_NIC_PARAVIRTUAL:
1897 nic_model = self._VIRTIO
1898 try:
1899 if self._VIRTIO_NET_RE.search(devlist):
1900 nic_model = self._VIRTIO_NET_PCI
1901 vnet_hdr = up_hvp[constants.HV_VNET_HDR]
1902 except errors.HypervisorError, _:
1903
1904
1905 pass
1906
1907 if up_hvp[constants.HV_VHOST_NET]:
1908
1909 if self._VHOST_RE.search(kvmhelp):
1910 tap_extra = ",vhost=on"
1911 else:
1912 raise errors.HypervisorError("vhost_net is configured"
1913 " but it is not available")
1914 else:
1915 nic_model = nic_type
1916
1917 kvm_supports_netdev = self._NETDEV_RE.search(kvmhelp)
1918
1919 for nic_seq, nic in enumerate(kvm_nics):
1920 tapname, tapfd = _OpenTap(vnet_hdr=vnet_hdr)
1921 tapfds.append(tapfd)
1922 taps.append(tapname)
1923 if kvm_supports_netdev:
1924 nic_val = "%s,mac=%s" % (nic_model, nic.mac)
1925 try:
1926
1927
1928 kvm_devid = _GenerateDeviceKVMId(constants.HOTPLUG_TARGET_NIC, nic)
1929 netdev = kvm_devid
1930 nic_val += (",id=%s,bus=pci.0,addr=%s" % (kvm_devid, hex(nic.pci)))
1931 except errors.HotplugError:
1932 netdev = "netdev%d" % nic_seq
1933 nic_val += (",netdev=%s" % netdev)
1934 tap_val = ("type=tap,id=%s,fd=%d%s" %
1935 (netdev, tapfd, tap_extra))
1936 kvm_cmd.extend(["-netdev", tap_val, "-device", nic_val])
1937 else:
1938 nic_val = "nic,vlan=%s,macaddr=%s,model=%s" % (nic_seq,
1939 nic.mac, nic_model)
1940 tap_val = "tap,vlan=%s,fd=%d" % (nic_seq, tapfd)
1941 kvm_cmd.extend(["-net", tap_val, "-net", nic_val])
1942
1943 if incoming:
1944 target, port = incoming
1945 kvm_cmd.extend(["-incoming", "tcp:%s:%s" % (target, port)])
1946
1947
1948
1949
1950 vnc_pwd_file = conf_hvp[constants.HV_VNC_PASSWORD_FILE]
1951 vnc_pwd = None
1952 if vnc_pwd_file:
1953 try:
1954 vnc_pwd = utils.ReadOneLineFile(vnc_pwd_file, strict=True)
1955 except EnvironmentError, err:
1956 raise errors.HypervisorError("Failed to open VNC password file %s: %s"
1957 % (vnc_pwd_file, err))
1958
1959 if conf_hvp[constants.HV_KVM_USE_CHROOT]:
1960 utils.EnsureDirs([(self._InstanceChrootDir(name),
1961 constants.SECURE_DIR_MODE)])
1962
1963
1964 if self._QMP_RE.search(kvmhelp):
1965 logging.debug("Enabling QMP")
1966 kvm_cmd.extend(["-qmp", "unix:%s,server,nowait" %
1967 self._InstanceQmpMonitor(instance.name)])
1968
1969 kvm_cmd.extend(["-qmp", "unix:%s,server,nowait" %
1970 self._InstanceKvmdMonitor(instance.name)])
1971
1972
1973
1974
1975 for nic_seq, nic in enumerate(kvm_nics):
1976 if (incoming and
1977 nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_ROUTED):
1978 continue
1979 self._ConfigureNIC(instance, nic_seq, nic, taps[nic_seq])
1980
1981 bdev_opts = self._GenerateKVMBlockDevicesOptions(instance,
1982 up_hvp,
1983 kvm_disks,
1984 kvmhelp,
1985 devlist)
1986 kvm_cmd.extend(bdev_opts)
1987
1988
1989
1990 start_kvm_paused = not (_KVM_START_PAUSED_FLAG in kvm_cmd) and not incoming
1991 if start_kvm_paused:
1992 kvm_cmd.extend([_KVM_START_PAUSED_FLAG])
1993
1994
1995
1996
1997 cpu_pinning = False
1998 if up_hvp.get(constants.HV_CPU_MASK, None):
1999 cpu_pinning = True
2000
2001 if security_model == constants.HT_SM_POOL:
2002 ss = ssconf.SimpleStore()
2003 uid_pool = uidpool.ParseUidPool(ss.GetUidPool(), separator="\n")
2004 all_uids = set(uidpool.ExpandUidPool(uid_pool))
2005 uid = uidpool.RequestUnusedUid(all_uids)
2006 try:
2007 username = pwd.getpwuid(uid.GetUid()).pw_name
2008 kvm_cmd.extend(["-runas", username])
2009 self._RunKVMCmd(name, kvm_cmd, tapfds)
2010 except:
2011 uidpool.ReleaseUid(uid)
2012 raise
2013 else:
2014 uid.Unlock()
2015 utils.WriteFile(self._InstanceUidFile(name), data=uid.AsStr())
2016 else:
2017 self._RunKVMCmd(name, kvm_cmd, tapfds)
2018
2019 utils.EnsureDirs([(self._InstanceNICDir(instance.name),
2020 constants.RUN_DIRS_MODE)])
2021 for nic_seq, tap in enumerate(taps):
2022 utils.WriteFile(self._InstanceNICFile(instance.name, nic_seq),
2023 data=tap)
2024
2025 if vnc_pwd:
2026 change_cmd = "change vnc password %s" % vnc_pwd
2027 self._CallMonitorCommand(instance.name, change_cmd)
2028
2029
2030
2031
2032
2033
2034 spice_password_file = conf_hvp[constants.HV_KVM_SPICE_PASSWORD_FILE]
2035 if spice_password_file:
2036 spice_pwd = ""
2037 try:
2038 spice_pwd = utils.ReadOneLineFile(spice_password_file, strict=True)
2039 except EnvironmentError, err:
2040 raise errors.HypervisorError("Failed to open SPICE password file %s: %s"
2041 % (spice_password_file, err))
2042
2043 qmp = QmpConnection(self._InstanceQmpMonitor(instance.name))
2044 qmp.connect()
2045 arguments = {
2046 "protocol": "spice",
2047 "password": spice_pwd,
2048 }
2049 qmp.Execute("set_password", arguments)
2050
2051 for filename in temp_files:
2052 utils.RemoveFile(filename)
2053
2054
2055 if cpu_pinning:
2056 self._ExecuteCpuAffinity(instance.name, up_hvp[constants.HV_CPU_MASK])
2057
2058 start_memory = self._InstanceStartupMemory(instance)
2059 if start_memory < instance.beparams[constants.BE_MAXMEM]:
2060 self.BalloonInstanceMemory(instance, start_memory)
2061
2062 if start_kvm_paused:
2063
2064
2065
2066 self._CallMonitorCommand(instance.name, self._CONT_CMD)
2067
2068 @staticmethod
2085
2086 - def StartInstance(self, instance, block_devices, startup_paused):
2097
2098 @classmethod
2100 """Invoke a command on the instance monitor.
2101
2102 """
2103 if timeout is not None:
2104 timeout_cmd = "timeout %s" % (timeout, )
2105 else:
2106 timeout_cmd = ""
2107
2108
2109
2110
2111
2112
2113
2114
2115 socat = ("echo %s | %s %s STDIO UNIX-CONNECT:%s" %
2116 (utils.ShellQuote(command),
2117 timeout_cmd,
2118 constants.SOCAT_PATH,
2119 utils.ShellQuote(cls._InstanceMonitor(instance_name))))
2120 result = utils.RunCmd(socat)
2121 if result.failed:
2122 msg = ("Failed to send command '%s' to instance '%s', reason '%s',"
2123 " output: %s" %
2124 (command, instance_name, result.fail_reason, result.output))
2125 raise errors.HypervisorError(msg)
2126
2127 return result
2128
2130 """Get the first available pci slot of a runnung instance.
2131
2132 """
2133 slots = bitarray(32)
2134 slots.setall(False)
2135 output = self._CallMonitorCommand(instance.name, self._INFO_PCI_CMD)
2136 for line in output.stdout.splitlines():
2137 match = self._INFO_PCI_RE.search(line)
2138 if match:
2139 slot = int(match.group(1))
2140 slots[slot] = True
2141
2142 dev.pci = _GetFreeSlot(slots)
2143
2170
2194
2199
2202 """Checks if a previous hotplug command has succeeded.
2203
2204 It issues info pci monitor command and checks depending on should_exist
2205 value if an entry with PCI slot and device ID is found or not.
2206
2207 @raise errors.HypervisorError: if result is not the expected one
2208
2209 """
2210 output = self._CallMonitorCommand(instance_name, self._INFO_PCI_CMD)
2211 kvm_devid = _GenerateDeviceKVMId(dev_type, device)
2212 match = \
2213 self._FIND_PCI_DEVICE_RE(device.pci, kvm_devid).search(output.stdout)
2214 if match and not should_exist:
2215 msg = "Device %s should have been removed but is still there" % kvm_devid
2216 raise errors.HypervisorError(msg)
2217
2218 if not match and should_exist:
2219 msg = "Device %s should have been added but is missing" % kvm_devid
2220 raise errors.HypervisorError(msg)
2221
2222 logging.info("Device %s has been correctly hot-plugged", kvm_devid)
2223
2224 - def HotAddDevice(self, instance, dev_type, device, extra, seq):
2225 """ Helper method to hot-add a new device
2226
2227 It gets free pci slot generates the device name and invokes the
2228 device specific method.
2229
2230 """
2231
2232 if device.pci is None:
2233 self._GetFreePCISlot(instance, device)
2234 kvm_devid = _GenerateDeviceKVMId(dev_type, device)
2235 runtime = self._LoadKVMRuntime(instance)
2236 if dev_type == constants.HOTPLUG_TARGET_DISK:
2237 drive_uri = _GetDriveURI(device, extra[0], extra[1])
2238 cmds = ["drive_add dummy file=%s,if=none,id=%s,format=raw" %
2239 (drive_uri, kvm_devid)]
2240 cmds += ["device_add virtio-blk-pci,bus=pci.0,addr=%s,drive=%s,id=%s" %
2241 (hex(device.pci), kvm_devid, kvm_devid)]
2242 elif dev_type == constants.HOTPLUG_TARGET_NIC:
2243 (tap, fd) = _OpenTap()
2244 self._ConfigureNIC(instance, seq, device, tap)
2245 self._PassTapFd(instance, fd, device)
2246 cmds = ["netdev_add tap,id=%s,fd=%s" % (kvm_devid, kvm_devid)]
2247 args = "virtio-net-pci,bus=pci.0,addr=%s,mac=%s,netdev=%s,id=%s" % \
2248 (hex(device.pci), device.mac, kvm_devid, kvm_devid)
2249 cmds += ["device_add %s" % args]
2250 utils.WriteFile(self._InstanceNICFile(instance.name, seq), data=tap)
2251
2252 self._CallHotplugCommands(instance.name, cmds)
2253 self._VerifyHotplugCommand(instance.name, device, dev_type, True)
2254
2255 index = _DEVICE_RUNTIME_INDEX[dev_type]
2256 entry = _RUNTIME_ENTRY[dev_type](device, extra)
2257 runtime[index].append(entry)
2258 self._SaveKVMRuntime(instance, runtime)
2259
2260 - def HotDelDevice(self, instance, dev_type, device, _, seq):
2261 """ Helper method for hot-del device
2262
2263 It gets device info from runtime file, generates the device name and
2264 invokes the device specific method.
2265
2266 """
2267 runtime = self._LoadKVMRuntime(instance)
2268 entry = _GetExistingDeviceInfo(dev_type, device, runtime)
2269 kvm_device = _RUNTIME_DEVICE[dev_type](entry)
2270 kvm_devid = _GenerateDeviceKVMId(dev_type, kvm_device)
2271 if dev_type == constants.HOTPLUG_TARGET_DISK:
2272 cmds = ["device_del %s" % kvm_devid]
2273 cmds += ["drive_del %s" % kvm_devid]
2274 elif dev_type == constants.HOTPLUG_TARGET_NIC:
2275 cmds = ["device_del %s" % kvm_devid]
2276 cmds += ["netdev_del %s" % kvm_devid]
2277 utils.RemoveFile(self._InstanceNICFile(instance.name, seq))
2278 self._CallHotplugCommands(instance.name, cmds)
2279 self._VerifyHotplugCommand(instance.name, kvm_device, dev_type, False)
2280 index = _DEVICE_RUNTIME_INDEX[dev_type]
2281 runtime[index].remove(entry)
2282 self._SaveKVMRuntime(instance, runtime)
2283
2284 return kvm_device.pci
2285
2286 - def HotModDevice(self, instance, dev_type, device, _, seq):
2287 """ Helper method for hot-mod device
2288
2289 It gets device info from runtime file, generates the device name and
2290 invokes the device specific method. Currently only NICs support hot-mod
2291
2292 """
2293 if dev_type == constants.HOTPLUG_TARGET_NIC:
2294
2295 device.pci = self.HotDelDevice(instance, dev_type, device, _, seq)
2296 self.HotAddDevice(instance, dev_type, device, _, seq)
2297
2299 """Pass file descriptor to kvm process via monitor socket using SCM_RIGHTS
2300
2301 """
2302
2303
2304 kvm_devid = _GenerateDeviceKVMId(constants.HOTPLUG_TARGET_NIC, nic)
2305 command = "getfd %s\n" % kvm_devid
2306 fds = [fd]
2307 logging.info("%s", fds)
2308 try:
2309 monsock = MonitorSocket(self._InstanceMonitor(instance.name))
2310 monsock.connect()
2311 fdsend.sendfds(monsock.sock, command, fds=fds)
2312 finally:
2313 monsock.close()
2314
2315 @classmethod
2317 """Parse the KVM version from the --help output.
2318
2319 @type text: string
2320 @param text: output of kvm --help
2321 @return: (version, v_maj, v_min, v_rev)
2322 @raise errors.HypervisorError: when the KVM version cannot be retrieved
2323
2324 """
2325 match = cls._VERSION_RE.search(text.splitlines()[0])
2326 if not match:
2327 raise errors.HypervisorError("Unable to get KVM version")
2328
2329 v_all = match.group(0)
2330 v_maj = int(match.group(1))
2331 v_min = int(match.group(2))
2332 if match.group(4):
2333 v_rev = int(match.group(4))
2334 else:
2335 v_rev = 0
2336 return (v_all, v_maj, v_min, v_rev)
2337
2338 @classmethod
2340 """Return the output of a kvm invocation
2341
2342 @type kvm_path: string
2343 @param kvm_path: path to the kvm executable
2344 @type option: a key of _KVMOPTS_CMDS
2345 @param option: kvm option to fetch the output from
2346 @return: output a supported kvm invocation
2347 @raise errors.HypervisorError: when the KVM help output cannot be retrieved
2348
2349 """
2350 assert option in cls._KVMOPTS_CMDS, "Invalid output option"
2351
2352 optlist, can_fail = cls._KVMOPTS_CMDS[option]
2353
2354 result = utils.RunCmd([kvm_path] + optlist)
2355 if result.failed and not can_fail:
2356 raise errors.HypervisorError("Unable to get KVM %s output" %
2357 " ".join(optlist))
2358 return result.output
2359
2360 @classmethod
2362 """Return the installed KVM version.
2363
2364 @return: (version, v_maj, v_min, v_rev)
2365 @raise errors.HypervisorError: when the KVM version cannot be retrieved
2366
2367 """
2368 return cls._ParseKVMVersion(cls._GetKVMOutput(kvm_path, cls._KVMOPT_HELP))
2369
2370 @classmethod
2381
2382 @classmethod
2383 - def _StopInstance(cls, instance, force=False, name=None, timeout=None):
2403
2404 - def StopInstance(self, instance, force=False, retry=False, name=None,
2405 timeout=None):
2410
2420
2443
2445 """Get instance information to perform a migration.
2446
2447 @type instance: L{objects.Instance}
2448 @param instance: instance to be migrated
2449 @rtype: string
2450 @return: content of the KVM runtime file
2451
2452 """
2453 return self._ReadKVMRuntime(instance.name)
2454
2456 """Prepare to accept an instance.
2457
2458 @type instance: L{objects.Instance}
2459 @param instance: instance to be accepted
2460 @type info: string
2461 @param info: content of the KVM runtime file on the source node
2462 @type target: string
2463 @param target: target host (usually ip), on this node
2464
2465 """
2466 kvm_runtime = self._LoadKVMRuntime(instance, serialized_runtime=info)
2467 incoming_address = (target, instance.hvparams[constants.HV_MIGRATION_PORT])
2468 kvmpath = instance.hvparams[constants.HV_KVM_PATH]
2469 kvmhelp = self._GetKVMOutput(kvmpath, self._KVMOPT_HELP)
2470 self._ExecuteKVMRuntime(instance, kvm_runtime, kvmhelp,
2471 incoming=incoming_address)
2472
2474 """Finalize the instance migration on the target node.
2475
2476 Stop the incoming mode KVM.
2477
2478 @type instance: L{objects.Instance}
2479 @param instance: instance whose migration is being finalized
2480
2481 """
2482 if success:
2483 kvm_runtime = self._LoadKVMRuntime(instance, serialized_runtime=info)
2484 kvm_nics = kvm_runtime[1]
2485
2486 for nic_seq, nic in enumerate(kvm_nics):
2487 if nic.nicparams[constants.NIC_MODE] != constants.NIC_MODE_ROUTED:
2488
2489 continue
2490 try:
2491 tap = utils.ReadFile(self._InstanceNICFile(instance.name, nic_seq))
2492 except EnvironmentError, err:
2493 logging.warning("Failed to find host interface for %s NIC #%d: %s",
2494 instance.name, nic_seq, str(err))
2495 continue
2496 try:
2497 self._ConfigureNIC(instance, nic_seq, nic, tap)
2498 except errors.HypervisorError, err:
2499 logging.warning(str(err))
2500
2501 self._WriteKVMRuntime(instance.name, info)
2502 else:
2503 self.StopInstance(instance, force=True)
2504
2506 """Migrate an instance to a target node.
2507
2508 The migration will not be attempted if the instance is not
2509 currently running.
2510
2511 @type cluster_name: string
2512 @param cluster_name: name of the cluster
2513 @type instance: L{objects.Instance}
2514 @param instance: the instance to be migrated
2515 @type target: string
2516 @param target: ip address of the target node
2517 @type live: boolean
2518 @param live: perform a live migration
2519
2520 """
2521 instance_name = instance.name
2522 port = instance.hvparams[constants.HV_MIGRATION_PORT]
2523 _, _, alive = self._InstancePidAlive(instance_name)
2524 if not alive:
2525 raise errors.HypervisorError("Instance not running, cannot migrate")
2526
2527 if not live:
2528 self._CallMonitorCommand(instance_name, "stop")
2529
2530 migrate_command = ("migrate_set_speed %dm" %
2531 instance.hvparams[constants.HV_MIGRATION_BANDWIDTH])
2532 self._CallMonitorCommand(instance_name, migrate_command)
2533
2534 migrate_command = ("migrate_set_downtime %dms" %
2535 instance.hvparams[constants.HV_MIGRATION_DOWNTIME])
2536 self._CallMonitorCommand(instance_name, migrate_command)
2537
2538 migrate_command = "migrate -d tcp:%s:%s" % (target, port)
2539 self._CallMonitorCommand(instance_name, migrate_command)
2540
2559
2561 """Get the migration status
2562
2563 @type instance: L{objects.Instance}
2564 @param instance: the instance that is being migrated
2565 @rtype: L{objects.MigrationStatus}
2566 @return: the status of the current migration (one of
2567 L{constants.HV_MIGRATION_VALID_STATUSES}), plus any additional
2568 progress info that can be retrieved from the hypervisor
2569
2570 """
2571 info_command = "info migrate"
2572 for _ in range(self._MIGRATION_INFO_MAX_BAD_ANSWERS):
2573 result = self._CallMonitorCommand(instance.name, info_command)
2574 match = self._MIGRATION_STATUS_RE.search(result.stdout)
2575 if not match:
2576 if not result.stdout:
2577 logging.info("KVM: empty 'info migrate' result")
2578 else:
2579 logging.warning("KVM: unknown 'info migrate' result: %s",
2580 result.stdout)
2581 else:
2582 status = match.group(1)
2583 if status in constants.HV_KVM_MIGRATION_VALID_STATUSES:
2584 migration_status = objects.MigrationStatus(status=status)
2585 match = self._MIGRATION_PROGRESS_RE.search(result.stdout)
2586 if match:
2587 migration_status.transferred_ram = match.group("transferred")
2588 migration_status.total_ram = match.group("total")
2589
2590 return migration_status
2591
2592 logging.warning("KVM: unknown migration status '%s'", status)
2593
2594 time.sleep(self._MIGRATION_INFO_RETRY_DELAY)
2595
2596 return objects.MigrationStatus(status=constants.HV_MIGRATION_FAILED)
2597
2599 """Balloon an instance memory to a certain value.
2600
2601 @type instance: L{objects.Instance}
2602 @param instance: instance to be accepted
2603 @type mem: int
2604 @param mem: actual memory size to use for instance runtime
2605
2606 """
2607 self._CallMonitorCommand(instance.name, "balloon %d" % mem)
2608
2610 """Return information about the node.
2611
2612 @type hvparams: dict of strings
2613 @param hvparams: hypervisor parameters, not used in this class
2614
2615 @return: a dict as returned by L{BaseHypervisor.GetLinuxNodeInfo} plus
2616 the following keys:
2617 - hv_version: the hypervisor version in the form (major, minor,
2618 revision)
2619
2620 """
2621 result = self.GetLinuxNodeInfo()
2622 kvmpath = constants.KVM_PATH
2623 if hvparams is not None:
2624 kvmpath = hvparams.get(constants.HV_KVM_PATH, constants.KVM_PATH)
2625 _, v_major, v_min, v_rev = self._GetKVMVersion(kvmpath)
2626 result[constants.HV_NODEINFO_KEY_VERSION] = (v_major, v_min, v_rev)
2627 return result
2628
2629 @classmethod
2630 - def GetInstanceConsole(cls, instance, primary_node, node_group,
2631 hvparams, beparams):
2632 """Return a command for connecting to the console of an instance.
2633
2634 """
2635 if hvparams[constants.HV_SERIAL_CONSOLE]:
2636 cmd = [pathutils.KVM_CONSOLE_WRAPPER,
2637 constants.SOCAT_PATH, utils.ShellQuote(instance.name),
2638 utils.ShellQuote(cls._InstanceMonitor(instance.name)),
2639 "STDIO,%s" % cls._SocatUnixConsoleParams(),
2640 "UNIX-CONNECT:%s" % cls._InstanceSerial(instance.name)]
2641 ndparams = node_group.FillND(primary_node)
2642 return objects.InstanceConsole(instance=instance.name,
2643 kind=constants.CONS_SSH,
2644 host=primary_node.name,
2645 port=ndparams.get(constants.ND_SSH_PORT),
2646 user=constants.SSH_CONSOLE_USER,
2647 command=cmd)
2648
2649 vnc_bind_address = hvparams[constants.HV_VNC_BIND_ADDRESS]
2650 if vnc_bind_address and instance.network_port > constants.VNC_BASE_PORT:
2651 display = instance.network_port - constants.VNC_BASE_PORT
2652 return objects.InstanceConsole(instance=instance.name,
2653 kind=constants.CONS_VNC,
2654 host=vnc_bind_address,
2655 port=instance.network_port,
2656 display=display)
2657
2658 spice_bind = hvparams[constants.HV_KVM_SPICE_BIND]
2659 if spice_bind:
2660 return objects.InstanceConsole(instance=instance.name,
2661 kind=constants.CONS_SPICE,
2662 host=spice_bind,
2663 port=instance.network_port)
2664
2665 return objects.InstanceConsole(instance=instance.name,
2666 kind=constants.CONS_MESSAGE,
2667 message=("No serial shell for instance %s" %
2668 instance.name))
2669
2670 - def Verify(self, hvparams=None):
2671 """Verify the hypervisor.
2672
2673 Check that the required binaries exist.
2674
2675 @type hvparams: dict of strings
2676 @param hvparams: hypervisor parameters to be verified against, not used here
2677
2678 @return: Problem description if something is wrong, C{None} otherwise
2679
2680 """
2681 msgs = []
2682 kvmpath = constants.KVM_PATH
2683 if hvparams is not None:
2684 kvmpath = hvparams.get(constants.HV_KVM_PATH, constants.KVM_PATH)
2685 if not os.path.exists(kvmpath):
2686 msgs.append("The KVM binary ('%s') does not exist" % kvmpath)
2687 if not os.path.exists(constants.SOCAT_PATH):
2688 msgs.append("The socat binary ('%s') does not exist" %
2689 constants.SOCAT_PATH)
2690
2691 return self._FormatVerifyResults(msgs)
2692
2693 @classmethod
2695 """Check the given parameters for validity.
2696
2697 @type hvparams: dict of strings
2698 @param hvparams: hypervisor parameters
2699 @raise errors.HypervisorError: when a parameter is not valid
2700
2701 """
2702 super(KVMHypervisor, cls).CheckParameterSyntax(hvparams)
2703
2704 kernel_path = hvparams[constants.HV_KERNEL_PATH]
2705 if kernel_path:
2706 if not hvparams[constants.HV_ROOT_PATH]:
2707 raise errors.HypervisorError("Need a root partition for the instance,"
2708 " if a kernel is defined")
2709
2710 if (hvparams[constants.HV_VNC_X509_VERIFY] and
2711 not hvparams[constants.HV_VNC_X509]):
2712 raise errors.HypervisorError("%s must be defined, if %s is" %
2713 (constants.HV_VNC_X509,
2714 constants.HV_VNC_X509_VERIFY))
2715
2716 if hvparams[constants.HV_SERIAL_CONSOLE]:
2717 serial_speed = hvparams[constants.HV_SERIAL_SPEED]
2718 valid_speeds = constants.VALID_SERIAL_SPEEDS
2719 if not serial_speed or serial_speed not in valid_speeds:
2720 raise errors.HypervisorError("Invalid serial console speed, must be"
2721 " one of: %s" %
2722 utils.CommaJoin(valid_speeds))
2723
2724 boot_order = hvparams[constants.HV_BOOT_ORDER]
2725 if (boot_order == constants.HT_BO_CDROM and
2726 not hvparams[constants.HV_CDROM_IMAGE_PATH]):
2727 raise errors.HypervisorError("Cannot boot from cdrom without an"
2728 " ISO path")
2729
2730 security_model = hvparams[constants.HV_SECURITY_MODEL]
2731 if security_model == constants.HT_SM_USER:
2732 if not hvparams[constants.HV_SECURITY_DOMAIN]:
2733 raise errors.HypervisorError("A security domain (user to run kvm as)"
2734 " must be specified")
2735 elif (security_model == constants.HT_SM_NONE or
2736 security_model == constants.HT_SM_POOL):
2737 if hvparams[constants.HV_SECURITY_DOMAIN]:
2738 raise errors.HypervisorError("Cannot have a security domain when the"
2739 " security model is 'none' or 'pool'")
2740
2741 spice_bind = hvparams[constants.HV_KVM_SPICE_BIND]
2742 spice_ip_version = hvparams[constants.HV_KVM_SPICE_IP_VERSION]
2743 if spice_bind:
2744 if spice_ip_version != constants.IFACE_NO_IP_VERSION_SPECIFIED:
2745
2746
2747 if (netutils.IP4Address.IsValid(spice_bind) and
2748 spice_ip_version != constants.IP4_VERSION):
2749 raise errors.HypervisorError("SPICE: Got an IPv4 address (%s), but"
2750 " the specified IP version is %s" %
2751 (spice_bind, spice_ip_version))
2752
2753 if (netutils.IP6Address.IsValid(spice_bind) and
2754 spice_ip_version != constants.IP6_VERSION):
2755 raise errors.HypervisorError("SPICE: Got an IPv6 address (%s), but"
2756 " the specified IP version is %s" %
2757 (spice_bind, spice_ip_version))
2758 else:
2759
2760
2761 for param in _SPICE_ADDITIONAL_PARAMS:
2762 if hvparams[param]:
2763 raise errors.HypervisorError("SPICE: %s requires %s to be set" %
2764 (param, constants.HV_KVM_SPICE_BIND))
2765
2766 @classmethod
2826
2827 @classmethod
2829 """KVM powercycle, just a wrapper over Linux powercycle.
2830
2831 @type hvparams: dict of strings
2832 @param hvparams: hypervisor parameters to be used on this node
2833
2834 """
2835 cls.LinuxPowercycle()
2836