Statistics
| Branch: | Tag: | Revision:

root / lib / backend.py @ c1912a48

History | View | Annotate | Download (121.1 kB)

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

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

29 360b0dc2 Iustin Pop
"""
30 a8083063 Iustin Pop
31 b459a848 Andrea Spadaccini
# pylint: disable=E1103
32 6c881c52 Iustin Pop
33 6c881c52 Iustin Pop
# E1103: %s %r has no %r member (but some types could not be
34 6c881c52 Iustin Pop
# inferred), because the _TryOSFromDisk returns either (True, os_obj)
35 6c881c52 Iustin Pop
# or (False, "string") which confuses pylint
36 6c881c52 Iustin Pop
37 a8083063 Iustin Pop
38 a8083063 Iustin Pop
import os
39 a8083063 Iustin Pop
import os.path
40 a8083063 Iustin Pop
import shutil
41 a8083063 Iustin Pop
import time
42 a8083063 Iustin Pop
import stat
43 a8083063 Iustin Pop
import errno
44 a8083063 Iustin Pop
import re
45 b544cfe0 Iustin Pop
import random
46 18682bca Iustin Pop
import logging
47 3b9e6a30 Iustin Pop
import tempfile
48 12bce260 Michael Hanselmann
import zlib
49 12bce260 Michael Hanselmann
import base64
50 f81c4737 Michael Hanselmann
import signal
51 a8083063 Iustin Pop
52 a8083063 Iustin Pop
from ganeti import errors
53 a8083063 Iustin Pop
from ganeti import utils
54 a8083063 Iustin Pop
from ganeti import ssh
55 a8083063 Iustin Pop
from ganeti import hypervisor
56 a8083063 Iustin Pop
from ganeti import constants
57 a8083063 Iustin Pop
from ganeti import bdev
58 a8083063 Iustin Pop
from ganeti import objects
59 880478f8 Iustin Pop
from ganeti import ssconf
60 1651d116 Michael Hanselmann
from ganeti import serializer
61 a744b676 Manuel Franceschini
from ganeti import netutils
62 82b22e19 René Nussbaumer
from ganeti import runtime
63 0fa481f5 Andrea Spadaccini
from ganeti import mcpu
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 a8083063 Iustin Pop
69 a8083063 Iustin Pop
70 13998ef2 Michael Hanselmann
_BOOT_ID_PATH = "/proc/sys/kernel/random/boot_id"
71 b8028dcf Michael Hanselmann
_ALLOWED_CLEAN_DIRS = compat.UniqueFrozenset([
72 710f30ec Michael Hanselmann
  pathutils.DATA_DIR,
73 710f30ec Michael Hanselmann
  pathutils.JOB_QUEUE_ARCHIVE_DIR,
74 710f30ec Michael Hanselmann
  pathutils.QUEUE_DIR,
75 710f30ec Michael Hanselmann
  pathutils.CRYPTO_KEYS_DIR,
76 714ea7ca Iustin Pop
  ])
77 f942a838 Michael Hanselmann
_MAX_SSL_CERT_VALIDITY = 7 * 24 * 60 * 60
78 f942a838 Michael Hanselmann
_X509_KEY_FILE = "key"
79 f942a838 Michael Hanselmann
_X509_CERT_FILE = "cert"
80 1651d116 Michael Hanselmann
_IES_STATUS_FILE = "status"
81 1651d116 Michael Hanselmann
_IES_PID_FILE = "pid"
82 1651d116 Michael Hanselmann
_IES_CA_FILE = "ca"
83 13998ef2 Michael Hanselmann
84 0b5303da Iustin Pop
#: Valid LVS output line regex
85 a1f38213 Iustin Pop
_LVSLINE_REGEX = re.compile("^ *([^|]+)\|([^|]+)\|([0-9.]+)\|([^|]{6,})\|?$")
86 0b5303da Iustin Pop
87 702eff21 Andrea Spadaccini
# Actions for the master setup script
88 702eff21 Andrea Spadaccini
_MASTER_START = "start"
89 702eff21 Andrea Spadaccini
_MASTER_STOP = "stop"
90 702eff21 Andrea Spadaccini
91 1a2eb2dc Michael Hanselmann
#: Maximum file permissions for remote command directory and executables
92 1a2eb2dc Michael Hanselmann
_RCMD_MAX_MODE = (stat.S_IRWXU |
93 1a2eb2dc Michael Hanselmann
                  stat.S_IRGRP | stat.S_IXGRP |
94 1a2eb2dc Michael Hanselmann
                  stat.S_IROTH | stat.S_IXOTH)
95 1a2eb2dc Michael Hanselmann
96 1a2eb2dc Michael Hanselmann
#: Delay before returning an error for remote commands
97 1a2eb2dc Michael Hanselmann
_RCMD_INVALID_DELAY = 10
98 1a2eb2dc Michael Hanselmann
99 1a2eb2dc Michael Hanselmann
#: How long to wait to acquire lock for remote commands (shorter than
100 1a2eb2dc Michael Hanselmann
#: L{_RCMD_INVALID_DELAY}) to reduce blockage of noded forks when many
101 1a2eb2dc Michael Hanselmann
#: command requests arrive
102 1a2eb2dc Michael Hanselmann
_RCMD_LOCK_TIMEOUT = _RCMD_INVALID_DELAY * 0.8
103 1a2eb2dc Michael Hanselmann
104 13998ef2 Michael Hanselmann
105 2cc6781a Iustin Pop
class RPCFail(Exception):
106 2cc6781a Iustin Pop
  """Class denoting RPC failure.
107 2cc6781a Iustin Pop

108 2cc6781a Iustin Pop
  Its argument is the error message.
109 2cc6781a Iustin Pop

110 2cc6781a Iustin Pop
  """
111 2cc6781a Iustin Pop
112 13998ef2 Michael Hanselmann
113 2cc6781a Iustin Pop
def _Fail(msg, *args, **kwargs):
114 2cc6781a Iustin Pop
  """Log an error and the raise an RPCFail exception.
115 2cc6781a Iustin Pop

116 2cc6781a Iustin Pop
  This exception is then handled specially in the ganeti daemon and
117 2cc6781a Iustin Pop
  turned into a 'failed' return type. As such, this function is a
118 2cc6781a Iustin Pop
  useful shortcut for logging the error and returning it to the master
119 2cc6781a Iustin Pop
  daemon.
120 2cc6781a Iustin Pop

121 2cc6781a Iustin Pop
  @type msg: string
122 2cc6781a Iustin Pop
  @param msg: the text of the exception
123 2cc6781a Iustin Pop
  @raise RPCFail
124 2cc6781a Iustin Pop

125 2cc6781a Iustin Pop
  """
126 2cc6781a Iustin Pop
  if args:
127 2cc6781a Iustin Pop
    msg = msg % args
128 afdc3985 Iustin Pop
  if "log" not in kwargs or kwargs["log"]: # if we should log this error
129 afdc3985 Iustin Pop
    if "exc" in kwargs and kwargs["exc"]:
130 afdc3985 Iustin Pop
      logging.exception(msg)
131 afdc3985 Iustin Pop
    else:
132 afdc3985 Iustin Pop
      logging.error(msg)
133 2cc6781a Iustin Pop
  raise RPCFail(msg)
134 2cc6781a Iustin Pop
135 2cc6781a Iustin Pop
136 c657dcc9 Michael Hanselmann
def _GetConfig():
137 93384844 Iustin Pop
  """Simple wrapper to return a SimpleStore.
138 10c2650b Iustin Pop

139 93384844 Iustin Pop
  @rtype: L{ssconf.SimpleStore}
140 93384844 Iustin Pop
  @return: a SimpleStore instance
141 10c2650b Iustin Pop

142 10c2650b Iustin Pop
  """
143 93384844 Iustin Pop
  return ssconf.SimpleStore()
144 c657dcc9 Michael Hanselmann
145 c657dcc9 Michael Hanselmann
146 62c9ec92 Iustin Pop
def _GetSshRunner(cluster_name):
147 10c2650b Iustin Pop
  """Simple wrapper to return an SshRunner.
148 10c2650b Iustin Pop

149 10c2650b Iustin Pop
  @type cluster_name: str
150 10c2650b Iustin Pop
  @param cluster_name: the cluster name, which is needed
151 10c2650b Iustin Pop
      by the SshRunner constructor
152 10c2650b Iustin Pop
  @rtype: L{ssh.SshRunner}
153 10c2650b Iustin Pop
  @return: an SshRunner instance
154 10c2650b Iustin Pop

155 10c2650b Iustin Pop
  """
156 62c9ec92 Iustin Pop
  return ssh.SshRunner(cluster_name)
157 c92b310a Michael Hanselmann
158 c92b310a Michael Hanselmann
159 12bce260 Michael Hanselmann
def _Decompress(data):
160 12bce260 Michael Hanselmann
  """Unpacks data compressed by the RPC client.
161 12bce260 Michael Hanselmann

162 12bce260 Michael Hanselmann
  @type data: list or tuple
163 12bce260 Michael Hanselmann
  @param data: Data sent by RPC client
164 12bce260 Michael Hanselmann
  @rtype: str
165 12bce260 Michael Hanselmann
  @return: Decompressed data
166 12bce260 Michael Hanselmann

167 12bce260 Michael Hanselmann
  """
168 52e2f66e Michael Hanselmann
  assert isinstance(data, (list, tuple))
169 12bce260 Michael Hanselmann
  assert len(data) == 2
170 12bce260 Michael Hanselmann
  (encoding, content) = data
171 12bce260 Michael Hanselmann
  if encoding == constants.RPC_ENCODING_NONE:
172 12bce260 Michael Hanselmann
    return content
173 12bce260 Michael Hanselmann
  elif encoding == constants.RPC_ENCODING_ZLIB_BASE64:
174 12bce260 Michael Hanselmann
    return zlib.decompress(base64.b64decode(content))
175 12bce260 Michael Hanselmann
  else:
176 12bce260 Michael Hanselmann
    raise AssertionError("Unknown data encoding")
177 12bce260 Michael Hanselmann
178 12bce260 Michael Hanselmann
179 3bc6be5c Iustin Pop
def _CleanDirectory(path, exclude=None):
180 76ab5558 Michael Hanselmann
  """Removes all regular files in a directory.
181 76ab5558 Michael Hanselmann

182 10c2650b Iustin Pop
  @type path: str
183 10c2650b Iustin Pop
  @param path: the directory to clean
184 76ab5558 Michael Hanselmann
  @type exclude: list
185 10c2650b Iustin Pop
  @param exclude: list of files to be excluded, defaults
186 10c2650b Iustin Pop
      to the empty list
187 76ab5558 Michael Hanselmann

188 76ab5558 Michael Hanselmann
  """
189 714ea7ca Iustin Pop
  if path not in _ALLOWED_CLEAN_DIRS:
190 714ea7ca Iustin Pop
    _Fail("Path passed to _CleanDirectory not in allowed clean targets: '%s'",
191 714ea7ca Iustin Pop
          path)
192 714ea7ca Iustin Pop
193 3956cee1 Michael Hanselmann
  if not os.path.isdir(path):
194 3956cee1 Michael Hanselmann
    return
195 3bc6be5c Iustin Pop
  if exclude is None:
196 3bc6be5c Iustin Pop
    exclude = []
197 3bc6be5c Iustin Pop
  else:
198 3bc6be5c Iustin Pop
    # Normalize excluded paths
199 3bc6be5c Iustin Pop
    exclude = [os.path.normpath(i) for i in exclude]
200 76ab5558 Michael Hanselmann
201 3956cee1 Michael Hanselmann
  for rel_name in utils.ListVisibleFiles(path):
202 c4feafe8 Iustin Pop
    full_name = utils.PathJoin(path, rel_name)
203 76ab5558 Michael Hanselmann
    if full_name in exclude:
204 76ab5558 Michael Hanselmann
      continue
205 3956cee1 Michael Hanselmann
    if os.path.isfile(full_name) and not os.path.islink(full_name):
206 3956cee1 Michael Hanselmann
      utils.RemoveFile(full_name)
207 3956cee1 Michael Hanselmann
208 3956cee1 Michael Hanselmann
209 360b0dc2 Iustin Pop
def _BuildUploadFileList():
210 360b0dc2 Iustin Pop
  """Build the list of allowed upload files.
211 360b0dc2 Iustin Pop

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

214 360b0dc2 Iustin Pop
  """
215 b397a7d2 Iustin Pop
  allowed_files = set([
216 710f30ec Michael Hanselmann
    pathutils.CLUSTER_CONF_FILE,
217 ee045466 Michael Hanselmann
    pathutils.ETC_HOSTS,
218 710f30ec Michael Hanselmann
    pathutils.SSH_KNOWN_HOSTS_FILE,
219 710f30ec Michael Hanselmann
    pathutils.VNC_PASSWORD_FILE,
220 710f30ec Michael Hanselmann
    pathutils.RAPI_CERT_FILE,
221 710f30ec Michael Hanselmann
    pathutils.SPICE_CERT_FILE,
222 710f30ec Michael Hanselmann
    pathutils.SPICE_CACERT_FILE,
223 710f30ec Michael Hanselmann
    pathutils.RAPI_USERS_FILE,
224 710f30ec Michael Hanselmann
    pathutils.CONFD_HMAC_KEY,
225 710f30ec Michael Hanselmann
    pathutils.CLUSTER_DOMAIN_SECRET_FILE,
226 b397a7d2 Iustin Pop
    ])
227 b397a7d2 Iustin Pop
228 b397a7d2 Iustin Pop
  for hv_name in constants.HYPER_TYPES:
229 e5a45a16 Iustin Pop
    hv_class = hypervisor.GetHypervisorClass(hv_name)
230 69ab2e12 Guido Trotter
    allowed_files.update(hv_class.GetAncillaryFiles()[0])
231 b397a7d2 Iustin Pop
232 3439fd6b Michael Hanselmann
  assert pathutils.FILE_STORAGE_PATHS_FILE not in allowed_files, \
233 3439fd6b Michael Hanselmann
    "Allowed file storage paths should never be uploaded via RPC"
234 3439fd6b Michael Hanselmann
235 b397a7d2 Iustin Pop
  return frozenset(allowed_files)
236 360b0dc2 Iustin Pop
237 360b0dc2 Iustin Pop
238 360b0dc2 Iustin Pop
_ALLOWED_UPLOAD_FILES = _BuildUploadFileList()
239 360b0dc2 Iustin Pop
240 360b0dc2 Iustin Pop
241 1bc59f76 Michael Hanselmann
def JobQueuePurge():
242 10c2650b Iustin Pop
  """Removes job queue files and archived jobs.
243 10c2650b Iustin Pop

244 c8457ce7 Iustin Pop
  @rtype: tuple
245 c8457ce7 Iustin Pop
  @return: True, None
246 24fc781f Michael Hanselmann

247 24fc781f Michael Hanselmann
  """
248 710f30ec Michael Hanselmann
  _CleanDirectory(pathutils.QUEUE_DIR, exclude=[pathutils.JOB_QUEUE_LOCK_FILE])
249 710f30ec Michael Hanselmann
  _CleanDirectory(pathutils.JOB_QUEUE_ARCHIVE_DIR)
250 24fc781f Michael Hanselmann
251 24fc781f Michael Hanselmann
252 bd1e4562 Iustin Pop
def GetMasterInfo():
253 bd1e4562 Iustin Pop
  """Returns master information.
254 bd1e4562 Iustin Pop

255 bd1e4562 Iustin Pop
  This is an utility function to compute master information, either
256 bd1e4562 Iustin Pop
  for consumption here or from the node daemon.
257 bd1e4562 Iustin Pop

258 bd1e4562 Iustin Pop
  @rtype: tuple
259 909b3a0e Andrea Spadaccini
  @return: master_netdev, master_ip, master_name, primary_ip_family,
260 909b3a0e Andrea Spadaccini
    master_netmask
261 2a52a064 Iustin Pop
  @raise RPCFail: in case of errors
262 b1b6ea87 Iustin Pop

263 b1b6ea87 Iustin Pop
  """
264 b1b6ea87 Iustin Pop
  try:
265 c657dcc9 Michael Hanselmann
    cfg = _GetConfig()
266 c657dcc9 Michael Hanselmann
    master_netdev = cfg.GetMasterNetdev()
267 c657dcc9 Michael Hanselmann
    master_ip = cfg.GetMasterIP()
268 5a8648eb Andrea Spadaccini
    master_netmask = cfg.GetMasterNetmask()
269 c657dcc9 Michael Hanselmann
    master_node = cfg.GetMasterNode()
270 d8e0caa6 Manuel Franceschini
    primary_ip_family = cfg.GetPrimaryIPFamily()
271 b1b6ea87 Iustin Pop
  except errors.ConfigurationError, err:
272 29921401 Iustin Pop
    _Fail("Cluster configuration incomplete: %s", err, exc=True)
273 909b3a0e Andrea Spadaccini
  return (master_netdev, master_ip, master_node, primary_ip_family,
274 5ae4945a Iustin Pop
          master_netmask)
275 b1b6ea87 Iustin Pop
276 b1b6ea87 Iustin Pop
277 0fa481f5 Andrea Spadaccini
def RunLocalHooks(hook_opcode, hooks_path, env_builder_fn):
278 0fa481f5 Andrea Spadaccini
  """Decorator that runs hooks before and after the decorated function.
279 0fa481f5 Andrea Spadaccini

280 0fa481f5 Andrea Spadaccini
  @type hook_opcode: string
281 0fa481f5 Andrea Spadaccini
  @param hook_opcode: opcode of the hook
282 0fa481f5 Andrea Spadaccini
  @type hooks_path: string
283 0fa481f5 Andrea Spadaccini
  @param hooks_path: path of the hooks
284 0fa481f5 Andrea Spadaccini
  @type env_builder_fn: function
285 0fa481f5 Andrea Spadaccini
  @param env_builder_fn: function that returns a dictionary containing the
286 3ccd3243 Andrea Spadaccini
    environment variables for the hooks. Will get all the parameters of the
287 3ccd3243 Andrea Spadaccini
    decorated function.
288 0fa481f5 Andrea Spadaccini
  @raise RPCFail: in case of pre-hook failure
