Statistics
| Branch: | Tag: | Revision:

root / lib / backend.py @ aea5caef

History | View | Annotate | Download (106.7 kB)

1 2f31098c Iustin Pop
#
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 397693d3 Iustin Pop
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Google Inc.
5 a8083063 Iustin Pop
#
6 a8083063 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 a8083063 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 a8083063 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 a8083063 Iustin Pop
# (at your option) any later version.
10 a8083063 Iustin Pop
#
11 a8083063 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 a8083063 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 a8083063 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 a8083063 Iustin Pop
# General Public License for more details.
15 a8083063 Iustin Pop
#
16 a8083063 Iustin Pop
# You should have received a copy of the GNU General Public License
17 a8083063 Iustin Pop
# along with this program; if not, write to the Free Software
18 a8083063 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 a8083063 Iustin Pop
# 02110-1301, USA.
20 a8083063 Iustin Pop
21 a8083063 Iustin Pop
22 360b0dc2 Iustin Pop
"""Functions used by the node daemon
23 360b0dc2 Iustin Pop

24 360b0dc2 Iustin Pop
@var _ALLOWED_UPLOAD_FILES: denotes which files are accepted in
25 360b0dc2 Iustin Pop
     the L{UploadFile} function
26 714ea7ca Iustin Pop
@var _ALLOWED_CLEAN_DIRS: denotes which directories are accepted
27 714ea7ca Iustin Pop
     in the L{_CleanDirectory} function
28 360b0dc2 Iustin Pop

29 360b0dc2 Iustin Pop
"""
30 a8083063 Iustin Pop
31 b459a848 Andrea Spadaccini
# pylint: disable=E1103
32 6c881c52 Iustin Pop
33 6c881c52 Iustin Pop
# E1103: %s %r has no %r member (but some types could not be
34 6c881c52 Iustin Pop
# inferred), because the _TryOSFromDisk returns either (True, os_obj)
35 6c881c52 Iustin Pop
# or (False, "string") which confuses pylint
36 6c881c52 Iustin Pop
37 a8083063 Iustin Pop
38 a8083063 Iustin Pop
import os
39 a8083063 Iustin Pop
import os.path
40 a8083063 Iustin Pop
import shutil
41 a8083063 Iustin Pop
import time
42 a8083063 Iustin Pop
import stat
43 a8083063 Iustin Pop
import errno
44 a8083063 Iustin Pop
import re
45 b544cfe0 Iustin Pop
import random
46 18682bca Iustin Pop
import logging
47 3b9e6a30 Iustin Pop
import tempfile
48 12bce260 Michael Hanselmann
import zlib
49 12bce260 Michael Hanselmann
import base64
50 f81c4737 Michael Hanselmann
import signal
51 a8083063 Iustin Pop
52 a8083063 Iustin Pop
from ganeti import errors
53 a8083063 Iustin Pop
from ganeti import utils
54 a8083063 Iustin Pop
from ganeti import ssh
55 a8083063 Iustin Pop
from ganeti import hypervisor
56 a8083063 Iustin Pop
from ganeti import constants
57 a8083063 Iustin Pop
from ganeti import bdev
58 a8083063 Iustin Pop
from ganeti import objects
59 880478f8 Iustin Pop
from ganeti import ssconf
60 1651d116 Michael Hanselmann
from ganeti import serializer
61 a744b676 Manuel Franceschini
from ganeti import netutils
62 82b22e19 René Nussbaumer
from ganeti import runtime
63 a8083063 Iustin Pop
64 a8083063 Iustin Pop
65 13998ef2 Michael Hanselmann
_BOOT_ID_PATH = "/proc/sys/kernel/random/boot_id"
66 714ea7ca Iustin Pop
_ALLOWED_CLEAN_DIRS = frozenset([
67 714ea7ca Iustin Pop
  constants.DATA_DIR,
68 714ea7ca Iustin Pop
  constants.JOB_QUEUE_ARCHIVE_DIR,
69 714ea7ca Iustin Pop
  constants.QUEUE_DIR,
70 f942a838 Michael Hanselmann
  constants.CRYPTO_KEYS_DIR,
71 714ea7ca Iustin Pop
  ])
72 f942a838 Michael Hanselmann
_MAX_SSL_CERT_VALIDITY = 7 * 24 * 60 * 60
73 f942a838 Michael Hanselmann
_X509_KEY_FILE = "key"
74 f942a838 Michael Hanselmann
_X509_CERT_FILE = "cert"
75 1651d116 Michael Hanselmann
_IES_STATUS_FILE = "status"
76 1651d116 Michael Hanselmann
_IES_PID_FILE = "pid"
77 1651d116 Michael Hanselmann
_IES_CA_FILE = "ca"
78 13998ef2 Michael Hanselmann
79 0b5303da Iustin Pop
#: Valid LVS output line regex
80 84d7e26b Dmitry Chernyak
_LVSLINE_REGEX = re.compile("^ *([^|]+)\|([^|]+)\|([0-9.]+)\|([^|]{6})\|?$")
81 0b5303da Iustin Pop
82 13998ef2 Michael Hanselmann
83 2cc6781a Iustin Pop
class RPCFail(Exception):
84 2cc6781a Iustin Pop
  """Class denoting RPC failure.
85 2cc6781a Iustin Pop

86 2cc6781a Iustin Pop
  Its argument is the error message.
87 2cc6781a Iustin Pop

88 2cc6781a Iustin Pop
  """
89 2cc6781a Iustin Pop
90 13998ef2 Michael Hanselmann
91 2cc6781a Iustin Pop
def _Fail(msg, *args, **kwargs):
92 2cc6781a Iustin Pop
  """Log an error and the raise an RPCFail exception.
93 2cc6781a Iustin Pop

94 2cc6781a Iustin Pop
  This exception is then handled specially in the ganeti daemon and
95 2cc6781a Iustin Pop
  turned into a 'failed' return type. As such, this function is a
96 2cc6781a Iustin Pop
  useful shortcut for logging the error and returning it to the master
97 2cc6781a Iustin Pop
  daemon.
98 2cc6781a Iustin Pop

99 2cc6781a Iustin Pop
  @type msg: string
100 2cc6781a Iustin Pop
  @param msg: the text of the exception
101 2cc6781a Iustin Pop
  @raise RPCFail
102 2cc6781a Iustin Pop

103 2cc6781a Iustin Pop
  """
104 2cc6781a Iustin Pop
  if args:
105 2cc6781a Iustin Pop
    msg = msg % args
106 afdc3985 Iustin Pop
  if "log" not in kwargs or kwargs["log"]: # if we should log this error
107 afdc3985 Iustin Pop
    if "exc" in kwargs and kwargs["exc"]:
108 afdc3985 Iustin Pop
      logging.exception(msg)
109 afdc3985 Iustin Pop
    else:
110 afdc3985 Iustin Pop
      logging.error(msg)
111 2cc6781a Iustin Pop
  raise RPCFail(msg)
112 2cc6781a Iustin Pop
113 2cc6781a Iustin Pop
114 c657dcc9 Michael Hanselmann
def _GetConfig():
115 93384844 Iustin Pop
  """Simple wrapper to return a SimpleStore.
116 10c2650b Iustin Pop

117 93384844 Iustin Pop
  @rtype: L{ssconf.SimpleStore}
118 93384844 Iustin Pop
  @return: a SimpleStore instance
119 10c2650b Iustin Pop

120 10c2650b Iustin Pop
  """
121 93384844 Iustin Pop
  return ssconf.SimpleStore()
122 c657dcc9 Michael Hanselmann
123 c657dcc9 Michael Hanselmann
124 62c9ec92 Iustin Pop
def _GetSshRunner(cluster_name):
125 10c2650b Iustin Pop
  """Simple wrapper to return an SshRunner.
126 10c2650b Iustin Pop

127 10c2650b Iustin Pop
  @type cluster_name: str
128 10c2650b Iustin Pop
  @param cluster_name: the cluster name, which is needed
129 10c2650b Iustin Pop
      by the SshRunner constructor
130 10c2650b Iustin Pop
  @rtype: L{ssh.SshRunner}
131 10c2650b Iustin Pop
  @return: an SshRunner instance
132 10c2650b Iustin Pop

133 10c2650b Iustin Pop
  """
134 62c9ec92 Iustin Pop
  return ssh.SshRunner(cluster_name)
135 c92b310a Michael Hanselmann
136 c92b310a Michael Hanselmann
137 12bce260 Michael Hanselmann
def _Decompress(data):
138 12bce260 Michael Hanselmann
  """Unpacks data compressed by the RPC client.
139 12bce260 Michael Hanselmann

140 12bce260 Michael Hanselmann
  @type data: list or tuple
141 12bce260 Michael Hanselmann
  @param data: Data sent by RPC client
142 12bce260 Michael Hanselmann
  @rtype: str
143 12bce260 Michael Hanselmann
  @return: Decompressed data
144 12bce260 Michael Hanselmann

145 12bce260 Michael Hanselmann
  """
146 52e2f66e Michael Hanselmann
  assert isinstance(data, (list, tuple))
147 12bce260 Michael Hanselmann
  assert len(data) == 2
148 12bce260 Michael Hanselmann
  (encoding, content) = data
149 12bce260 Michael Hanselmann
  if encoding == constants.RPC_ENCODING_NONE:
150 12bce260 Michael Hanselmann
    return content
151 12bce260 Michael Hanselmann
  elif encoding == constants.RPC_ENCODING_ZLIB_BASE64:
152 12bce260 Michael Hanselmann
    return zlib.decompress(base64.b64decode(content))
153 12bce260 Michael Hanselmann
  else:
154 12bce260 Michael Hanselmann
    raise AssertionError("Unknown data encoding")
155 12bce260 Michael Hanselmann
156 12bce260 Michael Hanselmann
157 3bc6be5c Iustin Pop
def _CleanDirectory(path, exclude=None):
158 76ab5558 Michael Hanselmann
  """Removes all regular files in a directory.
159 76ab5558 Michael Hanselmann

160 10c2650b Iustin Pop
  @type path: str
161 10c2650b Iustin Pop
  @param path: the directory to clean
162 76ab5558 Michael Hanselmann
  @type exclude: list
163 10c2650b Iustin Pop
  @param exclude: list of files to be excluded, defaults
164 10c2650b Iustin Pop
      to the empty list
165 76ab5558 Michael Hanselmann

166 76ab5558 Michael Hanselmann
  """
167 714ea7ca Iustin Pop
  if path not in _ALLOWED_CLEAN_DIRS:
168 714ea7ca Iustin Pop
    _Fail("Path passed to _CleanDirectory not in allowed clean targets: '%s'",
169 714ea7ca Iustin Pop
          path)
170 714ea7ca Iustin Pop
171 3956cee1 Michael Hanselmann
  if not os.path.isdir(path):
172 3956cee1 Michael Hanselmann
    return
173 3bc6be5c Iustin Pop
  if exclude is None:
174 3bc6be5c Iustin Pop
    exclude = []
175 3bc6be5c Iustin Pop
  else:
176 3bc6be5c Iustin Pop
    # Normalize excluded paths
177 3bc6be5c Iustin Pop
    exclude = [os.path.normpath(i) for i in exclude]
178 76ab5558 Michael Hanselmann
179 3956cee1 Michael Hanselmann
  for rel_name in utils.ListVisibleFiles(path):
180 c4feafe8 Iustin Pop
    full_name = utils.PathJoin(path, rel_name)
181 76ab5558 Michael Hanselmann
    if full_name in exclude:
182 76ab5558 Michael Hanselmann
      continue
183 3956cee1 Michael Hanselmann
    if os.path.isfile(full_name) and not os.path.islink(full_name):
184 3956cee1 Michael Hanselmann
      utils.RemoveFile(full_name)
185 3956cee1 Michael Hanselmann
186 3956cee1 Michael Hanselmann
187 360b0dc2 Iustin Pop
def _BuildUploadFileList():
188 360b0dc2 Iustin Pop
  """Build the list of allowed upload files.
189 360b0dc2 Iustin Pop

190 360b0dc2 Iustin Pop
  This is abstracted so that it's built only once at module import time.
191 360b0dc2 Iustin Pop

192 360b0dc2 Iustin Pop
  """
193 b397a7d2 Iustin Pop
  allowed_files = set([
194 b397a7d2 Iustin Pop
    constants.CLUSTER_CONF_FILE,
195 b397a7d2 Iustin Pop
    constants.ETC_HOSTS,
196 b397a7d2 Iustin Pop
    constants.SSH_KNOWN_HOSTS_FILE,
197 b397a7d2 Iustin Pop
    constants.VNC_PASSWORD_FILE,
198 b397a7d2 Iustin Pop
    constants.RAPI_CERT_FILE,
199 bfe86c76 Andrea Spadaccini
    constants.SPICE_CERT_FILE,
200 bfe86c76 Andrea Spadaccini
    constants.SPICE_CACERT_FILE,
201 b397a7d2 Iustin Pop
    constants.RAPI_USERS_FILE,
202 6b7d5878 Michael Hanselmann
    constants.CONFD_HMAC_KEY,
203 ff89a747 Michael Hanselmann
    constants.CLUSTER_DOMAIN_SECRET_FILE,
204 b397a7d2 Iustin Pop
    ])
205 b397a7d2 Iustin Pop
206 b397a7d2 Iustin Pop
  for hv_name in constants.HYPER_TYPES:
207 e5a45a16 Iustin Pop
    hv_class = hypervisor.GetHypervisorClass(hv_name)
208 b397a7d2 Iustin Pop
    allowed_files.update(hv_class.GetAncillaryFiles())
209 b397a7d2 Iustin Pop
210 b397a7d2 Iustin Pop
  return frozenset(allowed_files)
211 360b0dc2 Iustin Pop
212 360b0dc2 Iustin Pop
213 360b0dc2 Iustin Pop
_ALLOWED_UPLOAD_FILES = _BuildUploadFileList()
214 360b0dc2 Iustin Pop
215 360b0dc2 Iustin Pop
216 1bc59f76 Michael Hanselmann
def JobQueuePurge():
217 10c2650b Iustin Pop
  """Removes job queue files and archived jobs.
218 10c2650b Iustin Pop

219 c8457ce7 Iustin Pop
  @rtype: tuple
220 c8457ce7 Iustin Pop
  @return: True, None
221 24fc781f Michael Hanselmann

222 24fc781f Michael Hanselmann
  """
223 1bc59f76 Michael Hanselmann
  _CleanDirectory(constants.QUEUE_DIR, exclude=[constants.JOB_QUEUE_LOCK_FILE])
224 24fc781f Michael Hanselmann
  _CleanDirectory(constants.JOB_QUEUE_ARCHIVE_DIR)
225 24fc781f Michael Hanselmann
226 24fc781f Michael Hanselmann
227 bd1e4562 Iustin Pop
def GetMasterInfo():
228 bd1e4562 Iustin Pop
  """Returns master information.
229 bd1e4562 Iustin Pop

230 bd1e4562 Iustin Pop
  This is an utility function to compute master information, either
231 bd1e4562 Iustin Pop
  for consumption here or from the node daemon.
232 bd1e4562 Iustin Pop

233 bd1e4562 Iustin Pop
  @rtype: tuple
234 d8e0caa6 Manuel Franceschini
  @return: master_netdev, master_ip, master_name, primary_ip_family
235 2a52a064 Iustin Pop
  @raise RPCFail: in case of errors
236 b1b6ea87 Iustin Pop

237 b1b6ea87 Iustin Pop
  """
238 b1b6ea87 Iustin Pop
  try:
239 c657dcc9 Michael Hanselmann
    cfg = _GetConfig()
240 c657dcc9 Michael Hanselmann
    master_netdev = cfg.GetMasterNetdev()
241 c657dcc9 Michael Hanselmann
    master_ip = cfg.GetMasterIP()
242 c657dcc9 Michael Hanselmann
    master_node = cfg.GetMasterNode()
243 d8e0caa6 Manuel Franceschini
    primary_ip_family = cfg.GetPrimaryIPFamily()
244 b1b6ea87 Iustin Pop
  except errors.ConfigurationError, err:
245 29921401 Iustin Pop
    _Fail("Cluster configuration incomplete: %s", err, exc=True)
246 d8e0caa6 Manuel Franceschini
  return (master_netdev, master_ip, master_node, primary_ip_family)
247 b1b6ea87 Iustin Pop
248 b1b6ea87 Iustin Pop
249 3583908a Guido Trotter
def StartMaster(start_daemons, no_voting):
250 a8083063 Iustin Pop
  """Activate local node as master node.
251 a8083063 Iustin Pop

252 91492e57 Iustin Pop
  The function will either try activate the IP address of the master
253 91492e57 Iustin Pop
  (unless someone else has it) or also start the master daemons, based
254 91492e57 Iustin Pop
  on the start_daemons parameter.
255 10c2650b Iustin Pop

256 10c2650b Iustin Pop
  @type start_daemons: boolean
257 91492e57 Iustin Pop
  @param start_daemons: whether to start the master daemons
258 91492e57 Iustin Pop
      (ganeti-masterd and ganeti-rapi), or (if false) activate the
259 91492e57 Iustin Pop
      master ip
260 3583908a Guido Trotter
  @type no_voting: boolean
261 3583908a Guido Trotter
  @param no_voting: whether to start ganeti-masterd without a node vote
262 3583908a Guido Trotter
      (if start_daemons is True), but still non-interactively
263 10c2650b Iustin Pop
  @rtype: None
264 a8083063 Iustin Pop

265 a8083063 Iustin Pop
  """
266 2a52a064 Iustin Pop
  # GetMasterInfo will raise an exception if not able to return data
267 d8e0caa6 Manuel Franceschini
  master_netdev, master_ip, _, family = GetMasterInfo()
268 a8083063 Iustin Pop
269 396b5733 Iustin Pop
  err_msgs = []
270 91492e57 Iustin Pop
  # either start the master and rapi daemons
271 b1b6ea87 Iustin Pop
  if start_daemons:
272 3583908a Guido Trotter
    if no_voting:
273 f154a7a3 Michael Hanselmann
      masterd_args = "--no-voting --yes-do-it"
274 f154a7a3 Michael Hanselmann
    else:
275 f154a7a3 Michael Hanselmann
      masterd_args = ""
276 f154a7a3 Michael Hanselmann
277 f154a7a3 Michael Hanselmann
    env = {
278 f154a7a3 Michael Hanselmann
      "EXTRA_MASTERD_ARGS": masterd_args,
279 f154a7a3 Michael Hanselmann
      }
280 f154a7a3 Michael Hanselmann
281 f154a7a3 Michael Hanselmann
    result = utils.RunCmd([constants.DAEMON_UTIL, "start-master"], env=env)
282 f154a7a3 Michael Hanselmann
    if result.failed:
283 f154a7a3 Michael Hanselmann
      msg = "Can't start Ganeti master: %s" % result.output
284 f154a7a3 Michael Hanselmann
      logging.error(msg)
285 f154a7a3 Michael Hanselmann
      err_msgs.append(msg)
286 91492e57 Iustin Pop
  # or activate the IP
287 91492e57 Iustin Pop
  else:
288 91492e57 Iustin Pop
    if netutils.TcpPing(master_ip, constants.DEFAULT_NODED_PORT):
289 8b312c1d Manuel Franceschini
      if netutils.IPAddress.Own(master_ip):
290 91492e57 Iustin Pop
        # we already have the ip:
291 91492e57 Iustin Pop
        logging.debug("Master IP already configured, doing nothing")
292 91492e57 Iustin Pop
      else:
293 91492e57 Iustin Pop
        msg = "Someone else has the master ip, not activating"
294 91492e57 Iustin Pop
        logging.error(msg)
295 91492e57 Iustin Pop
        err_msgs.append(msg)
296 91492e57 Iustin Pop
    else:
297 d8e0caa6 Manuel Franceschini
      ipcls = netutils.IP4Address
298 d8e0caa6 Manuel Franceschini
      if family == netutils.IP6Address.family:
299 d8e0caa6 Manuel Franceschini
        ipcls = netutils.IP6Address
300 e7323b5e Manuel Franceschini
301 c4dfb0b6 Andrea Spadaccini
      result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "add",
302 d8e0caa6 Manuel Franceschini
                             "%s/%d" % (master_ip, ipcls.iplen),
303 91492e57 Iustin Pop
                             "dev", master_netdev, "label",
304 91492e57 Iustin Pop
                             "%s:0" % master_netdev])
305 91492e57 Iustin Pop
      if result.failed:
306 91492e57 Iustin Pop
        msg = "Can't activate master IP: %s" % result.output
307 91492e57 Iustin Pop
        logging.error(msg)
308 91492e57 Iustin Pop
        err_msgs.append(msg)
309 91492e57 Iustin Pop
310 e7323b5e Manuel Franceschini
      # we ignore the exit code of the following cmds
311 d8e0caa6 Manuel Franceschini
      if ipcls == netutils.IP4Address:
312 e7323b5e Manuel Franceschini
        utils.RunCmd(["arping", "-q", "-U", "-c 3", "-I", master_netdev, "-s",
313 e7323b5e Manuel Franceschini
                      master_ip, master_ip])
314 d8e0caa6 Manuel Franceschini
      elif ipcls == netutils.IP6Address:
315 2dc1237c Manuel Franceschini
        try:
316 2dc1237c Manuel Franceschini
          utils.RunCmd(["ndisc6", "-q", "-r 3", master_ip, master_netdev])
317 2dc1237c Manuel Franceschini
        except errors.OpExecError:
318 2dc1237c Manuel Franceschini
          # TODO: Better error reporting
319 2dc1237c Manuel Franceschini
          logging.warning("Can't execute ndisc6, please install if missing")
320 b726aff0 Iustin Pop
321 396b5733 Iustin Pop
  if err_msgs:
322 396b5733 Iustin Pop
    _Fail("; ".join(err_msgs))
323 afdc3985 Iustin Pop
324 a8083063 Iustin Pop
325 1c65840b Iustin Pop
def StopMaster(stop_daemons):
326 a8083063 Iustin Pop
  """Deactivate this node as master.
327 a8083063 Iustin Pop

328 1c65840b Iustin Pop
  The function will always try to deactivate the IP address of the
329 10c2650b Iustin Pop
  master. It will also stop the master daemons depending on the
330 10c2650b Iustin Pop
  stop_daemons parameter.
331 10c2650b Iustin Pop

332 10c2650b Iustin Pop
  @type stop_daemons: boolean
333 10c2650b Iustin Pop
  @param stop_daemons: whether to also stop the master daemons
334 10c2650b Iustin Pop
      (ganeti-masterd and ganeti-rapi)
335 10c2650b Iustin Pop
  @rtype: None
336 a8083063 Iustin Pop

337 a8083063 Iustin Pop
  """
338 6c00d19a Iustin Pop
  # TODO: log and report back to the caller the error failures; we
339 6c00d19a Iustin Pop
  # need to decide in which case we fail the RPC for this
340 2a52a064 Iustin Pop
341 2a52a064 Iustin Pop
  # GetMasterInfo will raise an exception if not able to return data
342 d8e0caa6 Manuel Franceschini
  master_netdev, master_ip, _, family = GetMasterInfo()
343 a8083063 Iustin Pop
344 d8e0caa6 Manuel Franceschini
  ipcls = netutils.IP4Address
345 d8e0caa6 Manuel Franceschini
  if family == netutils.IP6Address.family:
346 d8e0caa6 Manuel Franceschini
    ipcls = netutils.IP6Address
347 e7323b5e Manuel Franceschini
348 c4dfb0b6 Andrea Spadaccini
  result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "del",
349 d8e0caa6 Manuel Franceschini
                         "%s/%d" % (master_ip, ipcls.iplen),
350 b1b6ea87 Iustin Pop
                         "dev", master_netdev])
351 a8083063 Iustin Pop
  if result.failed:
352 3b9e6a30 Iustin Pop
    logging.error("Can't remove the master IP, error: %s", result.output)
353 b1b6ea87 Iustin Pop
    # but otherwise ignore the failure
354 b1b6ea87 Iustin Pop
355 b1b6ea87 Iustin Pop
  if stop_daemons:
356 f154a7a3 Michael Hanselmann
    result = utils.RunCmd([constants.DAEMON_UTIL, "stop-master"])
357 f154a7a3 Michael Hanselmann
    if result.failed:
358 f154a7a3 Michael Hanselmann
      logging.error("Could not stop Ganeti master, command %s had exitcode %s"
359 f154a7a3 Michael Hanselmann
                    " and error %s",
360 f154a7a3 Michael Hanselmann
                    result.cmd, result.exit_code, result.output)
361 a8083063 Iustin Pop
362 a8083063 Iustin Pop
363 19ddc57a René Nussbaumer
def EtcHostsModify(mode, host, ip):
364 19ddc57a René Nussbaumer
  """Modify a host entry in /etc/hosts.
365 19ddc57a René Nussbaumer

366 19ddc57a René Nussbaumer
  @param mode: The mode to operate. Either add or remove entry
367 19ddc57a René Nussbaumer
  @param host: The host to operate on
368 19ddc57a René Nussbaumer
  @param ip: The ip associated with the entry
369 19ddc57a René Nussbaumer

370 19ddc57a René Nussbaumer
  """
371 19ddc57a René Nussbaumer
  if mode == constants.ETC_HOSTS_ADD:
372 19ddc57a René Nussbaumer
    if not ip:
373 19ddc57a René Nussbaumer
      RPCFail("Mode 'add' needs 'ip' parameter, but parameter not"
374 19ddc57a René Nussbaumer
              " present")
375 19ddc57a René Nussbaumer
    utils.AddHostToEtcHosts(host, ip)
376 19ddc57a René Nussbaumer
  elif mode == constants.ETC_HOSTS_REMOVE:
377 19ddc57a René Nussbaumer
    if ip:
378 19ddc57a René Nussbaumer
      RPCFail("Mode 'remove' does not allow 'ip' parameter, but"
379 19ddc57a René Nussbaumer
              " parameter is present")
380 19ddc57a René Nussbaumer
    utils.RemoveHostFromEtcHosts(host)
381 19ddc57a René Nussbaumer
  else:
382 19ddc57a René Nussbaumer
    RPCFail("Mode not supported")
383 19ddc57a René Nussbaumer
384 19ddc57a René Nussbaumer
385 b989b9d9 Ken Wehr
def LeaveCluster(modify_ssh_setup):
386 10c2650b Iustin Pop
  """Cleans up and remove the current node.
387 10c2650b Iustin Pop

388 10c2650b Iustin Pop
  This function cleans up and prepares the current node to be removed
389 10c2650b Iustin Pop
  from the cluster.
390 10c2650b Iustin Pop

391 10c2650b Iustin Pop
  If processing is successful, then it raises an
392 c41eea6e Iustin Pop
  L{errors.QuitGanetiException} which is used as a special case to
393 10c2650b Iustin Pop
  shutdown the node daemon.
394 a8083063 Iustin Pop

395 b989b9d9 Ken Wehr
  @param modify_ssh_setup: boolean
396 b989b9d9 Ken Wehr

397 a8083063 Iustin Pop
  """
398 f78346f5 Michael Hanselmann
  _CleanDirectory(constants.DATA_DIR)
399 f942a838 Michael Hanselmann
  _CleanDirectory(constants.CRYPTO_KEYS_DIR)
400 1bc59f76 Michael Hanselmann
  JobQueuePurge()
401 f78346f5 Michael Hanselmann
402 b989b9d9 Ken Wehr
  if modify_ssh_setup:
403 b989b9d9 Ken Wehr
    try:
404 b989b9d9 Ken Wehr
      priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS)
405 7900ed01 Iustin Pop
406 b989b9d9 Ken Wehr
      utils.RemoveAuthorizedKey(auth_keys, utils.ReadFile(pub_key))
407 a8083063 Iustin Pop
408 b989b9d9 Ken Wehr
      utils.RemoveFile(priv_key)
409 b989b9d9 Ken Wehr
      utils.RemoveFile(pub_key)
410 b989b9d9 Ken Wehr
    except errors.OpExecError:
411 b989b9d9 Ken Wehr
      logging.exception("Error while processing ssh files")
412 a8083063 Iustin Pop
413 ed008420 Guido Trotter
  try:
414 6b7d5878 Michael Hanselmann
    utils.RemoveFile(constants.CONFD_HMAC_KEY)
415 ed008420 Guido Trotter
    utils.RemoveFile(constants.RAPI_CERT_FILE)
416 bfe86c76 Andrea Spadaccini
    utils.RemoveFile(constants.SPICE_CERT_FILE)
417 bfe86c76 Andrea Spadaccini
    utils.RemoveFile(constants.SPICE_CACERT_FILE)
418 168c1de2 Michael Hanselmann
    utils.RemoveFile(constants.NODED_CERT_FILE)
419 b459a848 Andrea Spadaccini
  except: # pylint: disable=W0702
420 ed008420 Guido Trotter
    logging.exception("Error while removing cluster secrets")
421 ed008420 Guido Trotter
422 f154a7a3 Michael Hanselmann
  result = utils.RunCmd([constants.DAEMON_UTIL, "stop", constants.CONFD])
423 f154a7a3 Michael Hanselmann
  if result.failed:
424 f154a7a3 Michael Hanselmann
    logging.error("Command %s failed with exitcode %s and error %s",
425 f154a7a3 Michael Hanselmann
                  result.cmd, result.exit_code, result.output)
426 ed008420 Guido Trotter
427 0623d351 Iustin Pop
  # Raise a custom exception (handled in ganeti-noded)
428 d0c8c01d Iustin Pop
  raise errors.QuitGanetiException(True, "Shutdown scheduled")
429 6d8b6238 Guido Trotter
430 a8083063 Iustin Pop
431 e69d05fd Iustin Pop
def GetNodeInfo(vgname, hypervisor_type):
432 5bbd3f7f Michael Hanselmann
  """Gives back a hash with different information about the node.
433 a8083063 Iustin Pop

434 e69d05fd Iustin Pop
  @type vgname: C{string}
435 e69d05fd Iustin Pop
  @param vgname: the name of the volume group to ask for disk space information
436 e69d05fd Iustin Pop
  @type hypervisor_type: C{str}
437 e69d05fd Iustin Pop
  @param hypervisor_type: the name of the hypervisor to ask for
438 e69d05fd Iustin Pop
      memory information
439 e69d05fd Iustin Pop
  @rtype: C{dict}
440 e69d05fd Iustin Pop
  @return: dictionary with the following keys:
441 e69d05fd Iustin Pop
      - vg_size is the size of the configured volume group in MiB
442 e69d05fd Iustin Pop
      - vg_free is the free size of the volume group in MiB
443 e69d05fd Iustin Pop
      - memory_dom0 is the memory allocated for domain0 in MiB
444 e69d05fd Iustin Pop
      - memory_free is the currently available (free) ram in MiB
445 e69d05fd Iustin Pop
      - memory_total is the total number of ram in MiB
446 a8083063 Iustin Pop

447 098c0958 Michael Hanselmann
  """
448 a8083063 Iustin Pop
  outputarray = {}
449 673cd9c4 René Nussbaumer
450 cb6a0296 Iustin Pop
  if vgname is not None:
451 cb6a0296 Iustin Pop
    vginfo = bdev.LogicalVolume.GetVGInfo([vgname])
452 cb6a0296 Iustin Pop
    vg_free = vg_size = None
453 cb6a0296 Iustin Pop
    if vginfo:
454 cb6a0296 Iustin Pop
      vg_free = int(round(vginfo[0][0], 0))
455 cb6a0296 Iustin Pop
      vg_size = int(round(vginfo[0][1], 0))
456 d0c8c01d Iustin Pop
    outputarray["vg_size"] = vg_size
457 d0c8c01d Iustin Pop
    outputarray["vg_free"] = vg_free
458 cb6a0296 Iustin Pop
459 cb6a0296 Iustin Pop
  if hypervisor_type is not None:
