Added password for SPICE sessions
[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_FLOPPY_IMAGE_PATH: hv_base.OPT_FILE_CHECK,
423     constants.HV_CDROM_IMAGE_PATH: hv_base.OPT_FILE_CHECK,
424     constants.HV_KVM_CDROM2_IMAGE_PATH: hv_base.OPT_FILE_CHECK,
425     constants.HV_BOOT_ORDER:
426       hv_base.ParamInSet(True, constants.HT_KVM_VALID_BO_TYPES),
427     constants.HV_NIC_TYPE:
428       hv_base.ParamInSet(True, constants.HT_KVM_VALID_NIC_TYPES),
429     constants.HV_DISK_TYPE:
430       hv_base.ParamInSet(True, constants.HT_KVM_VALID_DISK_TYPES),
431     constants.HV_KVM_CDROM_DISK_TYPE:
432       hv_base.ParamInSet(False, constants.HT_KVM_VALID_DISK_TYPES),
433     constants.HV_USB_MOUSE:
434       hv_base.ParamInSet(False, constants.HT_KVM_VALID_MOUSE_TYPES),
435     constants.HV_KEYMAP: hv_base.NO_CHECK,
436     constants.HV_MIGRATION_PORT: hv_base.REQ_NET_PORT_CHECK,
437     constants.HV_MIGRATION_BANDWIDTH: hv_base.NO_CHECK,
438     constants.HV_MIGRATION_DOWNTIME: hv_base.NO_CHECK,
439     constants.HV_MIGRATION_MODE: hv_base.MIGRATION_MODE_CHECK,
440     constants.HV_USE_LOCALTIME: hv_base.NO_CHECK,
441     constants.HV_DISK_CACHE:
442       hv_base.ParamInSet(True, constants.HT_VALID_CACHE_TYPES),
443     constants.HV_SECURITY_MODEL:
444       hv_base.ParamInSet(True, constants.HT_KVM_VALID_SM_TYPES),
445     constants.HV_SECURITY_DOMAIN: hv_base.NO_CHECK,
446     constants.HV_KVM_FLAG:
447       hv_base.ParamInSet(False, constants.HT_KVM_FLAG_VALUES),
448     constants.HV_VHOST_NET: hv_base.NO_CHECK,
449     constants.HV_KVM_USE_CHROOT: hv_base.NO_CHECK,
450     constants.HV_MEM_PATH: hv_base.OPT_DIR_CHECK,
451     constants.HV_REBOOT_BEHAVIOR:
452       hv_base.ParamInSet(True, constants.REBOOT_BEHAVIORS)
453     }
454
455   _MIGRATION_STATUS_RE = re.compile("Migration\s+status:\s+(\w+)",
456                                     re.M | re.I)
457   _MIGRATION_INFO_MAX_BAD_ANSWERS = 5
458   _MIGRATION_INFO_RETRY_DELAY = 2
459
460   _VERSION_RE = re.compile(r"\b(\d+)\.(\d+)\.(\d+)\b")
461
462   ANCILLARY_FILES = [
463     _KVM_NETWORK_SCRIPT,
464     ]
465
466   def __init__(self):
467     hv_base.BaseHypervisor.__init__(self)
468     # Let's make sure the directories we need exist, even if the RUN_DIR lives
469     # in a tmpfs filesystem or has been otherwise wiped out.
470     dirs = [(dname, constants.RUN_DIRS_MODE) for dname in self._DIRS]
471     utils.EnsureDirs(dirs)
472
473   @classmethod
474   def _InstancePidFile(cls, instance_name):
475     """Returns the instance pidfile.
476
477     """
478     return utils.PathJoin(cls._PIDS_DIR, instance_name)
479
480   @classmethod
481   def _InstanceUidFile(cls, instance_name):
482     """Returns the instance uidfile.
483
484     """
485     return utils.PathJoin(cls._UIDS_DIR, instance_name)
486
487   @classmethod
488   def _InstancePidInfo(cls, pid):
489     """Check pid file for instance information.
490
491     Check that a pid file is associated with an instance, and retrieve
492     information from its command line.
493
494     @type pid: string or int
495     @param pid: process id of the instance to check
496     @rtype: tuple
497     @return: (instance_name, memory, vcpus)
498     @raise errors.HypervisorError: when an instance cannot be found
499
500     """
501     alive = utils.IsProcessAlive(pid)
502     if not alive:
503       raise errors.HypervisorError("Cannot get info for pid %s" % pid)
504
505     cmdline_file = utils.PathJoin("/proc", str(pid), "cmdline")
506     try:
507       cmdline = utils.ReadFile(cmdline_file)
508     except EnvironmentError, err:
509       raise errors.HypervisorError("Can't open cmdline file for pid %s: %s" %
510                                    (pid, err))
511
512     instance = None
513     memory = 0
514     vcpus = 0
515
516     arg_list = cmdline.split("\x00")
517     while arg_list:
518       arg = arg_list.pop(0)
519       if arg == "-name":
520         instance = arg_list.pop(0)
521       elif arg == "-m":
522         memory = int(arg_list.pop(0))
523       elif arg == "-smp":
524         vcpus = int(arg_list.pop(0))
525
526     if instance is None:
527       raise errors.HypervisorError("Pid %s doesn't contain a ganeti kvm"
528                                    " instance" % pid)
529
530     return (instance, memory, vcpus)
531
532   def _InstancePidAlive(self, instance_name):
533     """Returns the instance pidfile, pid, and liveness.
534
535     @type instance_name: string
536     @param instance_name: instance name
537     @rtype: tuple
538     @return: (pid file name, pid, liveness)
539
540     """
541     pidfile = self._InstancePidFile(instance_name)
542     pid = utils.ReadPidFile(pidfile)
543
544     alive = False
545     try:
546       cmd_instance = self._InstancePidInfo(pid)[0]
547       alive = (cmd_instance == instance_name)
548     except errors.HypervisorError:
549       pass
550
551     return (pidfile, pid, alive)
552
553   def _CheckDown(self, instance_name):
554     """Raises an error unless the given instance is down.
555
556     """
557     alive = self._InstancePidAlive(instance_name)[2]
558     if alive:
559       raise errors.HypervisorError("Failed to start instance %s: %s" %
560                                    (instance_name, "already running"))
561
562   @classmethod
563   def _InstanceMonitor(cls, instance_name):
564     """Returns the instance monitor socket name
565
566     """
567     return utils.PathJoin(cls._CTRL_DIR, "%s.monitor" % instance_name)
568
569   @classmethod
570   def _InstanceSerial(cls, instance_name):
571     """Returns the instance serial socket name
572
573     """
574     return utils.PathJoin(cls._CTRL_DIR, "%s.serial" % instance_name)
575
576   @classmethod
577   def _InstanceQmpMonitor(cls, instance_name):
578     """Returns the instance serial QMP socket name
579
580     """
581     return utils.PathJoin(cls._CTRL_DIR, "%s.qmp" % instance_name)
582
583   @staticmethod
584   def _SocatUnixConsoleParams():
585     """Returns the correct parameters for socat
586
587     If we have a new-enough socat we can use raw mode with an escape character.
588
589     """
590     if constants.SOCAT_USE_ESCAPE:
591       return "raw,echo=0,escape=%s" % constants.SOCAT_ESCAPE_CODE
592     else:
593       return "echo=0,icanon=0"
594
595   @classmethod
596   def _InstanceKVMRuntime(cls, instance_name):
597     """Returns the instance KVM runtime filename
598
599     """
600     return utils.PathJoin(cls._CONF_DIR, "%s.runtime" % instance_name)
601
602   @classmethod
603   def _InstanceChrootDir(cls, instance_name):
604     """Returns the name of the KVM chroot dir of the instance
605
606     """
607     return utils.PathJoin(cls._CHROOT_DIR, instance_name)
608
609   @classmethod
610   def _InstanceNICDir(cls, instance_name):
611     """Returns the name of the directory holding the tap device files for a
612     given instance.
613
614     """
615     return utils.PathJoin(cls._NICS_DIR, instance_name)
616
617   @classmethod
618   def _InstanceNICFile(cls, instance_name, seq):
619     """Returns the name of the file containing the tap device for a given NIC
620
621     """
622     return utils.PathJoin(cls._InstanceNICDir(instance_name), str(seq))
623
624   @classmethod
625   def _InstanceKeymapFile(cls, instance_name):
626     """Returns the name of the file containing the keymap for a given instance
627
628     """
629     return utils.PathJoin(cls._KEYMAP_DIR, instance_name)
630
631   @classmethod
632   def _TryReadUidFile(cls, uid_file):
633     """Try to read a uid file
634
635     """
636     if os.path.exists(uid_file):
637       try:
638         uid = int(utils.ReadOneLineFile(uid_file))
639         return uid
640       except EnvironmentError:
641         logging.warning("Can't read uid file", exc_info=True)
642       except (TypeError, ValueError):
643         logging.warning("Can't parse uid file contents", exc_info=True)
644     return None
645
646   @classmethod
647   def _RemoveInstanceRuntimeFiles(cls, pidfile, instance_name):
648     """Removes an instance's rutime sockets/files/dirs.
649
650     """
651     utils.RemoveFile(pidfile)
652     utils.RemoveFile(cls._InstanceMonitor(instance_name))
653     utils.RemoveFile(cls._InstanceSerial(instance_name))
654     utils.RemoveFile(cls._InstanceQmpMonitor(instance_name))
655     utils.RemoveFile(cls._InstanceKVMRuntime(instance_name))
656     utils.RemoveFile(cls._InstanceKeymapFile(instance_name))
657     uid_file = cls._InstanceUidFile(instance_name)
658     uid = cls._TryReadUidFile(uid_file)
659     utils.RemoveFile(uid_file)
660     if uid is not None:
661       uidpool.ReleaseUid(uid)
662     try:
663       shutil.rmtree(cls._InstanceNICDir(instance_name))
664     except OSError, err:
665       if err.errno != errno.ENOENT:
666         raise
667     try:
668       chroot_dir = cls._InstanceChrootDir(instance_name)
669       utils.RemoveDir(chroot_dir)
670     except OSError, err:
671       if err.errno == errno.ENOTEMPTY:
672         # The chroot directory is expected to be empty, but it isn't.
673         new_chroot_dir = tempfile.mkdtemp(dir=cls._CHROOT_QUARANTINE_DIR,
674                                           prefix="%s-%s-" %
675                                           (instance_name,
676                                            utils.TimestampForFilename()))
677         logging.warning("The chroot directory of instance %s can not be"
678                         " removed as it is not empty. Moving it to the"
679                         " quarantine instead. Please investigate the"
680                         " contents (%s) and clean up manually",
681                         instance_name, new_chroot_dir)
682         utils.RenameFile(chroot_dir, new_chroot_dir)
683       else:
684         raise
685
686   @staticmethod
687   def _ConfigureNIC(instance, seq, nic, tap):
688     """Run the network configuration script for a specified NIC
689
690     @param instance: instance we're acting on
691     @type instance: instance object
692     @param seq: nic sequence number
693     @type seq: int
694     @param nic: nic we're acting on
695     @type nic: nic object
696     @param tap: the host's tap interface this NIC corresponds to
697     @type tap: str
698
699     """
700
701     if instance.tags:
702       tags = " ".join(instance.tags)
703     else:
704       tags = ""
705
706     env = {
707       "PATH": "%s:/sbin:/usr/sbin" % os.environ["PATH"],
708       "INSTANCE": instance.name,
709       "MAC": nic.mac,
710       "MODE": nic.nicparams[constants.NIC_MODE],
711       "INTERFACE": tap,
712       "INTERFACE_INDEX": str(seq),
713       "TAGS": tags,
714     }
715
716     if nic.ip:
717       env["IP"] = nic.ip
718
719     if nic.nicparams[constants.NIC_LINK]:
720       env["LINK"] = nic.nicparams[constants.NIC_LINK]
721
722     if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
723       env["BRIDGE"] = nic.nicparams[constants.NIC_LINK]
724
725     result = utils.RunCmd([constants.KVM_IFUP, tap], env=env)
726     if result.failed:
727       raise errors.HypervisorError("Failed to configure interface %s: %s."
728                                    " Network configuration script output: %s" %
729                                    (tap, result.fail_reason, result.output))
730
731   def ListInstances(self):
732     """Get the list of running instances.
733
734     We can do this by listing our live instances directory and
735     checking whether the associated kvm process is still alive.
736
737     """
738     result = []
739     for name in os.listdir(self._PIDS_DIR):
740       if self._InstancePidAlive(name)[2]:
741         result.append(name)
742     return result
743
744   def GetInstanceInfo(self, instance_name):
745     """Get instance properties.
746
747     @type instance_name: string
748     @param instance_name: the instance name
749     @rtype: tuple of strings
750     @return: (name, id, memory, vcpus, stat, times)
751
752     """
753     _, pid, alive = self._InstancePidAlive(instance_name)
754     if not alive:
755       return None
756
757     _, memory, vcpus = self._InstancePidInfo(pid)
758     stat = "---b-"
759     times = "0"
760
761     return (instance_name, pid, memory, vcpus, stat, times)
762
763   def GetAllInstancesInfo(self):
764     """Get properties of all instances.
765
766     @return: list of tuples (name, id, memory, vcpus, stat, times)
767
768     """
769     data = []
770     for name in os.listdir(self._PIDS_DIR):
771       try:
772         info = self.GetInstanceInfo(name)
773       except errors.HypervisorError:
774         continue
775       if info:
776         data.append(info)
777     return data
778
779   def _GenerateKVMRuntime(self, instance, block_devices, startup_paused):
780     """Generate KVM information to start an instance.
781
782     """
783     _, v_major, v_min, _ = self._GetKVMVersion()
784
785     pidfile = self._InstancePidFile(instance.name)
786     kvm = constants.KVM_PATH
787     kvm_cmd = [kvm]
788     # used just by the vnc server, if enabled
789     kvm_cmd.extend(["-name", instance.name])
790     kvm_cmd.extend(["-m", instance.beparams[constants.BE_MEMORY]])
791     kvm_cmd.extend(["-smp", instance.beparams[constants.BE_VCPUS]])
792     kvm_cmd.extend(["-pidfile", pidfile])
793     kvm_cmd.extend(["-daemonize"])
794     if not instance.hvparams[constants.HV_ACPI]:
795       kvm_cmd.extend(["-no-acpi"])
796     if startup_paused:
797       kvm_cmd.extend(["-S"])
798     if instance.hvparams[constants.HV_REBOOT_BEHAVIOR] == \
799         constants.INSTANCE_REBOOT_EXIT:
800       kvm_cmd.extend(["-no-reboot"])
801
802     hvp = instance.hvparams
803     boot_disk = hvp[constants.HV_BOOT_ORDER] == constants.HT_BO_DISK
804     boot_cdrom = hvp[constants.HV_BOOT_ORDER] == constants.HT_BO_CDROM
805     boot_floppy = hvp[constants.HV_BOOT_ORDER] == constants.HT_BO_FLOPPY
806     boot_network = hvp[constants.HV_BOOT_ORDER] == constants.HT_BO_NETWORK
807
808     self.ValidateParameters(hvp)
809
810     if hvp[constants.HV_KVM_FLAG] == constants.HT_KVM_ENABLED:
811       kvm_cmd.extend(["-enable-kvm"])
812     elif hvp[constants.HV_KVM_FLAG] == constants.HT_KVM_DISABLED:
813       kvm_cmd.extend(["-disable-kvm"])
814
815     if boot_network:
816       kvm_cmd.extend(["-boot", "n"])
817
818     disk_type = hvp[constants.HV_DISK_TYPE]
819     if disk_type == constants.HT_DISK_PARAVIRTUAL:
820       if_val = ",if=virtio"
821     else:
822       if_val = ",if=%s" % disk_type
823     # Cache mode
824     disk_cache = hvp[constants.HV_DISK_CACHE]
825     if instance.disk_template in constants.DTS_EXT_MIRROR:
826       if disk_cache != "none":
827         # TODO: make this a hard error, instead of a silent overwrite
828         logging.warning("KVM: overriding disk_cache setting '%s' with 'none'"
829                         " to prevent shared storage corruption on migration",
830                         disk_cache)
831       cache_val = ",cache=none"
832     elif disk_cache != constants.HT_CACHE_DEFAULT:
833       cache_val = ",cache=%s" % disk_cache
834     else:
835       cache_val = ""
836     for cfdev, dev_path in block_devices:
837       if cfdev.mode != constants.DISK_RDWR:
838         raise errors.HypervisorError("Instance has read-only disks which"
839                                      " are not supported by KVM")
840       # TODO: handle FD_LOOP and FD_BLKTAP (?)
841       boot_val = ""
842       if boot_disk:
843         kvm_cmd.extend(["-boot", "c"])
844         boot_disk = False
845         if (v_major, v_min) < (0, 14) and disk_type != constants.HT_DISK_IDE:
846           boot_val = ",boot=on"
847
848       drive_val = "file=%s,format=raw%s%s%s" % (dev_path, if_val, boot_val,
849                                                 cache_val)
850       kvm_cmd.extend(["-drive", drive_val])
851
852     #Now we can specify a different device type for CDROM devices.
853     cdrom_disk_type = hvp[constants.HV_KVM_CDROM_DISK_TYPE]
854     if not cdrom_disk_type:
855       cdrom_disk_type = disk_type
856
857     iso_image = hvp[constants.HV_CDROM_IMAGE_PATH]
858     if iso_image:
859       options = ",format=raw,media=cdrom"
860       if boot_cdrom:
861         kvm_cmd.extend(["-boot", "d"])
862         if cdrom_disk_type != constants.HT_DISK_IDE:
863           options = "%s,boot=on,if=%s" % (options, constants.HT_DISK_IDE)
864         else:
865           options = "%s,boot=on" % options
866       else:
867         if cdrom_disk_type == constants.HT_DISK_PARAVIRTUAL:
868           if_val = ",if=virtio"
869         else:
870           if_val = ",if=%s" % cdrom_disk_type
871         options = "%s%s" % (options, if_val)
872       drive_val = "file=%s%s" % (iso_image, options)
873       kvm_cmd.extend(["-drive", drive_val])
874
875     iso_image2 = hvp[constants.HV_KVM_CDROM2_IMAGE_PATH]
876     if iso_image2:
877       options = ",format=raw,media=cdrom"
878       if cdrom_disk_type == constants.HT_DISK_PARAVIRTUAL:
879         if_val = ",if=virtio"
880       else:
881         if_val = ",if=%s" % cdrom_disk_type
882       options = "%s%s" % (options, if_val)
883       drive_val = "file=%s%s" % (iso_image2, options)
884       kvm_cmd.extend(["-drive", drive_val])
885
886     floppy_image = hvp[constants.HV_KVM_FLOPPY_IMAGE_PATH]
887     if floppy_image:
888       options = ",format=raw,media=disk"
889       if boot_floppy:
890         kvm_cmd.extend(["-boot", "a"])
891         options = "%s,boot=on" % options
892       if_val = ",if=floppy"
893       options = "%s%s" % (options, if_val)
894       drive_val = "file=%s%s" % (floppy_image, options)
895       kvm_cmd.extend(["-drive", drive_val])
896
897     kernel_path = hvp[constants.HV_KERNEL_PATH]
898     if kernel_path:
899       kvm_cmd.extend(["-kernel", kernel_path])
900       initrd_path = hvp[constants.HV_INITRD_PATH]
901       if initrd_path:
902         kvm_cmd.extend(["-initrd", initrd_path])
903       root_append = ["root=%s" % hvp[constants.HV_ROOT_PATH],
904                      hvp[constants.HV_KERNEL_ARGS]]
905       if hvp[constants.HV_SERIAL_CONSOLE]:
906         root_append.append("console=ttyS0,38400")
907       kvm_cmd.extend(["-append", " ".join(root_append)])
908
909     mem_path = hvp[constants.HV_MEM_PATH]
910     if mem_path:
911       kvm_cmd.extend(["-mem-path", mem_path, "-mem-prealloc"])
912
913     mouse_type = hvp[constants.HV_USB_MOUSE]
914     vnc_bind_address = hvp[constants.HV_VNC_BIND_ADDRESS]
915
916     if mouse_type:
917       kvm_cmd.extend(["-usb"])
918       kvm_cmd.extend(["-usbdevice", mouse_type])
919     elif vnc_bind_address:
920       kvm_cmd.extend(["-usbdevice", constants.HT_MOUSE_TABLET])
921
922     keymap = hvp[constants.HV_KEYMAP]
923     if keymap:
924       keymap_path = self._InstanceKeymapFile(instance.name)
925       # If a keymap file is specified, KVM won't use its internal defaults. By
926       # first including the "en-us" layout, an error on loading the actual
927       # layout (e.g. because it can't be found) won't lead to a non-functional
928       # keyboard. A keyboard with incorrect keys is still better than none.
929       utils.WriteFile(keymap_path, data="include en-us\ninclude %s\n" % keymap)
930       kvm_cmd.extend(["-k", keymap_path])
931
932     if vnc_bind_address:
933       if netutils.IP4Address.IsValid(vnc_bind_address):
934         if instance.network_port > constants.VNC_BASE_PORT:
935           display = instance.network_port - constants.VNC_BASE_PORT
936           if vnc_bind_address == constants.IP4_ADDRESS_ANY:
937             vnc_arg = ":%d" % (display)
938           else:
939             vnc_arg = "%s:%d" % (vnc_bind_address, display)
940         else:
941           logging.error("Network port is not a valid VNC display (%d < %d)."
942                         " Not starting VNC", instance.network_port,
943                         constants.VNC_BASE_PORT)
944           vnc_arg = "none"
945
946         # Only allow tls and other option when not binding to a file, for now.
947         # kvm/qemu gets confused otherwise about the filename to use.
948         vnc_append = ""
949         if hvp[constants.HV_VNC_TLS]:
950           vnc_append = "%s,tls" % vnc_append
951           if hvp[constants.HV_VNC_X509_VERIFY]:
952             vnc_append = "%s,x509verify=%s" % (vnc_append,
953                                                hvp[constants.HV_VNC_X509])
954           elif hvp[constants.HV_VNC_X509]:
955             vnc_append = "%s,x509=%s" % (vnc_append,
956                                          hvp[constants.HV_VNC_X509])
957         if hvp[constants.HV_VNC_PASSWORD_FILE]:
958           vnc_append = "%s,password" % vnc_append
959
960         vnc_arg = "%s%s" % (vnc_arg, vnc_append)
961
962       else:
963         vnc_arg = "unix:%s/%s.vnc" % (vnc_bind_address, instance.name)
964
965       kvm_cmd.extend(["-vnc", vnc_arg])
966     else:
967       kvm_cmd.extend(["-nographic"])
968
969     monitor_dev = ("unix:%s,server,nowait" %
970                    self._InstanceMonitor(instance.name))
971     kvm_cmd.extend(["-monitor", monitor_dev])
972     if hvp[constants.HV_SERIAL_CONSOLE]:
973       serial_dev = ("unix:%s,server,nowait" %
974                     self._InstanceSerial(instance.name))
975       kvm_cmd.extend(["-serial", serial_dev])
976     else:
977       kvm_cmd.extend(["-serial", "none"])
978
979     spice_bind = hvp[constants.HV_KVM_SPICE_BIND]
980     spice_ip_version = None
981     if spice_bind:
982       if netutils.IsValidInterface(spice_bind):
983         # The user specified a network interface, we have to figure out the IP
984         # address.
985         addresses = netutils.GetInterfaceIpAddresses(spice_bind)
986         spice_ip_version = hvp[constants.HV_KVM_SPICE_IP_VERSION]
987
988         # if the user specified an IP version and the interface does not
989         # have that kind of IP addresses, throw an exception
990         if spice_ip_version != constants.IFACE_NO_IP_VERSION_SPECIFIED:
991           if not addresses[spice_ip_version]:
992             raise errors.HypervisorError("spice: unable to get an IPv%s address"
993                                          " for %s" % (spice_ip_version,
994                                                       spice_bind))
995
996         # the user did not specify an IP version, we have to figure it out
997         elif (addresses[constants.IP4_VERSION] and
998               addresses[constants.IP6_VERSION]):
999           # we have both ipv4 and ipv6, let's use the cluster default IP
1000           # version
1001           cluster_family = ssconf.SimpleStore().GetPrimaryIPFamily()
1002           spice_ip_version = netutils.IPAddress.GetVersionFromAddressFamily(
1003               cluster_family)
1004         elif addresses[constants.IP4_VERSION]:
1005           spice_ip_version = constants.IP4_VERSION
1006         elif addresses[constants.IP6_VERSION]:
1007           spice_ip_version = constants.IP6_VERSION
1008         else:
1009           raise errors.HypervisorError("spice: unable to get an IP address"
1010                                        " for %s" % (spice_bind))
1011
1012         spice_address = addresses[spice_ip_version][0]
1013
1014       else:
1015         # spice_bind is known to be a valid IP address, because
1016         # ValidateParameters checked it.
1017         spice_address = spice_bind
1018
1019       spice_arg = "addr=%s,port=%s" % (spice_address, instance.network_port)
1020       if not hvp[constants.HV_KVM_SPICE_PASSWORD_FILE]:
1021         spice_arg = "%s,disable-ticketing" % spice_arg
1022
1023       if spice_ip_version:
1024         spice_arg = "%s,ipv%s" % (spice_arg, spice_ip_version)
1025
1026       logging.info("KVM: SPICE will listen on port %s", instance.network_port)
1027       kvm_cmd.extend(["-spice", spice_arg])
1028
1029     if hvp[constants.HV_USE_LOCALTIME]:
1030       kvm_cmd.extend(["-localtime"])
1031
1032     if hvp[constants.HV_KVM_USE_CHROOT]:
1033       kvm_cmd.extend(["-chroot", self._InstanceChrootDir(instance.name)])
1034
1035     # Save the current instance nics, but defer their expansion as parameters,
1036     # as we'll need to generate executable temp files for them.
1037     kvm_nics = instance.nics
1038     hvparams = hvp
1039
1040     return (kvm_cmd, kvm_nics, hvparams)
1041
1042   def _WriteKVMRuntime(self, instance_name, data):
1043     """Write an instance's KVM runtime
1044
1045     """
1046     try:
1047       utils.WriteFile(self._InstanceKVMRuntime(instance_name),
1048                       data=data)
1049     except EnvironmentError, err:
1050       raise errors.HypervisorError("Failed to save KVM runtime file: %s" % err)
1051
1052   def _ReadKVMRuntime(self, instance_name):
1053     """Read an instance's KVM runtime
1054
1055     """
1056     try:
1057       file_content = utils.ReadFile(self._InstanceKVMRuntime(instance_name))
1058     except EnvironmentError, err:
1059       raise errors.HypervisorError("Failed to load KVM runtime file: %s" % err)
1060     return file_content
1061
1062   def _SaveKVMRuntime(self, instance, kvm_runtime):
1063     """Save an instance's KVM runtime
1064
1065     """
1066     kvm_cmd, kvm_nics, hvparams = kvm_runtime
1067     serialized_nics = [nic.ToDict() for nic in kvm_nics]
1068     serialized_form = serializer.Dump((kvm_cmd, serialized_nics, hvparams))
1069     self._WriteKVMRuntime(instance.name, serialized_form)
1070
1071   def _LoadKVMRuntime(self, instance, serialized_runtime=None):
1072     """Load an instance's KVM runtime
1073
1074     """
1075     if not serialized_runtime:
1076       serialized_runtime = self._ReadKVMRuntime(instance.name)
1077     loaded_runtime = serializer.Load(serialized_runtime)
1078     kvm_cmd, serialized_nics, hvparams = loaded_runtime
1079     kvm_nics = [objects.NIC.FromDict(snic) for snic in serialized_nics]
1080     return (kvm_cmd, kvm_nics, hvparams)
1081
1082   def _RunKVMCmd(self, name, kvm_cmd, tap_fds=None):
1083     """Run the KVM cmd and check for errors
1084
1085     @type name: string
1086     @param name: instance name
1087     @type kvm_cmd: list of strings
1088     @param kvm_cmd: runcmd input for kvm
1089     @type tap_fds: list of int
1090     @param tap_fds: fds of tap devices opened by Ganeti
1091
1092     """
1093     try:
1094       result = utils.RunCmd(kvm_cmd, noclose_fds=tap_fds)
1095     finally:
1096       for fd in tap_fds:
1097         utils_wrapper.CloseFdNoError(fd)
1098
1099     if result.failed:
1100       raise errors.HypervisorError("Failed to start instance %s: %s (%s)" %
1101                                    (name, result.fail_reason, result.output))
1102     if not self._InstancePidAlive(name)[2]:
1103       raise errors.HypervisorError("Failed to start instance %s" % name)
1104
1105   def _ExecuteKVMRuntime(self, instance, kvm_runtime, incoming=None):
1106     """Execute a KVM cmd, after completing it with some last minute data
1107
1108     @type incoming: tuple of strings
1109     @param incoming: (target_host_ip, port)
1110
1111     """
1112     # Small _ExecuteKVMRuntime hv parameters programming howto:
1113     #  - conf_hvp contains the parameters as configured on ganeti. they might
1114     #    have changed since the instance started; only use them if the change
1115     #    won't affect the inside of the instance (which hasn't been rebooted).
1116     #  - up_hvp contains the parameters as they were when the instance was
1117     #    started, plus any new parameter which has been added between ganeti
1118     #    versions: it is paramount that those default to a value which won't
1119     #    affect the inside of the instance as well.
1120     conf_hvp = instance.hvparams
1121     name = instance.name
1122     self._CheckDown(name)
1123
1124     temp_files = []
1125
1126     kvm_cmd, kvm_nics, up_hvp = kvm_runtime
1127     up_hvp = objects.FillDict(conf_hvp, up_hvp)
1128
1129     _, v_major, v_min, _ = self._GetKVMVersion()
1130
1131     # We know it's safe to run as a different user upon migration, so we'll use
1132     # the latest conf, from conf_hvp.
1133     security_model = conf_hvp[constants.HV_SECURITY_MODEL]
1134     if security_model == constants.HT_SM_USER:
1135       kvm_cmd.extend(["-runas", conf_hvp[constants.HV_SECURITY_DOMAIN]])
1136
1137     # We have reasons to believe changing something like the nic driver/type
1138     # upon migration won't exactly fly with the instance kernel, so for nic
1139     # related parameters we'll use up_hvp
1140     tapfds = []
1141     taps = []
1142     if not kvm_nics:
1143       kvm_cmd.extend(["-net", "none"])
1144     else:
1145       vnet_hdr = False
1146       tap_extra = ""
1147       nic_type = up_hvp[constants.HV_NIC_TYPE]
1148       if nic_type == constants.HT_NIC_PARAVIRTUAL:
1149         # From version 0.12.0, kvm uses a new sintax for network configuration.
1150         if (v_major, v_min) >= (0, 12):
1151           nic_model = "virtio-net-pci"
1152           vnet_hdr = True
1153         else:
1154           nic_model = "virtio"
1155
1156         if up_hvp[constants.HV_VHOST_NET]:
1157           # vhost_net is only available from version 0.13.0 or newer
1158           if (v_major, v_min) >= (0, 13):
1159             tap_extra = ",vhost=on"
1160           else:
1161             raise errors.HypervisorError("vhost_net is configured"
1162                                         " but it is not available")
1163       else:
1164         nic_model = nic_type
1165
1166       for nic_seq, nic in enumerate(kvm_nics):
1167         tapname, tapfd = _OpenTap(vnet_hdr)
1168         tapfds.append(tapfd)
1169         taps.append(tapname)
1170         if (v_major, v_min) >= (0, 12):
1171           nic_val = "%s,mac=%s,netdev=netdev%s" % (nic_model, nic.mac, nic_seq)
1172           tap_val = "type=tap,id=netdev%s,fd=%d%s" % (nic_seq, tapfd, tap_extra)
1173           kvm_cmd.extend(["-netdev", tap_val, "-device", nic_val])
1174         else:
1175           nic_val = "nic,vlan=%s,macaddr=%s,model=%s" % (nic_seq,
1176                                                          nic.mac, nic_model)
1177           tap_val = "tap,vlan=%s,fd=%d" % (nic_seq, tapfd)
1178           kvm_cmd.extend(["-net", tap_val, "-net", nic_val])
1179
1180     if incoming:
1181       target, port = incoming
1182       kvm_cmd.extend(["-incoming", "tcp:%s:%s" % (target, port)])
1183
1184     # Changing the vnc password doesn't bother the guest that much. At most it
1185     # will surprise people who connect to it. Whether positively or negatively
1186     # it's debatable.
1187     vnc_pwd_file = conf_hvp[constants.HV_VNC_PASSWORD_FILE]
1188     vnc_pwd = None
1189     if vnc_pwd_file:
1190       try:
1191         vnc_pwd = utils.ReadOneLineFile(vnc_pwd_file, strict=True)
1192       except EnvironmentError, err:
1193         raise errors.HypervisorError("Failed to open VNC password file %s: %s"
1194                                      % (vnc_pwd_file, err))
1195
1196     if conf_hvp[constants.HV_KVM_USE_CHROOT]:
1197       utils.EnsureDirs([(self._InstanceChrootDir(name),
1198                          constants.SECURE_DIR_MODE)])
1199
1200     # Automatically enable QMP if version is >= 0.14
1201     if (v_major, v_min) >= (0, 14):
1202       logging.debug("Enabling QMP")
1203       kvm_cmd.extend(["-qmp", "unix:%s,server,nowait" %
1204                     self._InstanceQmpMonitor(instance.name)])
1205
1206     # Configure the network now for starting instances and bridged interfaces,
1207     # during FinalizeMigration for incoming instances' routed interfaces
1208     for nic_seq, nic in enumerate(kvm_nics):
1209       if (incoming and
1210           nic.nicparams[constants.NIC_MODE] != constants.NIC_MODE_BRIDGED):
1211         continue
1212       self._ConfigureNIC(instance, nic_seq, nic, taps[nic_seq])
1213
1214     if security_model == constants.HT_SM_POOL:
1215       ss = ssconf.SimpleStore()
1216       uid_pool = uidpool.ParseUidPool(ss.GetUidPool(), separator="\n")
1217       all_uids = set(uidpool.ExpandUidPool(uid_pool))
1218       uid = uidpool.RequestUnusedUid(all_uids)
1219       try:
1220         username = pwd.getpwuid(uid.GetUid()).pw_name
1221         kvm_cmd.extend(["-runas", username])
1222         self._RunKVMCmd(name, kvm_cmd, tapfds)
1223       except:
1224         uidpool.ReleaseUid(uid)
1225         raise
1226       else:
1227         uid.Unlock()
1228         utils.WriteFile(self._InstanceUidFile(name), data=uid.AsStr())
1229     else:
1230       self._RunKVMCmd(name, kvm_cmd, tapfds)
1231
1232     utils.EnsureDirs([(self._InstanceNICDir(instance.name),
1233                      constants.RUN_DIRS_MODE)])
1234     for nic_seq, tap in enumerate(taps):
1235       utils.WriteFile(self._InstanceNICFile(instance.name, nic_seq),
1236                       data=tap)
1237
1238     if vnc_pwd:
1239       change_cmd = "change vnc password %s" % vnc_pwd
1240       self._CallMonitorCommand(instance.name, change_cmd)
1241
1242     # Setting SPICE password. We are not vulnerable to malicious passwordless
1243     # connection attempts because SPICE by default does not allow connections
1244     # if neither a password nor the "disable_ticketing" options are specified.
1245     # As soon as we send the password via QMP, that password is a valid ticket
1246     # for connection.
1247     spice_password_file = conf_hvp[constants.HV_KVM_SPICE_PASSWORD_FILE]
1248     if spice_password_file:
1249       try:
1250         spice_pwd = utils.ReadOneLineFile(spice_password_file, strict=True)
1251         qmp = QmpConnection(self._InstanceQmpMonitor(instance.name))
1252         qmp.connect()
1253         arguments = {
1254             "protocol": "spice",
1255             "password": spice_pwd,
1256         }
1257         qmp.Execute("set_password", arguments)
1258       except EnvironmentError, err:
1259         raise errors.HypervisorError("Failed to open SPICE password file %s: %s"
1260                                      % (spice_password_file, err))
1261
1262     for filename in temp_files:
1263       utils.RemoveFile(filename)
1264
1265   def StartInstance(self, instance, block_devices, startup_paused):
1266     """Start an instance.
1267
1268     """
1269     self._CheckDown(instance.name)
1270     kvm_runtime = self._GenerateKVMRuntime(instance, block_devices,
1271                                            startup_paused)
1272     self._SaveKVMRuntime(instance, kvm_runtime)
1273     self._ExecuteKVMRuntime(instance, kvm_runtime)
1274
1275   def _CallMonitorCommand(self, instance_name, command):
1276     """Invoke a command on the instance monitor.
1277
1278     """
1279     socat = ("echo %s | %s STDIO UNIX-CONNECT:%s" %
1280              (utils.ShellQuote(command),
1281               constants.SOCAT_PATH,
1282               utils.ShellQuote(self._InstanceMonitor(instance_name))))
1283     result = utils.RunCmd(socat)
1284     if result.failed:
1285       msg = ("Failed to send command '%s' to instance %s."
1286              " output: %s, error: %s, fail_reason: %s" %
1287              (command, instance_name,
1288               result.stdout, result.stderr, result.fail_reason))
1289       raise errors.HypervisorError(msg)
1290
1291     return result
1292
1293   @classmethod
1294   def _GetKVMVersion(cls):
1295     """Return the installed KVM version.
1296
1297     @return: (version, v_maj, v_min, v_rev)
1298     @raise L{errors.HypervisorError}: when the KVM version cannot be retrieved
1299
1300     """
1301     result = utils.RunCmd([constants.KVM_PATH, "--help"])
1302     if result.failed:
1303       raise errors.HypervisorError("Unable to get KVM version")
1304     match = cls._VERSION_RE.search(result.output.splitlines()[0])
1305     if not match:
1306       raise errors.HypervisorError("Unable to get KVM version")
1307
1308     return (match.group(0), int(match.group(1)), int(match.group(2)),
1309             int(match.group(3)))
1310
1311   def StopInstance(self, instance, force=False, retry=False, name=None):
1312     """Stop an instance.
1313
1314     """
1315     if name is not None and not force:
1316       raise errors.HypervisorError("Cannot shutdown cleanly by name only")
1317     if name is None:
1318       name = instance.name
1319       acpi = instance.hvparams[constants.HV_ACPI]
1320     else:
1321       acpi = False
1322     _, pid, alive = self._InstancePidAlive(name)
1323     if pid > 0 and alive:
1324       if force or not acpi:
1325         utils.KillProcess(pid)
1326       else:
1327         self._CallMonitorCommand(name, "system_powerdown")
1328
1329   def CleanupInstance(self, instance_name):
1330     """Cleanup after a stopped instance
1331
1332     """
1333     pidfile, pid, alive = self._InstancePidAlive(instance_name)
1334     if pid > 0 and alive:
1335       raise errors.HypervisorError("Cannot cleanup a live instance")
1336     self._RemoveInstanceRuntimeFiles(pidfile, instance_name)
1337
1338   def RebootInstance(self, instance):
1339     """Reboot an instance.
1340
1341     """
1342     # For some reason if we do a 'send-key ctrl-alt-delete' to the control
1343     # socket the instance will stop, but now power up again. So we'll resort
1344     # to shutdown and restart.
1345     _, _, alive = self._InstancePidAlive(instance.name)
1346     if not alive:
1347       raise errors.HypervisorError("Failed to reboot instance %s:"
1348                                    " not running" % instance.name)
1349     # StopInstance will delete the saved KVM runtime so:
1350     # ...first load it...
1351     kvm_runtime = self._LoadKVMRuntime(instance)
1352     # ...now we can safely call StopInstance...
1353     if not self.StopInstance(instance):
1354       self.StopInstance(instance, force=True)
1355     # ...and finally we can save it again, and execute it...
1356     self._SaveKVMRuntime(instance, kvm_runtime)
1357     self._ExecuteKVMRuntime(instance, kvm_runtime)
1358
1359   def MigrationInfo(self, instance):
1360     """Get instance information to perform a migration.
1361
1362     @type instance: L{objects.Instance}
1363     @param instance: instance to be migrated
1364     @rtype: string
1365     @return: content of the KVM runtime file
1366
1367     """
1368     return self._ReadKVMRuntime(instance.name)
1369
1370   def AcceptInstance(self, instance, info, target):
1371     """Prepare to accept an instance.
1372
1373     @type instance: L{objects.Instance}
1374     @param instance: instance to be accepted
1375     @type info: string
1376     @param info: content of the KVM runtime file on the source node
1377     @type target: string
1378     @param target: target host (usually ip), on this node
1379
1380     """
1381     kvm_runtime = self._LoadKVMRuntime(instance, serialized_runtime=info)
1382     incoming_address = (target, instance.hvparams[constants.HV_MIGRATION_PORT])
1383     self._ExecuteKVMRuntime(instance, kvm_runtime, incoming=incoming_address)
1384
1385   def FinalizeMigration(self, instance, info, success):
1386     """Finalize an instance migration.
1387
1388     Stop the incoming mode KVM.
1389
1390     @type instance: L{objects.Instance}
1391     @param instance: instance whose migration is being finalized
1392
1393     """
1394     if success:
1395       kvm_runtime = self._LoadKVMRuntime(instance, serialized_runtime=info)
1396       kvm_nics = kvm_runtime[1]
1397
1398       for nic_seq, nic in enumerate(kvm_nics):
1399         if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
1400           # Bridged interfaces have already been configured
1401           continue
1402         try:
1403           tap = utils.ReadFile(self._InstanceNICFile(instance.name, nic_seq))
1404         except EnvironmentError, err:
1405           logging.warning("Failed to find host interface for %s NIC #%d: %s",
1406                           instance.name, nic_seq, str(err))
1407           continue
1408         try:
1409           self._ConfigureNIC(instance, nic_seq, nic, tap)
1410         except errors.HypervisorError, err:
1411           logging.warning(str(err))
1412
1413       self._WriteKVMRuntime(instance.name, info)
1414     else:
1415       self.StopInstance(instance, force=True)
1416
1417   def MigrateInstance(self, instance, target, live):
1418     """Migrate an instance to a target node.
1419
1420     The migration will not be attempted if the instance is not
1421     currently running.
1422
1423     @type instance: L{objects.Instance}
1424     @param instance: the instance to be migrated
1425     @type target: string
1426     @param target: ip address of the target node
1427     @type live: boolean
1428     @param live: perform a live migration
1429
1430     """
1431     instance_name = instance.name
1432     port = instance.hvparams[constants.HV_MIGRATION_PORT]
1433     pidfile, pid, alive = self._InstancePidAlive(instance_name)
1434     if not alive:
1435       raise errors.HypervisorError("Instance not running, cannot migrate")
1436
1437     if not live:
1438       self._CallMonitorCommand(instance_name, "stop")
1439
1440     migrate_command = ("migrate_set_speed %dm" %
1441         instance.hvparams[constants.HV_MIGRATION_BANDWIDTH])
1442     self._CallMonitorCommand(instance_name, migrate_command)
1443
1444     migrate_command = ("migrate_set_downtime %dms" %
1445         instance.hvparams[constants.HV_MIGRATION_DOWNTIME])
1446     self._CallMonitorCommand(instance_name, migrate_command)
1447
1448     migrate_command = "migrate -d tcp:%s:%s" % (target, port)
1449     self._CallMonitorCommand(instance_name, migrate_command)
1450
1451     info_command = "info migrate"
1452     done = False
1453     broken_answers = 0
1454     while not done:
1455       result = self._CallMonitorCommand(instance_name, info_command)
1456       match = self._MIGRATION_STATUS_RE.search(result.stdout)
1457       if not match:
1458         broken_answers += 1
1459         if not result.stdout:
1460           logging.info("KVM: empty 'info migrate' result")
1461         else:
1462           logging.warning("KVM: unknown 'info migrate' result: %s",
1463                           result.stdout)
1464         time.sleep(self._MIGRATION_INFO_RETRY_DELAY)
1465       else:
1466         status = match.group(1)
1467         if status == "completed":
1468           done = True
1469         elif status == "active":
1470           # reset the broken answers count
1471           broken_answers = 0
1472           time.sleep(self._MIGRATION_INFO_RETRY_DELAY)
1473         elif status == "failed" or status == "cancelled":
1474           if not live:
1475             self._CallMonitorCommand(instance_name, 'cont')
1476           raise errors.HypervisorError("Migration %s at the kvm level" %
1477                                        status)
1478         else:
1479           logging.warning("KVM: unknown migration status '%s'", status)
1480           broken_answers += 1
1481           time.sleep(self._MIGRATION_INFO_RETRY_DELAY)
1482       if broken_answers >= self._MIGRATION_INFO_MAX_BAD_ANSWERS:
1483         raise errors.HypervisorError("Too many 'info migrate' broken answers")
1484
1485     utils.KillProcess(pid)
1486     self._RemoveInstanceRuntimeFiles(pidfile, instance_name)
1487
1488   def GetNodeInfo(self):
1489     """Return information about the node.
1490
1491     This is just a wrapper over the base GetLinuxNodeInfo method.
1492
1493     @return: a dict with the following keys (values in MiB):
1494           - memory_total: the total memory size on the node
1495           - memory_free: the available memory on the node for instances
1496           - memory_dom0: the memory used by the node itself, if available
1497
1498     """
1499     return self.GetLinuxNodeInfo()
1500
1501   @classmethod
1502   def GetInstanceConsole(cls, instance, hvparams, beparams):
1503     """Return a command for connecting to the console of an instance.
1504
1505     """
1506     if hvparams[constants.HV_SERIAL_CONSOLE]:
1507       cmd = [constants.KVM_CONSOLE_WRAPPER,
1508              constants.SOCAT_PATH, utils.ShellQuote(instance.name),
1509              utils.ShellQuote(cls._InstanceMonitor(instance.name)),
1510              "STDIO,%s" % cls._SocatUnixConsoleParams(),
1511              "UNIX-CONNECT:%s" % cls._InstanceSerial(instance.name)]
1512       return objects.InstanceConsole(instance=instance.name,
1513                                      kind=constants.CONS_SSH,
1514                                      host=instance.primary_node,
1515                                      user=constants.GANETI_RUNAS,
1516                                      command=cmd)
1517
1518     vnc_bind_address = hvparams[constants.HV_VNC_BIND_ADDRESS]
1519     if vnc_bind_address and instance.network_port > constants.VNC_BASE_PORT:
1520       display = instance.network_port - constants.VNC_BASE_PORT
1521       return objects.InstanceConsole(instance=instance.name,
1522                                      kind=constants.CONS_VNC,
1523                                      host=vnc_bind_address,
1524                                      port=instance.network_port,
1525                                      display=display)
1526
1527     return objects.InstanceConsole(instance=instance.name,
1528                                    kind=constants.CONS_MESSAGE,
1529                                    message=("No serial shell for instance %s" %
1530                                             instance.name))
1531
1532   def Verify(self):
1533     """Verify the hypervisor.
1534
1535     Check that the binary exists.
1536
1537     """
1538     if not os.path.exists(constants.KVM_PATH):
1539       return "The kvm binary ('%s') does not exist." % constants.KVM_PATH
1540     if not os.path.exists(constants.SOCAT_PATH):
1541       return "The socat binary ('%s') does not exist." % constants.SOCAT_PATH
1542
1543   @classmethod
1544   def CheckParameterSyntax(cls, hvparams):
1545     """Check the given parameters for validity.
1546
1547     @type hvparams:  dict
1548     @param hvparams: dictionary with parameter names/value
1549     @raise errors.HypervisorError: when a parameter is not valid
1550
1551     """
1552     super(KVMHypervisor, cls).CheckParameterSyntax(hvparams)
1553
1554     kernel_path = hvparams[constants.HV_KERNEL_PATH]
1555     if kernel_path:
1556       if not hvparams[constants.HV_ROOT_PATH]:
1557         raise errors.HypervisorError("Need a root partition for the instance,"
1558                                      " if a kernel is defined")
1559
1560     if (hvparams[constants.HV_VNC_X509_VERIFY] and
1561         not hvparams[constants.HV_VNC_X509]):
1562       raise errors.HypervisorError("%s must be defined, if %s is" %
1563                                    (constants.HV_VNC_X509,
1564                                     constants.HV_VNC_X509_VERIFY))
1565
1566     boot_order = hvparams[constants.HV_BOOT_ORDER]
1567     if (boot_order == constants.HT_BO_CDROM and
1568         not hvparams[constants.HV_CDROM_IMAGE_PATH]):
1569       raise errors.HypervisorError("Cannot boot from cdrom without an"
1570                                    " ISO path")
1571
1572     security_model = hvparams[constants.HV_SECURITY_MODEL]
1573     if security_model == constants.HT_SM_USER:
1574       if not hvparams[constants.HV_SECURITY_DOMAIN]:
1575         raise errors.HypervisorError("A security domain (user to run kvm as)"
1576                                      " must be specified")
1577     elif (security_model == constants.HT_SM_NONE or
1578           security_model == constants.HT_SM_POOL):
1579       if hvparams[constants.HV_SECURITY_DOMAIN]:
1580         raise errors.HypervisorError("Cannot have a security domain when the"
1581                                      " security model is 'none' or 'pool'")
1582
1583     spice_bind = hvparams[constants.HV_KVM_SPICE_BIND]
1584     spice_ip_version = hvparams[constants.HV_KVM_SPICE_IP_VERSION]
1585     spice_password_file = hvparams[constants.HV_KVM_SPICE_PASSWORD_FILE]
1586     if spice_bind:
1587       if spice_ip_version != constants.IFACE_NO_IP_VERSION_SPECIFIED:
1588         # if an IP version is specified, the spice_bind parameter must be an
1589         # IP of that family
1590         if (netutils.IP4Address.IsValid(spice_bind) and
1591             spice_ip_version != constants.IP4_VERSION):
1592           raise errors.HypervisorError("spice: got an IPv4 address (%s), but"
1593                                        " the specified IP version is %s" %
1594                                        (spice_bind, spice_ip_version))
1595
1596         if (netutils.IP6Address.IsValid(spice_bind) and
1597             spice_ip_version != constants.IP6_VERSION):
1598           raise errors.HypervisorError("spice: got an IPv6 address (%s), but"
1599                                        " the specified IP version is %s" %
1600                                        (spice_bind, spice_ip_version))
1601     else:
1602       if spice_ip_version:
1603         raise errors.HypervisorError("spice: the %s option is useless"
1604                                      " without %s" %
1605                                      (constants.HV_KVM_SPICE_IP_VERSION,
1606                                       constants.HV_KVM_SPICE_BIND))
1607       if spice_password_file:
1608         raise errors.HypervisorError("spice: the %s option is useless"
1609                                      " without %s" %
1610                                      (constants.HV_KVM_SPICE_PASSWORD_FILE,
1611                                       constants.HV_KVM_SPICE_BIND))
1612
1613   @classmethod
1614   def ValidateParameters(cls, hvparams):
1615     """Check the given parameters for validity.
1616
1617     @type hvparams:  dict
1618     @param hvparams: dictionary with parameter names/value
1619     @raise errors.HypervisorError: when a parameter is not valid
1620
1621     """
1622     super(KVMHypervisor, cls).ValidateParameters(hvparams)
1623
1624     security_model = hvparams[constants.HV_SECURITY_MODEL]
1625     if security_model == constants.HT_SM_USER:
1626       username = hvparams[constants.HV_SECURITY_DOMAIN]
1627       try:
1628         pwd.getpwnam(username)
1629       except KeyError:
1630         raise errors.HypervisorError("Unknown security domain user %s"
1631                                      % username)
1632
1633     spice_bind = hvparams[constants.HV_KVM_SPICE_BIND]
1634     if spice_bind:
1635       # only one of VNC and SPICE can be used currently.
1636       if hvparams[constants.HV_VNC_BIND_ADDRESS]:
1637         raise errors.HypervisorError("both SPICE and VNC are configured, but"
1638                                      " only one of them can be used at a"
1639                                      " given time.")
1640
1641       # KVM version should be >= 0.14.0
1642       _, v_major, v_min, _ = cls._GetKVMVersion()
1643       if (v_major, v_min) < (0, 14):
1644         raise errors.HypervisorError("spice is configured, but it is not"
1645                                      " available in versions of KVM < 0.14")
1646
1647       # if spice_bind is not an IP address, it must be a valid interface
1648       bound_to_addr = (netutils.IP4Address.IsValid(spice_bind)
1649                        or netutils.IP6Address.IsValid(spice_bind))
1650       if not bound_to_addr and not netutils.IsValidInterface(spice_bind):
1651         raise errors.HypervisorError("spice: the %s parameter must be either"
1652                                      " a valid IP address or interface name" %
1653                                      constants.HV_KVM_SPICE_BIND)
1654
1655   @classmethod
1656   def PowercycleNode(cls):
1657     """KVM powercycle, just a wrapper over Linux powercycle.
1658
1659     """
1660     cls.LinuxPowercycle()