289 0fa481f5 Andrea Spadaccini

290 0fa481f5 Andrea Spadaccini
  """
291 0fa481f5 Andrea Spadaccini
  def decorator(fn):
292 0fa481f5 Andrea Spadaccini
    def wrapper(*args, **kwargs):
293 0fa481f5 Andrea Spadaccini
      _, myself = ssconf.GetMasterAndMyself()
294 0fa481f5 Andrea Spadaccini
      nodes = ([myself], [myself])  # these hooks run locally
295 0fa481f5 Andrea Spadaccini
296 3ccd3243 Andrea Spadaccini
      env_fn = compat.partial(env_builder_fn, *args, **kwargs)
297 3ccd3243 Andrea Spadaccini
298 0fa481f5 Andrea Spadaccini
      cfg = _GetConfig()
299 0fa481f5 Andrea Spadaccini
      hr = HooksRunner()
300 0fa481f5 Andrea Spadaccini
      hm = mcpu.HooksMaster(hook_opcode, hooks_path, nodes, hr.RunLocalHooks,
301 3ccd3243 Andrea Spadaccini
                            None, env_fn, logging.warning, cfg.GetClusterName(),
302 3ccd3243 Andrea Spadaccini
                            cfg.GetMasterNode())
303 0fa481f5 Andrea Spadaccini
304 0fa481f5 Andrea Spadaccini
      hm.RunPhase(constants.HOOKS_PHASE_PRE)
305 0fa481f5 Andrea Spadaccini
      result = fn(*args, **kwargs)
306 0fa481f5 Andrea Spadaccini
      hm.RunPhase(constants.HOOKS_PHASE_POST)
307 0fa481f5 Andrea Spadaccini
308 0fa481f5 Andrea Spadaccini
      return result
309 0fa481f5 Andrea Spadaccini
    return wrapper
310 0fa481f5 Andrea Spadaccini
  return decorator
311 0fa481f5 Andrea Spadaccini
312 0fa481f5 Andrea Spadaccini
313 57c7bc57 Andrea Spadaccini
def _BuildMasterIpEnv(master_params, use_external_mip_script=None):
314 2d88fdd3 Andrea Spadaccini
  """Builds environment variables for master IP hooks.
315 2d88fdd3 Andrea Spadaccini

316 3ccd3243 Andrea Spadaccini
  @type master_params: L{objects.MasterNetworkParameters}
317 3ccd3243 Andrea Spadaccini
  @param master_params: network parameters of the master
318 57c7bc57 Andrea Spadaccini
  @type use_external_mip_script: boolean
319 57c7bc57 Andrea Spadaccini
  @param use_external_mip_script: whether to use an external master IP
320 57c7bc57 Andrea Spadaccini
    address setup script (unused, but necessary per the implementation of the
321 57c7bc57 Andrea Spadaccini
    _RunLocalHooks decorator)
322 3ccd3243 Andrea Spadaccini

323 2d88fdd3 Andrea Spadaccini
  """
324 57c7bc57 Andrea Spadaccini
  # pylint: disable=W0613
325 3ccd3243 Andrea Spadaccini
  ver = netutils.IPAddress.GetVersionFromAddressFamily(master_params.ip_family)
326 2d88fdd3 Andrea Spadaccini
  env = {
327 3ccd3243 Andrea Spadaccini
    "MASTER_NETDEV": master_params.netdev,
328 3ccd3243 Andrea Spadaccini
    "MASTER_IP": master_params.ip,
329 702eff21 Andrea Spadaccini
    "MASTER_NETMASK": str(master_params.netmask),
330 3ccd3243 Andrea Spadaccini
    "CLUSTER_IP_VERSION": str(ver),
331 2d88fdd3 Andrea Spadaccini
  }
332 2d88fdd3 Andrea Spadaccini
333 2d88fdd3 Andrea Spadaccini
  return env
334 2d88fdd3 Andrea Spadaccini
335 2d88fdd3 Andrea Spadaccini
336 702eff21 Andrea Spadaccini
def _RunMasterSetupScript(master_params, action, use_external_mip_script):
337 702eff21 Andrea Spadaccini
  """Execute the master IP address setup script.
338 702eff21 Andrea Spadaccini

339 702eff21 Andrea Spadaccini
  @type master_params: L{objects.MasterNetworkParameters}
340 702eff21 Andrea Spadaccini
  @param master_params: network parameters of the master
341 702eff21 Andrea Spadaccini
  @type action: string
342 702eff21 Andrea Spadaccini
  @param action: action to pass to the script. Must be one of
343 702eff21 Andrea Spadaccini
    L{backend._MASTER_START} or L{backend._MASTER_STOP}
344 702eff21 Andrea Spadaccini
  @type use_external_mip_script: boolean
345 702eff21 Andrea Spadaccini
  @param use_external_mip_script: whether to use an external master IP
346 702eff21 Andrea Spadaccini
    address setup script
347 702eff21 Andrea Spadaccini
  @raise backend.RPCFail: if there are errors during the execution of the
348 702eff21 Andrea Spadaccini
    script
349 702eff21 Andrea Spadaccini

350 702eff21 Andrea Spadaccini
  """
351 702eff21 Andrea Spadaccini
  env = _BuildMasterIpEnv(master_params)
352 702eff21 Andrea Spadaccini
353 702eff21 Andrea Spadaccini
  if use_external_mip_script:
354 710f30ec Michael Hanselmann
    setup_script = pathutils.EXTERNAL_MASTER_SETUP_SCRIPT
355 702eff21 Andrea Spadaccini
  else:
356 710f30ec Michael Hanselmann
    setup_script = pathutils.DEFAULT_MASTER_SETUP_SCRIPT
357 702eff21 Andrea Spadaccini
358 702eff21 Andrea Spadaccini
  result = utils.RunCmd([setup_script, action], env=env, reset_env=True)
359 702eff21 Andrea Spadaccini
360 702eff21 Andrea Spadaccini
  if result.failed:
361 702eff21 Andrea Spadaccini
    _Fail("Failed to %s the master IP. Script return value: %s" %
362 702eff21 Andrea Spadaccini
          (action, result.exit_code), log=True)
363 702eff21 Andrea Spadaccini
364 702eff21 Andrea Spadaccini
365 2d88fdd3 Andrea Spadaccini
@RunLocalHooks(constants.FAKE_OP_MASTER_TURNUP, "master-ip-turnup",
366 3a3e4f1e Andrea Spadaccini
               _BuildMasterIpEnv)
367 57c7bc57 Andrea Spadaccini
def ActivateMasterIp(master_params, use_external_mip_script):
368 fb460cf7 Andrea Spadaccini
  """Activate the IP address of the master daemon.
369 fb460cf7 Andrea Spadaccini

370 c79198a0 Andrea Spadaccini
  @type master_params: L{objects.MasterNetworkParameters}
371 c79198a0 Andrea Spadaccini
  @param master_params: network parameters of the master
372 57c7bc57 Andrea Spadaccini
  @type use_external_mip_script: boolean
373 57c7bc57 Andrea Spadaccini
  @param use_external_mip_script: whether to use an external master IP
374 57c7bc57 Andrea Spadaccini
    address setup script
375 702eff21 Andrea Spadaccini
  @raise RPCFail: in case of errors during the IP startup
376 8da2bd43 Andrea Spadaccini

377 fb460cf7 Andrea Spadaccini
  """
378 702eff21 Andrea Spadaccini
  _RunMasterSetupScript(master_params, _MASTER_START,
379 702eff21 Andrea Spadaccini
                        use_external_mip_script)
380 fb460cf7 Andrea Spadaccini
381 fb460cf7 Andrea Spadaccini
382 fb460cf7 Andrea Spadaccini
def StartMasterDaemons(no_voting):
383 a8083063 Iustin Pop
  """Activate local node as master node.
384 a8083063 Iustin Pop

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

387 3583908a Guido Trotter
  @type no_voting: boolean
388 3583908a Guido Trotter
  @param no_voting: whether to start ganeti-masterd without a node vote
389 fb460cf7 Andrea Spadaccini
      but still non-interactively
390 10c2650b Iustin Pop
  @rtype: None
391 a8083063 Iustin Pop

392 a8083063 Iustin Pop
  """
393 a8083063 Iustin Pop
394 fb460cf7 Andrea Spadaccini
  if no_voting:
395 fb460cf7 Andrea Spadaccini
    masterd_args = "--no-voting --yes-do-it"
396 fb460cf7 Andrea Spadaccini
  else:
397 fb460cf7 Andrea Spadaccini
    masterd_args = ""
398 f154a7a3 Michael Hanselmann
399 fb460cf7 Andrea Spadaccini
  env = {
400 fb460cf7 Andrea Spadaccini
    "EXTRA_MASTERD_ARGS": masterd_args,
401 fb460cf7 Andrea Spadaccini
    }
402 fb460cf7 Andrea Spadaccini
403 710f30ec Michael Hanselmann
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "start-master"], env=env)
404 fb460cf7 Andrea Spadaccini
  if result.failed:
405 fb460cf7 Andrea Spadaccini
    msg = "Can't start Ganeti master: %s" % result.output
406 fb460cf7 Andrea Spadaccini
    logging.error(msg)
407 fb460cf7 Andrea Spadaccini
    _Fail(msg)
408 f154a7a3 Michael Hanselmann
409 fb460cf7 Andrea Spadaccini
410 2d88fdd3 Andrea Spadaccini
@RunLocalHooks(constants.FAKE_OP_MASTER_TURNDOWN, "master-ip-turndown",
411 3a3e4f1e Andrea Spadaccini
               _BuildMasterIpEnv)
412 57c7bc57 Andrea Spadaccini
def DeactivateMasterIp(master_params, use_external_mip_script):
413 fb460cf7 Andrea Spadaccini
  """Deactivate the master IP on this node.
414 a8083063 Iustin Pop

415 c79198a0 Andrea Spadaccini
  @type master_params: L{objects.MasterNetworkParameters}
416 c79198a0 Andrea Spadaccini
  @param master_params: network parameters of the master
417 57c7bc57 Andrea Spadaccini
  @type use_external_mip_script: boolean
418 57c7bc57 Andrea Spadaccini
  @param use_external_mip_script: whether to use an external master IP
419 57c7bc57 Andrea Spadaccini
    address setup script
420 702eff21 Andrea Spadaccini
  @raise RPCFail: in case of errors during the IP turndown
421 96e0d5cc Andrea Spadaccini

422 a8083063 Iustin Pop
  """
423 702eff21 Andrea Spadaccini
  _RunMasterSetupScript(master_params, _MASTER_STOP,
424 702eff21 Andrea Spadaccini
                        use_external_mip_script)
425 b1b6ea87 Iustin Pop
426 fb460cf7 Andrea Spadaccini
427 fb460cf7 Andrea Spadaccini
def StopMasterDaemons():
428 fb460cf7 Andrea Spadaccini
  """Stop the master daemons on this node.
429 fb460cf7 Andrea Spadaccini

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

432 fb460cf7 Andrea Spadaccini
  @rtype: None
433 fb460cf7 Andrea Spadaccini

434 fb460cf7 Andrea Spadaccini
  """
435 fb460cf7 Andrea Spadaccini
  # TODO: log and report back to the caller the error failures; we
436 fb460cf7 Andrea Spadaccini
  # need to decide in which case we fail the RPC for this
437 fb460cf7 Andrea Spadaccini
438 710f30ec Michael Hanselmann
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop-master"])
439 fb460cf7 Andrea Spadaccini
  if result.failed:
440 fb460cf7 Andrea Spadaccini
    logging.error("Could not stop Ganeti master, command %s had exitcode %s"
441 fb460cf7 Andrea Spadaccini
                  " and error %s",
442 fb460cf7 Andrea Spadaccini
                  result.cmd, result.exit_code, result.output)
443 a8083063 Iustin Pop
444 a8083063 Iustin Pop
445 41e079ce Andrea Spadaccini
def ChangeMasterNetmask(old_netmask, netmask, master_ip, master_netdev):
446 5a8648eb Andrea Spadaccini
  """Change the netmask of the master IP.
447 5a8648eb Andrea Spadaccini

448 41e079ce Andrea Spadaccini
  @param old_netmask: the old value of the netmask
449 41e079ce Andrea Spadaccini
  @param netmask: the new value of the netmask
450 41e079ce Andrea Spadaccini
  @param master_ip: the master IP
451 41e079ce Andrea Spadaccini
  @param master_netdev: the master network device
452 41e079ce Andrea Spadaccini

453 5a8648eb Andrea Spadaccini
  """
454 5a8648eb Andrea Spadaccini
  if old_netmask == netmask:
455 5a8648eb Andrea Spadaccini
    return
456 5a8648eb Andrea Spadaccini
457 9e6014b9 Andrea Spadaccini
  if not netutils.IPAddress.Own(master_ip):
458 9e6014b9 Andrea Spadaccini
    _Fail("The master IP address is not up, not attempting to change its"
459 9e6014b9 Andrea Spadaccini
          " netmask")
460 9e6014b9 Andrea Spadaccini
461 5a8648eb Andrea Spadaccini
  result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "add",
462 5a8648eb Andrea Spadaccini
                         "%s/%s" % (master_ip, netmask),
463 5a8648eb Andrea Spadaccini
                         "dev", master_netdev, "label",
464 5a8648eb Andrea Spadaccini
                         "%s:0" % master_netdev])
465 5a8648eb Andrea Spadaccini
  if result.failed:
466 9e6014b9 Andrea Spadaccini
    _Fail("Could not set the new netmask on the master IP address")
467 5a8648eb Andrea Spadaccini
468 5a8648eb Andrea Spadaccini
  result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "del",
469 5a8648eb Andrea Spadaccini
                         "%s/%s" % (master_ip, old_netmask),
470 5a8648eb Andrea Spadaccini
                         "dev", master_netdev, "label",
471 5a8648eb Andrea Spadaccini
                         "%s:0" % master_netdev])
472 5a8648eb Andrea Spadaccini
  if result.failed:
473 9e6014b9 Andrea Spadaccini
    _Fail("Could not bring down the master IP address with the old netmask")
474 5a8648eb Andrea Spadaccini
475 5a8648eb Andrea Spadaccini
476 19ddc57a René Nussbaumer
def EtcHostsModify(mode, host, ip):
477 19ddc57a René Nussbaumer
  """Modify a host entry in /etc/hosts.
478 19ddc57a René Nussbaumer

479 19ddc57a René Nussbaumer
  @param mode: The mode to operate. Either add or remove entry
480 19ddc57a René Nussbaumer
  @param host: The host to operate on
481 19ddc57a René Nussbaumer
  @param ip: The ip associated with the entry
482 19ddc57a René Nussbaumer

483 19ddc57a René Nussbaumer
  """
484 19ddc57a René Nussbaumer
  if mode == constants.ETC_HOSTS_ADD:
485 19ddc57a René Nussbaumer
    if not ip:
486 19ddc57a René Nussbaumer
      RPCFail("Mode 'add' needs 'ip' parameter, but parameter not"
487 19ddc57a René Nussbaumer
              " present")
488 19ddc57a René Nussbaumer
    utils.AddHostToEtcHosts(host, ip)
489 19ddc57a René Nussbaumer
  elif mode == constants.ETC_HOSTS_REMOVE:
490 19ddc57a René Nussbaumer
    if ip:
491 19ddc57a René Nussbaumer
      RPCFail("Mode 'remove' does not allow 'ip' parameter, but"
492 19ddc57a René Nussbaumer
              " parameter is present")
493 19ddc57a René Nussbaumer
    utils.RemoveHostFromEtcHosts(host)
494 19ddc57a René Nussbaumer
  else:
495 19ddc57a René Nussbaumer
    RPCFail("Mode not supported")
496 19ddc57a René Nussbaumer
497 19ddc57a René Nussbaumer
498 b989b9d9 Ken Wehr
def LeaveCluster(modify_ssh_setup):
499 10c2650b Iustin Pop
  """Cleans up and remove the current node.
500 10c2650b Iustin Pop

501 10c2650b Iustin Pop
  This function cleans up and prepares the current node to be removed
502 10c2650b Iustin Pop
  from the cluster.
503 10c2650b Iustin Pop

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

508 b989b9d9 Ken Wehr
  @param modify_ssh_setup: boolean
509 b989b9d9 Ken Wehr

510 a8083063 Iustin Pop
  """
511 710f30ec Michael Hanselmann
  _CleanDirectory(pathutils.DATA_DIR)
512 710f30ec Michael Hanselmann
  _CleanDirectory(pathutils.CRYPTO_KEYS_DIR)
513 1bc59f76 Michael Hanselmann
  JobQueuePurge()
514 f78346f5 Michael Hanselmann
515 b989b9d9 Ken Wehr
  if modify_ssh_setup:
516 b989b9d9 Ken Wehr
    try:
517 052783ff Michael Hanselmann
      priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.SSH_LOGIN_USER)
518 7900ed01 Iustin Pop
519 b989b9d9 Ken Wehr
      utils.RemoveAuthorizedKey(auth_keys, utils.ReadFile(pub_key))
520 a8083063 Iustin Pop
521 b989b9d9 Ken Wehr
      utils.RemoveFile(priv_key)
522 b989b9d9 Ken Wehr
      utils.RemoveFile(pub_key)
523 b989b9d9 Ken Wehr
    except errors.OpExecError:
524 b989b9d9 Ken Wehr
      logging.exception("Error while processing ssh files")
525 a8083063 Iustin Pop
526 ed008420 Guido Trotter
  try:
527 710f30ec Michael Hanselmann
    utils.RemoveFile(pathutils.CONFD_HMAC_KEY)
528 710f30ec Michael Hanselmann
    utils.RemoveFile(pathutils.RAPI_CERT_FILE)
529 710f30ec Michael Hanselmann
    utils.RemoveFile(pathutils.SPICE_CERT_FILE)
530 710f30ec Michael Hanselmann
    utils.RemoveFile(pathutils.SPICE_CACERT_FILE)
531 710f30ec Michael Hanselmann
    utils.RemoveFile(pathutils.NODED_CERT_FILE)
532 b459a848 Andrea Spadaccini
  except: # pylint: disable=W0702
533 ed008420 Guido Trotter
    logging.exception("Error while removing cluster secrets")
534 ed008420 Guido Trotter
535 710f30ec Michael Hanselmann
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop", constants.CONFD])
536 f154a7a3 Michael Hanselmann
  if result.failed:
537 f154a7a3 Michael Hanselmann
    logging.error("Command %s failed with exitcode %s and error %s",
538 f154a7a3 Michael Hanselmann
                  result.cmd, result.exit_code, result.output)
539 ed008420 Guido Trotter
540 0623d351 Iustin Pop
  # Raise a custom exception (handled in ganeti-noded)
541 d0c8c01d Iustin Pop
  raise errors.QuitGanetiException(True, "Shutdown scheduled")
542 6d8b6238 Guido Trotter
543 a8083063 Iustin Pop
544 78519c10 Michael Hanselmann
def _GetVgInfo(name):
545 78519c10 Michael Hanselmann
  """Retrieves information about a LVM volume group.