460 cb6a0296 Iustin Pop
    hyper = hypervisor.GetHypervisor(hypervisor_type)
461 cb6a0296 Iustin Pop
    hyp_info = hyper.GetNodeInfo()
462 cb6a0296 Iustin Pop
    if hyp_info is not None:
463 cb6a0296 Iustin Pop
      outputarray.update(hyp_info)
464 a8083063 Iustin Pop
465 13998ef2 Michael Hanselmann
  outputarray["bootid"] = utils.ReadFile(_BOOT_ID_PATH, size=128).rstrip("\n")
466 3ef10550 Michael Hanselmann
467 c26a6bd2 Iustin Pop
  return outputarray
468 a8083063 Iustin Pop
469 a8083063 Iustin Pop
470 62c9ec92 Iustin Pop
def VerifyNode(what, cluster_name):
471 a8083063 Iustin Pop
  """Verify the status of the local node.
472 a8083063 Iustin Pop

473 e69d05fd Iustin Pop
  Based on the input L{what} parameter, various checks are done on the
474 e69d05fd Iustin Pop
  local node.
475 e69d05fd Iustin Pop

476 e69d05fd Iustin Pop
  If the I{filelist} key is present, this list of
477 e69d05fd Iustin Pop
  files is checksummed and the file/checksum pairs are returned.
478 e69d05fd Iustin Pop

479 e69d05fd Iustin Pop
  If the I{nodelist} key is present, we check that we have
480 e69d05fd Iustin Pop
  connectivity via ssh with the target nodes (and check the hostname
481 e69d05fd Iustin Pop
  report).
482 a8083063 Iustin Pop

483 e69d05fd Iustin Pop
  If the I{node-net-test} key is present, we check that we have
484 e69d05fd Iustin Pop
  connectivity to the given nodes via both primary IP and, if
485 e69d05fd Iustin Pop
  applicable, secondary IPs.
486 e69d05fd Iustin Pop

487 e69d05fd Iustin Pop
  @type what: C{dict}
488 e69d05fd Iustin Pop
  @param what: a dictionary of things to check:
489 e69d05fd Iustin Pop
      - filelist: list of files for which to compute checksums
490 e69d05fd Iustin Pop
      - nodelist: list of nodes we should check ssh communication with
491 e69d05fd Iustin Pop
      - node-net-test: list of nodes we should check node daemon port
492 e69d05fd Iustin Pop
        connectivity with
493 e69d05fd Iustin Pop
      - hypervisor: list with hypervisors to run the verify for
494 10c2650b Iustin Pop
  @rtype: dict
495 10c2650b Iustin Pop
  @return: a dictionary with the same keys as the input dict, and
496 10c2650b Iustin Pop
      values representing the result of the checks
497 a8083063 Iustin Pop

498 a8083063 Iustin Pop
  """
499 a8083063 Iustin Pop
  result = {}
500 b705c7a6 Manuel Franceschini
  my_name = netutils.Hostname.GetSysName()
501 a744b676 Manuel Franceschini
  port = netutils.GetDaemonPort(constants.NODED)
502 8964ee14 Iustin Pop
  vm_capable = my_name not in what.get(constants.NV_VMNODES, [])
503 a8083063 Iustin Pop
504 8964ee14 Iustin Pop
  if constants.NV_HYPERVISOR in what and vm_capable:
505 25361b9a Iustin Pop
    result[constants.NV_HYPERVISOR] = tmp = {}
506 25361b9a Iustin Pop
    for hv_name in what[constants.NV_HYPERVISOR]:
507 0cf5e7f5 Iustin Pop
      try:
508 0cf5e7f5 Iustin Pop
        val = hypervisor.GetHypervisor(hv_name).Verify()
509 0cf5e7f5 Iustin Pop
      except errors.HypervisorError, err:
510 0cf5e7f5 Iustin Pop
        val = "Error while checking hypervisor: %s" % str(err)
511 0cf5e7f5 Iustin Pop
      tmp[hv_name] = val
512 25361b9a Iustin Pop
513 58a59652 Iustin Pop
  if constants.NV_HVPARAMS in what and vm_capable:
514 58a59652 Iustin Pop
    result[constants.NV_HVPARAMS] = tmp = []
515 58a59652 Iustin Pop
    for source, hv_name, hvparms in what[constants.NV_HVPARAMS]:
516 58a59652 Iustin Pop
      try:
517 58a59652 Iustin Pop
        logging.info("Validating hv %s, %s", hv_name, hvparms)
518 58a59652 Iustin Pop
        hypervisor.GetHypervisor(hv_name).ValidateParameters(hvparms)
519 58a59652 Iustin Pop
      except errors.HypervisorError, err:
520 58a59652 Iustin Pop
        tmp.append((source, hv_name, str(err)))
521 58a59652 Iustin Pop
522 25361b9a Iustin Pop
  if constants.NV_FILELIST in what:
523 25361b9a Iustin Pop
    result[constants.NV_FILELIST] = utils.FingerprintFiles(
524 25361b9a Iustin Pop
      what[constants.NV_FILELIST])
525 25361b9a Iustin Pop
526 25361b9a Iustin Pop
  if constants.NV_NODELIST in what:
527 25361b9a Iustin Pop
    result[constants.NV_NODELIST] = tmp = {}
528 25361b9a Iustin Pop
    random.shuffle(what[constants.NV_NODELIST])
529 25361b9a Iustin Pop
    for node in what[constants.NV_NODELIST]:
530 62c9ec92 Iustin Pop
      success, message = _GetSshRunner(cluster_name).VerifyNodeHostname(node)
531 a8083063 Iustin Pop
      if not success:
532 25361b9a Iustin Pop
        tmp[node] = message
533 25361b9a Iustin Pop
534 25361b9a Iustin Pop
  if constants.NV_NODENETTEST in what:
535 25361b9a Iustin Pop
    result[constants.NV_NODENETTEST] = tmp = {}
536 9d4bfc96 Iustin Pop
    my_pip = my_sip = None
537 25361b9a Iustin Pop
    for name, pip, sip in what[constants.NV_NODENETTEST]:
538 9d4bfc96 Iustin Pop
      if name == my_name:
539 9d4bfc96 Iustin Pop
        my_pip = pip
540 9d4bfc96 Iustin Pop
        my_sip = sip
541 9d4bfc96 Iustin Pop
        break
542 9d4bfc96 Iustin Pop
    if not my_pip:
543 25361b9a Iustin Pop
      tmp[my_name] = ("Can't find my own primary/secondary IP"
544 25361b9a Iustin Pop
                      " in the node list")
545 9d4bfc96 Iustin Pop
    else:
546 25361b9a Iustin Pop
      for name, pip, sip in what[constants.NV_NODENETTEST]:
547 9d4bfc96 Iustin Pop
        fail = []
548 a744b676 Manuel Franceschini
        if not netutils.TcpPing(pip, port, source=my_pip):
549 9d4bfc96 Iustin Pop
          fail.append("primary")
550 9d4bfc96 Iustin Pop
        if sip != pip:
551 a744b676 Manuel Franceschini
          if not netutils.TcpPing(sip, port, source=my_sip):
552 9d4bfc96 Iustin Pop
            fail.append("secondary")
553 9d4bfc96 Iustin Pop
        if fail:
554 25361b9a Iustin Pop
          tmp[name] = ("failure using the %s interface(s)" %
555 25361b9a Iustin Pop
                       " and ".join(fail))
556 25361b9a Iustin Pop
557 a3a5f850 Iustin Pop
  if constants.NV_MASTERIP in what:
558 a3a5f850 Iustin Pop
    # FIXME: add checks on incoming data structures (here and in the
559 a3a5f850 Iustin Pop
    # rest of the function)
560 a3a5f850 Iustin Pop
    master_name, master_ip = what[constants.NV_MASTERIP]
561 a3a5f850 Iustin Pop
    if master_name == my_name:
562 9769bb78 Manuel Franceschini
      source = constants.IP4_ADDRESS_LOCALHOST
563 a3a5f850 Iustin Pop
    else:
564 a3a5f850 Iustin Pop
      source = None
565 a744b676 Manuel Franceschini
    result[constants.NV_MASTERIP] = netutils.TcpPing(master_ip, port,
566 a3a5f850 Iustin Pop
                                                  source=source)
567 a3a5f850 Iustin Pop
568 16f41f24 René Nussbaumer
  if constants.NV_OOB_PATHS in what:
569 16f41f24 René Nussbaumer
    result[constants.NV_OOB_PATHS] = tmp = []
570 16f41f24 René Nussbaumer
    for path in what[constants.NV_OOB_PATHS]:
571 16f41f24 René Nussbaumer
      try:
572 16f41f24 René Nussbaumer
        st = os.stat(path)
573 16f41f24 René Nussbaumer
      except OSError, err:
574 16f41f24 René Nussbaumer
        tmp.append("error stating out of band helper: %s" % err)
575 16f41f24 René Nussbaumer
      else:
576 16f41f24 René Nussbaumer
        if stat.S_ISREG(st.st_mode):
577 16f41f24 René Nussbaumer
          if stat.S_IMODE(st.st_mode) & stat.S_IXUSR:
578 16f41f24 René Nussbaumer
            tmp.append(None)
579 16f41f24 René Nussbaumer
          else:
580 16f41f24 René Nussbaumer
            tmp.append("out of band helper %s is not executable" % path)
581 16f41f24 René Nussbaumer
        else:
582 16f41f24 René Nussbaumer
          tmp.append("out of band helper %s is not a file" % path)
583 16f41f24 René Nussbaumer
584 8964ee14 Iustin Pop
  if constants.NV_LVLIST in what and vm_capable:
585 ed904904 Iustin Pop
    try:
586 84d7e26b Dmitry Chernyak
      val = GetVolumeList(utils.ListVolumeGroups().keys())
587 ed904904 Iustin Pop
    except RPCFail, err:
588 ed904904 Iustin Pop
      val = str(err)
589 ed904904 Iustin Pop
    result[constants.NV_LVLIST] = val
590 25361b9a Iustin Pop
591 8964ee14 Iustin Pop
  if constants.NV_INSTANCELIST in what and vm_capable:
592 0cf5e7f5 Iustin Pop
    # GetInstanceList can fail
593 0cf5e7f5 Iustin Pop
    try:
594 0cf5e7f5 Iustin Pop
      val = GetInstanceList(what[constants.NV_INSTANCELIST])
595 0cf5e7f5 Iustin Pop
    except RPCFail, err:
596 0cf5e7f5 Iustin Pop
      val = str(err)
597 0cf5e7f5 Iustin Pop
    result[constants.NV_INSTANCELIST] = val
598 25361b9a Iustin Pop
599 8964ee14 Iustin Pop
  if constants.NV_VGLIST in what and vm_capable:
600 e480923b Iustin Pop
    result[constants.NV_VGLIST] = utils.ListVolumeGroups()
601 25361b9a Iustin Pop
602 8964ee14 Iustin Pop
  if constants.NV_PVLIST in what and vm_capable:
603 d091393e Iustin Pop
    result[constants.NV_PVLIST] = \
604 d091393e Iustin Pop
      bdev.LogicalVolume.GetPVInfo(what[constants.NV_PVLIST],
605 d091393e Iustin Pop
                                   filter_allocatable=False)
606 d091393e Iustin Pop
607 25361b9a Iustin Pop
  if constants.NV_VERSION in what:
608 e9ce0a64 Iustin Pop
    result[constants.NV_VERSION] = (constants.PROTOCOL_VERSION,
609 e9ce0a64 Iustin Pop
                                    constants.RELEASE_VERSION)
610 25361b9a Iustin Pop
611 8964ee14 Iustin Pop
  if constants.NV_HVINFO in what and vm_capable:
612 25361b9a Iustin Pop
    hyper = hypervisor.GetHypervisor(what[constants.NV_HVINFO])
613 25361b9a Iustin Pop
    result[constants.NV_HVINFO] = hyper.GetNodeInfo()
614 9d4bfc96 Iustin Pop
615 8964ee14 Iustin Pop
  if constants.NV_DRBDLIST in what and vm_capable:
616 6d2e83d5 Iustin Pop
    try:
617 6d2e83d5 Iustin Pop
      used_minors = bdev.DRBD8.GetUsedDevs().keys()
618 f6eaed12 Iustin Pop
    except errors.BlockDeviceError, err:
619 6d2e83d5 Iustin Pop
      logging.warning("Can't get used minors list", exc_info=True)
620 f6eaed12 Iustin Pop
      used_minors = str(err)
621 6d2e83d5 Iustin Pop
    result[constants.NV_DRBDLIST] = used_minors
622 6d2e83d5 Iustin Pop
623 8964ee14 Iustin Pop
  if constants.NV_DRBDHELPER in what and vm_capable:
624 7ef40fbe Luca Bigliardi
    status = True
625 7ef40fbe Luca Bigliardi
    try:
626 7ef40fbe Luca Bigliardi
      payload = bdev.BaseDRBD.GetUsermodeHelper()
627 7ef40fbe Luca Bigliardi
    except errors.BlockDeviceError, err:
628 7ef40fbe Luca Bigliardi
      logging.error("Can't get DRBD usermode helper: %s", str(err))
629 7ef40fbe Luca Bigliardi
      status = False
630 7ef40fbe Luca Bigliardi
      payload = str(err)
631 7ef40fbe Luca Bigliardi
    result[constants.NV_DRBDHELPER] = (status, payload)
632 7ef40fbe Luca Bigliardi
633 7c0aa8e9 Iustin Pop
  if constants.NV_NODESETUP in what:
634 7c0aa8e9 Iustin Pop
    result[constants.NV_NODESETUP] = tmpr = []
635 7c0aa8e9 Iustin Pop
    if not os.path.isdir("/sys/block") or not os.path.isdir("/sys/class/net"):
636 7c0aa8e9 Iustin Pop
      tmpr.append("The sysfs filesytem doesn't seem to be mounted"
637 7c0aa8e9 Iustin Pop
                  " under /sys, missing required directories /sys/block"
638 7c0aa8e9 Iustin Pop
                  " and /sys/class/net")
639 7c0aa8e9 Iustin Pop
    if (not os.path.isdir("/proc/sys") or
640 7c0aa8e9 Iustin Pop
        not os.path.isfile("/proc/sysrq-trigger")):
641 7c0aa8e9 Iustin Pop
      tmpr.append("The procfs filesystem doesn't seem to be mounted"
642 7c0aa8e9 Iustin Pop
                  " under /proc, missing required directory /proc/sys and"
643 7c0aa8e9 Iustin Pop
                  " the file /proc/sysrq-trigger")
644 313b2dd4 Michael Hanselmann
645 313b2dd4 Michael Hanselmann
  if constants.NV_TIME in what:
646 313b2dd4 Michael Hanselmann
    result[constants.NV_TIME] = utils.SplitTime(time.time())
647 313b2dd4 Michael Hanselmann
648 8964ee14 Iustin Pop
  if constants.NV_OSLIST in what and vm_capable:
649 b0d85178 Iustin Pop
    result[constants.NV_OSLIST] = DiagnoseOS()
650 b0d85178 Iustin Pop
651 20d317d4 Iustin Pop
  if constants.NV_BRIDGES in what and vm_capable:
652 20d317d4 Iustin Pop
    result[constants.NV_BRIDGES] = [bridge
653 20d317d4 Iustin Pop
                                    for bridge in what[constants.NV_BRIDGES]
654 20d317d4 Iustin Pop
                                    if not utils.BridgeExists(bridge)]
655 c26a6bd2 Iustin Pop
  return result
656 a8083063 Iustin Pop
657 a8083063 Iustin Pop
658 2be7273c Apollon Oikonomopoulos
def GetBlockDevSizes(devices):
659 2be7273c Apollon Oikonomopoulos
  """Return the size of the given block devices
660 2be7273c Apollon Oikonomopoulos

661 2be7273c Apollon Oikonomopoulos
  @type devices: list
662 2be7273c Apollon Oikonomopoulos
  @param devices: list of block device nodes to query
663 2be7273c Apollon Oikonomopoulos
  @rtype: dict
664 2be7273c Apollon Oikonomopoulos
  @return:
665 2be7273c Apollon Oikonomopoulos
    dictionary of all block devices under /dev (key). The value is their
666 2be7273c Apollon Oikonomopoulos
    size in MiB.
667 2be7273c Apollon Oikonomopoulos

668 2be7273c Apollon Oikonomopoulos
    {'/dev/disk/by-uuid/123456-12321231-312312-312': 124}
669 2be7273c Apollon Oikonomopoulos

670 2be7273c Apollon Oikonomopoulos
  """
671 2be7273c Apollon Oikonomopoulos
  DEV_PREFIX = "/dev/"
672 2be7273c Apollon Oikonomopoulos
  blockdevs = {}
673 2be7273c Apollon Oikonomopoulos
674 2be7273c Apollon Oikonomopoulos
  for devpath in devices:
675 cf00dba0 René Nussbaumer
    if not utils.IsBelowDir(DEV_PREFIX, devpath):
676 2be7273c Apollon Oikonomopoulos
      continue
677 2be7273c Apollon Oikonomopoulos
678 2be7273c Apollon Oikonomopoulos
    try:
679 2be7273c Apollon Oikonomopoulos
      st = os.stat(devpath)
680 2be7273c Apollon Oikonomopoulos
    except EnvironmentError, err:
681 2be7273c Apollon Oikonomopoulos
      logging.warning("Error stat()'ing device %s: %s", devpath, str(err))
682 2be7273c Apollon Oikonomopoulos
      continue
683 2be7273c Apollon Oikonomopoulos
684 2be7273c Apollon Oikonomopoulos
    if stat.S_ISBLK(st.st_mode):
685 2be7273c Apollon Oikonomopoulos
      result = utils.RunCmd(["blockdev", "--getsize64", devpath])
686 2be7273c Apollon Oikonomopoulos
      if result.failed:
687 2be7273c Apollon Oikonomopoulos
        # We don't want to fail, just do not list this device as available
688 2be7273c Apollon Oikonomopoulos
        logging.warning("Cannot get size for block device %s", devpath)
689 2be7273c Apollon Oikonomopoulos
        continue
690 2be7273c Apollon Oikonomopoulos
691 2be7273c Apollon Oikonomopoulos
      size = int(result.stdout) / (1024 * 1024)
692 2be7273c Apollon Oikonomopoulos
      blockdevs[devpath] = size
693 2be7273c Apollon Oikonomopoulos
  return blockdevs
694 2be7273c Apollon Oikonomopoulos
695 2be7273c Apollon Oikonomopoulos
696 84d7e26b Dmitry Chernyak
def GetVolumeList(vg_names):
697 a8083063 Iustin Pop
  """Compute list of logical volumes and their size.
698 a8083063 Iustin Pop

699 84d7e26b Dmitry Chernyak
  @type vg_names: list
700 397693d3 Iustin Pop
  @param vg_names: the volume groups whose LVs we should list, or
701 397693d3 Iustin Pop
      empty for all volume groups
702 10c2650b Iustin Pop
  @rtype: dict
703 10c2650b Iustin Pop
  @return:
704 10c2650b Iustin Pop
      dictionary of all partions (key) with value being a tuple of
705 10c2650b Iustin Pop
      their size (in MiB), inactive and online status::
706 10c2650b Iustin Pop

707 84d7e26b Dmitry Chernyak
        {'xenvg/test1': ('20.06', True, True)}
708 10c2650b Iustin Pop

709 10c2650b Iustin Pop
      in case of errors, a string is returned with the error
710 10c2650b Iustin Pop
      details.
711 a8083063 Iustin Pop

712 a8083063 Iustin Pop
  """
713 cb2037a2 Iustin Pop
  lvs = {}
714 d0c8c01d Iustin Pop
  sep = "|"
715 397693d3 Iustin Pop
  if not vg_names:
716 397693d3 Iustin Pop
    vg_names = []
717 cb2037a2 Iustin Pop
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
718 cb2037a2 Iustin Pop
                         "--separator=%s" % sep,
719 84d7e26b Dmitry Chernyak
                         "-ovg_name,lv_name,lv_size,lv_attr"] + vg_names)
720 a8083063 Iustin Pop
  if result.failed:
721 29d376ec Iustin Pop
    _Fail("Failed to list logical volumes, lvs output: %s", result.output)
722 cb2037a2 Iustin Pop
723 cb2037a2 Iustin Pop
  for line in result.stdout.splitlines():
724 df4c2628 Iustin Pop
    line = line.strip()
725 0b5303da Iustin Pop
    match = _LVSLINE_REGEX.match(line)
726 df4c2628 Iustin Pop
    if not match:
727 18682bca Iustin Pop
      logging.error("Invalid line returned from lvs output: '%s'", line)
728 df4c2628 Iustin Pop
      continue
729 84d7e26b Dmitry Chernyak
    vg_name, name, size, attr = match.groups()
730 d0c8c01d Iustin Pop
    inactive = attr[4] == "-"
731 d0c8c01d Iustin Pop
    online = attr[5] == "o"
732 d0c8c01d Iustin Pop
    virtual = attr[0] == "v"
733 33f2a81a Iustin Pop
    if virtual:
734 33f2a81a Iustin Pop
      # we don't want to report such volumes as existing, since they
735 33f2a81a Iustin Pop
      # don't really hold data
736 33f2a81a Iustin Pop
      continue
737 e687ec01 Michael Hanselmann
    lvs[vg_name + "/" + name] = (size, inactive, online)
738 cb2037a2 Iustin Pop
739 cb2037a2 Iustin Pop
  return lvs
740 a8083063 Iustin Pop
741 a8083063 Iustin Pop
742 a8083063 Iustin Pop
def ListVolumeGroups():
743 2f8598a5 Alexander Schreiber
  """List the volume groups and their size.
744 a8083063 Iustin Pop

745 10c2650b Iustin Pop
  @rtype: dict
746 10c2650b Iustin Pop
  @return: dictionary with keys volume name and values the
747 10c2650b Iustin Pop
      size of the volume
748 a8083063 Iustin Pop

749 a8083063 Iustin Pop
  """
750 c26a6bd2 Iustin Pop
  return utils.ListVolumeGroups()
751 a8083063 Iustin Pop
752 a8083063 Iustin Pop
753 dcb93971 Michael Hanselmann
def NodeVolumes():
754 dcb93971 Michael Hanselmann
  """List all volumes on this node.
755 dcb93971 Michael Hanselmann

756 10c2650b Iustin Pop
  @rtype: list
757 10c2650b Iustin Pop
  @return:
758 10c2650b Iustin Pop
    A list of dictionaries, each having four keys:
759 10c2650b Iustin Pop
      - name: the logical volume name,
760 10c2650b Iustin Pop
      - size: the size of the logical volume
761 10c2650b Iustin Pop
      - dev: the physical device on which the LV lives
762 10c2650b Iustin Pop
      - vg: the volume group to which it belongs
763 10c2650b Iustin Pop

764 10c2650b Iustin Pop
    In case of errors, we return an empty list and log the
765 10c2650b Iustin Pop
    error.
766 10c2650b Iustin Pop

767 10c2650b Iustin Pop
    Note that since a logical volume can live on multiple physical
768 10c2650b Iustin Pop
    volumes, the resulting list might include a logical volume
769 10c2650b Iustin Pop
    multiple times.
770 10c2650b Iustin Pop

771 dcb93971 Michael Hanselmann
  """
772 dcb93971 Michael Hanselmann
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
773 dcb93971 Michael Hanselmann
                         "--separator=|",
774 dcb93971 Michael Hanselmann
                         "--options=lv_name,lv_size,devices,vg_name"])
775 dcb93971 Michael Hanselmann
  if result.failed:
776 10bfe6cb Iustin Pop
    _Fail("Failed to list logical volumes, lvs output: %s",
777 10bfe6cb Iustin Pop
          result.output)
778 dcb93971 Michael Hanselmann
779 dcb93971 Michael Hanselmann
  def parse_dev(dev):
780 d0c8c01d Iustin Pop
    return dev.split("(")[0]
781 89e5ab02 Iustin Pop
782 89e5ab02 Iustin Pop
  def handle_dev(dev):
783 89e5ab02 Iustin Pop
    return [parse_dev(x) for x in dev.split(",")]
784 dcb93971 Michael Hanselmann
785 dcb93971 Michael Hanselmann
  def map_line(line):
786 89e5ab02 Iustin Pop
    line = [v.strip() for v in line]
787 d0c8c01d Iustin Pop
    return [{"name": line[0], "size": line[1],
788 d0c8c01d Iustin Pop
             "dev": dev, "vg": line[3]} for dev in handle_dev(line[2])]
789 89e5ab02 Iustin Pop
790 89e5ab02 Iustin Pop
  all_devs = []
791 89e5ab02 Iustin Pop
  for line in result.stdout.splitlines():
792 d0c8c01d Iustin Pop
    if line.count("|") >= 3:
793 d0c8c01d Iustin Pop
      all_devs.extend(map_line(line.split("|")))
794 89e5ab02 Iustin Pop
    else:
795 89e5ab02 Iustin Pop
      logging.warning("Strange line in the output from lvs: '%s'", line)
796 89e5ab02 Iustin Pop
  return all_devs
797 dcb93971 Michael Hanselmann
798 dcb93971 Michael Hanselmann
799 a8083063 Iustin Pop
def BridgesExist(bridges_list):
800 2f8598a5 Alexander Schreiber
  """Check if a list of bridges exist on the current node.
801 a8083063 Iustin Pop

802 b1206984 Iustin Pop
  @rtype: boolean
803 b1206984 Iustin Pop
  @return: C{True} if all of them exist, C{False} otherwise
804 a8083063 Iustin Pop

805 a8083063 Iustin Pop
  """
806 35c0c8da Iustin Pop
  missing = []
807 a8083063 Iustin Pop
  for bridge in bridges_list:
808 a8083063 Iustin Pop
    if not utils.BridgeExists(bridge):
809 35c0c8da Iustin Pop
      missing.append(bridge)
810 a8083063 Iustin Pop
811 35c0c8da Iustin Pop
  if missing:
812 1f864b60 Iustin Pop
    _Fail("Missing bridges %s", utils.CommaJoin(missing))
813 35c0c8da Iustin Pop
814 a8083063 Iustin Pop
815 e69d05fd Iustin Pop
def GetInstanceList(hypervisor_list):
816 2f8598a5 Alexander Schreiber
  """Provides a list of instances.
817 a8083063 Iustin Pop

818 e69d05fd Iustin Pop
  @type hypervisor_list: list
819 e69d05fd Iustin Pop
  @param hypervisor_list: the list of hypervisors to query information
820 e69d05fd Iustin Pop

821 e69d05fd Iustin Pop
  @rtype: list
822 e69d05fd Iustin Pop
  @return: a list of all running instances on the current node
823 10c2650b Iustin Pop
    - instance1.example.com
824 10c2650b Iustin Pop
    - instance2.example.com
825 a8083063 Iustin Pop

826 098c0958 Michael Hanselmann
  """
827 e69d05fd Iustin Pop
  results = []
828 e69d05fd Iustin Pop
  for hname in hypervisor_list:
829 e69d05fd Iustin Pop
    try:
830 e69d05fd Iustin Pop
      names = hypervisor.GetHypervisor(hname).ListInstances()
831 e69d05fd Iustin Pop
      results.extend(names)
832 e69d05fd Iustin Pop
    except errors.HypervisorError, err:
833 aca13712 Iustin Pop
      _Fail("Error enumerating instances (hypervisor %s): %s",
834 aca13712 Iustin Pop
            hname, err, exc=True)
835 a8083063 Iustin Pop
836 e69d05fd Iustin Pop
  return results
837 a8083063 Iustin Pop
838 a8083063 Iustin Pop
839 e69d05fd Iustin Pop
def GetInstanceInfo(instance, hname):
840 5bbd3f7f Michael Hanselmann
  """Gives back the information about an instance as a dictionary.
841 a8083063 Iustin Pop

842 e69d05fd Iustin Pop
  @type instance: string
843 e69d05fd Iustin Pop
  @param instance: the instance name
844 e69d05fd Iustin Pop
  @type hname: string
845 e69d05fd Iustin Pop
  @param hname: the hypervisor type of the instance
846 a8083063 Iustin Pop

847 e69d05fd Iustin Pop
  @rtype: dict
848 e69d05fd Iustin Pop
  @return: dictionary with the following keys:
849 e69d05fd Iustin Pop
      - memory: memory size of instance (int)
850 e69d05fd Iustin Pop
      - state: xen state of instance (string)
851 e69d05fd Iustin Pop
      - time: cpu time of instance (float)
852 a8083063 Iustin Pop

853 098c0958 Michael Hanselmann
  """
854 a8083063 Iustin Pop
  output = {}
855 a8083063 Iustin Pop
856 e69d05fd Iustin Pop
  iinfo = hypervisor.GetHypervisor(hname).GetInstanceInfo(instance)
857 a8083063 Iustin Pop
  if iinfo is not None:
858 d0c8c01d Iustin Pop
    output["memory"] = iinfo[2]
859 d0c8c01d Iustin Pop
    output["state"] = iinfo[4]
860 d0c8c01d Iustin Pop
    output["time"] = iinfo[5]
861 a8083063 Iustin Pop
862 c26a6bd2 Iustin Pop
  return output
863 a8083063 Iustin Pop
864 a8083063 Iustin Pop
865 56e7640c Iustin Pop
def GetInstanceMigratable(instance):
866 56e7640c Iustin Pop
  """Gives whether an instance can be migrated.
867 56e7640c Iustin Pop

868 56e7640c Iustin Pop
  @type instance: L{objects.Instance}
869 56e7640c Iustin Pop
  @param instance: object representing the instance to be checked.
870 56e7640c Iustin Pop

871 56e7640c Iustin Pop
  @rtype: tuple
872 56e7640c Iustin Pop
  @return: tuple of (result, description) where:
873 56e7640c Iustin Pop
      - result: whether the instance can be migrated or not
874 56e7640c Iustin Pop
      - description: a description of the issue, if relevant
875 56e7640c Iustin Pop

876 56e7640c Iustin Pop
  """
877 56e7640c Iustin Pop
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
878 afdc3985 Iustin Pop
  iname = instance.name
879 afdc3985 Iustin Pop
  if iname not in hyper.ListInstances():
880 afdc3985 Iustin Pop
    _Fail("Instance %s is not running", iname)
881 56e7640c Iustin Pop
882 56e7640c Iustin Pop
  for idx in range(len(instance.disks)):
883 afdc3985 Iustin Pop
    link_name = _GetBlockDevSymlinkPath(iname, idx)
884 56e7640c Iustin Pop
    if not os.path.islink(link_name):
885 b8ebd37b Iustin Pop
      logging.warning("Instance %s is missing symlink %s for disk %d",
886 b8ebd37b Iustin Pop
                      iname, link_name, idx)
887 56e7640c Iustin Pop
888 56e7640c Iustin Pop
889 e69d05fd Iustin Pop
def GetAllInstancesInfo(hypervisor_list):
890 a8083063 Iustin Pop
  """Gather data about all instances.
891 a8083063 Iustin Pop

892 10c2650b Iustin Pop
  This is the equivalent of L{GetInstanceInfo}, except that it
893 a8083063 Iustin Pop
  computes data for all instances at once, thus being faster if one
894 a8083063 Iustin Pop
  needs data about more than one instance.
895 a8083063 Iustin Pop

896 e69d05fd Iustin Pop
  @type hypervisor_list: list
897 e69d05fd Iustin Pop
  @param hypervisor_list: list of hypervisors to query for instance data
898 e69d05fd Iustin Pop

899 955db481 Guido Trotter
  @rtype: dict
900 e69d05fd Iustin Pop
  @return: dictionary of instance: data, with data having the following keys:
901 e69d05fd Iustin Pop
      - memory: memory size of instance (int)
902 e69d05fd Iustin Pop
      - state: xen state of instance (string)
903 e69d05fd Iustin Pop
      - time: cpu time of instance (float)
904 10c2650b Iustin Pop
      - vcpus: the number of vcpus
905 a8083063 Iustin Pop

906 098c0958 Michael Hanselmann
  """
