5bab1f25b765a567436c0860bafe901be39d4272
[ganeti-github.git] / lib / hypervisor / hv_kvm.py
1 #
2 #
3
4 # Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
9 # met:
10 #
11 # 1. Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 #
14 # 2. Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the distribution.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
19 # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 # pylint: disable=F0401
52 except ImportError:
53 affinity = None
54 try:
55 import fdsend # pylint: disable=F0401
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 # TUN/TAP driver constants, taken from <linux/if_tun.h>
76 # They are architecture-independent and already hardcoded in qemu-kvm source,
77 # so we can safely include them here.
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 #: SPICE parameters which depend on L{constants.HV_KVM_SPICE_BIND}
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 # Constant bitarray that reflects to a free pci slot
98 # Use it with bitarray.search()
99 _AVAILABLE_PCI_SLOT = bitarray("0")
100
101 # below constants show the format of runtime file
102 # the nics are in second possition, while the disks in 4th (last)
103 # moreover disk entries are stored as a list of in tuples
104 # (L{objects.Disk}, link_name, uri)
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 }
126
127
128 def _GetDriveURI(disk, link, uri):
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
148
149 def _GenerateDeviceKVMId(dev_type, dev):
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
170
171 def _GetFreeSlot(slots, slot=None, reserve=False):
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
202
203 def _GetExistingDeviceInfo(dev_type, device, runtime):
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
228
229 def _UpgradeSerializedRuntime(serialized_runtime):
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 # Add a dummy uuid slot if an pre-2.8 NIC is found
251 if "uuid" not in nic:
252 nic["uuid"] = utils.NewUUID()
253
254 return kvm_cmd, serialized_nics, hvparams, serialized_disks
255
256
257 def _AnalyzeSerializedRuntime(serialized_runtime):
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
274
275 def _GetTunFeatures(fd, _ioctl=fcntl.ioctl):
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
291
292 def _ProbeTapVnetHdr(fd, _features_fn=_GetTunFeatures):
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 # Not supported
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
322
323 def _OpenTap(vnet_hdr=True):
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 # The struct ifreq ioctl request (see netdevice(7))
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 # Get the interface name from the ioctl
355 ifname = struct.unpack("16sh", res)[0].strip("\x00")
356 return (ifname, tapfd)
357
358
359 class QmpMessage:
360 """QEMU Messaging Protocol (QMP) message.
361
362 """
363 def __init__(self, data):
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
372 def __getitem__(self, field_name):
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
383 def __setitem__(self, field_name, field_value):
384 """Set the value of the required field_name to field_value.
385
386 """
387 self.data[field_name] = field_value
388
389 def __len__(self):
390 """Return the number of fields stored in this QmpMessage.
391
392 """
393 return len(self.data)
394
395 def __delitem__(self, key):
396 """Delete the specified element from the QmpMessage.
397
398 """
399 del(self.data[key])
400
401 @staticmethod
402 def BuildFromJsonString(json_string):
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 # Parse the string
412 data = serializer.LoadJson(json_string)
413 return QmpMessage(data)
414
415 def __str__(self):
416 # The protocol expects the JSON object to be sent as a single line.
417 return serializer.DumpJson(self.data)
418
419 def __eq__(self, other):
420 # When comparing two QmpMessages, we are interested in comparing
421 # their internal representation of the message data
422 return self.data == other.data
423
424
425 class MonitorSocket(object):
426 _SOCKET_TIMEOUT = 5
427
428 def __init__(self, monitor_filename):
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 # We want to fail if the server doesn't send a complete message
439 # in a reasonable amount of time
440 self.sock.settimeout(self._SOCKET_TIMEOUT)
441 self._connected = False
442
443 def _check_socket(self):
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
456 def _check_connection(self):
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
464 def connect(self):
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 # Check file existance/stuff
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
484 def close(self):
485 """Closes the socket
486
487 It cannot be used after this call.
488
489 """
490 self.sock.close()
491
492
493 class QmpConnection(MonitorSocket):
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
509 def __init__(self, monitor_filename):
510 super(QmpConnection, self).__init__(monitor_filename)
511 self._buf = ""
512
513 def connect(self):
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 # Check if we receive a correct greeting message from the server
525 # (As per the QEMU Protocol Specification 0.1 - section 2.2)
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 # This is needed because QMP can return more than one greetings
533 # see https://groups.google.com/d/msg/ganeti-devel/gZYcvHKDooU/SnukC8dgS5AJ
534 self._buf = ""
535
536 # Let's put the monitor in command mode using the qmp_capabilities
537 # command, or else no command will be executable.
538 # (As per the QEMU Protocol Specification 0.1 - section 4)
539 self.Execute(self._CAPABILITIES_COMMAND)
540
541 def _ParseMessage(self, buf):
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 # Check if we got the message end token (CRLF, as per the QEMU Protocol
553 # Specification 0.1 - Section 2.1.1)
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
564 def _Recv(self):
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 # Check if there is already a message in the buffer
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 # Events can occur between the sending of the command and the reception
644 # of the response, so we need to filter out messages with the event key.
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
658
659 class KVMHypervisor(hv_base.BaseHypervisor):
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" # contains live instances pids
667 _UIDS_DIR = _ROOT_DIR + "/uid" # contains instances reserved uids
668 _CTRL_DIR = _ROOT_DIR + "/ctrl" # contains instances control sockets
669 _CONF_DIR = _ROOT_DIR + "/conf" # contains instances startup data
670 _NICS_DIR = _ROOT_DIR + "/nic" # contains instances nic <-> tap associations
671 _KEYMAP_DIR = _ROOT_DIR + "/keymap" # contains instances keymaps
672 # KVM instances with chroot enabled are started in empty chroot directories.
673 _CHROOT_DIR = _ROOT_DIR + "/chroot" # for empty chroot directories
674 # After an instance is stopped, its chroot directory is removed.
675 # If the chroot directory is not empty, it can't be removed.
676 # A non-empty chroot directory indicates a possible security incident.
677 # To support forensics, the non-empty chroot directory is quarantined in
678 # a separate directory, called 'chroot-quarantine'.
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, # will be checked later
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, # will be checked later
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 # match -drive.*boot=on|off on different lines, but in between accept only
800 # dashes not preceeded by a new line (which would mean another option
801 # different than -drive is starting)
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 # Slot 0 for Host bridge, Slot 1 for ISA bridge, Slot 2 for VGA controller
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 # Supported kvm options to get output from
828 _KVMOPT_HELP = "help"
829 _KVMOPT_MLIST = "mlist"
830 _KVMOPT_DEVICELIST = "devicelist"
831
832 # Command to execute to get the output from kvm, and whether to
833 # accept the output even on failure.
834 _KVMOPTS_CMDS = {
835 _KVMOPT_HELP: (["--help"], False),
836 _KVMOPT_MLIST: (["-M", "?"], False),
837 _KVMOPT_DEVICELIST: (["-device", "?"], True),
838 }
839
840 def __init__(self):
841 hv_base.BaseHypervisor.__init__(self)
842 # Let's make sure the directories we need exist, even if the RUN_DIR lives
843 # in a tmpfs filesystem or has been otherwise wiped out.
844 dirs = [(dname, constants.RUN_DIRS_MODE) for dname in self._DIRS]
845 utils.EnsureDirs(dirs)
846
847 @classmethod
848 def _InstancePidFile(cls, instance_name):
849 """Returns the instance pidfile.
850
851 """
852 return utils.PathJoin(cls._PIDS_DIR, instance_name)
853
854 @classmethod
855 def _InstanceUidFile(cls, instance_name):
856 """Returns the instance uidfile.
857
858 """
859 return utils.PathJoin(cls._UIDS_DIR, instance_name)
860
861 @classmethod
862 def _InstancePidInfo(cls, pid):
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
907 def _InstancePidAlive(cls, instance_name):
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
929 def _CheckDown(cls, instance_name):
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
939 def _InstanceMonitor(cls, instance_name):
940 """Returns the instance monitor socket name
941
942 """
943 return utils.PathJoin(cls._CTRL_DIR, "%s.monitor" % instance_name)
944
945 @classmethod
946 def _InstanceSerial(cls, instance_name):
947 """Returns the instance serial socket name
948
949 """
950 return utils.PathJoin(cls._CTRL_DIR, "%s.serial" % instance_name)
951
952 @classmethod
953 def _InstanceQmpMonitor(cls, instance_name):
954 """Returns the instance serial QMP socket name
955
956 """
957 return utils.PathJoin(cls._CTRL_DIR, "%s.qmp" % instance_name)
958
959 @classmethod
960 def _InstanceKvmdMonitor(cls, instance_name):
961 """Returns the instance kvm daemon socket name
962
963 """
964 return utils.PathJoin(cls._CTRL_DIR, "%s.kvmd" % instance_name)
965
966 @classmethod
967 def _InstanceShutdownMonitor(cls, instance_name):
968 """Returns the instance QMP output filename
969
970 """
971 return utils.PathJoin(cls._CTRL_DIR, "%s.shutdown" % instance_name)
972
973 @staticmethod
974 def _SocatUnixConsoleParams():
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
986 def _InstanceKVMRuntime(cls, instance_name):
987 """Returns the instance KVM runtime filename
988
989 """
990 return utils.PathJoin(cls._CONF_DIR, "%s.runtime" % instance_name)
991
992 @classmethod
993 def _InstanceChrootDir(cls, instance_name):
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
1000 def _InstanceNICDir(cls, instance_name):
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
1008 def _InstanceNICFile(cls, instance_name, seq):
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
1015 def _InstanceKeymapFile(cls, instance_name):
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
1022 def _TryReadUidFile(cls, uid_file):
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
1037 def _RemoveInstanceRuntimeFiles(cls, pidfile, instance_name):
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 # The chroot directory is expected to be empty, but it isn't.
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
1077 def _ConfigureNIC(instance, seq, nic, tap):
1078 """Run the network configuration script for a specified NIC
1079
1080 @param instance: instance we're acting on
1081 @type instance: instance object
1082 @param seq: nic sequence number
1083 @type seq: int
1084 @param nic: nic we're acting on
1085 @type nic: nic object
1086 @param tap: the host's tap interface this NIC corresponds to
1087 @type tap: str
1088
1089 """
1090 env = {
1091 "PATH": "%s:/sbin:/usr/sbin" % os.environ["PATH"],
1092 "INSTANCE": instance.name,
1093 "MAC": nic.mac,
1094 "MODE": nic.nicparams[constants.NIC_MODE],
1095 "INTERFACE": tap,
1096 "INTERFACE_INDEX": str(seq),
1097 "INTERFACE_UUID": nic.uuid,
1098 "TAGS": " ".join(instance.GetTags()),
1099 }
1100
1101 if nic.ip:
1102 env["IP"] = nic.ip
1103
1104 if nic.name:
1105 env["INTERFACE_NAME"] = nic.name
1106
1107 if nic.nicparams[constants.NIC_LINK]:
1108 env["LINK"] = nic.nicparams[constants.NIC_LINK]
1109
1110 if constants.NIC_VLAN in nic.nicparams:
1111 env["VLAN"] = nic.nicparams[constants.NIC_VLAN]
1112
1113 if nic.network:
1114 n = objects.Network.FromDict(nic.netinfo)
1115 env.update(n.HooksDict())
1116
1117 if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
1118 env["BRIDGE"] = nic.nicparams[constants.NIC_LINK]
1119
1120 result = utils.RunCmd([pathutils.KVM_IFUP, tap], env=env)
1121 if result.failed:
1122 raise errors.HypervisorError("Failed to configure interface %s: %s;"
1123 " network configuration script output: %s" %
1124 (tap, result.fail_reason, result.output))
1125
1126 @staticmethod
1127 def _VerifyAffinityPackage():
1128 if affinity is None:
1129 raise errors.HypervisorError("affinity Python package not"
1130 " found; cannot use CPU pinning under KVM")
1131
1132 @staticmethod
1133 def _BuildAffinityCpuMask(cpu_list):
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
1152 def _AssignCpuAffinity(cls, cpu_mask, process_id, thread_dict):
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 # Convert the string CPU mask to a list of list of int's
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 # If CPU pinning has 1 entry that's "all", then do nothing
1171 pass
1172 else:
1173 # If CPU pinning has one non-all entry, map the entire VM to
1174 # one set of physical CPUs
1175 cls._VerifyAffinityPackage()
1176 affinity.set_process_affinity_mask(
1177 process_id, cls._BuildAffinityCpuMask(all_cpu_mapping))
1178 else:
1179 # The number of vCPUs mapped should match the number of vCPUs
1180 # reported by KVM. This was already verified earlier, so
1181 # here only as a sanity check.
1182 assert len(thread_dict) == len(cpu_list)
1183 cls._VerifyAffinityPackage()
1184
1185 # For each vCPU, map it to the proper list of physical CPUs
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
1190 def _GetVcpuThreadIds(self, instance_name):
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
1210 def _ExecuteCpuAffinity(self, instance_name, cpu_mask):
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 # Get KVM process ID, to be used if need to pin entire VM
1220 _, pid, _ = self._InstancePidAlive(instance_name)
1221 # Get vCPU thread IDs, to be used if need to pin vCPUs separately
1222 thread_dict = self._GetVcpuThreadIds(instance_name)
1223 # Run CPU pinning, based on configured mask
1224 self._AssignCpuAffinity(cpu_mask, pid, thread_dict)
1225
1226 def ListInstances(self, hvparams=None):
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
1240 def _IsUserShutdown(cls, instance_name):
1241 return os.path.exists(cls._InstanceShutdownMonitor(instance_name))
1242
1243 @classmethod
1244 def _ClearUserShutdown(cls, instance_name):
1245 utils.RemoveFile(cls._InstanceShutdownMonitor(instance_name))
1246
1247 def GetInstanceInfo(self, instance_name, hvparams=None):
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 # Will fail if ballooning is not enabled, but we can then just resort to
1274 # the value above.
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
1282 def GetAllInstancesInfo(self, hvparams=None):
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 # Ignore exceptions due to instances being shut down
1296 continue
1297 if info:
1298 data.append(info)
1299 return data
1300
1301 def _GenerateKVMBlockDevicesOptions(self, instance, up_hvp, kvm_disks,
1302 kvmhelp, devlist):
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 # whether this is an older KVM version that uses the boot=on flag
1326 # on devices
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 # will be passed in -device option as driver
1338 device_driver = self._VIRTIO_BLK_PCI
1339 except errors.HypervisorError, _:
1340 pass
1341 else:
1342 if_val = ",if=%s" % disk_type
1343 # Cache mode
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 # TODO: make this a hard error, instead of a silent overwrite
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 # TODO: handle FD_LOOP and FD_BLKTAP (?)
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 # kvm_disks are the 4th entry of runtime file that did not exist in
1375 # the past. That means that cfdev should always have pci slot and
1376 # _GenerateDeviceKVMId() will not raise a exception.
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
1389 def _GenerateKVMRuntime(self, instance, block_devices, startup_paused,
1390 kvmhelp):
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 # pylint: disable=R0912,R0914,R0915
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 # used just by the vnc server, if enabled
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 # As requested by music lovers
1429 if hvp[constants.HV_SOUNDHW]:
1430 soundhw = hvp[constants.HV_SOUNDHW]
1431 # For some reason only few sound devices require a PCI slot
1432 # while the Audio controller *must* be in slot 3.
1433 # That's why we bridge this option early in command line
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 # The SCSI controller requires another PCI slot.
1440 _ = _GetFreeSlot(pci_reservations, reserve=True)
1441
1442 # Add id to ballon and place to the first available slot (3 or 4)
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 # TODO (2.8): kernel_irqchip and kvm_shadow_mem machine properties, as
1458 # extra hypervisor parameters. We should also investigate whether and how
1459 # shadow_mem should be considered for the resource model.
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 # whether this is an older KVM version that uses the boot=on flag
1490 # on devices
1491 needs_boot_flag = self._BOOT_RE.search(kvmhelp)
1492
1493 disk_type = hvp[constants.HV_DISK_TYPE]
1494
1495 #Now we can specify a different device type for CDROM devices.
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 # set cdrom 'if' type
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 # set boot flag, if needed
1512 boot_val = ""
1513 if boot_cdrom:
1514 kvm_cmd.extend(["-boot", "d"])
1515 if needs_boot_flag:
1516 boot_val = ",boot=on"
1517 # and finally build the entire '-drive' value
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 # Only allow tls and other option when not binding to a file, for now.
1603 # kvm/qemu gets confused otherwise about the filename to use.
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 # FIXME: this is wrong here; the iface ip address differs
1624 # between systems, so it should be done in _ExecuteKVMRuntime
1625 if netutils.IsValidInterface(spice_bind):
1626 # The user specified a network interface, we have to figure out the IP
1627 # address.
1628 addresses = netutils.GetInterfaceIpAddresses(spice_bind)
1629 spice_ip_version = hvp[constants.HV_KVM_SPICE_IP_VERSION]
1630
1631 # if the user specified an IP version and the interface does not
1632 # have that kind of IP addresses, throw an exception
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 # the user did not specify an IP version, we have to figure it out
1640 elif (addresses[constants.IP4_VERSION] and
1641 addresses[constants.IP6_VERSION]):
1642 # we have both ipv4 and ipv6, let's use the cluster default IP
1643 # version
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 # spice_bind is known to be a valid IP address, because
1659 # ValidateParameters checked it.
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 # Image compression options
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 # Video stream detection
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 # Audio compression, by default in qemu-kvm it is on
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 # Enable the spice agent communication channel between the host and the
1705 # agent.
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 # From qemu 1.4 -nographic is incompatible with -daemonize. The new way
1720 # also works in earlier versions though (tested with 1.1 and 1.3)
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 # Add qemu-KVM -cpu param
1733 if hvp[constants.HV_CPU_TYPE]:
1734 kvm_cmd.extend(["-cpu", hvp[constants.HV_CPU_TYPE]])
1735
1736 # Pass a -vga option if requested, or if spice is used, for backwards
1737 # compatibility.
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 # Various types of usb devices, comma separated
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 # Set system UUID to instance UUID
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
1769 def _WriteKVMRuntime(self, instance_name, data):
1770 """Write an instance's KVM runtime
1771
1772 """
1773 try:
1774 utils.WriteFile(self._InstanceKVMRuntime(instance_name),
1775 data=data)
1776 except EnvironmentError, err:
1777 raise errors.HypervisorError("Failed to save KVM runtime file: %s" % err)
1778
1779 def _ReadKVMRuntime(self, instance_name):
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
1789 def _SaveKVMRuntime(self, instance, kvm_runtime):
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
1803 def _LoadKVMRuntime(self, instance, serialized_runtime=None):
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 # too many local variables
1836 # pylint: disable=R0914
1837 def _ExecuteKVMRuntime(self, instance, kvm_runtime, kvmhelp, incoming=None):
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 # Small _ExecuteKVMRuntime hv parameters programming howto:
1847 # - conf_hvp contains the parameters as configured on ganeti. they might
1848 # have changed since the instance started; only use them if the change
1849 # won't affect the inside of the instance (which hasn't been rebooted).
1850 # - up_hvp contains the parameters as they were when the instance was
1851 # started, plus any new parameter which has been added between ganeti
1852 # versions: it is paramount that those default to a value which won't
1853 # affect the inside of the instance as well.
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 # the first element of kvm_cmd is always the path to the kvm binary
1865 kvm_path = kvm_cmd[0]
1866 up_hvp = objects.FillDict(conf_hvp, up_hvp)
1867
1868 # We know it's safe to run as a different user upon migration, so we'll use
1869 # the latest conf, from conf_hvp.
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 # If a keymap file is specified, KVM won't use its internal defaults. By
1878 # first including the "en-us" layout, an error on loading the actual
1879 # layout (e.g. because it can't be found) won't lead to a non-functional
1880 # keyboard. A keyboard with incorrect keys is still better than none.
1881 utils.WriteFile(keymap_path, data="include en-us\ninclude %s\n" % keymap)
1882 kvm_cmd.extend(["-k", keymap_path])
1883
1884 # We have reasons to believe changing something like the nic driver/type
1885 # upon migration won't exactly fly with the instance kernel, so for nic
1886 # related parameters we'll use up_hvp
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 # Older versions of kvm don't support DEVICE_LIST, but they don't
1904 # have new virtio syntax either.
1905 pass
1906
1907 if up_hvp[constants.HV_VHOST_NET]:
1908 # check for vhost_net support
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 # kvm_nics already exist in old runtime files and thus there might
1927 # be some entries without pci slot (therefore try: except:)
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 # Changing the vnc password doesn't bother the guest that much. At most it
1948 # will surprise people who connect to it. Whether positively or negatively
1949 # it's debatable.
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 # Automatically enable QMP if version is >= 0.14
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 # Add a second monitor for kvmd
1969 kvm_cmd.extend(["-qmp", "unix:%s,server,nowait" %
1970 self._InstanceKvmdMonitor(instance.name)])
1971
1972 # Configure the network now for starting instances and bridged/OVS
1973 # interfaces, during FinalizeMigration for incoming instances' routed
1974 # interfaces.
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 # CPU affinity requires kvm to start paused, so we set this flag if the
1988 # instance is not already paused and if we are not going to accept a
1989 # migrating instance. In the latter case, pausing is not needed.
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 # Note: CPU pinning is using up_hvp since changes take effect
1995 # during instance startup anyway, and to avoid problems when soft
1996 # rebooting the instance.
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 # Setting SPICE password. We are not vulnerable to malicious passwordless
2030 # connection attempts because SPICE by default does not allow connections
2031 # if neither a password nor the "disable_ticketing" options are specified.
2032 # As soon as we send the password via QMP, that password is a valid ticket
2033 # for connection.
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 # If requested, set CPU affinity and resume instance execution
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 # To control CPU pinning, ballooning, and vnc/spice passwords
2064 # the VM was started in a frozen state. If freezing was not
2065 # explicitly requested resume the vm status.
2066 self._CallMonitorCommand(instance.name, self._CONT_CMD)
2067
2068 @staticmethod
2069 def _StartKvmd(hvparams):
2070 """Ensure that the Kvm daemon is running.
2071
2072 @type hvparams: dict of strings
2073 @param hvparams: hypervisor parameters
2074
2075 """
2076 if hvparams is None \
2077 or not hvparams[constants.HV_KVM_USER_SHUTDOWN] \
2078 or utils.IsDaemonAlive(constants.KVMD):
2079 return
2080
2081 result = utils.RunCmd(constants.KVMD)
2082
2083 if result.failed:
2084 raise errors.HypervisorError("Failed to start KVM daemon")
2085
2086 def StartInstance(self, instance, block_devices, startup_paused):
2087 """Start an instance.
2088
2089 """
2090 self._CheckDown(instance.name)
2091 kvmpath = instance.hvparams[constants.HV_KVM_PATH]
2092 kvmhelp = self._GetKVMOutput(kvmpath, self._KVMOPT_HELP)
2093 kvm_runtime = self._GenerateKVMRuntime(instance, block_devices,
2094 startup_paused, kvmhelp)
2095 self._SaveKVMRuntime(instance, kvm_runtime)
2096 self._ExecuteKVMRuntime(instance, kvm_runtime, kvmhelp)
2097
2098 @classmethod
2099 def _CallMonitorCommand(cls, instance_name, command, timeout=None):
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 # TODO: Replace monitor calls with QMP once KVM >= 0.14 is the minimum
2109 # version. The monitor protocol is designed for human consumption, whereas
2110 # QMP is made for programmatic usage. In the worst case QMP can also
2111 # execute monitor commands. As it is, all calls to socat take at least
2112 # 500ms and likely more: socat can't detect the end of the reply and waits
2113 # for 500ms of no data received before exiting (500 ms is the default for
2114 # the "-t" parameter).
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
2129 def _GetFreePCISlot(self, instance, dev):
2130 """Get the first available pci slot of a runnung instance.
2131
2132 """
2133 slots = bitarray(32)
2134 slots.setall(False) # pylint: disable=E1101
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
2144 def VerifyHotplugSupport(self, instance, action, dev_type):
2145 """Verifies that hotplug is supported.
2146
2147 Hotplug is *not* supported in case of:
2148 - security models and chroot (disk hotplug)
2149 - fdsend module is missing (nic hot-add)
2150
2151 @raise errors.HypervisorError: in one of the previous cases
2152
2153 """
2154 if dev_type == constants.HOTPLUG_TARGET_DISK:
2155 hvp = instance.hvparams
2156 security_model = hvp[constants.HV_SECURITY_MODEL]
2157 use_chroot = hvp[constants.HV_KVM_USE_CHROOT]
2158 if action == constants.HOTPLUG_ACTION_ADD:
2159 if use_chroot:
2160 raise errors.HotplugError("Disk hotplug is not supported"
2161 " in case of chroot.")
2162 if security_model != constants.HT_SM_NONE:
2163 raise errors.HotplugError("Disk Hotplug is not supported in case"
2164 " security models are used.")
2165
2166 if (dev_type == constants.HOTPLUG_TARGET_NIC and
2167 action == constants.HOTPLUG_ACTION_ADD and not fdsend):
2168 raise errors.HotplugError("Cannot hot-add NIC."
2169 " fdsend python module is missing.")
2170
2171 def HotplugSupported(self, instance):
2172 """Checks if hotplug is generally supported.
2173
2174 Hotplug is *not* supported in case of:
2175 - qemu versions < 1.0
2176 - for stopped instances
2177
2178 @raise errors.HypervisorError: in one of the previous cases
2179
2180 """
2181 try:
2182 output = self._CallMonitorCommand(instance.name, self._INFO_VERSION_CMD)
2183 except errors.HypervisorError:
2184 raise errors.HotplugError("Instance is probably down")
2185
2186 # TODO: search for netdev_add, drive_add, device_add.....
2187 match = self._INFO_VERSION_RE.search(output.stdout)
2188 if not match:
2189 raise errors.HotplugError("Cannot parse qemu version via monitor")
2190
2191 v_major, v_min, _, _ = match.groups()
2192 if (int(v_major), int(v_min)) < (1, 0):
2193 raise errors.HotplugError("Hotplug not supported for qemu versions < 1.0")
2194
2195 def _CallHotplugCommands(self, name, cmds):
2196 for c in cmds:
2197 self._CallMonitorCommand(name, c)
2198 time.sleep(1)
2199
2200 def _VerifyHotplugCommand(self, instance_name, device, dev_type,
2201 should_exist):
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 # in case of hot-mod this is given
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 # update relevant entries in runtime file
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 # putting it back in the same pci slot
2295 device.pci = self.HotDelDevice(instance, dev_type, device, _, seq)
2296 self.HotAddDevice(instance, dev_type, device, _, seq)
2297
2298 def _PassTapFd(self, instance, fd, nic):
2299 """Pass file descriptor to kvm process via monitor socket using SCM_RIGHTS
2300
2301 """
2302 # TODO: factor out code related to unix sockets.
2303 # squash common parts between monitor and qmp
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
2316 def _ParseKVMVersion(cls, text):
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
2339 def _GetKVMOutput(cls, kvm_path, option):
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
2361 def _GetKVMVersion(cls, kvm_path):
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
2371 def _GetDefaultMachineVersion(cls, kvm_path):
2372 """Return the default hardware revision (e.g. pc-1.1)
2373
2374 """
2375 output = cls._GetKVMOutput(kvm_path, cls._KVMOPT_MLIST)
2376 match = cls._DEFAULT_MACHINE_VERSION_RE.search(output)
2377 if match:
2378 return match.group(1)
2379 else:
2380 return "pc"
2381
2382 @classmethod
2383 def _StopInstance(cls, instance, force=False, name=None, timeout=None):
2384 """Stop an instance.
2385
2386 """
2387 assert(timeout is None or force is not None)
2388
2389 if name is not None and not force:
2390 raise errors.HypervisorError("Cannot shutdown cleanly by name only")
2391 if name is None:
2392 name = instance.name
2393 acpi = instance.hvparams[constants.HV_ACPI]
2394 else:
2395 acpi = False
2396 _, pid, alive = cls._InstancePidAlive(name)
2397 if pid > 0 and alive:
2398 if force or not acpi:
2399 utils.KillProcess(pid)
2400 else:
2401 cls._CallMonitorCommand(name, "system_powerdown", timeout)
2402 cls._ClearUserShutdown(instance.name)
2403
2404 def StopInstance(self, instance, force=False, retry=False, name=None,
2405 timeout=None):
2406 """Stop an instance.
2407
2408 """
2409 self._StopInstance(instance, force, name=name, timeout=timeout)
2410
2411 def CleanupInstance(self, instance_name):
2412 """Cleanup after a stopped instance
2413
2414 """
2415 pidfile, pid, alive = self._InstancePidAlive(instance_name)
2416 if pid > 0 and alive:
2417 raise errors.HypervisorError("Cannot cleanup a live instance")
2418 self._RemoveInstanceRuntimeFiles(pidfile, instance_name)
2419 self._ClearUserShutdown(instance_name)
2420
2421 def RebootInstance(self, instance):
2422 """Reboot an instance.
2423
2424 """
2425 # For some reason if we do a 'send-key ctrl-alt-delete' to the control
2426 # socket the instance will stop, but now power up again. So we'll resort
2427 # to shutdown and restart.
2428 _, _, alive = self._InstancePidAlive(instance.name)
2429 if not alive:
2430 raise errors.HypervisorError("Failed to reboot instance %s:"
2431 " not running" % instance.name)
2432 # StopInstance will delete the saved KVM runtime so:
2433 # ...first load it...
2434 kvm_runtime = self._LoadKVMRuntime(instance)
2435 # ...now we can safely call StopInstance...
2436 if not self.StopInstance(instance):
2437 self.StopInstance(instance, force=True)
2438 # ...and finally we can save it again, and execute it...
2439 self._SaveKVMRuntime(instance, kvm_runtime)
2440 kvmpath = instance.hvparams[constants.HV_KVM_PATH]
2441 kvmhelp = self._GetKVMOutput(kvmpath, self._KVMOPT_HELP)
2442 self._ExecuteKVMRuntime(instance, kvm_runtime, kvmhelp)
2443
2444 def MigrationInfo(self, instance):
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
2455 def AcceptInstance(self, instance, info, target):
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
2473 def FinalizeMigrationDst(self, instance, info, success):
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 # Bridged/OVS interfaces have already been configured
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
2505 def MigrateInstance(self, cluster_name, instance, target, live):
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
2541 def FinalizeMigrationSource(self, instance, success, live):
2542 """Finalize the instance migration on the source node.
2543
2544 @type instance: L{objects.Instance}
2545 @param instance: the instance that was migrated
2546 @type success: bool
2547 @param success: whether the migration succeeded or not
2548 @type live: bool
2549 @param live: whether the user requested a live migration or not
2550
2551 """
2552 if success:
2553 pidfile, pid, _ = self._InstancePidAlive(instance.name)
2554 utils.KillProcess(pid)
2555 self._RemoveInstanceRuntimeFiles(pidfile, instance.name)
2556 elif live:
2557 self._CallMonitorCommand(instance.name, self._CONT_CMD)
2558 self._ClearUserShutdown(instance.name)
2559
2560 def GetMigrationStatus(self, instance):
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
2598 def BalloonInstanceMemory(self, instance, mem):
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
2609 def GetNodeInfo(self, hvparams=None):
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
2694 def CheckParameterSyntax(cls, hvparams):
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 # if an IP version is specified, the spice_bind parameter must be an
2746 # IP of that family
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 # All the other SPICE parameters depend on spice_bind being set. Raise an
2760 # error if any of them is set without it.
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
2767 def ValidateParameters(cls, hvparams):
2768 """Check the given parameters for validity.
2769
2770 @type hvparams: dict of strings
2771 @param hvparams: hypervisor parameters
2772 @raise errors.HypervisorError: when a parameter is not valid
2773
2774 """
2775 super(KVMHypervisor, cls).ValidateParameters(hvparams)
2776
2777 kvm_path = hvparams[constants.HV_KVM_PATH]
2778
2779 security_model = hvparams[constants.HV_SECURITY_MODEL]
2780 if security_model == constants.HT_SM_USER:
2781 username = hvparams[constants.HV_SECURITY_DOMAIN]
2782 try:
2783 pwd.getpwnam(username)
2784 except KeyError:
2785 raise errors.HypervisorError("Unknown security domain user %s"
2786 % username)
2787 vnc_bind_address = hvparams[constants.HV_VNC_BIND_ADDRESS]
2788 if vnc_bind_address:
2789 bound_to_addr = netutils.IP4Address.IsValid(vnc_bind_address)
2790 is_interface = netutils.IsValidInterface(vnc_bind_address)
2791 is_path = utils.IsNormAbsPath(vnc_bind_address)
2792 if not bound_to_addr and not is_interface and not is_path:
2793 raise errors.HypervisorError("VNC: The %s parameter must be either"
2794 " a valid IP address, an interface name,"
2795 " or an absolute path" %
2796 constants.HV_KVM_SPICE_BIND)
2797
2798 spice_bind = hvparams[constants.HV_KVM_SPICE_BIND]
2799 if spice_bind:
2800 # only one of VNC and SPICE can be used currently.
2801 if hvparams[constants.HV_VNC_BIND_ADDRESS]:
2802 raise errors.HypervisorError("Both SPICE and VNC are configured, but"
2803 " only one of them can be used at a"
2804 " given time")
2805
2806 # check that KVM supports SPICE
2807 kvmhelp = cls._GetKVMOutput(kvm_path, cls._KVMOPT_HELP)
2808 if not cls._SPICE_RE.search(kvmhelp):
2809 raise errors.HypervisorError("SPICE is configured, but it is not"
2810 " supported according to 'kvm --help'")
2811
2812 # if spice_bind is not an IP address, it must be a valid interface
2813 bound_to_addr = (netutils.IP4Address.IsValid(spice_bind) or
2814 netutils.IP6Address.IsValid(spice_bind))
2815 if not bound_to_addr and not netutils.IsValidInterface(spice_bind):
2816 raise errors.HypervisorError("SPICE: The %s parameter must be either"
2817 " a valid IP address or interface name" %
2818 constants.HV_KVM_SPICE_BIND)
2819
2820 machine_version = hvparams[constants.HV_KVM_MACHINE_VERSION]
2821 if machine_version:
2822 output = cls._GetKVMOutput(kvm_path, cls._KVMOPT_MLIST)
2823 if not cls._CHECK_MACHINE_VERSION_RE(machine_version).search(output):
2824 raise errors.HypervisorError("Unsupported machine version: %s" %
2825 machine_version)
2826
2827 @classmethod
2828 def PowercycleNode(cls, hvparams=None):
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()