546 78519c10 Michael Hanselmann

547 78519c10 Michael Hanselmann
  """
548 78519c10 Michael Hanselmann
  # TODO: GetVGInfo supports returning information for multiple VGs at once
549 78519c10 Michael Hanselmann
  vginfo = bdev.LogicalVolume.GetVGInfo([name])
550 78519c10 Michael Hanselmann
  if vginfo:
551 78519c10 Michael Hanselmann
    vg_free = int(round(vginfo[0][0], 0))
552 78519c10 Michael Hanselmann
    vg_size = int(round(vginfo[0][1], 0))
553 78519c10 Michael Hanselmann
  else:
554 78519c10 Michael Hanselmann
    vg_free = None
555 78519c10 Michael Hanselmann
    vg_size = None
556 78519c10 Michael Hanselmann
557 78519c10 Michael Hanselmann
  return {
558 78519c10 Michael Hanselmann
    "name": name,
559 1e89a135 Michael Hanselmann
    "vg_free": vg_free,
560 1e89a135 Michael Hanselmann
    "vg_size": vg_size,
561 78519c10 Michael Hanselmann
    }
562 78519c10 Michael Hanselmann
563 78519c10 Michael Hanselmann
564 78519c10 Michael Hanselmann
def _GetHvInfo(name):
565 78519c10 Michael Hanselmann
  """Retrieves node information from a hypervisor.
566 78519c10 Michael Hanselmann

567 78519c10 Michael Hanselmann
  The information returned depends on the hypervisor. Common items:
568 78519c10 Michael Hanselmann

569 78519c10 Michael Hanselmann
    - vg_size is the size of the configured volume group in MiB
570 78519c10 Michael Hanselmann
    - vg_free is the free size of the volume group in MiB
571 78519c10 Michael Hanselmann
    - memory_dom0 is the memory allocated for domain0 in MiB
572 78519c10 Michael Hanselmann
    - memory_free is the currently available (free) ram in MiB
573 78519c10 Michael Hanselmann
    - memory_total is the total number of ram in MiB
574 78519c10 Michael Hanselmann
    - hv_version: the hypervisor version, if available
575 78519c10 Michael Hanselmann

576 78519c10 Michael Hanselmann
  """
577 78519c10 Michael Hanselmann
  return hypervisor.GetHypervisor(name).GetNodeInfo()
578 78519c10 Michael Hanselmann
579 78519c10 Michael Hanselmann
580 78519c10 Michael Hanselmann
def _GetNamedNodeInfo(names, fn):
581 78519c10 Michael Hanselmann
  """Calls C{fn} for all names in C{names} and returns a dictionary.
582 78519c10 Michael Hanselmann

583 78519c10 Michael Hanselmann
  @rtype: None or dict
584 78519c10 Michael Hanselmann

585 78519c10 Michael Hanselmann
  """
586 78519c10 Michael Hanselmann
  if names is None:
587 78519c10 Michael Hanselmann
    return None
588 78519c10 Michael Hanselmann
  else:
589 ff3be305 Michael Hanselmann
    return map(fn, names)
590 78519c10 Michael Hanselmann
591 78519c10 Michael Hanselmann
592 78519c10 Michael Hanselmann
def GetNodeInfo(vg_names, hv_names):
593 5bbd3f7f Michael Hanselmann
  """Gives back a hash with different information about the node.
594 a8083063 Iustin Pop

595 78519c10 Michael Hanselmann
  @type vg_names: list of string
596 78519c10 Michael Hanselmann
  @param vg_names: Names of the volume groups to ask for disk space information
597 78519c10 Michael Hanselmann
  @type hv_names: list of string
598 78519c10 Michael Hanselmann
  @param hv_names: Names of the hypervisors to ask for node information
599 78519c10 Michael Hanselmann
  @rtype: tuple; (string, None/dict, None/dict)
600 78519c10 Michael Hanselmann
  @return: Tuple containing boot ID, volume group information and hypervisor
601 78519c10 Michael Hanselmann
    information
602 a8083063 Iustin Pop

603 098c0958 Michael Hanselmann
  """
604 78519c10 Michael Hanselmann
  bootid = utils.ReadFile(_BOOT_ID_PATH, size=128).rstrip("\n")
605 78519c10 Michael Hanselmann
  vg_info = _GetNamedNodeInfo(vg_names, _GetVgInfo)
606 78519c10 Michael Hanselmann
  hv_info = _GetNamedNodeInfo(hv_names, _GetHvInfo)
607 78519c10 Michael Hanselmann
608 78519c10 Michael Hanselmann
  return (bootid, vg_info, hv_info)
609 a8083063 Iustin Pop
610 a8083063 Iustin Pop
611 62c9ec92 Iustin Pop
def VerifyNode(what, cluster_name):
612 a8083063 Iustin Pop
  """Verify the status of the local node.
613 a8083063 Iustin Pop

614 e69d05fd Iustin Pop
  Based on the input L{what} parameter, various checks are done on the
615 e69d05fd Iustin Pop
  local node.
616 e69d05fd Iustin Pop

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

620 e69d05fd Iustin Pop
  If the I{nodelist} key is present, we check that we have
621 e69d05fd Iustin Pop
  connectivity via ssh with the target nodes (and check the hostname
622 e69d05fd Iustin Pop
  report).
623 a8083063 Iustin Pop

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

628 e69d05fd Iustin Pop
  @type what: C{dict}
629 e69d05fd Iustin Pop
  @param what: a dictionary of things to check:
630 e69d05fd Iustin Pop
      - filelist: list of files for which to compute checksums
631 e69d05fd Iustin Pop
      - nodelist: list of nodes we should check ssh communication with
632 e69d05fd Iustin Pop
      - node-net-test: list of nodes we should check node daemon port
633 e69d05fd Iustin Pop
        connectivity with
634 e69d05fd Iustin Pop
      - hypervisor: list with hypervisors to run the verify for
635 10c2650b Iustin Pop
  @rtype: dict
636 10c2650b Iustin Pop
  @return: a dictionary with the same keys as the input dict, and
637 10c2650b Iustin Pop
      values representing the result of the checks
638 a8083063 Iustin Pop

639 a8083063 Iustin Pop
  """
640 a8083063 Iustin Pop
  result = {}
641 b705c7a6 Manuel Franceschini
  my_name = netutils.Hostname.GetSysName()
642 a744b676 Manuel Franceschini
  port = netutils.GetDaemonPort(constants.NODED)
643 8964ee14 Iustin Pop
  vm_capable = my_name not in what.get(constants.NV_VMNODES, [])
644 a8083063 Iustin Pop
645 8964ee14 Iustin Pop
  if constants.NV_HYPERVISOR in what and vm_capable:
646 25361b9a Iustin Pop
    result[constants.NV_HYPERVISOR] = tmp = {}
647 25361b9a Iustin Pop
    for hv_name in what[constants.NV_HYPERVISOR]:
648 0cf5e7f5 Iustin Pop
      try:
649 0cf5e7f5 Iustin Pop
        val = hypervisor.GetHypervisor(hv_name).Verify()
650 0cf5e7f5 Iustin Pop
      except errors.HypervisorError, err:
651 0cf5e7f5 Iustin Pop
        val = "Error while checking hypervisor: %s" % str(err)
652 0cf5e7f5 Iustin Pop
      tmp[hv_name] = val
653 25361b9a Iustin Pop
654 58a59652 Iustin Pop
  if constants.NV_HVPARAMS in what and vm_capable:
655 58a59652 Iustin Pop
    result[constants.NV_HVPARAMS] = tmp = []
656 58a59652 Iustin Pop
    for source, hv_name, hvparms in what[constants.NV_HVPARAMS]:
657 58a59652 Iustin Pop
      try:
658 58a59652 Iustin Pop
        logging.info("Validating hv %s, %s", hv_name, hvparms)
659 58a59652 Iustin Pop
        hypervisor.GetHypervisor(hv_name).ValidateParameters(hvparms)
660 58a59652 Iustin Pop
      except errors.HypervisorError, err:
661 58a59652 Iustin Pop
        tmp.append((source, hv_name, str(err)))
662 58a59652 Iustin Pop
663 25361b9a Iustin Pop
  if constants.NV_FILELIST in what:
664 47130d50 Michael Hanselmann
    fingerprints = utils.FingerprintFiles(map(vcluster.LocalizeVirtualPath,
665 47130d50 Michael Hanselmann
                                              what[constants.NV_FILELIST]))
666 47130d50 Michael Hanselmann
    result[constants.NV_FILELIST] = \
667 47130d50 Michael Hanselmann
      dict((vcluster.MakeVirtualPath(key), value)
668 47130d50 Michael Hanselmann
           for (key, value) in fingerprints.items())
669 25361b9a Iustin Pop
670 25361b9a Iustin Pop
  if constants.NV_NODELIST in what:
671 64c7b383 Michael Hanselmann
    (nodes, bynode) = what[constants.NV_NODELIST]
672 64c7b383 Michael Hanselmann
673 64c7b383 Michael Hanselmann
    # Add nodes from other groups (different for each node)
674 64c7b383 Michael Hanselmann
    try:
675 64c7b383 Michael Hanselmann
      nodes.extend(bynode[my_name])
676 64c7b383 Michael Hanselmann
    except KeyError:
677 64c7b383 Michael Hanselmann
      pass
678 64c7b383 Michael Hanselmann
679 64c7b383 Michael Hanselmann
    # Use a random order
680 64c7b383 Michael Hanselmann
    random.shuffle(nodes)
681 64c7b383 Michael Hanselmann
682 64c7b383 Michael Hanselmann
    # Try to contact all nodes
683 64c7b383 Michael Hanselmann
    val = {}
684 64c7b383 Michael Hanselmann
    for node in nodes:
685 62c9ec92 Iustin Pop
      success, message = _GetSshRunner(cluster_name).VerifyNodeHostname(node)
686 a8083063 Iustin Pop
      if not success:
687 64c7b383 Michael Hanselmann
        val[node] = message
688 64c7b383 Michael Hanselmann
689 64c7b383 Michael Hanselmann
    result[constants.NV_NODELIST] = val
690 25361b9a Iustin Pop
691 25361b9a Iustin Pop
  if constants.NV_NODENETTEST in what:
692 25361b9a Iustin Pop
    result[constants.NV_NODENETTEST] = tmp = {}
693 9d4bfc96 Iustin Pop
    my_pip = my_sip = None
694 25361b9a Iustin Pop
    for name, pip, sip in what[constants.NV_NODENETTEST]:
695 9d4bfc96 Iustin Pop
      if name == my_name:
696 9d4bfc96 Iustin Pop
        my_pip = pip
697 9d4bfc96 Iustin Pop
        my_sip = sip
698 9d4bfc96 Iustin Pop
        break
699 9d4bfc96 Iustin Pop
    if not my_pip:
700 25361b9a Iustin Pop
      tmp[my_name] = ("Can't find my own primary/secondary IP"
701 25361b9a Iustin Pop
                      " in the node list")
702 9d4bfc96 Iustin Pop
    else:
703 25361b9a Iustin Pop
      for name, pip, sip in what[constants.NV_NODENETTEST]:
704 9d4bfc96 Iustin Pop
        fail = []
705 a744b676 Manuel Franceschini
        if not netutils.TcpPing(pip, port, source=my_pip):
706 9d4bfc96 Iustin Pop
          fail.append("primary")
707 9d4bfc96 Iustin Pop
        if sip != pip:
708 a744b676 Manuel Franceschini
          if not netutils.TcpPing(sip, port, source=my_sip):
709 9d4bfc96 Iustin Pop
            fail.append("secondary")
710 9d4bfc96 Iustin Pop
        if fail:
711 25361b9a Iustin Pop
          tmp[name] = ("failure using the %s interface(s)" %
712 25361b9a Iustin Pop
                       " and ".join(fail))
713 25361b9a Iustin Pop
714 a3a5f850 Iustin Pop
  if constants.NV_MASTERIP in what:
715 a3a5f850 Iustin Pop
    # FIXME: add checks on incoming data structures (here and in the
716 a3a5f850 Iustin Pop
    # rest of the function)
717 a3a5f850 Iustin Pop
    master_name, master_ip = what[constants.NV_MASTERIP]
718 a3a5f850 Iustin Pop
    if master_name == my_name:
719 9769bb78 Manuel Franceschini
      source = constants.IP4_ADDRESS_LOCALHOST
720 a3a5f850 Iustin Pop
    else:
721 a3a5f850 Iustin Pop
      source = None
722 a744b676 Manuel Franceschini
    result[constants.NV_MASTERIP] = netutils.TcpPing(master_ip, port,
723 5ae4945a Iustin Pop
                                                     source=source)
724 a3a5f850 Iustin Pop
725 17b0b812 Andrea Spadaccini
  if constants.NV_USERSCRIPTS in what:
726 17b0b812 Andrea Spadaccini
    result[constants.NV_USERSCRIPTS] = \
727 17b0b812 Andrea Spadaccini
      [script for script in what[constants.NV_USERSCRIPTS]
728 10b86782 Michael Hanselmann
       if not utils.IsExecutable(script)]
729 17b0b812 Andrea Spadaccini
730 16f41f24 René Nussbaumer
  if constants.NV_OOB_PATHS in what:
731 16f41f24 René Nussbaumer
    result[constants.NV_OOB_PATHS] = tmp = []
732 16f41f24 René Nussbaumer
    for path in what[constants.NV_OOB_PATHS]:
733 16f41f24 René Nussbaumer
      try:
734 16f41f24 René Nussbaumer
        st = os.stat(path)
735 16f41f24 René Nussbaumer
      except OSError, err:
736 16f41f24 René Nussbaumer
        tmp.append("error stating out of band helper: %s" % err)
737 16f41f24 René Nussbaumer
      else:
738 16f41f24 René Nussbaumer
        if stat.S_ISREG(st.st_mode):
739 16f41f24 René Nussbaumer
          if stat.S_IMODE(st.st_mode) & stat.S_IXUSR:
740 16f41f24 René Nussbaumer
            tmp.append(None)
741 16f41f24 René Nussbaumer
          else:
742 16f41f24 René Nussbaumer
            tmp.append("out of band helper %s is not executable" % path)
743 16f41f24 René Nussbaumer
        else:
744 16f41f24 René Nussbaumer
          tmp.append("out of band helper %s is not a file" % path)
745 16f41f24 René Nussbaumer
746 8964ee14 Iustin Pop
  if constants.NV_LVLIST in what and vm_capable:
747 ed904904 Iustin Pop
    try:
748 84d7e26b Dmitry Chernyak
      val = GetVolumeList(utils.ListVolumeGroups().keys())
749 ed904904 Iustin Pop
    except RPCFail, err:
750 ed904904 Iustin Pop
      val = str(err)
751 ed904904 Iustin Pop
    result[constants.NV_LVLIST] = val
752 25361b9a Iustin Pop
753 8964ee14 Iustin Pop
  if constants.NV_INSTANCELIST in what and vm_capable:
754 0cf5e7f5 Iustin Pop
    # GetInstanceList can fail
755 0cf5e7f5 Iustin Pop
    try:
756 0cf5e7f5 Iustin Pop
      val = GetInstanceList(what[constants.NV_INSTANCELIST])
757 0cf5e7f5 Iustin Pop
    except RPCFail, err:
758 0cf5e7f5 Iustin Pop
      val = str(err)
759 0cf5e7f5 Iustin Pop
    result[constants.NV_INSTANCELIST] = val
760 25361b9a Iustin Pop
761 8964ee14 Iustin Pop
  if constants.NV_VGLIST in what and vm_capable:
762 e480923b Iustin Pop
    result[constants.NV_VGLIST] = utils.ListVolumeGroups()
763 25361b9a Iustin Pop
764 8964ee14 Iustin Pop
  if constants.NV_PVLIST in what and vm_capable:
765 59726e15 Bernardo Dal Seno
    val = bdev.LogicalVolume.GetPVInfo(what[constants.NV_PVLIST],
766 59726e15 Bernardo Dal Seno
                                       filter_allocatable=False)
767 59726e15 Bernardo Dal Seno
    result[constants.NV_PVLIST] = map(objects.LvmPvInfo.ToDict, val)
768 d091393e Iustin Pop
769 25361b9a Iustin Pop
  if constants.NV_VERSION in what:
770 e9ce0a64 Iustin Pop
    result[constants.NV_VERSION] = (constants.PROTOCOL_VERSION,
771 e9ce0a64 Iustin Pop
                                    constants.RELEASE_VERSION)
772 25361b9a Iustin Pop
773 8964ee14 Iustin Pop
  if constants.NV_HVINFO in what and vm_capable:
774 25361b9a Iustin Pop
    hyper = hypervisor.GetHypervisor(what[constants.NV_HVINFO])
775 25361b9a Iustin Pop
    result[constants.NV_HVINFO] = hyper.GetNodeInfo()
776 9d4bfc96 Iustin Pop
777 8964ee14 Iustin Pop
  if constants.NV_DRBDLIST in what and vm_capable:
778 6d2e83d5 Iustin Pop
    try:
779 6d2e83d5 Iustin Pop
      used_minors = bdev.DRBD8.GetUsedDevs().keys()
780 f6eaed12 Iustin Pop
    except errors.BlockDeviceError, err:
781 6d2e83d5 Iustin Pop
      logging.warning("Can't get used minors list", exc_info=True)
782 f6eaed12 Iustin Pop
      used_minors = str(err)
783 6d2e83d5 Iustin Pop
    result[constants.NV_DRBDLIST] = used_minors
784 6d2e83d5 Iustin Pop
785 8964ee14 Iustin Pop
  if constants.NV_DRBDHELPER in what and vm_capable:
786 7ef40fbe Luca Bigliardi
    status = True
787 7ef40fbe Luca Bigliardi
    try:
788 7ef40fbe Luca Bigliardi
      payload = bdev.BaseDRBD.GetUsermodeHelper()
789 7ef40fbe Luca Bigliardi
    except errors.BlockDeviceError, err:
790 7ef40fbe Luca Bigliardi
      logging.error("Can't get DRBD usermode helper: %s", str(err))
791 7ef40fbe Luca Bigliardi
      status = False
792 7ef40fbe Luca Bigliardi
      payload = str(err)
793 7ef40fbe Luca Bigliardi
    result[constants.NV_DRBDHELPER] = (status, payload)
794 7ef40fbe Luca Bigliardi
795 7c0aa8e9 Iustin Pop
  if constants.NV_NODESETUP in what:
796 7c0aa8e9 Iustin Pop
    result[constants.NV_NODESETUP] = tmpr = []
797 7c0aa8e9 Iustin Pop
    if not os.path.isdir("/sys/block") or not os.path.isdir("/sys/class/net"):
798 7c0aa8e9 Iustin Pop
      tmpr.append("The sysfs filesytem doesn't seem to be mounted"
799 7c0aa8e9 Iustin Pop
                  " under /sys, missing required directories /sys/block"
800 7c0aa8e9 Iustin Pop
                  " and /sys/class/net")
801 7c0aa8e9 Iustin Pop
    if (not os.path.isdir("/proc/sys") or
802 7c0aa8e9 Iustin Pop
        not os.path.isfile("/proc/sysrq-trigger")):
803 7c0aa8e9 Iustin Pop
      tmpr.append("The procfs filesystem doesn't seem to be mounted"
804 7c0aa8e9 Iustin Pop
                  " under /proc, missing required directory /proc/sys and"
805 7c0aa8e9 Iustin Pop
                  " the file /proc/sysrq-trigger")
806 313b2dd4 Michael Hanselmann
807 313b2dd4 Michael Hanselmann
  if constants.NV_TIME in what:
808 313b2dd4 Michael Hanselmann
    result[constants.NV_TIME] = utils.SplitTime(time.time())
809 313b2dd4 Michael Hanselmann
810 8964ee14 Iustin Pop
  if constants.NV_OSLIST in what and vm_capable:
811 b0d85178 Iustin Pop
    result[constants.NV_OSLIST] = DiagnoseOS()
812 b0d85178 Iustin Pop
813 20d317d4 Iustin Pop
  if constants.NV_BRIDGES in what and vm_capable:
814 20d317d4 Iustin Pop
    result[constants.NV_BRIDGES] = [bridge
815 20d317d4 Iustin Pop
                                    for bridge in what[constants.NV_BRIDGES]
816 20d317d4 Iustin Pop
                                    if not utils.BridgeExists(bridge)]
817 23e3c9b7 Michael Hanselmann
818 72b35807 Michael Hanselmann
  if what.get(constants.NV_FILE_STORAGE_PATHS) == my_name:
819 72b35807 Michael Hanselmann
    result[constants.NV_FILE_STORAGE_PATHS] = \
820 72b35807 Michael Hanselmann
      bdev.ComputeWrongFileStoragePaths()
821 72b35807 Michael Hanselmann
822 c26a6bd2 Iustin Pop
  return result
823 a8083063 Iustin Pop
824 a8083063 Iustin Pop
825 2be7273c Apollon Oikonomopoulos
def GetBlockDevSizes(devices):
826 2be7273c Apollon Oikonomopoulos
  """Return the size of the given block devices