907 a8083063 Iustin Pop
  output = {}
908 a8083063 Iustin Pop
909 e69d05fd Iustin Pop
  for hname in hypervisor_list:
910 e69d05fd Iustin Pop
    iinfo = hypervisor.GetHypervisor(hname).GetAllInstancesInfo()
911 e69d05fd Iustin Pop
    if iinfo:
912 29921401 Iustin Pop
      for name, _, memory, vcpus, state, times in iinfo:
913 f23b5ae8 Iustin Pop
        value = {
914 d0c8c01d Iustin Pop
          "memory": memory,
915 d0c8c01d Iustin Pop
          "vcpus": vcpus,
916 d0c8c01d Iustin Pop
          "state": state,
917 d0c8c01d Iustin Pop
          "time": times,
918 e69d05fd Iustin Pop
          }
919 b33b6f55 Iustin Pop
        if name in output:
920 b33b6f55 Iustin Pop
          # we only check static parameters, like memory and vcpus,
921 b33b6f55 Iustin Pop
          # and not state and time which can change between the
922 b33b6f55 Iustin Pop
          # invocations of the different hypervisors
923 d0c8c01d Iustin Pop
          for key in "memory", "vcpus":
924 b33b6f55 Iustin Pop
            if value[key] != output[name][key]:
925 2fa74ef4 Iustin Pop
              _Fail("Instance %s is running twice"
926 2fa74ef4 Iustin Pop
                    " with different parameters", name)
927 f23b5ae8 Iustin Pop
        output[name] = value
928 a8083063 Iustin Pop
929 c26a6bd2 Iustin Pop
  return output
930 a8083063 Iustin Pop
931 a8083063 Iustin Pop
932 6aa7a354 Iustin Pop
def _InstanceLogName(kind, os_name, instance, component):
933 81a3406c Iustin Pop
  """Compute the OS log filename for a given instance and operation.
934 81a3406c Iustin Pop

935 81a3406c Iustin Pop
  The instance name and os name are passed in as strings since not all
936 81a3406c Iustin Pop
  operations have these as part of an instance object.
937 81a3406c Iustin Pop

938 81a3406c Iustin Pop
  @type kind: string
939 81a3406c Iustin Pop
  @param kind: the operation type (e.g. add, import, etc.)
940 81a3406c Iustin Pop
  @type os_name: string
941 81a3406c Iustin Pop
  @param os_name: the os name
942 81a3406c Iustin Pop
  @type instance: string
943 81a3406c Iustin Pop
  @param instance: the name of the instance being imported/added/etc.
944 6aa7a354 Iustin Pop
  @type component: string or None
945 6aa7a354 Iustin Pop
  @param component: the name of the component of the instance being
946 6aa7a354 Iustin Pop
      transferred
947 81a3406c Iustin Pop

948 81a3406c Iustin Pop
  """
949 1651d116 Michael Hanselmann
  # TODO: Use tempfile.mkstemp to create unique filename
950 6aa7a354 Iustin Pop
  if component:
951 6aa7a354 Iustin Pop
    assert "/" not in component
952 6aa7a354 Iustin Pop
    c_msg = "-%s" % component
953 6aa7a354 Iustin Pop
  else:
954 6aa7a354 Iustin Pop
    c_msg = ""
955 6aa7a354 Iustin Pop
  base = ("%s-%s-%s%s-%s.log" %
956 6aa7a354 Iustin Pop
          (kind, os_name, instance, c_msg, utils.TimestampForFilename()))
957 81a3406c Iustin Pop
  return utils.PathJoin(constants.LOG_OS_DIR, base)
958 81a3406c Iustin Pop
959 81a3406c Iustin Pop
960 4a0e011f Iustin Pop
def InstanceOsAdd(instance, reinstall, debug):
961 2f8598a5 Alexander Schreiber
  """Add an OS to an instance.
962 a8083063 Iustin Pop

963 d15a9ad3 Guido Trotter
  @type instance: L{objects.Instance}
964 d15a9ad3 Guido Trotter
  @param instance: Instance whose OS is to be installed
965 e557bae9 Guido Trotter
  @type reinstall: boolean
966 e557bae9 Guido Trotter
  @param reinstall: whether this is an instance reinstall
967 4a0e011f Iustin Pop
  @type debug: integer
968 4a0e011f Iustin Pop
  @param debug: debug level, passed to the OS scripts
969 c26a6bd2 Iustin Pop
  @rtype: None
970 a8083063 Iustin Pop

971 a8083063 Iustin Pop
  """
972 255dcebd Iustin Pop
  inst_os = OSFromDisk(instance.os)
973 255dcebd Iustin Pop
974 4a0e011f Iustin Pop
  create_env = OSEnvironment(instance, inst_os, debug)
975 e557bae9 Guido Trotter
  if reinstall:
976 d0c8c01d Iustin Pop
    create_env["INSTANCE_REINSTALL"] = "1"
977 a8083063 Iustin Pop
978 6aa7a354 Iustin Pop
  logfile = _InstanceLogName("add", instance.os, instance.name, None)
979 decd5f45 Iustin Pop
980 d868edb4 Iustin Pop
  result = utils.RunCmd([inst_os.create_script], env=create_env,
981 896a03f6 Iustin Pop
                        cwd=inst_os.path, output=logfile, reset_env=True)
982 decd5f45 Iustin Pop
  if result.failed:
983 18682bca Iustin Pop
    logging.error("os create command '%s' returned error: %s, logfile: %s,"
984 d868edb4 Iustin Pop
                  " output: %s", result.cmd, result.fail_reason, logfile,
985 18682bca Iustin Pop
                  result.output)
986 26f15862 Iustin Pop
    lines = [utils.SafeEncode(val)
987 20e01edd Iustin Pop
             for val in utils.TailFile(logfile, lines=20)]
