Revert "Added SPICE TLS option and related cert paths"
[ganeti-local] / lib / hypervisor / hv_kvm.py
1 #
2 #
3
4 # Copyright (C) 2008, 2009, 2010, 2011 Google Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21
22 """KVM hypervisor
23
24 """
25
26 import errno
27 import os
28 import os.path
29 import re
30 import tempfile
31 import time
32 import logging
33 import pwd
34 import struct
35 import fcntl
36 import shutil
37 import socket
38 import StringIO
39
40 from ganeti import utils
41 from ganeti import constants
42 from ganeti import errors
43 from ganeti import serializer
44 from ganeti import objects
45 from ganeti import uidpool
46 from ganeti import ssconf
47 from ganeti.hypervisor import hv_base
48 from ganeti import netutils
49 from ganeti.utils import wrapper as utils_wrapper
50
51
52 _KVM_NETWORK_SCRIPT = constants.SYSCONFDIR + "/ganeti/kvm-vif-bridge"
53
54 # TUN/TAP driver constants, taken from <linux/if_tun.h>
55 # They are architecture-independent and already hardcoded in qemu-kvm source,
56 # so we can safely include them here.
57 TUNSETIFF = 0x400454ca
58 TUNGETIFF = 0x800454d2
59 TUNGETFEATURES = 0x800454cf
60 IFF_TAP = 0x0002
61 IFF_NO_PI = 0x1000
62 IFF_VNET_HDR = 0x4000
63
64
65 def _ProbeTapVnetHdr(fd):
66   """Check whether to enable the IFF_VNET_HDR flag.
67
68   To do this, _all_ of the following conditions must be met:
69    1. TUNGETFEATURES ioctl() *must* be implemented
70    2. TUNGETFEATURES ioctl() result *must* contain the IFF_VNET_HDR flag
71    3. TUNGETIFF ioctl() *must* be implemented; reading the kernel code in
72       drivers/net/tun.c there is no way to test this until after the tap device
73       has been created using TUNSETIFF, and there is no way to change the
74       IFF_VNET_HDR flag after creating the interface, catch-22! However both
75       TUNGETIFF and TUNGETFEATURES were introduced in kernel version 2.6.27,
76       thus we can expect TUNGETIFF to be present if TUNGETFEATURES is.
77
78    @type fd: int
79    @param fd: the file descriptor of /dev/net/tun
80
81   """
82   req = struct.pack("I", 0)
83   try:
84     res = fcntl.ioctl(fd, TUNGETFEATURES, req)
85   except EnvironmentError:
86     logging.warning("TUNGETFEATURES ioctl() not implemented")
87     return False
88
89   tunflags = struct.unpack("I", res)[0]
90   if tunflags & IFF_VNET_HDR:
91     return True
92   else:
93     logging.warning("Host does not support IFF_VNET_HDR, not enabling")
94     return False
95
96
97 def _OpenTap(vnet_hdr=True):
98   """Open a new tap device and return its file descriptor.
99
100   This is intended to be used by a qemu-type hypervisor together with the -net
101   tap,fd=<fd> command line parameter.
102
103   @type vnet_hdr: boolean
104   @param vnet_hdr: Enable the VNET Header
105   @return: (ifname, tapfd)
106   @rtype: tuple
107
108   """
109   try:
110     tapfd = os.open("/dev/net/tun", os.O_RDWR)
111   except EnvironmentError:
112     raise errors.HypervisorError("Failed to open /dev/net/tun")
113
114   flags = IFF_TAP | IFF_NO_PI
115
116   if vnet_hdr and _ProbeTapVnetHdr(tapfd):
117     flags |= IFF_VNET_HDR
118
119   # The struct ifreq ioctl request (see netdevice(7))
120   ifr = struct.pack("16sh", "", flags)
121
122   try:
123     res = fcntl.ioctl(tapfd, TUNSETIFF, ifr)
124   except EnvironmentError:
125     raise errors.HypervisorError("Failed to allocate a new TAP device")
126
127   # Get the interface name from the ioctl
128   ifname = struct.unpack("16sh", res)[0].strip("\x00")
129   return (ifname, tapfd)
130
131
132 class QmpMessage:
133   """QEMU Messaging Protocol (QMP) message.
134
135   """
136
137   def __init__(self, data):
138     """Creates a new QMP message based on the passed data.
139
140     """
141     if not isinstance(data, dict):
142       raise TypeError("QmpMessage must be initialized with a dict")
143
144     self.data = data
145
146   def __getitem__(self, field_name):
147     """Get the value of the required field if present, or None.
148
149     Overrides the [] operator to provide access to the message data,
150     returning None if the required item is not in the message
151     @return: the value of the field_name field, or None if field_name
152              is not contained in the message
153
154     """
155
156     if field_name in self.data:
157       return self.data[field_name]
158
159     return None
160
161   def __setitem__(self, field_name, field_value):
162     """Set the value of the required field_name to field_value.
163
164     """
165     self.data[field_name] = field_value
166
167   @staticmethod
168   def BuildFromJsonString(json_string):
169     """Build a QmpMessage from a JSON encoded string.
170
171     @type json_string: str
172     @param json_string: JSON string representing the message
173     @rtype: L{QmpMessage}
174     @return: a L{QmpMessage} built from json_string
175
176     """
177     # Parse the string
178     data = serializer.LoadJson(json_string)
179     return QmpMessage(data)
180
181   def __str__(self):
182     # The protocol expects the JSON object to be sent as a single
183     # line, hence the need for indent=False.
184     return serializer.DumpJson(self.data, indent=False)
185
186   def __eq__(self, other):
187     # When comparing two QmpMessages, we are interested in comparing
188     # their internal representation of the message data
189     return self.data == other.data
190
191
192 class QmpConnection:
193   """Connection to the QEMU Monitor using the QEMU Monitor Protocol (QMP).
194
195   """
196   _FIRST_MESSAGE_KEY = "QMP"
197   _EVENT_KEY = "event"
198   _ERROR_KEY = "error"
199   _ERROR_CLASS_KEY = "class"
200   _ERROR_DATA_KEY = "data"
201   _ERROR_DESC_KEY = "desc"
202   _EXECUTE_KEY = "execute"
203   _ARGUMENTS_KEY = "arguments"
204   _CAPABILITIES_COMMAND = "qmp_capabilities"
205   _MESSAGE_END_TOKEN = "\r\n"
206   _SOCKET_TIMEOUT = 5
207
208   def __init__(self, monitor_filename):
209     """Instantiates the QmpConnection object.
210
211     @type monitor_filename: string
212     @param monitor_filename: the filename of the UNIX raw socket on which the
213                              QMP monitor is listening
214
215     """
216     self.monitor_filename = monitor_filename
217     self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
218     # We want to fail if the server doesn't send a complete message
219     # in a reasonable amount of time
220     self.sock.settimeout(self._SOCKET_TIMEOUT)
221     self._connected = False
222     self._buf = ""
223
224   def _check_connection(self):
225     """Make sure that the connection is established.
226
227     """
228     if not self._connected:
229       raise errors.ProgrammerError("To use a QmpConnection you need to first"
230                                    " invoke connect() on it")
231
232   def connect(self):
233     """Connects to the QMP monitor.
234
235     Connects to the UNIX socket and makes sure that we can actually send and
236     receive data to the kvm instance via QMP.
237
238     @raise errors.HypervisorError: when there are communication errors
239     @raise errors.ProgrammerError: when there are data serialization errors
240
241     """
242     self.sock.connect(self.monitor_filename)
243     self._connected = True
244
245     # Check if we receive a correct greeting message from the server
246     # (As per the QEMU Protocol Specification 0.1 - section 2.2)
247     greeting = self._Recv()
248     if not greeting[self._FIRST_MESSAGE_KEY]:
249       self._connected = False
250       raise errors.HypervisorError("kvm: qmp communication error (wrong"
251                                    " server greeting")
252
253     # Let's put the monitor in command mode using the qmp_capabilities
254     # command, or else no command will be executable.
255     # (As per the QEMU Protocol Specification 0.1 - section 4)
256     self.Execute(self._CAPABILITIES_COMMAND)
257
258   def _ParseMessage(self, buf):
259     """Extract and parse a QMP message from the given buffer.
260
261     Seeks for a QMP message in the given buf. If found, it parses it and
262     returns it together with the rest of the characters in the buf.
263     If no message is found, returns None and the whole buffer.
264
265     @raise errors.ProgrammerError: when there are data serialization errors
266
267     """
268     message = None
269     # Check if we got the message end token (CRLF, as per the QEMU Protocol
270     # Specification 0.1 - Section 2.1.1)
271     pos = buf.find(self._MESSAGE_END_TOKEN)
272     if pos >= 0:
273       try:
274         message = QmpMessage.BuildFromJsonString(buf[:pos + 1])
275       except Exception, err:
276         raise errors.ProgrammerError("QMP data serialization error: %s" % err)
277       buf = buf[pos + 1:]
278
279     return (message, buf)
280
281   def _Recv(self):
282     """Receives a message from QMP and decodes the received JSON object.
283
284     @rtype: QmpMessage
285     @return: the received message
286     @raise errors.HypervisorError: when there are communication errors
287     @raise errors.ProgrammerError: when there are data serialization errors
288
289     """
290     self._check_connection()
291
292     # Check if there is already a message in the buffer
293     (message, self._buf) = self._ParseMessage(self._buf)
294     if message:
295       return message
296
297     recv_buffer = StringIO.StringIO(self._buf)
298     recv_buffer.seek(len(self._buf))
299     try:
300       while True:
301         data = self.sock.recv(4096)
302         if not data:
303           break
304         recv_buffer.write(data)
305
306         (message, self._buf) = self._ParseMessage(recv_buffer.getvalue())
307         if message:
308           return message
309
310     except socket.timeout, err:
311       raise errors.HypervisorError("Timeout while receiving a QMP message: "
312                                    "%s" % (err))
313     except socket.error, err:
314       raise errors.HypervisorError("Unable to receive data from KVM using the"
315                                    " QMP protocol: %s" % err)
316
317   def _Send(self, message):
318     """Encodes and sends a message to KVM using QMP.
319
320     @type message: QmpMessage
321     @param message: message to send to KVM
322     @raise errors.HypervisorError: when there are communication errors
323     @raise errors.ProgrammerError: when there are data serialization errors
324
325     """
326     self._check_connection()
327     try:
328       message_str = str(message)
329     except Exception, err:
330       raise errors.ProgrammerError("QMP data deserialization error: %s" % err)
331
332     try:
333       self.sock.sendall(message_str)
334     except socket.timeout, err:
335       raise errors.HypervisorError("Timeout while sending a QMP message: "
336                                    "%s (%s)" % (err.string, err.errno))
337     except socket.error, err:
338       raise errors.HypervisorError("Unable to send data from KVM using the"
339                                    " QMP protocol: %s" % err)
340
341   def Execute(self, command, arguments=None):
342     """Executes a QMP command and returns the response of the server.
343
344     @type command: str
345     @param command: the command to execute
346     @type arguments: dict
347     @param arguments: dictionary of arguments to be passed to the command
348     @rtype: dict
349     @return: dictionary representing the received JSON object
350     @raise errors.HypervisorError: when there are communication errors
351     @raise errors.ProgrammerError: when there are data serialization errors
352
353     """
354     self._check_connection()
355     message = QmpMessage({self._EXECUTE_KEY: command})
356     if arguments:
357       message[self._ARGUMENTS_KEY] = arguments
358     self._Send(message)
359
360     # Events can occur between the sending of the command and the reception
361     # of the response, so we need to filter out messages with the event key.
362     while True:
363       response = self._Recv()
364       err = response[self._ERROR_KEY]
365       if err:
366         raise errors.HypervisorError("kvm: error executing the %s"
367                                      " command: %s (%s, %s):" %
368                                      (command,
369                                       err[self._ERROR_DESC_KEY],
370                                       err[self._ERROR_CLASS_KEY],
371                                       err[self._ERROR_DATA_KEY]))
372
373       elif not response[self._EVENT_KEY]:
374         return response
375
376
377 class KVMHypervisor(hv_base.BaseHypervisor):
378   """KVM hypervisor interface"""
379   CAN_MIGRATE = True
380
381   _ROOT_DIR = constants.RUN_GANETI_DIR + "/kvm-hypervisor"
382   _PIDS_DIR = _ROOT_DIR + "/pid" # contains live instances pids
383   _UIDS_DIR = _ROOT_DIR + "/uid" # contains instances reserved uids
384   _CTRL_DIR = _ROOT_DIR + "/ctrl" # contains instances control sockets
385   _CONF_DIR = _ROOT_DIR + "/conf" # contains instances startup data
386   _NICS_DIR = _ROOT_DIR + "/nic" # contains instances nic <-> tap associations
387   _KEYMAP_DIR = _ROOT_DIR + "/keymap" # contains instances keymaps
388   # KVM instances with chroot enabled are started in empty chroot directories.
389   _CHROOT_DIR = _ROOT_DIR + "/chroot" # for empty chroot directories
390   # After an instance is stopped, its chroot directory is removed.
391   # If the chroot directory is not empty, it can't be removed.
392   # A non-empty chroot directory indicates a possible security incident.
393   # To support forensics, the non-empty chroot directory is quarantined in
394   # a separate directory, called 'chroot-quarantine'.
395   _CHROOT_QUARANTINE_DIR = _ROOT_DIR + "/chroot-quarantine"
396   _DIRS = [_ROOT_DIR, _PIDS_DIR, _UIDS_DIR, _CTRL_DIR, _CONF_DIR, _NICS_DIR,
397            _CHROOT_DIR, _CHROOT_QUARANTINE_DIR]
398
399   PARAMETERS = {
400     constants.HV_KERNEL_PATH: hv_base.OPT_FILE_CHECK,
401     constants.HV_INITRD_PATH: hv_base.OPT_FILE_CHECK,
402     constants.HV_ROOT_PATH: hv_base.NO_CHECK,
403     constants.HV_KERNEL_ARGS: hv_base.NO_CHECK,
404     constants.HV_ACPI: hv_base.NO_CHECK,
405     constants.HV_SERIAL_CONSOLE: hv_base.NO_CHECK,
406     constants.HV_VNC_BIND_ADDRESS:
407       (False, lambda x: (netutils.IP4Address.IsValid(x) or
408                          utils.IsNormAbsPath(x)),
409        "the VNC bind address must be either a valid IP address or an absolute"
410        " pathname", None, None),
411     constants.HV_VNC_TLS: hv_base.NO_CHECK,
412     constants.HV_VNC_X509: hv_base.OPT_DIR_CHECK,
413     constants.HV_VNC_X509_VERIFY: hv_base.NO_CHECK,
414     constants.HV_VNC_PASSWORD_FILE: hv_base.OPT_FILE_CHECK,
415     constants.HV_KVM_SPICE_BIND: hv_base.NO_CHECK, # will be checked later
416     constants.HV_KVM_SPICE_IP_VERSION:
417       (False, lambda x: (x == constants.IFACE_NO_IP_VERSION_SPECIFIED or
418                          x in constants.VALID_IP_VERSIONS),
419        "the SPICE IP version should be 4 or 6",
420        None, None),
421     constants.HV_KVM_SPICE_PASSWORD_FILE: hv_base.OPT_FILE_CHECK,
422     constants.HV_KVM_SPICE_LOSSLESS_IMG_COMPR:
423       hv_base.ParamInSet(False,
424         constants.HT_KVM_SPICE_VALID_LOSSLESS_IMG_COMPR_OPTIONS),
425     constants.HV_KVM_SPICE_JPEG_IMG_COMPR:
426       hv_base.ParamInSet(False,
427         constants.HT_KVM_SPICE_VALID_LOSSY_IMG_COMPR_OPTIONS),
428     constants.HV_KVM_SPICE_ZLIB_GLZ_IMG_COMPR:
429       hv_base.ParamInSet(False,
430         constants.HT_KVM_SPICE_VALID_LOSSY_IMG_COMPR_OPTIONS),
431     constants.HV_KVM_SPICE_STREAMING_VIDEO_DETECTION:
432       hv_base.ParamInSet(False,
433         constants.HT_KVM_SPICE_VALID_VIDEO_STREAM_DETECTION_OPTIONS),
434     constants.HV_KVM_SPICE_AUDIO_COMPR: hv_base.NO_CHECK,
435     constants.HV_KVM_FLOPPY_IMAGE_PATH: hv_base.OPT_FILE_CHECK,
436     constants.HV_CDROM_IMAGE_PATH: hv_base.OPT_FILE_CHECK,
437     constants.HV_KVM_CDROM2_IMAGE_PATH: hv_base.OPT_FILE_CHECK,
438     constants.HV_BOOT_ORDER:
439       hv_base.ParamInSet(True, constants.HT_KVM_VALID_BO_TYPES),
440     constants.HV_NIC_TYPE:
441       hv_base.ParamInSet(True, constants.HT_KVM_VALID_NIC_TYPES),
442     constants.HV_DISK_TYPE:
443       hv_base.ParamInSet(True, constants.HT_KVM_VALID_DISK_TYPES),
444     constants.HV_KVM_CDROM_DISK_TYPE:
445       hv_base.ParamInSet(False, constants.HT_KVM_VALID_DISK_TYPES),
446     constants.HV_USB_MOUSE:
447       hv_base.ParamInSet(False, constants.HT_KVM_VALID_MOUSE_TYPES),
448     constants.HV_KEYMAP: hv_base.NO_CHECK,
449     constants.HV_MIGRATION_PORT: hv_base.REQ_NET_PORT_CHECK,
450     constants.HV_MIGRATION_BANDWIDTH: hv_base.NO_CHECK,
451     constants.HV_MIGRATION_DOWNTIME: hv_base.NO_CHECK,
452     constants.HV_MIGRATION_MODE: hv_base.MIGRATION_MODE_CHECK,
453     constants.HV_USE_LOCALTIME: hv_base.NO_CHECK,
454     constants.HV_DISK_CACHE:
455       hv_base.ParamInSet(True, constants.HT_VALID_CACHE_TYPES),
456     constants.HV_SECURITY_MODEL:
457       hv_base.ParamInSet(True, constants.HT_KVM_VALID_SM_TYPES),
458     constants.HV_SECURITY_DOMAIN: hv_base.NO_CHECK,
459     constants.HV_KVM_FLAG:
460       hv_base.ParamInSet(False, constants.HT_KVM_FLAG_VALUES),
461     constants.HV_VHOST_NET: hv_base.NO_CHECK,
462     constants.HV_KVM_USE_CHROOT: hv_base.NO_CHECK,
463     constants.HV_MEM_PATH: hv_base.OPT_DIR_CHECK,
464     constants.HV_REBOOT_BEHAVIOR:
465       hv_base.ParamInSet(True, constants.REBOOT_BEHAVIORS)
466     }
467
468   _MIGRATION_STATUS_RE = re.compile("Migration\s+status:\s+(\w+)",
469                                     re.M | re.I)
470   _MIGRATION_INFO_MAX_BAD_ANSWERS = 5
471   _MIGRATION_INFO_RETRY_DELAY = 2
472
473   _VERSION_RE = re.compile(r"\b(\d+)\.(\d+)\.(\d+)\b")
474
475   ANCILLARY_FILES = [
476     _KVM_NETWORK_SCRIPT,
477     ]
478
479   def __init__(self):
480     hv_base.BaseHypervisor.__init__(self)
481     # Let's make sure the directories we need exist, even if the RUN_DIR lives
482     # in a tmpfs filesystem or has been otherwise wiped out.
483     dirs = [(dname, constants.RUN_DIRS_MODE) for dname in self._DIRS]
484     utils.EnsureDirs(dirs)
485
486   @classmethod
487   def _InstancePidFile(cls, instance_name):
488     """Returns the instance pidfile.
489
490     """
491     return utils.PathJoin(cls._PIDS_DIR, instance_name)
492
493   @classmethod
494   def _InstanceUidFile(cls, instance_name):
495     """Returns the instance uidfile.
496
497     """
498     return utils.PathJoin(cls._UIDS_DIR, instance_name)
499
500   @classmethod
501   def _InstancePidInfo(cls, pid):
502     """Check pid file for instance information.
503
504     Check that a pid file is associated with an instance, and retrieve
505     information from its command line.
506
507     @type pid: string or int
508     @param pid: process id of the instance to check
509     @rtype: tuple
510     @return: (instance_name, memory, vcpus)
511     @raise errors.HypervisorError: when an instance cannot be found
512
513     """
514     alive = utils.IsProcessAlive(pid)
515     if not alive:
516       raise errors.HypervisorError("Cannot get info for pid %s" % pid)
517
518     cmdline_file = utils.PathJoin("/proc", str(pid), "cmdline")
519     try:
520       cmdline = utils.ReadFile(cmdline_file)
521     except EnvironmentError, err:
522       raise errors.HypervisorError("Can't open cmdline file for pid %s: %s" %
523                                    (pid, err))
524
525     instance = None
526     memory = 0
527     vcpus = 0
528
529     arg_list = cmdline.split("\x00")
530     while arg_list:
531       arg = arg_list.pop(0)
532       if arg == "-name":
533         instance = arg_list.pop(0)
534       elif arg == "-m":
535         memory = int(arg_list.pop(0))
536       elif arg == "-smp":
537         vcpus = int(arg_list.pop(0))
538
539     if instance is None:
540       raise errors.HypervisorError("Pid %s doesn't contain a ganeti kvm"
541                                    " instance" % pid)
542
543     return (instance, memory, vcpus)
544
545   def _InstancePidAlive(self, instance_name):
546     """Returns the instance pidfile, pid, and liveness.
547
548     @type instance_name: string
549     @param instance_name: instance name
550     @rtype: tuple
551     @return: (pid file name, pid, liveness)
552
553     """
554     pidfile = self._InstancePidFile(instance_name)
555     pid = utils.ReadPidFile(pidfile)
556
557     alive = False
558     try:
559       cmd_instance = self._InstancePidInfo(pid)[0]
560       alive = (cmd_instance == instance_name)
561     except errors.HypervisorError:
562       pass
563
564     return (pidfile, pid, alive)
565
566   def _CheckDown(self, instance_name):
567     """Raises an error unless the given instance is down.
568
569     """
570     alive = self._InstancePidAlive(instance_name)[2]
571     if alive:
572       raise errors.HypervisorError("Failed to start instance %s: %s" %
573                                    (instance_name, "already running"))
574
575   @classmethod
576   def _InstanceMonitor(cls, instance_name):
577     """Returns the instance monitor socket name
578
579     """
580     return utils.PathJoin(cls._CTRL_DIR, "%s.monitor" % instance_name)
581
582   @classmethod
583   def _InstanceSerial(cls, instance_name):
584     """Returns the instance serial socket name
585
586     """
587     return utils.PathJoin(cls._CTRL_DIR, "%s.serial" % instance_name)
588
589   @classmethod
590   def _InstanceQmpMonitor(cls, instance_name):
591     """Returns the instance serial QMP socket name
592
593     """
594     return utils.PathJoin(cls._CTRL_DIR, "%s.qmp" % instance_name)
595
596   @staticmethod
597   def _SocatUnixConsoleParams():
598     """Returns the correct parameters for socat
599
600     If we have a new-enough socat we can use raw mode with an escape character.
601
602     """
603     if constants.SOCAT_USE_ESCAPE:
604       return "raw,echo=0,escape=%s" % constants.SOCAT_ESCAPE_CODE
605     else:
606       return "echo=0,icanon=0"
607
608   @classmethod
609   def _InstanceKVMRuntime(cls, instance_name):
610     """Returns the instance KVM runtime filename
611
612     """
613     return utils.PathJoin(cls._CONF_DIR, "%s.runtime" % instance_name)
614
615   @classmethod
616   def _InstanceChrootDir(cls, instance_name):
617     """Returns the name of the KVM chroot dir of the instance
618
619     """
620     return utils.PathJoin(cls._CHROOT_DIR, instance_name)
621
622   @classmethod
623   def _InstanceNICDir(cls, instance_name):
624     """Returns the name of the directory holding the tap device files for a
625     given instance.
626
627     """
628     return utils.PathJoin(cls._NICS_DIR, instance_name)
629
630   @classmethod
631   def _InstanceNICFile(cls, instance_name, seq):
632     """Returns the name of the file containing the tap device for a given NIC
633
634     """
635     return utils.PathJoin(cls._InstanceNICDir(instance_name), str(seq))
636
637   @classmethod
638   def _InstanceKeymapFile(cls, instance_name):
639     """Returns the name of the file containing the keymap for a given instance
640
641     """
642     return utils.PathJoin(cls._KEYMAP_DIR, instance_name)
643
644   @classmethod
645   def _TryReadUidFile(cls, uid_file):
646     """Try to read a uid file
647
648     """
649     if os.path.exists(uid_file):
650       try:
651         uid = int(utils.ReadOneLineFile(uid_file))
652         return uid
653       except EnvironmentError:
654         logging.warning("Can't read uid file", exc_info=True)
655       except (TypeError, ValueError):
656         logging.warning("Can't parse uid file contents", exc_info=True)
657     return None
658
659   @classmethod
660   def _RemoveInstanceRuntimeFiles(cls, pidfile, instance_name):
661     """Removes an instance's rutime sockets/files/dirs.
662
663     """
664     utils.RemoveFile(pidfile)
665     utils.RemoveFile(cls._InstanceMonitor(instance_name))
666     utils.RemoveFile(cls._InstanceSerial(instance_name))
667     utils.RemoveFile(cls._InstanceQmpMonitor(instance_name))
668     utils.RemoveFile(cls._InstanceKVMRuntime(instance_name))
669     utils.RemoveFile(cls._InstanceKeymapFile(instance_name))
670     uid_file = cls._InstanceUidFile(instance_name)
671     uid = cls._TryReadUidFile(uid_file)
672     utils.RemoveFile(uid_file)
673     if uid is not None:
674       uidpool.ReleaseUid(uid)
675     try:
676       shutil.rmtree(cls._InstanceNICDir(instance_name))
677     except OSError, err:
678       if err.errno != errno.ENOENT:
679         raise
680     try:
681       chroot_dir = cls._InstanceChrootDir(instance_name)
682       utils.RemoveDir(chroot_dir)
683     except OSError, err:
684       if err.errno == errno.ENOTEMPTY:
685         # The chroot directory is expected to be empty, but it isn't.
686         new_chroot_dir = tempfile.mkdtemp(dir=cls._CHROOT_QUARANTINE_DIR,
687                                           prefix="%s-%s-" %
688                                           (instance_name,
689                                            utils.TimestampForFilename()))
690         logging.warning("The chroot directory of instance %s can not be"
691                         " removed as it is not empty. Moving it to the"
692                         " quarantine instead. Please investigate the"
693                         " contents (%s) and clean up manually",
694                         instance_name, new_chroot_dir)
695         utils.RenameFile(chroot_dir, new_chroot_dir)
696       else:
697         raise
698
699   @staticmethod
700   def _ConfigureNIC(instance, seq, nic, tap):
701     """Run the network configuration script for a specified NIC
702
703     @param instance: instance we're acting on
704     @type instance: instance object
705     @param seq: nic sequence number
706     @type seq: int
707     @param nic: nic we're acting on
708     @type nic: nic object
709     @param tap: the host's tap interface this NIC corresponds to
710     @type tap: str
711
712     """
713
714     if instance.tags:
715       tags = " ".join(instance.tags)
716     else:
717       tags = ""
718
719     env = {
720       "PATH": "%s:/sbin:/usr/sbin" % os.environ["PATH"],
721       "INSTANCE": instance.name,
722       "MAC": nic.mac,
723       "MODE": nic.nicparams[constants.NIC_MODE],
724       "INTERFACE": tap,
725       "INTERFACE_INDEX": str(seq),
726       "TAGS": tags,
727     }
728
729     if nic.ip:
730       env["IP"] = nic.ip
731
732     if nic.nicparams[constants.NIC_LINK]:
733       env["LINK"] = nic.nicparams[constants.NIC_LINK]
734
735     if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
736       env["BRIDGE"] = nic.nicparams[constants.NIC_LINK]
737
738     result = utils.RunCmd([constants.KVM_IFUP, tap], env=env)
739     if result.failed:
740       raise errors.HypervisorError("Failed to configure interface %s: %s."
741                                    " Network configuration script output: %s" %
742                                    (tap, result.fail_reason, result.output))
743
744   def ListInstances(self):
745     """Get the list of running instances.
746
747     We can do this by listing our live instances directory and
748     checking whether the associated kvm process is still alive.
749
750     """
751     result = []
752     for name in os.listdir(self._PIDS_DIR):
753       if self._InstancePidAlive(name)[2]:
754         result.append(name)
755     return result
756
757   def GetInstanceInfo(self, instance_name):
758     """Get instance properties.
759
760     @type instance_name: string
761     @param instance_name: the instance name
762     @rtype: tuple of strings
763     @return: (name, id, memory, vcpus, stat, times)
764
765     """
766     _, pid, alive = self._InstancePidAlive(instance_name)
767     if not alive:
768       return None
769
770     _, memory, vcpus = self._InstancePidInfo(pid)
771     stat = "---b-"
772     times = "0"
773
774     return (instance_name, pid, memory, vcpus, stat, times)
775
776   def GetAllInstancesInfo(self):
777     """Get properties of all instances.
778
779     @return: list of tuples (name, id, memory, vcpus, stat, times)
780
781     """
782     data = []
783     for name in os.listdir(self._PIDS_DIR):
784       try:
785         info = self.GetInstanceInfo(name)
786       except errors.HypervisorError:
787         continue
788       if info:
789         data.append(info)
790     return data
791
792   def _GenerateKVMRuntime(self, instance, block_devices, startup_paused):
793     """Generate KVM information to start an instance.
794
795     """
796     # pylint: disable=R0914
797     _, v_major, v_min, _ = self._GetKVMVersion()
798
799     pidfile = self._InstancePidFile(instance.name)
800     kvm = constants.KVM_PATH
801     kvm_cmd = [kvm]
802     # used just by the vnc server, if enabled
803     kvm_cmd.extend(["-name", instance.name])
804     kvm_cmd.extend(["-m", instance.beparams[constants.BE_MEMORY]])
805     kvm_cmd.extend(["-smp", instance.beparams[constants.BE_VCPUS]])
806     kvm_cmd.extend(["-pidfile", pidfile])
807     kvm_cmd.extend(["-daemonize"])
808     if not instance.hvparams[constants.HV_ACPI]:
809       kvm_cmd.extend(["-no-acpi"])
810     if startup_paused:
811       kvm_cmd.extend(["-S"])
812     if instance.hvparams[constants.HV_REBOOT_BEHAVIOR] == \
813         constants.INSTANCE_REBOOT_EXIT:
814       kvm_cmd.extend(["-no-reboot"])
815
816     hvp = instance.hvparams
817     boot_disk = hvp[constants.HV_BOOT_ORDER] == constants.HT_BO_DISK
818     boot_cdrom = hvp[constants.HV_BOOT_ORDER] == constants.HT_BO_CDROM
819     boot_floppy = hvp[constants.HV_BOOT_ORDER] == constants.HT_BO_FLOPPY
820     boot_network = hvp[constants.HV_BOOT_ORDER] == constants.HT_BO_NETWORK
821
822     self.ValidateParameters(hvp)
823
824     if hvp[constants.HV_KVM_FLAG] == constants.HT_KVM_ENABLED:
825       kvm_cmd.extend(["-enable-kvm"])
826     elif hvp[constants.HV_KVM_FLAG] == constants.HT_KVM_DISABLED:
827       kvm_cmd.extend(["-disable-kvm"])
828
829     if boot_network:
830       kvm_cmd.extend(["-boot", "n"])
831
832     disk_type = hvp[constants.HV_DISK_TYPE]
833     if disk_type == constants.HT_DISK_PARAVIRTUAL:
834       if_val = ",if=virtio"
835     else:
836       if_val = ",if=%s" % disk_type
837     # Cache mode
838     disk_cache = hvp[constants.HV_DISK_CACHE]
839     if instance.disk_template in constants.DTS_EXT_MIRROR:
840       if disk_cache != "none":
841         # TODO: make this a hard error, instead of a silent overwrite
842         logging.warning("KVM: overriding disk_cache setting '%s' with 'none'"
843                         " to prevent shared storage corruption on migration",
844                         disk_cache)
845       cache_val = ",cache=none"
846     elif disk_cache != constants.HT_CACHE_DEFAULT:
847       cache_val = ",cache=%s" % disk_cache
848     else:
849       cache_val = ""
850     for cfdev, dev_path in block_devices:
851       if cfdev.mode != constants.DISK_RDWR:
852         raise errors.HypervisorError("Instance has read-only disks which"
853                                      " are not supported by KVM")
854       # TODO: handle FD_LOOP and FD_BLKTAP (?)
855       boot_val = ""
856       if boot_disk:
857         kvm_cmd.extend(["-boot", "c"])
858         boot_disk = False
859         if (v_major, v_min) < (0, 14) and disk_type != constants.HT_DISK_IDE:
860           boot_val = ",boot=on"
861
862       drive_val = "file=%s,format=raw%s%s%s" % (dev_path, if_val, boot_val,
863                                                 cache_val)
864       kvm_cmd.extend(["-drive", drive_val])
865
866     #Now we can specify a different device type for CDROM devices.
867     cdrom_disk_type = hvp[constants.HV_KVM_CDROM_DISK_TYPE]
868     if not cdrom_disk_type:
869       cdrom_disk_type = disk_type
870
871     iso_image = hvp[constants.HV_CDROM_IMAGE_PATH]
872     if iso_image:
873       options = ",format=raw,media=cdrom"
874       if boot_cdrom:
875         kvm_cmd.extend(["-boot", "d"])
876         if cdrom_disk_type != constants.HT_DISK_IDE:
877           options = "%s,boot=on,if=%s" % (options, constants.HT_DISK_IDE)
878         else:
879           options = "%s,boot=on" % options
880       else:
881         if cdrom_disk_type == constants.HT_DISK_PARAVIRTUAL:
882           if_val = ",if=virtio"
883         else:
884           if_val = ",if=%s" % cdrom_disk_type
885         options = "%s%s" % (options, if_val)
886       drive_val = "file=%s%s" % (iso_image, options)
887       kvm_cmd.extend(["-drive", drive_val])
888
889     iso_image2 = hvp[constants.HV_KVM_CDROM2_IMAGE_PATH]
890     if iso_image2:
891       options = ",format=raw,media=cdrom"
892       if cdrom_disk_type == constants.HT_DISK_PARAVIRTUAL:
893         if_val = ",if=virtio"
894       else:
895         if_val = ",if=%s" % cdrom_disk_type
896       options = "%s%s" % (options, if_val)
897       drive_val = "file=%s%s" % (iso_image2, options)
898       kvm_cmd.extend(["-drive", drive_val])
899
900     floppy_image = hvp[constants.HV_KVM_FLOPPY_IMAGE_PATH]
901     if floppy_image:
902       options = ",format=raw,media=disk"
903       if boot_floppy:
904         kvm_cmd.extend(["-boot", "a"])
905         options = "%s,boot=on" % options
906       if_val = ",if=floppy"
907       options = "%s%s" % (options, if_val)
908       drive_val = "file=%s%s" % (floppy_image, options)
909       kvm_cmd.extend(["-drive", drive_val])
910
911     kernel_path = hvp[constants.HV_KERNEL_PATH]
912     if kernel_path:
913       kvm_cmd.extend(["-kernel", kernel_path])
914       initrd_path = hvp[constants.HV_INITRD_PATH]
915       if initrd_path:
916         kvm_cmd.extend(["-initrd", initrd_path])
917       root_append = ["root=%s" % hvp[constants.HV_ROOT_PATH],
918                      hvp[constants.HV_KERNEL_ARGS]]
919       if hvp[constants.HV_SERIAL_CONSOLE]:
920         root_append.append("console=ttyS0,38400")
921       kvm_cmd.extend(["-append", " ".join(root_append)])
922
923     mem_path = hvp[constants.HV_MEM_PATH]
924     if mem_path:
925       kvm_cmd.extend(["-mem-path", mem_path, "-mem-prealloc"])
926
927     mouse_type = hvp[constants.HV_USB_MOUSE]
928     vnc_bind_address = hvp[constants.HV_VNC_BIND_ADDRESS]
929
930     if mouse_type:
931       kvm_cmd.extend(["-usb"])
932       kvm_cmd.extend(["-usbdevice", mouse_type])
933     elif vnc_bind_address:
934       kvm_cmd.extend(["-usbdevice", constants.HT_MOUSE_TABLET])
935
936     keymap = hvp[constants.HV_KEYMAP]
937     if keymap:
938       keymap_path = self._InstanceKeymapFile(instance.name)
939       # If a keymap file is specified, KVM won't use its internal defaults. By
940       # first including the "en-us" layout, an error on loading the actual
941       # layout (e.g. because it can't be found) won't lead to a non-functional
942       # keyboard. A keyboard with incorrect keys is still better than none.
943       utils.WriteFile(keymap_path, data="include en-us\ninclude %s\n" % keymap)
944       kvm_cmd.extend(["-k", keymap_path])
945
946     if vnc_bind_address:
947       if netutils.IP4Address.IsValid(vnc_bind_address):
948         if instance.network_port > constants.VNC_BASE_PORT:
949           display = instance.network_port - constants.VNC_BASE_PORT
950           if vnc_bind_address == constants.IP4_ADDRESS_ANY:
951             vnc_arg = ":%d" % (display)
952           else:
953             vnc_arg = "%s:%d" % (vnc_bind_address, display)
954         else:
955           logging.error("Network port is not a valid VNC display (%d < %d)."
956                         " Not starting VNC", instance.network_port,
957                         constants.VNC_BASE_PORT)
958           vnc_arg = "none"
959
960         # Only allow tls and other option when not binding to a file, for now.
961         # kvm/qemu gets confused otherwise about the filename to use.
962         vnc_append = ""
963         if hvp[constants.HV_VNC_TLS]:
964           vnc_append = "%s,tls" % vnc_append
965           if hvp[constants.HV_VNC_X509_VERIFY]:
966             vnc_append = "%s,x509verify=%s" % (vnc_append,
967                                                hvp[constants.HV_VNC_X509])
968           elif hvp[constants.HV_VNC_X509]:
969             vnc_append = "%s,x509=%s" % (vnc_append,
970                                          hvp[constants.HV_VNC_X509])
971         if hvp[constants.HV_VNC_PASSWORD_FILE]:
972           vnc_append = "%s,password" % vnc_append
973
974         vnc_arg = "%s%s" % (vnc_arg, vnc_append)
975
976       else:
977         vnc_arg = "unix:%s/%s.vnc" % (vnc_bind_address, instance.name)
978
979       kvm_cmd.extend(["-vnc", vnc_arg])
980     else:
981       kvm_cmd.extend(["-nographic"])
982
983     monitor_dev = ("unix:%s,server,nowait" %
984                    self._InstanceMonitor(instance.name))
985     kvm_cmd.extend(["-monitor", monitor_dev])
986     if hvp[constants.HV_SERIAL_CONSOLE]:
987       serial_dev = ("unix:%s,server,nowait" %
988                     self._InstanceSerial(instance.name))
989       kvm_cmd.extend(["-serial", serial_dev])
990     else:
991       kvm_cmd.extend(["-serial", "none"])
992
993     spice_bind = hvp[constants.HV_KVM_SPICE_BIND]
994     spice_ip_version = None
995     if spice_bind:
996       if netutils.IsValidInterface(spice_bind):
997         # The user specified a network interface, we have to figure out the IP
998         # address.
999         addresses = netutils.GetInterfaceIpAddresses(spice_bind)
1000         spice_ip_version = hvp[constants.HV_KVM_SPICE_IP_VERSION]
1001
1002         # if the user specified an IP version and the interface does not
1003         # have that kind of IP addresses, throw an exception
1004         if spice_ip_version != constants.IFACE_NO_IP_VERSION_SPECIFIED:
1005           if not addresses[spice_ip_version]:
1006             raise errors.HypervisorError("spice: unable to get an IPv%s address"
1007                                          " for %s" % (spice_ip_version,
1008                                                       spice_bind))
1009
1010         # the user did not specify an IP version, we have to figure it out
1011         elif (addresses[constants.IP4_VERSION] and
1012               addresses[constants.IP6_VERSION]):
1013           # we have both ipv4 and ipv6, let's use the cluster default IP
1014           # version
1015           cluster_family = ssconf.SimpleStore().GetPrimaryIPFamily()
1016           spice_ip_version = netutils.IPAddress.GetVersionFromAddressFamily(
1017               cluster_family)
1018         elif addresses[constants.IP4_VERSION]:
1019           spice_ip_version = constants.IP4_VERSION
1020         elif addresses[constants.IP6_VERSION]:
1021           spice_ip_version = constants.IP6_VERSION
1022         else:
1023           raise errors.HypervisorError("spice: unable to get an IP address"
1024                                        " for %s" % (spice_bind))
1025
1026         spice_address = addresses[spice_ip_version][0]
1027
1028       else:
1029         # spice_bind is known to be a valid IP address, because
1030         # ValidateParameters checked it.
1031         spice_address = spice_bind
1032
1033       spice_arg = "addr=%s,port=%s" % (spice_address, instance.network_port)
1034       if not hvp[constants.HV_KVM_SPICE_PASSWORD_FILE]:
1035         spice_arg = "%s,disable-ticketing" % spice_arg
1036
1037       if spice_ip_version:
1038         spice_arg = "%s,ipv%s" % (spice_arg, spice_ip_version)
1039
1040       # Image compression options
1041       img_lossless = hvp[constants.HV_KVM_SPICE_LOSSLESS_IMG_COMPR]
1042       img_jpeg = hvp[constants.HV_KVM_SPICE_JPEG_IMG_COMPR]
1043       img_zlib_glz = hvp[constants.HV_KVM_SPICE_ZLIB_GLZ_IMG_COMPR]
1044       if img_lossless:
1045         spice_arg = "%s,image-compression=%s" % (spice_arg, img_lossless)
1046       if img_jpeg:
1047         spice_arg = "%s,jpeg-wan-compression=%s" % (spice_arg, img_jpeg)
1048       if img_zlib_glz:
1049         spice_arg = "%s,zlib-glz-wan-compression=%s" % (spice_arg, img_zlib_glz)
1050
1051       # Video stream detection
1052       video_streaming = hvp[constants.HV_KVM_SPICE_STREAMING_VIDEO_DETECTION]
1053       if video_streaming:
1054         spice_arg = "%s,streaming-video=%s" % (spice_arg, video_streaming)
1055
1056       # Audio compression, by default in qemu-kvm it is on
1057       if not hvp[constants.HV_KVM_SPICE_AUDIO_COMPR]:
1058         spice_arg = "%s,playback-compression=off" % spice_arg
1059
1060       logging.info("KVM: SPICE will listen on port %s", instance.network_port)
1061       kvm_cmd.extend(["-spice", spice_arg])
1062
1063       # Tell kvm to use the paravirtualized graphic card, optimized for SPICE
1064       kvm_cmd.extend(["-vga", "qxl"])
1065
1066     if hvp[constants.HV_USE_LOCALTIME]:
1067       kvm_cmd.extend(["-localtime"])
1068
1069     if hvp[constants.HV_KVM_USE_CHROOT]:
1070       kvm_cmd.extend(["-chroot", self._InstanceChrootDir(instance.name)])
1071
1072     # Save the current instance nics, but defer their expansion as parameters,
1073     # as we'll need to generate executable temp files for them.
1074     kvm_nics = instance.nics
1075     hvparams = hvp
1076
1077     return (kvm_cmd, kvm_nics, hvparams)
1078
1079   def _WriteKVMRuntime(self, instance_name, data):
1080     """Write an instance's KVM runtime
1081
1082     """
1083     try:
1084       utils.WriteFile(self._InstanceKVMRuntime(instance_name),
1085                       data=data)
1086     except EnvironmentError, err:
1087       raise errors.HypervisorError("Failed to save KVM runtime file: %s" % err)
1088
1089   def _ReadKVMRuntime(self, instance_name):
1090     """Read an instance's KVM runtime
1091
1092     """
1093     try:
1094       file_content = utils.ReadFile(self._InstanceKVMRuntime(instance_name))
1095     except EnvironmentError, err:
1096       raise errors.HypervisorError("Failed to load KVM runtime file: %s" % err)
1097     return file_content
1098
1099   def _SaveKVMRuntime(self, instance, kvm_runtime):
1100     """Save an instance's KVM runtime
1101
1102     """
1103     kvm_cmd, kvm_nics, hvparams = kvm_runtime
1104     serialized_nics = [nic.ToDict() for nic in kvm_nics]
1105     serialized_form = serializer.Dump((kvm_cmd, serialized_nics, hvparams))
1106     self._WriteKVMRuntime(instance.name, serialized_form)
1107
1108   def _LoadKVMRuntime(self, instance, serialized_runtime=None):
1109     """Load an instance's KVM runtime
1110
1111     """
1112     if not serialized_runtime:
1113       serialized_runtime = self._ReadKVMRuntime(instance.name)
1114     loaded_runtime = serializer.Load(serialized_runtime)
1115     kvm_cmd, serialized_nics, hvparams = loaded_runtime
1116     kvm_nics = [objects.NIC.FromDict(snic) for snic in serialized_nics]
1117     return (kvm_cmd, kvm_nics, hvparams)
1118
1119   def _RunKVMCmd(self, name, kvm_cmd, tap_fds=None):
1120     """Run the KVM cmd and check for errors
1121
1122     @type name: string
1123     @param name: instance name
1124     @type kvm_cmd: list of strings
1125     @param kvm_cmd: runcmd input for kvm
1126     @type tap_fds: list of int
1127     @param tap_fds: fds of tap devices opened by Ganeti
1128
1129     """
1130     try:
1131       result = utils.RunCmd(kvm_cmd, noclose_fds=tap_fds)
1132     finally:
1133       for fd in tap_fds:
1134         utils_wrapper.CloseFdNoError(fd)
1135
1136     if result.failed:
1137       raise errors.HypervisorError("Failed to start instance %s: %s (%s)" %
1138                                    (name, result.fail_reason, result.output))
1139     if not self._InstancePidAlive(name)[2]:
1140       raise errors.HypervisorError("Failed to start instance %s" % name)
1141
1142   def _ExecuteKVMRuntime(self, instance, kvm_runtime, incoming=None):
1143     """Execute a KVM cmd, after completing it with some last minute data
1144
1145     @type incoming: tuple of strings
1146     @param incoming: (target_host_ip, port)
1147
1148     """
1149     # Small _ExecuteKVMRuntime hv parameters programming howto:
1150     #  - conf_hvp contains the parameters as configured on ganeti. they might
1151     #    have changed since the instance started; only use them if the change
1152     #    won't affect the inside of the instance (which hasn't been rebooted).
1153     #  - up_hvp contains the parameters as they were when the instance was
1154     #    started, plus any new parameter which has been added between ganeti
1155     #    versions: it is paramount that those default to a value which won't
1156     #    affect the inside of the instance as well.
1157     conf_hvp = instance.hvparams
1158     name = instance.name
1159     self._CheckDown(name)
1160
1161     temp_files = []
1162
1163     kvm_cmd, kvm_nics, up_hvp = kvm_runtime
1164     up_hvp = objects.FillDict(conf_hvp, up_hvp)
1165
1166     _, v_major, v_min, _ = self._GetKVMVersion()
1167
1168     # We know it's safe to run as a different user upon migration, so we'll use
1169     # the latest conf, from conf_hvp.
1170     security_model = conf_hvp[constants.HV_SECURITY_MODEL]
1171     if security_model == constants.HT_SM_USER:
1172       kvm_cmd.extend(["-runas", conf_hvp[constants.HV_SECURITY_DOMAIN]])
1173
1174     # We have reasons to believe changing something like the nic driver/type
1175     # upon migration won't exactly fly with the instance kernel, so for nic
1176     # related parameters we'll use up_hvp
1177     tapfds = []
1178     taps = []
1179     if not kvm_nics:
1180       kvm_cmd.extend(["-net", "none"])
1181     else:
1182       vnet_hdr = False
1183       tap_extra = ""
1184       nic_type = up_hvp[constants.HV_NIC_TYPE]
1185       if nic_type == constants.HT_NIC_PARAVIRTUAL:
1186         # From version 0.12.0, kvm uses a new sintax for network configuration.
1187         if (v_major, v_min) >= (0, 12):
1188           nic_model = "virtio-net-pci"
1189           vnet_hdr = True
1190         else:
1191           nic_model = "virtio"
1192
1193         if up_hvp[constants.HV_VHOST_NET]:
1194           # vhost_net is only available from version 0.13.0 or newer
1195           if (v_major, v_min) >= (0, 13):
1196             tap_extra = ",vhost=on"
1197           else:
1198             raise errors.HypervisorError("vhost_net is configured"
1199                                         " but it is not available")
1200       else:
1201         nic_model = nic_type
1202
1203       for nic_seq, nic in enumerate(kvm_nics):
1204         tapname, tapfd = _OpenTap(vnet_hdr)
1205         tapfds.append(tapfd)
1206         taps.append(tapname)
1207         if (v_major, v_min) >= (0, 12):
1208           nic_val = "%s,mac=%s,netdev=netdev%s" % (nic_model, nic.mac, nic_seq)
1209           tap_val = "type=tap,id=netdev%s,fd=%d%s" % (nic_seq, tapfd, tap_extra)
1210           kvm_cmd.extend(["-netdev", tap_val, "-device", nic_val])
1211         else:
1212           nic_val = "nic,vlan=%s,macaddr=%s,model=%s" % (nic_seq,
1213                                                          nic.mac, nic_model)
1214           tap_val = "tap,vlan=%s,fd=%d" % (nic_seq, tapfd)
1215           kvm_cmd.extend(["-net", tap_val, "-net", nic_val])
1216
1217     if incoming:
1218       target, port = incoming
1219       kvm_cmd.extend(["-incoming", "tcp:%s:%s" % (target, port)])
1220
1221     # Changing the vnc password doesn't bother the guest that much. At most it
1222     # will surprise people who connect to it. Whether positively or negatively
1223     # it's debatable.
1224     vnc_pwd_file = conf_hvp[constants.HV_VNC_PASSWORD_FILE]
1225     vnc_pwd = None
1226     if vnc_pwd_file:
1227       try:
1228         vnc_pwd = utils.ReadOneLineFile(vnc_pwd_file, strict=True)
1229       except EnvironmentError, err:
1230         raise errors.HypervisorError("Failed to open VNC password file %s: %s"
1231                                      % (vnc_pwd_file, err))
1232
1233     if conf_hvp[constants.HV_KVM_USE_CHROOT]:
1234       utils.EnsureDirs([(self._InstanceChrootDir(name),
1235                          constants.SECURE_DIR_MODE)])
1236
1237     # Automatically enable QMP if version is >= 0.14
1238     if (v_major, v_min) >= (0, 14):
1239       logging.debug("Enabling QMP")
1240       kvm_cmd.extend(["-qmp", "unix:%s,server,nowait" %
1241                     self._InstanceQmpMonitor(instance.name)])
1242
1243     # Configure the network now for starting instances and bridged interfaces,
1244     # during FinalizeMigration for incoming instances' routed interfaces
1245     for nic_seq, nic in enumerate(kvm_nics):
1246       if (incoming and
1247           nic.nicparams[constants.NIC_MODE] != constants.NIC_MODE_BRIDGED):
1248         continue
1249       self._ConfigureNIC(instance, nic_seq, nic, taps[nic_seq])
1250
1251     if security_model == constants.HT_SM_POOL:
1252       ss = ssconf.SimpleStore()
1253       uid_pool = uidpool.ParseUidPool(ss.GetUidPool(), separator="\n")
1254       all_uids = set(uidpool.ExpandUidPool(uid_pool))
1255       uid = uidpool.RequestUnusedUid(all_uids)
1256       try:
1257         username = pwd.getpwuid(uid.GetUid()).pw_name
1258         kvm_cmd.extend(["-runas", username])
1259         self._RunKVMCmd(name, kvm_cmd, tapfds)
1260       except:
1261         uidpool.ReleaseUid(uid)
1262         raise
1263       else:
1264         uid.Unlock()
1265         utils.WriteFile(self._InstanceUidFile(name), data=uid.AsStr())
1266     else:
1267       self._RunKVMCmd(name, kvm_cmd, tapfds)
1268
1269     utils.EnsureDirs([(self._InstanceNICDir(instance.name),
1270                      constants.RUN_DIRS_MODE)])
1271     for nic_seq, tap in enumerate(taps):
1272       utils.WriteFile(self._InstanceNICFile(instance.name, nic_seq),
1273                       data=tap)
1274
1275     if vnc_pwd:
1276       change_cmd = "change vnc password %s" % vnc_pwd
1277       self._CallMonitorCommand(instance.name, change_cmd)
1278
1279     # Setting SPICE password. We are not vulnerable to malicious passwordless
1280     # connection attempts because SPICE by default does not allow connections
1281     # if neither a password nor the "disable_ticketing" options are specified.
1282     # As soon as we send the password via QMP, that password is a valid ticket
1283     # for connection.
1284     spice_password_file = conf_hvp[constants.HV_KVM_SPICE_PASSWORD_FILE]
1285     if spice_password_file:
1286       try:
1287         spice_pwd = utils.ReadOneLineFile(spice_password_file, strict=True)
1288         qmp = QmpConnection(self._InstanceQmpMonitor(instance.name))
1289         qmp.connect()
1290         arguments = {
1291             "protocol": "spice",
1292             "password": spice_pwd,
1293         }
1294         qmp.Execute("set_password", arguments)
1295       except EnvironmentError, err:
1296         raise errors.HypervisorError("Failed to open SPICE password file %s: %s"
1297                                      % (spice_password_file, err))
1298
1299     for filename in temp_files:
1300       utils.RemoveFile(filename)
1301
1302   def StartInstance(self, instance, block_devices, startup_paused):
1303     """Start an instance.
1304
1305     """
1306     self._CheckDown(instance.name)
1307     kvm_runtime = self._GenerateKVMRuntime(instance, block_devices,
1308                                            startup_paused)
1309     self._SaveKVMRuntime(instance, kvm_runtime)
1310     self._ExecuteKVMRuntime(instance, kvm_runtime)
1311
1312   def _CallMonitorCommand(self, instance_name, command):
1313     """Invoke a command on the instance monitor.
1314
1315     """
1316     socat = ("echo %s | %s STDIO UNIX-CONNECT:%s" %
1317              (utils.ShellQuote(command),
1318               constants.SOCAT_PATH,
1319               utils.ShellQuote(self._InstanceMonitor(instance_name))))
1320     result = utils.RunCmd(socat)
1321     if result.failed:
1322       msg = ("Failed to send command '%s' to instance %s."
1323              " output: %s, error: %s, fail_reason: %s" %
1324              (command, instance_name,
1325               result.stdout, result.stderr, result.fail_reason))
1326       raise errors.HypervisorError(msg)
1327
1328     return result
1329
1330   @classmethod
1331   def _GetKVMVersion(cls):
1332     """Return the installed KVM version.
1333
1334     @return: (version, v_maj, v_min, v_rev)
1335     @raise L{errors.HypervisorError}: when the KVM version cannot be retrieved
1336
1337     """
1338     result = utils.RunCmd([constants.KVM_PATH, "--help"])
1339     if result.failed:
1340       raise errors.HypervisorError("Unable to get KVM version")
1341     match = cls._VERSION_RE.search(result.output.splitlines()[0])
1342     if not match:
1343       raise errors.HypervisorError("Unable to get KVM version")
1344
1345     return (match.group(0), int(match.group(1)), int(match.group(2)),
1346             int(match.group(3)))
1347
1348   def StopInstance(self, instance, force=False, retry=False, name=None):
1349     """Stop an instance.
1350
1351     """
1352     if name is not None and not force:
1353       raise errors.HypervisorError("Cannot shutdown cleanly by name only")
1354     if name is None:
1355       name = instance.name
1356       acpi = instance.hvparams[constants.HV_ACPI]
1357     else:
1358       acpi = False
1359     _, pid, alive = self._InstancePidAlive(name)
1360     if pid > 0 and alive:
1361       if force or not acpi:
1362         utils.KillProcess(pid)
1363       else:
1364         self._CallMonitorCommand(name, "system_powerdown")
1365
1366   def CleanupInstance(self, instance_name):
1367     """Cleanup after a stopped instance
1368
1369     """
1370     pidfile, pid, alive = self._InstancePidAlive(instance_name)
1371     if pid > 0 and alive:
1372       raise errors.HypervisorError("Cannot cleanup a live instance")
1373     self._RemoveInstanceRuntimeFiles(pidfile, instance_name)
1374
1375   def RebootInstance(self, instance):
1376     """Reboot an instance.
1377
1378     """
1379     # For some reason if we do a 'send-key ctrl-alt-delete' to the control
1380     # socket the instance will stop, but now power up again. So we'll resort
1381     # to shutdown and restart.
1382     _, _, alive = self._InstancePidAlive(instance.name)
1383     if not alive:
1384       raise errors.HypervisorError("Failed to reboot instance %s:"
1385                                    " not running" % instance.name)
1386     # StopInstance will delete the saved KVM runtime so:
1387     # ...first load it...
1388     kvm_runtime = self._LoadKVMRuntime(instance)
1389     # ...now we can safely call StopInstance...
1390     if not self.StopInstance(instance):
1391       self.StopInstance(instance, force=True)
1392     # ...and finally we can save it again, and execute it...
1393     self._SaveKVMRuntime(instance, kvm_runtime)
1394     self._ExecuteKVMRuntime(instance, kvm_runtime)
1395
1396   def MigrationInfo(self, instance):
1397     """Get instance information to perform a migration.
1398
1399     @type instance: L{objects.Instance}
1400     @param instance: instance to be migrated
1401     @rtype: string
1402     @return: content of the KVM runtime file
1403
1404     """
1405     return self._ReadKVMRuntime(instance.name)
1406
1407   def AcceptInstance(self, instance, info, target):
1408     """Prepare to accept an instance.
1409
1410     @type instance: L{objects.Instance}
1411     @param instance: instance to be accepted
1412     @type info: string
1413     @param info: content of the KVM runtime file on the source node
1414     @type target: string
1415     @param target: target host (usually ip), on this node
1416
1417     """
1418     kvm_runtime = self._LoadKVMRuntime(instance, serialized_runtime=info)
1419     incoming_address = (target, instance.hvparams[constants.HV_MIGRATION_PORT])
1420     self._ExecuteKVMRuntime(instance, kvm_runtime, incoming=incoming_address)
1421
1422   def FinalizeMigration(self, instance, info, success):
1423     """Finalize an instance migration.
1424
1425     Stop the incoming mode KVM.
1426
1427     @type instance: L{objects.Instance}
1428     @param instance: instance whose migration is being finalized
1429
1430     """
1431     if success:
1432       kvm_runtime = self._LoadKVMRuntime(instance, serialized_runtime=info)
1433       kvm_nics = kvm_runtime[1]
1434
1435       for nic_seq, nic in enumerate(kvm_nics):
1436         if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
1437           # Bridged interfaces have already been configured
1438           continue
1439         try:
1440           tap = utils.ReadFile(self._InstanceNICFile(instance.name, nic_seq))
1441         except EnvironmentError, err:
1442           logging.warning("Failed to find host interface for %s NIC #%d: %s",
1443                           instance.name, nic_seq, str(err))
1444           continue
1445         try:
1446           self._ConfigureNIC(instance, nic_seq, nic, tap)
1447         except errors.HypervisorError, err:
1448           logging.warning(str(err))
1449
1450       self._WriteKVMRuntime(instance.name, info)
1451     else:
1452       self.StopInstance(instance, force=True)
1453
1454   def MigrateInstance(self, instance, target, live):
1455     """Migrate an instance to a target node.
1456
1457     The migration will not be attempted if the instance is not
1458     currently running.
1459
1460     @type instance: L{objects.Instance}
1461     @param instance: the instance to be migrated
1462     @type target: string
1463     @param target: ip address of the target node
1464     @type live: boolean
1465     @param live: perform a live migration
1466
1467     """
1468     instance_name = instance.name
1469     port = instance.hvparams[constants.HV_MIGRATION_PORT]
1470     pidfile, pid, alive = self._InstancePidAlive(instance_name)
1471     if not alive:
1472       raise errors.HypervisorError("Instance not running, cannot migrate")
1473
1474     if not live:
1475       self._CallMonitorCommand(instance_name, "stop")
1476
1477     migrate_command = ("migrate_set_speed %dm" %
1478         instance.hvparams[constants.HV_MIGRATION_BANDWIDTH])
1479     self._CallMonitorCommand(instance_name, migrate_command)
1480
1481     migrate_command = ("migrate_set_downtime %dms" %
1482         instance.hvparams[constants.HV_MIGRATION_DOWNTIME])
1483     self._CallMonitorCommand(instance_name, migrate_command)
1484
1485     migrate_command = "migrate -d tcp:%s:%s" % (target, port)
1486     self._CallMonitorCommand(instance_name, migrate_command)
1487
1488     info_command = "info migrate"
1489     done = False
1490     broken_answers = 0
1491     while not done:
1492       result = self._CallMonitorCommand(instance_name, info_command)
1493       match = self._MIGRATION_STATUS_RE.search(result.stdout)
1494       if not match:
1495         broken_answers += 1
1496         if not result.stdout:
1497           logging.info("KVM: empty 'info migrate' result")
1498         else:
1499           logging.warning("KVM: unknown 'info migrate' result: %s",
1500                           result.stdout)
1501         time.sleep(self._MIGRATION_INFO_RETRY_DELAY)
1502       else:
1503         status = match.group(1)
1504         if status == "completed":
1505           done = True
1506         elif status == "active":
1507           # reset the broken answers count
1508           broken_answers = 0
1509           time.sleep(self._MIGRATION_INFO_RETRY_DELAY)
1510         elif status == "failed" or status == "cancelled":
1511           if not live:
1512             self._CallMonitorCommand(instance_name, 'cont')
1513           raise errors.HypervisorError("Migration %s at the kvm level" %
1514                                        status)
1515         else:
1516           logging.warning("KVM: unknown migration status '%s'", status)
1517           broken_answers += 1
1518           time.sleep(self._MIGRATION_INFO_RETRY_DELAY)
1519       if broken_answers >= self._MIGRATION_INFO_MAX_BAD_ANSWERS:
1520         raise errors.HypervisorError("Too many 'info migrate' broken answers")
1521
1522     utils.KillProcess(pid)
1523     self._RemoveInstanceRuntimeFiles(pidfile, instance_name)
1524
1525   def GetNodeInfo(self):
1526     """Return information about the node.
1527
1528     @return: a dict with the following keys (values in MiB):
1529           - memory_total: the total memory size on the node
1530           - memory_free: the available memory on the node for instances
1531           - memory_dom0: the memory used by the node itself, if available
1532           - hv_version: the hypervisor version in the form (major, minor,
1533                         revision)
1534
1535     """
1536     result = self.GetLinuxNodeInfo()
1537     _, v_major, v_min, v_rev = self._GetKVMVersion()
1538     result[constants.HV_NODEINFO_KEY_VERSION] = (v_major, v_min, v_rev)
1539     return result
1540
1541   @classmethod
1542   def GetInstanceConsole(cls, instance, hvparams, beparams):
1543     """Return a command for connecting to the console of an instance.
1544
1545     """
1546     if hvparams[constants.HV_SERIAL_CONSOLE]:
1547       cmd = [constants.KVM_CONSOLE_WRAPPER,
1548              constants.SOCAT_PATH, utils.ShellQuote(instance.name),
1549              utils.ShellQuote(cls._InstanceMonitor(instance.name)),
1550              "STDIO,%s" % cls._SocatUnixConsoleParams(),
1551              "UNIX-CONNECT:%s" % cls._InstanceSerial(instance.name)]
1552       return objects.InstanceConsole(instance=instance.name,
1553                                      kind=constants.CONS_SSH,
1554                                      host=instance.primary_node,
1555                                      user=constants.GANETI_RUNAS,
1556                                      command=cmd)
1557
1558     vnc_bind_address = hvparams[constants.HV_VNC_BIND_ADDRESS]
1559     if vnc_bind_address and instance.network_port > constants.VNC_BASE_PORT:
1560       display = instance.network_port - constants.VNC_BASE_PORT
1561       return objects.InstanceConsole(instance=instance.name,
1562                                      kind=constants.CONS_VNC,
1563                                      host=vnc_bind_address,
1564                                      port=instance.network_port,
1565                                      display=display)
1566
1567     spice_bind = hvparams[constants.HV_KVM_SPICE_BIND]
1568     if spice_bind:
1569       return objects.InstanceConsole(instance=instance.name,
1570                                      kind=constants.CONS_SPICE,
1571                                      host=spice_bind,
1572                                      port=instance.network_port)
1573
1574     return objects.InstanceConsole(instance=instance.name,
1575                                    kind=constants.CONS_MESSAGE,
1576                                    message=("No serial shell for instance %s" %
1577                                             instance.name))
1578
1579   def Verify(self):
1580     """Verify the hypervisor.
1581
1582     Check that the binary exists.
1583
1584     """
1585     if not os.path.exists(constants.KVM_PATH):
1586       return "The kvm binary ('%s') does not exist." % constants.KVM_PATH
1587     if not os.path.exists(constants.SOCAT_PATH):
1588       return "The socat binary ('%s') does not exist." % constants.SOCAT_PATH
1589
1590   @classmethod
1591   def CheckParameterSyntax(cls, hvparams):
1592     """Check the given parameters for validity.
1593
1594     @type hvparams:  dict
1595     @param hvparams: dictionary with parameter names/value
1596     @raise errors.HypervisorError: when a parameter is not valid
1597
1598     """
1599     super(KVMHypervisor, cls).CheckParameterSyntax(hvparams)
1600
1601     kernel_path = hvparams[constants.HV_KERNEL_PATH]
1602     if kernel_path:
1603       if not hvparams[constants.HV_ROOT_PATH]:
1604         raise errors.HypervisorError("Need a root partition for the instance,"
1605                                      " if a kernel is defined")
1606
1607     if (hvparams[constants.HV_VNC_X509_VERIFY] and
1608         not hvparams[constants.HV_VNC_X509]):
1609       raise errors.HypervisorError("%s must be defined, if %s is" %
1610                                    (constants.HV_VNC_X509,
1611                                     constants.HV_VNC_X509_VERIFY))
1612
1613     boot_order = hvparams[constants.HV_BOOT_ORDER]
1614     if (boot_order == constants.HT_BO_CDROM and
1615         not hvparams[constants.HV_CDROM_IMAGE_PATH]):
1616       raise errors.HypervisorError("Cannot boot from cdrom without an"
1617                                    " ISO path")
1618
1619     security_model = hvparams[constants.HV_SECURITY_MODEL]
1620     if security_model == constants.HT_SM_USER:
1621       if not hvparams[constants.HV_SECURITY_DOMAIN]:
1622         raise errors.HypervisorError("A security domain (user to run kvm as)"
1623                                      " must be specified")
1624     elif (security_model == constants.HT_SM_NONE or
1625           security_model == constants.HT_SM_POOL):
1626       if hvparams[constants.HV_SECURITY_DOMAIN]:
1627         raise errors.HypervisorError("Cannot have a security domain when the"
1628                                      " security model is 'none' or 'pool'")
1629
1630     spice_bind = hvparams[constants.HV_KVM_SPICE_BIND]
1631     spice_ip_version = hvparams[constants.HV_KVM_SPICE_IP_VERSION]
1632     if spice_bind:
1633       if spice_ip_version != constants.IFACE_NO_IP_VERSION_SPECIFIED:
1634         # if an IP version is specified, the spice_bind parameter must be an
1635         # IP of that family
1636         if (netutils.IP4Address.IsValid(spice_bind) and
1637             spice_ip_version != constants.IP4_VERSION):
1638           raise errors.HypervisorError("spice: got an IPv4 address (%s), but"
1639                                        " the specified IP version is %s" %
1640                                        (spice_bind, spice_ip_version))
1641
1642         if (netutils.IP6Address.IsValid(spice_bind) and
1643             spice_ip_version != constants.IP6_VERSION):
1644           raise errors.HypervisorError("spice: got an IPv6 address (%s), but"
1645                                        " the specified IP version is %s" %
1646                                        (spice_bind, spice_ip_version))
1647     else:
1648       # All the other SPICE parameters depend on spice_bind being set. Raise an
1649       # error if any of them is set without it.
1650       spice_additional_params = frozenset([
1651         constants.HV_KVM_SPICE_IP_VERSION,
1652         constants.HV_KVM_SPICE_PASSWORD_FILE,
1653         constants.HV_KVM_SPICE_LOSSLESS_IMG_COMPR,
1654         constants.HV_KVM_SPICE_JPEG_IMG_COMPR,
1655         constants.HV_KVM_SPICE_ZLIB_GLZ_IMG_COMPR,
1656         constants.HV_KVM_SPICE_STREAMING_VIDEO_DETECTION,
1657         ])
1658       for param in spice_additional_params:
1659         if hvparams[param]:
1660           raise errors.HypervisorError("spice: %s requires %s to be set" %
1661                                        (param, constants.HV_KVM_SPICE_BIND))
1662
1663   @classmethod
1664   def ValidateParameters(cls, hvparams):
1665     """Check the given parameters for validity.
1666
1667     @type hvparams:  dict
1668     @param hvparams: dictionary with parameter names/value
1669     @raise errors.HypervisorError: when a parameter is not valid
1670
1671     """
1672     super(KVMHypervisor, cls).ValidateParameters(hvparams)
1673
1674     security_model = hvparams[constants.HV_SECURITY_MODEL]
1675     if security_model == constants.HT_SM_USER:
1676       username = hvparams[constants.HV_SECURITY_DOMAIN]
1677       try:
1678         pwd.getpwnam(username)
1679       except KeyError:
1680         raise errors.HypervisorError("Unknown security domain user %s"
1681                                      % username)
1682
1683     spice_bind = hvparams[constants.HV_KVM_SPICE_BIND]
1684     if spice_bind:
1685       # only one of VNC and SPICE can be used currently.
1686       if hvparams[constants.HV_VNC_BIND_ADDRESS]:
1687         raise errors.HypervisorError("both SPICE and VNC are configured, but"
1688                                      " only one of them can be used at a"
1689                                      " given time.")
1690
1691       # KVM version should be >= 0.14.0
1692       _, v_major, v_min, _ = cls._GetKVMVersion()
1693       if (v_major, v_min) < (0, 14):
1694         raise errors.HypervisorError("spice is configured, but it is not"
1695                                      " available in versions of KVM < 0.14")
1696
1697       # if spice_bind is not an IP address, it must be a valid interface
1698       bound_to_addr = (netutils.IP4Address.IsValid(spice_bind)
1699                        or netutils.IP6Address.IsValid(spice_bind))
1700       if not bound_to_addr and not netutils.IsValidInterface(spice_bind):
1701         raise errors.HypervisorError("spice: the %s parameter must be either"
1702                                      " a valid IP address or interface name" %
1703                                      constants.HV_KVM_SPICE_BIND)
1704
1705   @classmethod
1706   def PowercycleNode(cls):
1707     """KVM powercycle, just a wrapper over Linux powercycle.
1708
1709     """
1710     cls.LinuxPowercycle()