827 2be7273c Apollon Oikonomopoulos

828 2be7273c Apollon Oikonomopoulos
  @type devices: list
829 2be7273c Apollon Oikonomopoulos
  @param devices: list of block device nodes to query
830 2be7273c Apollon Oikonomopoulos
  @rtype: dict
831 2be7273c Apollon Oikonomopoulos
  @return:
832 2be7273c Apollon Oikonomopoulos
    dictionary of all block devices under /dev (key). The value is their
833 2be7273c Apollon Oikonomopoulos
    size in MiB.
834 2be7273c Apollon Oikonomopoulos

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

837 2be7273c Apollon Oikonomopoulos
  """
838 2be7273c Apollon Oikonomopoulos
  DEV_PREFIX = "/dev/"
839 2be7273c Apollon Oikonomopoulos
  blockdevs = {}
840 2be7273c Apollon Oikonomopoulos
841 2be7273c Apollon Oikonomopoulos
  for devpath in devices:
842 cf00dba0 René Nussbaumer
    if not utils.IsBelowDir(DEV_PREFIX, devpath):
843 2be7273c Apollon Oikonomopoulos
      continue
844 2be7273c Apollon Oikonomopoulos
845 2be7273c Apollon Oikonomopoulos
    try:
846 2be7273c Apollon Oikonomopoulos
      st = os.stat(devpath)
847 2be7273c Apollon Oikonomopoulos
    except EnvironmentError, err:
848 2be7273c Apollon Oikonomopoulos
      logging.warning("Error stat()'ing device %s: %s", devpath, str(err))
849 2be7273c Apollon Oikonomopoulos
      continue
850 2be7273c Apollon Oikonomopoulos
851 2be7273c Apollon Oikonomopoulos
    if stat.S_ISBLK(st.st_mode):
852 2be7273c Apollon Oikonomopoulos
      result = utils.RunCmd(["blockdev", "--getsize64", devpath])
853 2be7273c Apollon Oikonomopoulos
      if result.failed:
854 2be7273c Apollon Oikonomopoulos
        # We don't want to fail, just do not list this device as available
855 2be7273c Apollon Oikonomopoulos
        logging.warning("Cannot get size for block device %s", devpath)
856 2be7273c Apollon Oikonomopoulos
        continue
857 2be7273c Apollon Oikonomopoulos
858 2be7273c Apollon Oikonomopoulos
      size = int(result.stdout) / (1024 * 1024)
859 2be7273c Apollon Oikonomopoulos
      blockdevs[devpath] = size
860 2be7273c Apollon Oikonomopoulos
  return blockdevs
861 2be7273c Apollon Oikonomopoulos
862 2be7273c Apollon Oikonomopoulos
863 84d7e26b Dmitry Chernyak
def GetVolumeList(vg_names):
864 a8083063 Iustin Pop
  """Compute list of logical volumes and their size.
865 a8083063 Iustin Pop

866 84d7e26b Dmitry Chernyak
  @type vg_names: list
867 397693d3 Iustin Pop
  @param vg_names: the volume groups whose LVs we should list, or
868 397693d3 Iustin Pop
      empty for all volume groups
869 10c2650b Iustin Pop
  @rtype: dict
870 10c2650b Iustin Pop
  @return:
871 10c2650b Iustin Pop
      dictionary of all partions (key) with value being a tuple of
872 10c2650b Iustin Pop
      their size (in MiB), inactive and online status::
873 10c2650b Iustin Pop

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

876 10c2650b Iustin Pop
      in case of errors, a string is returned with the error
877 10c2650b Iustin Pop
      details.
878 a8083063 Iustin Pop

879 a8083063 Iustin Pop
  """
880 cb2037a2 Iustin Pop
  lvs = {}
881 d0c8c01d Iustin Pop
  sep = "|"
882 397693d3 Iustin Pop
  if not vg_names:
883 397693d3 Iustin Pop
    vg_names = []
884 cb2037a2 Iustin Pop
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
885 cb2037a2 Iustin Pop
                         "--separator=%s" % sep,
886 84d7e26b Dmitry Chernyak
                         "-ovg_name,lv_name,lv_size,lv_attr"] + vg_names)
887 a8083063 Iustin Pop
  if result.failed:
888 29d376ec Iustin Pop
    _Fail("Failed to list logical volumes, lvs output: %s", result.output)
889 cb2037a2 Iustin Pop
890 cb2037a2 Iustin Pop
  for line in result.stdout.splitlines():
891 df4c2628 Iustin Pop
    line = line.strip()
892 0b5303da Iustin Pop
    match = _LVSLINE_REGEX.match(line)
893 df4c2628 Iustin Pop
    if not match:
894 18682bca Iustin Pop
      logging.error("Invalid line returned from lvs output: '%s'", line)
895 df4c2628 Iustin Pop
      continue
896 84d7e26b Dmitry Chernyak
    vg_name, name, size, attr = match.groups()
897 d0c8c01d Iustin Pop
    inactive = attr[4] == "-"
898 d0c8c01d Iustin Pop
    online = attr[5] == "o"
899 d0c8c01d Iustin Pop
    virtual = attr[0] == "v"
900 33f2a81a Iustin Pop
    if virtual:
901 33f2a81a Iustin Pop
      # we don't want to report such volumes as existing, since they
902 33f2a81a Iustin Pop
      # don't really hold data
903 33f2a81a Iustin Pop
      continue
904 e687ec01 Michael Hanselmann
    lvs[vg_name + "/" + name] = (size, inactive, online)
905 cb2037a2 Iustin Pop
906 cb2037a2 Iustin Pop
  return lvs
907 a8083063 Iustin Pop
908 a8083063 Iustin Pop
909 a8083063 Iustin Pop
def ListVolumeGroups():
910 2f8598a5 Alexander Schreiber
  """List the volume groups and their size.
911 a8083063 Iustin Pop

912 10c2650b Iustin Pop
  @rtype: dict
913 10c2650b Iustin Pop
  @return: dictionary with keys volume name and values the
914 10c2650b Iustin Pop
      size of the volume
915 a8083063 Iustin Pop

916 a8083063 Iustin Pop
  """
917 c26a6bd2 Iustin Pop
  return utils.ListVolumeGroups()
918 a8083063 Iustin Pop
919 a8083063 Iustin Pop
920 dcb93971 Michael Hanselmann
def NodeVolumes():
921 dcb93971 Michael Hanselmann
  """List all volumes on this node.
922 dcb93971 Michael Hanselmann

923 10c2650b Iustin Pop
  @rtype: list
924 10c2650b Iustin Pop
  @return:
925 10c2650b Iustin Pop
    A list of dictionaries, each having four keys:
926 10c2650b Iustin Pop
      - name: the logical volume name,
927 10c2650b Iustin Pop
      - size: the size of the logical volume
928 10c2650b Iustin Pop
      - dev: the physical device on which the LV lives
929 10c2650b Iustin Pop
      - vg: the volume group to which it belongs
930 10c2650b Iustin Pop

931 10c2650b Iustin Pop
    In case of errors, we return an empty list and log the
932 10c2650b Iustin Pop
    error.
933 10c2650b Iustin Pop

934 10c2650b Iustin Pop
    Note that since a logical volume can live on multiple physical
935 10c2650b Iustin Pop
    volumes, the resulting list might include a logical volume
936 10c2650b Iustin Pop
    multiple times.
937 10c2650b Iustin Pop

938 dcb93971 Michael Hanselmann
  """
939 dcb93971 Michael Hanselmann
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
940 dcb93971 Michael Hanselmann
                         "--separator=|",
941 dcb93971 Michael Hanselmann
                         "--options=lv_name,lv_size,devices,vg_name"])
942 dcb93971 Michael Hanselmann
  if result.failed:
943 10bfe6cb Iustin Pop
    _Fail("Failed to list logical volumes, lvs output: %s",
944 10bfe6cb Iustin Pop
          result.output)
945 dcb93971 Michael Hanselmann
946 dcb93971 Michael Hanselmann
  def parse_dev(dev):
947 d0c8c01d Iustin Pop
    return dev.split("(")[0]
948 89e5ab02 Iustin Pop
949 89e5ab02 Iustin Pop
  def handle_dev(dev):
950 89e5ab02 Iustin Pop
    return [parse_dev(x) for x in dev.split(",")]
951 dcb93971 Michael Hanselmann
952 dcb93971 Michael Hanselmann
  def map_line(line):
953 89e5ab02 Iustin Pop
    line = [v.strip() for v in line]
954 d0c8c01d Iustin Pop
    return [{"name": line[0], "size": line[1],
955 d0c8c01d Iustin Pop
             "dev": dev, "vg": line[3]} for dev in handle_dev(line[2])]
956 89e5ab02 Iustin Pop
957 89e5ab02 Iustin Pop
  all_devs = []
958 89e5ab02 Iustin Pop
  for line in result.stdout.splitlines():
959 d0c8c01d Iustin Pop
    if line.count("|") >= 3:
960 d0c8c01d Iustin Pop
      all_devs.extend(map_line(line.split("|")))
961 89e5ab02 Iustin Pop
    else:
962 89e5ab02 Iustin Pop
      logging.warning("Strange line in the output from lvs: '%s'", line)
963 89e5ab02 Iustin Pop
  return all_devs
964 dcb93971 Michael Hanselmann
965 dcb93971 Michael Hanselmann
966 a8083063 Iustin Pop
def BridgesExist(bridges_list):
967 2f8598a5 Alexander Schreiber
  """Check if a list of bridges exist on the current node.
968 a8083063 Iustin Pop

969 b1206984 Iustin Pop
  @rtype: boolean
970 b1206984 Iustin Pop
  @return: C{True} if all of them exist, C{False} otherwise
971 a8083063 Iustin Pop

972 a8083063 Iustin Pop
  """
973 35c0c8da Iustin Pop
  missing = []
974 a8083063 Iustin Pop
  for bridge in bridges_list:
975 a8083063 Iustin Pop
    if not utils.BridgeExists(bridge):
976 35c0c8da Iustin Pop
      missing.append(bridge)
977 a8083063 Iustin Pop
978 35c0c8da Iustin Pop
  if missing:
979 1f864b60 Iustin Pop
    _Fail("Missing bridges %s", utils.CommaJoin(missing))
980 35c0c8da Iustin Pop
981 a8083063 Iustin Pop
982 e69d05fd Iustin Pop
def GetInstanceList(hypervisor_list):
983 2f8598a5 Alexander Schreiber
  """Provides a list of instances.
984 a8083063 Iustin Pop

985 e69d05fd Iustin Pop
  @type hypervisor_list: list
986 e69d05fd Iustin Pop
  @param hypervisor_list: the list of hypervisors to query information
987 e69d05fd Iustin Pop

988 e69d05fd Iustin Pop
  @rtype: list
989 e69d05fd Iustin Pop
  @return: a list of all running instances on the current node
990 10c2650b Iustin Pop
    - instance1.example.com
991 10c2650b Iustin Pop
    - instance2.example.com
992 a8083063 Iustin Pop

993 098c0958 Michael Hanselmann
  """
994 e69d05fd Iustin Pop
  results = []
995 e69d05fd Iustin Pop
  for hname in hypervisor_list:
996 e69d05fd Iustin Pop
    try:
997 e69d05fd Iustin Pop
      names = hypervisor.GetHypervisor(hname).ListInstances()
998 e69d05fd Iustin Pop
      results.extend(names)
999 e69d05fd Iustin Pop
    except errors.HypervisorError, err:
1000 aca13712 Iustin Pop
      _Fail("Error enumerating instances (hypervisor %s): %s",
1001 aca13712 Iustin Pop
            hname, err, exc=True)
1002 a8083063 Iustin Pop
1003 e69d05fd Iustin Pop
  return results
1004 a8083063 Iustin Pop
1005 a8083063 Iustin Pop
1006 e69d05fd Iustin Pop
def GetInstanceInfo(instance, hname):
1007 5bbd3f7f Michael Hanselmann
  """Gives back the information about an instance as a dictionary.
1008 a8083063 Iustin Pop

1009 e69d05fd Iustin Pop
  @type instance: string
1010 e69d05fd Iustin Pop
  @param instance: the instance name
1011 e69d05fd Iustin Pop
  @type hname: string
1012 e69d05fd Iustin Pop
  @param hname: the hypervisor type of the instance
1013 a8083063 Iustin Pop

1014 e69d05fd Iustin Pop
  @rtype: dict
1015 e69d05fd Iustin Pop
  @return: dictionary with the following keys:
1016 e69d05fd Iustin Pop
      - memory: memory size of instance (int)
1017 e69d05fd Iustin Pop
      - state: xen state of instance (string)
1018 e69d05fd Iustin Pop
      - time: cpu time of instance (float)
1019 1cb97324 Agata Murawska
      - vcpus: the number of vcpus (int)
1020 a8083063 Iustin Pop

1021 098c0958 Michael Hanselmann
  """
1022 a8083063 Iustin Pop
  output = {}
1023 a8083063 Iustin Pop
1024 e69d05fd Iustin Pop
  iinfo = hypervisor.GetHypervisor(hname).GetInstanceInfo(instance)
1025 a8083063 Iustin Pop
  if iinfo is not None:
1026 d0c8c01d Iustin Pop
    output["memory"] = iinfo[2]
1027 1cb97324 Agata Murawska
    output["vcpus"] = iinfo[3]
1028 d0c8c01d Iustin Pop
    output["state"] = iinfo[4]
1029 d0c8c01d Iustin Pop
    output["time"] = iinfo[5]
1030 a8083063 Iustin Pop
1031 c26a6bd2 Iustin Pop
  return output
1032 a8083063 Iustin Pop
1033 a8083063 Iustin Pop
1034 56e7640c Iustin Pop
def GetInstanceMigratable(instance):
1035 56e7640c Iustin Pop
  """Gives whether an instance can be migrated.
1036 56e7640c Iustin Pop

1037 56e7640c Iustin Pop
  @type instance: L{objects.Instance}
1038 56e7640c Iustin Pop
  @param instance: object representing the instance to be checked.
1039 56e7640c Iustin Pop

1040 56e7640c Iustin Pop
  @rtype: tuple
1041 56e7640c Iustin Pop
  @return: tuple of (result, description) where:
1042 56e7640c Iustin Pop
      - result: whether the instance can be migrated or not
1043 56e7640c Iustin Pop
      - description: a description of the issue, if relevant
1044 56e7640c Iustin Pop

