| Branch: | Tag: | Revision:

root / lib / @ b54ecf12

History | View | Annotate | Download (127.5 kB)

1 2f31098c Iustin Pop
2 a8083063 Iustin Pop
3 a8083063 Iustin Pop
4 45bc4635 Iustin Pop
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 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
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 cde49218 Helga Velroyen
from import bdev
58 cde49218 Helga Velroyen
from import drbd
59 a8083063 Iustin Pop
from ganeti import objects
60 880478f8 Iustin Pop
from ganeti import ssconf
61 1651d116 Michael Hanselmann
from ganeti import serializer
62 a744b676 Manuel Franceschini
from ganeti import netutils
63 82b22e19 René Nussbaumer
from ganeti import runtime
64 3ccd3243 Andrea Spadaccini
from ganeti import compat
65 710f30ec Michael Hanselmann
from ganeti import pathutils
66 cffbbae7 Michael Hanselmann
from ganeti import vcluster
67 99e222b1 Michael Hanselmann
from ganeti import ht
68 cde49218 Helga Velroyen
from import BlockDev
69 cde49218 Helga Velroyen
from import DRBD8
70 68d95757 Guido Trotter
from ganeti import hooksmaster
71 a8083063 Iustin Pop
72 a8083063 Iustin Pop
73 13998ef2 Michael Hanselmann
_BOOT_ID_PATH = "/proc/sys/kernel/random/boot_id"
74 b8028dcf Michael Hanselmann
_ALLOWED_CLEAN_DIRS = compat.UniqueFrozenset([
75 710f30ec Michael Hanselmann
76 710f30ec Michael Hanselmann
77 710f30ec Michael Hanselmann
78 710f30ec Michael Hanselmann
79 714ea7ca Iustin Pop
80 f942a838 Michael Hanselmann
_MAX_SSL_CERT_VALIDITY = 7 * 24 * 60 * 60
81 f942a838 Michael Hanselmann
_X509_KEY_FILE = "key"
82 f942a838 Michael Hanselmann
_X509_CERT_FILE = "cert"
83 1651d116 Michael Hanselmann
_IES_STATUS_FILE = "status"
84 1651d116 Michael Hanselmann
_IES_PID_FILE = "pid"
85 1651d116 Michael Hanselmann
_IES_CA_FILE = "ca"
86 13998ef2 Michael Hanselmann
87 0b5303da Iustin Pop
#: Valid LVS output line regex
88 a1f38213 Iustin Pop
_LVSLINE_REGEX = re.compile("^ *([^|]+)\|([^|]+)\|([0-9.]+)\|([^|]{6,})\|?$")
89 0b5303da Iustin Pop
90 702eff21 Andrea Spadaccini
# Actions for the master setup script
91 702eff21 Andrea Spadaccini
_MASTER_START = "start"
92 702eff21 Andrea Spadaccini
_MASTER_STOP = "stop"
93 702eff21 Andrea Spadaccini
94 45bc4635 Iustin Pop
#: Maximum file permissions for restricted command directory and executables
95 1a2eb2dc Michael Hanselmann
96 1a2eb2dc Michael Hanselmann
                  stat.S_IRGRP | stat.S_IXGRP |
97 1a2eb2dc Michael Hanselmann
                  stat.S_IROTH | stat.S_IXOTH)
98 1a2eb2dc Michael Hanselmann
99 45bc4635 Iustin Pop
#: Delay before returning an error for restricted commands
100 1a2eb2dc Michael Hanselmann
101 1a2eb2dc Michael Hanselmann
102 45bc4635 Iustin Pop
#: How long to wait to acquire lock for restricted commands (shorter than
103 1a2eb2dc Michael Hanselmann
#: L{_RCMD_INVALID_DELAY}) to reduce blockage of noded forks when many
104 1a2eb2dc Michael Hanselmann
#: command requests arrive
105 1a2eb2dc Michael Hanselmann
106 1a2eb2dc Michael Hanselmann
107 13998ef2 Michael Hanselmann
108 2cc6781a Iustin Pop
class RPCFail(Exception):
109 2cc6781a Iustin Pop
  """Class denoting RPC failure.
110 2cc6781a Iustin Pop

111 2cc6781a Iustin Pop
  Its argument is the error message.
112 2cc6781a Iustin Pop

113 2cc6781a Iustin Pop
114 2cc6781a Iustin Pop
115 13998ef2 Michael Hanselmann
116 584ea340 Michele Tartara
def _GetInstReasonFilename(instance_name):
117 a59d5fa1 Michele Tartara
  """Path of the file containing the reason of the instance status change.
118 a59d5fa1 Michele Tartara

119 a59d5fa1 Michele Tartara
  @type instance_name: string
120 a59d5fa1 Michele Tartara
  @param instance_name: The name of the instance
121 a59d5fa1 Michele Tartara
  @rtype: string
122 a59d5fa1 Michele Tartara
  @return: The path of the file
123 a59d5fa1 Michele Tartara

124 a59d5fa1 Michele Tartara
125 a59d5fa1 Michele Tartara
  return utils.PathJoin(pathutils.INSTANCE_REASON_DIR, instance_name)
126 a59d5fa1 Michele Tartara
127 a59d5fa1 Michele Tartara
128 584ea340 Michele Tartara
def _StoreInstReasonTrail(instance_name, trail):
129 584ea340 Michele Tartara
  """Serialize a reason trail related to an instance change of state to file.
130 584ea340 Michele Tartara

131 584ea340 Michele Tartara
  The exact location of the file depends on the name of the instance and on
132 584ea340 Michele Tartara
  the configuration of the Ganeti cluster defined at deploy time.
133 584ea340 Michele Tartara

134 584ea340 Michele Tartara
  @type instance_name: string
135 584ea340 Michele Tartara
  @param instance_name: The name of the instance
136 584ea340 Michele Tartara
  @rtype: None
137 584ea340 Michele Tartara

138 584ea340 Michele Tartara
139 584ea340 Michele Tartara
  json = serializer.DumpJson(trail)
140 584ea340 Michele Tartara
  filename = _GetInstReasonFilename(instance_name)
141 584ea340 Michele Tartara
  utils.WriteFile(filename, data=json)
142 584ea340 Michele Tartara
143 584ea340 Michele Tartara
144 2cc6781a Iustin Pop
def _Fail(msg, *args, **kwargs):
145 2cc6781a Iustin Pop
  """Log an error and the raise an RPCFail exception.
146 2cc6781a Iustin Pop

147 2cc6781a Iustin Pop
  This exception is then handled specially in the ganeti daemon and
148 2cc6781a Iustin Pop
  turned into a 'failed' return type. As such, this function is a
149 2cc6781a Iustin Pop
  useful shortcut for logging the error and returning it to the master
150 2cc6781a Iustin Pop
151 2cc6781a Iustin Pop

152 2cc6781a Iustin Pop
  @type msg: string
153 2cc6781a Iustin Pop
  @param msg: the text of the exception
154 2cc6781a Iustin Pop
  @raise RPCFail
155 2cc6781a Iustin Pop

156 2cc6781a Iustin Pop
157 2cc6781a Iustin Pop
  if args:
158 2cc6781a Iustin Pop
    msg = msg % args
159 afdc3985 Iustin Pop
  if "log" not in kwargs or kwargs["log"]: # if we should log this error
160 afdc3985 Iustin Pop
    if "exc" in kwargs and kwargs["exc"]:
161 afdc3985 Iustin Pop
162 afdc3985 Iustin Pop
163 afdc3985 Iustin Pop
164 2cc6781a Iustin Pop
  raise RPCFail(msg)
165 2cc6781a Iustin Pop
166 2cc6781a Iustin Pop
167 c657dcc9 Michael Hanselmann
def _GetConfig():
168 93384844 Iustin Pop
  """Simple wrapper to return a SimpleStore.
169 10c2650b Iustin Pop

170 93384844 Iustin Pop
  @rtype: L{ssconf.SimpleStore}
171 93384844 Iustin Pop
  @return: a SimpleStore instance
172 10c2650b Iustin Pop

173 10c2650b Iustin Pop
174 93384844 Iustin Pop
  return ssconf.SimpleStore()
175 c657dcc9 Michael Hanselmann
176 c657dcc9 Michael Hanselmann
177 62c9ec92 Iustin Pop
def _GetSshRunner(cluster_name):
178 10c2650b Iustin Pop
  """Simple wrapper to return an SshRunner.
179 10c2650b Iustin Pop

180 10c2650b Iustin Pop
  @type cluster_name: str
181 10c2650b Iustin Pop
  @param cluster_name: the cluster name, which is needed
182 10c2650b Iustin Pop
      by the SshRunner constructor
183 10c2650b Iustin Pop
  @rtype: L{ssh.SshRunner}
184 10c2650b Iustin Pop
  @return: an SshRunner instance
185 10c2650b Iustin Pop

186 10c2650b Iustin Pop
187 62c9ec92 Iustin Pop
  return ssh.SshRunner(cluster_name)
188 c92b310a Michael Hanselmann
189 c92b310a Michael Hanselmann
190 12bce260 Michael Hanselmann
def _Decompress(data):
191 12bce260 Michael Hanselmann
  """Unpacks data compressed by the RPC client.
192 12bce260 Michael Hanselmann

193 12bce260 Michael Hanselmann
  @type data: list or tuple
194 12bce260 Michael Hanselmann
  @param data: Data sent by RPC client
195 12bce260 Michael Hanselmann
  @rtype: str
196 12bce260 Michael Hanselmann
  @return: Decompressed data
197 12bce260 Michael Hanselmann

198 12bce260 Michael Hanselmann
199 52e2f66e Michael Hanselmann
  assert isinstance(data, (list, tuple))
200 12bce260 Michael Hanselmann
  assert len(data) == 2
201 12bce260 Michael Hanselmann
  (encoding, content) = data
202 12bce260 Michael Hanselmann
  if encoding == constants.RPC_ENCODING_NONE:
203 12bce260 Michael Hanselmann
    return content
204 12bce260 Michael Hanselmann
  elif encoding == constants.RPC_ENCODING_ZLIB_BASE64:
205 12bce260 Michael Hanselmann
    return zlib.decompress(base64.b64decode(content))
206 12bce260 Michael Hanselmann
207 12bce260 Michael Hanselmann
    raise AssertionError("Unknown data encoding")
208 12bce260 Michael Hanselmann
209 12bce260 Michael Hanselmann
210 3bc6be5c Iustin Pop
def _CleanDirectory(path, exclude=None):
211 76ab5558 Michael Hanselmann
  """Removes all regular files in a directory.
212 76ab5558 Michael Hanselmann

213 10c2650b Iustin Pop
  @type path: str
214 10c2650b Iustin Pop
  @param path: the directory to clean
215 76ab5558 Michael Hanselmann
  @type exclude: list
216 10c2650b Iustin Pop
  @param exclude: list of files to be excluded, defaults
217 10c2650b Iustin Pop
      to the empty list
218 76ab5558 Michael Hanselmann

219 76ab5558 Michael Hanselmann
220 714ea7ca Iustin Pop
  if path not in _ALLOWED_CLEAN_DIRS:
221 714ea7ca Iustin Pop
    _Fail("Path passed to _CleanDirectory not in allowed clean targets: '%s'",
222 714ea7ca Iustin Pop
223 714ea7ca Iustin Pop
224 3956cee1 Michael Hanselmann
  if not os.path.isdir(path):
225 3956cee1 Michael Hanselmann
226 3bc6be5c Iustin Pop
  if exclude is None:
227 3bc6be5c Iustin Pop
    exclude = []
228 3bc6be5c Iustin Pop
229 3bc6be5c Iustin Pop
    # Normalize excluded paths
230 3bc6be5c Iustin Pop
    exclude = [os.path.normpath(i) for i in exclude]
231 76ab5558 Michael Hanselmann
232 3956cee1 Michael Hanselmann
  for rel_name in utils.ListVisibleFiles(path):
233 c4feafe8 Iustin Pop
    full_name = utils.PathJoin(path, rel_name)
234 76ab5558 Michael Hanselmann
    if full_name in exclude:
235 76ab5558 Michael Hanselmann
236 3956cee1 Michael Hanselmann
    if os.path.isfile(full_name) and not os.path.islink(full_name):
237 3956cee1 Michael Hanselmann
238 3956cee1 Michael Hanselmann
239 3956cee1 Michael Hanselmann
240 360b0dc2 Iustin Pop
def _BuildUploadFileList():
241 360b0dc2 Iustin Pop
  """Build the list of allowed upload files.
242 360b0dc2 Iustin Pop

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

245 360b0dc2 Iustin Pop
246 b397a7d2 Iustin Pop
  allowed_files = set([
247 710f30ec Michael Hanselmann
248 ee045466 Michael Hanselmann
249 710f30ec Michael Hanselmann
250 710f30ec Michael Hanselmann
251 710f30ec Michael Hanselmann
252 710f30ec Michael Hanselmann
253 710f30ec Michael Hanselmann
254 710f30ec Michael Hanselmann
255 710f30ec Michael Hanselmann
256 710f30ec Michael Hanselmann
257 b397a7d2 Iustin Pop
258 b397a7d2 Iustin Pop
259 b397a7d2 Iustin Pop
  for hv_name in constants.HYPER_TYPES:
260 e5a45a16 Iustin Pop
    hv_class = hypervisor.GetHypervisorClass(hv_name)
261 69ab2e12 Guido Trotter
262 b397a7d2 Iustin Pop
263 3439fd6b Michael Hanselmann
  assert pathutils.FILE_STORAGE_PATHS_FILE not in allowed_files, \
264 3439fd6b Michael Hanselmann
    "Allowed file storage paths should never be uploaded via RPC"
265 3439fd6b Michael Hanselmann
266 b397a7d2 Iustin Pop
  return frozenset(allowed_files)
267 360b0dc2 Iustin Pop
268 360b0dc2 Iustin Pop
269 360b0dc2 Iustin Pop
_ALLOWED_UPLOAD_FILES = _BuildUploadFileList()
270 360b0dc2 Iustin Pop
271 360b0dc2 Iustin Pop
272 1bc59f76 Michael Hanselmann
def JobQueuePurge():
273 10c2650b Iustin Pop
  """Removes job queue files and archived jobs.
274 10c2650b Iustin Pop

275 c8457ce7 Iustin Pop
  @rtype: tuple
276 c8457ce7 Iustin Pop
  @return: True, None
277 24fc781f Michael Hanselmann

278 24fc781f Michael Hanselmann
279 710f30ec Michael Hanselmann
  _CleanDirectory(pathutils.QUEUE_DIR, exclude=[pathutils.JOB_QUEUE_LOCK_FILE])
280 710f30ec Michael Hanselmann
281 24fc781f Michael Hanselmann
282 24fc781f Michael Hanselmann
283 bd1e4562 Iustin Pop
def GetMasterInfo():
284 bd1e4562 Iustin Pop
  """Returns master information.
285 bd1e4562 Iustin Pop

286 bd1e4562 Iustin Pop
  This is an utility function to compute master information, either
287 bd1e4562 Iustin Pop
  for consumption here or from the node daemon.
288 bd1e4562 Iustin Pop

289 bd1e4562 Iustin Pop
  @rtype: tuple
290 909b3a0e Andrea Spadaccini
  @return: master_netdev, master_ip, master_name, primary_ip_family,
291 909b3a0e Andrea Spadaccini
292 2a52a064 Iustin Pop
  @raise RPCFail: in case of errors
293 b1b6ea87 Iustin Pop

294 b1b6ea87 Iustin Pop
295 b1b6ea87 Iustin Pop
296 c657dcc9 Michael Hanselmann
    cfg = _GetConfig()
297 c657dcc9 Michael Hanselmann
    master_netdev = cfg.GetMasterNetdev()
298 c657dcc9 Michael Hanselmann
    master_ip = cfg.GetMasterIP()
299 5a8648eb Andrea Spadaccini
    master_netmask = cfg.GetMasterNetmask()
300 c657dcc9 Michael Hanselmann
    master_node = cfg.GetMasterNode()
301 d8e0caa6 Manuel Franceschini
    primary_ip_family = cfg.GetPrimaryIPFamily()
302 b1b6ea87 Iustin Pop
  except errors.ConfigurationError, err:
303 29921401 Iustin Pop
    _Fail("Cluster configuration incomplete: %s", err, exc=True)
304 909b3a0e Andrea Spadaccini
  return (master_netdev, master_ip, master_node, primary_ip_family,
305 5ae4945a Iustin Pop
306 b1b6ea87 Iustin Pop
307 b1b6ea87 Iustin Pop
308 0fa481f5 Andrea Spadaccini
def RunLocalHooks(hook_opcode, hooks_path, env_builder_fn):
309 0fa481f5 Andrea Spadaccini
  """Decorator that runs hooks before and after the decorated function.
310 0fa481f5 Andrea Spadaccini

311 0fa481f5 Andrea Spadaccini
  @type hook_opcode: string
312 0fa481f5 Andrea Spadaccini
  @param hook_opcode: opcode of the hook
313 0fa481f5 Andrea Spadaccini
  @type hooks_path: string
314 0fa481f5 Andrea Spadaccini
  @param hooks_path: path of the hooks
315 0fa481f5 Andrea Spadaccini
  @type env_builder_fn: function
316 0fa481f5 Andrea Spadaccini
  @param env_builder_fn: function that returns a dictionary containing the
317 3ccd3243 Andrea Spadaccini
    environment variables for the hooks. Will get all the parameters of the
318 3ccd3243 Andrea Spadaccini
    decorated function.
319 0fa481f5 Andrea Spadaccini
  @raise RPCFail: in case of pre-hook failure
320 0fa481f5 Andrea Spadaccini

321 0fa481f5 Andrea Spadaccini
322 0fa481f5 Andrea Spadaccini
  def decorator(fn):
323 0fa481f5 Andrea Spadaccini
    def wrapper(*args, **kwargs):
324 0fa481f5 Andrea Spadaccini
      _, myself = ssconf.GetMasterAndMyself()
325 0fa481f5 Andrea Spadaccini
      nodes = ([myself], [myself])  # these hooks run locally
326 0fa481f5 Andrea Spadaccini
327 3ccd3243 Andrea Spadaccini
      env_fn = compat.partial(env_builder_fn, *args, **kwargs)
328 3ccd3243 Andrea Spadaccini
329 0fa481f5 Andrea Spadaccini
      cfg = _GetConfig()
330 0fa481f5 Andrea Spadaccini
      hr = HooksRunner()
331 68d95757 Guido Trotter
      hm = hooksmaster.HooksMaster(hook_opcode, hooks_path, nodes,
332 68d95757 Guido Trotter
                                   hr.RunLocalHooks, None, env_fn,
333 68d95757 Guido Trotter
                                   logging.warning, cfg.GetClusterName(),
334 68d95757 Guido Trotter
335 0fa481f5 Andrea Spadaccini
336 0fa481f5 Andrea Spadaccini
      result = fn(*args, **kwargs)
337 0fa481f5 Andrea Spadaccini
338 0fa481f5 Andrea Spadaccini
339 0fa481f5 Andrea Spadaccini
      return result
340 0fa481f5 Andrea Spadaccini
    return wrapper
341 0fa481f5 Andrea Spadaccini
  return decorator
342 0fa481f5 Andrea Spadaccini
343 0fa481f5 Andrea Spadaccini
344 57c7bc57 Andrea Spadaccini
def _BuildMasterIpEnv(master_params, use_external_mip_script=None):
345 2d88fdd3 Andrea Spadaccini
  """Builds environment variables for master IP hooks.
346 2d88fdd3 Andrea Spadaccini

347 3ccd3243 Andrea Spadaccini
  @type master_params: L{objects.MasterNetworkParameters}
348 3ccd3243 Andrea Spadaccini
  @param master_params: network parameters of the master
349 57c7bc57 Andrea Spadaccini
  @type use_external_mip_script: boolean
350 57c7bc57 Andrea Spadaccini
  @param use_external_mip_script: whether to use an external master IP
351 57c7bc57 Andrea Spadaccini
    address setup script (unused, but necessary per the implementation of the
352 57c7bc57 Andrea Spadaccini
    _RunLocalHooks decorator)
353 3ccd3243 Andrea Spadaccini

354 2d88fdd3 Andrea Spadaccini
355 57c7bc57 Andrea Spadaccini
  # pylint: disable=W0613
356 3ccd3243 Andrea Spadaccini
  ver = netutils.IPAddress.GetVersionFromAddressFamily(master_params.ip_family)
357 2d88fdd3 Andrea Spadaccini
  env = {
358 3ccd3243 Andrea Spadaccini
    "MASTER_NETDEV": master_params.netdev,
359 3ccd3243 Andrea Spadaccini
    "MASTER_IP": master_params.ip,
360 702eff21 Andrea Spadaccini
    "MASTER_NETMASK": str(master_params.netmask),
361 3ccd3243 Andrea Spadaccini
    "CLUSTER_IP_VERSION": str(ver),
362 2d88fdd3 Andrea Spadaccini
363 2d88fdd3 Andrea Spadaccini
364 2d88fdd3 Andrea Spadaccini
  return env
365 2d88fdd3 Andrea Spadaccini
366 2d88fdd3 Andrea Spadaccini
367 702eff21 Andrea Spadaccini
def _RunMasterSetupScript(master_params, action, use_external_mip_script):
368 702eff21 Andrea Spadaccini
  """Execute the master IP address setup script.
369 702eff21 Andrea Spadaccini

370 702eff21 Andrea Spadaccini
  @type master_params: L{objects.MasterNetworkParameters}
371 702eff21 Andrea Spadaccini
  @param master_params: network parameters of the master
372 702eff21 Andrea Spadaccini
  @type action: string
373 702eff21 Andrea Spadaccini
  @param action: action to pass to the script. Must be one of
374 702eff21 Andrea Spadaccini
    L{backend._MASTER_START} or L{backend._MASTER_STOP}
375 702eff21 Andrea Spadaccini
  @type use_external_mip_script: boolean
376 702eff21 Andrea Spadaccini
  @param use_external_mip_script: whether to use an external master IP
377 702eff21 Andrea Spadaccini
    address setup script
378 702eff21 Andrea Spadaccini
  @raise backend.RPCFail: if there are errors during the execution of the
379 702eff21 Andrea Spadaccini
380 702eff21 Andrea Spadaccini

381 702eff21 Andrea Spadaccini
382 702eff21 Andrea Spadaccini
  env = _BuildMasterIpEnv(master_params)
383 702eff21 Andrea Spadaccini
384 702eff21 Andrea Spadaccini
  if use_external_mip_script:
385 710f30ec Michael Hanselmann
    setup_script = pathutils.EXTERNAL_MASTER_SETUP_SCRIPT
386 702eff21 Andrea Spadaccini
387 710f30ec Michael Hanselmann
    setup_script = pathutils.DEFAULT_MASTER_SETUP_SCRIPT
388 702eff21 Andrea Spadaccini
389 702eff21 Andrea Spadaccini
  result = utils.RunCmd([setup_script, action], env=env, reset_env=True)
390 702eff21 Andrea Spadaccini
391 702eff21 Andrea Spadaccini
  if result.failed:
392 19e1b715 Iustin Pop
    _Fail("Failed to %s the master IP. Script return value: %s, output: '%s'" %
393 19e1b715 Iustin Pop
          (action, result.exit_code, result.output), log=True)
394 702eff21 Andrea Spadaccini
395 702eff21 Andrea Spadaccini
396 2d88fdd3 Andrea Spadaccini
@RunLocalHooks(constants.FAKE_OP_MASTER_TURNUP, "master-ip-turnup",
397 3a3e4f1e Andrea Spadaccini
398 57c7bc57 Andrea Spadaccini
def ActivateMasterIp(master_params, use_external_mip_script):
399 fb460cf7 Andrea Spadaccini
  """Activate the IP address of the master daemon.
400 fb460cf7 Andrea Spadaccini

401 c79198a0 Andrea Spadaccini
  @type master_params: L{objects.MasterNetworkParameters}
402 c79198a0 Andrea Spadaccini
  @param master_params: network parameters of the master
403 57c7bc57 Andrea Spadaccini
  @type use_external_mip_script: boolean
404 57c7bc57 Andrea Spadaccini
  @param use_external_mip_script: whether to use an external master IP
405 57c7bc57 Andrea Spadaccini
    address setup script
406 702eff21 Andrea Spadaccini
  @raise RPCFail: in case of errors during the IP startup
407 8da2bd43 Andrea Spadaccini

408 fb460cf7 Andrea Spadaccini
409 702eff21 Andrea Spadaccini
  _RunMasterSetupScript(master_params, _MASTER_START,
410 702eff21 Andrea Spadaccini
411 fb460cf7 Andrea Spadaccini
412 fb460cf7 Andrea Spadaccini
413 fb460cf7 Andrea Spadaccini
def StartMasterDaemons(no_voting):
414 a8083063 Iustin Pop
  """Activate local node as master node.
415 a8083063 Iustin Pop

416 fb460cf7 Andrea Spadaccini
  The function will start the master daemons (ganeti-masterd and ganeti-rapi).
417 10c2650b Iustin Pop

418 3583908a Guido Trotter
  @type no_voting: boolean
419 3583908a Guido Trotter
  @param no_voting: whether to start ganeti-masterd without a node vote
420 fb460cf7 Andrea Spadaccini
      but still non-interactively
421 10c2650b Iustin Pop
  @rtype: None
422 a8083063 Iustin Pop

423 a8083063 Iustin Pop
424 a8083063 Iustin Pop
425 fb460cf7 Andrea Spadaccini
  if no_voting:
426 fb460cf7 Andrea Spadaccini
    masterd_args = "--no-voting --yes-do-it"
427 fb460cf7 Andrea Spadaccini
428 fb460cf7 Andrea Spadaccini
    masterd_args = ""
429 f154a7a3 Michael Hanselmann
430 fb460cf7 Andrea Spadaccini
  env = {
431 fb460cf7 Andrea Spadaccini
    "EXTRA_MASTERD_ARGS": masterd_args,
432 fb460cf7 Andrea Spadaccini
433 fb460cf7 Andrea Spadaccini
434 710f30ec Michael Hanselmann
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "start-master"], env=env)
435 fb460cf7 Andrea Spadaccini
  if result.failed:
436 fb460cf7 Andrea Spadaccini
    msg = "Can't start Ganeti master: %s" % result.output
437 fb460cf7 Andrea Spadaccini
438 fb460cf7 Andrea Spadaccini
439 f154a7a3 Michael Hanselmann
440 fb460cf7 Andrea Spadaccini
441 2d88fdd3 Andrea Spadaccini
@RunLocalHooks(constants.FAKE_OP_MASTER_TURNDOWN, "master-ip-turndown",
442 3a3e4f1e Andrea Spadaccini
443 57c7bc57 Andrea Spadaccini
def DeactivateMasterIp(master_params, use_external_mip_script):
444 fb460cf7 Andrea Spadaccini
  """Deactivate the master IP on this node.
445 a8083063 Iustin Pop

446 c79198a0 Andrea Spadaccini
  @type master_params: L{objects.MasterNetworkParameters}
447 c79198a0 Andrea Spadaccini
  @param master_params: network parameters of the master
448 57c7bc57 Andrea Spadaccini
  @type use_external_mip_script: boolean
449 57c7bc57 Andrea Spadaccini
  @param use_external_mip_script: whether to use an external master IP
450 57c7bc57 Andrea Spadaccini
    address setup script
451 702eff21 Andrea Spadaccini
  @raise RPCFail: in case of errors during the IP turndown
452 96e0d5cc Andrea Spadaccini

453 a8083063 Iustin Pop
454 702eff21 Andrea Spadaccini
  _RunMasterSetupScript(master_params, _MASTER_STOP,
455 702eff21 Andrea Spadaccini
456 b1b6ea87 Iustin Pop
457 fb460cf7 Andrea Spadaccini
458 fb460cf7 Andrea Spadaccini
def StopMasterDaemons():
459 fb460cf7 Andrea Spadaccini
  """Stop the master daemons on this node.
460 fb460cf7 Andrea Spadaccini

461 fb460cf7 Andrea Spadaccini
  Stop the master daemons (ganeti-masterd and ganeti-rapi) on this node.
462 fb460cf7 Andrea Spadaccini

463 fb460cf7 Andrea Spadaccini
  @rtype: None
464 fb460cf7 Andrea Spadaccini

465 fb460cf7 Andrea Spadaccini
466 fb460cf7 Andrea Spadaccini
  # TODO: log and report back to the caller the error failures; we
467 fb460cf7 Andrea Spadaccini
  # need to decide in which case we fail the RPC for this
468 fb460cf7 Andrea Spadaccini
469 710f30ec Michael Hanselmann
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop-master"])
470 fb460cf7 Andrea Spadaccini
  if result.failed:
471 fb460cf7 Andrea Spadaccini
    logging.error("Could not stop Ganeti master, command %s had exitcode %s"
472 fb460cf7 Andrea Spadaccini
                  " and error %s",
473 fb460cf7 Andrea Spadaccini
                  result.cmd, result.exit_code, result.output)
474 a8083063 Iustin Pop
475 a8083063 Iustin Pop
476 41e079ce Andrea Spadaccini
def ChangeMasterNetmask(old_netmask, netmask, master_ip, master_netdev):
477 5a8648eb Andrea Spadaccini
  """Change the netmask of the master IP.
478 5a8648eb Andrea Spadaccini

479 41e079ce Andrea Spadaccini
  @param old_netmask: the old value of the netmask
480 41e079ce Andrea Spadaccini
  @param netmask: the new value of the netmask
481 41e079ce Andrea Spadaccini
  @param master_ip: the master IP
482 41e079ce Andrea Spadaccini
  @param master_netdev: the master network device
483 41e079ce Andrea Spadaccini

484 5a8648eb Andrea Spadaccini
485 5a8648eb Andrea Spadaccini
  if old_netmask == netmask:
486 5a8648eb Andrea Spadaccini
487 5a8648eb Andrea Spadaccini
488 9e6014b9 Andrea Spadaccini
  if not netutils.IPAddress.Own(master_ip):
489 9e6014b9 Andrea Spadaccini
    _Fail("The master IP address is not up, not attempting to change its"
490 9e6014b9 Andrea Spadaccini
          " netmask")
491 9e6014b9 Andrea Spadaccini
492 5a8648eb Andrea Spadaccini
  result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "add",
493 5a8648eb Andrea Spadaccini
                         "%s/%s" % (master_ip, netmask),
494 5a8648eb Andrea Spadaccini
                         "dev", master_netdev, "label",
495 5a8648eb Andrea Spadaccini
                         "%s:0" % master_netdev])
496 5a8648eb Andrea Spadaccini
  if result.failed:
497 9e6014b9 Andrea Spadaccini
    _Fail("Could not set the new netmask on the master IP address")
498 5a8648eb Andrea Spadaccini
499 5a8648eb Andrea Spadaccini
  result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "del",
500 5a8648eb Andrea Spadaccini
                         "%s/%s" % (master_ip, old_netmask),
501 5a8648eb Andrea Spadaccini
                         "dev", master_netdev, "label",
502 5a8648eb Andrea Spadaccini
                         "%s:0" % master_netdev])
503 5a8648eb Andrea Spadaccini
  if result.failed:
504 9e6014b9 Andrea Spadaccini
    _Fail("Could not bring down the master IP address with the old netmask")
505 5a8648eb Andrea Spadaccini
506 5a8648eb Andrea Spadaccini
507 19ddc57a René Nussbaumer
def EtcHostsModify(mode, host, ip):
508 19ddc57a René Nussbaumer
  """Modify a host entry in /etc/hosts.
509 19ddc57a René Nussbaumer

510 19ddc57a René Nussbaumer
  @param mode: The mode to operate. Either add or remove entry
511 19ddc57a René Nussbaumer
  @param host: The host to operate on
512 19ddc57a René Nussbaumer
  @param ip: The ip associated with the entry
513 19ddc57a René Nussbaumer

514 19ddc57a René Nussbaumer
515 19ddc57a René Nussbaumer
  if mode == constants.ETC_HOSTS_ADD:
516 19ddc57a René Nussbaumer
    if not ip:
517 19ddc57a René Nussbaumer
      RPCFail("Mode 'add' needs 'ip' parameter, but parameter not"
518 19ddc57a René Nussbaumer
              " present")
519 19ddc57a René Nussbaumer
    utils.AddHostToEtcHosts(host, ip)
520 19ddc57a René Nussbaumer
  elif mode == constants.ETC_HOSTS_REMOVE:
521 19ddc57a René Nussbaumer
    if ip:
522 19ddc57a René Nussbaumer
      RPCFail("Mode 'remove' does not allow 'ip' parameter, but"
523 19ddc57a René Nussbaumer
              " parameter is present")
524 19ddc57a René Nussbaumer
525 19ddc57a René Nussbaumer
526 19ddc57a René Nussbaumer
    RPCFail("Mode not supported")
527 19ddc57a René Nussbaumer
528 19ddc57a René Nussbaumer
529 b989b9d9 Ken Wehr
def LeaveCluster(modify_ssh_setup):
530 10c2650b Iustin Pop
  """Cleans up and remove the current node.
531 10c2650b Iustin Pop

532 10c2650b Iustin Pop
  This function cleans up and prepares the current node to be removed
533 10c2650b Iustin Pop
  from the cluster.
534 10c2650b Iustin Pop

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

539 b989b9d9 Ken Wehr
  @param modify_ssh_setup: boolean
540 b989b9d9 Ken Wehr

541 a8083063 Iustin Pop
542 710f30ec Michael Hanselmann
543 710f30ec Michael Hanselmann
544 1bc59f76 Michael Hanselmann
545 f78346f5 Michael Hanselmann
546 b989b9d9 Ken Wehr
  if modify_ssh_setup:
547 b989b9d9 Ken Wehr
548 052783ff Michael Hanselmann
      priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.SSH_LOGIN_USER)
549 7900ed01 Iustin Pop
550 b989b9d9 Ken Wehr
      utils.RemoveAuthorizedKey(auth_keys, utils.ReadFile(pub_key))
551 a8083063 Iustin Pop
552 b989b9d9 Ken Wehr
553 b989b9d9 Ken Wehr
554 b989b9d9 Ken Wehr
    except errors.OpExecError:
555 b989b9d9 Ken Wehr
      logging.exception("Error while processing ssh files")
556 a8083063 Iustin Pop
557 ed008420 Guido Trotter
558 710f30ec Michael Hanselmann
559 710f30ec Michael Hanselmann
560 710f30ec Michael Hanselmann
561 710f30ec Michael Hanselmann
562 710f30ec Michael Hanselmann
563 b459a848 Andrea Spadaccini
  except: # pylint: disable=W0702
564 ed008420 Guido Trotter
    logging.exception("Error while removing cluster secrets")
565 ed008420 Guido Trotter
566 710f30ec Michael Hanselmann
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop", constants.CONFD])
567 f154a7a3 Michael Hanselmann
  if result.failed:
568 f154a7a3 Michael Hanselmann
    logging.error("Command %s failed with exitcode %s and error %s",
569 f154a7a3 Michael Hanselmann
                  result.cmd, result.exit_code, result.output)
570 ed008420 Guido Trotter
571 0623d351 Iustin Pop
  # Raise a custom exception (handled in ganeti-noded)
572 d0c8c01d Iustin Pop
  raise errors.QuitGanetiException(True, "Shutdown scheduled")
573 6d8b6238 Guido Trotter
574 a8083063 Iustin Pop
575 1a3c5d4e Bernardo Dal Seno
def _GetVgInfo(name, excl_stor):
576 78519c10 Michael Hanselmann
  """Retrieves information about a LVM volume group.
577 78519c10 Michael Hanselmann

578 78519c10 Michael Hanselmann
579 78519c10 Michael Hanselmann
  # TODO: GetVGInfo supports returning information for multiple VGs at once
580 1a3c5d4e Bernardo Dal Seno
  vginfo = bdev.LogicalVolume.GetVGInfo([name], excl_stor)
581 78519c10 Michael Hanselmann
  if vginfo:
582 78519c10 Michael Hanselmann
    vg_free = int(round(vginfo[0][0], 0))
583 78519c10 Michael Hanselmann
    vg_size = int(round(vginfo[0][1], 0))
584 78519c10 Michael Hanselmann
585 78519c10 Michael Hanselmann
    vg_free = None
586 78519c10 Michael Hanselmann
    vg_size = None
587 78519c10 Michael Hanselmann
588 78519c10 Michael Hanselmann
  return {
589 78519c10 Michael Hanselmann
    "name": name,
590 1e89a135 Michael Hanselmann
    "vg_free": vg_free,
591 1e89a135 Michael Hanselmann
    "vg_size": vg_size,
592 78519c10 Michael Hanselmann
593 78519c10 Michael Hanselmann
594 78519c10 Michael Hanselmann
595 78519c10 Michael Hanselmann
def _GetHvInfo(name):
596 78519c10 Michael Hanselmann
  """Retrieves node information from a hypervisor.
597 78519c10 Michael Hanselmann

598 78519c10 Michael Hanselmann
  The information returned depends on the hypervisor. Common items:
599 78519c10 Michael Hanselmann

600 78519c10 Michael Hanselmann
    - vg_size is the size of the configured volume group in MiB
601 78519c10 Michael Hanselmann
    - vg_free is the free size of the volume group in MiB
602 78519c10 Michael Hanselmann
    - memory_dom0 is the memory allocated for domain0 in MiB
603 78519c10 Michael Hanselmann
    - memory_free is the currently available (free) ram in MiB
604 78519c10 Michael Hanselmann
    - memory_total is the total number of ram in MiB
605 78519c10 Michael Hanselmann
    - hv_version: the hypervisor version, if available
606 78519c10 Michael Hanselmann

607 78519c10 Michael Hanselmann
608 78519c10 Michael Hanselmann
  return hypervisor.GetHypervisor(name).GetNodeInfo()
609 78519c10 Michael Hanselmann
610 78519c10 Michael Hanselmann
611 78519c10 Michael Hanselmann
def _GetNamedNodeInfo(names, fn):
612 78519c10 Michael Hanselmann
  """Calls C{fn} for all names in C{names} and returns a dictionary.
613 78519c10 Michael Hanselmann

614 78519c10 Michael Hanselmann
  @rtype: None or dict
615 78519c10 Michael Hanselmann

616 78519c10 Michael Hanselmann
617 78519c10 Michael Hanselmann
  if names is None:
618 78519c10 Michael Hanselmann
    return None
619 78519c10 Michael Hanselmann
620 ff3be305 Michael Hanselmann
    return map(fn, names)
621 78519c10 Michael Hanselmann
622 78519c10 Michael Hanselmann
623 4b92e992 Helga Velroyen
def GetNodeInfo(storage_units, hv_names, excl_stor):
624 5bbd3f7f Michael Hanselmann
  """Gives back a hash with different information about the node.
625 a8083063 Iustin Pop

626 4b92e992 Helga Velroyen
  @type storage_units: list of pairs (string, string)
627 4b92e992 Helga Velroyen
  @param storage_units: List of pairs (storage unit, identifier) to ask for disk
628 4b92e992 Helga Velroyen
                        space information. In case of lvm-vg, the identifier is
629 4b92e992 Helga Velroyen
                        the VG name.
630 78519c10 Michael Hanselmann
  @type hv_names: list of string
631 78519c10 Michael Hanselmann
  @param hv_names: Names of the hypervisors to ask for node information
632 1a3c5d4e Bernardo Dal Seno
  @type excl_stor: boolean
633 1a3c5d4e Bernardo Dal Seno
  @param excl_stor: Whether exclusive_storage is active
634 78519c10 Michael Hanselmann
  @rtype: tuple; (string, None/dict, None/dict)
635 78519c10 Michael Hanselmann
  @return: Tuple containing boot ID, volume group information and hypervisor
636 78519c10 Michael Hanselmann
637 a8083063 Iustin Pop

638 098c0958 Michael Hanselmann
639 78519c10 Michael Hanselmann
  bootid = utils.ReadFile(_BOOT_ID_PATH, size=128).rstrip("\n")
640 4b92e992 Helga Velroyen
  storage_info = _GetNamedNodeInfo(
641 4b92e992 Helga Velroyen
642 4b92e992 Helga Velroyen
    (lambda storage_unit: _ApplyStorageInfoFunction(storage_unit[0],
643 4b92e992 Helga Velroyen
644 4b92e992 Helga Velroyen
645 78519c10 Michael Hanselmann
  hv_info = _GetNamedNodeInfo(hv_names, _GetHvInfo)
646 78519c10 Michael Hanselmann
647 4b92e992 Helga Velroyen
  return (bootid, storage_info, hv_info)
648 4b92e992 Helga Velroyen
649 4b92e992 Helga Velroyen
650 4b92e992 Helga Velroyen
# FIXME: implement storage reporting for all missing storage types.
651 4b92e992 Helga Velroyen
652 4b92e992 Helga Velroyen
  constants.ST_BLOCK: None,
653 4b92e992 Helga Velroyen
  constants.ST_DISKLESS: None,
654 4b92e992 Helga Velroyen
  constants.ST_EXT: None,
655 4b92e992 Helga Velroyen
  constants.ST_FILE: None,
656 4b92e992 Helga Velroyen
  constants.ST_LVM_VG: _GetVgInfo,
657 4b92e992 Helga Velroyen
  constants.ST_RADOS: None,
658 4b92e992 Helga Velroyen
659 4b92e992 Helga Velroyen
660 4b92e992 Helga Velroyen
661 4b92e992 Helga Velroyen
def _ApplyStorageInfoFunction(storage_type, storage_key, *args):
662 4b92e992 Helga Velroyen
  """Looks up and applies the correct function to calculate free and total
663 4b92e992 Helga Velroyen
  storage for the given storage type.
664 4b92e992 Helga Velroyen

665 4b92e992 Helga Velroyen
  @type storage_type: string
666 4b92e992 Helga Velroyen
  @param storage_type: the storage type for which the storage shall be reported.
667 4b92e992 Helga Velroyen
  @type storage_key: string
668 4b92e992 Helga Velroyen
  @param storage_key: identifier of a storage unit, e.g. the volume group name
669 4b92e992 Helga Velroyen
    of an LVM storage unit
670 4b92e992 Helga Velroyen
  @type args: any
671 4b92e992 Helga Velroyen
  @param args: various parameters that can be used for storage reporting. These
672 4b92e992 Helga Velroyen
    parameters and their semantics vary from storage type to storage type and
673 4b92e992 Helga Velroyen
    are just propagated in this function.
674 4b92e992 Helga Velroyen
  @return: the results of the application of the storage space function (see
675 4b92e992 Helga Velroyen
    _STORAGE_TYPE_INFO_FN) if storage space reporting is implemented for that
676 4b92e992 Helga Velroyen
    storage type
677 4b92e992 Helga Velroyen
  @raises NotImplementedError: for storage types who don't support space
678 4b92e992 Helga Velroyen
    reporting yet
679 4b92e992 Helga Velroyen
680 4b92e992 Helga Velroyen
  fn = _STORAGE_TYPE_INFO_FN[storage_type]
681 4b92e992 Helga Velroyen
  if fn is not None:
682 4b92e992 Helga Velroyen
    return fn(storage_key, *args)
683 4b92e992 Helga Velroyen
684 4b92e992 Helga Velroyen
    raise NotImplementedError
685 a8083063 Iustin Pop
686 a8083063 Iustin Pop
687 d5a690cb Bernardo Dal Seno
def _CheckExclusivePvs(pvi_list):
688 d5a690cb Bernardo Dal Seno
  """Check that PVs are not shared among LVs
689 d5a690cb Bernardo Dal Seno

690 d5a690cb Bernardo Dal Seno
  @type pvi_list: list of L{objects.LvmPvInfo} objects
691 d5a690cb Bernardo Dal Seno
  @param pvi_list: information about the PVs
692 d5a690cb Bernardo Dal Seno

693 d5a690cb Bernardo Dal Seno
  @rtype: list of tuples (string, list of strings)
694 d5a690cb Bernardo Dal Seno
  @return: offending volumes, as tuples: (pv_name, [lv1_name, lv2_name...])
695 d5a690cb Bernardo Dal Seno

696 d5a690cb Bernardo Dal Seno
697 d5a690cb Bernardo Dal Seno
  res = []
698 d5a690cb Bernardo Dal Seno
  for pvi in pvi_list:
699 d5a690cb Bernardo Dal Seno
    if len(pvi.lv_list) > 1:
700 d5a690cb Bernardo Dal Seno
      res.append((, pvi.lv_list))
701 d5a690cb Bernardo Dal Seno
  return res
702 d5a690cb Bernardo Dal Seno
703 d5a690cb Bernardo Dal Seno
704 62c9ec92 Iustin Pop
def VerifyNode(what, cluster_name):
705 a8083063 Iustin Pop
  """Verify the status of the local node.
706 a8083063 Iustin Pop

707 e69d05fd Iustin Pop
  Based on the input L{what} parameter, various checks are done on the
708 e69d05fd Iustin Pop
  local node.
709 e69d05fd Iustin Pop

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

713 e69d05fd Iustin Pop
  If the I{nodelist} key is present, we check that we have
714 e69d05fd Iustin Pop
  connectivity via ssh with the target nodes (and check the hostname
715 e69d05fd Iustin Pop
716 a8083063 Iustin Pop

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

721 e69d05fd Iustin Pop
  @type what: C{dict}
722 e69d05fd Iustin Pop
  @param what: a dictionary of things to check:
723 e69d05fd Iustin Pop
      - filelist: list of files for which to compute checksums
724 e69d05fd Iustin Pop
      - nodelist: list of nodes we should check ssh communication with
725 e69d05fd Iustin Pop
      - node-net-test: list of nodes we should check node daemon port
726 e69d05fd Iustin Pop
        connectivity with
727 e69d05fd Iustin Pop
      - hypervisor: list with hypervisors to run the verify for
728 10c2650b Iustin Pop
  @rtype: dict
729 10c2650b Iustin Pop
  @return: a dictionary with the same keys as the input dict, and
730 10c2650b Iustin Pop
      values representing the result of the checks
731 a8083063 Iustin Pop

732 a8083063 Iustin Pop
733 a8083063 Iustin Pop
  result = {}
734 b705c7a6 Manuel Franceschini
  my_name = netutils.Hostname.GetSysName()
735 a744b676 Manuel Franceschini
  port = netutils.GetDaemonPort(constants.NODED)
736 8964ee14 Iustin Pop
  vm_capable = my_name not in what.get(constants.NV_VMNODES, [])
737 a8083063 Iustin Pop
738 8964ee14 Iustin Pop
  if constants.NV_HYPERVISOR in what and vm_capable:
739 25361b9a Iustin Pop
    result[constants.NV_HYPERVISOR] = tmp = {}
740 25361b9a Iustin Pop
    for hv_name in what[constants.NV_HYPERVISOR]:
741 0cf5e7f5 Iustin Pop
742 0cf5e7f5 Iustin Pop
        val = hypervisor.GetHypervisor(hv_name).Verify()
743 0cf5e7f5 Iustin Pop
      except errors.HypervisorError, err:
744 0cf5e7f5 Iustin Pop
        val = "Error while checking hypervisor: %s" % str(err)
745 0cf5e7f5 Iustin Pop
      tmp[hv_name] = val
746 25361b9a Iustin Pop
747 58a59652 Iustin Pop
  if constants.NV_HVPARAMS in what and vm_capable:
748 58a59652 Iustin Pop
    result[constants.NV_HVPARAMS] = tmp = []
749 58a59652 Iustin Pop
    for source, hv_name, hvparms in what[constants.NV_HVPARAMS]:
750 58a59652 Iustin Pop
751 58a59652 Iustin Pop"Validating hv %s, %s", hv_name, hvparms)
752 58a59652 Iustin Pop
753 58a59652 Iustin Pop
      except errors.HypervisorError, err:
754 58a59652 Iustin Pop
        tmp.append((source, hv_name, str(err)))
755 58a59652 Iustin Pop
756 25361b9a Iustin Pop
  if constants.NV_FILELIST in what:
757 47130d50 Michael Hanselmann
    fingerprints = utils.FingerprintFiles(map(vcluster.LocalizeVirtualPath,
758 47130d50 Michael Hanselmann
759 47130d50 Michael Hanselmann
    result[constants.NV_FILELIST] = \
760 47130d50 Michael Hanselmann
      dict((vcluster.MakeVirtualPath(key), value)
761 47130d50 Michael Hanselmann
           for (key, value) in fingerprints.items())
762 25361b9a Iustin Pop
763 25361b9a Iustin Pop
  if constants.NV_NODELIST in what:
764 64c7b383 Michael Hanselmann
    (nodes, bynode) = what[constants.NV_NODELIST]
765 64c7b383 Michael Hanselmann
766 64c7b383 Michael Hanselmann
    # Add nodes from other groups (different for each node)
767 64c7b383 Michael Hanselmann
768 64c7b383 Michael Hanselmann
769 64c7b383 Michael Hanselmann
    except KeyError:
770 64c7b383 Michael Hanselmann
771 64c7b383 Michael Hanselmann
772 64c7b383 Michael Hanselmann
    # Use a random order
773 64c7b383 Michael Hanselmann
774 64c7b383 Michael Hanselmann
775 64c7b383 Michael Hanselmann
    # Try to contact all nodes
776 64c7b383 Michael Hanselmann
    val = {}
777 64c7b383 Michael Hanselmann
    for node in nodes:
778 62c9ec92 Iustin Pop
      success, message = _GetSshRunner(cluster_name).VerifyNodeHostname(node)
779 a8083063 Iustin Pop
      if not success:
780 64c7b383 Michael Hanselmann
        val[node] = message
781 64c7b383 Michael Hanselmann
782 64c7b383 Michael Hanselmann
    result[constants.NV_NODELIST] = val
783 25361b9a Iustin Pop
784 25361b9a Iustin Pop
  if constants.NV_NODENETTEST in what:
785 25361b9a Iustin Pop
    result[constants.NV_NODENETTEST] = tmp = {}
786 9d4bfc96 Iustin Pop
    my_pip = my_sip = None
787 25361b9a Iustin Pop
    for name, pip, sip in what[constants.NV_NODENETTEST]:
788 9d4bfc96 Iustin Pop
      if name == my_name:
789 9d4bfc96 Iustin Pop
        my_pip = pip
790 9d4bfc96 Iustin Pop
        my_sip = sip
791 9d4bfc96 Iustin Pop
792 9d4bfc96 Iustin Pop
    if not my_pip:
793 25361b9a Iustin Pop
      tmp[my_name] = ("Can't find my own primary/secondary IP"
794 25361b9a Iustin Pop
                      " in the node list")
795 9d4bfc96 Iustin Pop
796 25361b9a Iustin Pop
      for name, pip, sip in what[constants.NV_NODENETTEST]:
797 9d4bfc96 Iustin Pop
        fail = []
798 a744b676 Manuel Franceschini
        if not netutils.TcpPing(pip, port, source=my_pip):
799 9d4bfc96 Iustin Pop
800 9d4bfc96 Iustin Pop
        if sip != pip:
801 a744b676 Manuel Franceschini
          if not netutils.TcpPing(sip, port, source=my_sip):
802 9d4bfc96 Iustin Pop
803 9d4bfc96 Iustin Pop
        if fail:
804 25361b9a Iustin Pop
          tmp[name] = ("failure using the %s interface(s)" %
805 25361b9a Iustin Pop
                       " and ".join(fail))
806 25361b9a Iustin Pop
807 a3a5f850 Iustin Pop
  if constants.NV_MASTERIP in what:
808 a3a5f850 Iustin Pop
    # FIXME: add checks on incoming data structures (here and in the
809 a3a5f850 Iustin Pop
    # rest of the function)
810 a3a5f850 Iustin Pop
    master_name, master_ip = what[constants.NV_MASTERIP]
811 a3a5f850 Iustin Pop
    if master_name == my_name:
812 9769bb78 Manuel Franceschini
      source = constants.IP4_ADDRESS_LOCALHOST
813 a3a5f850 Iustin Pop
814 a3a5f850 Iustin Pop
      source = None
815 a744b676 Manuel Franceschini
    result[constants.NV_MASTERIP] = netutils.TcpPing(master_ip, port,
816 5ae4945a Iustin Pop
817 a3a5f850 Iustin Pop
818 17b0b812 Andrea Spadaccini
  if constants.NV_USERSCRIPTS in what:
819 17b0b812 Andrea Spadaccini
    result[constants.NV_USERSCRIPTS] = \
820 17b0b812 Andrea Spadaccini
      [script for script in what[constants.NV_USERSCRIPTS]
821 10b86782 Michael Hanselmann
       if not utils.IsExecutable(script)]
822 17b0b812 Andrea Spadaccini
823 16f41f24 René Nussbaumer
  if constants.NV_OOB_PATHS in what:
824 16f41f24 René Nussbaumer
    result[constants.NV_OOB_PATHS] = tmp = []
825 16f41f24 René Nussbaumer
    for path in what[constants.NV_OOB_PATHS]:
826 16f41f24 René Nussbaumer
827 16f41f24 René Nussbaumer
        st = os.stat(path)
828 16f41f24 René Nussbaumer
      except OSError, err:
829 16f41f24 René Nussbaumer
        tmp.append("error stating out of band helper: %s" % err)
830 16f41f24 René Nussbaumer
831 16f41f24 René Nussbaumer
        if stat.S_ISREG(st.st_mode):
832 16f41f24 René Nussbaumer
          if stat.S_IMODE(st.st_mode) & stat.S_IXUSR:
833 16f41f24 René Nussbaumer
834 16f41f24 René Nussbaumer
835 16f41f24 René Nussbaumer
            tmp.append("out of band helper %s is not executable" % path)
836 16f41f24 René Nussbaumer
837 16f41f24 René Nussbaumer
          tmp.append("out of band helper %s is not a file" % path)
838 16f41f24 René Nussbaumer
839 8964ee14 Iustin Pop
  if constants.NV_LVLIST in what and vm_capable:
840 ed904904 Iustin Pop
841 84d7e26b Dmitry Chernyak
      val = GetVolumeList(utils.ListVolumeGroups().keys())
842 ed904904 Iustin Pop
    except RPCFail, err:
843 ed904904 Iustin Pop
      val = str(err)
844 ed904904 Iustin Pop
    result[constants.NV_LVLIST] = val
845 25361b9a Iustin Pop
846 8964ee14 Iustin Pop
  if constants.NV_INSTANCELIST in what and vm_capable:
847 0cf5e7f5 Iustin Pop
    # GetInstanceList can fail
848 0cf5e7f5 Iustin Pop
849 0cf5e7f5 Iustin Pop
      val = GetInstanceList(what[constants.NV_INSTANCELIST])
850 0cf5e7f5 Iustin Pop
    except RPCFail, err:
851 0cf5e7f5 Iustin Pop
      val = str(err)
852 0cf5e7f5 Iustin Pop
    result[constants.NV_INSTANCELIST] = val
853 25361b9a Iustin Pop
854 8964ee14 Iustin Pop
  if constants.NV_VGLIST in what and vm_capable:
855 e480923b Iustin Pop
    result[constants.NV_VGLIST] = utils.ListVolumeGroups()
856 25361b9a Iustin Pop
857 8964ee14 Iustin Pop
  if constants.NV_PVLIST in what and vm_capable:
858 d5a690cb Bernardo Dal Seno
    check_exclusive_pvs = constants.NV_EXCLUSIVEPVS in what
859 59726e15 Bernardo Dal Seno
    val = bdev.LogicalVolume.GetPVInfo(what[constants.NV_PVLIST],
860 d5a690cb Bernardo Dal Seno
861 d5a690cb Bernardo Dal Seno
862 d5a690cb Bernardo Dal Seno
    if check_exclusive_pvs:
863 d5a690cb Bernardo Dal Seno
      result[constants.NV_EXCLUSIVEPVS] = _CheckExclusivePvs(val)
864 d5a690cb Bernardo Dal Seno
      for pvi in val:
865 d5a690cb Bernardo Dal Seno
        # Avoid sending useless data on the wire
866 d5a690cb Bernardo Dal Seno
        pvi.lv_list = []
867 59726e15 Bernardo Dal Seno
    result[constants.NV_PVLIST] = map(objects.LvmPvInfo.ToDict, val)
868 d091393e Iustin Pop
869 25361b9a Iustin Pop
  if constants.NV_VERSION in what:
870 e9ce0a64 Iustin Pop
    result[constants.NV_VERSION] = (constants.PROTOCOL_VERSION,
871 e9ce0a64 Iustin Pop
872 25361b9a Iustin Pop
873 8964ee14 Iustin Pop
  if constants.NV_HVINFO in what and vm_capable:
874 25361b9a Iustin Pop
    hyper = hypervisor.GetHypervisor(what[constants.NV_HVINFO])
875 25361b9a Iustin Pop
    result[constants.NV_HVINFO] = hyper.GetNodeInfo()
876 9d4bfc96 Iustin Pop
877 5bb0a1cb Thomas Thrainer
  if constants.NV_DRBDVERSION in what and vm_capable:
878 5bb0a1cb Thomas Thrainer
879 47e0abee Thomas Thrainer
      drbd_version = DRBD8.GetProcInfo().GetVersionString()
880 5bb0a1cb Thomas Thrainer
    except errors.BlockDeviceError, err:
881 5bb0a1cb Thomas Thrainer
      logging.warning("Can't get DRBD version", exc_info=True)
882 5bb0a1cb Thomas Thrainer
      drbd_version = str(err)
883 5bb0a1cb Thomas Thrainer
    result[constants.NV_DRBDVERSION] = drbd_version
884 5bb0a1cb Thomas Thrainer
885 8964ee14 Iustin Pop
  if constants.NV_DRBDLIST in what and vm_capable:
886 6d2e83d5 Iustin Pop
887 47e0abee Thomas Thrainer
      used_minors = drbd.DRBD8.GetUsedDevs()
888 f6eaed12 Iustin Pop
    except errors.BlockDeviceError, err:
889 6d2e83d5 Iustin Pop
      logging.warning("Can't get used minors list", exc_info=True)
890 f6eaed12 Iustin Pop
      used_minors = str(err)
891 6d2e83d5 Iustin Pop
    result[constants.NV_DRBDLIST] = used_minors
892 6d2e83d5 Iustin Pop
893 8964ee14 Iustin Pop
  if constants.NV_DRBDHELPER in what and vm_capable:
894 7ef40fbe Luca Bigliardi
    status = True
895 7ef40fbe Luca Bigliardi
896 47e0abee Thomas Thrainer
      payload = drbd.DRBD8.GetUsermodeHelper()
897 7ef40fbe Luca Bigliardi
    except errors.BlockDeviceError, err:
898 7ef40fbe Luca Bigliardi
      logging.error("Can't get DRBD usermode helper: %s", str(err))
899 7ef40fbe Luca Bigliardi
      status = False
900 7ef40fbe Luca Bigliardi
      payload = str(err)
901 7ef40fbe Luca Bigliardi
    result[constants.NV_DRBDHELPER] = (status, payload)
902 7ef40fbe Luca Bigliardi
903 7c0aa8e9 Iustin Pop
  if constants.NV_NODESETUP in what:
904 7c0aa8e9 Iustin Pop
    result[constants.NV_NODESETUP] = tmpr = []
905 7c0aa8e9 Iustin Pop
    if not os.path.isdir("/sys/block") or not os.path.isdir("/sys/class/net"):
906 7c0aa8e9 Iustin Pop
      tmpr.append("The sysfs filesytem doesn't seem to be mounted"
907 7c0aa8e9 Iustin Pop
                  " under /sys, missing required directories /sys/block"
908 7c0aa8e9 Iustin Pop
                  " and /sys/class/net")
909 7c0aa8e9 Iustin Pop
    if (not os.path.isdir("/proc/sys") or
910 7c0aa8e9 Iustin Pop
        not os.path.isfile("/proc/sysrq-trigger")):
911 7c0aa8e9 Iustin Pop
      tmpr.append("The procfs filesystem doesn't seem to be mounted"
912 7c0aa8e9 Iustin Pop
                  " under /proc, missing required directory /proc/sys and"
913 7c0aa8e9 Iustin Pop
                  " the file /proc/sysrq-trigger")
914 313b2dd4 Michael Hanselmann
915 313b2dd4 Michael Hanselmann
  if constants.NV_TIME in what:
916 313b2dd4 Michael Hanselmann
    result[constants.NV_TIME] = utils.SplitTime(time.time())
917 313b2dd4 Michael Hanselmann
918 8964ee14 Iustin Pop
  if constants.NV_OSLIST in what and vm_capable:
919 b0d85178 Iustin Pop
    result[constants.NV_OSLIST] = DiagnoseOS()
920 b0d85178 Iustin Pop
921 20d317d4 Iustin Pop
  if constants.NV_BRIDGES in what and vm_capable:
922 20d317d4 Iustin Pop
    result[constants.NV_BRIDGES] = [bridge
923 20d317d4 Iustin Pop
                                    for bridge in what[constants.NV_BRIDGES]
924 20d317d4 Iustin Pop
                                    if not utils.BridgeExists(bridge)]
925 23e3c9b7 Michael Hanselmann
926 72b35807 Michael Hanselmann
  if what.get(constants.NV_FILE_STORAGE_PATHS) == my_name:
927 72b35807 Michael Hanselmann
    result[constants.NV_FILE_STORAGE_PATHS] = \
928 72b35807 Michael Hanselmann
929 72b35807 Michael Hanselmann
930 c26a6bd2 Iustin Pop
  return result
931 a8083063 Iustin Pop
932 a8083063 Iustin Pop
933 2be7273c Apollon Oikonomopoulos
def GetBlockDevSizes(devices):
934 2be7273c Apollon Oikonomopoulos
  """Return the size of the given block devices
935 2be7273c Apollon Oikonomopoulos

936 2be7273c Apollon Oikonomopoulos
  @type devices: list
937 2be7273c Apollon Oikonomopoulos
  @param devices: list of block device nodes to query
938 2be7273c Apollon Oikonomopoulos
  @rtype: dict
939 2be7273c Apollon Oikonomopoulos
940 2be7273c Apollon Oikonomopoulos
    dictionary of all block devices under /dev (key). The value is their
941 2be7273c Apollon Oikonomopoulos
    size in MiB.
942 2be7273c Apollon Oikonomopoulos

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

945 2be7273c Apollon Oikonomopoulos
946 2be7273c Apollon Oikonomopoulos
  DEV_PREFIX = "/dev/"
947 2be7273c Apollon Oikonomopoulos
  blockdevs = {}
948 2be7273c Apollon Oikonomopoulos
949 2be7273c Apollon Oikonomopoulos
  for devpath in devices:
950 cf00dba0 René Nussbaumer
    if not utils.IsBelowDir(DEV_PREFIX, devpath):
951 2be7273c Apollon Oikonomopoulos
952 2be7273c Apollon Oikonomopoulos
953 2be7273c Apollon Oikonomopoulos
954 2be7273c Apollon Oikonomopoulos
      st = os.stat(devpath)
955 2be7273c Apollon Oikonomopoulos
    except EnvironmentError, err:
956 2be7273c Apollon Oikonomopoulos
      logging.warning("Error stat()'ing device %s: %s", devpath, str(err))
957 2be7273c Apollon Oikonomopoulos
958 2be7273c Apollon Oikonomopoulos
959 2be7273c Apollon Oikonomopoulos
    if stat.S_ISBLK(st.st_mode):
960 2be7273c Apollon Oikonomopoulos
      result = utils.RunCmd(["blockdev", "--getsize64", devpath])
961 2be7273c Apollon Oikonomopoulos
      if result.failed:
962 2be7273c Apollon Oikonomopoulos
        # We don't want to fail, just do not list this device as available
963 2be7273c Apollon Oikonomopoulos
        logging.warning("Cannot get size for block device %s", devpath)
964 2be7273c Apollon Oikonomopoulos
965 2be7273c Apollon Oikonomopoulos
966 2be7273c Apollon Oikonomopoulos
      size = int(result.stdout) / (1024 * 1024)
967 2be7273c Apollon Oikonomopoulos
      blockdevs[devpath] = size
968 2be7273c Apollon Oikonomopoulos
  return blockdevs
969 2be7273c Apollon Oikonomopoulos
970 2be7273c Apollon Oikonomopoulos
971 84d7e26b Dmitry Chernyak
def GetVolumeList(vg_names):
972 a8083063 Iustin Pop
  """Compute list of logical volumes and their size.
973 a8083063 Iustin Pop

974 84d7e26b Dmitry Chernyak
  @type vg_names: list
975 397693d3 Iustin Pop
  @param vg_names: the volume groups whose LVs we should list, or
976 397693d3 Iustin Pop
      empty for all volume groups
977 10c2650b Iustin Pop
  @rtype: dict
978 10c2650b Iustin Pop
979 10c2650b Iustin Pop
      dictionary of all partions (key) with value being a tuple of
980 10c2650b Iustin Pop
      their size (in MiB), inactive and online status::
981 10c2650b Iustin Pop

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

984 10c2650b Iustin Pop
      in case of errors, a string is returned with the error
985 10c2650b Iustin Pop
986 a8083063 Iustin Pop

987 a8083063 Iustin Pop
988 cb2037a2 Iustin Pop
  lvs = {}
989 d0c8c01d Iustin Pop
  sep = "|"
990 397693d3 Iustin Pop
  if not vg_names:
991 397693d3 Iustin Pop
    vg_names = []
992 cb2037a2 Iustin Pop
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
993 cb2037a2 Iustin Pop
                         "--separator=%s" % sep,
994 84d7e26b Dmitry Chernyak
                         "-ovg_name,lv_name,lv_size,lv_attr"] + vg_names)
995 a8083063 Iustin Pop
  if result.failed:
996 29d376ec Iustin Pop
    _Fail("Failed to list logical volumes, lvs output: %s", result.output)
997 cb2037a2 Iustin Pop
998 cb2037a2 Iustin Pop
  for line in result.stdout.splitlines():
999 df4c2628 Iustin Pop
    line = line.strip()
1000 0b5303da Iustin Pop
    match = _LVSLINE_REGEX.match(line)
1001 df4c2628 Iustin Pop
    if not match:
1002 18682bca Iustin Pop
      logging.error("Invalid line returned from lvs output: '%s'", line)
1003 df4c2628 Iustin Pop
1004 84d7e26b Dmitry Chernyak
    vg_name, name, size, attr = match.groups()
1005 d0c8c01d Iustin Pop
    inactive = attr[4] == "-"
1006 d0c8c01d Iustin Pop
    online = attr[5] == "o"
1007 d0c8c01d Iustin Pop
    virtual = attr[0] == "v"
1008 33f2a81a Iustin Pop
    if virtual:
1009 33f2a81a Iustin Pop
      # we don't want to report such volumes as existing, since they
1010 33f2a81a Iustin Pop
      # don't really hold data
1011 33f2a81a Iustin Pop
1012 e687ec01 Michael Hanselmann
    lvs[vg_name + "/" + name] = (size, inactive, online)
1013 cb2037a2 Iustin Pop
1014 cb2037a2 Iustin Pop
  return lvs
1015 a8083063 Iustin Pop
1016 a8083063 Iustin Pop
1017 a8083063 Iustin Pop
def ListVolumeGroups():
1018 2f8598a5 Alexander Schreiber
  """List the volume groups and their size.
1019 a8083063 Iustin Pop

1020 10c2650b Iustin Pop
  @rtype: dict
1021 10c2650b Iustin Pop
  @return: dictionary with keys volume name and values the
1022 10c2650b Iustin Pop
      size of the volume
1023 a8083063 Iustin Pop

1024 a8083063 Iustin Pop
1025 c26a6bd2 Iustin Pop
  return utils.ListVolumeGroups()
1026 a8083063 Iustin Pop
1027 a8083063 Iustin Pop
1028 dcb93971 Michael Hanselmann
def NodeVolumes():
1029 dcb93971 Michael Hanselmann
  """List all volumes on this node.
1030 dcb93971 Michael Hanselmann

1031 10c2650b Iustin Pop
  @rtype: list
1032 10c2650b Iustin Pop
1033 10c2650b Iustin Pop
    A list of dictionaries, each having four keys:
1034 10c2650b Iustin Pop
      - name: the logical volume name,
1035 10c2650b Iustin Pop
      - size: the size of the logical volume
1036 10c2650b Iustin Pop
      - dev: the physical device on which the LV lives
1037 10c2650b Iustin Pop
      - vg: the volume group to which it belongs
1038 10c2650b Iustin Pop

1039 10c2650b Iustin Pop
    In case of errors, we return an empty list and log the
1040 10c2650b Iustin Pop
1041 10c2650b Iustin Pop

1042 10c2650b Iustin Pop
    Note that since a logical volume can live on multiple physical
1043 10c2650b Iustin Pop
    volumes, the resulting list might include a logical volume
1044 10c2650b Iustin Pop
    multiple times.
1045 10c2650b Iustin Pop

1046 dcb93971 Michael Hanselmann
1047 dcb93971 Michael Hanselmann
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
1048 dcb93971 Michael Hanselmann
1049 dcb93971 Michael Hanselmann
1050 dcb93971 Michael Hanselmann
  if result.failed:
1051 10bfe6cb Iustin Pop
    _Fail("Failed to list logical volumes, lvs output: %s",
1052 10bfe6cb Iustin Pop
1053 dcb93971 Michael Hanselmann
1054 dcb93971 Michael Hanselmann
  def parse_dev(dev):
1055 d0c8c01d Iustin Pop
    return dev.split("(")[0]
1056 89e5ab02 Iustin Pop
1057 89e5ab02 Iustin Pop
  def handle_dev(dev):
1058 89e5ab02 Iustin Pop
    return [parse_dev(x) for x in dev.split(",")]
1059 dcb93971 Michael Hanselmann
1060 dcb93971 Michael Hanselmann
  def map_line(line):
1061 89e5ab02 Iustin Pop
    line = [v.strip() for v in line]
1062 d0c8c01d Iustin Pop
    return [{"name": line[0], "size": line[1],
1063 d0c8c01d Iustin Pop
             "dev": dev, "vg": line[3]} for dev in handle_dev(line[2])]
1064 89e5ab02 Iustin Pop
1065 89e5ab02 Iustin Pop
  all_devs = []
1066 89e5ab02 Iustin Pop
  for line in result.stdout.splitlines():
1067 d0c8c01d Iustin Pop
    if line.count("|") >= 3:
1068 d0c8c01d Iustin Pop
1069 89e5ab02 Iustin Pop
1070 89e5ab02 Iustin Pop
      logging.warning("Strange line in the output from lvs: '%s'", line)
1071 89e5ab02 Iustin Pop
  return all_devs
1072 dcb93971 Michael Hanselmann
1073 dcb93971 Michael Hanselmann
1074 a8083063 Iustin Pop
def BridgesExist(bridges_list):
1075 2f8598a5 Alexander Schreiber
  """Check if a list of bridges exist on the current node.
1076 a8083063 Iustin Pop

1077 b1206984 Iustin Pop
  @rtype: boolean
1078 b1206984 Iustin Pop
  @return: C{True} if all of them exist, C{False} otherwise
1079 a8083063 Iustin Pop

1080 a8083063 Iustin Pop
1081 35c0c8da Iustin Pop
  missing = []
1082 a8083063 Iustin Pop
  for bridge in bridges_list:
1083 a8083063 Iustin Pop
    if not utils.BridgeExists(bridge):
1084 35c0c8da Iustin Pop
1085 a8083063 Iustin Pop
1086 35c0c8da Iustin Pop
  if missing:
1087 1f864b60 Iustin Pop
    _Fail("Missing bridges %s", utils.CommaJoin(missing))
1088 35c0c8da Iustin Pop
1089 a8083063 Iustin Pop
1090 e69d05fd Iustin Pop
def GetInstanceList(hypervisor_list):
1091 2f8598a5 Alexander Schreiber
  """Provides a list of instances.
1092 a8083063 Iustin Pop

1093 e69d05fd Iustin Pop
  @type hypervisor_list: list
1094 e69d05fd Iustin Pop
  @param hypervisor_list: the list of hypervisors to query information
1095 e69d05fd Iustin Pop

1096 e69d05fd Iustin Pop
  @rtype: list
1097 e69d05fd Iustin Pop
  @return: a list of all running instances on the current node
1098 10c2650b Iustin Pop
1099 10c2650b Iustin Pop
1100 a8083063 Iustin Pop

1101 098c0958 Michael Hanselmann
1102 e69d05fd Iustin Pop
  results = []
1103 e69d05fd Iustin Pop
  for hname in hypervisor_list:
1104 e69d05fd Iustin Pop
1105 e69d05fd Iustin Pop
      names = hypervisor.GetHypervisor(hname).ListInstances()
1106 e69d05fd Iustin Pop
1107 e69d05fd Iustin Pop
    except errors.HypervisorError, err:
1108 aca13712 Iustin Pop
      _Fail("Error enumerating instances (hypervisor %s): %s",
1109 aca13712 Iustin Pop
            hname, err, exc=True)
1110 a8083063 Iustin Pop
1111 e69d05fd Iustin Pop
  return results
1112 a8083063 Iustin Pop
1113 a8083063 Iustin Pop
1114 e69d05fd Iustin Pop
def GetInstanceInfo(instance, hname):
1115 5bbd3f7f Michael Hanselmann
  """Gives back the information about an instance as a dictionary.
1116 a8083063 Iustin Pop

1117 e69d05fd Iustin Pop
  @type instance: string
1118 e69d05fd Iustin Pop
  @param instance: the instance name
1119 e69d05fd Iustin Pop
  @type hname: string
1120 e69d05fd Iustin Pop
  @param hname: the hypervisor type of the instance
1121 a8083063 Iustin Pop

1122 e69d05fd Iustin Pop
  @rtype: dict
1123 e69d05fd Iustin Pop
  @return: dictionary with the following keys:
1124 e69d05fd Iustin Pop
      - memory: memory size of instance (int)
1125 e69d05fd Iustin Pop
      - state: xen state of instance (string)
1126 e69d05fd Iustin Pop
      - time: cpu time of instance (float)
1127 1cb97324 Agata Murawska
      - vcpus: the number of vcpus (int)
1128 a8083063 Iustin Pop

1129 098c0958 Michael Hanselmann
1130 a8083063 Iustin Pop
  output = {}
1131 a8083063 Iustin Pop
1132 e69d05fd Iustin Pop
  iinfo = hypervisor.GetHypervisor(hname).GetInstanceInfo(instance)
1133 a8083063 Iustin Pop
  if iinfo is not None:
1134 d0c8c01d Iustin Pop
    output["memory"] = iinfo[2]
1135 1cb97324 Agata Murawska
    output["vcpus"] = iinfo[3]
1136 d0c8c01d Iustin Pop
    output["state"] = iinfo[4]
1137 d0c8c01d Iustin Pop
    output["time"] = iinfo[5]
1138 a8083063 Iustin Pop
1139 c26a6bd2 Iustin Pop
  return output
1140 a8083063 Iustin Pop
1141 a8083063 Iustin Pop
1142 56e7640c Iustin Pop
def GetInstanceMigratable(instance):
1143 56e7640c Iustin Pop
  """Gives whether an instance can be migrated.
1144 56e7640c Iustin Pop

1145 56e7640c Iustin Pop
  @type instance: L{objects.Instance}
1146 56e7640c Iustin Pop
  @param instance: object representing the instance to be checked.
1147 56e7640c Iustin Pop

1148 56e7640c Iustin Pop
  @rtype: tuple
1149 56e7640c Iustin Pop
  @return: tuple of (result, description) where:
1150 56e7640c Iustin Pop
      - result: whether the instance can be migrated or not
1151 56e7640c Iustin Pop
      - description: a description of the issue, if relevant
1152 56e7640c Iustin Pop

1153 56e7640c Iustin Pop
1154 56e7640c Iustin Pop
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1155 afdc3985 Iustin Pop
  iname =
1156 afdc3985 Iustin Pop
  if iname not in hyper.ListInstances():
1157 afdc3985 Iustin Pop
    _Fail("Instance %s is not running", iname)
1158 56e7640c Iustin Pop
1159 56e7640c Iustin Pop
  for idx in range(len(instance.disks)):
1160 afdc3985 Iustin Pop
    link_name = _GetBlockDevSymlinkPath(iname, idx)
1161 56e7640c Iustin Pop
    if not os.path.islink(link_name):
1162 b8ebd37b Iustin Pop
      logging.warning("Instance %s is missing symlink %s for disk %d",
1163 b8ebd37b Iustin Pop
                      iname, link_name, idx)
1164 56e7640c Iustin Pop
1165 56e7640c Iustin Pop
1166 e69d05fd Iustin Pop
def GetAllInstancesInfo(hypervisor_list):
1167 a8083063 Iustin Pop
  """Gather data about all instances.
1168 a8083063 Iustin Pop

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

1173 e69d05fd Iustin Pop
  @type hypervisor_list: list
1174 e69d05fd Iustin Pop
  @param hypervisor_list: list of hypervisors to query for instance data
1175 e69d05fd Iustin Pop

1176 955db481 Guido Trotter
  @rtype: dict
1177 e69d05fd Iustin Pop
  @return: dictionary of instance: data, with data having the following keys:
1178 e69d05fd Iustin Pop
      - memory: memory size of instance (int)
1179 e69d05fd Iustin Pop
      - state: xen state of instance (string)
1180 e69d05fd Iustin Pop
      - time: cpu time of instance (float)
1181 10c2650b Iustin Pop
      - vcpus: the number of vcpus
1182 a8083063 Iustin Pop

1183 098c0958 Michael Hanselmann
1184 a8083063 Iustin Pop
  output = {}
1185 a8083063 Iustin Pop
1186 e69d05fd Iustin Pop
  for hname in hypervisor_list:
1187 e69d05fd Iustin Pop
    iinfo = hypervisor.GetHypervisor(hname).GetAllInstancesInfo()
1188 e69d05fd Iustin Pop
    if iinfo:
1189 29921401 Iustin Pop
      for name, _, memory, vcpus, state, times in iinfo:
1190 f23b5ae8 Iustin Pop
        value = {
1191 d0c8c01d Iustin Pop
          "memory": memory,
1192 d0c8c01d Iustin Pop
          "vcpus": vcpus,
1193 d0c8c01d Iustin Pop
          "state": state,
1194 d0c8c01d Iustin Pop
          "time": times,
1195 e69d05fd Iustin Pop
1196 b33b6f55 Iustin Pop
        if name in output:
1197 b33b6f55 Iustin Pop
          # we only check static parameters, like memory and vcpus,
1198 b33b6f55 Iustin Pop
          # and not state and time which can change between the
1199 b33b6f55 Iustin Pop
          # invocations of the different hypervisors
1200 d0c8c01d Iustin Pop
          for key in "memory", "vcpus":
1201 b33b6f55 Iustin Pop
            if value[key] != output[name][key]:
1202 2fa74ef4 Iustin Pop
              _Fail("Instance %s is running twice"
1203 2fa74ef4 Iustin Pop
                    " with different parameters", name)
1204 f23b5ae8 Iustin Pop
        output[name] = value
1205 a8083063 Iustin Pop
1206 c26a6bd2 Iustin Pop
  return output
1207 a8083063 Iustin Pop
1208 a8083063 Iustin Pop
1209 6aa7a354 Iustin Pop
def _InstanceLogName(kind, os_name, instance, component):
1210 81a3406c Iustin Pop
  """Compute the OS log filename for a given instance and operation.
1211 81a3406c Iustin Pop

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

1215 81a3406c Iustin Pop
  @type kind: string
1216 81a3406c Iustin Pop
  @param kind: the operation type (e.g. add, import, etc.)
1217 81a3406c Iustin Pop
  @type os_name: string
1218 81a3406c Iustin Pop
  @param os_name: the os name
1219 81a3406c Iustin Pop
  @type instance: string
1220 81a3406c Iustin Pop
  @param instance: the name of the instance being imported/added/etc.
1221 6aa7a354 Iustin Pop
  @type component: string or None
1222 6aa7a354 Iustin Pop
  @param component: the name of the component of the instance being
1223 6aa7a354 Iustin Pop
1224 81a3406c Iustin Pop

1225 81a3406c Iustin Pop
1226 1651d116 Michael Hanselmann
  # TODO: Use tempfile.mkstemp to create unique filename
1227 6aa7a354 Iustin Pop
  if component:
1228 6aa7a354 Iustin Pop
    assert "/" not in component
1229 6aa7a354 Iustin Pop
    c_msg = "-%s" % component
1230 6aa7a354 Iustin Pop
1231 6aa7a354 Iustin Pop
    c_msg = ""
1232 6aa7a354 Iustin Pop
  base = ("%s-%s-%s%s-%s.log" %
1233 6aa7a354 Iustin Pop
          (kind, os_name, instance, c_msg, utils.TimestampForFilename()))
1234 710f30ec Michael Hanselmann
  return utils.PathJoin(pathutils.LOG_OS_DIR, base)
1235 81a3406c Iustin Pop
1236 81a3406c Iustin Pop
1237 4a0e011f Iustin Pop
def InstanceOsAdd(instance, reinstall, debug):
1238 2f8598a5 Alexander Schreiber
  """Add an OS to an instance.
1239 a8083063 Iustin Pop

1240 d15a9ad3 Guido Trotter
  @type instance: L{objects.Instance}
1241 d15a9ad3 Guido Trotter
  @param instance: Instance whose OS is to be installed
1242 e557bae9 Guido Trotter
  @type reinstall: boolean
1243 e557bae9 Guido Trotter
  @param reinstall: whether this is an instance reinstall
1244 4a0e011f Iustin Pop
  @type debug: integer
1245 4a0e011f Iustin Pop
  @param debug: debug level, passed to the OS scripts
1246 c26a6bd2 Iustin Pop
  @rtype: None
1247 a8083063 Iustin Pop

1248 a8083063 Iustin Pop
1249 255dcebd Iustin Pop
  inst_os = OSFromDisk(instance.os)
1250 255dcebd Iustin Pop
1251 4a0e011f Iustin Pop
  create_env = OSEnvironment(instance, inst_os, debug)
1252 e557bae9 Guido Trotter
  if reinstall:
1253 d0c8c01d Iustin Pop
    create_env["INSTANCE_REINSTALL"] = "1"
1254 a8083063 Iustin Pop
1255 6aa7a354 Iustin Pop
  logfile = _InstanceLogName("add", instance.os,, None)
1256 decd5f45 Iustin Pop
1257 d868edb4 Iustin Pop
  result = utils.RunCmd([inst_os.create_script], env=create_env,
1258 896a03f6 Iustin Pop
                        cwd=inst_os.path, output=logfile, reset_env=True)
1259 decd5f45 Iustin Pop
  if result.failed:
1260 18682bca Iustin Pop
    logging.error("os create command '%s' returned error: %s, logfile: %s,"
1261 d868edb4 Iustin Pop
                  " output: %s", result.cmd, result.fail_reason, logfile,
1262 18682bca Iustin Pop
1263 26f15862 Iustin Pop
    lines = [utils.SafeEncode(val)
1264 20e01edd Iustin Pop
             for val in utils.TailFile(logfile, lines=20)]
1265 afdc3985 Iustin Pop
    _Fail("OS create script failed (%s), last lines in the"
1266 afdc3985 Iustin Pop
          " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
1267 decd5f45 Iustin Pop
1268 decd5f45 Iustin Pop
1269 4a0e011f Iustin Pop
def RunRenameInstance(instance, old_name, debug):
1270 decd5f45 Iustin Pop
  """Run the OS rename script for an instance.
1271 decd5f45 Iustin Pop

1272 b1206984 Iustin Pop
  @type instance: L{objects.Instance}
1273 d15a9ad3 Guido Trotter
  @param instance: Instance whose OS is to be installed
1274 d15a9ad3 Guido Trotter
  @type old_name: string
1275 d15a9ad3 Guido Trotter
  @param old_name: previous instance name
1276 4a0e011f Iustin Pop
  @type debug: integer
1277 4a0e011f Iustin Pop
  @param debug: debug level, passed to the OS scripts
1278 10c2650b Iustin Pop
  @rtype: boolean
1279 10c2650b Iustin Pop
  @return: the success of the operation
1280 decd5f45 Iustin Pop

1281 decd5f45 Iustin Pop
1282 decd5f45 Iustin Pop
  inst_os = OSFromDisk(instance.os)
1283 decd5f45 Iustin Pop
1284 4a0e011f Iustin Pop
  rename_env = OSEnvironment(instance, inst_os, debug)
1285 d0c8c01d Iustin Pop
  rename_env["OLD_INSTANCE_NAME"] = old_name
1286 decd5f45 Iustin Pop
1287 81a3406c Iustin Pop
  logfile = _InstanceLogName("rename", instance.os,
1288 6aa7a354 Iustin Pop
                             "%s-%s" % (old_name,, None)
1289 a8083063 Iustin Pop
1290 d868edb4 Iustin Pop
  result = utils.RunCmd([inst_os.rename_script], env=rename_env,
1291 896a03f6 Iustin Pop
                        cwd=inst_os.path, output=logfile, reset_env=True)
1292 a8083063 Iustin Pop
1293 a8083063 Iustin Pop
  if result.failed:
1294 18682bca Iustin Pop
    logging.error("os create command '%s' returned error: %s output: %s",
1295 d868edb4 Iustin Pop
                  result.cmd, result.fail_reason, result.output)
1296 26f15862 Iustin Pop
    lines = [utils.SafeEncode(val)
1297 96841384 Iustin Pop
             for val in utils.TailFile(logfile, lines=20)]
1298 afdc3985 Iustin Pop
    _Fail("OS rename script failed (%s), last lines in the"
1299 afdc3985 Iustin Pop
          " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
1300 a8083063 Iustin Pop
1301 a8083063 Iustin Pop
1302 3b721842 Michael Hanselmann
def _GetBlockDevSymlinkPath(instance_name, idx, _dir=None):
1303 3b721842 Michael Hanselmann
  """Returns symlink path for block device.
1304 3b721842 Michael Hanselmann

1305 3b721842 Michael Hanselmann
1306 3b721842 Michael Hanselmann
  if _dir is None:
1307 3b721842 Michael Hanselmann
    _dir = pathutils.DISK_LINKS_DIR
1308 3b721842 Michael Hanselmann
1309 3b721842 Michael Hanselmann
  return utils.PathJoin(_dir,
1310 3b721842 Michael Hanselmann
                        ("%s%s%s" %
1311 3b721842 Michael Hanselmann
                         (instance_name, constants.DISK_SEPARATOR, idx)))
1312 5282084b Iustin Pop
1313 5282084b Iustin Pop
1314 5282084b Iustin Pop
def _SymlinkBlockDev(instance_name, device_path, idx):
1315 9332fd8a Iustin Pop
  """Set up symlinks to a instance's block device.
1316 9332fd8a Iustin Pop

1317 9332fd8a Iustin Pop
  This is an auxiliary function run when an instance is start (on the primary
1318 9332fd8a Iustin Pop
  node) or when an instance is migrated (on the target node).
1319 9332fd8a Iustin Pop

1320 9332fd8a Iustin Pop

1321 5282084b Iustin Pop
  @param instance_name: the name of the target instance
1322 5282084b Iustin Pop
  @param device_path: path of the physical block device, on the node
1323 5282084b Iustin Pop
  @param idx: the disk index
1324 5282084b Iustin Pop
  @return: absolute path to the disk's symlink
1325 9332fd8a Iustin Pop

1326 9332fd8a Iustin Pop
1327 5282084b Iustin Pop
  link_name = _GetBlockDevSymlinkPath(instance_name, idx)
1328 9332fd8a Iustin Pop
1329 9332fd8a Iustin Pop
    os.symlink(device_path, link_name)
1330 5282084b Iustin Pop
  except OSError, err:
1331 5282084b Iustin Pop
    if err.errno == errno.EEXIST:
1332 9332fd8a Iustin Pop
      if (not os.path.islink(link_name) or
1333 9332fd8a Iustin Pop
          os.readlink(link_name) != device_path):
1334 9332fd8a Iustin Pop
1335 9332fd8a Iustin Pop
        os.symlink(device_path, link_name)
1336 9332fd8a Iustin Pop
1337 9332fd8a Iustin Pop
1338 9332fd8a Iustin Pop
1339 9332fd8a Iustin Pop
  return link_name
1340 9332fd8a Iustin Pop
1341 9332fd8a Iustin Pop
1342 5282084b Iustin Pop
def _RemoveBlockDevLinks(instance_name, disks):
1343 3c9c571d Iustin Pop
  """Remove the block device symlinks belonging to the given instance.
1344 3c9c571d Iustin Pop

1345 3c9c571d Iustin Pop
1346 29921401 Iustin Pop
  for idx, _ in enumerate(disks):
1347 5282084b Iustin Pop
    link_name = _GetBlockDevSymlinkPath(instance_name, idx)
1348 5282084b Iustin Pop
    if os.path.islink(link_name):
1349 3c9c571d Iustin Pop
1350 03dfa658 Iustin Pop
1351 03dfa658 Iustin Pop
      except OSError:
1352 03dfa658 Iustin Pop
        logging.exception("Can't remove symlink '%s'", link_name)
1353 3c9c571d Iustin Pop
1354 3c9c571d Iustin Pop
1355 9332fd8a Iustin Pop
def _GatherAndLinkBlockDevs(instance):
1356 a8083063 Iustin Pop
  """Set up an instance's block device(s).
1357 a8083063 Iustin Pop

1358 a8083063 Iustin Pop
  This is run on the primary node at instance startup. The block
1359 a8083063 Iustin Pop
  devices must be already assembled.
1360 a8083063 Iustin Pop

1361 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1362 10c2650b Iustin Pop
  @param instance: the instance whose disks we shoul assemble
1363 069cfbf1 Iustin Pop
  @rtype: list
1364 069cfbf1 Iustin Pop
  @return: list of (disk_object, device_path)
1365 10c2650b Iustin Pop

1366 a8083063 Iustin Pop
1367 a8083063 Iustin Pop
  block_devices = []
1368 9332fd8a Iustin Pop
  for idx, disk in enumerate(instance.disks):
1369 a8083063 Iustin Pop
    device = _RecursiveFindBD(disk)
1370 a8083063 Iustin Pop
    if device is None:
1371 a8083063 Iustin Pop
      raise errors.BlockDeviceError("Block device '%s' is not set up." %
1372 a8083063 Iustin Pop
1373 a8083063 Iustin Pop
1374 9332fd8a Iustin Pop
1375 5282084b Iustin Pop
      link_name = _SymlinkBlockDev(, device.dev_path, idx)
1376 9332fd8a Iustin Pop
    except OSError, e:
1377 9332fd8a Iustin Pop
      raise errors.BlockDeviceError("Cannot create block device symlink: %s" %
1378 9332fd8a Iustin Pop
1379 9332fd8a Iustin Pop
1380 9332fd8a Iustin Pop
    block_devices.append((disk, link_name))
1381 9332fd8a Iustin Pop
1382 a8083063 Iustin Pop
  return block_devices
1383 a8083063 Iustin Pop
1384 a8083063 Iustin Pop
1385 1fa6fcba Michele Tartara
def StartInstance(instance, startup_paused, reason, store_reason=True):
1386 a8083063 Iustin Pop
  """Start an instance.
1387 a8083063 Iustin Pop

1388 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1389 e69d05fd Iustin Pop
  @param instance: the instance object
1390 323f9095 Stephen Shirley
  @type startup_paused: bool
1391 323f9095 Stephen Shirley
  @param instance: pause instance at startup?
1392 1fa6fcba Michele Tartara
  @type reason: list of reasons
1393 1fa6fcba Michele Tartara
  @param reason: the reason trail for this startup
1394 1fa6fcba Michele Tartara
  @type store_reason: boolean
1395 1fa6fcba Michele Tartara
  @param store_reason: whether to store the shutdown reason trail on file
1396 c26a6bd2 Iustin Pop
  @rtype: None
1397 a8083063 Iustin Pop

1398 098c0958 Michael Hanselmann
1399 e69d05fd Iustin Pop
  running_instances = GetInstanceList([instance.hypervisor])
1400 a8083063 Iustin Pop
1401 a8083063 Iustin Pop
  if in running_instances:
1402 c26a6bd2 Iustin Pop"Instance %s already running, not starting",
1403 c26a6bd2 Iustin Pop
1404 a8083063 Iustin Pop
1405 a8083063 Iustin Pop
1406 ec596c24 Iustin Pop
    block_devices = _GatherAndLinkBlockDevs(instance)
1407 ec596c24 Iustin Pop
    hyper = hypervisor.GetHypervisor(instance.hypervisor)
1408 323f9095 Stephen Shirley
    hyper.StartInstance(instance, block_devices, startup_paused)
1409 1fa6fcba Michele Tartara
    if store_reason:
1410 1fa6fcba Michele Tartara
      _StoreInstReasonTrail(, reason)
1411 ec596c24 Iustin Pop
  except errors.BlockDeviceError, err:
1412 2cc6781a Iustin Pop
    _Fail("Block device error: %s", err, exc=True)
1413 a8083063 Iustin Pop
  except errors.HypervisorError, err:
1414 5282084b Iustin Pop
    _RemoveBlockDevLinks(, instance.disks)
1415 2cc6781a Iustin Pop
    _Fail("Hypervisor error: %s", err, exc=True)
1416 a8083063 Iustin Pop
1417 a8083063 Iustin Pop
1418 1f350e0f Michele Tartara
def InstanceShutdown(instance, timeout, reason, store_reason=True):
1419 a8083063 Iustin Pop
  """Shut an instance down.
1420 a8083063 Iustin Pop

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

1423 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1424 e69d05fd Iustin Pop
  @param instance: the instance object
1425 6263189c Guido Trotter
  @type timeout: integer
1426 6263189c Guido Trotter
  @param timeout: maximum timeout for soft shutdown
1427 1f350e0f Michele Tartara
  @type reason: list of reasons
1428 1f350e0f Michele Tartara
  @param reason: the reason trail for this shutdown
1429 1f350e0f Michele Tartara
  @type store_reason: boolean
1430 1f350e0f Michele Tartara
  @param store_reason: whether to store the shutdown reason trail on file
1431 c26a6bd2 Iustin Pop
  @rtype: None
1432 a8083063 Iustin Pop

1433 098c0958 Michael Hanselmann
1434 e69d05fd Iustin Pop
  hv_name = instance.hypervisor
1435 e4e9b806 Guido Trotter
  hyper = hypervisor.GetHypervisor(hv_name)
1436 c26a6bd2 Iustin Pop
  iname =
1437 a8083063 Iustin Pop
1438 3c0cdc83 Michael Hanselmann
  if not in hyper.ListInstances():
1439 c26a6bd2 Iustin Pop"Instance %s not running, doing nothing", iname)
1440 c26a6bd2 Iustin Pop
1441 a8083063 Iustin Pop
1442 3c0cdc83 Michael Hanselmann
  class _TryShutdown:
1443 3c0cdc83 Michael Hanselmann
    def __init__(self):
1444 3c0cdc83 Michael Hanselmann
      self.tried_once = False
1445 a8083063 Iustin Pop
1446 3c0cdc83 Michael Hanselmann
    def __call__(self):
1447 3c0cdc83 Michael Hanselmann
      if iname not in hyper.ListInstances():
1448 3c0cdc83 Michael Hanselmann
1449 3c0cdc83 Michael Hanselmann
1450 3c0cdc83 Michael Hanselmann
1451 3c0cdc83 Michael Hanselmann
        hyper.StopInstance(instance, retry=self.tried_once)
1452 1f350e0f Michele Tartara
        if store_reason:
1453 1f350e0f Michele Tartara
          _StoreInstReasonTrail(, reason)
1454 3c0cdc83 Michael Hanselmann
      except errors.HypervisorError, err:
1455 3c0cdc83 Michael Hanselmann
        if iname not in hyper.ListInstances():
1456 3c0cdc83 Michael Hanselmann
          # if the instance is no longer existing, consider this a
1457 3c0cdc83 Michael Hanselmann
          # success and go to cleanup
1458 3c0cdc83 Michael Hanselmann
1459 3c0cdc83 Michael Hanselmann
1460 3c0cdc83 Michael Hanselmann
        _Fail("Failed to stop instance %s: %s", iname, err)
1461 3c0cdc83 Michael Hanselmann
1462 3c0cdc83 Michael Hanselmann
      self.tried_once = True
1463 3c0cdc83 Michael Hanselmann
1464 3c0cdc83 Michael Hanselmann
      raise utils.RetryAgain()
1465 3c0cdc83 Michael Hanselmann
1466 3c0cdc83 Michael Hanselmann
1467 3c0cdc83 Michael Hanselmann
    utils.Retry(_TryShutdown(), 5, timeout)
1468 3c0cdc83 Michael Hanselmann
  except utils.RetryTimeout:
1469 a8083063 Iustin Pop
    # the shutdown did not succeed
1470 e4e9b806 Guido Trotter
    logging.error("Shutdown of '%s' unsuccessful, forcing", iname)
1471 a8083063 Iustin Pop
1472 a8083063 Iustin Pop
1473 a8083063 Iustin Pop
      hyper.StopInstance(instance, force=True)
1474 a8083063 Iustin Pop
    except errors.HypervisorError, err:
1475 3c0cdc83 Michael Hanselmann
      if iname in hyper.ListInstances():
1476 3782acd7 Iustin Pop
        # only raise an error if the instance still exists, otherwise
1477 3782acd7 Iustin Pop
        # the error could simply be "instance ... unknown"!
1478 3782acd7 Iustin Pop
        _Fail("Failed to force stop instance %s: %s", iname, err)
1479 a8083063 Iustin Pop
1480 a8083063 Iustin Pop
1481 3c0cdc83 Michael Hanselmann
1482 3c0cdc83 Michael Hanselmann
    if iname in hyper.ListInstances():
1483 c26a6bd2 Iustin Pop
      _Fail("Could not shutdown instance %s even by destroy", iname)
1484 3c9c571d Iustin Pop
1485 f28ec899 Guido Trotter
1486 f28ec899 Guido Trotter
1487 f28ec899 Guido Trotter
  except errors.HypervisorError, err:
1488 f28ec899 Guido Trotter
    logging.warning("Failed to execute post-shutdown cleanup step: %s", err)
1489 f28ec899 Guido Trotter
1490 c26a6bd2 Iustin Pop
  _RemoveBlockDevLinks(iname, instance.disks)
1491 a8083063 Iustin Pop
1492 a8083063 Iustin Pop
1493 55cec070 Michele Tartara
def InstanceReboot(instance, reboot_type, shutdown_timeout, reason):
1494 007a2f3e Alexander Schreiber
  """Reboot an instance.
1495 007a2f3e Alexander Schreiber

1496 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1497 10c2650b Iustin Pop
  @param instance: the instance object to reboot
1498 10c2650b Iustin Pop
  @type reboot_type: str
1499 10c2650b Iustin Pop
  @param reboot_type: the type of reboot, one the following
1500 10c2650b Iustin Pop
1501 10c2650b Iustin Pop
      - L{constants.INSTANCE_REBOOT_SOFT}: only reboot the
1502 10c2650b Iustin Pop
        instance OS, do not recreate the VM
1503 10c2650b Iustin Pop
      - L{constants.INSTANCE_REBOOT_HARD}: tear down and
1504 10c2650b Iustin Pop
        restart the VM (at the hypervisor level)
1505 73e5a4f4 Iustin Pop
      - the other reboot type (L{constants.INSTANCE_REBOOT_FULL}) is
1506 73e5a4f4 Iustin Pop
        not accepted here, since that mode is handled differently, in
1507 73e5a4f4 Iustin Pop
        cmdlib, and translates into full stop and start of the
1508 73e5a4f4 Iustin Pop
        instance (instead of a call_instance_reboot RPC)
1509 23057d29 Michael Hanselmann
  @type shutdown_timeout: integer
1510 23057d29 Michael Hanselmann
  @param shutdown_timeout: maximum timeout for soft shutdown
1511 55cec070 Michele Tartara
  @type reason: list of reasons
1512 55cec070 Michele Tartara
  @param reason: the reason trail for this reboot
1513 c26a6bd2 Iustin Pop
  @rtype: None
1514 007a2f3e Alexander Schreiber

1515 007a2f3e Alexander Schreiber
1516 e69d05fd Iustin Pop
  running_instances = GetInstanceList([instance.hypervisor])
1517 007a2f3e Alexander Schreiber
1518 007a2f3e Alexander Schreiber
  if not in running_instances:
1519 2cc6781a Iustin Pop
    _Fail("Cannot reboot instance %s that is not running",
1520 007a2f3e Alexander Schreiber
1521 e69d05fd Iustin Pop
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1522 007a2f3e Alexander Schreiber
  if reboot_type == constants.INSTANCE_REBOOT_SOFT:
1523 007a2f3e Alexander Schreiber
1524 007a2f3e Alexander Schreiber
1525 007a2f3e Alexander Schreiber
    except errors.HypervisorError, err:
1526 2cc6781a Iustin Pop
      _Fail("Failed to soft reboot instance %s: %s",, err)
1527 007a2f3e Alexander Schreiber
  elif reboot_type == constants.INSTANCE_REBOOT_HARD:
1528 007a2f3e Alexander Schreiber
1529 1f350e0f Michele Tartara
      InstanceShutdown(instance, shutdown_timeout, reason, store_reason=False)
1530 1fa6fcba Michele Tartara
      result = StartInstance(instance, False, reason, store_reason=False)
1531 55cec070 Michele Tartara
      _StoreInstReasonTrail(, reason)
1532 4a90bd4f Michele Tartara
      return result
1533 007a2f3e Alexander Schreiber
    except errors.HypervisorError, err:
1534 2cc6781a Iustin Pop
      _Fail("Failed to hard reboot instance %s: %s",, err)
1535 007a2f3e Alexander Schreiber
1536 2cc6781a Iustin Pop
    _Fail("Invalid reboot_type received: %s", reboot_type)
1537 007a2f3e Alexander Schreiber
1538 007a2f3e Alexander Schreiber
1539 ebe466d8 Guido Trotter
def InstanceBalloonMemory(instance, memory):
1540 ebe466d8 Guido Trotter
  """Resize an instance's memory.
1541 ebe466d8 Guido Trotter

1542 ebe466d8 Guido Trotter
  @type instance: L{objects.Instance}
1543 ebe466d8 Guido Trotter
  @param instance: the instance object
1544 ebe466d8 Guido Trotter
  @type memory: int
1545 ebe466d8 Guido Trotter
  @param memory: new memory amount in MB
1546 ebe466d8 Guido Trotter
  @rtype: None
1547 ebe466d8 Guido Trotter

1548 ebe466d8 Guido Trotter
1549 ebe466d8 Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1550 ebe466d8 Guido Trotter
  running = hyper.ListInstances()
1551 ebe466d8 Guido Trotter
  if not in running:
1552 ebe466d8 Guido Trotter"Instance %s is not running, cannot balloon",
1553 ebe466d8 Guido Trotter
1554 ebe466d8 Guido Trotter
1555 ebe466d8 Guido Trotter
    hyper.BalloonInstanceMemory(instance, memory)
1556 ebe466d8 Guido Trotter
  except errors.HypervisorError, err:
1557 ebe466d8 Guido Trotter
    _Fail("Failed to balloon instance memory: %s", err, exc=True)
1558 ebe466d8 Guido Trotter
1559 ebe466d8 Guido Trotter
1560 6906a9d8 Guido Trotter
def MigrationInfo(instance):
1561 6906a9d8 Guido Trotter
  """Gather information about an instance to be migrated.
1562 6906a9d8 Guido Trotter

1563 6906a9d8 Guido Trotter
  @type instance: L{objects.Instance}
1564 6906a9d8 Guido Trotter
  @param instance: the instance definition
1565 6906a9d8 Guido Trotter

1566 6906a9d8 Guido Trotter
1567 cd42d0ad Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1568 cd42d0ad Guido Trotter
1569 cd42d0ad Guido Trotter
    info = hyper.MigrationInfo(instance)
1570 cd42d0ad Guido Trotter
  except errors.HypervisorError, err:
1571 2cc6781a Iustin Pop
    _Fail("Failed to fetch migration information: %s", err, exc=True)
1572 c26a6bd2 Iustin Pop
  return info
1573 6906a9d8 Guido Trotter
1574 6906a9d8 Guido Trotter
1575 6906a9d8 Guido Trotter
def AcceptInstance(instance, info, target):
1576 6906a9d8 Guido Trotter
  """Prepare the node to accept an instance.
1577 6906a9d8 Guido Trotter

1578 6906a9d8 Guido Trotter
  @type instance: L{objects.Instance}
1579 6906a9d8 Guido Trotter
  @param instance: the instance definition
1580 6906a9d8 Guido Trotter
  @type info: string/data (opaque)
1581 6906a9d8 Guido Trotter
  @param info: migration information, from the source node
1582 6906a9d8 Guido Trotter
  @type target: string
1583 6906a9d8 Guido Trotter
  @param target: target host (usually ip), on this node
1584 6906a9d8 Guido Trotter

1585 6906a9d8 Guido Trotter
1586 77fcff4a Apollon Oikonomopoulos
  # TODO: why is this required only for DTS_EXT_MIRROR?
1587 77fcff4a Apollon Oikonomopoulos
  if instance.disk_template in constants.DTS_EXT_MIRROR:
1588 77fcff4a Apollon Oikonomopoulos
    # Create the symlinks, as the disks are not active
1589 77fcff4a Apollon Oikonomopoulos
    # in any way
1590 77fcff4a Apollon Oikonomopoulos
1591 77fcff4a Apollon Oikonomopoulos
1592 77fcff4a Apollon Oikonomopoulos
    except errors.BlockDeviceError, err:
1593 77fcff4a Apollon Oikonomopoulos
      _Fail("Block device error: %s", err, exc=True)
1594 77fcff4a Apollon Oikonomopoulos
1595 cd42d0ad Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1596 cd42d0ad Guido Trotter
1597 cd42d0ad Guido Trotter
    hyper.AcceptInstance(instance, info, target)
1598 cd42d0ad Guido Trotter
  except errors.HypervisorError, err:
1599 77fcff4a Apollon Oikonomopoulos
    if instance.disk_template in constants.DTS_EXT_MIRROR:
1600 77fcff4a Apollon Oikonomopoulos
      _RemoveBlockDevLinks(, instance.disks)
1601 2cc6781a Iustin Pop
    _Fail("Failed to accept instance: %s", err, exc=True)
1602 6906a9d8 Guido Trotter
1603 6906a9d8 Guido Trotter
1604 6a1434d7 Andrea Spadaccini
def FinalizeMigrationDst(instance, info, success):
1605 6906a9d8 Guido Trotter
  """Finalize any preparation to accept an instance.
1606 6906a9d8 Guido Trotter

1607 6906a9d8 Guido Trotter
  @type instance: L{objects.Instance}
1608 6906a9d8 Guido Trotter
  @param instance: the instance definition
1609 6906a9d8 Guido Trotter
  @type info: string/data (opaque)
1610 6906a9d8 Guido Trotter
  @param info: migration information, from the source node
1611 6906a9d8 Guido Trotter
  @type success: boolean
1612 6906a9d8 Guido Trotter
  @param success: whether the migration was a success or a failure
1613 6906a9d8 Guido Trotter

1614 6906a9d8 Guido Trotter
1615 cd42d0ad Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1616 cd42d0ad Guido Trotter
1617 6a1434d7 Andrea Spadaccini
    hyper.FinalizeMigrationDst(instance, info, success)
1618 cd42d0ad Guido Trotter
  except errors.HypervisorError, err:
1619 6a1434d7 Andrea Spadaccini
    _Fail("Failed to finalize migration on the target node: %s", err, exc=True)
1620 6906a9d8 Guido Trotter
1621 6906a9d8 Guido Trotter
1622 2a10865c Iustin Pop
def MigrateInstance(instance, target, live):
1623 2a10865c Iustin Pop
  """Migrates an instance to another node.
1624 2a10865c Iustin Pop

1625 b1206984 Iustin Pop
  @type instance: L{objects.Instance}
1626 9f0e6b37 Iustin Pop
  @param instance: the instance definition
1627 9f0e6b37 Iustin Pop
  @type target: string
1628 9f0e6b37 Iustin Pop
  @param target: the target node name
1629 9f0e6b37 Iustin Pop
  @type live: boolean
1630 9f0e6b37 Iustin Pop
  @param live: whether the migration should be done live or not (the
1631 9f0e6b37 Iustin Pop
      interpretation of this parameter is left to the hypervisor)
1632 c03fe62b Andrea Spadaccini
  @raise RPCFail: if migration fails for some reason
1633 9f0e6b37 Iustin Pop

1634 2a10865c Iustin Pop
1635 53c776b5 Iustin Pop
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1636 2a10865c Iustin Pop
1637 2a10865c Iustin Pop
1638 58d38b02 Iustin Pop
    hyper.MigrateInstance(instance, target, live)
1639 2a10865c Iustin Pop
  except errors.HypervisorError, err:
1640 2cc6781a Iustin Pop
    _Fail("Failed to migrate instance: %s", err, exc=True)
1641 2a10865c Iustin Pop
1642 2a10865c Iustin Pop
1643 6a1434d7 Andrea Spadaccini
def FinalizeMigrationSource(instance, success, live):
1644 6a1434d7 Andrea Spadaccini
  """Finalize the instance migration on the source node.
1645 6a1434d7 Andrea Spadaccini

1646 6a1434d7 Andrea Spadaccini
  @type instance: L{objects.Instance}
1647 6a1434d7 Andrea Spadaccini
  @param instance: the instance definition of the migrated instance
1648 6a1434d7 Andrea Spadaccini
  @type success: bool
1649 6a1434d7 Andrea Spadaccini
  @param success: whether the migration succeeded or not
1650 6a1434d7 Andrea Spadaccini
  @type live: bool
1651 6a1434d7 Andrea Spadaccini
  @param live: whether the user requested a live migration or not
1652 6a1434d7 Andrea Spadaccini
  @raise RPCFail: If the execution fails for some reason
1653 6a1434d7 Andrea Spadaccini

1654 6a1434d7 Andrea Spadaccini
1655 6a1434d7 Andrea Spadaccini
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1656 6a1434d7 Andrea Spadaccini
1657 6a1434d7 Andrea Spadaccini
1658 6a1434d7 Andrea Spadaccini
    hyper.FinalizeMigrationSource(instance, success, live)
1659 6a1434d7 Andrea Spadaccini
  except Exception, err:  # pylint: disable=W0703
1660 6a1434d7 Andrea Spadaccini
    _Fail("Failed to finalize the migration on the source node: %s", err,
1661 6a1434d7 Andrea Spadaccini
1662 6a1434d7 Andrea Spadaccini
1663 6a1434d7 Andrea Spadaccini
1664 6a1434d7 Andrea Spadaccini
def GetMigrationStatus(instance):
1665 6a1434d7 Andrea Spadaccini
  """Get the migration status
1666 6a1434d7 Andrea Spadaccini

1667 6a1434d7 Andrea Spadaccini
  @type instance: L{objects.Instance}
1668 6a1434d7 Andrea Spadaccini
  @param instance: the instance that is being migrated
1669 6a1434d7 Andrea Spadaccini
  @rtype: L{objects.MigrationStatus}
1670 6a1434d7 Andrea Spadaccini
  @return: the status of the current migration (one of
1671 6a1434d7 Andrea Spadaccini
           L{constants.HV_MIGRATION_VALID_STATUSES}), plus any additional
1672 6a1434d7 Andrea Spadaccini
           progress info that can be retrieved from the hypervisor
1673 6a1434d7 Andrea Spadaccini
  @raise RPCFail: If the migration status cannot be retrieved
1674 6a1434d7 Andrea Spadaccini

1675 6a1434d7 Andrea Spadaccini
1676 6a1434d7 Andrea Spadaccini
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1677 6a1434d7 Andrea Spadaccini
1678 6a1434d7 Andrea Spadaccini
    return hyper.GetMigrationStatus(instance)
1679 6a1434d7 Andrea Spadaccini
  except Exception, err:  # pylint: disable=W0703
1680 6a1434d7 Andrea Spadaccini
    _Fail("Failed to get migration status: %s", err, exc=True)
1681 6a1434d7 Andrea Spadaccini
1682 6a1434d7 Andrea Spadaccini
1683 ee1478e5 Bernardo Dal Seno
def BlockdevCreate(disk, size, owner, on_primary, info, excl_stor):
1684 a8083063 Iustin Pop
  """Creates a block device for an instance.
1685 a8083063 Iustin Pop

1686 b1206984 Iustin Pop
  @type disk: L{objects.Disk}
1687 b1206984 Iustin Pop
  @param disk: the object describing the disk we should create
1688 b1206984 Iustin Pop
  @type size: int
1689 b1206984 Iustin Pop
  @param size: the size of the physical underlying device, in MiB
1690 b1206984 Iustin Pop
  @type owner: str
1691 b1206984 Iustin Pop
  @param owner: the name of the instance for which disk is created,
1692 b1206984 Iustin Pop
      used for device cache data
1693 b1206984 Iustin Pop
  @type on_primary: boolean
1694 b1206984 Iustin Pop
  @param on_primary:  indicates if it is the primary node or not
1695 b1206984 Iustin Pop
  @type info: string
1696 b1206984 Iustin Pop
  @param info: string that will be sent to the physical device
1697 b1206984 Iustin Pop
      creation, used for example to set (LVM) tags on LVs
1698 ee1478e5 Bernardo Dal Seno
  @type excl_stor: boolean
1699 ee1478e5 Bernardo Dal Seno
  @param excl_stor: Whether exclusive_storage is active
1700 b1206984 Iustin Pop

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

1705 a8083063 Iustin Pop
1706 d0c8c01d Iustin Pop
  # TODO: remove the obsolete "size" argument
1707 b459a848 Andrea Spadaccini
  # pylint: disable=W0613
1708 a8083063 Iustin Pop
  clist = []
1709 a8083063 Iustin Pop
  if disk.children:
1710 a8083063 Iustin Pop
    for child in disk.children:
1711 1063abd1 Iustin Pop
1712 1063abd1 Iustin Pop
        crdev = _RecursiveAssembleBD(child, owner, on_primary)
1713 1063abd1 Iustin Pop
      except errors.BlockDeviceError, err:
1714 2cc6781a Iustin Pop
        _Fail("Can't assemble device %s: %s", child, err)
1715 a8083063 Iustin Pop
      if on_primary or disk.AssembleOnSecondary():
1716 a8083063 Iustin Pop
        # we need the children open in case the device itself has to
1717 a8083063 Iustin Pop
        # be assembled
1718 1063abd1 Iustin Pop
1719 b459a848 Andrea Spadaccini
          # pylint: disable=E1103
1720 1063abd1 Iustin Pop
1721 1063abd1 Iustin Pop
        except errors.BlockDeviceError, err:
1722 2cc6781a Iustin Pop
          _Fail("Can't make child '%s' read-write: %s", child, err)
1723 a8083063 Iustin Pop
1724 a8083063 Iustin Pop
1725 dab69e97 Iustin Pop
1726 ee1478e5 Bernardo Dal Seno
    device = bdev.Create(disk, clist, excl_stor)
1727 1063abd1 Iustin Pop
  except errors.BlockDeviceError, err:
1728 2cc6781a Iustin Pop
    _Fail("Can't create block device: %s", err)
1729 6c626518 Iustin Pop
1730 a8083063 Iustin Pop
  if on_primary or disk.AssembleOnSecondary():
1731 1063abd1 Iustin Pop
1732 1063abd1 Iustin Pop
1733 1063abd1 Iustin Pop
    except errors.BlockDeviceError, err:
1734 2cc6781a Iustin Pop
      _Fail("Can't assemble device after creation, unusual event: %s", err)
1735 a8083063 Iustin Pop
    if on_primary or disk.OpenOnSecondary():
1736 1063abd1 Iustin Pop
1737 1063abd1 Iustin Pop
1738 1063abd1 Iustin Pop
      except errors.BlockDeviceError, err:
1739 2cc6781a Iustin Pop
        _Fail("Can't make device r/w after creation, unusual event: %s", err)
1740 3f78eef2 Iustin Pop
    DevCacheManager.UpdateCache(device.dev_path, owner,
1741 3f78eef2 Iustin Pop
                                on_primary, disk.iv_name)
1742 a0c3fea1 Michael Hanselmann
1743 a0c3fea1 Michael Hanselmann
1744 a0c3fea1 Michael Hanselmann
1745 c26a6bd2 Iustin Pop
  return device.unique_id
1746 a8083063 Iustin Pop
1747 a8083063 Iustin Pop
1748 da63bb4e René Nussbaumer
def _WipeDevice(path, offset, size):
1749 69dd363f René Nussbaumer
  """This function actually wipes the device.
1750 69dd363f René Nussbaumer

1751 69dd363f René Nussbaumer
  @param path: The path to the device to wipe
1752 da63bb4e René Nussbaumer
  @param offset: The offset in MiB in the file
1753 da63bb4e René Nussbaumer
  @param size: The size in MiB to write
1754 69dd363f René Nussbaumer

1755 69dd363f René Nussbaumer
1756 0188611b Michael Hanselmann
  # Internal sizes are always in Mebibytes; if the following "dd" command
1757 0188611b Michael Hanselmann
  # should use a different block size the offset and size given to this
1758 0188611b Michael Hanselmann
  # function must be adjusted accordingly before being passed to "dd".
1759 0188611b Michael Hanselmann
  block_size = 1024 * 1024
1760 0188611b Michael Hanselmann
1761 da63bb4e René Nussbaumer
  cmd = [constants.DD_CMD, "if=/dev/zero", "seek=%d" % offset,
1762 0188611b Michael Hanselmann
         "bs=%s" % block_size, "oflag=direct", "of=%s" % path,
1763 da63bb4e René Nussbaumer
         "count=%d" % size]
1764 da63bb4e René Nussbaumer
  result = utils.RunCmd(cmd)
1765 69dd363f René Nussbaumer
1766 69dd363f René Nussbaumer
  if result.failed:
1767 69dd363f René Nussbaumer
    _Fail("Wipe command '%s' exited with error: %s; output: %s", result.cmd,
1768 69dd363f René Nussbaumer
          result.fail_reason, result.output)
1769 69dd363f René Nussbaumer
1770 69dd363f René Nussbaumer
1771 da63bb4e René Nussbaumer
def BlockdevWipe(disk, offset, size):
1772 69dd363f René Nussbaumer
  """Wipes a block device.
1773 69dd363f René Nussbaumer

1774 69dd363f René Nussbaumer
  @type disk: L{objects.Disk}
1775 69dd363f René Nussbaumer
  @param disk: the disk object we want to wipe
1776 da63bb4e René Nussbaumer
  @type offset: int
1777 da63bb4e René Nussbaumer
  @param offset: The offset in MiB in the file
1778 da63bb4e René Nussbaumer
  @type size: int
1779 da63bb4e René Nussbaumer
  @param size: The size in MiB to write
1780 69dd363f René Nussbaumer

1781 69dd363f René Nussbaumer
1782 69dd363f René Nussbaumer
1783 69dd363f René Nussbaumer
    rdev = _RecursiveFindBD(disk)
1784 da63bb4e René Nussbaumer
  except errors.BlockDeviceError:
1785 da63bb4e René Nussbaumer
    rdev = None
1786 da63bb4e René Nussbaumer
1787 da63bb4e René Nussbaumer
  if not rdev:
1788 da63bb4e René Nussbaumer
    _Fail("Cannot execute wipe for device %s: device not found", disk.iv_name)
1789 da63bb4e René Nussbaumer
1790 da63bb4e René Nussbaumer
  # Do cross verify some of the parameters
1791 0188611b Michael Hanselmann
  if offset < 0:
1792 0188611b Michael Hanselmann
    _Fail("Negative offset")
1793 0188611b Michael Hanselmann
  if size < 0:
1794 0188611b Michael Hanselmann
    _Fail("Negative size")
1795 da63bb4e René Nussbaumer
  if offset > rdev.size:
1796 da63bb4e René Nussbaumer
    _Fail("Offset is bigger than device size")
1797 da63bb4e René Nussbaumer
  if (offset + size) > rdev.size:
1798 da63bb4e René Nussbaumer
    _Fail("The provided offset and size to wipe is bigger than device size")
1799 69dd363f René Nussbaumer
1800 da63bb4e René Nussbaumer
  _WipeDevice(rdev.dev_path, offset, size)
1801 69dd363f René Nussbaumer
1802 69dd363f René Nussbaumer
1803 5119c79e René Nussbaumer
def BlockdevPauseResumeSync(disks, pause):
1804 5119c79e René Nussbaumer
  """Pause or resume the sync of the block device.
1805 5119c79e René Nussbaumer

1806 0f39886a René Nussbaumer
  @type disks: list of L{objects.Disk}
1807 0f39886a René Nussbaumer
  @param disks: the disks object we want to pause/resume
1808 5119c79e René Nussbaumer
  @type pause: bool
1809 5119c79e René Nussbaumer
  @param pause: Wheater to pause or resume
1810 5119c79e René Nussbaumer

1811 5119c79e René Nussbaumer
1812 5119c79e René Nussbaumer
  success = []
1813 5119c79e René Nussbaumer
  for disk in disks:
1814 5119c79e René Nussbaumer
1815 5119c79e René Nussbaumer
      rdev = _RecursiveFindBD(disk)
1816 5119c79e René Nussbaumer
    except errors.BlockDeviceError:
1817 5119c79e René Nussbaumer
      rdev = None
1818 5119c79e René Nussbaumer
1819 5119c79e René Nussbaumer
    if not rdev:
1820 5119c79e René Nussbaumer
      success.append((False, ("Cannot change sync for device %s:"
1821 5119c79e René Nussbaumer
                              " device not found" % disk.iv_name)))
1822 5119c79e René Nussbaumer
1823 5119c79e René Nussbaumer
1824 5119c79e René Nussbaumer
    result = rdev.PauseResumeSync(pause)
1825 5119c79e René Nussbaumer
1826 5119c79e René Nussbaumer
    if result:
1827 5119c79e René Nussbaumer
      success.append((result, None))
1828 5119c79e René Nussbaumer
1829 5119c79e René Nussbaumer
      if pause:
1830 5119c79e René Nussbaumer
        msg = "Pause"
1831 5119c79e René Nussbaumer
1832 5119c79e René Nussbaumer
        msg = "Resume"
1833 5119c79e René Nussbaumer
      success.append((result, "%s for device %s failed" % (msg, disk.iv_name)))
1834 5119c79e René Nussbaumer
1835 5119c79e René Nussbaumer
  return success
1836 5119c79e René Nussbaumer
1837 5119c79e René Nussbaumer
1838 821d1bd1 Iustin Pop
def BlockdevRemove(disk):
1839 a8083063 Iustin Pop
  """Remove a block device.
1840 a8083063 Iustin Pop

1841 10c2650b Iustin Pop
  @note: This is intended to be called recursively.
1842 10c2650b Iustin Pop

1843 c41eea6e Iustin Pop
  @type disk: L{objects.Disk}
1844 10c2650b Iustin Pop
  @param disk: the disk object we should remove
1845 10c2650b Iustin Pop
  @rtype: boolean
1846 10c2650b Iustin Pop
  @return: the success of the operation
1847 a8083063 Iustin Pop

1848 a8083063 Iustin Pop
1849 e1bc0878 Iustin Pop
  msgs = []
1850 a8083063 Iustin Pop
1851 bca2e7f4 Iustin Pop
    rdev = _RecursiveFindBD(disk)
1852 a8083063 Iustin Pop
  except errors.BlockDeviceError, err:
1853 a8083063 Iustin Pop
    # probably can't attach
1854 18682bca Iustin Pop"Can't attach to device %s in remove", disk)
1855 a8083063 Iustin Pop
    rdev = None
1856 a8083063 Iustin Pop
  if rdev is not None:
1857 3f78eef2 Iustin Pop
    r_path = rdev.dev_path
1858 e1bc0878 Iustin Pop
1859 0c6c04ec Iustin Pop
1860 e1bc0878 Iustin Pop
    except errors.BlockDeviceError, err:
1861 e1bc0878 Iustin Pop
1862 c26a6bd2 Iustin Pop
    if not msgs:
1863 3f78eef2 Iustin Pop
1864 e1bc0878 Iustin Pop
1865 a8083063 Iustin Pop
  if disk.children:
1866 a8083063 Iustin Pop
    for child in disk.children:
1867 c26a6bd2 Iustin Pop
1868 c26a6bd2 Iustin Pop
1869 c26a6bd2 Iustin Pop
      except RPCFail, err:
1870 c26a6bd2 Iustin Pop
1871 e1bc0878 Iustin Pop
1872 c26a6bd2 Iustin Pop
  if msgs:
1873 afdc3985 Iustin Pop
    _Fail("; ".join(msgs))
1874 afdc3985 Iustin Pop
1875 a8083063 Iustin Pop
1876 3f78eef2 Iustin Pop
def _RecursiveAssembleBD(disk, owner, as_primary):
1877 a8083063 Iustin Pop
  """Activate a block device for an instance.
1878 a8083063 Iustin Pop

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

1881 10c2650b Iustin Pop
  @note: this function is called recursively.
1882 a8083063 Iustin Pop

1883 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
1884 10c2650b Iustin Pop
  @param disk: the disk we try to assemble
1885 10c2650b Iustin Pop
  @type owner: str
1886 10c2650b Iustin Pop
  @param owner: the name of the instance which owns the disk
1887 10c2650b Iustin Pop
  @type as_primary: boolean
1888 10c2650b Iustin Pop
  @param as_primary: if we should make the block device
1889 10c2650b Iustin Pop
1890 a8083063 Iustin Pop

1891 10c2650b Iustin Pop
  @return: the assembled device or None (in case no device
1892 10c2650b Iustin Pop
      was assembled)
1893 10c2650b Iustin Pop
  @raise errors.BlockDeviceError: in case there is an error
1894 10c2650b Iustin Pop
      during the activation of the children or the device
1895 10c2650b Iustin Pop
1896 a8083063 Iustin Pop

1897 a8083063 Iustin Pop
1898 a8083063 Iustin Pop
  children = []
1899 a8083063 Iustin Pop
  if disk.children:
1900 fc1dc9d7 Iustin Pop
    mcn = disk.ChildrenNeeded()
1901 fc1dc9d7 Iustin Pop
    if mcn == -1:
1902 fc1dc9d7 Iustin Pop
      mcn = 0 # max number of Nones allowed
1903 fc1dc9d7 Iustin Pop
1904 fc1dc9d7 Iustin Pop
      mcn = len(disk.children) - mcn # max number of Nones
1905 a8083063 Iustin Pop
    for chld_disk in disk.children:
1906 fc1dc9d7 Iustin Pop
1907 fc1dc9d7 Iustin Pop
        cdev = _RecursiveAssembleBD(chld_disk, owner, as_primary)
1908 fc1dc9d7 Iustin Pop
      except errors.BlockDeviceError, err:
1909 7803d4d3 Iustin Pop
        if children.count(None) >= mcn:
1910 fc1dc9d7 Iustin Pop
1911 fc1dc9d7 Iustin Pop
        cdev = None
1912 1063abd1 Iustin Pop
        logging.error("Error in child activation (but continuing): %s",
1913 1063abd1 Iustin Pop
1914 fc1dc9d7 Iustin Pop
1915 a8083063 Iustin Pop
1916 a8083063 Iustin Pop
  if as_primary or disk.AssembleOnSecondary():
1917 94dcbdb0 Andrea Spadaccini
    r_dev = bdev.Assemble(disk, children)
1918 a8083063 Iustin Pop
    result = r_dev
1919 a8083063 Iustin Pop
    if as_primary or disk.OpenOnSecondary():
1920 a8083063 Iustin Pop
1921 3f78eef2 Iustin Pop
    DevCacheManager.UpdateCache(r_dev.dev_path, owner,
1922 3f78eef2 Iustin Pop
                                as_primary, disk.iv_name)
1923 3f78eef2 Iustin Pop
1924 a8083063 Iustin Pop
1925 a8083063 Iustin Pop
    result = True
1926 a8083063 Iustin Pop
  return result
1927 a8083063 Iustin Pop
1928 a8083063 Iustin Pop
1929 c417e115 Iustin Pop
def BlockdevAssemble(disk, owner, as_primary, idx):
1930 a8083063 Iustin Pop
  """Activate a block device for an instance.
1931 a8083063 Iustin Pop

1932 a8083063 Iustin Pop
  This is a wrapper over _RecursiveAssembleBD.
1933 a8083063 Iustin Pop

1934 b1206984 Iustin Pop
  @rtype: str or boolean
1935 b1206984 Iustin Pop
  @return: a C{/dev/...} path for primary nodes, and
1936 b1206984 Iustin Pop
      C{True} for secondary nodes
1937 a8083063 Iustin Pop

1938 a8083063 Iustin Pop
1939 53c14ef1 Iustin Pop
1940 53c14ef1 Iustin Pop
    result = _RecursiveAssembleBD(disk, owner, as_primary)
1941 89ff748d Thomas Thrainer
    if isinstance(result, BlockDev):
1942 b459a848 Andrea Spadaccini
      # pylint: disable=E1103
1943 53c14ef1 Iustin Pop
      result = result.dev_path
1944 c417e115 Iustin Pop
      if as_primary:
1945 c417e115 Iustin Pop
        _SymlinkBlockDev(owner, result, idx)
1946 53c14ef1 Iustin Pop
  except errors.BlockDeviceError, err:
1947 afdc3985 Iustin Pop
    _Fail("Error while assembling disk: %s", err, exc=True)
1948 c417e115 Iustin Pop
  except OSError, err:
1949 c417e115 Iustin Pop
    _Fail("Error while symlinking disk: %s", err, exc=True)
1950 afdc3985 Iustin Pop
1951 c26a6bd2 Iustin Pop
  return result
1952 a8083063 Iustin Pop
1953 a8083063 Iustin Pop
1954 821d1bd1 Iustin Pop
def BlockdevShutdown(disk):
1955 a8083063 Iustin Pop
  """Shut down a block device.
1956 a8083063 Iustin Pop

1957 5bbd3f7f Michael Hanselmann
  First, if the device is assembled (Attach() is successful), then
1958 c41eea6e Iustin Pop
  the device is shutdown. Then the children of the device are
1959 c41eea6e Iustin Pop
1960 a8083063 Iustin Pop

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

1965 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
1966 10c2650b Iustin Pop
  @param disk: the description of the disk we should
1967 10c2650b Iustin Pop
1968 c26a6bd2 Iustin Pop
  @rtype: None
1969 10c2650b Iustin Pop

1970 a8083063 Iustin Pop
1971 cacfd1fd Iustin Pop
  msgs = []
1972 a8083063 Iustin Pop
  r_dev = _RecursiveFindBD(disk)
1973 a8083063 Iustin Pop
  if r_dev is not None:
1974 3f78eef2 Iustin Pop
    r_path = r_dev.dev_path
1975 cacfd1fd Iustin Pop
1976 746f7476 Iustin Pop
1977 746f7476 Iustin Pop
1978 cacfd1fd Iustin Pop
    except errors.BlockDeviceError, err:
1979 cacfd1fd Iustin Pop
1980 746f7476 Iustin Pop
1981 a8083063 Iustin Pop
  if disk.children:
1982 a8083063 Iustin Pop
    for child in disk.children:
1983 c26a6bd2 Iustin Pop
1984 c26a6bd2 Iustin Pop
1985 c26a6bd2 Iustin Pop
      except RPCFail, err:
1986 c26a6bd2 Iustin Pop
1987 746f7476 Iustin Pop
1988 c26a6bd2 Iustin Pop
  if msgs:
1989 afdc3985 Iustin Pop
    _Fail("; ".join(msgs))
1990 a8083063 Iustin Pop
1991 a8083063 Iustin Pop
1992 821d1bd1 Iustin Pop
def BlockdevAddchildren(parent_cdev, new_cdevs):
1993 153d9724 Iustin Pop
  """Extend a mirrored block device.
1994 a8083063 Iustin Pop

1995 10c2650b Iustin Pop
  @type parent_cdev: L{objects.Disk}
1996 10c2650b Iustin Pop
  @param parent_cdev: the disk to which we should add children
1997 10c2650b Iustin Pop
  @type new_cdevs: list of L{objects.Disk}
1998 10c2650b Iustin Pop
  @param new_cdevs: the list of children which we should add
1999 c26a6bd2 Iustin Pop
  @rtype: None
2000 10c2650b Iustin Pop

2001 a8083063 Iustin Pop
2002 bca2e7f4 Iustin Pop
  parent_bdev = _RecursiveFindBD(parent_cdev)
2003 153d9724 Iustin Pop
  if parent_bdev is None:
2004 2cc6781a Iustin Pop
    _Fail("Can't find parent device '%s' in add children", parent_cdev)
2005 153d9724 Iustin Pop
  new_bdevs = [_RecursiveFindBD(disk) for disk in new_cdevs]
2006 153d9724 Iustin Pop
  if new_bdevs.count(None) > 0:
2007 2cc6781a Iustin Pop
    _Fail("Can't find new device(s) to add: %s:%s", new_bdevs, new_cdevs)
2008 153d9724 Iustin Pop
2009 a8083063 Iustin Pop
2010 a8083063 Iustin Pop
2011 821d1bd1 Iustin Pop
def BlockdevRemovechildren(parent_cdev, new_cdevs):
2012 153d9724 Iustin Pop
  """Shrink a mirrored block device.
2013 a8083063 Iustin Pop

2014 10c2650b Iustin Pop
  @type parent_cdev: L{objects.Disk}
2015 10c2650b Iustin Pop
  @param parent_cdev: the disk from which we should remove children
2016 10c2650b Iustin Pop
  @type new_cdevs: list of L{objects.Disk}
2017 10c2650b Iustin Pop
  @param new_cdevs: the list of children which we should remove
2018 c26a6bd2 Iustin Pop
  @rtype: None
2019 10c2650b Iustin Pop

2020 a8083063 Iustin Pop
2021 153d9724 Iustin Pop
  parent_bdev = _RecursiveFindBD(parent_cdev)
2022 153d9724 Iustin Pop
  if parent_bdev is None:
2023 2cc6781a Iustin Pop
    _Fail("Can't find parent device '%s' in remove children", parent_cdev)
2024 e739bd57 Iustin Pop
  devs = []
2025 e739bd57 Iustin Pop
  for disk in new_cdevs:
2026 e739bd57 Iustin Pop
    rpath = disk.StaticDevPath()
2027 e739bd57 Iustin Pop
    if rpath is None:
2028 e739bd57 Iustin Pop
      bd = _RecursiveFindBD(disk)
2029 e739bd57 Iustin Pop
      if bd is None:
2030 2cc6781a Iustin Pop
        _Fail("Can't find device %s while removing children", disk)
2031 e739bd57 Iustin Pop
2032 e739bd57 Iustin Pop
2033 e739bd57 Iustin Pop
2034 e51db2a6 Iustin Pop
      if not utils.IsNormAbsPath(rpath):
2035 e51db2a6 Iustin Pop
        _Fail("Strange path returned from StaticDevPath: '%s'", rpath)
2036 e739bd57 Iustin Pop
2037 e739bd57 Iustin Pop
2038 a8083063 Iustin Pop
2039 a8083063 Iustin Pop
2040 821d1bd1 Iustin Pop
def BlockdevGetmirrorstatus(disks):
2041 a8083063 Iustin Pop
  """Get the mirroring status of a list of devices.
2042 a8083063 Iustin Pop

2043 10c2650b Iustin Pop
  @type disks: list of L{objects.Disk}
2044 10c2650b Iustin Pop
  @param disks: the list of disks which we should query
2045 10c2650b Iustin Pop
  @rtype: disk
2046 c6a9dffa Michael Hanselmann
  @return: List of L{objects.BlockDevStatus}, one for each disk
2047 10c2650b Iustin Pop
  @raise errors.BlockDeviceError: if any of the disks cannot be
2048 10c2650b Iustin Pop
2049 a8083063 Iustin Pop

2050 a8083063 Iustin Pop
2051 a8083063 Iustin Pop
  stats = []
2052 a8083063 Iustin Pop
  for dsk in disks:
2053 a8083063 Iustin Pop
    rbd = _RecursiveFindBD(dsk)
2054 a8083063 Iustin Pop
    if rbd is None:
2055 3efa9051 Iustin Pop
      _Fail("Can't find device %s", dsk)
2056 96acbc09 Michael Hanselmann
2057 36145b12 Michael Hanselmann
2058 96acbc09 Michael Hanselmann
2059 c26a6bd2 Iustin Pop
  return stats
2060 a8083063 Iustin Pop
2061 a8083063 Iustin Pop
2062 c6a9dffa Michael Hanselmann
def BlockdevGetmirrorstatusMulti(disks):
2063 c6a9dffa Michael Hanselmann
  """Get the mirroring status of a list of devices.
2064 c6a9dffa Michael Hanselmann

2065 c6a9dffa Michael Hanselmann
  @type disks: list of L{objects.Disk}
2066 c6a9dffa Michael Hanselmann
  @param disks: the list of disks which we should query
2067 c6a9dffa Michael Hanselmann
  @rtype: disk
2068 c6a9dffa Michael Hanselmann
  @return: List of tuples, (bool, status), one for each disk; bool denotes
2069 c6a9dffa Michael Hanselmann
    success/failure, status is L{objects.BlockDevStatus} on success, string
2070 c6a9dffa Michael Hanselmann
2071 c6a9dffa Michael Hanselmann

2072 c6a9dffa Michael Hanselmann
2073 c6a9dffa Michael Hanselmann
  result = []
2074 c6a9dffa Michael Hanselmann
  for disk in disks:
2075 c6a9dffa Michael Hanselmann
2076 c6a9dffa Michael Hanselmann
      rbd = _RecursiveFindBD(disk)
2077 c6a9dffa Michael Hanselmann
      if rbd is None:
2078 c6a9dffa Michael Hanselmann
        result.append((False, "Can't find device %s" % disk))
2079 c6a9dffa Michael Hanselmann
2080 c6a9dffa Michael Hanselmann
2081 c6a9dffa Michael Hanselmann
      status = rbd.CombinedSyncStatus()
2082 c6a9dffa Michael Hanselmann
    except errors.BlockDeviceError, err:
2083 c6a9dffa Michael Hanselmann
      logging.exception("Error while getting disk status")