988 afdc3985 Iustin Pop
    _Fail("OS create script failed (%s), last lines in the"
989 afdc3985 Iustin Pop
          " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
990 decd5f45 Iustin Pop
991 decd5f45 Iustin Pop
992 4a0e011f Iustin Pop
def RunRenameInstance(instance, old_name, debug):
993 decd5f45 Iustin Pop
  """Run the OS rename script for an instance.
994 decd5f45 Iustin Pop

995 b1206984 Iustin Pop
  @type instance: L{objects.Instance}
996 d15a9ad3 Guido Trotter
  @param instance: Instance whose OS is to be installed
997 d15a9ad3 Guido Trotter
  @type old_name: string
998 d15a9ad3 Guido Trotter
  @param old_name: previous instance name
999 4a0e011f Iustin Pop
  @type debug: integer
1000 4a0e011f Iustin Pop
  @param debug: debug level, passed to the OS scripts
1001 10c2650b Iustin Pop
  @rtype: boolean
1002 10c2650b Iustin Pop
  @return: the success of the operation
1003 decd5f45 Iustin Pop

1004 decd5f45 Iustin Pop
  """
1005 decd5f45 Iustin Pop
  inst_os = OSFromDisk(instance.os)
1006 decd5f45 Iustin Pop
1007 4a0e011f Iustin Pop
  rename_env = OSEnvironment(instance, inst_os, debug)
1008 d0c8c01d Iustin Pop
  rename_env["OLD_INSTANCE_NAME"] = old_name
1009 decd5f45 Iustin Pop
1010 81a3406c Iustin Pop
  logfile = _InstanceLogName("rename", instance.os,
1011 6aa7a354 Iustin Pop
                             "%s-%s" % (old_name, instance.name), None)
1012 a8083063 Iustin Pop
1013 d868edb4 Iustin Pop
  result = utils.RunCmd([inst_os.rename_script], env=rename_env,
1014 896a03f6 Iustin Pop
                        cwd=inst_os.path, output=logfile, reset_env=True)
1015 a8083063 Iustin Pop
1016 a8083063 Iustin Pop
  if result.failed:
1017 18682bca Iustin Pop
    logging.error("os create command '%s' returned error: %s output: %s",
1018 d868edb4 Iustin Pop
                  result.cmd, result.fail_reason, result.output)
1019 26f15862 Iustin Pop
    lines = [utils.SafeEncode(val)
1020 96841384 Iustin Pop
             for val in utils.TailFile(logfile, lines=20)]
1021 afdc3985 Iustin Pop
    _Fail("OS rename script failed (%s), last lines in the"
1022 afdc3985 Iustin Pop
          " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
1023 a8083063 Iustin Pop
1024 a8083063 Iustin Pop
1025 5282084b Iustin Pop
def _GetBlockDevSymlinkPath(instance_name, idx):
1026 3536c792 Iustin Pop
  return utils.PathJoin(constants.DISK_LINKS_DIR, "%s%s%d" %
1027 3536c792 Iustin Pop
                        (instance_name, constants.DISK_SEPARATOR, idx))
1028 5282084b Iustin Pop
1029 5282084b Iustin Pop
1030 5282084b Iustin Pop
def _SymlinkBlockDev(instance_name, device_path, idx):
1031 9332fd8a Iustin Pop
  """Set up symlinks to a instance's block device.
1032 9332fd8a Iustin Pop

1033 9332fd8a Iustin Pop
  This is an auxiliary function run when an instance is start (on the primary
1034 9332fd8a Iustin Pop
  node) or when an instance is migrated (on the target node).
1035 9332fd8a Iustin Pop

1036 9332fd8a Iustin Pop

1037 5282084b Iustin Pop
  @param instance_name: the name of the target instance
1038 5282084b Iustin Pop
  @param device_path: path of the physical block device, on the node
1039 5282084b Iustin Pop
  @param idx: the disk index
1040 5282084b Iustin Pop
  @return: absolute path to the disk's symlink
1041 9332fd8a Iustin Pop

1042 9332fd8a Iustin Pop
  """
1043 5282084b Iustin Pop
  link_name = _GetBlockDevSymlinkPath(instance_name, idx)
1044 9332fd8a Iustin Pop
  try:
1045 9332fd8a Iustin Pop
    os.symlink(device_path, link_name)
1046 5282084b Iustin Pop
  except OSError, err:
1047 5282084b Iustin Pop
    if err.errno == errno.EEXIST:
1048 9332fd8a Iustin Pop
      if (not os.path.islink(link_name) or
1049 9332fd8a Iustin Pop
          os.readlink(link_name) != device_path):
1050 9332fd8a Iustin Pop
        os.remove(link_name)
1051 9332fd8a Iustin Pop
        os.symlink(device_path, link_name)
1052 9332fd8a Iustin Pop
    else:
1053 9332fd8a Iustin Pop
      raise
1054 9332fd8a Iustin Pop
1055 9332fd8a Iustin Pop
  return link_name
1056 9332fd8a Iustin Pop
1057 9332fd8a Iustin Pop
1058 5282084b Iustin Pop
def _RemoveBlockDevLinks(instance_name, disks):
1059 3c9c571d Iustin Pop
  """Remove the block device symlinks belonging to the given instance.
1060 3c9c571d Iustin Pop

1061 3c9c571d Iustin Pop
  """
1062 29921401 Iustin Pop
  for idx, _ in enumerate(disks):
1063 5282084b Iustin Pop
    link_name = _GetBlockDevSymlinkPath(instance_name, idx)
1064 5282084b Iustin Pop
    if os.path.islink(link_name):
1065 3c9c571d Iustin Pop
      try:
1066 03dfa658 Iustin Pop
        os.remove(link_name)
1067 03dfa658 Iustin Pop
      except OSError:
1068 03dfa658 Iustin Pop
        logging.exception("Can't remove symlink '%s'", link_name)
1069 3c9c571d Iustin Pop
1070 3c9c571d Iustin Pop
1071 9332fd8a Iustin Pop
def _GatherAndLinkBlockDevs(instance):
1072 a8083063 Iustin Pop
  """Set up an instance's block device(s).
1073 a8083063 Iustin Pop

1074 a8083063 Iustin Pop
  This is run on the primary node at instance startup. The block
1075 a8083063 Iustin Pop
  devices must be already assembled.
1076 a8083063 Iustin Pop

1077 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1078 10c2650b Iustin Pop
  @param instance: the instance whose disks we shoul assemble
1079 069cfbf1 Iustin Pop
  @rtype: list
1080 069cfbf1 Iustin Pop
  @return: list of (disk_object, device_path)
1081 10c2650b Iustin Pop

1082 a8083063 Iustin Pop
  """
1083 a8083063 Iustin Pop
  block_devices = []
1084 9332fd8a Iustin Pop
  for idx, disk in enumerate(instance.disks):
1085 a8083063 Iustin Pop
    device = _RecursiveFindBD(disk)
1086 a8083063 Iustin Pop
    if device is None:
1087 a8083063 Iustin Pop
      raise errors.BlockDeviceError("Block device '%s' is not set up." %
1088 a8083063 Iustin Pop
                                    str(disk))
1089 a8083063 Iustin Pop
    device.Open()
1090 9332fd8a Iustin Pop
    try:
1091 5282084b Iustin Pop
      link_name = _SymlinkBlockDev(instance.name, device.dev_path, idx)
1092 9332fd8a Iustin Pop
    except OSError, e:
1093 9332fd8a Iustin Pop
      raise errors.BlockDeviceError("Cannot create block device symlink: %s" %
1094 9332fd8a Iustin Pop
                                    e.strerror)
1095 9332fd8a Iustin Pop
1096 9332fd8a Iustin Pop
    block_devices.append((disk, link_name))
1097 9332fd8a Iustin Pop
1098 a8083063 Iustin Pop
  return block_devices
1099 a8083063 Iustin Pop
1100 a8083063 Iustin Pop
1101 323f9095 Stephen Shirley
def StartInstance(instance, startup_paused):
1102 a8083063 Iustin Pop
  """Start an instance.
1103 a8083063 Iustin Pop

1104 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1105 e69d05fd Iustin Pop
  @param instance: the instance object
1106 323f9095 Stephen Shirley
  @type startup_paused: bool
1107 323f9095 Stephen Shirley
  @param instance: pause instance at startup?
1108 c26a6bd2 Iustin Pop
  @rtype: None
1109 a8083063 Iustin Pop

1110 098c0958 Michael Hanselmann
  """
1111 e69d05fd Iustin Pop
  running_instances = GetInstanceList([instance.hypervisor])
1112 a8083063 Iustin Pop
1113 a8083063 Iustin Pop
  if instance.name in running_instances:
1114 c26a6bd2 Iustin Pop
    logging.info("Instance %s already running, not starting", instance.name)
1115 c26a6bd2 Iustin Pop
    return
1116 a8083063 Iustin Pop
1117 a8083063 Iustin Pop
  try:
1118 ec596c24 Iustin Pop
    block_devices = _GatherAndLinkBlockDevs(instance)
1119 ec596c24 Iustin Pop
    hyper = hypervisor.GetHypervisor(instance.hypervisor)
1120 323f9095 Stephen Shirley
    hyper.StartInstance(instance, block_devices, startup_paused)
1121 ec596c24 Iustin Pop
  except errors.BlockDeviceError, err:
1122 2cc6781a Iustin Pop
    _Fail("Block device error: %s", err, exc=True)
1123 a8083063 Iustin Pop
  except errors.HypervisorError, err:
1124 5282084b Iustin Pop
    _RemoveBlockDevLinks(instance.name, instance.disks)
1125 2cc6781a Iustin Pop
    _Fail("Hypervisor error: %s", err, exc=True)
1126 a8083063 Iustin Pop
1127 a8083063 Iustin Pop
1128 6263189c Guido Trotter
def InstanceShutdown(instance, timeout):
1129 a8083063 Iustin Pop
  """Shut an instance down.
1130 a8083063 Iustin Pop

1131 10c2650b Iustin Pop
  @note: this functions uses polling with a hardcoded timeout.
1132 10c2650b Iustin Pop

1133 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1134 e69d05fd Iustin Pop
  @param instance: the instance object
1135 6263189c Guido Trotter
  @type timeout: integer
1136 6263189c Guido Trotter
  @param timeout: maximum timeout for soft shutdown
1137 c26a6bd2 Iustin Pop
  @rtype: None
1138 a8083063 Iustin Pop

1139 098c0958 Michael Hanselmann
  """
1140 e69d05fd Iustin Pop
  hv_name = instance.hypervisor
1141 e4e9b806 Guido Trotter
  hyper = hypervisor.GetHypervisor(hv_name)
1142 c26a6bd2 Iustin Pop
  iname = instance.name
1143 a8083063 Iustin Pop
1144 3c0cdc83 Michael Hanselmann
  if instance.name not in hyper.ListInstances():
1145 c26a6bd2 Iustin Pop
    logging.info("Instance %s not running, doing nothing", iname)
1146 c26a6bd2 Iustin Pop
    return
1147 a8083063 Iustin Pop
1148 3c0cdc83 Michael Hanselmann
  class _TryShutdown:
1149 3c0cdc83 Michael Hanselmann
    def __init__(self):
1150 3c0cdc83 Michael Hanselmann
      self.tried_once = False
1151 a8083063 Iustin Pop
1152 3c0cdc83 Michael Hanselmann
    def __call__(self):
1153 3c0cdc83 Michael Hanselmann
      if iname not in hyper.ListInstances():
1154 3c0cdc83 Michael Hanselmann
        return
1155 3c0cdc83 Michael Hanselmann
1156 3c0cdc83 Michael Hanselmann
      try:
1157 3c0cdc83 Michael Hanselmann
        hyper.StopInstance(instance, retry=self.tried_once)
1158 3c0cdc83 Michael Hanselmann
      except errors.HypervisorError, err:
1159 3c0cdc83 Michael Hanselmann
        if iname not in hyper.ListInstances():
1160 3c0cdc83 Michael Hanselmann
          # if the instance is no longer existing, consider this a
1161 3c0cdc83 Michael Hanselmann
          # success and go to cleanup
1162 3c0cdc83 Michael Hanselmann
          return
1163 3c0cdc83 Michael Hanselmann
1164 3c0cdc83 Michael Hanselmann
        _Fail("Failed to stop instance %s: %s", iname, err)
1165 3c0cdc83 Michael Hanselmann
1166 3c0cdc83 Michael Hanselmann
      self.tried_once = True
1167 3c0cdc83 Michael Hanselmann
1168 3c0cdc83 Michael Hanselmann
      raise utils.RetryAgain()
1169 3c0cdc83 Michael Hanselmann
1170 3c0cdc83 Michael Hanselmann
  try:
1171 3c0cdc83 Michael Hanselmann
    utils.Retry(_TryShutdown(), 5, timeout)
1172 3c0cdc83 Michael Hanselmann
  except utils.RetryTimeout:
1173 a8083063 Iustin Pop
    # the shutdown did not succeed
1174 e4e9b806 Guido Trotter
    logging.error("Shutdown of '%s' unsuccessful, forcing", iname)
1175 a8083063 Iustin Pop
1176 a8083063 Iustin Pop
    try:
1177 a8083063 Iustin Pop
      hyper.StopInstance(instance, force=True)
1178 a8083063 Iustin Pop
    except errors.HypervisorError, err:
1179 3c0cdc83 Michael Hanselmann
      if iname in hyper.ListInstances():
1180 3782acd7 Iustin Pop
        # only raise an error if the instance still exists, otherwise
1181 3782acd7 Iustin Pop
        # the error could simply be "instance ... unknown"!
1182 3782acd7 Iustin Pop
        _Fail("Failed to force stop instance %s: %s", iname, err)
1183 a8083063 Iustin Pop
1184 a8083063 Iustin Pop
    time.sleep(1)
1185 3c0cdc83 Michael Hanselmann
1186 3c0cdc83 Michael Hanselmann
    if iname in hyper.ListInstances():
1187 c26a6bd2 Iustin Pop
      _Fail("Could not shutdown instance %s even by destroy", iname)
1188 3c9c571d Iustin Pop
1189 f28ec899 Guido Trotter
  try:
1190 f28ec899 Guido Trotter
    hyper.CleanupInstance(instance.name)
1191 f28ec899 Guido Trotter
  except errors.HypervisorError, err:
1192 f28ec899 Guido Trotter
    logging.warning("Failed to execute post-shutdown cleanup step: %s", err)
1193 f28ec899 Guido Trotter
1194 c26a6bd2 Iustin Pop
  _RemoveBlockDevLinks(iname, instance.disks)
1195 a8083063 Iustin Pop
1196 a8083063 Iustin Pop
1197 17c3f802 Guido Trotter
def InstanceReboot(instance, reboot_type, shutdown_timeout):
1198 007a2f3e Alexander Schreiber
  """Reboot an instance.
1199 007a2f3e Alexander Schreiber

1200 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1201 10c2650b Iustin Pop
  @param instance: the instance object to reboot
1202 10c2650b Iustin Pop
  @type reboot_type: str
1203 10c2650b Iustin Pop
  @param reboot_type: the type of reboot, one the following
1204 10c2650b Iustin Pop
    constants:
1205 10c2650b Iustin Pop
      - L{constants.INSTANCE_REBOOT_SOFT}: only reboot the
1206 10c2650b Iustin Pop
        instance OS, do not recreate the VM
1207 10c2650b Iustin Pop
      - L{constants.INSTANCE_REBOOT_HARD}: tear down and
1208 10c2650b Iustin Pop
        restart the VM (at the hypervisor level)
1209 73e5a4f4 Iustin Pop
      - the other reboot type (L{constants.INSTANCE_REBOOT_FULL}) is
1210 73e5a4f4 Iustin Pop
        not accepted here, since that mode is handled differently, in
1211 73e5a4f4 Iustin Pop
        cmdlib, and translates into full stop and start of the
1212 73e5a4f4 Iustin Pop
        instance (instead of a call_instance_reboot RPC)
1213 23057d29 Michael Hanselmann
  @type shutdown_timeout: integer
1214 23057d29 Michael Hanselmann
  @param shutdown_timeout: maximum timeout for soft shutdown
1215 c26a6bd2 Iustin Pop
  @rtype: None
1216 007a2f3e Alexander Schreiber

1217 007a2f3e Alexander Schreiber
  """
1218 e69d05fd Iustin Pop
  running_instances = GetInstanceList([instance.hypervisor])
1219 007a2f3e Alexander Schreiber
1220 007a2f3e Alexander Schreiber
  if instance.name not in running_instances:
1221 2cc6781a Iustin Pop
    _Fail("Cannot reboot instance %s that is not running", instance.name)
1222 007a2f3e Alexander Schreiber
1223 e69d05fd Iustin Pop
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1224 007a2f3e Alexander Schreiber
  if reboot_type == constants.INSTANCE_REBOOT_SOFT:
1225 007a2f3e Alexander Schreiber
    try:
1226 007a2f3e Alexander Schreiber
      hyper.RebootInstance(instance)
1227 007a2f3e Alexander Schreiber
    except errors.HypervisorError, err:
1228 2cc6781a Iustin Pop
      _Fail("Failed to soft reboot instance %s: %s", instance.name, err)
1229 007a2f3e Alexander Schreiber
  elif reboot_type == constants.INSTANCE_REBOOT_HARD:
1230 007a2f3e Alexander Schreiber
    try:
1231 17c3f802 Guido Trotter
      InstanceShutdown(instance, shutdown_timeout)
1232 82bc21e2 Stephen Shirley
      return StartInstance(instance, False)
1233 007a2f3e Alexander Schreiber
    except errors.HypervisorError, err:
1234 2cc6781a Iustin Pop
      _Fail("Failed to hard reboot instance %s: %s", instance.name, err)
1235 007a2f3e Alexander Schreiber
  else:
1236 2cc6781a Iustin Pop
    _Fail("Invalid reboot_type received: %s", reboot_type)
1237 007a2f3e Alexander Schreiber
1238 007a2f3e Alexander Schreiber
1239 6906a9d8 Guido Trotter
def MigrationInfo(instance):
1240 6906a9d8 Guido Trotter
  """Gather information about an instance to be migrated.
1241 6906a9d8 Guido Trotter

1242 6906a9d8 Guido Trotter
  @type instance: L{objects.Instance}
1243 6906a9d8 Guido Trotter
  @param instance: the instance definition
1244 6906a9d8 Guido Trotter

1245 6906a9d8 Guido Trotter
  """
1246 cd42d0ad Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1247 cd42d0ad Guido Trotter
  try:
1248 cd42d0ad Guido Trotter
    info = hyper.MigrationInfo(instance)
1249 cd42d0ad Guido Trotter
  except errors.HypervisorError, err:
1250 2cc6781a Iustin Pop
    _Fail("Failed to fetch migration information: %s", err, exc=True)
1251 c26a6bd2 Iustin Pop
  return info
1252 6906a9d8 Guido Trotter
1253 6906a9d8 Guido Trotter
1254 6906a9d8 Guido Trotter
def AcceptInstance(instance, info, target):
1255 6906a9d8 Guido Trotter
  """Prepare the node to accept an instance.
1256 6906a9d8 Guido Trotter

1257 6906a9d8 Guido Trotter
  @type instance: L{objects.Instance}
1258 6906a9d8 Guido Trotter
  @param instance: the instance definition
1259 6906a9d8 Guido Trotter
  @type info: string/data (opaque)
1260 6906a9d8 Guido Trotter
  @param info: migration information, from the source node
1261 6906a9d8 Guido Trotter
  @type target: string
1262 6906a9d8 Guido Trotter
  @param target: target host (usually ip), on this node
1263 6906a9d8 Guido Trotter

1264 6906a9d8 Guido Trotter
  """
1265 77fcff4a Apollon Oikonomopoulos
  # TODO: why is this required only for DTS_EXT_MIRROR?
1266 77fcff4a Apollon Oikonomopoulos
  if instance.disk_template in constants.DTS_EXT_MIRROR:
1267 77fcff4a Apollon Oikonomopoulos
    # Create the symlinks, as the disks are not active
1268 77fcff4a Apollon Oikonomopoulos
    # in any way
1269 77fcff4a Apollon Oikonomopoulos
    try:
1270 77fcff4a Apollon Oikonomopoulos
      _GatherAndLinkBlockDevs(instance)
1271 77fcff4a Apollon Oikonomopoulos
    except errors.BlockDeviceError, err:
1272 77fcff4a Apollon Oikonomopoulos
      _Fail("Block device error: %s", err, exc=True)
1273 77fcff4a Apollon Oikonomopoulos
1274 cd42d0ad Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1275 cd42d0ad Guido Trotter
  try:
1276 cd42d0ad Guido Trotter
    hyper.AcceptInstance(instance, info, target)
1277 cd42d0ad Guido Trotter
  except errors.HypervisorError, err:
1278 77fcff4a Apollon Oikonomopoulos
    if instance.disk_template in constants.DTS_EXT_MIRROR:
1279 77fcff4a Apollon Oikonomopoulos
      _RemoveBlockDevLinks(instance.name, instance.disks)
1280 2cc6781a Iustin Pop
    _Fail("Failed to accept instance: %s", err, exc=True)
1281 6906a9d8 Guido Trotter
1282 6906a9d8 Guido Trotter
1283 6906a9d8 Guido Trotter
def FinalizeMigration(instance, info, success):
1284 6906a9d8 Guido Trotter
  """Finalize any preparation to accept an instance.
1285 6906a9d8 Guido Trotter

1286 6906a9d8 Guido Trotter
  @type instance: L{objects.Instance}
1287 6906a9d8 Guido Trotter
  @param instance: the instance definition
1288 6906a9d8 Guido Trotter
  @type info: string/data (opaque)
1289 6906a9d8 Guido Trotter
  @param info: migration information, from the source node
1290 6906a9d8 Guido Trotter
  @type success: boolean
1291 6906a9d8 Guido Trotter
  @param success: whether the migration was a success or a failure
1292 6906a9d8 Guido Trotter

1293 6906a9d8 Guido Trotter
  """
1294 cd42d0ad Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1295 cd42d0ad Guido Trotter
  try:
1296 cd42d0ad Guido Trotter
    hyper.FinalizeMigration(instance, info, success)
1297 cd42d0ad Guido Trotter
  except errors.HypervisorError, err:
1298 2cc6781a Iustin Pop
    _Fail("Failed to finalize migration: %s", err, exc=True)
1299 6906a9d8 Guido Trotter
1300 6906a9d8 Guido Trotter
1301 2a10865c Iustin Pop
def MigrateInstance(instance, target, live):
1302 2a10865c Iustin Pop
  """Migrates an instance to another node.
1303 2a10865c Iustin Pop

1304 b1206984 Iustin Pop
  @type instance: L{objects.Instance}
1305 9f0e6b37 Iustin Pop
  @param instance: the instance definition
1306 9f0e6b37 Iustin Pop
  @type target: string
1307 9f0e6b37 Iustin Pop
  @param target: the target node name
1308 9f0e6b37 Iustin Pop
  @type live: boolean
1309 9f0e6b37 Iustin Pop
  @param live: whether the migration should be done live or not (the
1310 9f0e6b37 Iustin Pop
      interpretation of this parameter is left to the hypervisor)
1311 c03fe62b Andrea Spadaccini
  @raise RPCFail: if migration fails for some reason
1312 9f0e6b37 Iustin Pop

1313 2a10865c Iustin Pop
  """
1314 53c776b5 Iustin Pop
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1315 2a10865c Iustin Pop
1316 2a10865c Iustin Pop
  try:
1317 58d38b02 Iustin Pop
    hyper.MigrateInstance(instance, target, live)
1318 2a10865c Iustin Pop
  except errors.HypervisorError, err:
1319 2cc6781a Iustin Pop
    _Fail("Failed to migrate instance: %s", err, exc=True)
1320 2a10865c Iustin Pop
1321 2a10865c Iustin Pop
1322 821d1bd1 Iustin Pop
def BlockdevCreate(disk, size, owner, on_primary, info):
1323 a8083063 Iustin Pop
  """Creates a block device for an instance.
1324 a8083063 Iustin Pop

1325 b1206984 Iustin Pop
  @type disk: L{objects.Disk}
1326 b1206984 Iustin Pop
  @param disk: the object describing the disk we should create
1327 b1206984 Iustin Pop
  @type size: int
1328 b1206984 Iustin Pop
  @param size: the size of the physical underlying device, in MiB
1329 b1206984 Iustin Pop
  @type owner: str
1330 b1206984 Iustin Pop
  @param owner: the name of the instance for which disk is created,
1331 b1206984 Iustin Pop
      used for device cache data
1332 b1206984 Iustin Pop
  @type on_primary: boolean
1333 b1206984 Iustin Pop
  @param on_primary:  indicates if it is the primary node or not
1334 b1206984 Iustin Pop
  @type info: string
1335 b1206984 Iustin Pop
  @param info: string that will be sent to the physical device
1336 b1206984 Iustin Pop
      creation, used for example to set (LVM) tags on LVs
1337 b1206984 Iustin Pop

1338 b1206984 Iustin Pop
  @return: the new unique_id of the device (this can sometime be
1339 b1206984 Iustin Pop
      computed only after creation), or None. On secondary nodes,
1340 b1206984 Iustin Pop
      it's not required to return anything.
1341 a8083063 Iustin Pop

1342 a8083063 Iustin Pop
  """
1343 d0c8c01d Iustin Pop
  # TODO: remove the obsolete "size" argument
1344 b459a848 Andrea Spadaccini
  # pylint: disable=W0613
1345 a8083063 Iustin Pop
  clist = []
1346 a8083063 Iustin Pop
  if disk.children:
1347 a8083063 Iustin Pop
    for child in disk.children:
1348 1063abd1 Iustin Pop
      try:
1349 1063abd1 Iustin Pop
        crdev = _RecursiveAssembleBD(child, owner, on_primary)
1350 1063abd1 Iustin Pop
      except errors.BlockDeviceError, err:
1351 2cc6781a Iustin Pop
        _Fail("Can't assemble device %s: %s", child, err)
1352 a8083063 Iustin Pop
      if on_primary or disk.AssembleOnSecondary():
1353 a8083063 Iustin Pop
        # we need the children open in case the device itself has to
1354 a8083063 Iustin Pop
        # be assembled
1355 1063abd1 Iustin Pop
        try:
1356 b459a848 Andrea Spadaccini
          # pylint: disable=E1103
1357 1063abd1 Iustin Pop
          crdev.Open()
1358 1063abd1 Iustin Pop
        except errors.BlockDeviceError, err:
1359 2cc6781a Iustin Pop
          _Fail("Can't make child '%s' read-write: %s", child, err)
1360 a8083063 Iustin Pop
      clist.append(crdev)
1361 a8083063 Iustin Pop
1362 dab69e97 Iustin Pop
  try:
1363 464f8daf Iustin Pop
    device = bdev.Create(disk.dev_type, disk.physical_id, clist, disk.size)
1364 1063abd1 Iustin Pop
  except errors.BlockDeviceError, err:
1365 2cc6781a Iustin Pop
    _Fail("Can't create block device: %s", err)
1366 6c626518 Iustin Pop
1367 a8083063 Iustin Pop
  if on_primary or disk.AssembleOnSecondary():
1368 1063abd1 Iustin Pop
    try:
1369 1063abd1 Iustin Pop
      device.Assemble()
1370 1063abd1 Iustin Pop
    except errors.BlockDeviceError, err:
1371 2cc6781a Iustin Pop
      _Fail("Can't assemble device after creation, unusual event: %s", err)
1372 e31c43f7 Michael Hanselmann
    device.SetSyncSpeed(constants.SYNC_SPEED)
1373 a8083063 Iustin Pop
    if on_primary or disk.OpenOnSecondary():
1374 1063abd1 Iustin Pop
      try:
1375 1063abd1 Iustin Pop
        device.Open(force=True)
1376 1063abd1 Iustin Pop
      except errors.BlockDeviceError, err:
1377 2cc6781a Iustin Pop
        _Fail("Can't make device r/w after creation, unusual event: %s", err)
1378 3f78eef2 Iustin Pop
    DevCacheManager.UpdateCache(device.dev_path, owner,
1379 3f78eef2 Iustin Pop
                                on_primary, disk.iv_name)
1380 a0c3fea1 Michael Hanselmann
1381 a0c3fea1 Michael Hanselmann
  device.SetInfo(info)
1382 a0c3fea1 Michael Hanselmann
1383 c26a6bd2 Iustin Pop
  return device.unique_id
1384 a8083063 Iustin Pop
1385 a8083063 Iustin Pop
1386 da63bb4e René Nussbaumer
def _WipeDevice(path, offset, size):
1387 69dd363f René Nussbaumer
  """This function actually wipes the device.
1388 69dd363f René Nussbaumer

1389 69dd363f René Nussbaumer
  @param path: The path to the device to wipe
1390 da63bb4e René Nussbaumer
  @param offset: The offset in MiB in the file
1391 da63bb4e René Nussbaumer
  @param size: The size in MiB to write
1392 69dd363f René Nussbaumer

1393 69dd363f René Nussbaumer
  """
1394 da63bb4e René Nussbaumer
  cmd = [constants.DD_CMD, "if=/dev/zero", "seek=%d" % offset,
1395 da63bb4e René Nussbaumer
         "bs=%d" % constants.WIPE_BLOCK_SIZE, "oflag=direct", "of=%s" % path,
1396 da63bb4e René Nussbaumer
         "count=%d" % size]
1397 da63bb4e René Nussbaumer
  result = utils.RunCmd(cmd)
1398 69dd363f René Nussbaumer
1399 69dd363f René Nussbaumer
  if result.failed:
1400 69dd363f René Nussbaumer
    _Fail("Wipe command '%s' exited with error: %s; output: %s", result.cmd,
1401 69dd363f René Nussbaumer
          result.fail_reason, result.output)
1402 69dd363f René Nussbaumer
1403 69dd363f René Nussbaumer
1404 da63bb4e René Nussbaumer
def BlockdevWipe(disk, offset, size):
1405 69dd363f René Nussbaumer
  """Wipes a block device.
1406 69dd363f René Nussbaumer

1407 69dd363f René Nussbaumer
  @type disk: L{objects.Disk}
1408 69dd363f René Nussbaumer
  @param disk: the disk object we want to wipe
1409 da63bb4e René Nussbaumer
  @type offset: int
1410 da63bb4e René Nussbaumer
  @param offset: The offset in MiB in the file
1411 da63bb4e René Nussbaumer
  @type size: int
1412 da63bb4e René Nussbaumer
  @param size: The size in MiB to write
1413 69dd363f René Nussbaumer

1414 69dd363f René Nussbaumer
  """
1415 69dd363f René Nussbaumer
  try:
1416 69dd363f René Nussbaumer
    rdev = _RecursiveFindBD(disk)
1417 da63bb4e René Nussbaumer
  except errors.BlockDeviceError:
1418 da63bb4e René Nussbaumer
    rdev = None
1419 da63bb4e René Nussbaumer
1420 da63bb4e René Nussbaumer
  if not rdev:
1421 da63bb4e René Nussbaumer
    _Fail("Cannot execute wipe for device %s: device not found", disk.iv_name)
1422 da63bb4e René Nussbaumer
1423 da63bb4e René Nussbaumer
  # Do cross verify some of the parameters
1424 da63bb4e René Nussbaumer
  if offset > rdev.size:
1425 da63bb4e René Nussbaumer
    _Fail("Offset is bigger than device size")
1426 da63bb4e René Nussbaumer
  if (offset + size) > rdev.size:
1427 da63bb4e René Nussbaumer
    _Fail("The provided offset and size to wipe is bigger than device size")
1428 69dd363f René Nussbaumer
1429 da63bb4e René Nussbaumer
  _WipeDevice(rdev.dev_path, offset, size)
1430 69dd363f René Nussbaumer
1431 69dd363f René Nussbaumer
1432 5119c79e René Nussbaumer
def BlockdevPauseResumeSync(disks, pause):
1433 5119c79e René Nussbaumer
  """Pause or resume the sync of the block device.
1434 5119c79e René Nussbaumer

1435 0f39886a René Nussbaumer
  @type disks: list of L{objects.Disk}
1436 0f39886a René Nussbaumer
  @param disks: the disks object we want to pause/resume
1437 5119c79e René Nussbaumer
  @type pause: bool
1438 5119c79e René Nussbaumer
  @param pause: Wheater to pause or resume
1439 5119c79e René Nussbaumer

1440 5119c79e René Nussbaumer
  """
1441 5119c79e René Nussbaumer
  success = []
1442 5119c79e René Nussbaumer
  for disk in disks:
1443 5119c79e René Nussbaumer
    try:
1444 5119c79e René Nussbaumer
      rdev = _RecursiveFindBD(disk)
1445 5119c79e René Nussbaumer
    except errors.BlockDeviceError:
1446 5119c79e René Nussbaumer
      rdev = None
1447 5119c79e René Nussbaumer
1448 5119c79e René Nussbaumer
    if not rdev:
1449 5119c79e René Nussbaumer
      success.append((False, ("Cannot change sync for device %s:"
1450 5119c79e René Nussbaumer
                              " device not found" % disk.iv_name)))
1451 5119c79e René Nussbaumer
      continue
1452 5119c79e René Nussbaumer
1453 5119c79e René Nussbaumer
    result = rdev.PauseResumeSync(pause)
1454 5119c79e René Nussbaumer
1455 5119c79e René Nussbaumer
    if result:
1456 5119c79e René Nussbaumer
      success.append((result, None))
1457 5119c79e René Nussbaumer
    else:
1458 5119c79e René Nussbaumer
      if pause:
1459 5119c79e René Nussbaumer
        msg = "Pause"
1460 5119c79e René Nussbaumer
      else:
1461 5119c79e René Nussbaumer
        msg = "Resume"
1462 5119c79e René Nussbaumer
      success.append((result, "%s for device %s failed" % (msg, disk.iv_name)))
1463 5119c79e René Nussbaumer
1464 5119c79e René Nussbaumer
  return success
1465 5119c79e René Nussbaumer
1466 5119c79e René Nussbaumer
1467 821d1bd1 Iustin Pop
def BlockdevRemove(disk):
1468 a8083063 Iustin Pop
  """Remove a block device.
1469 a8083063 Iustin Pop

1470 10c2650b Iustin Pop
  @note: This is intended to be called recursively.
1471 10c2650b Iustin Pop

1472 c41eea6e Iustin Pop
  @type disk: L{objects.Disk}
1473 10c2650b Iustin Pop
  @param disk: the disk object we should remove
1474 10c2650b Iustin Pop
  @rtype: boolean
1475 10c2650b Iustin Pop
  @return: the success of the operation
1476 a8083063 Iustin Pop

1477 a8083063 Iustin Pop
  """
1478 e1bc0878 Iustin Pop
  msgs = []
1479 a8083063 Iustin Pop
  try:
1480 bca2e7f4 Iustin Pop
    rdev = _RecursiveFindBD(disk)
1481 a8083063 Iustin Pop
  except errors.BlockDeviceError, err:
1482 a8083063 Iustin Pop
    # probably can't attach
1483 18682bca Iustin Pop
    logging.info("Can't attach to device %s in remove", disk)
1484 a8083063 Iustin Pop
    rdev = None
1485 a8083063 Iustin Pop
  if rdev is not None:
1486 3f78eef2 Iustin Pop
    r_path = rdev.dev_path
1487 e1bc0878 Iustin Pop
    try:
1488 0c6c04ec Iustin Pop
      rdev.Remove()
1489 e1bc0878 Iustin Pop
    except errors.BlockDeviceError, err:
1490 e1bc0878 Iustin Pop
      msgs.append(str(err))
1491 c26a6bd2 Iustin Pop
    if not msgs:
1492 3f78eef2 Iustin Pop
      DevCacheManager.RemoveCache(r_path)
1493 e1bc0878 Iustin Pop
1494 a8083063 Iustin Pop
  if disk.children:
1495 a8083063 Iustin Pop
    for child in disk.children:
1496 c26a6bd2 Iustin Pop
      try:
1497 c26a6bd2 Iustin Pop
        BlockdevRemove(child)
1498 c26a6bd2 Iustin Pop
      except RPCFail, err:
1499 c26a6bd2 Iustin Pop
        msgs.append(str(err))
1500 e1bc0878 Iustin Pop
1501 c26a6bd2 Iustin Pop
  if msgs:
1502 afdc3985 Iustin Pop
    _Fail("; ".join(msgs))
1503 afdc3985 Iustin Pop
1504 a8083063 Iustin Pop
1505 3f78eef2 Iustin Pop
def _RecursiveAssembleBD(disk, owner, as_primary):
1506 a8083063 Iustin Pop
  """Activate a block device for an instance.
1507 a8083063 Iustin Pop

1508 a8083063 Iustin Pop
  This is run on the primary and secondary nodes for an instance.
1509 a8083063 Iustin Pop

1510 10c2650b Iustin Pop
  @note: this function is called recursively.
1511 a8083063 Iustin Pop

1512 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
1513 10c2650b Iustin Pop
  @param disk: the disk we try to assemble
1514 10c2650b Iustin Pop
  @type owner: str
1515 10c2650b Iustin Pop
  @param owner: the name of the instance which owns the disk
1516 10c2650b Iustin Pop
  @type as_primary: boolean
1517 10c2650b Iustin Pop
  @param as_primary: if we should make the block device
1518 10c2650b Iustin Pop
      read/write
1519 a8083063 Iustin Pop

1520 10c2650b Iustin Pop
  @return: the assembled device or None (in case no device
1521 10c2650b Iustin Pop
      was assembled)
1522 10c2650b Iustin Pop
  @raise errors.BlockDeviceError: in case there is an error
1523 10c2650b Iustin Pop
      during the activation of the children or the device
1524 10c2650b Iustin Pop
      itself
1525 a8083063 Iustin Pop

1526 a8083063 Iustin Pop
  """
1527 a8083063 Iustin Pop
  children = []
1528 a8083063 Iustin Pop
  if disk.children:
1529 fc1dc9d7 Iustin Pop
    mcn = disk.ChildrenNeeded()
1530 fc1dc9d7 Iustin Pop
    if mcn == -1:
1531 fc1dc9d7 Iustin Pop
      mcn = 0 # max number of Nones allowed
1532 fc1dc9d7 Iustin Pop
    else:
1533 fc1dc9d7 Iustin Pop
      mcn = len(disk.children) - mcn # max number of Nones
1534 a8083063 Iustin Pop
    for chld_disk in disk.children:
1535 fc1dc9d7 Iustin Pop
      try:
1536 fc1dc9d7 Iustin Pop
        cdev = _RecursiveAssembleBD(chld_disk, owner, as_primary)
1537 fc1dc9d7 Iustin Pop
      except errors.BlockDeviceError, err:
1538 7803d4d3 Iustin Pop
        if children.count(None) >= mcn:
1539 fc1dc9d7 Iustin Pop
          raise
1540 fc1dc9d7 Iustin Pop
        cdev = None
1541 1063abd1 Iustin Pop
        logging.error("Error in child activation (but continuing): %s",
1542 1063abd1 Iustin Pop
                      str(err))
1543 fc1dc9d7 Iustin Pop
      children.append(cdev)
1544 a8083063 Iustin Pop
1545 a8083063 Iustin Pop
  if as_primary or disk.AssembleOnSecondary():
1546 464f8daf Iustin Pop
    r_dev = bdev.Assemble(disk.dev_type, disk.physical_id, children, disk.size)
1547 e31c43f7 Michael Hanselmann
    r_dev.SetSyncSpeed(constants.SYNC_SPEED)
1548 a8083063 Iustin Pop
    result = r_dev
1549 a8083063 Iustin Pop
    if as_primary or disk.OpenOnSecondary():
1550 a8083063 Iustin Pop
      r_dev.Open()
1551 3f78eef2 Iustin Pop
    DevCacheManager.UpdateCache(r_dev.dev_path, owner,
1552 3f78eef2 Iustin Pop
                                as_primary, disk.iv_name)
1553 3f78eef2 Iustin Pop
1554 a8083063 Iustin Pop
  else:
1555 a8083063 Iustin Pop
    result = True
1556 a8083063 Iustin Pop
  return result
1557 a8083063 Iustin Pop
1558 a8083063 Iustin Pop
1559 c417e115 Iustin Pop
def BlockdevAssemble(disk, owner, as_primary, idx):
1560 a8083063 Iustin Pop
  """Activate a block device for an instance.
1561 a8083063 Iustin Pop

1562 a8083063 Iustin Pop
  This is a wrapper over _RecursiveAssembleBD.
1563 a8083063 Iustin Pop

1564 b1206984 Iustin Pop
  @rtype: str or boolean
1565 b1206984 Iustin Pop
  @return: a C{/dev/...} path for primary nodes, and
1566 b1206984 Iustin Pop
      C{True} for secondary nodes
1567 a8083063 Iustin Pop

1568 a8083063 Iustin Pop
  """
1569 53c14ef1 Iustin Pop
  try:
1570 53c14ef1 Iustin Pop
    result = _RecursiveAssembleBD(disk, owner, as_primary)
1571 53c14ef1 Iustin Pop
    if isinstance(result, bdev.BlockDev):
1572 b459a848 Andrea Spadaccini
      # pylint: disable=E1103
1573 53c14ef1 Iustin Pop
      result = result.dev_path
1574 c417e115 Iustin Pop
      if as_primary:
1575 c417e115 Iustin Pop
        _SymlinkBlockDev(owner, result, idx)
1576 53c14ef1 Iustin Pop
  except errors.BlockDeviceError, err:
1577 afdc3985 Iustin Pop
    _Fail("Error while assembling disk: %s", err, exc=True)
1578 c417e115 Iustin Pop
  except OSError, err:
1579 c417e115 Iustin Pop
    _Fail("Error while symlinking disk: %s", err, exc=True)
1580 afdc3985 Iustin Pop
1581 c26a6bd2 Iustin Pop
  return result
1582 a8083063 Iustin Pop
1583 a8083063 Iustin Pop
1584 821d1bd1 Iustin Pop
def BlockdevShutdown(disk):
1585 a8083063 Iustin Pop
  """Shut down a block device.
1586 a8083063 Iustin Pop

1587 5bbd3f7f Michael Hanselmann
  First, if the device is assembled (Attach() is successful), then
1588 c41eea6e Iustin Pop
  the device is shutdown. Then the children of the device are
1589 c41eea6e Iustin Pop
  shutdown.
1590 a8083063 Iustin Pop

1591 a8083063 Iustin Pop
  This function is called recursively. Note that we don't cache the
1592 a8083063 Iustin Pop
  children or such, as oppossed to assemble, shutdown of different
1593 a8083063 Iustin Pop
  devices doesn't require that the upper device was active.
1594 a8083063 Iustin Pop

1595 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
1596 10c2650b Iustin Pop
  @param disk: the description of the disk we should
1597 10c2650b Iustin Pop
      shutdown
1598 c26a6bd2 Iustin Pop
  @rtype: None
1599 10c2650b Iustin Pop

1600 a8083063 Iustin Pop
  """
1601 cacfd1fd Iustin Pop
  msgs = []
1602 a8083063 Iustin Pop
  r_dev = _RecursiveFindBD(disk)
1603 a8083063 Iustin Pop
  if r_dev is not None:
1604 3f78eef2 Iustin Pop
    r_path = r_dev.dev_path
1605 cacfd1fd Iustin Pop
    try:
1606 746f7476 Iustin Pop
      r_dev.Shutdown()
1607 746f7476 Iustin Pop
      DevCacheManager.RemoveCache(r_path)
1608 cacfd1fd Iustin Pop
    except errors.BlockDeviceError, err:
1609 cacfd1fd Iustin Pop
      msgs.append(str(err))
1610 746f7476 Iustin Pop
1611 a8083063 Iustin Pop
  if disk.children:
1612 a8083063 Iustin Pop
    for child in disk.children:
1613 c26a6bd2 Iustin Pop
      try:
1614 c26a6bd2 Iustin Pop
        BlockdevShutdown(child)
1615 c26a6bd2 Iustin Pop
      except RPCFail, err:
1616 c26a6bd2 Iustin Pop
        msgs.append(str(err))
1617 746f7476 Iustin Pop
1618 c26a6bd2 Iustin Pop
  if msgs:
1619 afdc3985 Iustin Pop
    _Fail("; ".join(msgs))
1620 a8083063 Iustin Pop
1621 a8083063 Iustin Pop
1622 821d1bd1 Iustin Pop
def BlockdevAddchildren(parent_cdev, new_cdevs):
1623 153d9724 Iustin Pop
  """Extend a mirrored block device.
1624 a8083063 Iustin Pop

1625 10c2650b Iustin Pop
  @type parent_cdev: L{objects.Disk}
1626 10c2650b Iustin Pop
  @param parent_cdev: the disk to which we should add children
1627 10c2650b Iustin Pop
  @type new_cdevs: list of L{objects.Disk}
1628 10c2650b Iustin Pop
  @param new_cdevs: the list of children which we should add
1629 c26a6bd2 Iustin Pop
  @rtype: None
1630 10c2650b Iustin Pop

1631 a8083063 Iustin Pop
  """
1632 bca2e7f4 Iustin Pop
  parent_bdev = _RecursiveFindBD(parent_cdev)
1633 153d9724 Iustin Pop
  if parent_bdev is None:
1634 2cc6781a Iustin Pop
    _Fail("Can't find parent device '%s' in add children", parent_cdev)
1635 153d9724 Iustin Pop
  new_bdevs = [_RecursiveFindBD(disk) for disk in new_cdevs]
1636 153d9724 Iustin Pop
  if new_bdevs.count(None) > 0:
1637 2cc6781a Iustin Pop
    _Fail("Can't find new device(s) to add: %s:%s", new_bdevs, new_cdevs)
1638 153d9724 Iustin Pop
  parent_bdev.AddChildren(new_bdevs)
1639 a8083063 Iustin Pop
1640 a8083063 Iustin Pop
1641 821d1bd1 Iustin Pop
def BlockdevRemovechildren(parent_cdev, new_cdevs):
1642 153d9724 Iustin Pop
  """Shrink a mirrored block device.
1643 a8083063 Iustin Pop

1644 10c2650b Iustin Pop
  @type parent_cdev: L{objects.Disk}
1645 10c2650b Iustin Pop
  @param parent_cdev: the disk from which we should remove children
1646 10c2650b Iustin Pop
  @type new_cdevs: list of L{objects.Disk}
1647 10c2650b Iustin Pop
  @param new_cdevs: the list of children which we should remove
1648 c26a6bd2 Iustin Pop
  @rtype: None
1649 10c2650b Iustin Pop

1650 a8083063 Iustin Pop
  """
1651 153d9724 Iustin Pop
  parent_bdev = _RecursiveFindBD(parent_cdev)
1652 153d9724 Iustin Pop
  if parent_bdev is None:
1653 2cc6781a Iustin Pop
    _Fail("Can't find parent device '%s' in remove children", parent_cdev)
1654 e739bd57 Iustin Pop
  devs = []
1655 e739bd57 Iustin Pop
  for disk in new_cdevs:
1656 e739bd57 Iustin Pop
    rpath = disk.StaticDevPath()
1657 e739bd57 Iustin Pop
    if rpath is None:
1658 e739bd57 Iustin Pop
      bd = _RecursiveFindBD(disk)
1659 e739bd57 Iustin Pop
      if bd is None:
1660 2cc6781a Iustin Pop
        _Fail("Can't find device %s while removing children", disk)
1661 e739bd57 Iustin Pop
      else:
1662 e739bd57 Iustin Pop
        devs.append(bd.dev_path)
1663 e739bd57 Iustin Pop
    else:
1664 e51db2a6 Iustin Pop
      if not utils.IsNormAbsPath(rpath):
1665 e51db2a6 Iustin Pop
        _Fail("Strange path returned from StaticDevPath: '%s'", rpath)
1666 e739bd57 Iustin Pop
      devs.append(rpath)
1667 e739bd57 Iustin Pop
  parent_bdev.RemoveChildren(devs)
1668 a8083063 Iustin Pop
1669 a8083063 Iustin Pop
1670 821d1bd1 Iustin Pop
def BlockdevGetmirrorstatus(disks):
1671 a8083063 Iustin Pop
  """Get the mirroring status of a list of devices.
1672 a8083063 Iustin Pop

1673 10c2650b Iustin Pop
  @type disks: list of L{objects.Disk}
1674 10c2650b Iustin Pop
  @param disks: the list of disks which we should query
1675 10c2650b Iustin Pop
  @rtype: disk
1676 c6a9dffa Michael Hanselmann
  @return: List of L{objects.BlockDevStatus}, one for each disk
1677 10c2650b Iustin Pop
  @raise errors.BlockDeviceError: if any of the disks cannot be
1678 10c2650b Iustin Pop
      found
1679 a8083063 Iustin Pop

1680 a8083063 Iustin Pop
  """
1681 a8083063 Iustin Pop
  stats = []
1682 a8083063 Iustin Pop
  for dsk in disks:
1683 a8083063 Iustin Pop
    rbd = _RecursiveFindBD(dsk)
1684 a8083063 Iustin Pop
    if rbd is None:
1685 3efa9051 Iustin Pop
      _Fail("Can't find device %s", dsk)
1686 96acbc09 Michael Hanselmann
1687 36145b12 Michael Hanselmann
    stats.append(rbd.CombinedSyncStatus())
1688 96acbc09 Michael Hanselmann
1689 c26a6bd2 Iustin Pop
  return stats
1690 a8083063 Iustin Pop
1691 a8083063 Iustin Pop
1692 c6a9dffa Michael Hanselmann
def BlockdevGetmirrorstatusMulti(disks):
1693 c6a9dffa Michael Hanselmann
  """Get the mirroring status of a list of devices.
1694 c6a9dffa Michael Hanselmann

1695 c6a9dffa Michael Hanselmann
  @type disks: list of L{objects.Disk}
1696 c6a9dffa Michael Hanselmann
  @param disks: the list of disks which we should query
1697 c6a9dffa Michael Hanselmann
  @rtype: disk
1698 c6a9dffa Michael Hanselmann
  @return: List of tuples, (bool, status), one for each disk; bool denotes
1699 c6a9dffa Michael Hanselmann
    success/failure, status is L{objects.BlockDevStatus} on success, string
1700 c6a9dffa Michael Hanselmann
    otherwise
1701 c6a9dffa Michael Hanselmann

1702 c6a9dffa Michael Hanselmann
  """
1703 c6a9dffa Michael Hanselmann
  result = []
1704 c6a9dffa Michael Hanselmann
  for disk in disks:
1705 c6a9dffa Michael Hanselmann
    try:
1706 c6a9dffa Michael Hanselmann
      rbd = _RecursiveFindBD(disk)
1707 c6a9dffa Michael Hanselmann
      if rbd is None:
1708 c6a9dffa Michael Hanselmann
        result.append((False, "Can't find device %s" % disk))
1709 c6a9dffa Michael Hanselmann
        continue
1710 c6a9dffa Michael Hanselmann
1711 c6a9dffa Michael Hanselmann
      status = rbd.CombinedSyncStatus()
1712 c6a9dffa Michael Hanselmann
    except errors.BlockDeviceError, err:
1713 c6a9dffa Michael Hanselmann
      logging.exception("Error while getting disk status")
1714 c6a9dffa Michael Hanselmann
      result.append((False, str(err)))
1715 c6a9dffa Michael Hanselmann
    else:
1716 c6a9dffa Michael Hanselmann
      result.append((True, status))
1717 c6a9dffa Michael Hanselmann
1718 c6a9dffa Michael Hanselmann
  assert len(disks) == len(result)
1719 c6a9dffa Michael Hanselmann
1720 c6a9dffa Michael Hanselmann
  return result
1721 c6a9dffa Michael Hanselmann
1722 c6a9dffa Michael Hanselmann
1723 bca2e7f4 Iustin Pop
def _RecursiveFindBD(disk):
1724 a8083063 Iustin Pop
  """Check if a device is activated.
1725 a8083063 Iustin Pop

1726 5bbd3f7f Michael Hanselmann
  If so, return information about the real device.
1727 a8083063 Iustin Pop

1728 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
1729 10c2650b Iustin Pop
  @param disk: the disk object we need to find
1730 a8083063 Iustin Pop

1731 10c2650b Iustin Pop
  @return: None if the device can't be found,
1732 10c2650b Iustin Pop
      otherwise the device instance
1733 a8083063 Iustin Pop

1734 a8083063 Iustin Pop
  """
1735 a8083063 Iustin Pop
  children = []
1736 a8083063 Iustin Pop
  if disk.children:
1737 a8083063 Iustin Pop
    for chdisk in disk.children:
1738 a8083063 Iustin Pop
      children.append(_RecursiveFindBD(chdisk))
1739 a8083063 Iustin Pop
1740 464f8daf Iustin Pop
  return bdev.FindDevice(disk.dev_type, disk.physical_id, children, disk.size)
1741 a8083063 Iustin Pop
1742 a8083063 Iustin Pop
1743 f2e07bb4 Michael Hanselmann
def _OpenRealBD(disk):
1744 f2e07bb4 Michael Hanselmann
  """Opens the underlying block device of a disk.
1745 f2e07bb4 Michael Hanselmann

1746 f2e07bb4 Michael Hanselmann
  @type disk: L{objects.Disk}
1747 f2e07bb4 Michael Hanselmann
  @param disk: the disk object we want to open
1748 f2e07bb4 Michael Hanselmann

1749 f2e07bb4 Michael Hanselmann
  """
1750 f2e07bb4 Michael Hanselmann
  real_disk = _RecursiveFindBD(disk)
1751 f2e07bb4 Michael Hanselmann
  if real_disk is None:
1752 f2e07bb4 Michael Hanselmann
    _Fail("Block device '%s' is not set up", disk)
1753 f2e07bb4 Michael Hanselmann
1754 f2e07bb4 Michael Hanselmann
  real_disk.Open()
1755 f2e07bb4 Michael Hanselmann
1756 f2e07bb4 Michael Hanselmann
  return real_disk
1757 f2e07bb4 Michael Hanselmann
1758 f2e07bb4 Michael Hanselmann
1759 821d1bd1 Iustin Pop
def BlockdevFind(disk):
1760 a8083063 Iustin Pop
  """Check if a device is activated.
1761 a8083063 Iustin Pop

1762 5bbd3f7f Michael Hanselmann
  If it is, return information about the real device.
1763 a8083063 Iustin Pop

1764 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
1765 10c2650b Iustin Pop
  @param disk: the disk to find
1766 96acbc09 Michael Hanselmann
  @rtype: None or objects.BlockDevStatus
1767 96acbc09 Michael Hanselmann
  @return: None if the disk cannot be found, otherwise a the current
1768 96acbc09 Michael Hanselmann
           information
1769 a8083063 Iustin Pop

1770 a8083063 Iustin Pop
  """
1771 23829f6f Iustin Pop
  try:
1772 23829f6f Iustin Pop
    rbd = _RecursiveFindBD(disk)
1773 23829f6f Iustin Pop
  except errors.BlockDeviceError, err:
1774 2cc6781a Iustin Pop
    _Fail("Failed to find device: %s", err, exc=True)
1775 96acbc09 Michael Hanselmann
1776 a8083063 Iustin Pop
  if rbd is None:
1777 c26a6bd2 Iustin Pop
    return None
1778 96acbc09 Michael Hanselmann
1779 96acbc09 Michael Hanselmann
  return rbd.GetSyncStatus()
1780 a8083063 Iustin Pop
1781 a8083063 Iustin Pop
1782 968a7623 Iustin Pop
def BlockdevGetsize(disks):
1783 968a7623 Iustin Pop
  """Computes the size of the given disks.
1784 968a7623 Iustin Pop

1785 968a7623 Iustin Pop
  If a disk is not found, returns None instead.
1786 968a7623 Iustin Pop

1787 968a7623 Iustin Pop
  @type disks: list of L{objects.Disk}
1788 968a7623 Iustin Pop
  @param disks: the list of disk to compute the size for
1789 968a7623 Iustin Pop
  @rtype: list
1790 968a7623 Iustin Pop
  @return: list with elements None if the disk cannot be found,
1791 968a7623 Iustin Pop
      otherwise the size
1792 968a7623 Iustin Pop

1793 968a7623 Iustin Pop
  """
1794 968a7623 Iustin Pop
  result = []
1795 968a7623 Iustin Pop
  for cf in disks:
1796 968a7623 Iustin Pop
    try:
1797 968a7623 Iustin Pop
      rbd = _RecursiveFindBD(cf)
1798 1122eb25 Iustin Pop
    except errors.BlockDeviceError:
1799 968a7623 Iustin Pop
      result.append(None)
1800 968a7623 Iustin Pop
      continue
1801 968a7623 Iustin Pop
    if rbd is None:
1802 968a7623 Iustin Pop
      result.append(None)
1803 968a7623 Iustin Pop
    else:
1804 968a7623 Iustin Pop
      result.append(rbd.GetActualSize())
1805 968a7623 Iustin Pop
  return result
1806 968a7623 Iustin Pop
1807 968a7623 Iustin Pop
1808 858f3d18 Iustin Pop
def BlockdevExport(disk, dest_node, dest_path, cluster_name):
1809 858f3d18 Iustin Pop
  """Export a block device to a remote node.
1810 858f3d18 Iustin Pop

1811 858f3d18 Iustin Pop
  @type disk: L{objects.Disk}
1812 858f3d18 Iustin Pop
  @param disk: the description of the disk to export
1813 858f3d18 Iustin Pop
  @type dest_node: str
1814 858f3d18 Iustin Pop
  @param dest_node: the destination node to export to
1815 858f3d18 Iustin Pop
  @type dest_path: str
1816 858f3d18 Iustin Pop
  @param dest_path: the destination path on the target node
1817 858f3d18 Iustin Pop
  @type cluster_name: str
1818 858f3d18 Iustin Pop
  @param cluster_name: the cluster name, needed for SSH hostalias
1819 858f3d18 Iustin Pop
  @rtype: None
1820 858f3d18 Iustin Pop

1821 858f3d18 Iustin Pop
  """
1822 f2e07bb4 Michael Hanselmann
  real_disk = _OpenRealBD(disk)
1823 858f3d18 Iustin Pop
1824 858f3d18 Iustin Pop
  # the block size on the read dd is 1MiB to match our units
1825 858f3d18 Iustin Pop
  expcmd = utils.BuildShellCmd("set -e; set -o pipefail; "
1826 858f3d18 Iustin Pop
                               "dd if=%s bs=1048576 count=%s",
1827 858f3d18 Iustin Pop
                               real_disk.dev_path, str(disk.size))
1828 858f3d18 Iustin Pop
1829 858f3d18 Iustin Pop
  # we set here a smaller block size as, due to ssh buffering, more
1830 858f3d18 Iustin Pop
  # than 64-128k will mostly ignored; we use nocreat to fail if the
1831 858f3d18 Iustin Pop
  # device is not already there or we pass a wrong path; we use
1832 858f3d18 Iustin Pop
  # notrunc to no attempt truncate on an LV device; we use oflag=dsync
1833 858f3d18 Iustin Pop
  # to not buffer too much memory; this means that at best, we flush
1834 858f3d18 Iustin Pop
  # every 64k, which will not be very fast
1835 858f3d18 Iustin Pop
  destcmd = utils.BuildShellCmd("dd of=%s conv=nocreat,notrunc bs=65536"
1836 858f3d18 Iustin Pop
                                " oflag=dsync", dest_path)
1837 858f3d18 Iustin Pop
1838 858f3d18 Iustin Pop
  remotecmd = _GetSshRunner(cluster_name).BuildCmd(dest_node,
1839 858f3d18 Iustin Pop
                                                   constants.GANETI_RUNAS,
1840 858f3d18 Iustin Pop
                                                   destcmd)
1841 858f3d18 Iustin Pop
1842 858f3d18 Iustin Pop
  # all commands have been checked, so we're safe to combine them
1843 d0c8c01d Iustin Pop
  command = "|".join([expcmd, utils.ShellQuoteArgs(remotecmd)])
1844 858f3d18 Iustin Pop
1845 858f3d18 Iustin Pop
  result = utils.RunCmd(["bash", "-c", command])
1846 858f3d18 Iustin Pop
1847 858f3d18 Iustin Pop
  if result.failed:
1848 858f3d18 Iustin Pop
    _Fail("Disk copy command '%s' returned error: %s"
1849 858f3d18 Iustin Pop
          " output: %s", command, result.fail_reason, result.output)
1850 858f3d18 Iustin Pop
1851 858f3d18 Iustin Pop
1852 a8083063 Iustin Pop
def UploadFile(file_name, data, mode, uid, gid, atime, mtime):
1853 a8083063 Iustin Pop
  """Write a file to the filesystem.
1854 a8083063 Iustin Pop

1855 a8083063 Iustin Pop
  This allows the master to overwrite(!) a file. It will only perform
1856 a8083063 Iustin Pop
  the operation if the file belongs to a list of configuration files.
1857 a8083063 Iustin Pop

1858 10c2650b Iustin Pop
  @type file_name: str
1859 10c2650b Iustin Pop
  @param file_name: the target file name
1860 10c2650b Iustin Pop
  @type data: str
1861 10c2650b Iustin Pop
  @param data: the new contents of the file
1862 10c2650b Iustin Pop
  @type mode: int
1863 10c2650b Iustin Pop
  @param mode: the mode to give the file (can be None)
1864 9a914f7a René Nussbaumer
  @type uid: string
1865 9a914f7a René Nussbaumer
  @param uid: the owner of the file
1866 9a914f7a René Nussbaumer
  @type gid: string
1867 9a914f7a René Nussbaumer
  @param gid: the group of the file
1868 10c2650b Iustin Pop
  @type atime: float
1869 10c2650b Iustin Pop
  @param atime: the atime to set on the file (can be None)
1870 10c2650b Iustin Pop
  @type mtime: float
1871 10c2650b Iustin Pop
  @param mtime: the mtime to set on the file (can be None)
1872 c26a6bd2 Iustin Pop
  @rtype: None
1873 10c2650b Iustin Pop

1874 a8083063 Iustin Pop
  """
1875 a8083063 Iustin Pop
  if not os.path.isabs(file_name):
1876 2cc6781a Iustin Pop
    _Fail("Filename passed to UploadFile is not absolute: '%s'", file_name)
1877 a8083063 Iustin Pop
1878 360b0dc2 Iustin Pop
  if file_name not in _ALLOWED_UPLOAD_FILES:
1879 2cc6781a Iustin Pop
    _Fail("Filename passed to UploadFile not in allowed upload targets: '%s'",
1880 2cc6781a Iustin Pop
          file_name)
1881 a8083063 Iustin Pop
1882 12bce260 Michael Hanselmann
  raw_data = _Decompress(data)
1883 12bce260 Michael Hanselmann
1884 9a914f7a René Nussbaumer
  if not (isinstance(uid, basestring) and isinstance(gid, basestring)):
1885 9a914f7a René Nussbaumer
    _Fail("Invalid username/groupname type")
1886 9a914f7a René Nussbaumer
1887 9a914f7a René Nussbaumer
  getents = runtime.GetEnts()
1888 9a914f7a René Nussbaumer
  uid = getents.LookupUser(uid)
1889 9a914f7a René Nussbaumer
  gid = getents.LookupGroup(gid)
1890 9a914f7a René Nussbaumer
1891 8f065ae2 Iustin Pop
  utils.SafeWriteFile(file_name, None,
1892 8f065ae2 Iustin Pop
                      data=raw_data, mode=mode, uid=uid, gid=gid,
1893 8f065ae2 Iustin Pop
                      atime=atime, mtime=mtime)
1894 a8083063 Iustin Pop
1895 386b57af Iustin Pop
1896 b2f29800 René Nussbaumer
def RunOob(oob_program, command, node, timeout):
1897 b2f29800 René Nussbaumer
  """Executes oob_program with given command on given node.
1898 b2f29800 René Nussbaumer

1899 b2f29800 René Nussbaumer
  @param oob_program: The path to the executable oob_program
1900 b2f29800 René Nussbaumer
  @param command: The command to invoke on oob_program
1901 b2f29800 René Nussbaumer
  @param node: The node given as an argument to the program
1902 b2f29800 René Nussbaumer
  @param timeout: Timeout after which we kill the oob program
1903 b2f29800 René Nussbaumer

1904 b2f29800 René Nussbaumer
  @return: stdout
1905 b2f29800 René Nussbaumer
  @raise RPCFail: If execution fails for some reason
1906 b2f29800 René Nussbaumer

1907 b2f29800 René Nussbaumer
  """
1908 b2f29800 René Nussbaumer
  result = utils.RunCmd([oob_program, command, node], timeout=timeout)
1909 b2f29800 René Nussbaumer
1910 b2f29800 René Nussbaumer
  if result.failed:
1911 b2f29800 René Nussbaumer
    _Fail("'%s' failed with reason '%s'; output: %s", result.cmd,
1912 b2f29800 René Nussbaumer
          result.fail_reason, result.output)
1913 b2f29800 René Nussbaumer
1914 b2f29800 René Nussbaumer
  return result.stdout
1915 b2f29800 René Nussbaumer
1916 b2f29800 René Nussbaumer
1917 03d1dba2 Michael Hanselmann
def WriteSsconfFiles(values):
1918 89b14f05 Iustin Pop
  """Update all ssconf files.
1919 89b14f05 Iustin Pop

1920 89b14f05 Iustin Pop
  Wrapper around the SimpleStore.WriteFiles.
1921 89b14f05 Iustin Pop

1922 89b14f05 Iustin Pop
  """
1923 89b14f05 Iustin Pop
  ssconf.SimpleStore().WriteFiles(values)
1924 6ddc95ec Michael Hanselmann
1925 6ddc95ec Michael Hanselmann
1926 a8083063 Iustin Pop
def _ErrnoOrStr(err):
1927 a8083063 Iustin Pop
  """Format an EnvironmentError exception.
1928 a8083063 Iustin Pop

1929 10c2650b Iustin Pop
  If the L{err} argument has an errno attribute, it will be looked up
1930 10c2650b Iustin Pop
  and converted into a textual C{E...} description. Otherwise the
1931 10c2650b Iustin Pop
  string representation of the error will be returned.
1932 10c2650b Iustin Pop

1933 10c2650b Iustin Pop
  @type err: L{EnvironmentError}
1934 10c2650b Iustin Pop
  @param err: the exception to format
1935 a8083063 Iustin Pop

1936 a8083063 Iustin Pop
  """
1937 d0c8c01d Iustin Pop
  if hasattr(err, "errno"):
1938 a8083063 Iustin Pop
    detail = errno.errorcode[err.errno]
1939 a8083063 Iustin Pop
  else:
1940 a8083063 Iustin Pop
    detail = str(err)
1941 a8083063 Iustin Pop
  return detail
1942 a8083063 Iustin Pop
1943 5d0fe286 Iustin Pop
1944 c19f9810 Iustin Pop
def _OSOndiskAPIVersion(os_dir):
1945 2f8598a5 Alexander Schreiber
  """Compute and return the API version of a given OS.
1946 a8083063 Iustin Pop

1947 c19f9810 Iustin Pop
  This function will try to read the API version of the OS residing in
1948 c19f9810 Iustin Pop
  the 'os_dir' directory.
1949 7c3d51d4 Guido Trotter

1950 10c2650b Iustin Pop
  @type os_dir: str
1951 c19f9810 Iustin Pop
  @param os_dir: the directory in which we should look for the OS
1952 8e70b181 Iustin Pop
  @rtype: tuple
1953 8e70b181 Iustin Pop
  @return: tuple (status, data) with status denoting the validity and
1954 8e70b181 Iustin Pop
      data holding either the vaid versions or an error message
1955 a8083063 Iustin Pop

1956 a8083063 Iustin Pop
  """
1957 e02b9114 Iustin Pop
  api_file = utils.PathJoin(os_dir, constants.OS_API_FILE)
1958 a8083063 Iustin Pop
1959 a8083063 Iustin Pop
  try:
1960 a8083063 Iustin Pop
    st = os.stat(api_file)
1961 a8083063 Iustin Pop
  except EnvironmentError, err:
1962 b6b45e0d Guido Trotter
    return False, ("Required file '%s' not found under path %s: %s" %
1963 b6b45e0d Guido Trotter
                   (constants.OS_API_FILE, os_dir, _ErrnoOrStr(err)))
1964 a8083063 Iustin Pop
1965 a8083063 Iustin Pop
  if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
1966 b6b45e0d Guido Trotter
    return False, ("File '%s' in %s is not a regular file" %
1967 b6b45e0d Guido Trotter
                   (constants.OS_API_FILE, os_dir))
1968 a8083063 Iustin Pop
1969 a8083063 Iustin Pop
  try:
1970 3374afa9 Guido Trotter
    api_versions = utils.ReadFile(api_file).splitlines()
1971 a8083063 Iustin Pop
  except EnvironmentError, err:
1972 255dcebd Iustin Pop
    return False, ("Error while reading the API version file at %s: %s" %
1973 255dcebd Iustin Pop
                   (api_file, _ErrnoOrStr(err)))
1974 a8083063 Iustin Pop
1975 a8083063 Iustin Pop
  try:
1976 63b9b186 Guido Trotter
    api_versions = [int(version.strip()) for version in api_versions]
1977 a8083063 Iustin Pop
  except (TypeError, ValueError), err:
1978 255dcebd Iustin Pop
    return False, ("API version(s) can't be converted to integer: %s" %
1979 255dcebd Iustin Pop
                   str(err))
1980 a8083063 Iustin Pop
1981 255dcebd Iustin Pop
  return True, api_versions
1982 a8083063 Iustin Pop
1983 386b57af Iustin Pop
1984 7c3d51d4 Guido Trotter
def DiagnoseOS(top_dirs=None):
1985 a8083063 Iustin Pop
  """Compute the validity for all OSes.
1986 a8083063 Iustin Pop

1987 10c2650b Iustin Pop
  @type top_dirs: list
1988 10c2650b Iustin Pop
  @param top_dirs: the list of directories in which to
1989 10c2650b Iustin Pop
      search (if not given defaults to
1990 10c2650b Iustin Pop
      L{constants.OS_SEARCH_PATH})
1991 10c2650b Iustin Pop
  @rtype: list of L{objects.OS}
1992 bad78e66 Iustin Pop
  @return: a list of tuples (name, path, status, diagnose, variants,
1993 bad78e66 Iustin Pop
      parameters, api_version) for all (potential) OSes under all
1994 bad78e66 Iustin Pop
      search paths, where:
1995 255dcebd Iustin Pop
          - name is the (potential) OS name
1996 255dcebd Iustin Pop
          - path is the full path to the OS
1997 255dcebd Iustin Pop
          - status True/False is the validity of the OS
1998 255dcebd Iustin Pop
          - diagnose is the error message for an invalid OS, otherwise empty
1999 ba00557a Guido Trotter
          - variants is a list of supported OS variants, if any
2000 c7d04a6b Iustin Pop
          - parameters is a list of (name, help) parameters, if any
2001 bad78e66 Iustin Pop
          - api_version is a list of support OS API versions
2002 a8083063 Iustin Pop

2003 a8083063 Iustin Pop
  """
2004 7c3d51d4 Guido Trotter
  if top_dirs is None:
2005 7c3d51d4 Guido Trotter
    top_dirs = constants.OS_SEARCH_PATH
2006 a8083063 Iustin Pop
2007 a8083063 Iustin Pop
  result = []
2008 65fe4693 Iustin Pop
  for dir_name in top_dirs:
2009 65fe4693 Iustin Pop
    if os.path.isdir(dir_name):
2010 7c3d51d4 Guido Trotter
      try:
2011 65fe4693 Iustin Pop
        f_names = utils.ListVisibleFiles(dir_name)
2012 7c3d51d4 Guido Trotter
      except EnvironmentError, err:
2013 29921401 Iustin Pop
        logging.exception("Can't list the OS directory %s: %s", dir_name, err)
2014 7c3d51d4 Guido Trotter
        break
2015 7c3d51d4 Guido Trotter
      for name in f_names:
2016 e02b9114 Iustin Pop
        os_path = utils.PathJoin(dir_name, name)
2017 255dcebd Iustin Pop
        status, os_inst = _TryOSFromDisk(name, base_dir=dir_name)
2018 255dcebd Iustin Pop
        if status:
2019 255dcebd Iustin Pop
          diagnose = ""
2020 ba00557a Guido Trotter
          variants = os_inst.supported_variants
2021 c7d04a6b Iustin Pop
          parameters = os_inst.supported_parameters
2022 bad78e66 Iustin Pop
          api_versions = os_inst.api_versions
2023 255dcebd Iustin Pop
        else:
2024 255dcebd Iustin Pop
          diagnose = os_inst
2025 bad78e66 Iustin Pop
          variants = parameters = api_versions = []
2026 bad78e66 Iustin Pop
        result.append((name, os_path, status, diagnose, variants,
2027 bad78e66 Iustin Pop
                       parameters, api_versions))
2028 a8083063 Iustin Pop
2029 c26a6bd2 Iustin Pop
  return result
2030 a8083063 Iustin Pop
2031 a8083063 Iustin Pop
2032 255dcebd Iustin Pop
def _TryOSFromDisk(name, base_dir=None):
2033 a8083063 Iustin Pop
  """Create an OS instance from disk.
2034 a8083063 Iustin Pop

2035 a8083063 Iustin Pop
  This function will return an OS instance if the given name is a
2036 8e70b181 Iustin Pop
  valid OS name.
2037 a8083063 Iustin Pop

2038 8ee4dc80 Guido Trotter
  @type base_dir: string
2039 8ee4dc80 Guido Trotter
  @keyword base_dir: Base directory containing OS installations.
2040 8ee4dc80 Guido Trotter
                     Defaults to a search in all the OS_SEARCH_PATH dirs.
2041 255dcebd Iustin Pop
  @rtype: tuple
2042 255dcebd Iustin Pop
  @return: success and either the OS instance if we find a valid one,
2043 255dcebd Iustin Pop
      or error message
2044 7c3d51d4 Guido Trotter

2045 a8083063 Iustin Pop
  """
2046 56bcd3f4 Guido Trotter
  if base_dir is None:
2047 57c177af Iustin Pop
    os_dir = utils.FindFile(name, constants.OS_SEARCH_PATH, os.path.isdir)
2048 c34c0cfd Iustin Pop
  else:
2049 f95c81bf Iustin Pop
    os_dir = utils.FindFile(name, [base_dir], os.path.isdir)
2050 f95c81bf Iustin Pop
2051 f95c81bf Iustin Pop
  if os_dir is None:
2052 5c0433d6 Iustin Pop
    return False, "Directory for OS %s not found in search path" % name
2053 a8083063 Iustin Pop
2054 c19f9810 Iustin Pop
  status, api_versions = _OSOndiskAPIVersion(os_dir)
2055 255dcebd Iustin Pop
  if not status:
2056 255dcebd Iustin Pop
    # push the error up
2057 255dcebd Iustin Pop
    return status, api_versions
2058 a8083063 Iustin Pop
2059 d1a7d66f Guido Trotter
  if not constants.OS_API_VERSIONS.intersection(api_versions):
2060 255dcebd Iustin Pop
    return False, ("API version mismatch for path '%s': found %s, want %s." %
2061 d1a7d66f Guido Trotter
                   (os_dir, api_versions, constants.OS_API_VERSIONS))
2062 a8083063 Iustin Pop
2063 35007011 Iustin Pop
  # OS Files dictionary, we will populate it with the absolute path
2064 35007011 Iustin Pop
  # names; if the value is True, then it is a required file, otherwise
2065 35007011 Iustin Pop
  # an optional one
2066 35007011 Iustin Pop
  os_files = dict.fromkeys(constants.OS_SCRIPTS, True)
2067 a8083063 Iustin Pop
2068 95075fba Guido Trotter
  if max(api_versions) >= constants.OS_API_V15:
2069 35007011 Iustin Pop
    os_files[constants.OS_VARIANTS_FILE] = False
2070 95075fba Guido Trotter
2071 c7d04a6b Iustin Pop
  if max(api_versions) >= constants.OS_API_V20:
2072 35007011 Iustin Pop
    os_files[constants.OS_PARAMETERS_FILE] = True
2073 c7d04a6b Iustin Pop
  else:
2074 c7d04a6b Iustin Pop
    del os_files[constants.OS_SCRIPT_VERIFY]
2075 c7d04a6b Iustin Pop
2076 35007011 Iustin Pop
  for (filename, required) in os_files.items():
2077 e02b9114 Iustin Pop
    os_files[filename] = utils.PathJoin(os_dir, filename)
2078 a8083063 Iustin Pop
2079 a8083063 Iustin Pop
    try:
2080 ea79fc15 Michael Hanselmann
      st = os.stat(os_files[filename])
2081 a8083063 Iustin Pop
    except EnvironmentError, err:
2082 35007011 Iustin Pop
      if err.errno == errno.ENOENT and not required:
2083 35007011 Iustin Pop
        del os_files[filename]
2084 35007011 Iustin Pop
        continue
2085 41ba4061 Guido Trotter
      return False, ("File '%s' under path '%s' is missing (%s)" %
2086 ea79fc15 Michael Hanselmann
                     (filename, os_dir, _ErrnoOrStr(err)))
2087 a8083063 Iustin Pop
2088 a8083063 Iustin Pop
    if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
2089 41ba4061 Guido Trotter
      return False, ("File '%s' under path '%s' is not a regular file" %
2090 ea79fc15 Michael Hanselmann
                     (filename, os_dir))
2091 255dcebd Iustin Pop
2092 ea79fc15 Michael Hanselmann
    if filename in constants.OS_SCRIPTS:
2093 0757c107 Guido Trotter
      if stat.S_IMODE(st.st_mode) & stat.S_IXUSR != stat.S_IXUSR:
2094 0757c107 Guido Trotter
        return False, ("File '%s' under path '%s' is not executable" %
2095 ea79fc15 Michael Hanselmann
                       (filename, os_dir))
2096 0757c107 Guido Trotter
2097 845da3e8 Iustin Pop
  variants = []
2098 95075fba Guido Trotter
  if constants.OS_VARIANTS_FILE in os_files:
2099 95075fba Guido Trotter
    variants_file = os_files[constants.OS_VARIANTS_FILE]
2100 95075fba Guido Trotter
    try:
2101 95075fba Guido Trotter
      variants = utils.ReadFile(variants_file).splitlines()
2102 95075fba Guido Trotter
    except EnvironmentError, err:
2103 35007011 Iustin Pop
      # we accept missing files, but not other errors
2104 35007011 Iustin Pop
      if err.errno != errno.ENOENT:
2105 35007011 Iustin Pop
        return False, ("Error while reading the OS variants file at %s: %s" %
2106 35007011 Iustin Pop
                       (variants_file, _ErrnoOrStr(err)))
2107 0757c107 Guido Trotter
2108 c7d04a6b Iustin Pop
  parameters = []
2109 c7d04a6b Iustin Pop
  if constants.OS_PARAMETERS_FILE in os_files:
2110 c7d04a6b Iustin Pop
    parameters_file = os_files[constants.OS_PARAMETERS_FILE]
2111 c7d04a6b Iustin Pop
    try:
2112 c7d04a6b Iustin Pop
      parameters = utils.ReadFile(parameters_file).splitlines()
2113 c7d04a6b Iustin Pop
    except EnvironmentError, err:
2114 c7d04a6b Iustin Pop
      return False, ("Error while reading the OS parameters file at %s: %s" %
2115 c7d04a6b Iustin Pop
                     (parameters_file, _ErrnoOrStr(err)))
2116 c7d04a6b Iustin Pop
    parameters = [v.split(None, 1) for v in parameters]
2117 c7d04a6b Iustin Pop
2118 8e70b181 Iustin Pop
  os_obj = objects.OS(name=name, path=os_dir,
2119 41ba4061 Guido Trotter
                      create_script=os_files[constants.OS_SCRIPT_CREATE],
2120 41ba4061 Guido Trotter
                      export_script=os_files[constants.OS_SCRIPT_EXPORT],
2121 41ba4061 Guido Trotter
                      import_script=os_files[constants.OS_SCRIPT_IMPORT],
2122 41ba4061 Guido Trotter
                      rename_script=os_files[constants.OS_SCRIPT_RENAME],
2123 40684c3a Iustin Pop
                      verify_script=os_files.get(constants.OS_SCRIPT_VERIFY,
2124 40684c3a Iustin Pop
                                                 None),
2125 95075fba Guido Trotter
                      supported_variants=variants,
2126 c7d04a6b Iustin Pop
                      supported_parameters=parameters,
2127 255dcebd Iustin Pop
                      api_versions=api_versions)
2128 255dcebd Iustin Pop
  return True, os_obj
2129 255dcebd Iustin Pop
2130 255dcebd Iustin Pop
2131 255dcebd Iustin Pop
def OSFromDisk(name, base_dir=None):
2132 255dcebd Iustin Pop
  """Create an OS instance from disk.
2133 255dcebd Iustin Pop

2134 255dcebd Iustin Pop
  This function will return an OS instance if the given name is a
2135 255dcebd Iustin Pop
  valid OS name. Otherwise, it will raise an appropriate
2136 255dcebd Iustin Pop
  L{RPCFail} exception, detailing why this is not a valid OS.
2137 255dcebd Iustin Pop

2138 255dcebd Iustin Pop
  This is just a wrapper over L{_TryOSFromDisk}, which doesn't raise
2139 255dcebd Iustin Pop
  an exception but returns true/false status data.
2140 255dcebd Iustin Pop

2141 255dcebd Iustin Pop
  @type base_dir: string
2142 255dcebd Iustin Pop
  @keyword base_dir: Base directory containing OS installations.
2143 255dcebd Iustin Pop
                     Defaults to a search in all the OS_SEARCH_PATH dirs.
2144 255dcebd Iustin Pop
  @rtype: L{objects.OS}
2145 255dcebd Iustin Pop
  @return: the OS instance if we find a valid one
2146 255dcebd Iustin Pop
  @raise RPCFail: if we don't find a valid OS
2147 255dcebd Iustin Pop

2148 255dcebd Iustin Pop
  """
2149 870dc44c Iustin Pop
  name_only = objects.OS.GetName(name)
2150 6ee7102a Guido Trotter
  status, payload = _TryOSFromDisk(name_only, base_dir)
2151 255dcebd Iustin Pop
2152 255dcebd Iustin Pop
  if not status:
2153 255dcebd Iustin Pop
    _Fail(payload)
2154 a8083063 Iustin Pop
2155 255dcebd Iustin Pop
  return payload
2156 a8083063 Iustin Pop
2157 a8083063 Iustin Pop
2158 a025e535 Vitaly Kuznetsov
def OSCoreEnv(os_name, inst_os, os_params, debug=0):
2159 efaa9b06 Iustin Pop
  """Calculate the basic environment for an os script.
2160 2266edb2 Guido Trotter

2161 a025e535 Vitaly Kuznetsov
  @type os_name: str
2162 a025e535 Vitaly Kuznetsov
  @param os_name: full operating system name (including variant)
2163 099c52ad Iustin Pop
  @type inst_os: L{objects.OS}
2164 099c52ad Iustin Pop
  @param inst_os: operating system for which the environment is being built
2165 1bdcbbab Iustin Pop
  @type os_params: dict
2166 1bdcbbab Iustin Pop
  @param os_params: the OS parameters
2167 2266edb2 Guido Trotter
  @type debug: integer
2168 10c2650b Iustin Pop
  @param debug: debug level (0 or 1, for OS Api 10)
2169 2266edb2 Guido Trotter
  @rtype: dict
2170 2266edb2 Guido Trotter
  @return: dict of environment variables
2171 10c2650b Iustin Pop
  @raise errors.BlockDeviceError: if the block device
2172 10c2650b Iustin Pop
      cannot be found
2173 2266edb2 Guido Trotter

2174 2266edb2 Guido Trotter
  """
2175 2266edb2 Guido Trotter
  result = {}
2176 099c52ad Iustin Pop
  api_version = \
2177 099c52ad Iustin Pop
    max(constants.OS_API_VERSIONS.intersection(inst_os.api_versions))
2178 d0c8c01d Iustin Pop
  result["OS_API_VERSION"] = "%d" % api_version
2179 d0c8c01d Iustin Pop
  result["OS_NAME"] = inst_os.name
2180 d0c8c01d Iustin Pop
  result["DEBUG_LEVEL"] = "%d" % debug
2181 efaa9b06 Iustin Pop
2182 efaa9b06 Iustin Pop
  # OS variants
2183 35007011 Iustin Pop
  if api_version >= constants.OS_API_V15 and inst_os.supported_variants:
2184 870dc44c Iustin Pop
    variant = objects.OS.GetVariant(os_name)
2185 870dc44c Iustin Pop
    if not variant:
2186 099c52ad Iustin Pop
      variant = inst_os.supported_variants[0]
2187 35007011 Iustin Pop
  else:
2188 35007011 Iustin Pop
    variant = ""
2189 35007011 Iustin Pop
  result["OS_VARIANT"] = variant
2190 efaa9b06 Iustin Pop
2191 1bdcbbab Iustin Pop
  # OS params
2192 1bdcbbab Iustin Pop
  for pname, pvalue in os_params.items():
2193 d0c8c01d Iustin Pop
    result["OSP_%s" % pname.upper()] = pvalue
2194 1bdcbbab Iustin Pop
2195 efaa9b06 Iustin Pop
  return result
2196 efaa9b06 Iustin Pop
2197 efaa9b06 Iustin Pop
2198 efaa9b06 Iustin Pop
def OSEnvironment(instance, inst_os, debug=0):
2199 efaa9b06 Iustin Pop
  """Calculate the environment for an os script.
2200 efaa9b06 Iustin Pop

2201 efaa9b06 Iustin Pop
  @type instance: L{objects.Instance}
2202 efaa9b06 Iustin Pop
  @param instance: target instance for the os script run
2203 efaa9b06 Iustin Pop
  @type inst_os: L{objects.OS}
2204 efaa9b06 Iustin Pop
  @param inst_os: operating system for which the environment is being built
2205 efaa9b06 Iustin Pop
  @type debug: integer
2206 efaa9b06 Iustin Pop
  @param debug: debug level (0 or 1, for OS Api 10)
2207 efaa9b06 Iustin Pop
  @rtype: dict
2208 efaa9b06 Iustin Pop
  @return: dict of environment variables
2209 efaa9b06 Iustin Pop
  @raise errors.BlockDeviceError: if the block device
2210 efaa9b06 Iustin Pop
      cannot be found
2211 efaa9b06 Iustin Pop

2212 efaa9b06 Iustin Pop
  """
2213 a025e535 Vitaly Kuznetsov
  result = OSCoreEnv(instance.os, inst_os, instance.osparams, debug=debug)
2214 efaa9b06 Iustin Pop
2215 519719fd Marco Casavecchia
  for attr in ["name", "os", "uuid", "ctime", "mtime", "primary_node"]:
2216 f2165b8a Iustin Pop
    result["INSTANCE_%s" % attr.upper()] = str(getattr(instance, attr))
2217 f2165b8a Iustin Pop
2218 d0c8c01d Iustin Pop
  result["HYPERVISOR"] = instance.hypervisor
2219 d0c8c01d Iustin Pop
  result["DISK_COUNT"] = "%d" % len(instance.disks)
2220 d0c8c01d Iustin Pop
  result["NIC_COUNT"] = "%d" % len(instance.nics)
2221 d0c8c01d Iustin Pop
  result["INSTANCE_SECONDARY_NODES"] = \
2222 d0c8c01d Iustin Pop
      ("%s" % " ".join(instance.secondary_nodes))
2223 efaa9b06 Iustin Pop
2224 efaa9b06 Iustin Pop
  # Disks
2225 2266edb2 Guido Trotter
  for idx, disk in enumerate(instance.disks):
2226 f2e07bb4 Michael Hanselmann
    real_disk = _OpenRealBD(disk)
2227 d0c8c01d Iustin Pop
    result["DISK_%d_PATH" % idx] = real_disk.dev_path
2228 d0c8c01d Iustin Pop
    result["DISK_%d_ACCESS" % idx] = disk.mode
2229 2266edb2 Guido Trotter
    if constants.HV_DISK_TYPE in instance.hvparams:
2230 d0c8c01d Iustin Pop
      result["DISK_%d_FRONTEND_TYPE" % idx] = \
2231 2266edb2 Guido Trotter
        instance.hvparams[constants.HV_DISK_TYPE]
2232 2266edb2 Guido Trotter
    if disk.dev_type in constants.LDS_BLOCK:
2233 d0c8c01d Iustin Pop
      result["DISK_%d_BACKEND_TYPE" % idx] = "block"
2234 2266edb2 Guido Trotter
    elif disk.dev_type == constants.LD_FILE:
2235 d0c8c01d Iustin Pop
      result["DISK_%d_BACKEND_TYPE" % idx] = \
2236 d0c8c01d Iustin Pop
        "file:%s" % disk.physical_id[0]
2237 efaa9b06 Iustin Pop
2238 efaa9b06 Iustin Pop
  # NICs
2239 2266edb2 Guido Trotter
  for idx, nic in enumerate(instance.nics):
2240 d0c8c01d Iustin Pop
    result["NIC_%d_MAC" % idx] = nic.mac
2241 2266edb2 Guido Trotter
    if nic.ip:
2242 d0c8c01d Iustin Pop
      result["NIC_%d_IP" % idx] = nic.ip
2243 d0c8c01d Iustin Pop
    result["NIC_%d_MODE" % idx] = nic.nicparams[constants.NIC_MODE]
2244 1ba9227f Guido Trotter
    if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
2245 d0c8c01d Iustin Pop
      result["NIC_%d_BRIDGE" % idx] = nic.nicparams[constants.NIC_LINK]
2246 1ba9227f Guido Trotter
    if nic.nicparams[constants.NIC_LINK]:
2247 d0c8c01d Iustin Pop
      result["NIC_%d_LINK" % idx] = nic.nicparams[constants.NIC_LINK]
2248 2266edb2 Guido Trotter
    if constants.HV_NIC_TYPE in instance.hvparams:
2249 d0c8c01d Iustin Pop
      result["NIC_%d_FRONTEND_TYPE" % idx] = \
2250 2266edb2 Guido Trotter
        instance.hvparams[constants.HV_NIC_TYPE]
2251 2266edb2 Guido Trotter
2252 efaa9b06 Iustin Pop
  # HV/BE params
2253 67fc3042 Iustin Pop
  for source, kind in [(instance.beparams, "BE"), (instance.hvparams, "HV")]:
2254 67fc3042 Iustin Pop
    for key, value in source.items():
2255 030b218a Iustin Pop
      result["INSTANCE_%s_%s" % (kind, key)] = str(value)
2256 67fc3042 Iustin Pop
2257 2266edb2 Guido Trotter
  return result
2258 a8083063 Iustin Pop
2259 f2e07bb4 Michael Hanselmann
2260 a59faf4b Iustin Pop
def BlockdevGrow(disk, amount, dryrun):
2261 594609c0 Iustin Pop
  """Grow a stack of block devices.
2262 594609c0 Iustin Pop

2263 594609c0 Iustin Pop
  This function is called recursively, with the childrens being the
2264 10c2650b Iustin Pop
  first ones to resize.
2265 594609c0 Iustin Pop

2266 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
2267 10c2650b Iustin Pop
  @param disk: the disk to be grown
2268 a59faf4b Iustin Pop
  @type amount: integer
2269 a59faf4b Iustin Pop
  @param amount: the amount (in mebibytes) to grow with
2270 a59faf4b Iustin Pop
  @type dryrun: boolean
2271 a59faf4b Iustin Pop
  @param dryrun: whether to execute the operation in simulation mode
2272 a59faf4b Iustin Pop
      only, without actually increasing the size
2273 10c2650b Iustin Pop
  @rtype: (status, result)
2274 a59faf4b Iustin Pop
  @return: a tuple with the status of the operation (True/False), and
2275 a59faf4b Iustin Pop
      the errors message if status is False
2276 594609c0 Iustin Pop

2277 594609c0 Iustin Pop
  """
2278 594609c0 Iustin Pop
  r_dev = _RecursiveFindBD(disk)
2279 594609c0 Iustin Pop
  if r_dev is None:
2280 afdc3985 Iustin Pop
    _Fail("Cannot find block device %s", disk)
2281 594609c0 Iustin Pop
2282 594609c0 Iustin Pop
  try:
2283 a59faf4b Iustin Pop
    r_dev.Grow(amount, dryrun)
2284 594609c0 Iustin Pop
  except errors.BlockDeviceError, err:
2285 2cc6781a Iustin Pop
    _Fail("Failed to grow block device: %s", err, exc=True)
2286 594609c0 Iustin Pop
2287 594609c0 Iustin Pop
2288 821d1bd1 Iustin Pop
def BlockdevSnapshot(disk):
2289 a8083063 Iustin Pop
  """Create a snapshot copy of a block device.
2290 a8083063 Iustin Pop

2291 a8083063 Iustin Pop
  This function is called recursively, and the snapshot is actually created
2292 a8083063 Iustin Pop
  just for the leaf lvm backend device.
2293 a8083063 Iustin Pop

2294 e9e9263d Guido Trotter
  @type disk: L{objects.Disk}
2295 e9e9263d Guido Trotter
  @param disk: the disk to be snapshotted
2296 e9e9263d Guido Trotter
  @rtype: string
2297 800ac399 Iustin Pop
  @return: snapshot disk ID as (vg, lv)
2298 a8083063 Iustin Pop

2299 098c0958 Michael Hanselmann
  """
2300 433c63aa Iustin Pop
  if disk.dev_type == constants.LD_DRBD8:
2301 433c63aa Iustin Pop
    if not disk.children:
2302 433c63aa Iustin Pop
      _Fail("DRBD device '%s' without backing storage cannot be snapshotted",
2303 433c63aa Iustin Pop
            disk.unique_id)
2304 433c63aa Iustin Pop
    return BlockdevSnapshot(disk.children[0])
2305 fe96220b Iustin Pop
  elif disk.dev_type == constants.LD_LV:
2306 a8083063 Iustin Pop
    r_dev = _RecursiveFindBD(disk)
2307 a8083063 Iustin Pop
    if r_dev is not None:
2308 433c63aa Iustin Pop
      # FIXME: choose a saner value for the snapshot size
2309 a8083063 Iustin Pop
      # let's stay on the safe side and ask for the full size, for now
2310 c26a6bd2 Iustin Pop
      return r_dev.Snapshot(disk.size)
2311 a8083063 Iustin Pop
    else:
2312 87812fd3 Iustin Pop
      _Fail("Cannot find block device %s", disk)
2313 a8083063 Iustin Pop
  else:
2314 87812fd3 Iustin Pop
    _Fail("Cannot snapshot non-lvm block device '%s' of type '%s'",
2315 87812fd3 Iustin Pop
          disk.unique_id, disk.dev_type)
2316 a8083063 Iustin Pop
2317 a8083063 Iustin Pop
2318 a8083063 Iustin Pop
def FinalizeExport(instance, snap_disks):
2319 a8083063 Iustin Pop
  """Write out the export configuration information.
2320 a8083063 Iustin Pop

2321 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
2322 10c2650b Iustin Pop
  @param instance: the instance which we export, used for
2323 10c2650b Iustin Pop
      saving configuration
2324 10c2650b Iustin Pop
  @type snap_disks: list of L{objects.Disk}
2325 10c2650b Iustin Pop
  @param snap_disks: list of snapshot block devices, which
2326 10c2650b Iustin Pop
      will be used to get the actual name of the dump file
2327 a8083063 Iustin Pop

2328 c26a6bd2 Iustin Pop
  @rtype: None
2329 a8083063 Iustin Pop

2330 098c0958 Michael Hanselmann
  """
2331 c4feafe8 Iustin Pop
  destdir = utils.PathJoin(constants.EXPORT_DIR, instance.name + ".new")
2332 c4feafe8 Iustin Pop
  finaldestdir = utils.PathJoin(constants.EXPORT_DIR, instance.name)
2333 a8083063 Iustin Pop
2334 a8083063 Iustin Pop
  config = objects.SerializableConfigParser()
2335 a8083063 Iustin Pop
2336 a8083063 Iustin Pop
  config.add_section(constants.INISECT_EXP)
2337 d0c8c01d Iustin Pop
  config.set(constants.INISECT_EXP, "version", "0")
2338 d0c8c01d Iustin Pop
  config.set(constants.INISECT_EXP, "timestamp", "%d" % int(time.time()))
2339 d0c8c01d Iustin Pop
  config.set(constants.INISECT_EXP, "source", instance.primary_node)
2340 d0c8c01d Iustin Pop
  config.set(constants.INISECT_EXP, "os", instance.os)
2341 775b8743 Michael Hanselmann
  config.set(constants.INISECT_EXP, "compression", "none")
2342 a8083063 Iustin Pop
2343 a8083063 Iustin Pop
  config.add_section(constants.INISECT_INS)
2344 d0c8c01d Iustin Pop
  config.set(constants.INISECT_INS, "name", instance.name)
2345 d0c8c01d Iustin Pop
  config.set(constants.INISECT_INS, "memory", "%d" %
2346 51de46bf Iustin Pop
             instance.beparams[constants.BE_MEMORY])
2347 d0c8c01d Iustin Pop
  config.set(constants.INISECT_INS, "vcpus", "%d" %
2348 51de46bf Iustin Pop
             instance.beparams[constants.BE_VCPUS])
2349 d0c8c01d Iustin Pop
  config.set(constants.INISECT_INS, "disk_template", instance.disk_template)
2350 d0c8c01d Iustin Pop
  config.set(constants.INISECT_INS, "hypervisor", instance.hypervisor)
2351 fbb2c636 Michael Hanselmann
  config.set(constants.INISECT_INS, "tags", " ".join(instance.GetTags()))
2352 66f93869 Manuel Franceschini
2353 95268cc3 Iustin Pop
  nic_total = 0
2354 a8083063 Iustin Pop
  for nic_count, nic in enumerate(instance.nics):
2355 95268cc3 Iustin Pop
    nic_total += 1
2356 d0c8c01d Iustin Pop
    config.set(constants.INISECT_INS, "nic%d_mac" %
2357 d0c8c01d Iustin Pop
               nic_count, "%s" % nic.mac)
2358 d0c8c01d Iustin Pop
    config.set(constants.INISECT_INS, "nic%d_ip" % nic_count, "%s" % nic.ip)
2359 6801eb5c Iustin Pop
    for param in constants.NICS_PARAMETER_TYPES:
2360 d0c8c01d Iustin Pop
      config.set(constants.INISECT_INS, "nic%d_%s" % (nic_count, param),
2361 d0c8c01d Iustin Pop
                 "%s" % nic.nicparams.get(param, None))
2362 a8083063 Iustin Pop
  # TODO: redundant: on load can read nics until it doesn't exist
2363 e687ec01 Michael Hanselmann
  config.set(constants.INISECT_INS, "nic_count", "%d" % nic_total)
2364 a8083063 Iustin Pop
2365 726d7d68 Iustin Pop
  disk_total = 0
2366 a8083063 Iustin Pop
  for disk_count, disk in enumerate(snap_disks):
2367 19d7f90a Guido Trotter
    if disk:
2368 726d7d68 Iustin Pop
      disk_total += 1
2369 d0c8c01d Iustin Pop
      config.set(constants.INISECT_INS, "disk%d_ivname" % disk_count,
2370 d0c8c01d Iustin Pop
                 ("%s" % disk.iv_name))
2371 d0c8c01d Iustin Pop
      config.set(constants.INISECT_INS, "disk%d_dump" % disk_count,
2372 d0c8c01d Iustin Pop
                 ("%s" % disk.physical_id[1]))
2373 d0c8c01d Iustin Pop
      config.set(constants.INISECT_INS, "disk%d_size" % disk_count,
2374 d0c8c01d Iustin Pop
                 ("%d" % disk.size))
2375 d0c8c01d Iustin Pop
2376 e687ec01 Michael Hanselmann
  config.set(constants.INISECT_INS, "disk_count", "%d" % disk_total)
2377 a8083063 Iustin Pop
2378 3c8954ad Iustin Pop
  # New-style hypervisor/backend parameters
2379 3c8954ad Iustin Pop
2380 3c8954ad Iustin Pop
  config.add_section(constants.INISECT_HYP)
2381 3c8954ad Iustin Pop
  for name, value in instance.hvparams.items():
2382 3c8954ad Iustin Pop
    if name not in constants.HVC_GLOBALS:
2383 3c8954ad Iustin Pop
      config.set(constants.INISECT_HYP, name, str(value))
2384 3c8954ad Iustin Pop
2385 3c8954ad Iustin Pop
  config.add_section(constants.INISECT_BEP)
2386 3c8954ad Iustin Pop
  for name, value in instance.beparams.items():
2387 3c8954ad Iustin Pop
    config.set(constants.INISECT_BEP, name, str(value))
2388 3c8954ad Iustin Pop
2389 535b49cb Iustin Pop
  config.add_section(constants.INISECT_OSP)
2390 535b49cb Iustin Pop
  for name, value in instance.osparams.items():
2391 535b49cb Iustin Pop
    config.set(constants.INISECT_OSP, name, str(value))
2392 535b49cb Iustin Pop
2393 c4feafe8 Iustin Pop
  utils.WriteFile(utils.PathJoin(destdir, constants.EXPORT_CONF_FILE),
2394 726d7d68 Iustin Pop
                  data=config.Dumps())
2395 56569f4e Michael Hanselmann
  shutil.rmtree(finaldestdir, ignore_errors=True)
2396 a8083063 Iustin Pop
  shutil.move(destdir, finaldestdir)
2397 a8083063 Iustin Pop
2398 a8083063 Iustin Pop
2399 a8083063 Iustin Pop
def ExportInfo(dest):
2400 a8083063 Iustin Pop
  """Get export configuration information.
2401 a8083063 Iustin Pop

2402 10c2650b Iustin Pop
  @type dest: str
2403 10c2650b Iustin Pop
  @param dest: directory containing the export
2404 a8083063 Iustin Pop

2405 10c2650b Iustin Pop
  @rtype: L{objects.SerializableConfigParser}
2406 10c2650b Iustin Pop
  @return: a serializable config file containing the
2407 10c2650b Iustin Pop
      export info
2408 a8083063 Iustin Pop

2409 a8083063 Iustin Pop
  """
2410 c4feafe8 Iustin Pop
  cff = utils.PathJoin(dest, constants.EXPORT_CONF_FILE)
2411 a8083063 Iustin Pop
2412 a8083063 Iustin Pop
  config = objects.SerializableConfigParser()
2413 a8083063 Iustin Pop
  config.read(cff)
2414 a8083063 Iustin Pop
2415 a8083063 Iustin Pop
  if (not config.has_section(constants.INISECT_EXP) or
2416 a8083063 Iustin Pop
      not config.has_section(constants.INISECT_INS)):
2417 3eccac06 Iustin Pop
    _Fail("Export info file doesn't have the required fields")
2418 a8083063 Iustin Pop
2419 c26a6bd2 Iustin Pop
  return config.Dumps()
2420 a8083063 Iustin Pop
2421 a8083063 Iustin Pop
2422 a8083063 Iustin Pop
def ListExports():
2423 a8083063 Iustin Pop
  """Return a list of exports currently available on this machine.
2424 098c0958 Michael Hanselmann

2425 10c2650b Iustin Pop
  @rtype: list
2426 10c2650b Iustin Pop
  @return: list of the exports
2427 10c2650b Iustin Pop

2428 a8083063 Iustin Pop
  """
2429 a8083063 Iustin Pop
  if os.path.isdir(constants.EXPORT_DIR):
2430 b5b8309d Guido Trotter
    return sorted(utils.ListVisibleFiles(constants.EXPORT_DIR))
2431 a8083063 Iustin Pop
  else:
2432 afdc3985 Iustin Pop
    _Fail("No exports directory")
2433 a8083063 Iustin Pop
2434 a8083063 Iustin Pop
2435 a8083063 Iustin Pop
def RemoveExport(export):
2436 a8083063 Iustin Pop
  """Remove an existing export from the node.
2437 a8083063 Iustin Pop

2438 10c2650b Iustin Pop
  @type export: str
2439 10c2650b Iustin Pop
  @param export: the name of the export to remove
2440 c26a6bd2 Iustin Pop
  @rtype: None
2441 a8083063 Iustin Pop

2442 098c0958 Michael Hanselmann
  """
2443 c4feafe8 Iustin Pop
  target = utils.PathJoin(constants.EXPORT_DIR, export)
2444 a8083063 Iustin Pop
2445 35fbcd11 Iustin Pop
  try:
2446 35fbcd11 Iustin Pop
    shutil.rmtree(target)
2447 35fbcd11 Iustin Pop
  except EnvironmentError, err:
2448 35fbcd11 Iustin Pop
    _Fail("Error while removing the export: %s", err, exc=True)
2449 a8083063 Iustin Pop
2450 a8083063 Iustin Pop
2451 821d1bd1 Iustin Pop
def BlockdevRename(devlist):
2452 f3e513ad Iustin Pop
  """Rename a list of block devices.
2453 f3e513ad Iustin Pop

2454 10c2650b Iustin Pop
  @type devlist: list of tuples
2455 10c2650b Iustin Pop
  @param devlist: list of tuples of the form  (disk,
2456 10c2650b Iustin Pop
      new_logical_id, new_physical_id); disk is an
2457 10c2650b Iustin Pop
      L{objects.Disk} object describing the current disk,
2458 10c2650b Iustin Pop
      and new logical_id/physical_id is the name we
2459 10c2650b Iustin Pop
      rename it to
2460 10c2650b Iustin Pop
  @rtype: boolean
2461 10c2650b Iustin Pop
  @return: True if all renames succeeded, False otherwise
2462 f3e513ad Iustin Pop

2463 f3e513ad Iustin Pop
  """
2464 6b5e3f70 Iustin Pop
  msgs = []
2465 f3e513ad Iustin Pop
  result = True
2466 f3e513ad Iustin Pop
  for disk, unique_id in devlist:
2467 f3e513ad Iustin Pop
    dev = _RecursiveFindBD(disk)
2468 f3e513ad Iustin Pop
    if dev is None:
2469 6b5e3f70 Iustin Pop
      msgs.append("Can't find device %s in rename" % str(disk))
2470 f3e513ad Iustin Pop
      result = False
2471 f3e513ad Iustin Pop
      continue
2472 f3e513ad Iustin Pop
    try:
2473 3f78eef2 Iustin Pop
      old_rpath = dev.dev_path
2474 f3e513ad Iustin Pop
      dev.Rename(unique_id)
2475 3f78eef2 Iustin Pop
      new_rpath = dev.dev_path
2476 3f78eef2 Iustin Pop
      if old_rpath != new_rpath:
2477 3f78eef2 Iustin Pop
        DevCacheManager.RemoveCache(old_rpath)
2478 3f78eef2 Iustin Pop
        # FIXME: we should add the new cache information here, like:
2479 3f78eef2 Iustin Pop
        # DevCacheManager.UpdateCache(new_rpath, owner, ...)
2480 3f78eef2 Iustin Pop
        # but we don't have the owner here - maybe parse from existing
2481 3f78eef2 Iustin Pop
        # cache? for now, we only lose lvm data when we rename, which
2482 3f78eef2 Iustin Pop
        # is less critical than DRBD or MD
2483 f3e513ad Iustin Pop
    except errors.BlockDeviceError, err:
2484 6b5e3f70 Iustin Pop
      msgs.append("Can't rename device '%s' to '%s': %s" %
2485 6b5e3f70 Iustin Pop
                  (dev, unique_id, err))
2486 18682bca Iustin Pop
      logging.exception("Can't rename device '%s' to '%s'", dev, unique_id)
2487 f3e513ad Iustin Pop
      result = False
2488 afdc3985 Iustin Pop
  if not result:
2489 afdc3985 Iustin Pop
    _Fail("; ".join(msgs))
2490 f3e513ad Iustin Pop
2491 f3e513ad Iustin Pop
2492 4b97f902 Apollon Oikonomopoulos
def _TransformFileStorageDir(fs_dir):
2493 778b75bb Manuel Franceschini
  """Checks whether given file_storage_dir is valid.
2494 778b75bb Manuel Franceschini

2495 4b97f902 Apollon Oikonomopoulos
  Checks wheter the given fs_dir is within the cluster-wide default
2496 4b97f902 Apollon Oikonomopoulos
  file_storage_dir or the shared_file_storage_dir, which are stored in
2497 4b97f902 Apollon Oikonomopoulos
  SimpleStore. Only paths under those directories are allowed.
2498 778b75bb Manuel Franceschini

2499 4b97f902 Apollon Oikonomopoulos
  @type fs_dir: str
2500 4b97f902 Apollon Oikonomopoulos
  @param fs_dir: the path to check
2501 d61cbe76 Iustin Pop

2502 b1206984 Iustin Pop
  @return: the normalized path if valid, None otherwise
2503 778b75bb Manuel Franceschini

2504 778b75bb Manuel Franceschini
  """
2505 cb7c0198 Iustin Pop
  if not constants.ENABLE_FILE_STORAGE:
2506 cb7c0198 Iustin Pop
    _Fail("File storage disabled at configure time")
2507 c657dcc9 Michael Hanselmann
  cfg = _GetConfig()
2508 4b97f902 Apollon Oikonomopoulos
  fs_dir = os.path.normpath(fs_dir)
2509 4b97f902 Apollon Oikonomopoulos
  base_fstore = cfg.GetFileStorageDir()
2510 4b97f902 Apollon Oikonomopoulos
  base_shared = cfg.GetSharedFileStorageDir()
2511 cf00dba0 René Nussbaumer
  if not (utils.IsBelowDir(base_fstore, fs_dir) or
2512 cf00dba0 René Nussbaumer
          utils.IsBelowDir(base_shared, fs_dir)):
2513 b2b8bcce Iustin Pop
    _Fail("File storage directory '%s' is not under base file"
2514 4b97f902 Apollon Oikonomopoulos
          " storage directory '%s' or shared storage directory '%s'",
2515 4b97f902 Apollon Oikonomopoulos
          fs_dir, base_fstore, base_shared)
2516 4b97f902 Apollon Oikonomopoulos
  return fs_dir
2517 778b75bb Manuel Franceschini
2518 778b75bb Manuel Franceschini
2519 778b75bb Manuel Franceschini
def CreateFileStorageDir(file_storage_dir):
2520 778b75bb Manuel Franceschini
  """Create file storage directory.
2521 778b75bb Manuel Franceschini

2522 b1206984 Iustin Pop
  @type file_storage_dir: str
2523 b1206984 Iustin Pop
  @param file_storage_dir: directory to create
2524 778b75bb Manuel Franceschini

2525 b1206984 Iustin Pop
  @rtype: tuple
2526 b1206984 Iustin Pop
  @return: tuple with first element a boolean indicating wheter dir
2527 b1206984 Iustin Pop
      creation was successful or not
2528 778b75bb Manuel Franceschini

2529 778b75bb Manuel Franceschini
  """
2530 778b75bb Manuel Franceschini
  file_storage_dir = _TransformFileStorageDir(file_storage_dir)
2531 b2b8bcce Iustin Pop
  if os.path.exists(file_storage_dir):
2532 b2b8bcce Iustin Pop
    if not os.path.isdir(file_storage_dir):
2533 b2b8bcce Iustin Pop
      _Fail("Specified storage dir '%s' is not a directory",
2534 b2b8bcce Iustin Pop
            file_storage_dir)
2535 778b75bb Manuel Franceschini
  else:
2536 b2b8bcce Iustin Pop
    try:
2537 b2b8bcce Iustin Pop
      os.makedirs(file_storage_dir, 0750)
2538 b2b8bcce Iustin Pop
    except OSError, err:
2539 b2b8bcce Iustin Pop
      _Fail("Cannot create file storage directory '%s': %s",
2540 b2b8bcce Iustin Pop
            file_storage_dir, err, exc=True)
2541 778b75bb Manuel Franceschini
2542 778b75bb Manuel Franceschini
2543 778b75bb Manuel Franceschini
def RemoveFileStorageDir(file_storage_dir):
2544 778b75bb Manuel Franceschini
  """Remove file storage directory.
2545 778b75bb Manuel Franceschini

2546 778b75bb Manuel Franceschini
  Remove it only if it's empty. If not log an error and return.
2547 778b75bb Manuel Franceschini

2548 10c2650b Iustin Pop
  @type file_storage_dir: str
2549 10c2650b Iustin Pop
  @param file_storage_dir: the directory we should cleanup
2550 10c2650b Iustin Pop
  @rtype: tuple (success,)
2551 10c2650b Iustin Pop
  @return: tuple of one element, C{success}, denoting
2552 5bbd3f7f Michael Hanselmann
      whether the operation was successful
2553 778b75bb Manuel Franceschini

2554 778b75bb Manuel Franceschini
  """
2555 778b75bb Manuel Franceschini
  file_storage_dir = _TransformFileStorageDir(file_storage_dir)
2556 b2b8bcce Iustin Pop
  if os.path.exists(file_storage_dir):
2557 b2b8bcce Iustin Pop
    if not os.path.isdir(file_storage_dir):
2558 b2b8bcce Iustin Pop
      _Fail("Specified Storage directory '%s' is not a directory",
2559 b2b8bcce Iustin Pop
            file_storage_dir)
2560 afdc3985 Iustin Pop
    # deletes dir only if empty, otherwise we want to fail the rpc call
2561 b2b8bcce Iustin Pop
    try:
2562 b2b8bcce Iustin Pop
      os.rmdir(file_storage_dir)
2563 b2b8bcce Iustin Pop
    except OSError, err:
2564 b2b8bcce Iustin Pop
      _Fail("Cannot remove file storage directory '%s': %s",
2565 b2b8bcce Iustin Pop
            file_storage_dir, err)
2566 b2b8bcce Iustin Pop
2567 778b75bb Manuel Franceschini
2568 778b75bb Manuel Franceschini
def RenameFileStorageDir(old_file_storage_dir, new_file_storage_dir):
2569 778b75bb Manuel Franceschini
  """Rename the file storage directory.
2570 778b75bb Manuel Franceschini

2571 10c2650b Iustin Pop
  @type old_file_storage_dir: str
2572 10c2650b Iustin Pop
  @param old_file_storage_dir: the current path
2573 10c2650b Iustin Pop
  @type new_file_storage_dir: str
2574 10c2650b Iustin Pop
  @param new_file_storage_dir: the name we should rename to
2575 10c2650b Iustin Pop
  @rtype: tuple (success,)
2576 10c2650b Iustin Pop
  @return: tuple of one element, C{success}, denoting
2577 10c2650b Iustin Pop
      whether the operation was successful
2578 778b75bb Manuel Franceschini

2579 778b75bb Manuel Franceschini
  """
2580 778b75bb Manuel Franceschini
  old_file_storage_dir = _TransformFileStorageDir(old_file_storage_dir)
2581 778b75bb Manuel Franceschini
  new_file_storage_dir = _TransformFileStorageDir(new_file_storage_dir)
2582 b2b8bcce Iustin Pop
  if not os.path.exists(new_file_storage_dir):
2583 b2b8bcce Iustin Pop
    if os.path.isdir(old_file_storage_dir):
2584 b2b8bcce Iustin Pop
      try:
2585 b2b8bcce Iustin Pop
        os.rename(old_file_storage_dir, new_file_storage_dir)
2586 b2b8bcce Iustin Pop
      except OSError, err:
2587 b2b8bcce Iustin Pop
        _Fail("Cannot rename '%s' to '%s': %s",
2588 b2b8bcce Iustin Pop
              old_file_storage_dir, new_file_storage_dir, err)
2589 778b75bb Manuel Franceschini
    else:
2590 b2b8bcce Iustin Pop
      _Fail("Specified storage dir '%s' is not a directory",
2591 b2b8bcce Iustin Pop
            old_file_storage_dir)
2592 b2b8bcce Iustin Pop
  else:
2593 b2b8bcce Iustin Pop
    if os.path.exists(old_file_storage_dir):
2594 b2b8bcce Iustin Pop
      _Fail("Cannot rename '%s' to '%s': both locations exist",
2595 b2b8bcce Iustin Pop
            old_file_storage_dir, new_file_storage_dir)
2596 778b75bb Manuel Franceschini
2597 778b75bb Manuel Franceschini
2598 c8457ce7 Iustin Pop
def _EnsureJobQueueFile(file_name):
2599 dc31eae3 Michael Hanselmann
  """Checks whether the given filename is in the queue directory.
2600 ca52cdeb Michael Hanselmann

2601 10c2650b Iustin Pop
  @type file_name: str
2602 10c2650b Iustin Pop
  @param file_name: the file name we should check
2603 c8457ce7 Iustin Pop
  @rtype: None
2604 c8457ce7 Iustin Pop
  @raises RPCFail: if the file is not valid
2605 10c2650b Iustin Pop

2606 ca52cdeb Michael Hanselmann
  """
2607 ca52cdeb Michael Hanselmann
  queue_dir = os.path.normpath(constants.QUEUE_DIR)
2608 dc31eae3 Michael Hanselmann
  result = (os.path.commonprefix([queue_dir, file_name]) == queue_dir)
2609 dc31eae3 Michael Hanselmann
2610 dc31eae3 Michael Hanselmann
  if not result:
2611 c8457ce7 Iustin Pop
    _Fail("Passed job queue file '%s' does not belong to"
2612 c8457ce7 Iustin Pop
          " the queue directory '%s'", file_name, queue_dir)
2613 dc31eae3 Michael Hanselmann
2614 dc31eae3 Michael Hanselmann
2615 dc31eae3 Michael Hanselmann
def JobQueueUpdate(file_name, content):
2616 dc31eae3 Michael Hanselmann
  """Updates a file in the queue directory.
2617 dc31eae3 Michael Hanselmann

2618 3865ca48 Michael Hanselmann
  This is just a wrapper over L{utils.io.WriteFile}, with proper
2619 10c2650b Iustin Pop
  checking.
2620 10c2650b Iustin Pop

2621 10c2650b Iustin Pop
  @type file_name: str
2622 10c2650b Iustin Pop
  @param file_name: the job file name
2623 10c2650b Iustin Pop
  @type content: str
2624 10c2650b Iustin Pop
  @param content: the new job contents
2625 10c2650b Iustin Pop
  @rtype: boolean
2626 10c2650b Iustin Pop
  @return: the success of the operation
2627 10c2650b Iustin Pop

2628 dc31eae3 Michael Hanselmann
  """
2629 c8457ce7 Iustin Pop
  _EnsureJobQueueFile(file_name)
2630 82b22e19 René Nussbaumer
  getents = runtime.GetEnts()
2631 ca52cdeb Michael Hanselmann
2632 ca52cdeb Michael Hanselmann
  # Write and replace the file atomically
2633 82b22e19 René Nussbaumer
  utils.WriteFile(file_name, data=_Decompress(content), uid=getents.masterd_uid,
2634 82b22e19 René Nussbaumer
                  gid=getents.masterd_gid)
2635 ca52cdeb Michael Hanselmann
2636 ca52cdeb Michael Hanselmann
2637 af5ebcb1 Michael Hanselmann
def JobQueueRename(old, new):
2638 af5ebcb1 Michael Hanselmann
  """Renames a job queue file.
2639 af5ebcb1 Michael Hanselmann

2640 c41eea6e Iustin Pop
  This is just a wrapper over os.rename with proper checking.
2641 10c2650b Iustin Pop

2642 10c2650b Iustin Pop
  @type old: str
2643 10c2650b Iustin Pop
  @param old: the old (actual) file name
2644 10c2650b Iustin Pop
  @type new: str
2645 10c2650b Iustin Pop
  @param new: the desired file name
2646 c8457ce7 Iustin Pop
  @rtype: tuple
2647 c8457ce7 Iustin Pop
  @return: the success of the operation and payload
2648 10c2650b Iustin Pop

2649 af5ebcb1 Michael Hanselmann
  """
2650 c8457ce7 Iustin Pop
  _EnsureJobQueueFile(old)
2651 c8457ce7 Iustin Pop
  _EnsureJobQueueFile(new)
2652 af5ebcb1 Michael Hanselmann
2653 58b22b6e Michael Hanselmann
  utils.RenameFile(old, new, mkdir=True)
2654 af5ebcb1 Michael Hanselmann
2655 af5ebcb1 Michael Hanselmann
2656 821d1bd1 Iustin Pop
def BlockdevClose(instance_name, disks):
2657 d61cbe76 Iustin Pop
  """Closes the given block devices.
2658 d61cbe76 Iustin Pop

2659 10c2650b Iustin Pop
  This means they will be switched to secondary mode (in case of
2660 10c2650b Iustin Pop
  DRBD).
2661 10c2650b Iustin Pop

2662 b2e7666a Iustin Pop
  @param instance_name: if the argument is not empty, the symlinks
2663 b2e7666a Iustin Pop
      of this instance will be removed
2664 10c2650b Iustin Pop
  @type disks: list of L{objects.Disk}
2665 10c2650b Iustin Pop
  @param disks: the list of disks to be closed
2666 10c2650b Iustin Pop
  @rtype: tuple (success, message)
2667 10c2650b Iustin Pop
  @return: a tuple of success and message, where success
2668 10c2650b Iustin Pop
      indicates the succes of the operation, and message
2669 10c2650b Iustin Pop
      which will contain the error details in case we
2670 10c2650b Iustin Pop
      failed
2671 d61cbe76 Iustin Pop

2672 d61cbe76 Iustin Pop
  """
2673 d61cbe76 Iustin Pop
  bdevs = []
2674 d61cbe76 Iustin Pop
  for cf in disks:
2675 d61cbe76 Iustin Pop
    rd = _RecursiveFindBD(cf)
2676 d61cbe76 Iustin Pop
    if rd is None:
2677 2cc6781a Iustin Pop
      _Fail("Can't find device %s", cf)
2678 d61cbe76 Iustin Pop
    bdevs.append(rd)
2679 d61cbe76 Iustin Pop
2680 d61cbe76 Iustin Pop
  msg = []
2681 d61cbe76 Iustin Pop
  for rd in bdevs:
2682 d61cbe76 Iustin Pop
    try:
2683 d61cbe76 Iustin Pop
      rd.Close()
2684 d61cbe76 Iustin Pop
    except errors.BlockDeviceError, err:
2685 d61cbe76 Iustin Pop
      msg.append(str(err))
2686 d61cbe76 Iustin Pop
  if msg:
2687 afdc3985 Iustin Pop
    _Fail("Can't make devices secondary: %s", ",".join(msg))
2688 d61cbe76 Iustin Pop
  else:
2689 b2e7666a Iustin Pop
    if instance_name:
2690 5282084b Iustin Pop
      _RemoveBlockDevLinks(instance_name, disks)
2691 d61cbe76 Iustin Pop
2692 d61cbe76 Iustin Pop
2693 6217e295 Iustin Pop
def ValidateHVParams(hvname, hvparams):
2694 6217e295 Iustin Pop
  """Validates the given hypervisor parameters.
2695 6217e295 Iustin Pop

2696 6217e295 Iustin Pop
  @type hvname: string
2697 6217e295 Iustin Pop
  @param hvname: the hypervisor name
2698 6217e295 Iustin Pop
  @type hvparams: dict
2699 6217e295 Iustin Pop
  @param hvparams: the hypervisor parameters to be validated
2700 c26a6bd2 Iustin Pop
  @rtype: None
2701 6217e295 Iustin Pop

2702 6217e295 Iustin Pop
  """
2703 6217e295 Iustin Pop
  try:
2704 6217e295 Iustin Pop
    hv_type = hypervisor.GetHypervisor(hvname)
2705 6217e295 Iustin Pop
    hv_type.ValidateParameters(hvparams)
2706 6217e295 Iustin Pop
  except errors.HypervisorError, err:
2707 afdc3985 Iustin Pop
    _Fail(str(err), log=False)
2708 6217e295 Iustin Pop
2709 6217e295 Iustin Pop
2710 acd9ff9e Iustin Pop
def _CheckOSPList(os_obj, parameters):
2711 acd9ff9e Iustin Pop
  """Check whether a list of parameters is supported by the OS.
2712 acd9ff9e Iustin Pop

2713 acd9ff9e Iustin Pop
  @type os_obj: L{objects.OS}
2714 acd9ff9e Iustin Pop
  @param os_obj: OS object to check
2715 acd9ff9e Iustin Pop
  @type parameters: list
2716 acd9ff9e Iustin Pop
  @param parameters: the list of parameters to check
2717 acd9ff9e Iustin Pop

2718 acd9ff9e Iustin Pop
  """
2719 acd9ff9e Iustin Pop
  supported = [v[0] for v in os_obj.supported_parameters]
2720 acd9ff9e Iustin Pop
  delta = frozenset(parameters).difference(supported)
2721 acd9ff9e Iustin Pop
  if delta:
2722 acd9ff9e Iustin Pop
    _Fail("The following parameters are not supported"
2723 acd9ff9e Iustin Pop
          " by the OS %s: %s" % (os_obj.name, utils.CommaJoin(delta)))
2724 acd9ff9e Iustin Pop
2725 acd9ff9e Iustin Pop
2726 acd9ff9e Iustin Pop
def ValidateOS(required, osname, checks, osparams):
2727 acd9ff9e Iustin Pop
  """Validate the given OS' parameters.
2728 acd9ff9e Iustin Pop

2729 acd9ff9e Iustin Pop
  @type required: boolean
2730 acd9ff9e Iustin Pop
  @param required: whether absence of the OS should translate into
2731 acd9ff9e Iustin Pop
      failure or not
2732 acd9ff9e Iustin Pop
  @type osname: string
2733 acd9ff9e Iustin Pop
  @param osname: the OS to be validated
2734 acd9ff9e Iustin Pop
  @type checks: list
2735 acd9ff9e Iustin Pop
  @param checks: list of the checks to run (currently only 'parameters')
2736 acd9ff9e Iustin Pop
  @type osparams: dict
2737 acd9ff9e Iustin Pop
  @param osparams: dictionary with OS parameters
2738 acd9ff9e Iustin Pop
  @rtype: boolean
2739 acd9ff9e Iustin Pop
  @return: True if the validation passed, or False if the OS was not
2740 acd9ff9e Iustin Pop
      found and L{required} was false
2741 acd9ff9e Iustin Pop

2742 acd9ff9e Iustin Pop
  """
2743 acd9ff9e Iustin Pop
  if not constants.OS_VALIDATE_CALLS.issuperset(checks):
2744 acd9ff9e Iustin Pop
    _Fail("Unknown checks required for OS %s: %s", osname,
2745 acd9ff9e Iustin Pop
          set(checks).difference(constants.OS_VALIDATE_CALLS))
2746 acd9ff9e Iustin Pop
2747 870dc44c Iustin Pop
  name_only = objects.OS.GetName(osname)
2748 acd9ff9e Iustin Pop
  status, tbv = _TryOSFromDisk(name_only, None)
2749 acd9ff9e Iustin Pop
2750 acd9ff9e Iustin Pop
  if not status:
2751 acd9ff9e Iustin Pop
    if required:
2752 acd9ff9e Iustin Pop
      _Fail(tbv)
2753 acd9ff9e Iustin Pop
    else:
2754 acd9ff9e Iustin Pop
      return False
2755 acd9ff9e Iustin Pop
2756 72db3fd7 Iustin Pop
  if max(tbv.api_versions) < constants.OS_API_V20:
2757 72db3fd7 Iustin Pop
    return True
2758 72db3fd7 Iustin Pop
2759 acd9ff9e Iustin Pop
  if constants.OS_VALIDATE_PARAMETERS in checks:
2760 acd9ff9e Iustin Pop
    _CheckOSPList(tbv, osparams.keys())
2761 acd9ff9e Iustin Pop
2762 a025e535 Vitaly Kuznetsov
  validate_env = OSCoreEnv(osname, tbv, osparams)
2763 acd9ff9e Iustin Pop
  result = utils.RunCmd([tbv.verify_script] + checks, env=validate_env,
2764 896a03f6 Iustin Pop
                        cwd=tbv.path, reset_env=True)
2765 acd9ff9e Iustin Pop
  if result.failed:
2766 acd9ff9e Iustin Pop
    logging.error("os validate command '%s' returned error: %s output: %s",
2767 acd9ff9e Iustin Pop
                  result.cmd, result.fail_reason, result.output)
2768 acd9ff9e Iustin Pop
    _Fail("OS validation script failed (%s), output: %s",
2769 acd9ff9e Iustin Pop
          result.fail_reason, result.output, log=False)
2770 acd9ff9e Iustin Pop
2771 acd9ff9e Iustin Pop
  return True
2772 acd9ff9e Iustin Pop
2773 acd9ff9e Iustin Pop
2774 56aa9fd5 Iustin Pop
def DemoteFromMC():
2775 56aa9fd5 Iustin Pop
  """Demotes the current node from master candidate role.
2776 56aa9fd5 Iustin Pop

2777 56aa9fd5 Iustin Pop
  """
2778 56aa9fd5 Iustin Pop
  # try to ensure we're not the master by mistake
2779 56aa9fd5 Iustin Pop
  master, myself = ssconf.GetMasterAndMyself()
2780 56aa9fd5 Iustin Pop
  if master == myself:
2781 afdc3985 Iustin Pop
    _Fail("ssconf status shows I'm the master node, will not demote")
2782 f154a7a3 Michael Hanselmann
2783 f154a7a3 Michael Hanselmann
  result = utils.RunCmd([constants.DAEMON_UTIL, "check", constants.MASTERD])
2784 f154a7a3 Michael Hanselmann
  if not result.failed:
2785 afdc3985 Iustin Pop
    _Fail("The master daemon is running, will not demote")
2786 f154a7a3 Michael Hanselmann
2787 56aa9fd5 Iustin Pop
  try:
2788 9a5cb537 Iustin Pop
    if os.path.isfile(constants.CLUSTER_CONF_FILE):
2789 9a5cb537 Iustin Pop
      utils.CreateBackup(constants.CLUSTER_CONF_FILE)
2790 56aa9fd5 Iustin Pop
  except EnvironmentError, err:
2791 56aa9fd5 Iustin Pop
    if err.errno != errno.ENOENT:
2792 afdc3985 Iustin Pop
      _Fail("Error while backing up cluster file: %s", err, exc=True)
2793 f154a7a3 Michael Hanselmann
2794 56aa9fd5 Iustin Pop
  utils.RemoveFile(constants.CLUSTER_CONF_FILE)
2795 56aa9fd5 Iustin Pop
2796 56aa9fd5 Iustin Pop
2797 f942a838 Michael Hanselmann
def _GetX509Filenames(cryptodir, name):
2798 f942a838 Michael Hanselmann
  """Returns the full paths for the private key and certificate.
2799 f942a838 Michael Hanselmann

2800 f942a838 Michael Hanselmann
  """
2801 f942a838 Michael Hanselmann
  return (utils.PathJoin(cryptodir, name),
2802 f942a838 Michael Hanselmann
          utils.PathJoin(cryptodir, name, _X509_KEY_FILE),
2803 f942a838 Michael Hanselmann
          utils.PathJoin(cryptodir, name, _X509_CERT_FILE))
2804 f942a838 Michael Hanselmann
2805 f942a838 Michael Hanselmann
2806 f942a838 Michael Hanselmann
def CreateX509Certificate(validity, cryptodir=constants.CRYPTO_KEYS_DIR):
2807 f942a838 Michael Hanselmann
  """Creates a new X509 certificate for SSL/TLS.
2808 f942a838 Michael Hanselmann

2809 f942a838 Michael Hanselmann
  @type validity: int
2810 f942a838 Michael Hanselmann
  @param validity: Validity in seconds
2811 f942a838 Michael Hanselmann
  @rtype: tuple; (string, string)
2812 f942a838 Michael Hanselmann
  @return: Certificate name and public part
2813 f942a838 Michael Hanselmann

2814 f942a838 Michael Hanselmann
  """
2815 f942a838 Michael Hanselmann
  (key_pem, cert_pem) = \
2816 b705c7a6 Manuel Franceschini
    utils.GenerateSelfSignedX509Cert(netutils.Hostname.GetSysName(),
2817 f942a838 Michael Hanselmann
                                     min(validity, _MAX_SSL_CERT_VALIDITY))
2818 f942a838 Michael Hanselmann
2819 f942a838 Michael Hanselmann
  cert_dir = tempfile.mkdtemp(dir=cryptodir,
2820 f942a838 Michael Hanselmann
                              prefix="x509-%s-" % utils.TimestampForFilename())
2821 f942a838 Michael Hanselmann
  try:
2822 f942a838 Michael Hanselmann
    name = os.path.basename(cert_dir)
2823 f942a838 Michael Hanselmann
    assert len(name) > 5
2824 f942a838 Michael Hanselmann
2825 f942a838 Michael Hanselmann
    (_, key_file, cert_file) = _GetX509Filenames(cryptodir, name)
2826 f942a838 Michael Hanselmann
2827 f942a838 Michael Hanselmann
    utils.WriteFile(key_file, mode=0400, data=key_pem)
2828 f942a838 Michael Hanselmann
    utils.WriteFile(cert_file, mode=0400, data=cert_pem)
2829 f942a838 Michael Hanselmann
2830 f942a838 Michael Hanselmann
    # Never return private key as it shouldn't leave the node
2831 f942a838 Michael Hanselmann
    return (name, cert_pem)
2832 f942a838 Michael Hanselmann
  except Exception:
2833 f942a838 Michael Hanselmann
    shutil.rmtree(cert_dir, ignore_errors=True)
2834 f942a838 Michael Hanselmann
    raise
2835 f942a838 Michael Hanselmann
2836 f942a838 Michael Hanselmann
2837 f942a838 Michael Hanselmann
def RemoveX509Certificate(name, cryptodir=constants.CRYPTO_KEYS_DIR):
2838 f942a838 Michael Hanselmann
  """Removes a X509 certificate.
2839 f942a838 Michael Hanselmann

2840 f942a838 Michael Hanselmann
  @type name: string
2841 f942a838 Michael Hanselmann
  @param name: Certificate name
2842 f942a838 Michael Hanselmann

2843 f942a838 Michael Hanselmann
  """
2844 f942a838 Michael Hanselmann
  (cert_dir, key_file, cert_file) = _GetX509Filenames(cryptodir, name)
2845 f942a838 Michael Hanselmann
2846 f942a838 Michael Hanselmann
  utils.RemoveFile(key_file)
2847 f942a838 Michael Hanselmann
  utils.RemoveFile(cert_file)
2848 f942a838 Michael Hanselmann
2849 f942a838 Michael Hanselmann
  try:
2850 f942a838 Michael Hanselmann
    os.rmdir(cert_dir)
2851 f942a838 Michael Hanselmann
  except EnvironmentError, err:
2852 f942a838 Michael Hanselmann
    _Fail("Cannot remove certificate directory '%s': %s",
2853 f942a838 Michael Hanselmann
          cert_dir, err)
2854 f942a838 Michael Hanselmann
2855 f942a838 Michael Hanselmann
2856 1651d116 Michael Hanselmann
def _GetImportExportIoCommand(instance, mode, ieio, ieargs):
2857 1651d116 Michael Hanselmann
  """Returns the command for the requested input/output.
2858 1651d116 Michael Hanselmann

2859 1651d116 Michael Hanselmann
  @type instance: L{objects.Instance}
2860 1651d116 Michael Hanselmann
  @param instance: The instance object
2861 1651d116 Michael Hanselmann
  @param mode: Import/export mode
2862 1651d116 Michael Hanselmann
  @param ieio: Input/output type
2863 1651d116 Michael Hanselmann
  @param ieargs: Input/output arguments
2864 1651d116 Michael Hanselmann

2865 1651d116 Michael Hanselmann
  """
2866 1651d116 Michael Hanselmann
  assert mode in (constants.IEM_IMPORT, constants.IEM_EXPORT)
2867 1651d116 Michael Hanselmann
2868 1651d116 Michael Hanselmann
  env = None
2869 1651d116 Michael Hanselmann
  prefix = None
2870 1651d116 Michael Hanselmann
  suffix = None
2871 2ad5550d Michael Hanselmann
  exp_size = None
2872 1651d116 Michael Hanselmann
2873 1651d116 Michael Hanselmann
  if ieio == constants.IEIO_FILE:
2874 1651d116 Michael Hanselmann
    (filename, ) = ieargs
2875 1651d116 Michael Hanselmann
2876 1651d116 Michael Hanselmann
    if not utils.IsNormAbsPath(filename):
2877 1651d116 Michael Hanselmann
      _Fail("Path '%s' is not normalized or absolute", filename)
2878 1651d116 Michael Hanselmann
2879 748c9884 René Nussbaumer
    real_filename = os.path.realpath(filename)
2880 748c9884 René Nussbaumer
    directory = os.path.dirname(real_filename)
2881 1651d116 Michael Hanselmann
2882 945859e0 René Nussbaumer
    if not utils.IsBelowDir(constants.EXPORT_DIR, real_filename):
2883 748c9884 René Nussbaumer
      _Fail("File '%s' is not under exports directory '%s': %s",
2884 748c9884 René Nussbaumer
            filename, constants.EXPORT_DIR, real_filename)
2885 1651d116 Michael Hanselmann
2886 1651d116 Michael Hanselmann
    # Create directory
2887 1651d116 Michael Hanselmann
    utils.Makedirs(directory, mode=0750)
2888 1651d116 Michael Hanselmann
2889 1651d116 Michael Hanselmann
    quoted_filename = utils.ShellQuote(filename)
2890 1651d116 Michael Hanselmann
2891 1651d116 Michael Hanselmann
    if mode == constants.IEM_IMPORT:
2892 1651d116 Michael Hanselmann
      suffix = "> %s" % quoted_filename
2893 1651d116 Michael Hanselmann
    elif mode == constants.IEM_EXPORT:
2894 1651d116 Michael Hanselmann
      suffix = "< %s" % quoted_filename
2895 1651d116 Michael Hanselmann
2896 2ad5550d Michael Hanselmann
      # Retrieve file size
2897 2ad5550d Michael Hanselmann
      try:
2898 2ad5550d Michael Hanselmann
        st = os.stat(filename)
2899 2ad5550d Michael Hanselmann
      except EnvironmentError, err:
2900 2ad5550d Michael Hanselmann
        logging.error("Can't stat(2) %s: %s", filename, err)
2901 2ad5550d Michael Hanselmann
      else:
2902 2ad5550d Michael Hanselmann
        exp_size = utils.BytesToMebibyte(st.st_size)
2903 2ad5550d Michael Hanselmann
2904 1651d116 Michael Hanselmann
  elif ieio == constants.IEIO_RAW_DISK:
2905 1651d116 Michael Hanselmann
    (disk, ) = ieargs
2906 1651d116 Michael Hanselmann
2907 1651d116 Michael Hanselmann
    real_disk = _OpenRealBD(disk)
2908 1651d116 Michael Hanselmann
2909 1651d116 Michael Hanselmann
    if mode == constants.IEM_IMPORT:
2910 1651d116 Michael Hanselmann
      # we set here a smaller block size as, due to transport buffering, more
2911 1651d116 Michael Hanselmann
      # than 64-128k will mostly ignored; we use nocreat to fail if the device
2912 1651d116 Michael Hanselmann
      # is not already there or we pass a wrong path; we use notrunc to no
2913 1651d116 Michael Hanselmann
      # attempt truncate on an LV device; we use oflag=dsync to not buffer too
2914 1651d116 Michael Hanselmann
      # much memory; this means that at best, we flush every 64k, which will
2915 1651d116 Michael Hanselmann
      # not be very fast
2916 1651d116 Michael Hanselmann
      suffix = utils.BuildShellCmd(("| dd of=%s conv=nocreat,notrunc"
2917 1651d116 Michael Hanselmann
                                    " bs=%s oflag=dsync"),
2918 1651d116 Michael Hanselmann
                                    real_disk.dev_path,
2919 1651d116 Michael Hanselmann
                                    str(64 * 1024))
2920 1651d116 Michael Hanselmann
2921 1651d116 Michael Hanselmann
    elif mode == constants.IEM_EXPORT:
2922 1651d116 Michael Hanselmann
      # the block size on the read dd is 1MiB to match our units
2923 1651d116 Michael Hanselmann
      prefix = utils.BuildShellCmd("dd if=%s bs=%s count=%s |",
2924 1651d116 Michael Hanselmann
                                   real_disk.dev_path,
2925 1651d116 Michael Hanselmann
                                   str(1024 * 1024), # 1 MB
2926 1651d116 Michael Hanselmann
                                   str(disk.size))
2927 2ad5550d Michael Hanselmann
      exp_size = disk.size
2928 1651d116 Michael Hanselmann
2929 1651d116 Michael Hanselmann
  elif ieio == constants.IEIO_SCRIPT:
2930 1651d116 Michael Hanselmann
    (disk, disk_index, ) = ieargs
2931 1651d116 Michael Hanselmann
2932 1651d116 Michael Hanselmann
    assert isinstance(disk_index, (int, long))
2933 1651d116 Michael Hanselmann
2934 1651d116 Michael Hanselmann
    real_disk = _OpenRealBD(disk)
2935 1651d116 Michael Hanselmann
2936 1651d116 Michael Hanselmann
    inst_os = OSFromDisk(instance.os)
2937 1651d116 Michael Hanselmann
    env = OSEnvironment(instance, inst_os)
2938 1651d116 Michael Hanselmann
2939 1651d116 Michael Hanselmann
    if mode == constants.IEM_IMPORT:
2940 1651d116 Michael Hanselmann
      env["IMPORT_DEVICE"] = env["DISK_%d_PATH" % disk_index]
2941 1651d116 Michael Hanselmann
      env["IMPORT_INDEX"] = str(disk_index)
2942 1651d116 Michael Hanselmann
      script = inst_os.import_script
2943 1651d116 Michael Hanselmann
2944 1651d116 Michael Hanselmann
    elif mode == constants.IEM_EXPORT:
2945 1651d116 Michael Hanselmann
      env["EXPORT_DEVICE"] = real_disk.dev_path
2946 1651d116 Michael Hanselmann
      env["EXPORT_INDEX"] = str(disk_index)
2947 1651d116 Michael Hanselmann
      script = inst_os.export_script
2948 1651d116 Michael Hanselmann
2949 1651d116 Michael Hanselmann
    # TODO: Pass special environment only to script
2950 1651d116 Michael Hanselmann
    script_cmd = utils.BuildShellCmd("( cd %s && %s; )", inst_os.path, script)
2951 1651d116 Michael Hanselmann
2952 1651d116 Michael Hanselmann
    if mode == constants.IEM_IMPORT:
2953 1651d116 Michael Hanselmann
      suffix = "| %s" % script_cmd
2954 1651d116 Michael Hanselmann
2955 1651d116 Michael Hanselmann
    elif mode == constants.IEM_EXPORT:
2956 1651d116 Michael Hanselmann
      prefix = "%s |" % script_cmd
2957 1651d116 Michael Hanselmann
2958 2ad5550d Michael Hanselmann
    # Let script predict size
2959 2ad5550d Michael Hanselmann
    exp_size = constants.IE_CUSTOM_SIZE
2960 2ad5550d Michael Hanselmann
2961 1651d116 Michael Hanselmann
  else:
2962 1651d116 Michael Hanselmann
    _Fail("Invalid %s I/O mode %r", mode, ieio)
2963 1651d116 Michael Hanselmann
2964 2ad5550d Michael Hanselmann
  return (env, prefix, suffix, exp_size)
2965 1651d116 Michael Hanselmann
2966 1651d116 Michael Hanselmann
2967 1651d116 Michael Hanselmann
def _CreateImportExportStatusDir(prefix):
2968 1651d116 Michael Hanselmann
  """Creates status directory for import/export.
2969 1651d116 Michael Hanselmann

2970 1651d116 Michael Hanselmann
  """
2971 1651d116 Michael Hanselmann
  return tempfile.mkdtemp(dir=constants.IMPORT_EXPORT_DIR,
2972 1651d116 Michael Hanselmann
                          prefix=("%s-%s-" %
2973 1651d116 Michael Hanselmann
                                  (prefix, utils.TimestampForFilename())))
2974 1651d116 Michael Hanselmann
2975 1651d116 Michael Hanselmann
2976 6613661a Iustin Pop
def StartImportExportDaemon(mode, opts, host, port, instance, component,
2977 6613661a Iustin Pop
                            ieio, ieioargs):
2978 1651d116 Michael Hanselmann
  """Starts an import or export daemon.
2979 1651d116 Michael Hanselmann

2980 1651d116 Michael Hanselmann
  @param mode: Import/output mode
2981 eb630f50 Michael Hanselmann
  @type opts: L{objects.ImportExportOptions}
2982 eb630f50 Michael Hanselmann
  @param opts: Daemon options
2983 1651d116 Michael Hanselmann
  @type host: string
2984 1651d116 Michael Hanselmann
  @param host: Remote host for export (None for import)
2985 1651d116 Michael Hanselmann
  @type port: int
2986 1651d116 Michael Hanselmann
  @param port: Remote port for export (None for import)
2987 1651d116 Michael Hanselmann
  @type instance: L{objects.Instance}
2988 1651d116 Michael Hanselmann
  @param instance: Instance object
2989 6613661a Iustin Pop
  @type component: string
2990 6613661a Iustin Pop
  @param component: which part of the instance is transferred now,
2991 6613661a Iustin Pop
      e.g. 'disk/0'
2992 1651d116 Michael Hanselmann
  @param ieio: Input/output type
2993 1651d116 Michael Hanselmann
  @param ieioargs: Input/output arguments
2994 1651d116 Michael Hanselmann

2995 1651d116 Michael Hanselmann
  """
2996 1651d116 Michael Hanselmann
  if mode == constants.IEM_IMPORT:
2997 1651d116 Michael Hanselmann
    prefix = "import"
2998 1651d116 Michael Hanselmann
2999 1651d116 Michael Hanselmann
    if not (host is None and port is None):
3000 1651d116 Michael Hanselmann
      _Fail("Can not specify host or port on import")
3001 1651d116 Michael Hanselmann
3002 1651d116 Michael Hanselmann
  elif mode == constants.IEM_EXPORT:
3003 1651d116 Michael Hanselmann
    prefix = "export"
3004 1651d116 Michael Hanselmann
3005 1651d116 Michael Hanselmann
    if host is None or port is None:
3006 1651d116 Michael Hanselmann
      _Fail("Host and port must be specified for an export")
3007 1651d116 Michael Hanselmann
3008 1651d116 Michael Hanselmann
  else:
3009 1651d116 Michael Hanselmann
    _Fail("Invalid mode %r", mode)
3010 1651d116 Michael Hanselmann
3011 eb630f50 Michael Hanselmann
  if (opts.key_name is None) ^ (opts.ca_pem is None):
3012 1651d116 Michael Hanselmann
    _Fail("Cluster certificate can only be used for both key and CA")
3013 1651d116 Michael Hanselmann
3014 2ad5550d Michael Hanselmann
  (cmd_env, cmd_prefix, cmd_suffix, exp_size) = \
3015 1651d116 Michael Hanselmann
    _GetImportExportIoCommand(instance, mode, ieio, ieioargs)
3016 1651d116 Michael Hanselmann
3017 eb630f50 Michael Hanselmann
  if opts.key_name is None:
3018 1651d116 Michael Hanselmann
    # Use server.pem
3019 1651d116 Michael Hanselmann
    key_path = constants.NODED_CERT_FILE
3020 1651d116 Michael Hanselmann
    cert_path = constants.NODED_CERT_FILE
3021 eb630f50 Michael Hanselmann
    assert opts.ca_pem is None
3022 1651d116 Michael Hanselmann
  else:
3023 1651d116 Michael Hanselmann
    (_, key_path, cert_path) = _GetX509Filenames(constants.CRYPTO_KEYS_DIR,
3024 eb630f50 Michael Hanselmann
                                                 opts.key_name)
3025 eb630f50 Michael Hanselmann
    assert opts.ca_pem is not None
3026 1651d116 Michael Hanselmann
3027 63bcea2a Michael Hanselmann
  for i in [key_path, cert_path]:
3028 dcaabc4f Michael Hanselmann
    if not os.path.exists(i):
3029 63bcea2a Michael Hanselmann
      _Fail("File '%s' does not exist" % i)
3030 63bcea2a Michael Hanselmann
3031 6613661a Iustin Pop
  status_dir = _CreateImportExportStatusDir("%s-%s" % (prefix, component))
3032 1651d116 Michael Hanselmann
  try:
3033 1651d116 Michael Hanselmann
    status_file = utils.PathJoin(status_dir, _IES_STATUS_FILE)
3034 1651d116 Michael Hanselmann
    pid_file = utils.PathJoin(status_dir, _IES_PID_FILE)
3035 63bcea2a Michael Hanselmann
    ca_file = utils.PathJoin(status_dir, _IES_CA_FILE)
3036 1651d116 Michael Hanselmann
3037 eb630f50 Michael Hanselmann
    if opts.ca_pem is None:
3038 1651d116 Michael Hanselmann
      # Use server.pem
3039 63bcea2a Michael Hanselmann
      ca = utils.ReadFile(constants.NODED_CERT_FILE)
3040 eb630f50 Michael Hanselmann
    else:
3041 eb630f50 Michael Hanselmann
      ca = opts.ca_pem
3042 63bcea2a Michael Hanselmann
3043 eb630f50 Michael Hanselmann
    # Write CA file
3044 63bcea2a Michael Hanselmann
    utils.WriteFile(ca_file, data=ca, mode=0400)
3045 1651d116 Michael Hanselmann
3046 1651d116 Michael Hanselmann
    cmd = [
3047 1651d116 Michael Hanselmann
      constants.IMPORT_EXPORT_DAEMON,
3048 1651d116 Michael Hanselmann
      status_file, mode,
3049 1651d116 Michael Hanselmann
      "--key=%s" % key_path,
3050 1651d116 Michael Hanselmann
      "--cert=%s" % cert_path,
3051 63bcea2a Michael Hanselmann
      "--ca=%s" % ca_file,
3052 1651d116 Michael Hanselmann
      ]
3053 1651d116 Michael Hanselmann
3054 1651d116 Michael Hanselmann
    if host:
3055 1651d116 Michael Hanselmann
      cmd.append("--host=%s" % host)
3056 1651d116 Michael Hanselmann
3057 1651d116 Michael Hanselmann
    if port:
3058 1651d116 Michael Hanselmann
      cmd.append("--port=%s" % port)
3059 1651d116 Michael Hanselmann
3060 855d2fc7 Michael Hanselmann
    if opts.ipv6:
3061 855d2fc7 Michael Hanselmann
      cmd.append("--ipv6")
3062 855d2fc7 Michael Hanselmann
    else:
3063 855d2fc7 Michael Hanselmann
      cmd.append("--ipv4")
3064 855d2fc7 Michael Hanselmann
3065 a5310c2a Michael Hanselmann
    if opts.compress:
3066 a5310c2a Michael Hanselmann
      cmd.append("--compress=%s" % opts.compress)
3067 a5310c2a Michael Hanselmann
3068 af1d39b1 Michael Hanselmann
    if opts.magic:
3069 af1d39b1 Michael Hanselmann
      cmd.append("--magic=%s" % opts.magic)
3070 af1d39b1 Michael Hanselmann
3071 2ad5550d Michael Hanselmann
    if exp_size is not None:
3072 2ad5550d Michael Hanselmann
      cmd.append("--expected-size=%s" % exp_size)
3073 2ad5550d Michael Hanselmann
3074 1651d116 Michael Hanselmann
    if cmd_prefix:
3075 1651d116 Michael Hanselmann
      cmd.append("--cmd-prefix=%s" % cmd_prefix)
3076 1651d116 Michael Hanselmann
3077 1651d116 Michael Hanselmann
    if cmd_suffix:
3078 1651d116 Michael Hanselmann
      cmd.append("--cmd-suffix=%s" % cmd_suffix)
3079 1651d116 Michael Hanselmann
3080 4478301b Michael Hanselmann
    if mode == constants.IEM_EXPORT:
3081 4478301b Michael Hanselmann
      # Retry connection a few times when connecting to remote peer
3082 4478301b Michael Hanselmann
      cmd.append("--connect-retries=%s" % constants.RIE_CONNECT_RETRIES)
3083 4478301b Michael Hanselmann
      cmd.append("--connect-timeout=%s" % constants.RIE_CONNECT_ATTEMPT_TIMEOUT)
3084 4478301b Michael Hanselmann
    elif opts.connect_timeout is not None:
3085 4478301b Michael Hanselmann
      assert mode == constants.IEM_IMPORT
3086 4478301b Michael Hanselmann
      # Overall timeout for establishing connection while listening
3087 4478301b Michael Hanselmann
      cmd.append("--connect-timeout=%s" % opts.connect_timeout)
3088 4478301b Michael Hanselmann
3089 6aa7a354 Iustin Pop
    logfile = _InstanceLogName(prefix, instance.os, instance.name, component)
3090 1651d116 Michael Hanselmann
3091 1651d116 Michael Hanselmann
    # TODO: Once _InstanceLogName uses tempfile.mkstemp, StartDaemon has
3092 1651d116 Michael Hanselmann
    # support for receiving a file descriptor for output
3093 1651d116 Michael Hanselmann
    utils.StartDaemon(cmd, env=cmd_env, pidfile=pid_file,
3094 1651d116 Michael Hanselmann
                      output=logfile)
3095 1651d116 Michael Hanselmann
3096 1651d116 Michael Hanselmann
    # The import/export name is simply the status directory name
3097 1651d116 Michael Hanselmann
    return os.path.basename(status_dir)
3098 1651d116 Michael Hanselmann
3099 1651d116 Michael Hanselmann
  except Exception:
3100 1651d116 Michael Hanselmann
    shutil.rmtree(status_dir, ignore_errors=True)
3101 1651d116 Michael Hanselmann
    raise
3102 1651d116 Michael Hanselmann
3103 1651d116 Michael Hanselmann
3104 1651d116 Michael Hanselmann
def GetImportExportStatus(names):
3105 1651d116 Michael Hanselmann
  """Returns import/export daemon status.
3106 1651d116 Michael Hanselmann

3107 1651d116 Michael Hanselmann
  @type names: sequence
3108 1651d116 Michael Hanselmann
  @param names: List of names
3109 1651d116 Michael Hanselmann
  @rtype: List of dicts
3110 1651d116 Michael Hanselmann
  @return: Returns a list of the state of each named import/export or None if a
3111 1651d116 Michael Hanselmann
           status couldn't be read
3112 1651d116 Michael Hanselmann

3113 1651d116 Michael Hanselmann
  """
3114 1651d116 Michael Hanselmann
  result = []
3115 1651d116 Michael Hanselmann
3116 1651d116 Michael Hanselmann
  for name in names:
3117 1651d116 Michael Hanselmann
    status_file = utils.PathJoin(constants.IMPORT_EXPORT_DIR, name,
3118 1651d116 Michael Hanselmann
                                 _IES_STATUS_FILE)
3119 1651d116 Michael Hanselmann
3120 1651d116 Michael Hanselmann
    try:
3121 1651d116 Michael Hanselmann
      data = utils.ReadFile(status_file)
3122 1651d116 Michael Hanselmann
    except EnvironmentError, err:
3123 1651d116 Michael Hanselmann
      if err.errno != errno.ENOENT:
3124 1651d116 Michael Hanselmann
        raise
3125 1651d116 Michael Hanselmann
      data = None
3126 1651d116 Michael Hanselmann
3127 1651d116 Michael Hanselmann
    if not data:
3128 1651d116 Michael Hanselmann
      result.append(None)
3129 1651d116 Michael Hanselmann
      continue
3130 1651d116 Michael Hanselmann
3131 1651d116 Michael Hanselmann
    result.append(serializer.LoadJson(data))
3132 1651d116 Michael Hanselmann
3133 1651d116 Michael Hanselmann
  return result
3134 1651d116 Michael Hanselmann
3135 1651d116 Michael Hanselmann
3136 f81c4737 Michael Hanselmann
def AbortImportExport(name):
3137 f81c4737 Michael Hanselmann
  """Sends SIGTERM to a running import/export daemon.
3138 f81c4737 Michael Hanselmann

3139 f81c4737 Michael Hanselmann
  """
3140 f81c4737 Michael Hanselmann
  logging.info("Abort import/export %s", name)
3141 f81c4737 Michael Hanselmann
3142 f81c4737 Michael Hanselmann
  status_dir = utils.PathJoin(constants.IMPORT_EXPORT_DIR, name)
3143 f81c4737 Michael Hanselmann
  pid = utils.ReadLockedPidFile(utils.PathJoin(status_dir, _IES_PID_FILE))
3144 f81c4737 Michael Hanselmann
3145 f81c4737 Michael Hanselmann
  if pid:
3146 f81c4737 Michael Hanselmann
    logging.info("Import/export %s is running with PID %s, sending SIGTERM",
3147 f81c4737 Michael Hanselmann
                 name, pid)
3148 560cbec1 Michael Hanselmann
    utils.IgnoreProcessNotFound(os.kill, pid, signal.SIGTERM)
3149 f81c4737 Michael Hanselmann
3150 f81c4737 Michael Hanselmann
3151 1651d116 Michael Hanselmann
def CleanupImportExport(name):
3152 1651d116 Michael Hanselmann
  """Cleanup after an import or export.
3153 1651d116 Michael Hanselmann

3154 1651d116 Michael Hanselmann
  If the import/export daemon is still running it's killed. Afterwards the
3155 1651d116 Michael Hanselmann
  whole status directory is removed.
3156 1651d116 Michael Hanselmann

3157 1651d116 Michael Hanselmann
  """
3158 1651d116 Michael Hanselmann
  logging.info("Finalizing import/export %s", name)
3159 1651d116 Michael Hanselmann
3160 1651d116 Michael Hanselmann
  status_dir = utils.PathJoin(constants.IMPORT_EXPORT_DIR, name)
3161 1651d116 Michael Hanselmann
3162 debed9ae Michael Hanselmann
  pid = utils.ReadLockedPidFile(utils.PathJoin(status_dir, _IES_PID_FILE))
3163 1651d116 Michael Hanselmann
3164 1651d116 Michael Hanselmann
  if pid:
3165 1651d116 Michael Hanselmann
    logging.info("Import/export %s is still running with PID %s",
3166 1651d116 Michael Hanselmann
                 name, pid)
3167 1651d116 Michael Hanselmann
    utils.KillProcess(pid, waitpid=False)
3168 1651d116 Michael Hanselmann
3169 1651d116 Michael Hanselmann
  shutil.rmtree(status_dir, ignore_errors=True)
3170 1651d116 Michael Hanselmann
3171 1651d116 Michael Hanselmann
3172 6b93ec9d Iustin Pop
def _FindDisks(nodes_ip, disks):
3173 6b93ec9d Iustin Pop
  """Sets the physical ID on disks and returns the block devices.
3174 6b93ec9d Iustin Pop

3175 6b93ec9d Iustin Pop
  """
3176 6b93ec9d Iustin Pop
  # set the correct physical ID
3177 b705c7a6 Manuel Franceschini
  my_name = netutils.Hostname.GetSysName()
3178 6b93ec9d Iustin Pop
  for cf in disks:
3179 6b93ec9d Iustin Pop
    cf.SetPhysicalID(my_name, nodes_ip)
3180 6b93ec9d Iustin Pop
3181 6b93ec9d Iustin Pop
  bdevs = []
3182 6b93ec9d Iustin Pop
3183 6b93ec9d Iustin Pop
  for cf in disks:
3184 6b93ec9d Iustin Pop
    rd = _RecursiveFindBD(cf)
3185 6b93ec9d Iustin Pop
    if rd is None:
3186 5a533f8a Iustin Pop
      _Fail("Can't find device %s", cf)
3187 6b93ec9d Iustin Pop
    bdevs.append(rd)
3188 5a533f8a Iustin Pop
  return bdevs
3189 6b93ec9d Iustin Pop
3190 6b93ec9d Iustin Pop
3191 6b93ec9d Iustin Pop
def DrbdDisconnectNet(nodes_ip, disks):
3192 6b93ec9d Iustin Pop
  """Disconnects the network on a list of drbd devices.
3193 6b93ec9d Iustin Pop

3194 6b93ec9d Iustin Pop
  """
3195 5a533f8a Iustin Pop
  bdevs = _FindDisks(nodes_ip, disks)
3196 6b93ec9d Iustin Pop
3197 6b93ec9d Iustin Pop
  # disconnect disks
3198 6b93ec9d Iustin Pop
  for rd in bdevs:
3199 6b93ec9d Iustin Pop
    try:
3200 6b93ec9d Iustin Pop
      rd.DisconnectNet()
3201 6b93ec9d Iustin Pop
    except errors.BlockDeviceError, err:
3202 2cc6781a Iustin Pop
      _Fail("Can't change network configuration to standalone mode: %s",
3203 2cc6781a Iustin Pop
            err, exc=True)
3204 6b93ec9d Iustin Pop
3205 6b93ec9d Iustin Pop
3206 6b93ec9d Iustin Pop
def DrbdAttachNet(nodes_ip, disks, instance_name, multimaster):
3207 6b93ec9d Iustin Pop
  """Attaches the network on a list of drbd devices.
3208 6b93ec9d Iustin Pop

3209 6b93ec9d Iustin Pop
  """
3210 5a533f8a Iustin Pop
  bdevs = _FindDisks(nodes_ip, disks)
3211 6b93ec9d Iustin Pop
3212 6b93ec9d Iustin Pop
  if multimaster:
3213 53c776b5 Iustin Pop
    for idx, rd in enumerate(bdevs):
3214 6b93ec9d Iustin Pop
      try:
3215 53c776b5 Iustin Pop
        _SymlinkBlockDev(instance_name, rd.dev_path, idx)
3216 6b93ec9d Iustin Pop
      except EnvironmentError, err:
3217 2cc6781a Iustin Pop
        _Fail("Can't create symlink: %s", err)
3218 6b93ec9d Iustin Pop
  # reconnect disks, switch to new master configuration and if
3219 6b93ec9d Iustin Pop
  # needed primary mode
3220 6b93ec9d Iustin Pop
  for rd in bdevs:
3221 6b93ec9d Iustin Pop
    try:
3222 6b93ec9d Iustin Pop
      rd.AttachNet(multimaster)
3223 6b93ec9d Iustin Pop
    except errors.BlockDeviceError, err:
3224 2cc6781a Iustin Pop
      _Fail("Can't change network configuration: %s", err)
3225 3c0cdc83 Michael Hanselmann
3226 6b93ec9d Iustin Pop
  # wait until the disks are connected; we need to retry the re-attach
3227 6b93ec9d Iustin Pop
  # if the device becomes standalone, as this might happen if the one
3228 6b93ec9d Iustin Pop
  # node disconnects and reconnects in a different mode before the
3229 6b93ec9d Iustin Pop
  # other node reconnects; in this case, one or both of the nodes will
3230 6b93ec9d Iustin Pop
  # decide it has wrong configuration and switch to standalone
3231 3c0cdc83 Michael Hanselmann
3232 3c0cdc83 Michael Hanselmann
  def _Attach():
3233 6b93ec9d Iustin Pop
    all_connected = True
3234 3c0cdc83 Michael Hanselmann
3235 6b93ec9d Iustin Pop
    for rd in bdevs:
3236 6b93ec9d Iustin Pop
      stats = rd.GetProcStatus()
3237 3c0cdc83 Michael Hanselmann
3238 3c0cdc83 Michael Hanselmann
      all_connected = (all_connected and
3239 3c0cdc83 Michael Hanselmann
                       (stats.is_connected or stats.is_in_resync))
3240 3c0cdc83 Michael Hanselmann
3241 6b93ec9d Iustin Pop
      if stats.is_standalone:
3242 6b93ec9d Iustin Pop
        # peer had different config info and this node became
3243 6b93ec9d Iustin Pop
        # standalone, even though this should not happen with the
3244 6b93ec9d Iustin Pop
        # new staged way of changing disk configs
3245 6b93ec9d Iustin Pop
        try:
3246 c738375b Iustin Pop
          rd.AttachNet(multimaster)
3247 6b93ec9d Iustin Pop
        except errors.BlockDeviceError, err:
3248 2cc6781a Iustin Pop
          _Fail("Can't change network configuration: %s", err)
3249 3c0cdc83 Michael Hanselmann
3250 3c0cdc83 Michael Hanselmann
    if not all_connected:
3251 3c0cdc83 Michael Hanselmann
      raise utils.RetryAgain()
3252 3c0cdc83 Michael Hanselmann
3253 3c0cdc83 Michael Hanselmann
  try:
3254 3c0cdc83 Michael Hanselmann
    # Start with a delay of 100 miliseconds and go up to 5 seconds
3255 3c0cdc83 Michael Hanselmann
    utils.Retry(_Attach, (0.1, 1.5, 5.0), 2 * 60)
3256 3c0cdc83 Michael Hanselmann
  except utils.RetryTimeout:
3257 afdc3985 Iustin Pop
    _Fail("Timeout in disk reconnecting")
3258 3c0cdc83 Michael Hanselmann
3259 6b93ec9d Iustin Pop
  if multimaster:
3260 6b93ec9d Iustin Pop
    # change to primary mode
3261 6b93ec9d Iustin Pop
    for rd in bdevs:
3262 d3da87b8 Iustin Pop
      try:
3263 d3da87b8 Iustin Pop
        rd.Open()
3264 d3da87b8 Iustin Pop
      except errors.BlockDeviceError, err:
3265 2cc6781a Iustin Pop
        _Fail("Can't change to primary mode: %s", err)
3266 6b93ec9d Iustin Pop
3267 6b93ec9d Iustin Pop
3268 6b93ec9d Iustin Pop
def DrbdWaitSync(nodes_ip, disks):
3269 6b93ec9d Iustin Pop
  """Wait until DRBDs have synchronized.
3270 6b93ec9d Iustin Pop

3271 6b93ec9d Iustin Pop
  """
3272 db8667b7 Iustin Pop
  def _helper(rd):
3273 db8667b7 Iustin Pop
    stats = rd.GetProcStatus()
3274 db8667b7 Iustin Pop
    if not (stats.is_connected or stats.is_in_resync):
3275 db8667b7 Iustin Pop
      raise utils.RetryAgain()
3276 db8667b7 Iustin Pop
    return stats
3277 db8667b7 Iustin Pop
3278 5a533f8a Iustin Pop
  bdevs = _FindDisks(nodes_ip, disks)
3279 6b93ec9d Iustin Pop
3280 6b93ec9d Iustin Pop
  min_resync = 100
3281 6b93ec9d Iustin Pop
  alldone = True
3282 6b93ec9d Iustin Pop
  for rd in bdevs:
3283 db8667b7 Iustin Pop
    try:
3284 db8667b7 Iustin Pop
      # poll each second for 15 seconds
3285 db8667b7 Iustin Pop
      stats = utils.Retry(_helper, 1, 15, args=[rd])
3286 db8667b7 Iustin Pop
    except utils.RetryTimeout:
3287 db8667b7 Iustin Pop
      stats = rd.GetProcStatus()
3288 db8667b7 Iustin Pop
      # last check
3289 db8667b7 Iustin Pop
      if not (stats.is_connected or stats.is_in_resync):
3290 db8667b7 Iustin Pop
        _Fail("DRBD device %s is not in sync: stats=%s", rd, stats)
3291 6b93ec9d Iustin Pop
    alldone = alldone and (not stats.is_in_resync)
3292 6b93ec9d Iustin Pop
    if stats.sync_percent is not None:
3293 6b93ec9d Iustin Pop
      min_resync = min(min_resync, stats.sync_percent)
3294 afdc3985 Iustin Pop
3295 c26a6bd2 Iustin Pop
  return (alldone, min_resync)
3296 6b93ec9d Iustin Pop
3297 6b93ec9d Iustin Pop
3298 c46b9782 Luca Bigliardi
def GetDrbdUsermodeHelper():
3299 c46b9782 Luca Bigliardi
  """Returns DRBD usermode helper currently configured.
3300 c46b9782 Luca Bigliardi

3301 c46b9782 Luca Bigliardi
  """
3302 c46b9782 Luca Bigliardi
  try:
3303 c46b9782 Luca Bigliardi
    return bdev.BaseDRBD.GetUsermodeHelper()
3304 c46b9782 Luca Bigliardi
  except errors.BlockDeviceError, err:
3305 c46b9782 Luca Bigliardi
    _Fail(str(err))
3306 c46b9782 Luca Bigliardi
3307 c46b9782 Luca Bigliardi
3308 f5118ade Iustin Pop
def PowercycleNode(hypervisor_type):
3309 f5118ade Iustin Pop
  """Hard-powercycle the node.
3310 f5118ade Iustin Pop

3311 f5118ade Iustin Pop
  Because we need to return first, and schedule the powercycle in the
3312 f5118ade Iustin Pop
  background, we won't be able to report failures nicely.
3313 f5118ade Iustin Pop

3314 f5118ade Iustin Pop
  """
3315 f5118ade Iustin Pop
  hyper = hypervisor.GetHypervisor(hypervisor_type)
3316 f5118ade Iustin Pop
  try:
3317 f5118ade Iustin Pop
    pid = os.fork()
3318 29921401 Iustin Pop
  except OSError:
3319 f5118ade Iustin Pop
    # if we can't fork, we'll pretend that we're in the child process
3320 f5118ade Iustin Pop
    pid = 0
3321 f5118ade Iustin Pop
  if pid > 0:
3322 c26a6bd2 Iustin Pop
    return "Reboot scheduled in 5 seconds"
3323 1af6ac0f Luca Bigliardi
  # ensure the child is running on ram
3324 1af6ac0f Luca Bigliardi
  try:
3325 1af6ac0f Luca Bigliardi
    utils.Mlockall()
3326 b459a848 Andrea Spadaccini
  except Exception: # pylint: disable=W0703
3327 1af6ac0f Luca Bigliardi
    pass
3328 f5118ade Iustin Pop
  time.sleep(5)
3329 f5118ade Iustin Pop
  hyper.PowercycleNode()
3330 f5118ade Iustin Pop
3331 f5118ade Iustin Pop
3332 a8083063 Iustin Pop
class HooksRunner(object):
3333 a8083063 Iustin Pop
  """Hook runner.
3334 a8083063 Iustin Pop

3335 10c2650b Iustin Pop
  This class is instantiated on the node side (ganeti-noded) and not
3336 10c2650b Iustin Pop
  on the master side.
3337 a8083063 Iustin Pop

3338 a8083063 Iustin Pop
  """
3339 a8083063 Iustin Pop
  def __init__(self, hooks_base_dir=None):
3340 a8083063 Iustin Pop
    """Constructor for hooks runner.
3341 a8083063 Iustin Pop

3342 10c2650b Iustin Pop
    @type hooks_base_dir: str or None
3343 10c2650b Iustin Pop
    @param hooks_base_dir: if not None, this overrides the
3344 10c2650b Iustin Pop
        L{constants.HOOKS_BASE_DIR} (useful for unittests)
3345 a8083063 Iustin Pop

3346 a8083063 Iustin Pop
    """
3347 a8083063 Iustin Pop
    if hooks_base_dir is None:
3348 a8083063 Iustin Pop
      hooks_base_dir = constants.HOOKS_BASE_DIR
3349 fe267188 Iustin Pop
    # yeah, _BASE_DIR is not valid for attributes, we use it like a
3350 fe267188 Iustin Pop
    # constant
3351 b459a848 Andrea Spadaccini
    self._BASE_DIR = hooks_base_dir # pylint: disable=C0103
3352 a8083063 Iustin Pop
3353 a8083063 Iustin Pop
  def RunHooks(self, hpath, phase, env):
3354 a8083063 Iustin Pop
    """Run the scripts in the hooks directory.
3355 a8083063 Iustin Pop

3356 10c2650b Iustin Pop
    @type hpath: str
3357 10c2650b Iustin Pop
    @param hpath: the path to the hooks directory which
3358 10c2650b Iustin Pop
        holds the scripts
3359 10c2650b Iustin Pop
    @type phase: str
3360 10c2650b Iustin Pop
    @param phase: either L{constants.HOOKS_PHASE_PRE} or
3361 10c2650b Iustin Pop
        L{constants.HOOKS_PHASE_POST}
3362 10c2650b Iustin Pop
    @type env: dict
3363 10c2650b Iustin Pop
    @param env: dictionary with the environment for the hook
3364 10c2650b Iustin Pop
    @rtype: list
3365 10c2650b Iustin Pop
    @return: list of 3-element tuples:
3366 10c2650b Iustin Pop
      - script path
3367 10c2650b Iustin Pop
      - script result, either L{constants.HKR_SUCCESS} or
3368 10c2650b Iustin Pop
        L{constants.HKR_FAIL}
3369 10c2650b Iustin Pop
      - output of the script
3370 10c2650b Iustin Pop

3371 10c2650b Iustin Pop
    @raise errors.ProgrammerError: for invalid input
3372 10c2650b Iustin Pop
        parameters
3373 a8083063 Iustin Pop

3374 a8083063 Iustin Pop
    """
3375 a8083063 Iustin Pop
    if phase == constants.HOOKS_PHASE_PRE:
3376 a8083063 Iustin Pop
      suffix = "pre"
3377 a8083063 Iustin Pop
    elif phase == constants.HOOKS_PHASE_POST:
3378 a8083063 Iustin Pop
      suffix = "post"
3379 a8083063 Iustin Pop
    else:
3380 3fb4f740 Iustin Pop
      _Fail("Unknown hooks phase '%s'", phase)
3381 3fb4f740 Iustin Pop
3382 a8083063 Iustin Pop
    subdir = "%s-%s.d" % (hpath, suffix)
3383 0411c011 Iustin Pop
    dir_name = utils.PathJoin(self._BASE_DIR, subdir)
3384 6bb65e3a Guido Trotter
3385 6bb65e3a Guido Trotter
    results = []
3386 a9b7e346 Iustin Pop
3387 a9b7e346 Iustin Pop
    if not os.path.isdir(dir_name):
3388 a9b7e346 Iustin Pop
      # for non-existing/non-dirs, we simply exit instead of logging a
3389 a9b7e346 Iustin Pop
      # warning at every operation
3390 a9b7e346 Iustin Pop
      return results
3391 a9b7e346 Iustin Pop
3392 a9b7e346 Iustin Pop
    runparts_results = utils.RunParts(dir_name, env=env, reset_env=True)
3393 a9b7e346 Iustin Pop
3394 6bb65e3a Guido Trotter
    for (relname, relstatus, runresult)  in runparts_results:
3395 6bb65e3a Guido Trotter
      if relstatus == constants.RUNPARTS_SKIP:
3396 a8083063 Iustin Pop
        rrval = constants.HKR_SKIP
3397 a8083063 Iustin Pop
        output = ""
3398 6bb65e3a Guido Trotter
      elif relstatus == constants.RUNPARTS_ERR:
3399 6bb65e3a Guido Trotter
        rrval = constants.HKR_FAIL
3400 6bb65e3a Guido Trotter
        output = "Hook script execution error: %s" % runresult
3401 6bb65e3a Guido Trotter
      elif relstatus == constants.RUNPARTS_RUN:
3402 6bb65e3a Guido Trotter
        if runresult.failed:
3403 a8083063 Iustin Pop
          rrval = constants.HKR_FAIL
3404 a8083063 Iustin Pop
        else:
3405 6bb65e3a Guido Trotter
          rrval = constants.HKR_SUCCESS
3406 6bb65e3a Guido Trotter
        output = utils.SafeEncode(runresult.output.strip())
3407 6bb65e3a Guido Trotter
      results.append(("%s/%s" % (subdir, relname), rrval, output))
3408 6bb65e3a Guido Trotter
3409 6bb65e3a Guido Trotter
    return results
3410 3f78eef2 Iustin Pop
3411 3f78eef2 Iustin Pop
3412 8d528b7c Iustin Pop
class IAllocatorRunner(object):
3413 8d528b7c Iustin Pop
  """IAllocator runner.
3414 8d528b7c Iustin Pop

3415 8d528b7c Iustin Pop
  This class is instantiated on the node side (ganeti-noded) and not on
3416 8d528b7c Iustin Pop
  the master side.
3417 8d528b7c Iustin Pop

3418 8d528b7c Iustin Pop
  """
3419 7e950d31 Iustin Pop
  @staticmethod
3420 7e950d31 Iustin Pop
  def Run(name, idata):
3421 8d528b7c Iustin Pop
    """Run an iallocator script.
3422 8d528b7c Iustin Pop

3423 10c2650b Iustin Pop
    @type name: str
3424 10c2650b Iustin Pop
    @param name: the iallocator script name
3425 10c2650b Iustin Pop
    @type idata: str
3426 10c2650b Iustin Pop
    @param idata: the allocator input data
3427 10c2650b Iustin Pop

3428 10c2650b Iustin Pop
    @rtype: tuple
3429 87f5c298 Iustin Pop
    @return: two element tuple of:
3430 87f5c298 Iustin Pop
       - status
3431 87f5c298 Iustin Pop
       - either error message or stdout of allocator (for success)
3432 8d528b7c Iustin Pop

3433 8d528b7c Iustin Pop
    """
3434 8d528b7c Iustin Pop
    alloc_script = utils.FindFile(name, constants.IALLOCATOR_SEARCH_PATH,
3435 8d528b7c Iustin Pop
                                  os.path.isfile)
3436 8d528b7c Iustin Pop
    if alloc_script is None:
3437 87f5c298 Iustin Pop
      _Fail("iallocator module '%s' not found in the search path", name)
3438 8d528b7c Iustin Pop
3439 8d528b7c Iustin Pop
    fd, fin_name = tempfile.mkstemp(prefix="ganeti-iallocator.")
3440 8d528b7c Iustin Pop
    try:
3441 8d528b7c Iustin Pop
      os.write(fd, idata)
3442 8d528b7c Iustin Pop
      os.close(fd)
3443 8d528b7c Iustin Pop
      result = utils.RunCmd([alloc_script, fin_name])
3444 8d528b7c Iustin Pop
      if result.failed:
3445 87f5c298 Iustin Pop
        _Fail("iallocator module '%s' failed: %s, output '%s'",
3446 87f5c298 Iustin Pop
              name, result.fail_reason, result.output)
3447 8d528b7c Iustin Pop
    finally:
3448 8d528b7c Iustin Pop
      os.unlink(fin_name)
3449 8d528b7c Iustin Pop
3450 c26a6bd2 Iustin Pop
    return result.stdout
3451 8d528b7c Iustin Pop
3452 8d528b7c Iustin Pop
3453 3f78eef2 Iustin Pop
class DevCacheManager(object):
3454 c99a3cc0 Manuel Franceschini
  """Simple class for managing a cache of block device information.
3455 3f78eef2 Iustin Pop

3456 3f78eef2 Iustin Pop
  """
3457 3f78eef2 Iustin Pop
  _DEV_PREFIX = "/dev/"
3458 3f78eef2 Iustin Pop
  _ROOT_DIR = constants.BDEV_CACHE_DIR
3459 3f78eef2 Iustin Pop
3460 3f78eef2 Iustin Pop
  @classmethod
3461 3f78eef2 Iustin Pop
  def _ConvertPath(cls, dev_path):
3462 3f78eef2 Iustin Pop
    """Converts a /dev/name path to the cache file name.
3463 3f78eef2 Iustin Pop

3464 3f78eef2 Iustin Pop
    This replaces slashes with underscores and strips the /dev
3465 10c2650b Iustin Pop
    prefix. It then returns the full path to the cache file.
3466 10c2650b Iustin Pop

3467 10c2650b Iustin Pop
    @type dev_path: str
3468 10c2650b Iustin Pop
    @param dev_path: the C{/dev/} path name
3469 10c2650b Iustin Pop
    @rtype: str
3470 10c2650b Iustin Pop
    @return: the converted path name
3471 3f78eef2 Iustin Pop

3472 3f78eef2 Iustin Pop
    """
3473 3f78eef2 Iustin Pop
    if dev_path.startswith(cls._DEV_PREFIX):
3474 3f78eef2 Iustin Pop
      dev_path = dev_path[len(cls._DEV_PREFIX):]
3475 3f78eef2 Iustin Pop
    dev_path = dev_path.replace("/", "_")
3476 0411c011 Iustin Pop
    fpath = utils.PathJoin(cls._ROOT_DIR, "bdev_%s" % dev_path)
3477 3f78eef2 Iustin Pop
    return fpath
3478 3f78eef2 Iustin Pop
3479 3f78eef2 Iustin Pop
  @classmethod
3480 3f78eef2 Iustin Pop
  def UpdateCache(cls, dev_path, owner, on_primary, iv_name):
3481 3f78eef2 Iustin Pop
    """Updates the cache information for a given device.
3482 3f78eef2 Iustin Pop

3483 10c2650b Iustin Pop
    @type dev_path: str
3484 10c2650b Iustin Pop
    @param dev_path: the pathname of the device
3485 10c2650b Iustin Pop
    @type owner: str
3486 10c2650b Iustin Pop
    @param owner: the owner (instance name) of the device
3487 10c2650b Iustin Pop
    @type on_primary: bool
3488 10c2650b Iustin Pop
    @param on_primary: whether this is the primary
3489 10c2650b Iustin Pop
        node nor not
3490 10c2650b Iustin Pop
    @type iv_name: str
3491 10c2650b Iustin Pop
    @param iv_name: the instance-visible name of the
3492 c41eea6e Iustin Pop
        device, as in objects.Disk.iv_name
3493 10c2650b Iustin Pop

3494 10c2650b Iustin Pop
    @rtype: None
3495 10c2650b Iustin Pop

3496 3f78eef2 Iustin Pop
    """
3497 cf5a8306 Iustin Pop
    if dev_path is None:
3498 18682bca Iustin Pop
      logging.error("DevCacheManager.UpdateCache got a None dev_path")
3499 cf5a8306 Iustin Pop
      return
3500 3f78eef2 Iustin Pop
    fpath = cls._ConvertPath(dev_path)
3501 3f78eef2 Iustin Pop
    if on_primary:
3502 3f78eef2 Iustin Pop
      state = "primary"
3503 3f78eef2 Iustin Pop
    else:
3504 3f78eef2 Iustin Pop
      state = "secondary"
3505 3f78eef2 Iustin Pop
    if iv_name is None:
3506 3f78eef2 Iustin Pop
      iv_name = "not_visible"
3507 3f78eef2 Iustin Pop
    fdata = "%s %s %s\n" % (str(owner), state, iv_name)
3508 3f78eef2 Iustin Pop
    try:
3509 3f78eef2 Iustin Pop
      utils.WriteFile(fpath, data=fdata)
3510 3f78eef2 Iustin Pop
    except EnvironmentError, err:
3511 29921401 Iustin Pop
      logging.exception("Can't update bdev cache for %s: %s", dev_path, err)
3512 3f78eef2 Iustin Pop
3513 3f78eef2 Iustin Pop
  @classmethod
3514 3f78eef2 Iustin Pop
  def RemoveCache(cls, dev_path):
3515 3f78eef2 Iustin Pop
    """Remove data for a dev_path.
3516 3f78eef2 Iustin Pop

3517 3865ca48 Michael Hanselmann
    This is just a wrapper over L{utils.io.RemoveFile} with a converted
3518 10c2650b Iustin Pop
    path name and logging.
3519 10c2650b Iustin Pop

3520 10c2650b Iustin Pop
    @type dev_path: str
3521 10c2650b Iustin Pop
    @param dev_path: the pathname of the device
3522 10c2650b Iustin Pop

3523 10c2650b Iustin Pop
    @rtype: None
3524 10c2650b Iustin Pop

3525 3f78eef2 Iustin Pop
    """
3526 cf5a8306 Iustin Pop
    if dev_path is None:
3527 18682bca Iustin Pop
      logging.error("DevCacheManager.RemoveCache got a None dev_path")
3528 cf5a8306 Iustin Pop
      return
3529 3f78eef2 Iustin Pop
    fpath = cls._ConvertPath(dev_path)
3530 3f78eef2 Iustin Pop
    try:
3531 3f78eef2 Iustin Pop
      utils.RemoveFile(fpath)
3532 3f78eef2 Iustin Pop
    except EnvironmentError, err:
3533 29921401 Iustin Pop
      logging.exception("Can't update bdev cache for %s: %s", dev_path, err)