1045 56e7640c Iustin Pop
  """
1046 56e7640c Iustin Pop
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1047 afdc3985 Iustin Pop
  iname = instance.name
1048 afdc3985 Iustin Pop
  if iname not in hyper.ListInstances():
1049 afdc3985 Iustin Pop
    _Fail("Instance %s is not running", iname)
1050 56e7640c Iustin Pop
1051 56e7640c Iustin Pop
  for idx in range(len(instance.disks)):
1052 afdc3985 Iustin Pop
    link_name = _GetBlockDevSymlinkPath(iname, idx)
1053 56e7640c Iustin Pop
    if not os.path.islink(link_name):
1054 b8ebd37b Iustin Pop
      logging.warning("Instance %s is missing symlink %s for disk %d",
1055 b8ebd37b Iustin Pop
                      iname, link_name, idx)
1056 56e7640c Iustin Pop
1057 56e7640c Iustin Pop
1058 e69d05fd Iustin Pop
def GetAllInstancesInfo(hypervisor_list):
1059 a8083063 Iustin Pop
  """Gather data about all instances.
1060 a8083063 Iustin Pop

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

1065 e69d05fd Iustin Pop
  @type hypervisor_list: list
1066 e69d05fd Iustin Pop
  @param hypervisor_list: list of hypervisors to query for instance data
1067 e69d05fd Iustin Pop

1068 955db481 Guido Trotter
  @rtype: dict
1069 e69d05fd Iustin Pop
  @return: dictionary of instance: data, with data having the following keys:
1070 e69d05fd Iustin Pop
      - memory: memory size of instance (int)
1071 e69d05fd Iustin Pop
      - state: xen state of instance (string)
1072 e69d05fd Iustin Pop
      - time: cpu time of instance (float)
1073 10c2650b Iustin Pop
      - vcpus: the number of vcpus
1074 a8083063 Iustin Pop

1075 098c0958 Michael Hanselmann
  """
1076 a8083063 Iustin Pop
  output = {}
1077 a8083063 Iustin Pop
1078 e69d05fd Iustin Pop
  for hname in hypervisor_list:
1079 e69d05fd Iustin Pop
    iinfo = hypervisor.GetHypervisor(hname).GetAllInstancesInfo()
1080 e69d05fd Iustin Pop
    if iinfo:
1081 29921401 Iustin Pop
      for name, _, memory, vcpus, state, times in iinfo:
1082 f23b5ae8 Iustin Pop
        value = {
1083 d0c8c01d Iustin Pop
          "memory": memory,
1084 d0c8c01d Iustin Pop
          "vcpus": vcpus,
1085 d0c8c01d Iustin Pop
          "state": state,
1086 d0c8c01d Iustin Pop
          "time": times,
1087 e69d05fd Iustin Pop
          }
1088 b33b6f55 Iustin Pop
        if name in output:
1089 b33b6f55 Iustin Pop
          # we only check static parameters, like memory and vcpus,
1090 b33b6f55 Iustin Pop
          # and not state and time which can change between the
1091 b33b6f55 Iustin Pop
          # invocations of the different hypervisors
1092 d0c8c01d Iustin Pop
          for key in "memory", "vcpus":
1093 b33b6f55 Iustin Pop
            if value[key] != output[name][key]:
1094 2fa74ef4 Iustin Pop
              _Fail("Instance %s is running twice"
1095 2fa74ef4 Iustin Pop
                    " with different parameters", name)
1096 f23b5ae8 Iustin Pop
        output[name] = value
1097 a8083063 Iustin Pop
1098 c26a6bd2 Iustin Pop
  return output
1099 a8083063 Iustin Pop
1100 a8083063 Iustin Pop
1101 6aa7a354 Iustin Pop
def _InstanceLogName(kind, os_name, instance, component):
1102 81a3406c Iustin Pop
  """Compute the OS log filename for a given instance and operation.
1103 81a3406c Iustin Pop

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

1107 81a3406c Iustin Pop
  @type kind: string
1108 81a3406c Iustin Pop
  @param kind: the operation type (e.g. add, import, etc.)
1109 81a3406c Iustin Pop
  @type os_name: string
1110 81a3406c Iustin Pop
  @param os_name: the os name
1111 81a3406c Iustin Pop
  @type instance: string
1112 81a3406c Iustin Pop
  @param instance: the name of the instance being imported/added/etc.
1113 6aa7a354 Iustin Pop
  @type component: string or None
1114 6aa7a354 Iustin Pop
  @param component: the name of the component of the instance being
1115 6aa7a354 Iustin Pop
      transferred
1116 81a3406c Iustin Pop

1117 81a3406c Iustin Pop
  """
1118 1651d116 Michael Hanselmann
  # TODO: Use tempfile.mkstemp to create unique filename
1119 6aa7a354 Iustin Pop
  if component:
1120 6aa7a354 Iustin Pop
    assert "/" not in component
1121 6aa7a354 Iustin Pop
    c_msg = "-%s" % component
1122 6aa7a354 Iustin Pop
  else:
1123 6aa7a354 Iustin Pop
    c_msg = ""
1124 6aa7a354 Iustin Pop
  base = ("%s-%s-%s%s-%s.log" %
1125 6aa7a354 Iustin Pop
          (kind, os_name, instance, c_msg, utils.TimestampForFilename()))
1126 710f30ec Michael Hanselmann
  return utils.PathJoin(pathutils.LOG_OS_DIR, base)
1127 81a3406c Iustin Pop
1128 81a3406c Iustin Pop
1129 4a0e011f Iustin Pop
def InstanceOsAdd(instance, reinstall, debug):
1130 2f8598a5 Alexander Schreiber
  """Add an OS to an instance.
1131 a8083063 Iustin Pop

1132 d15a9ad3 Guido Trotter
  @type instance: L{objects.Instance}
1133 d15a9ad3 Guido Trotter
  @param instance: Instance whose OS is to be installed
1134 e557bae9 Guido Trotter
  @type reinstall: boolean
1135 e557bae9 Guido Trotter
  @param reinstall: whether this is an instance reinstall
1136 4a0e011f Iustin Pop
  @type debug: integer
1137 4a0e011f Iustin Pop
  @param debug: debug level, passed to the OS scripts
1138 c26a6bd2 Iustin Pop
  @rtype: None
1139 a8083063 Iustin Pop

1140 a8083063 Iustin Pop
  """
1141 255dcebd Iustin Pop
  inst_os = OSFromDisk(instance.os)
1142 255dcebd Iustin Pop
1143 4a0e011f Iustin Pop
  create_env = OSEnvironment(instance, inst_os, debug)
1144 e557bae9 Guido Trotter
  if reinstall:
1145 d0c8c01d Iustin Pop
    create_env["INSTANCE_REINSTALL"] = "1"
1146 a8083063 Iustin Pop
1147 6aa7a354 Iustin Pop
  logfile = _InstanceLogName("add", instance.os, instance.name, None)
1148 decd5f45 Iustin Pop
1149 d868edb4 Iustin Pop
  result = utils.RunCmd([inst_os.create_script], env=create_env,
1150 896a03f6 Iustin Pop
                        cwd=inst_os.path, output=logfile, reset_env=True)
1151 decd5f45 Iustin Pop
  if result.failed:
1152 18682bca Iustin Pop
    logging.error("os create command '%s' returned error: %s, logfile: %s,"
1153 d868edb4 Iustin Pop
                  " output: %s", result.cmd, result.fail_reason, logfile,
1154 18682bca Iustin Pop
                  result.output)
1155 26f15862 Iustin Pop
    lines = [utils.SafeEncode(val)
1156 20e01edd Iustin Pop
             for val in utils.TailFile(logfile, lines=20)]
1157 afdc3985 Iustin Pop
    _Fail("OS create script failed (%s), last lines in the"
1158 afdc3985 Iustin Pop
          " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
1159 decd5f45 Iustin Pop
1160 decd5f45 Iustin Pop
1161 4a0e011f Iustin Pop
def RunRenameInstance(instance, old_name, debug):
1162 decd5f45 Iustin Pop
  """Run the OS rename script for an instance.
1163 decd5f45 Iustin Pop

1164 b1206984 Iustin Pop
  @type instance: L{objects.Instance}
1165 d15a9ad3 Guido Trotter
  @param instance: Instance whose OS is to be installed
1166 d15a9ad3 Guido Trotter
  @type old_name: string
1167 d15a9ad3 Guido Trotter
  @param old_name: previous instance name
1168 4a0e011f Iustin Pop
  @type debug: integer
1169 4a0e011f Iustin Pop
  @param debug: debug level, passed to the OS scripts
1170 10c2650b Iustin Pop
  @rtype: boolean
1171 10c2650b Iustin Pop
  @return: the success of the operation
1172 decd5f45 Iustin Pop

1173 decd5f45 Iustin Pop
  """
1174 decd5f45 Iustin Pop
  inst_os = OSFromDisk(instance.os)
1175 decd5f45 Iustin Pop
1176 4a0e011f Iustin Pop
  rename_env = OSEnvironment(instance, inst_os, debug)
1177 d0c8c01d Iustin Pop
  rename_env["OLD_INSTANCE_NAME"] = old_name
1178 decd5f45 Iustin Pop
1179 81a3406c Iustin Pop
  logfile = _InstanceLogName("rename", instance.os,
1180 6aa7a354 Iustin Pop
                             "%s-%s" % (old_name, instance.name), None)
1181 a8083063 Iustin Pop
1182 d868edb4 Iustin Pop
  result = utils.RunCmd([inst_os.rename_script], env=rename_env,
1183 896a03f6 Iustin Pop
                        cwd=inst_os.path, output=logfile, reset_env=True)
1184 a8083063 Iustin Pop
1185 a8083063 Iustin Pop
  if result.failed:
1186 18682bca Iustin Pop
    logging.error("os create command '%s' returned error: %s output: %s",
1187 d868edb4 Iustin Pop
                  result.cmd, result.fail_reason, result.output)
1188 26f15862 Iustin Pop
    lines = [utils.SafeEncode(val)
1189 96841384 Iustin Pop
             for val in utils.TailFile(logfile, lines=20)]
1190 afdc3985 Iustin Pop
    _Fail("OS rename script failed (%s), last lines in the"
1191 afdc3985 Iustin Pop
          " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
1192 a8083063 Iustin Pop
1193 a8083063 Iustin Pop
1194 5282084b Iustin Pop
def _GetBlockDevSymlinkPath(instance_name, idx):
1195 710f30ec Michael Hanselmann
  return utils.PathJoin(pathutils.DISK_LINKS_DIR, "%s%s%d" %
1196 3536c792 Iustin Pop
                        (instance_name, constants.DISK_SEPARATOR, idx))
1197 5282084b Iustin Pop
1198 5282084b Iustin Pop
1199 5282084b Iustin Pop
def _SymlinkBlockDev(instance_name, device_path, idx):
1200 9332fd8a Iustin Pop
  """Set up symlinks to a instance's block device.
1201 9332fd8a Iustin Pop

1202 9332fd8a Iustin Pop
  This is an auxiliary function run when an instance is start (on the primary
1203 9332fd8a Iustin Pop
  node) or when an instance is migrated (on the target node).
1204 9332fd8a Iustin Pop

1205 9332fd8a Iustin Pop

1206 5282084b Iustin Pop
  @param instance_name: the name of the target instance
1207 5282084b Iustin Pop
  @param device_path: path of the physical block device, on the node
1208 5282084b Iustin Pop
  @param idx: the disk index
1209 5282084b Iustin Pop
  @return: absolute path to the disk's symlink
1210 9332fd8a Iustin Pop

1211 9332fd8a Iustin Pop
  """
1212 5282084b Iustin Pop
  link_name = _GetBlockDevSymlinkPath(instance_name, idx)
1213 9332fd8a Iustin Pop
  try:
1214 9332fd8a Iustin Pop
    os.symlink(device_path, link_name)
1215 5282084b Iustin Pop
  except OSError, err:
1216 5282084b Iustin Pop
    if err.errno == errno.EEXIST:
1217 9332fd8a Iustin Pop
      if (not os.path.islink(link_name) or
1218 9332fd8a Iustin Pop
          os.readlink(link_name) != device_path):
1219 9332fd8a Iustin Pop
        os.remove(link_name)
1220 9332fd8a Iustin Pop
        os.symlink(device_path, link_name)
1221 9332fd8a Iustin Pop
    else:
1222 9332fd8a Iustin Pop
      raise
1223 9332fd8a Iustin Pop
1224 9332fd8a Iustin Pop
  return link_name
1225 9332fd8a Iustin Pop
1226 9332fd8a Iustin Pop
1227 5282084b Iustin Pop
def _RemoveBlockDevLinks(instance_name, disks):
1228 3c9c571d Iustin Pop
  """Remove the block device symlinks belonging to the given instance.
1229 3c9c571d Iustin Pop

1230 3c9c571d Iustin Pop
  """
1231 29921401 Iustin Pop
  for idx, _ in enumerate(disks):
1232 5282084b Iustin Pop
    link_name = _GetBlockDevSymlinkPath(instance_name, idx)
1233 5282084b Iustin Pop
    if os.path.islink(link_name):
1234 3c9c571d Iustin Pop
      try:
1235 03dfa658 Iustin Pop
        os.remove(link_name)
1236 03dfa658 Iustin Pop
      except OSError:
1237 03dfa658 Iustin Pop
        logging.exception("Can't remove symlink '%s'", link_name)
1238 3c9c571d Iustin Pop
1239 3c9c571d Iustin Pop
1240 9332fd8a Iustin Pop
def _GatherAndLinkBlockDevs(instance):
1241 a8083063 Iustin Pop
  """Set up an instance's block device(s).
1242 a8083063 Iustin Pop

1243 a8083063 Iustin Pop
  This is run on the primary node at instance startup. The block
1244 a8083063 Iustin Pop
  devices must be already assembled.
1245 a8083063 Iustin Pop

1246 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1247 10c2650b Iustin Pop
  @param instance: the instance whose disks we shoul assemble
1248 069cfbf1 Iustin Pop
  @rtype: list
1249 069cfbf1 Iustin Pop
  @return: list of (disk_object, device_path)
1250 10c2650b Iustin Pop

1251 a8083063 Iustin Pop
  """
1252 a8083063 Iustin Pop
  block_devices = []
1253 9332fd8a Iustin Pop
  for idx, disk in enumerate(instance.disks):
1254 a8083063 Iustin Pop
    device = _RecursiveFindBD(disk)
1255 a8083063 Iustin Pop
    if device is None:
1256 a8083063 Iustin Pop
      raise errors.BlockDeviceError("Block device '%s' is not set up." %
1257 a8083063 Iustin Pop
                                    str(disk))
1258 a8083063 Iustin Pop
    device.Open()
1259 9332fd8a Iustin Pop
    try:
1260 5282084b Iustin Pop
      link_name = _SymlinkBlockDev(instance.name, device.dev_path, idx)
1261 9332fd8a Iustin Pop
    except OSError, e:
1262 9332fd8a Iustin Pop
      raise errors.BlockDeviceError("Cannot create block device symlink: %s" %
1263 9332fd8a Iustin Pop
                                    e.strerror)
1264 9332fd8a Iustin Pop
1265 9332fd8a Iustin Pop
    block_devices.append((disk, link_name))
1266 9332fd8a Iustin Pop
1267 a8083063 Iustin Pop
  return block_devices
1268 a8083063 Iustin Pop
1269 a8083063 Iustin Pop
1270 323f9095 Stephen Shirley
def StartInstance(instance, startup_paused):
1271 a8083063 Iustin Pop
  """Start an instance.
1272 a8083063 Iustin Pop

1273 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1274 e69d05fd Iustin Pop
  @param instance: the instance object
1275 323f9095 Stephen Shirley
  @type startup_paused: bool
1276 323f9095 Stephen Shirley
  @param instance: pause instance at startup?
1277 c26a6bd2 Iustin Pop
  @rtype: None
1278 a8083063 Iustin Pop

1279 098c0958 Michael Hanselmann
  """
1280 e69d05fd Iustin Pop
  running_instances = GetInstanceList([instance.hypervisor])
1281 a8083063 Iustin Pop
1282 a8083063 Iustin Pop
  if instance.name in running_instances:
1283 c26a6bd2 Iustin Pop
    logging.info("Instance %s already running, not starting", instance.name)
1284 c26a6bd2 Iustin Pop
    return
1285 a8083063 Iustin Pop
1286 a8083063 Iustin Pop
  try:
1287 ec596c24 Iustin Pop
    block_devices = _GatherAndLinkBlockDevs(instance)
1288 ec596c24 Iustin Pop
    hyper = hypervisor.GetHypervisor(instance.hypervisor)
1289 323f9095 Stephen Shirley
    hyper.StartInstance(instance, block_devices, startup_paused)
1290 ec596c24 Iustin Pop
  except errors.BlockDeviceError, err:
1291 2cc6781a Iustin Pop
    _Fail("Block device error: %s", err, exc=True)
1292 a8083063 Iustin Pop
  except errors.HypervisorError, err:
1293 5282084b Iustin Pop
    _RemoveBlockDevLinks(instance.name, instance.disks)
1294 2cc6781a Iustin Pop
    _Fail("Hypervisor error: %s", err, exc=True)
1295 a8083063 Iustin Pop
1296 a8083063 Iustin Pop
1297 6263189c Guido Trotter
def InstanceShutdown(instance, timeout):
1298 a8083063 Iustin Pop
  """Shut an instance down.
1299 a8083063 Iustin Pop

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

1302 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1303 e69d05fd Iustin Pop
  @param instance: the instance object
1304 6263189c Guido Trotter
  @type timeout: integer
1305 6263189c Guido Trotter
  @param timeout: maximum timeout for soft shutdown
1306 c26a6bd2 Iustin Pop
  @rtype: None
1307 a8083063 Iustin Pop

1308 098c0958 Michael Hanselmann
  """
1309 e69d05fd Iustin Pop
  hv_name = instance.hypervisor
1310 e4e9b806 Guido Trotter
  hyper = hypervisor.GetHypervisor(hv_name)
1311 c26a6bd2 Iustin Pop
  iname = instance.name
1312 a8083063 Iustin Pop
1313 3c0cdc83 Michael Hanselmann
  if instance.name not in hyper.ListInstances():
1314 c26a6bd2 Iustin Pop
    logging.info("Instance %s not running, doing nothing", iname)
1315 c26a6bd2 Iustin Pop
    return
1316 a8083063 Iustin Pop
1317 3c0cdc83 Michael Hanselmann
  class _TryShutdown:
1318 3c0cdc83 Michael Hanselmann
    def __init__(self):
1319 3c0cdc83 Michael Hanselmann
      self.tried_once = False
1320 a8083063 Iustin Pop
1321 3c0cdc83 Michael Hanselmann
    def __call__(self):
1322 3c0cdc83 Michael Hanselmann
      if iname not in hyper.ListInstances():
1323 3c0cdc83 Michael Hanselmann
        return
1324 3c0cdc83 Michael Hanselmann
1325 3c0cdc83 Michael Hanselmann
      try:
1326 3c0cdc83 Michael Hanselmann
        hyper.StopInstance(instance, retry=self.tried_once)
1327 3c0cdc83 Michael Hanselmann
      except errors.HypervisorError, err:
1328 3c0cdc83 Michael Hanselmann
        if iname not in hyper.ListInstances():
1329 3c0cdc83 Michael Hanselmann
          # if the instance is no longer existing, consider this a
1330 3c0cdc83 Michael Hanselmann
          # success and go to cleanup
1331 3c0cdc83 Michael Hanselmann
          return
1332 3c0cdc83 Michael Hanselmann
1333 3c0cdc83 Michael Hanselmann
        _Fail("Failed to stop instance %s: %s", iname, err)
1334 3c0cdc83 Michael Hanselmann
1335 3c0cdc83 Michael Hanselmann
      self.tried_once = True
1336 3c0cdc83 Michael Hanselmann
1337 3c0cdc83 Michael Hanselmann
      raise utils.RetryAgain()
1338 3c0cdc83 Michael Hanselmann
1339 3c0cdc83 Michael Hanselmann
  try:
1340 3c0cdc83 Michael Hanselmann
    utils.Retry(_TryShutdown(), 5, timeout)
1341 3c0cdc83 Michael Hanselmann
  except utils.RetryTimeout:
1342 a8083063 Iustin Pop
    # the shutdown did not succeed
1343 e4e9b806 Guido Trotter
    logging.error("Shutdown of '%s' unsuccessful, forcing", iname)
1344 a8083063 Iustin Pop
1345 a8083063 Iustin Pop
    try:
1346 a8083063 Iustin Pop
      hyper.StopInstance(instance, force=True)
1347 a8083063 Iustin Pop
    except errors.HypervisorError, err:
1348 3c0cdc83 Michael Hanselmann
      if iname in hyper.ListInstances():
1349 3782acd7 Iustin Pop
        # only raise an error if the instance still exists, otherwise
1350 3782acd7 Iustin Pop
        # the error could simply be "instance ... unknown"!
1351 3782acd7 Iustin Pop
        _Fail("Failed to force stop instance %s: %s", iname, err)
1352 a8083063 Iustin Pop
1353 a8083063 Iustin Pop
    time.sleep(1)
1354 3c0cdc83 Michael Hanselmann
1355 3c0cdc83 Michael Hanselmann
    if iname in hyper.ListInstances():
1356 c26a6bd2 Iustin Pop
      _Fail("Could not shutdown instance %s even by destroy", iname)
1357 3c9c571d Iustin Pop
1358 f28ec899 Guido Trotter
  try:
1359 f28ec899 Guido Trotter
    hyper.CleanupInstance(instance.name)
1360 f28ec899 Guido Trotter
  except errors.HypervisorError, err:
1361 f28ec899 Guido Trotter
    logging.warning("Failed to execute post-shutdown cleanup step: %s", err)
1362 f28ec899 Guido Trotter
1363 c26a6bd2 Iustin Pop
  _RemoveBlockDevLinks(iname, instance.disks)
1364 a8083063 Iustin Pop
1365 a8083063 Iustin Pop
1366 17c3f802 Guido Trotter
def InstanceReboot(instance, reboot_type, shutdown_timeout):
1367 007a2f3e Alexander Schreiber
  """Reboot an instance.
1368 007a2f3e Alexander Schreiber

1369 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1370 10c2650b Iustin Pop
  @param instance: the instance object to reboot
1371 10c2650b Iustin Pop
  @type reboot_type: str
1372 10c2650b Iustin Pop
  @param reboot_type: the type of reboot, one the following
1373 10c2650b Iustin Pop
    constants:
1374 10c2650b Iustin Pop
      - L{constants.INSTANCE_REBOOT_SOFT}: only reboot the
1375 10c2650b Iustin Pop
        instance OS, do not recreate the VM
1376 10c2650b Iustin Pop
      - L{constants.INSTANCE_REBOOT_HARD}: tear down and
1377 10c2650b Iustin Pop
        restart the VM (at the hypervisor level)
1378 73e5a4f4 Iustin Pop
      - the other reboot type (L{constants.INSTANCE_REBOOT_FULL}) is
1379 73e5a4f4 Iustin Pop
        not accepted here, since that mode is handled differently, in
1380 73e5a4f4 Iustin Pop
        cmdlib, and translates into full stop and start of the
1381 73e5a4f4 Iustin Pop
        instance (instead of a call_instance_reboot RPC)
1382 23057d29 Michael Hanselmann
  @type shutdown_timeout: integer
1383 23057d29 Michael Hanselmann
  @param shutdown_timeout: maximum timeout for soft shutdown
1384 c26a6bd2 Iustin Pop
  @rtype: None
1385 007a2f3e Alexander Schreiber

1386 007a2f3e Alexander Schreiber
  """
1387 e69d05fd Iustin Pop
  running_instances = GetInstanceList([instance.hypervisor])
1388 007a2f3e Alexander Schreiber
1389 007a2f3e Alexander Schreiber
  if instance.name not in running_instances:
1390 2cc6781a Iustin Pop
    _Fail("Cannot reboot instance %s that is not running", instance.name)
1391 007a2f3e Alexander Schreiber
1392 e69d05fd Iustin Pop
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1393 007a2f3e Alexander Schreiber
  if reboot_type == constants.INSTANCE_REBOOT_SOFT:
1394 007a2f3e Alexander Schreiber
    try:
1395 007a2f3e Alexander Schreiber
      hyper.RebootInstance(instance)
1396 007a2f3e Alexander Schreiber
    except errors.HypervisorError, err:
1397 2cc6781a Iustin Pop
      _Fail("Failed to soft reboot instance %s: %s", instance.name, err)
1398 007a2f3e Alexander Schreiber
  elif reboot_type == constants.INSTANCE_REBOOT_HARD:
1399 007a2f3e Alexander Schreiber
    try:
1400 17c3f802 Guido Trotter
      InstanceShutdown(instance, shutdown_timeout)
1401 82bc21e2 Stephen Shirley
      return StartInstance(instance, False)
1402 007a2f3e Alexander Schreiber
    except errors.HypervisorError, err:
1403 2cc6781a Iustin Pop
      _Fail("Failed to hard reboot instance %s: %s", instance.name, err)
1404 007a2f3e Alexander Schreiber
  else:
1405 2cc6781a Iustin Pop
    _Fail("Invalid reboot_type received: %s", reboot_type)
1406 007a2f3e Alexander Schreiber
1407 007a2f3e Alexander Schreiber
1408 ebe466d8 Guido Trotter
def InstanceBalloonMemory(instance, memory):
1409 ebe466d8 Guido Trotter
  """Resize an instance's memory.
1410 ebe466d8 Guido Trotter

1411 ebe466d8 Guido Trotter
  @type instance: L{objects.Instance}
1412 ebe466d8 Guido Trotter
  @param instance: the instance object
1413 ebe466d8 Guido Trotter
  @type memory: int
1414 ebe466d8 Guido Trotter
  @param memory: new memory amount in MB
1415 ebe466d8 Guido Trotter
  @rtype: None
1416 ebe466d8 Guido Trotter

1417 ebe466d8 Guido Trotter
  """
1418 ebe466d8 Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1419 ebe466d8 Guido Trotter
  running = hyper.ListInstances()
1420 ebe466d8 Guido Trotter
  if instance.name not in running:
1421 ebe466d8 Guido Trotter
    logging.info("Instance %s is not running, cannot balloon", instance.name)
1422 ebe466d8 Guido Trotter
    return
1423 ebe466d8 Guido Trotter
  try:
1424 ebe466d8 Guido Trotter
    hyper.BalloonInstanceMemory(instance, memory)
1425 ebe466d8 Guido Trotter
  except errors.HypervisorError, err:
1426 ebe466d8 Guido Trotter
    _Fail("Failed to balloon instance memory: %s", err, exc=True)
1427 ebe466d8 Guido Trotter
1428 ebe466d8 Guido Trotter
1429 6906a9d8 Guido Trotter
def MigrationInfo(instance):
1430 6906a9d8 Guido Trotter
  """Gather information about an instance to be migrated.
1431 6906a9d8 Guido Trotter

1432 6906a9d8 Guido Trotter
  @type instance: L{objects.Instance}
1433 6906a9d8 Guido Trotter
  @param instance: the instance definition
1434 6906a9d8 Guido Trotter

1435 6906a9d8 Guido Trotter
  """
1436 cd42d0ad Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1437 cd42d0ad Guido Trotter
  try:
1438 cd42d0ad Guido Trotter
    info = hyper.MigrationInfo(instance)
1439 cd42d0ad Guido Trotter
  except errors.HypervisorError, err:
1440 2cc6781a Iustin Pop
    _Fail("Failed to fetch migration information: %s", err, exc=True)
1441 c26a6bd2 Iustin Pop
  return info
1442 6906a9d8 Guido Trotter
1443 6906a9d8 Guido Trotter
1444 6906a9d8 Guido Trotter
def AcceptInstance(instance, info, target):
1445 6906a9d8 Guido Trotter
  """Prepare the node to accept an instance.
1446 6906a9d8 Guido Trotter

1447 6906a9d8 Guido Trotter
  @type instance: L{objects.Instance}
1448 6906a9d8 Guido Trotter
  @param instance: the instance definition
1449 6906a9d8 Guido Trotter
  @type info: string/data (opaque)
1450 6906a9d8 Guido Trotter
  @param info: migration information, from the source node
1451 6906a9d8 Guido Trotter
  @type target: string
1452 6906a9d8 Guido Trotter
  @param target: target host (usually ip), on this node
1453 6906a9d8 Guido Trotter

1454 6906a9d8 Guido Trotter
  """
1455 77fcff4a Apollon Oikonomopoulos
  # TODO: why is this required only for DTS_EXT_MIRROR?
1456 77fcff4a Apollon Oikonomopoulos
  if instance.disk_template in constants.DTS_EXT_MIRROR:
1457 77fcff4a Apollon Oikonomopoulos
    # Create the symlinks, as the disks are not active
1458 77fcff4a Apollon Oikonomopoulos
    # in any way
1459 77fcff4a Apollon Oikonomopoulos
    try:
1460 77fcff4a Apollon Oikonomopoulos
      _GatherAndLinkBlockDevs(instance)
1461 77fcff4a Apollon Oikonomopoulos
    except errors.BlockDeviceError, err:
1462 77fcff4a Apollon Oikonomopoulos
      _Fail("Block device error: %s", err, exc=True)
1463 77fcff4a Apollon Oikonomopoulos
1464 cd42d0ad Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1465 cd42d0ad Guido Trotter
  try:
1466 cd42d0ad Guido Trotter
    hyper.AcceptInstance(instance, info, target)
1467 cd42d0ad Guido Trotter
  except errors.HypervisorError, err:
1468 77fcff4a Apollon Oikonomopoulos
    if instance.disk_template in constants.DTS_EXT_MIRROR:
1469 77fcff4a Apollon Oikonomopoulos
      _RemoveBlockDevLinks(instance.name, instance.disks)
1470 2cc6781a Iustin Pop
    _Fail("Failed to accept instance: %s", err, exc=True)
1471 6906a9d8 Guido Trotter
1472 6906a9d8 Guido Trotter
1473 6a1434d7 Andrea Spadaccini
def FinalizeMigrationDst(instance, info, success):
1474 6906a9d8 Guido Trotter
  """Finalize any preparation to accept an instance.
1475 6906a9d8 Guido Trotter

1476 6906a9d8 Guido Trotter
  @type instance: L{objects.Instance}
1477 6906a9d8 Guido Trotter
  @param instance: the instance definition
1478 6906a9d8 Guido Trotter
  @type info: string/data (opaque)
1479 6906a9d8 Guido Trotter
  @param info: migration information, from the source node
1480 6906a9d8 Guido Trotter
  @type success: boolean
1481 6906a9d8 Guido Trotter
  @param success: whether the migration was a success or a failure
1482 6906a9d8 Guido Trotter

1483 6906a9d8 Guido Trotter
  """
1484 cd42d0ad Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1485 cd42d0ad Guido Trotter
  try:
1486 6a1434d7 Andrea Spadaccini
    hyper.FinalizeMigrationDst(instance, info, success)
1487 cd42d0ad Guido Trotter
  except errors.HypervisorError, err:
1488 6a1434d7 Andrea Spadaccini
    _Fail("Failed to finalize migration on the target node: %s", err, exc=True)
1489 6906a9d8 Guido Trotter
1490 6906a9d8 Guido Trotter
1491 2a10865c Iustin Pop
def MigrateInstance(instance, target, live):
1492 2a10865c Iustin Pop
  """Migrates an instance to another node.
1493 2a10865c Iustin Pop

1494 b1206984 Iustin Pop
  @type instance: L{objects.Instance}
1495 9f0e6b37 Iustin Pop
  @param instance: the instance definition
1496 9f0e6b37 Iustin Pop
  @type target: string
1497 9f0e6b37 Iustin Pop
  @param target: the target node name
1498 9f0e6b37 Iustin Pop
  @type live: boolean
1499 9f0e6b37 Iustin Pop
  @param live: whether the migration should be done live or not (the
1500 9f0e6b37 Iustin Pop
      interpretation of this parameter is left to the hypervisor)
1501 c03fe62b Andrea Spadaccini
  @raise RPCFail: if migration fails for some reason
1502 9f0e6b37 Iustin Pop

1503 2a10865c Iustin Pop
  """
1504 53c776b5 Iustin Pop
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1505 2a10865c Iustin Pop
1506 2a10865c Iustin Pop
  try:
1507 58d38b02 Iustin Pop
    hyper.MigrateInstance(instance, target, live)
1508 2a10865c Iustin Pop
  except errors.HypervisorError, err:
1509 2cc6781a Iustin Pop
    _Fail("Failed to migrate instance: %s", err, exc=True)
1510 2a10865c Iustin Pop
1511 2a10865c Iustin Pop
1512 6a1434d7 Andrea Spadaccini
def FinalizeMigrationSource(instance, success, live):
1513 6a1434d7 Andrea Spadaccini
  """Finalize the instance migration on the source node.
1514 6a1434d7 Andrea Spadaccini

1515 6a1434d7 Andrea Spadaccini
  @type instance: L{objects.Instance}
1516 6a1434d7 Andrea Spadaccini
  @param instance: the instance definition of the migrated instance
1517 6a1434d7 Andrea Spadaccini
  @type success: bool
1518 6a1434d7 Andrea Spadaccini
  @param success: whether the migration succeeded or not
1519 6a1434d7 Andrea Spadaccini
  @type live: bool
1520 6a1434d7 Andrea Spadaccini
  @param live: whether the user requested a live migration or not
1521 6a1434d7 Andrea Spadaccini
  @raise RPCFail: If the execution fails for some reason
1522 6a1434d7 Andrea Spadaccini

1523 6a1434d7 Andrea Spadaccini
  """
1524 6a1434d7 Andrea Spadaccini
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1525 6a1434d7 Andrea Spadaccini
1526 6a1434d7 Andrea Spadaccini
  try:
1527 6a1434d7 Andrea Spadaccini
    hyper.FinalizeMigrationSource(instance, success, live)
1528 6a1434d7 Andrea Spadaccini
  except Exception, err:  # pylint: disable=W0703
1529 6a1434d7 Andrea Spadaccini
    _Fail("Failed to finalize the migration on the source node: %s", err,
1530 6a1434d7 Andrea Spadaccini
          exc=True)
1531 6a1434d7 Andrea Spadaccini
1532 6a1434d7 Andrea Spadaccini
1533 6a1434d7 Andrea Spadaccini
def GetMigrationStatus(instance):
1534 6a1434d7 Andrea Spadaccini
  """Get the migration status
1535 6a1434d7 Andrea Spadaccini

1536 6a1434d7 Andrea Spadaccini
  @type instance: L{objects.Instance}
1537 6a1434d7 Andrea Spadaccini
  @param instance: the instance that is being migrated
1538 6a1434d7 Andrea Spadaccini
  @rtype: L{objects.MigrationStatus}
1539 6a1434d7 Andrea Spadaccini
  @return: the status of the current migration (one of
1540 6a1434d7 Andrea Spadaccini
           L{constants.HV_MIGRATION_VALID_STATUSES}), plus any additional
1541 6a1434d7 Andrea Spadaccini
           progress info that can be retrieved from the hypervisor
1542 6a1434d7 Andrea Spadaccini
  @raise RPCFail: If the migration status cannot be retrieved
1543 6a1434d7 Andrea Spadaccini

1544 6a1434d7 Andrea Spadaccini
  """
1545 6a1434d7 Andrea Spadaccini
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1546 6a1434d7 Andrea Spadaccini
  try:
1547 6a1434d7 Andrea Spadaccini
    return hyper.GetMigrationStatus(instance)
1548 6a1434d7 Andrea Spadaccini
  except Exception, err:  # pylint: disable=W0703
1549 6a1434d7 Andrea Spadaccini
    _Fail("Failed to get migration status: %s", err, exc=True)
1550 6a1434d7 Andrea Spadaccini
1551 6a1434d7 Andrea Spadaccini
1552 ee1478e5 Bernardo Dal Seno
def BlockdevCreate(disk, size, owner, on_primary, info, excl_stor):
1553 a8083063 Iustin Pop
  """Creates a block device for an instance.
1554 a8083063 Iustin Pop

1555 b1206984 Iustin Pop
  @type disk: L{objects.Disk}
1556 b1206984 Iustin Pop
  @param disk: the object describing the disk we should create
1557 b1206984 Iustin Pop
  @type size: int
1558 b1206984 Iustin Pop
  @param size: the size of the physical underlying device, in MiB
1559 b1206984 Iustin Pop
  @type owner: str
1560 b1206984 Iustin Pop
  @param owner: the name of the instance for which disk is created,
1561 b1206984 Iustin Pop
      used for device cache data
1562 b1206984 Iustin Pop
  @type on_primary: boolean
1563 b1206984 Iustin Pop
  @param on_primary:  indicates if it is the primary node or not
1564 b1206984 Iustin Pop
  @type info: string
1565 b1206984 Iustin Pop
  @param info: string that will be sent to the physical device
1566 b1206984 Iustin Pop
      creation, used for example to set (LVM) tags on LVs
1567 ee1478e5 Bernardo Dal Seno
  @type excl_stor: boolean
1568 ee1478e5 Bernardo Dal Seno
  @param excl_stor: Whether exclusive_storage is active
1569 b1206984 Iustin Pop

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

1574 a8083063 Iustin Pop
  """
1575 d0c8c01d Iustin Pop
  # TODO: remove the obsolete "size" argument
1576 b459a848 Andrea Spadaccini
  # pylint: disable=W0613
1577 a8083063 Iustin Pop
  clist = []
1578 a8083063 Iustin Pop
  if disk.children:
1579 a8083063 Iustin Pop
    for child in disk.children:
1580 1063abd1 Iustin Pop
      try:
1581 1063abd1 Iustin Pop
        crdev = _RecursiveAssembleBD(child, owner, on_primary)
1582 1063abd1 Iustin Pop
      except errors.BlockDeviceError, err:
1583 2cc6781a Iustin Pop
        _Fail("Can't assemble device %s: %s", child, err)
1584 a8083063 Iustin Pop
      if on_primary or disk.AssembleOnSecondary():
1585 a8083063 Iustin Pop
        # we need the children open in case the device itself has to
1586 a8083063 Iustin Pop
        # be assembled
1587 1063abd1 Iustin Pop
        try:
1588 b459a848 Andrea Spadaccini
          # pylint: disable=E1103
1589 1063abd1 Iustin Pop
          crdev.Open()
1590 1063abd1 Iustin Pop
        except errors.BlockDeviceError, err:
1591 2cc6781a Iustin Pop
          _Fail("Can't make child '%s' read-write: %s", child, err)
1592 a8083063 Iustin Pop
      clist.append(crdev)
1593 a8083063 Iustin Pop
1594 dab69e97 Iustin Pop
  try:
1595 ee1478e5 Bernardo Dal Seno
    device = bdev.Create(disk, clist, excl_stor)
1596 1063abd1 Iustin Pop
  except errors.BlockDeviceError, err:
1597 2cc6781a Iustin Pop
    _Fail("Can't create block device: %s", err)
1598 6c626518 Iustin Pop
1599 a8083063 Iustin Pop
  if on_primary or disk.AssembleOnSecondary():
1600 1063abd1 Iustin Pop
    try:
1601 1063abd1 Iustin Pop
      device.Assemble()
1602 1063abd1 Iustin Pop
    except errors.BlockDeviceError, err:
1603 2cc6781a Iustin Pop
      _Fail("Can't assemble device after creation, unusual event: %s", err)
1604 a8083063 Iustin Pop
    if on_primary or disk.OpenOnSecondary():
1605 1063abd1 Iustin Pop
      try:
1606 1063abd1 Iustin Pop
        device.Open(force=True)
1607 1063abd1 Iustin Pop
      except errors.BlockDeviceError, err:
1608 2cc6781a Iustin Pop
        _Fail("Can't make device r/w after creation, unusual event: %s", err)
1609 3f78eef2 Iustin Pop
    DevCacheManager.UpdateCache(device.dev_path, owner,
1610 3f78eef2 Iustin Pop
                                on_primary, disk.iv_name)
1611 a0c3fea1 Michael Hanselmann
1612 a0c3fea1 Michael Hanselmann
  device.SetInfo(info)
1613 a0c3fea1 Michael Hanselmann
1614 c26a6bd2 Iustin Pop
  return device.unique_id
1615 a8083063 Iustin Pop
1616 a8083063 Iustin Pop
1617 da63bb4e René Nussbaumer
def _WipeDevice(path, offset, size):
1618 69dd363f René Nussbaumer
  """This function actually wipes the device.
1619 69dd363f René Nussbaumer

1620 69dd363f René Nussbaumer
  @param path: The path to the device to wipe
1621 da63bb4e René Nussbaumer
  @param offset: The offset in MiB in the file
1622 da63bb4e René Nussbaumer
  @param size: The size in MiB to write
1623 69dd363f René Nussbaumer

1624 69dd363f René Nussbaumer
  """
1625 0188611b Michael Hanselmann
  # Internal sizes are always in Mebibytes; if the following "dd" command
1626 0188611b Michael Hanselmann
  # should use a different block size the offset and size given to this
1627 0188611b Michael Hanselmann
  # function must be adjusted accordingly before being passed to "dd".
1628 0188611b Michael Hanselmann
  block_size = 1024 * 1024
1629 0188611b Michael Hanselmann
1630 da63bb4e René Nussbaumer
  cmd = [constants.DD_CMD, "if=/dev/zero", "seek=%d" % offset,
1631 0188611b Michael Hanselmann
         "bs=%s" % block_size, "oflag=direct", "of=%s" % path,
1632 da63bb4e René Nussbaumer
         "count=%d" % size]
1633 da63bb4e René Nussbaumer
  result = utils.RunCmd(cmd)
1634 69dd363f René Nussbaumer
1635 69dd363f René Nussbaumer
  if result.failed:
1636 69dd363f René Nussbaumer
    _Fail("Wipe command '%s' exited with error: %s; output: %s", result.cmd,
1637 69dd363f René Nussbaumer
          result.fail_reason, result.output)
1638 69dd363f René Nussbaumer
1639 69dd363f René Nussbaumer
1640 da63bb4e René Nussbaumer
def BlockdevWipe(disk, offset, size):
1641 69dd363f René Nussbaumer
  """Wipes a block device.
1642 69dd363f René Nussbaumer

1643 69dd363f René Nussbaumer
  @type disk: L{objects.Disk}
1644 69dd363f René Nussbaumer
  @param disk: the disk object we want to wipe
1645 da63bb4e René Nussbaumer
  @type offset: int
1646 da63bb4e René Nussbaumer
  @param offset: The offset in MiB in the file
1647 da63bb4e René Nussbaumer
  @type size: int
1648 da63bb4e René Nussbaumer
  @param size: The size in MiB to write
1649 69dd363f René Nussbaumer

1650 69dd363f René Nussbaumer
  """
1651 69dd363f René Nussbaumer
  try:
1652 69dd363f René Nussbaumer
    rdev = _RecursiveFindBD(disk)
1653 da63bb4e René Nussbaumer
  except errors.BlockDeviceError:
1654 da63bb4e René Nussbaumer
    rdev = None
1655 da63bb4e René Nussbaumer
1656 da63bb4e René Nussbaumer
  if not rdev:
1657 da63bb4e René Nussbaumer
    _Fail("Cannot execute wipe for device %s: device not found", disk.iv_name)
1658 da63bb4e René Nussbaumer
1659 da63bb4e René Nussbaumer
  # Do cross verify some of the parameters
1660 0188611b Michael Hanselmann
  if offset < 0:
1661 0188611b Michael Hanselmann
    _Fail("Negative offset")
1662 0188611b Michael Hanselmann
  if size < 0:
1663 0188611b Michael Hanselmann
    _Fail("Negative size")
1664 da63bb4e René Nussbaumer
  if offset > rdev.size:
1665 da63bb4e René Nussbaumer
    _Fail("Offset is bigger than device size")
1666 da63bb4e René Nussbaumer
  if (offset + size) > rdev.size:
1667 da63bb4e René Nussbaumer
    _Fail("The provided offset and size to wipe is bigger than device size")
1668 69dd363f René Nussbaumer
1669 da63bb4e René Nussbaumer
  _WipeDevice(rdev.dev_path, offset, size)
1670 69dd363f René Nussbaumer
1671 69dd363f René Nussbaumer
1672 5119c79e René Nussbaumer
def BlockdevPauseResumeSync(disks, pause):
1673 5119c79e René Nussbaumer
  """Pause or resume the sync of the block device.
1674 5119c79e René Nussbaumer

1675 0f39886a René Nussbaumer
  @type disks: list of L{objects.Disk}
1676 0f39886a René Nussbaumer
  @param disks: the disks object we want to pause/resume
1677 5119c79e René Nussbaumer
  @type pause: bool
1678 5119c79e René Nussbaumer
  @param pause: Wheater to pause or resume
1679 5119c79e René Nussbaumer

1680 5119c79e René Nussbaumer
  """
1681 5119c79e René Nussbaumer
  success = []
1682 5119c79e René Nussbaumer
  for disk in disks:
1683 5119c79e René Nussbaumer
    try:
1684 5119c79e René Nussbaumer
      rdev = _RecursiveFindBD(disk)
1685 5119c79e René Nussbaumer
    except errors.BlockDeviceError:
1686 5119c79e René Nussbaumer
      rdev = None
1687 5119c79e René Nussbaumer
1688 5119c79e René Nussbaumer
    if not rdev:
1689 5119c79e René Nussbaumer
      success.append((False, ("Cannot change sync for device %s:"
1690 5119c79e René Nussbaumer
                              " device not found" % disk.iv_name)))
1691 5119c79e René Nussbaumer
      continue
1692 5119c79e René Nussbaumer
1693 5119c79e René Nussbaumer
    result = rdev.PauseResumeSync(pause)
1694 5119c79e René Nussbaumer
1695 5119c79e René Nussbaumer
    if result:
1696 5119c79e René Nussbaumer
      success.append((result, None))
1697 5119c79e René Nussbaumer
    else:
1698 5119c79e René Nussbaumer
      if pause:
1699 5119c79e René Nussbaumer
        msg = "Pause"
1700 5119c79e René Nussbaumer
      else:
1701 5119c79e René Nussbaumer
        msg = "Resume"
1702 5119c79e René Nussbaumer
      success.append((result, "%s for device %s failed" % (msg, disk.iv_name)))
1703 5119c79e René Nussbaumer
1704 5119c79e René Nussbaumer
  return success
1705 5119c79e René Nussbaumer
1706 5119c79e René Nussbaumer
1707 821d1bd1 Iustin Pop
def BlockdevRemove(disk):
1708 a8083063 Iustin Pop
  """Remove a block device.
1709 a8083063 Iustin Pop

1710 10c2650b Iustin Pop
  @note: This is intended to be called recursively.
1711 10c2650b Iustin Pop

1712 c41eea6e Iustin Pop
  @type disk: L{objects.Disk}
1713 10c2650b Iustin Pop
  @param disk: the disk object we should remove
1714 10c2650b Iustin Pop
  @rtype: boolean
1715 10c2650b Iustin Pop
  @return: the success of the operation
1716 a8083063 Iustin Pop

1717 a8083063 Iustin Pop
  """
1718 e1bc0878 Iustin Pop
  msgs = []
1719 a8083063 Iustin Pop
  try:
1720 bca2e7f4 Iustin Pop
    rdev = _RecursiveFindBD(disk)
1721 a8083063 Iustin Pop
  except errors.BlockDeviceError, err:
1722 a8083063 Iustin Pop
    # probably can't attach
1723 18682bca Iustin Pop
    logging.info("Can't attach to device %s in remove", disk)
1724 a8083063 Iustin Pop
    rdev = None
1725 a8083063 Iustin Pop
  if rdev is not None:
1726 3f78eef2 Iustin Pop
    r_path = rdev.dev_path
1727 e1bc0878 Iustin Pop
    try:
1728 0c6c04ec Iustin Pop
      rdev.Remove()
1729 e1bc0878 Iustin Pop
    except errors.BlockDeviceError, err:
1730 e1bc0878 Iustin Pop
      msgs.append(str(err))
1731 c26a6bd2 Iustin Pop
    if not msgs:
1732 3f78eef2 Iustin Pop
      DevCacheManager.RemoveCache(r_path)
1733 e1bc0878 Iustin Pop
1734 a8083063 Iustin Pop
  if disk.children:
1735 a8083063 Iustin Pop
    for child in disk.children:
1736 c26a6bd2 Iustin Pop
      try:
1737 c26a6bd2 Iustin Pop
        BlockdevRemove(child)
1738 c26a6bd2 Iustin Pop
      except RPCFail, err:
1739 c26a6bd2 Iustin Pop
        msgs.append(str(err))
1740 e1bc0878 Iustin Pop
1741 c26a6bd2 Iustin Pop
  if msgs:
1742 afdc3985 Iustin Pop
    _Fail("; ".join(msgs))
1743 afdc3985 Iustin Pop
1744 a8083063 Iustin Pop
1745 3f78eef2 Iustin Pop
def _RecursiveAssembleBD(disk, owner, as_primary):
1746 a8083063 Iustin Pop
  """Activate a block device for an instance.
1747 a8083063 Iustin Pop

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

1750 10c2650b Iustin Pop
  @note: this function is called recursively.
1751 a8083063 Iustin Pop

1752 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
1753 10c2650b Iustin Pop
  @param disk: the disk we try to assemble
1754 10c2650b Iustin Pop
  @type owner: str
1755 10c2650b Iustin Pop
  @param owner: the name of the instance which owns the disk
1756 10c2650b Iustin Pop
  @type as_primary: boolean
1757 10c2650b Iustin Pop
  @param as_primary: if we should make the block device
1758 10c2650b Iustin Pop
      read/write
1759 a8083063 Iustin Pop

1760 10c2650b Iustin Pop
  @return: the assembled device or None (in case no device
1761 10c2650b Iustin Pop
      was assembled)
1762 10c2650b Iustin Pop
  @raise errors.BlockDeviceError: in case there is an error
1763 10c2650b Iustin Pop
      during the activation of the children or the device
1764 10c2650b Iustin Pop
      itself
1765 a8083063 Iustin Pop

1766 a8083063 Iustin Pop
  """
1767 a8083063 Iustin Pop
  children = []
1768 a8083063 Iustin Pop
  if disk.children:
1769 fc1dc9d7 Iustin Pop
    mcn = disk.ChildrenNeeded()
1770 fc1dc9d7 Iustin Pop
    if mcn == -1:
1771 fc1dc9d7 Iustin Pop
      mcn = 0 # max number of Nones allowed
1772 fc1dc9d7 Iustin Pop
    else:
1773 fc1dc9d7 Iustin Pop
      mcn = len(disk.children) - mcn # max number of Nones
1774 a8083063 Iustin Pop
    for chld_disk in disk.children:
1775 fc1dc9d7 Iustin Pop
      try:
1776 fc1dc9d7 Iustin Pop
        cdev = _RecursiveAssembleBD(chld_disk, owner, as_primary)
1777 fc1dc9d7 Iustin Pop
      except errors.BlockDeviceError, err:
1778 7803d4d3 Iustin Pop
        if children.count(None) >= mcn:
1779 fc1dc9d7 Iustin Pop
          raise
1780 fc1dc9d7 Iustin Pop
        cdev = None
1781 1063abd1 Iustin Pop
        logging.error("Error in child activation (but continuing): %s",
1782 1063abd1 Iustin Pop
                      str(err))
1783 fc1dc9d7 Iustin Pop
      children.append(cdev)
1784 a8083063 Iustin Pop
1785 a8083063 Iustin Pop
  if as_primary or disk.AssembleOnSecondary():
1786 94dcbdb0 Andrea Spadaccini
    r_dev = bdev.Assemble(disk, children)
1787 a8083063 Iustin Pop
    result = r_dev
1788 a8083063 Iustin Pop
    if as_primary or disk.OpenOnSecondary():
1789 a8083063 Iustin Pop
      r_dev.Open()
1790 3f78eef2 Iustin Pop
    DevCacheManager.UpdateCache(r_dev.dev_path, owner,
1791 3f78eef2 Iustin Pop
                                as_primary, disk.iv_name)
1792 3f78eef2 Iustin Pop
1793 a8083063 Iustin Pop
  else:
1794 a8083063 Iustin Pop
    result = True
1795 a8083063 Iustin Pop
  return result
1796 a8083063 Iustin Pop
1797 a8083063 Iustin Pop
1798 c417e115 Iustin Pop
def BlockdevAssemble(disk, owner, as_primary, idx):
1799 a8083063 Iustin Pop
  """Activate a block device for an instance.
1800 a8083063 Iustin Pop

1801 a8083063 Iustin Pop
  This is a wrapper over _RecursiveAssembleBD.
1802 a8083063 Iustin Pop

1803 b1206984 Iustin Pop
  @rtype: str or boolean
1804 b1206984 Iustin Pop
  @return: a C{/dev/...} path for primary nodes, and
1805 b1206984 Iustin Pop
      C{True} for secondary nodes
1806 a8083063 Iustin Pop

1807 a8083063 Iustin Pop
  """
1808 53c14ef1 Iustin Pop
  try:
1809 53c14ef1 Iustin Pop
    result = _RecursiveAssembleBD(disk, owner, as_primary)
1810 53c14ef1 Iustin Pop
    if isinstance(result, bdev.BlockDev):
1811 b459a848 Andrea Spadaccini
      # pylint: disable=E1103
1812 53c14ef1 Iustin Pop
      result = result.dev_path
1813 c417e115 Iustin Pop
      if as_primary:
1814 c417e115 Iustin Pop
        _SymlinkBlockDev(owner, result, idx)
1815 53c14ef1 Iustin Pop
  except errors.BlockDeviceError, err:
1816 afdc3985 Iustin Pop
    _Fail("Error while assembling disk: %s", err, exc=True)
1817 c417e115 Iustin Pop
  except OSError, err:
1818 c417e115 Iustin Pop
    _Fail("Error while symlinking disk: %s", err, exc=True)
1819 afdc3985 Iustin Pop
1820 c26a6bd2 Iustin Pop
  return result
1821 a8083063 Iustin Pop
1822 a8083063 Iustin Pop
1823 821d1bd1 Iustin Pop
def BlockdevShutdown(disk):
1824 a8083063 Iustin Pop
  """Shut down a block device.
1825 a8083063 Iustin Pop

1826 5bbd3f7f Michael Hanselmann
  First, if the device is assembled (Attach() is successful), then
1827 c41eea6e Iustin Pop
  the device is shutdown. Then the children of the device are
1828 c41eea6e Iustin Pop
  shutdown.
1829 a8083063 Iustin Pop

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

1834 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
1835 10c2650b Iustin Pop
  @param disk: the description of the disk we should
1836 10c2650b Iustin Pop
      shutdown
1837 c26a6bd2 Iustin Pop
  @rtype: None
1838 10c2650b Iustin Pop

1839 a8083063 Iustin Pop
  """
1840 cacfd1fd Iustin Pop
  msgs = []
1841 a8083063 Iustin Pop
  r_dev = _RecursiveFindBD(disk)
1842 a8083063 Iustin Pop
  if r_dev is not None:
1843 3f78eef2 Iustin Pop
    r_path = r_dev.dev_path
1844 cacfd1fd Iustin Pop
    try:
1845 746f7476 Iustin Pop
      r_dev.Shutdown()
1846 746f7476 Iustin Pop
      DevCacheManager.RemoveCache(r_path)
1847 cacfd1fd Iustin Pop
    except errors.BlockDeviceError, err:
1848 cacfd1fd Iustin Pop
      msgs.append(str(err))
1849 746f7476 Iustin Pop
1850 a8083063 Iustin Pop
  if disk.children:
1851 a8083063 Iustin Pop
    for child in disk.children:
1852 c26a6bd2 Iustin Pop
      try:
1853 c26a6bd2 Iustin Pop
        BlockdevShutdown(child)
1854 c26a6bd2 Iustin Pop
      except RPCFail, err:
1855 c26a6bd2 Iustin Pop
        msgs.append(str(err))
1856 746f7476 Iustin Pop
1857 c26a6bd2 Iustin Pop
  if msgs:
1858 afdc3985 Iustin Pop
    _Fail("; ".join(msgs))
1859 a8083063 Iustin Pop
1860 a8083063 Iustin Pop
1861 821d1bd1 Iustin Pop
def BlockdevAddchildren(parent_cdev, new_cdevs):
1862 153d9724 Iustin Pop
  """Extend a mirrored block device.
1863 a8083063 Iustin Pop

1864 10c2650b Iustin Pop
  @type parent_cdev: L{objects.Disk}
1865 10c2650b Iustin Pop
  @param parent_cdev: the disk to which we should add children
1866 10c2650b Iustin Pop
  @type new_cdevs: list of L{objects.Disk}
1867 10c2650b Iustin Pop
  @param new_cdevs: the list of children which we should add
1868 c26a6bd2 Iustin Pop
  @rtype: None
1869 10c2650b Iustin Pop

1870 a8083063 Iustin Pop
  """
1871 bca2e7f4 Iustin Pop
  parent_bdev = _RecursiveFindBD(parent_cdev)
1872 153d9724 Iustin Pop
  if parent_bdev is None:
1873 2cc6781a Iustin Pop
    _Fail("Can't find parent device '%s' in add children", parent_cdev)
1874 153d9724 Iustin Pop
  new_bdevs = [_RecursiveFindBD(disk) for disk in new_cdevs]
1875 153d9724 Iustin Pop
  if new_bdevs.count(None) > 0:
1876 2cc6781a Iustin Pop
    _Fail("Can't find new device(s) to add: %s:%s", new_bdevs, new_cdevs)
1877 153d9724 Iustin Pop
  parent_bdev.AddChildren(new_bdevs)
1878 a8083063 Iustin Pop
1879 a8083063 Iustin Pop
1880 821d1bd1 Iustin Pop
def BlockdevRemovechildren(parent_cdev, new_cdevs):
1881 153d9724 Iustin Pop
  """Shrink a mirrored block device.
1882 a8083063 Iustin Pop

1883 10c2650b Iustin Pop
  @type parent_cdev: L{objects.Disk}
1884 10c2650b Iustin Pop
  @param parent_cdev: the disk from which we should remove children
1885 10c2650b Iustin Pop
  @type new_cdevs: list of L{objects.Disk}
1886 10c2650b Iustin Pop
  @param new_cdevs: the list of children which we should remove
1887 c26a6bd2 Iustin Pop
  @rtype: None
1888 10c2650b Iustin Pop

1889 a8083063 Iustin Pop
  """
1890 153d9724 Iustin Pop
  parent_bdev = _RecursiveFindBD(parent_cdev)
1891 153d9724 Iustin Pop
  if parent_bdev is None:
1892 2cc6781a Iustin Pop
    _Fail("Can't find parent device '%s' in remove children", parent_cdev)
1893 e739bd57 Iustin Pop
  devs = []
1894 e739bd57 Iustin Pop
  for disk in new_cdevs:
1895 e739bd57 Iustin Pop
    rpath = disk.StaticDevPath()
1896 e739bd57 Iustin Pop
    if rpath is None:
1897 e739bd57 Iustin Pop
      bd = _RecursiveFindBD(disk)
1898 e739bd57 Iustin Pop
      if bd is None:
1899 2cc6781a Iustin Pop
        _Fail("Can't find device %s while removing children", disk)
1900 e739bd57 Iustin Pop
      else:
1901 e739bd57 Iustin Pop
        devs.append(bd.dev_path)
1902 e739bd57 Iustin Pop
    else:
1903 e51db2a6 Iustin Pop
      if not utils.IsNormAbsPath(rpath):
1904 e51db2a6 Iustin Pop
        _Fail("Strange path returned from StaticDevPath: '%s'", rpath)
1905 e739bd57 Iustin Pop
      devs.append(rpath)
1906 e739bd57 Iustin Pop
  parent_bdev.RemoveChildren(devs)
1907 a8083063 Iustin Pop
1908 a8083063 Iustin Pop
1909 821d1bd1 Iustin Pop
def BlockdevGetmirrorstatus(disks):
1910 a8083063 Iustin Pop
  """Get the mirroring status of a list of devices.
1911 a8083063 Iustin Pop

1912 10c2650b Iustin Pop
  @type disks: list of L{objects.Disk}
1913 10c2650b Iustin Pop
  @param disks: the list of disks which we should query
1914 10c2650b Iustin Pop
  @rtype: disk
1915 c6a9dffa Michael Hanselmann
  @return: List of L{objects.BlockDevStatus}, one for each disk
1916 10c2650b Iustin Pop
  @raise errors.BlockDeviceError: if any of the disks cannot be
1917 10c2650b Iustin Pop
      found
1918 a8083063 Iustin Pop

1919 a8083063 Iustin Pop
  """
1920 a8083063 Iustin Pop
  stats = []
1921 a8083063 Iustin Pop
  for dsk in disks:
1922 a8083063 Iustin Pop
    rbd = _RecursiveFindBD(dsk)
1923 a8083063 Iustin Pop
    if rbd is None:
1924 3efa9051 Iustin Pop
      _Fail("Can't find device %s", dsk)
1925 96acbc09 Michael Hanselmann
1926 36145b12 Michael Hanselmann
    stats.append(rbd.CombinedSyncStatus())
1927 96acbc09 Michael Hanselmann
1928 c26a6bd2 Iustin Pop
  return stats
1929 a8083063 Iustin Pop
1930 a8083063 Iustin Pop
1931 c6a9dffa Michael Hanselmann
def BlockdevGetmirrorstatusMulti(disks):
1932 c6a9dffa Michael Hanselmann
  """Get the mirroring status of a list of devices.
1933 c6a9dffa Michael Hanselmann

1934 c6a9dffa Michael Hanselmann
  @type disks: list of L{objects.Disk}
1935 c6a9dffa Michael Hanselmann
  @param disks: the list of disks which we should query
1936 c6a9dffa Michael Hanselmann
  @rtype: disk
1937 c6a9dffa Michael Hanselmann
  @return: List of tuples, (bool, status), one for each disk; bool denotes
1938 c6a9dffa Michael Hanselmann
    success/failure, status is L{objects.BlockDevStatus} on success, string
1939 c6a9dffa Michael Hanselmann
    otherwise
1940 c6a9dffa Michael Hanselmann

1941 c6a9dffa Michael Hanselmann
  """
1942 c6a9dffa Michael Hanselmann
  result = []
1943 c6a9dffa Michael Hanselmann
  for disk in disks:
1944 c6a9dffa Michael Hanselmann
    try:
1945 c6a9dffa Michael Hanselmann
      rbd = _RecursiveFindBD(disk)
1946 c6a9dffa Michael Hanselmann
      if rbd is None:
1947 c6a9dffa Michael Hanselmann
        result.append((False, "Can't find device %s" % disk))
1948 c6a9dffa Michael Hanselmann
        continue
1949 c6a9dffa Michael Hanselmann
1950 c6a9dffa Michael Hanselmann
      status = rbd.CombinedSyncStatus()
1951 c6a9dffa Michael Hanselmann
    except errors.BlockDeviceError, err:
1952 c6a9dffa Michael Hanselmann
      logging.exception("Error while getting disk status")
1953 c6a9dffa Michael Hanselmann
      result.append((False, str(err)))
1954 c6a9dffa Michael Hanselmann
    else:
1955 c6a9dffa Michael Hanselmann
      result.append((True, status))
1956 c6a9dffa Michael Hanselmann
1957 c6a9dffa Michael Hanselmann
  assert len(disks) == len(result)
1958 c6a9dffa Michael Hanselmann
1959 c6a9dffa Michael Hanselmann
  return result
1960 c6a9dffa Michael Hanselmann
1961 c6a9dffa Michael Hanselmann
1962 bca2e7f4 Iustin Pop
def _RecursiveFindBD(disk):
1963 a8083063 Iustin Pop
  """Check if a device is activated.
1964 a8083063 Iustin Pop

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

1967 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
1968 10c2650b Iustin Pop
  @param disk: the disk object we need to find
1969 a8083063 Iustin Pop

1970 10c2650b Iustin Pop
  @return: None if the device can't be found,
1971 10c2650b Iustin Pop
      otherwise the device instance
1972 a8083063 Iustin Pop

1973 a8083063 Iustin Pop
  """
1974 a8083063 Iustin Pop
  children = []
1975 a8083063 Iustin Pop
  if disk.children:
1976 a8083063 Iustin Pop
    for chdisk in disk.children:
1977 a8083063 Iustin Pop
      children.append(_RecursiveFindBD(chdisk))
1978 a8083063 Iustin Pop
1979 94dcbdb0 Andrea Spadaccini
  return bdev.FindDevice(disk, children)
1980 a8083063 Iustin Pop
1981 a8083063 Iustin Pop
1982 f2e07bb4 Michael Hanselmann
def _OpenRealBD(disk):
1983 f2e07bb4 Michael Hanselmann
  """Opens the underlying block device of a disk.
1984 f2e07bb4 Michael Hanselmann

1985 f2e07bb4 Michael Hanselmann
  @type disk: L{objects.Disk}
1986 f2e07bb4 Michael Hanselmann
  @param disk: the disk object we want to open
1987 f2e07bb4 Michael Hanselmann

1988 f2e07bb4 Michael Hanselmann
  """
1989 f2e07bb4 Michael Hanselmann
  real_disk = _RecursiveFindBD(disk)
1990 f2e07bb4 Michael Hanselmann
  if real_disk is None:
1991 f2e07bb4 Michael Hanselmann
    _Fail("Block device '%s' is not set up", disk)
1992 f2e07bb4 Michael Hanselmann
1993 f2e07bb4 Michael Hanselmann
  real_disk.Open()
1994 f2e07bb4 Michael Hanselmann
1995 f2e07bb4 Michael Hanselmann
  return real_disk
1996 f2e07bb4 Michael Hanselmann
1997 f2e07bb4 Michael Hanselmann
1998 821d1bd1 Iustin Pop
def BlockdevFind(disk):
1999 a8083063 Iustin Pop
  """Check if a device is activated.
2000 a8083063 Iustin Pop

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

2003 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
2004 10c2650b Iustin Pop
  @param disk: the disk to find
2005 96acbc09 Michael Hanselmann
  @rtype: None or objects.BlockDevStatus
2006 96acbc09 Michael Hanselmann
  @return: None if the disk cannot be found, otherwise a the current
2007 96acbc09 Michael Hanselmann
           information
2008 a8083063 Iustin Pop

2009 a8083063 Iustin Pop
  """
2010 23829f6f Iustin Pop
  try:
2011 23829f6f Iustin Pop
    rbd = _RecursiveFindBD(disk)
2012 23829f6f Iustin Pop
  except errors.BlockDeviceError, err:
2013 2cc6781a Iustin Pop
    _Fail("Failed to find device: %s", err, exc=True)
2014 96acbc09 Michael Hanselmann
2015 a8083063 Iustin Pop
  if rbd is None:
2016 c26a6bd2 Iustin Pop
    return None
2017 96acbc09 Michael Hanselmann
2018 96acbc09 Michael Hanselmann
  return rbd.GetSyncStatus()
2019 a8083063 Iustin Pop
2020 a8083063 Iustin Pop
2021 968a7623 Iustin Pop
def BlockdevGetsize(disks):
2022 968a7623 Iustin Pop
  """Computes the size of the given disks.
2023 968a7623 Iustin Pop

2024 968a7623 Iustin Pop
  If a disk is not found, returns None instead.
2025 968a7623 Iustin Pop

2026 968a7623 Iustin Pop
  @type disks: list of L{objects.Disk}
2027 968a7623 Iustin Pop
  @param disks: the list of disk to compute the size for
2028 968a7623 Iustin Pop
  @rtype: list
2029 968a7623 Iustin Pop
  @return: list with elements None if the disk cannot be found,
2030 968a7623 Iustin Pop
      otherwise the size
2031 968a7623 Iustin Pop

2032 968a7623 Iustin Pop
  """
2033 968a7623 Iustin Pop
  result = []
2034 968a7623 Iustin Pop
  for cf in disks:
2035 968a7623 Iustin Pop
    try:
2036 968a7623 Iustin Pop
      rbd = _RecursiveFindBD(cf)
2037 1122eb25 Iustin Pop
    except errors.BlockDeviceError:
2038 968a7623 Iustin Pop
      result.append(None)
2039 968a7623 Iustin Pop
      continue
2040 968a7623 Iustin Pop
    if rbd is None:
2041 968a7623 Iustin Pop
      result.append(None)
2042 968a7623 Iustin Pop
    else:
2043 968a7623 Iustin Pop
      result.append(rbd.GetActualSize())
2044 968a7623 Iustin Pop
  return result
2045 968a7623 Iustin Pop
2046 968a7623 Iustin Pop
2047 858f3d18 Iustin Pop
def BlockdevExport(disk, dest_node, dest_path, cluster_name):
2048 858f3d18 Iustin Pop
  """Export a block device to a remote node.
2049 858f3d18 Iustin Pop

2050 858f3d18 Iustin Pop
  @type disk: L{objects.Disk}
2051 858f3d18 Iustin Pop
  @param disk: the description of the disk to export
2052 858f3d18 Iustin Pop
  @type dest_node: str
2053 858f3d18 Iustin Pop
  @param dest_node: the destination node to export to
2054 858f3d18 Iustin Pop
  @type dest_path: str
2055 858f3d18 Iustin Pop
  @param dest_path: the destination path on the target node
2056 858f3d18 Iustin Pop
  @type cluster_name: str
2057 858f3d18 Iustin Pop
  @param cluster_name: the cluster name, needed for SSH hostalias
2058 858f3d18 Iustin Pop
  @rtype: None
2059 858f3d18 Iustin Pop

2060 858f3d18 Iustin Pop
  """
2061 f2e07bb4 Michael Hanselmann
  real_disk = _OpenRealBD(disk)
2062 858f3d18 Iustin Pop
2063 858f3d18 Iustin Pop
  # the block size on the read dd is 1MiB to match our units
2064 858f3d18 Iustin Pop
  expcmd = utils.BuildShellCmd("set -e; set -o pipefail; "
2065 858f3d18 Iustin Pop
                               "dd if=%s bs=1048576 count=%s",
2066 858f3d18 Iustin Pop
                               real_disk.dev_path, str(disk.size))
2067 858f3d18 Iustin Pop
2068 858f3d18 Iustin Pop
  # we set here a smaller block size as, due to ssh buffering, more
2069 858f3d18 Iustin Pop
  # than 64-128k will mostly ignored; we use nocreat to fail if the
2070 858f3d18 Iustin Pop
  # device is not already there or we pass a wrong path; we use
2071 858f3d18 Iustin Pop
  # notrunc to no attempt truncate on an LV device; we use oflag=dsync
2072 858f3d18 Iustin Pop
  # to not buffer too much memory; this means that at best, we flush
2073 858f3d18 Iustin Pop
  # every 64k, which will not be very fast
2074 858f3d18 Iustin Pop
  destcmd = utils.BuildShellCmd("dd of=%s conv=nocreat,notrunc bs=65536"
2075 858f3d18 Iustin Pop
                                " oflag=dsync", dest_path)
2076 858f3d18 Iustin Pop
2077 858f3d18 Iustin Pop
  remotecmd = _GetSshRunner(cluster_name).BuildCmd(dest_node,
2078 052783ff Michael Hanselmann
                                                   constants.SSH_LOGIN_USER,
2079 858f3d18 Iustin Pop
                                                   destcmd)
2080 858f3d18 Iustin Pop
2081 858f3d18 Iustin Pop
  # all commands have been checked, so we're safe to combine them
2082 d0c8c01d Iustin Pop
  command = "|".join([expcmd, utils.ShellQuoteArgs(remotecmd)])
2083 858f3d18 Iustin Pop
2084 858f3d18 Iustin Pop
  result = utils.RunCmd(["bash", "-c", command])
2085 858f3d18 Iustin Pop
2086 858f3d18 Iustin Pop
  if result.failed:
2087 858f3d18 Iustin Pop
    _Fail("Disk copy command '%s' returned error: %s"
2088 858f3d18 Iustin Pop
          " output: %s", com