root / lib / cmdlib / instance_storage.py @ 4e7f986e
History | View | Annotate | Download (99.3 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Logical units dealing with storage of instances."""
|
23 |
|
24 |
import itertools |
25 |
import logging |
26 |
import os |
27 |
import time |
28 |
|
29 |
from ganeti import compat |
30 |
from ganeti import constants |
31 |
from ganeti import errors |
32 |
from ganeti import ht |
33 |
from ganeti import locking |
34 |
from ganeti.masterd import iallocator |
35 |
from ganeti import objects |
36 |
from ganeti import utils |
37 |
import ganeti.rpc.node as rpc |
38 |
from ganeti.cmdlib.base import LogicalUnit, NoHooksLU, Tasklet |
39 |
from ganeti.cmdlib.common import INSTANCE_DOWN, INSTANCE_NOT_RUNNING, \ |
40 |
AnnotateDiskParams, CheckIAllocatorOrNode, ExpandNodeUuidAndName, \ |
41 |
CheckNodeOnline, CheckInstanceNodeGroups, CheckInstanceState, \ |
42 |
IsExclusiveStorageEnabledNode, FindFaultyInstanceDisks, GetWantedNodes, \ |
43 |
CheckDiskTemplateEnabled
|
44 |
from ganeti.cmdlib.instance_utils import GetInstanceInfoText, \ |
45 |
CopyLockList, ReleaseLocks, CheckNodeVmCapable, \ |
46 |
BuildInstanceHookEnvByObject, CheckNodeNotDrained, CheckTargetNodeIPolicy |
47 |
|
48 |
import ganeti.masterd.instance |
49 |
|
50 |
|
51 |
_DISK_TEMPLATE_NAME_PREFIX = { |
52 |
constants.DT_PLAIN: "",
|
53 |
constants.DT_RBD: ".rbd",
|
54 |
constants.DT_EXT: ".ext",
|
55 |
constants.DT_FILE: ".file",
|
56 |
constants.DT_SHARED_FILE: ".sharedfile",
|
57 |
} |
58 |
|
59 |
|
60 |
def CreateSingleBlockDev(lu, node_uuid, instance, device, info, force_open, |
61 |
excl_stor): |
62 |
"""Create a single block device on a given node.
|
63 |
|
64 |
This will not recurse over children of the device, so they must be
|
65 |
created in advance.
|
66 |
|
67 |
@param lu: the lu on whose behalf we execute
|
68 |
@param node_uuid: the node on which to create the device
|
69 |
@type instance: L{objects.Instance}
|
70 |
@param instance: the instance which owns the device
|
71 |
@type device: L{objects.Disk}
|
72 |
@param device: the device to create
|
73 |
@param info: the extra 'metadata' we should attach to the device
|
74 |
(this will be represented as a LVM tag)
|
75 |
@type force_open: boolean
|
76 |
@param force_open: this parameter will be passes to the
|
77 |
L{backend.BlockdevCreate} function where it specifies
|
78 |
whether we run on primary or not, and it affects both
|
79 |
the child assembly and the device own Open() execution
|
80 |
@type excl_stor: boolean
|
81 |
@param excl_stor: Whether exclusive_storage is active for the node
|
82 |
|
83 |
"""
|
84 |
result = lu.rpc.call_blockdev_create(node_uuid, (device, instance), |
85 |
device.size, instance.name, force_open, |
86 |
info, excl_stor) |
87 |
result.Raise("Can't create block device %s on"
|
88 |
" node %s for instance %s" % (device,
|
89 |
lu.cfg.GetNodeName(node_uuid), |
90 |
instance.name)) |
91 |
|
92 |
|
93 |
def _CreateBlockDevInner(lu, node_uuid, instance, device, force_create, |
94 |
info, force_open, excl_stor): |
95 |
"""Create a tree of block devices on a given node.
|
96 |
|
97 |
If this device type has to be created on secondaries, create it and
|
98 |
all its children.
|
99 |
|
100 |
If not, just recurse to children keeping the same 'force' value.
|
101 |
|
102 |
@attention: The device has to be annotated already.
|
103 |
|
104 |
@param lu: the lu on whose behalf we execute
|
105 |
@param node_uuid: the node on which to create the device
|
106 |
@type instance: L{objects.Instance}
|
107 |
@param instance: the instance which owns the device
|
108 |
@type device: L{objects.Disk}
|
109 |
@param device: the device to create
|
110 |
@type force_create: boolean
|
111 |
@param force_create: whether to force creation of this device; this
|
112 |
will be change to True whenever we find a device which has
|
113 |
CreateOnSecondary() attribute
|
114 |
@param info: the extra 'metadata' we should attach to the device
|
115 |
(this will be represented as a LVM tag)
|
116 |
@type force_open: boolean
|
117 |
@param force_open: this parameter will be passes to the
|
118 |
L{backend.BlockdevCreate} function where it specifies
|
119 |
whether we run on primary or not, and it affects both
|
120 |
the child assembly and the device own Open() execution
|
121 |
@type excl_stor: boolean
|
122 |
@param excl_stor: Whether exclusive_storage is active for the node
|
123 |
|
124 |
@return: list of created devices
|
125 |
"""
|
126 |
created_devices = [] |
127 |
try:
|
128 |
if device.CreateOnSecondary():
|
129 |
force_create = True
|
130 |
|
131 |
if device.children:
|
132 |
for child in device.children: |
133 |
devs = _CreateBlockDevInner(lu, node_uuid, instance, child, |
134 |
force_create, info, force_open, excl_stor) |
135 |
created_devices.extend(devs) |
136 |
|
137 |
if not force_create: |
138 |
return created_devices
|
139 |
|
140 |
CreateSingleBlockDev(lu, node_uuid, instance, device, info, force_open, |
141 |
excl_stor) |
142 |
# The device has been completely created, so there is no point in keeping
|
143 |
# its subdevices in the list. We just add the device itself instead.
|
144 |
created_devices = [(node_uuid, device)] |
145 |
return created_devices
|
146 |
|
147 |
except errors.DeviceCreationError, e:
|
148 |
e.created_devices.extend(created_devices) |
149 |
raise e
|
150 |
except errors.OpExecError, e:
|
151 |
raise errors.DeviceCreationError(str(e), created_devices) |
152 |
|
153 |
|
154 |
def IsExclusiveStorageEnabledNodeUuid(cfg, node_uuid): |
155 |
"""Whether exclusive_storage is in effect for the given node.
|
156 |
|
157 |
@type cfg: L{config.ConfigWriter}
|
158 |
@param cfg: The cluster configuration
|
159 |
@type node_uuid: string
|
160 |
@param node_uuid: The node UUID
|
161 |
@rtype: bool
|
162 |
@return: The effective value of exclusive_storage
|
163 |
@raise errors.OpPrereqError: if no node exists with the given name
|
164 |
|
165 |
"""
|
166 |
ni = cfg.GetNodeInfo(node_uuid) |
167 |
if ni is None: |
168 |
raise errors.OpPrereqError("Invalid node UUID %s" % node_uuid, |
169 |
errors.ECODE_NOENT) |
170 |
return IsExclusiveStorageEnabledNode(cfg, ni)
|
171 |
|
172 |
|
173 |
def _CreateBlockDev(lu, node_uuid, instance, device, force_create, info, |
174 |
force_open): |
175 |
"""Wrapper around L{_CreateBlockDevInner}.
|
176 |
|
177 |
This method annotates the root device first.
|
178 |
|
179 |
"""
|
180 |
(disk,) = AnnotateDiskParams(instance, [device], lu.cfg) |
181 |
excl_stor = IsExclusiveStorageEnabledNodeUuid(lu.cfg, node_uuid) |
182 |
return _CreateBlockDevInner(lu, node_uuid, instance, disk, force_create, info,
|
183 |
force_open, excl_stor) |
184 |
|
185 |
|
186 |
def _UndoCreateDisks(lu, disks_created, instance): |
187 |
"""Undo the work performed by L{CreateDisks}.
|
188 |
|
189 |
This function is called in case of an error to undo the work of
|
190 |
L{CreateDisks}.
|
191 |
|
192 |
@type lu: L{LogicalUnit}
|
193 |
@param lu: the logical unit on whose behalf we execute
|
194 |
@param disks_created: the result returned by L{CreateDisks}
|
195 |
@type instance: L{objects.Instance}
|
196 |
@param instance: the instance for which disks were created
|
197 |
|
198 |
"""
|
199 |
for (node_uuid, disk) in disks_created: |
200 |
result = lu.rpc.call_blockdev_remove(node_uuid, (disk, instance)) |
201 |
result.Warn("Failed to remove newly-created disk %s on node %s" %
|
202 |
(disk, lu.cfg.GetNodeName(node_uuid)), logging.warning) |
203 |
|
204 |
|
205 |
def CreateDisks(lu, instance, to_skip=None, target_node_uuid=None, disks=None): |
206 |
"""Create all disks for an instance.
|
207 |
|
208 |
This abstracts away some work from AddInstance.
|
209 |
|
210 |
@type lu: L{LogicalUnit}
|
211 |
@param lu: the logical unit on whose behalf we execute
|
212 |
@type instance: L{objects.Instance}
|
213 |
@param instance: the instance whose disks we should create
|
214 |
@type to_skip: list
|
215 |
@param to_skip: list of indices to skip
|
216 |
@type target_node_uuid: string
|
217 |
@param target_node_uuid: if passed, overrides the target node for creation
|
218 |
@type disks: list of {objects.Disk}
|
219 |
@param disks: the disks to create; if not specified, all the disks of the
|
220 |
instance are created
|
221 |
@return: information about the created disks, to be used to call
|
222 |
L{_UndoCreateDisks}
|
223 |
@raise errors.OpPrereqError: in case of error
|
224 |
|
225 |
"""
|
226 |
info = GetInstanceInfoText(instance) |
227 |
if target_node_uuid is None: |
228 |
pnode_uuid = instance.primary_node |
229 |
all_node_uuids = lu.cfg.GetInstanceNodes(instance, disks=disks) |
230 |
else:
|
231 |
pnode_uuid = target_node_uuid |
232 |
all_node_uuids = [pnode_uuid] |
233 |
|
234 |
if disks is None: |
235 |
disks = instance.disks |
236 |
|
237 |
CheckDiskTemplateEnabled(lu.cfg.GetClusterInfo(), instance.disk_template) |
238 |
|
239 |
if instance.disk_template in constants.DTS_FILEBASED: |
240 |
file_storage_dir = os.path.dirname(instance.disks[0].logical_id[1]) |
241 |
result = lu.rpc.call_file_storage_dir_create(pnode_uuid, file_storage_dir) |
242 |
|
243 |
result.Raise("Failed to create directory '%s' on"
|
244 |
" node %s" % (file_storage_dir,
|
245 |
lu.cfg.GetNodeName(pnode_uuid))) |
246 |
|
247 |
disks_created = [] |
248 |
for idx, device in enumerate(disks): |
249 |
if to_skip and idx in to_skip: |
250 |
continue
|
251 |
logging.info("Creating disk %s for instance '%s'", idx, instance.name)
|
252 |
for node_uuid in all_node_uuids: |
253 |
f_create = node_uuid == pnode_uuid |
254 |
try:
|
255 |
_CreateBlockDev(lu, node_uuid, instance, device, f_create, info, |
256 |
f_create) |
257 |
disks_created.append((node_uuid, device)) |
258 |
except errors.DeviceCreationError, e:
|
259 |
logging.warning("Creating disk %s for instance '%s' failed",
|
260 |
idx, instance.name) |
261 |
disks_created.extend(e.created_devices) |
262 |
_UndoCreateDisks(lu, disks_created, instance) |
263 |
raise errors.OpExecError(e.message)
|
264 |
return disks_created
|
265 |
|
266 |
|
267 |
def ComputeDiskSizePerVG(disk_template, disks): |
268 |
"""Compute disk size requirements in the volume group
|
269 |
|
270 |
"""
|
271 |
def _compute(disks, payload): |
272 |
"""Universal algorithm.
|
273 |
|
274 |
"""
|
275 |
vgs = {} |
276 |
for disk in disks: |
277 |
vgs[disk[constants.IDISK_VG]] = \ |
278 |
vgs.get(constants.IDISK_VG, 0) + disk[constants.IDISK_SIZE] + payload
|
279 |
|
280 |
return vgs
|
281 |
|
282 |
# Required free disk space as a function of disk and swap space
|
283 |
req_size_dict = { |
284 |
constants.DT_DISKLESS: {}, |
285 |
constants.DT_PLAIN: _compute(disks, 0),
|
286 |
# 128 MB are added for drbd metadata for each disk
|
287 |
constants.DT_DRBD8: _compute(disks, constants.DRBD_META_SIZE), |
288 |
constants.DT_FILE: {}, |
289 |
constants.DT_SHARED_FILE: {}, |
290 |
constants.DT_GLUSTER: {}, |
291 |
} |
292 |
|
293 |
if disk_template not in req_size_dict: |
294 |
raise errors.ProgrammerError("Disk template '%s' size requirement" |
295 |
" is unknown" % disk_template)
|
296 |
|
297 |
return req_size_dict[disk_template]
|
298 |
|
299 |
|
300 |
def ComputeDisks(op, default_vg): |
301 |
"""Computes the instance disks.
|
302 |
|
303 |
@param op: The instance opcode
|
304 |
@param default_vg: The default_vg to assume
|
305 |
|
306 |
@return: The computed disks
|
307 |
|
308 |
"""
|
309 |
disks = [] |
310 |
for disk in op.disks: |
311 |
mode = disk.get(constants.IDISK_MODE, constants.DISK_RDWR) |
312 |
if mode not in constants.DISK_ACCESS_SET: |
313 |
raise errors.OpPrereqError("Invalid disk access mode '%s'" % |
314 |
mode, errors.ECODE_INVAL) |
315 |
size = disk.get(constants.IDISK_SIZE, None)
|
316 |
if size is None: |
317 |
raise errors.OpPrereqError("Missing disk size", errors.ECODE_INVAL) |
318 |
try:
|
319 |
size = int(size)
|
320 |
except (TypeError, ValueError): |
321 |
raise errors.OpPrereqError("Invalid disk size '%s'" % size, |
322 |
errors.ECODE_INVAL) |
323 |
|
324 |
ext_provider = disk.get(constants.IDISK_PROVIDER, None)
|
325 |
if ext_provider and op.disk_template != constants.DT_EXT: |
326 |
raise errors.OpPrereqError("The '%s' option is only valid for the %s" |
327 |
" disk template, not %s" %
|
328 |
(constants.IDISK_PROVIDER, constants.DT_EXT, |
329 |
op.disk_template), errors.ECODE_INVAL) |
330 |
|
331 |
data_vg = disk.get(constants.IDISK_VG, default_vg) |
332 |
name = disk.get(constants.IDISK_NAME, None)
|
333 |
if name is not None and name.lower() == constants.VALUE_NONE: |
334 |
name = None
|
335 |
new_disk = { |
336 |
constants.IDISK_SIZE: size, |
337 |
constants.IDISK_MODE: mode, |
338 |
constants.IDISK_VG: data_vg, |
339 |
constants.IDISK_NAME: name, |
340 |
} |
341 |
|
342 |
for key in [ |
343 |
constants.IDISK_METAVG, |
344 |
constants.IDISK_ADOPT, |
345 |
constants.IDISK_SPINDLES, |
346 |
]: |
347 |
if key in disk: |
348 |
new_disk[key] = disk[key] |
349 |
|
350 |
# For extstorage, demand the `provider' option and add any
|
351 |
# additional parameters (ext-params) to the dict
|
352 |
if op.disk_template == constants.DT_EXT:
|
353 |
if ext_provider:
|
354 |
new_disk[constants.IDISK_PROVIDER] = ext_provider |
355 |
for key in disk: |
356 |
if key not in constants.IDISK_PARAMS: |
357 |
new_disk[key] = disk[key] |
358 |
else:
|
359 |
raise errors.OpPrereqError("Missing provider for template '%s'" % |
360 |
constants.DT_EXT, errors.ECODE_INVAL) |
361 |
|
362 |
disks.append(new_disk) |
363 |
|
364 |
return disks
|
365 |
|
366 |
|
367 |
def CheckRADOSFreeSpace(): |
368 |
"""Compute disk size requirements inside the RADOS cluster.
|
369 |
|
370 |
"""
|
371 |
# For the RADOS cluster we assume there is always enough space.
|
372 |
pass
|
373 |
|
374 |
|
375 |
def _GenerateDRBD8Branch(lu, primary_uuid, secondary_uuid, size, vgnames, names, |
376 |
iv_name, p_minor, s_minor): |
377 |
"""Generate a drbd8 device complete with its children.
|
378 |
|
379 |
"""
|
380 |
assert len(vgnames) == len(names) == 2 |
381 |
port = lu.cfg.AllocatePort() |
382 |
shared_secret = lu.cfg.GenerateDRBDSecret(lu.proc.GetECId()) |
383 |
|
384 |
dev_data = objects.Disk(dev_type=constants.DT_PLAIN, size=size, |
385 |
logical_id=(vgnames[0], names[0]), |
386 |
params={}) |
387 |
dev_data.uuid = lu.cfg.GenerateUniqueID(lu.proc.GetECId()) |
388 |
dev_meta = objects.Disk(dev_type=constants.DT_PLAIN, |
389 |
size=constants.DRBD_META_SIZE, |
390 |
logical_id=(vgnames[1], names[1]), |
391 |
params={}) |
392 |
dev_meta.uuid = lu.cfg.GenerateUniqueID(lu.proc.GetECId()) |
393 |
drbd_dev = objects.Disk(dev_type=constants.DT_DRBD8, size=size, |
394 |
logical_id=(primary_uuid, secondary_uuid, port, |
395 |
p_minor, s_minor, |
396 |
shared_secret), |
397 |
children=[dev_data, dev_meta], |
398 |
iv_name=iv_name, params={}) |
399 |
drbd_dev.uuid = lu.cfg.GenerateUniqueID(lu.proc.GetECId()) |
400 |
return drbd_dev
|
401 |
|
402 |
|
403 |
def GenerateDiskTemplate( |
404 |
lu, template_name, instance_uuid, primary_node_uuid, secondary_node_uuids, |
405 |
disk_info, file_storage_dir, file_driver, base_index, |
406 |
feedback_fn, full_disk_params): |
407 |
"""Generate the entire disk layout for a given template type.
|
408 |
|
409 |
"""
|
410 |
vgname = lu.cfg.GetVGName() |
411 |
disk_count = len(disk_info)
|
412 |
disks = [] |
413 |
|
414 |
CheckDiskTemplateEnabled(lu.cfg.GetClusterInfo(), template_name) |
415 |
|
416 |
if template_name == constants.DT_DISKLESS:
|
417 |
pass
|
418 |
elif template_name == constants.DT_DRBD8:
|
419 |
if len(secondary_node_uuids) != 1: |
420 |
raise errors.ProgrammerError("Wrong template configuration") |
421 |
remote_node_uuid = secondary_node_uuids[0]
|
422 |
minors = lu.cfg.AllocateDRBDMinor( |
423 |
[primary_node_uuid, remote_node_uuid] * len(disk_info), instance_uuid)
|
424 |
|
425 |
(drbd_params, _, _) = objects.Disk.ComputeLDParams(template_name, |
426 |
full_disk_params) |
427 |
drbd_default_metavg = drbd_params[constants.LDP_DEFAULT_METAVG] |
428 |
|
429 |
names = [] |
430 |
for lv_prefix in _GenerateUniqueNames(lu, [".disk%d" % (base_index + i) |
431 |
for i in range(disk_count)]): |
432 |
names.append(lv_prefix + "_data")
|
433 |
names.append(lv_prefix + "_meta")
|
434 |
for idx, disk in enumerate(disk_info): |
435 |
disk_index = idx + base_index |
436 |
data_vg = disk.get(constants.IDISK_VG, vgname) |
437 |
meta_vg = disk.get(constants.IDISK_METAVG, drbd_default_metavg) |
438 |
disk_dev = _GenerateDRBD8Branch(lu, primary_node_uuid, remote_node_uuid, |
439 |
disk[constants.IDISK_SIZE], |
440 |
[data_vg, meta_vg], |
441 |
names[idx * 2:idx * 2 + 2], |
442 |
"disk/%d" % disk_index,
|
443 |
minors[idx * 2], minors[idx * 2 + 1]) |
444 |
disk_dev.mode = disk[constants.IDISK_MODE] |
445 |
disk_dev.name = disk.get(constants.IDISK_NAME, None)
|
446 |
disks.append(disk_dev) |
447 |
else:
|
448 |
if secondary_node_uuids:
|
449 |
raise errors.ProgrammerError("Wrong template configuration") |
450 |
|
451 |
name_prefix = _DISK_TEMPLATE_NAME_PREFIX.get(template_name, None)
|
452 |
if name_prefix is None: |
453 |
names = None
|
454 |
else:
|
455 |
names = _GenerateUniqueNames(lu, ["%s.disk%s" %
|
456 |
(name_prefix, base_index + i) |
457 |
for i in range(disk_count)]) |
458 |
|
459 |
if template_name == constants.DT_PLAIN:
|
460 |
|
461 |
def logical_id_fn(idx, _, disk): |
462 |
vg = disk.get(constants.IDISK_VG, vgname) |
463 |
return (vg, names[idx])
|
464 |
|
465 |
elif template_name == constants.DT_GLUSTER:
|
466 |
logical_id_fn = lambda _1, disk_index, _2: \
|
467 |
(file_driver, "ganeti/%s.%d" % (instance_uuid,
|
468 |
disk_index)) |
469 |
|
470 |
elif template_name in constants.DTS_FILEBASED: # Gluster handled above |
471 |
logical_id_fn = \ |
472 |
lambda _, disk_index, disk: (file_driver,
|
473 |
"%s/%s" % (file_storage_dir,
|
474 |
names[idx])) |
475 |
elif template_name == constants.DT_BLOCK:
|
476 |
logical_id_fn = \ |
477 |
lambda idx, disk_index, disk: (constants.BLOCKDEV_DRIVER_MANUAL,
|
478 |
disk[constants.IDISK_ADOPT]) |
479 |
elif template_name == constants.DT_RBD:
|
480 |
logical_id_fn = lambda idx, _, disk: ("rbd", names[idx]) |
481 |
elif template_name == constants.DT_EXT:
|
482 |
def logical_id_fn(idx, _, disk): |
483 |
provider = disk.get(constants.IDISK_PROVIDER, None)
|
484 |
if provider is None: |
485 |
raise errors.ProgrammerError("Disk template is %s, but '%s' is" |
486 |
" not found", constants.DT_EXT,
|
487 |
constants.IDISK_PROVIDER) |
488 |
return (provider, names[idx])
|
489 |
else:
|
490 |
raise errors.ProgrammerError("Unknown disk template '%s'" % template_name) |
491 |
|
492 |
dev_type = template_name |
493 |
|
494 |
for idx, disk in enumerate(disk_info): |
495 |
params = {} |
496 |
# Only for the Ext template add disk_info to params
|
497 |
if template_name == constants.DT_EXT:
|
498 |
params[constants.IDISK_PROVIDER] = disk[constants.IDISK_PROVIDER] |
499 |
for key in disk: |
500 |
if key not in constants.IDISK_PARAMS: |
501 |
params[key] = disk[key] |
502 |
disk_index = idx + base_index |
503 |
size = disk[constants.IDISK_SIZE] |
504 |
feedback_fn("* disk %s, size %s" %
|
505 |
(disk_index, utils.FormatUnit(size, "h")))
|
506 |
disk_dev = objects.Disk(dev_type=dev_type, size=size, |
507 |
logical_id=logical_id_fn(idx, disk_index, disk), |
508 |
iv_name="disk/%d" % disk_index,
|
509 |
mode=disk[constants.IDISK_MODE], |
510 |
params=params, |
511 |
spindles=disk.get(constants.IDISK_SPINDLES)) |
512 |
disk_dev.name = disk.get(constants.IDISK_NAME, None)
|
513 |
disk_dev.uuid = lu.cfg.GenerateUniqueID(lu.proc.GetECId()) |
514 |
disks.append(disk_dev) |
515 |
|
516 |
return disks
|
517 |
|
518 |
|
519 |
def CheckSpindlesExclusiveStorage(diskdict, es_flag, required): |
520 |
"""Check the presence of the spindle options with exclusive_storage.
|
521 |
|
522 |
@type diskdict: dict
|
523 |
@param diskdict: disk parameters
|
524 |
@type es_flag: bool
|
525 |
@param es_flag: the effective value of the exlusive_storage flag
|
526 |
@type required: bool
|
527 |
@param required: whether spindles are required or just optional
|
528 |
@raise errors.OpPrereqError when spindles are given and they should not
|
529 |
|
530 |
"""
|
531 |
if (not es_flag and constants.IDISK_SPINDLES in diskdict and |
532 |
diskdict[constants.IDISK_SPINDLES] is not None): |
533 |
raise errors.OpPrereqError("Spindles in instance disks cannot be specified" |
534 |
" when exclusive storage is not active",
|
535 |
errors.ECODE_INVAL) |
536 |
if (es_flag and required and (constants.IDISK_SPINDLES not in diskdict or |
537 |
diskdict[constants.IDISK_SPINDLES] is None)): |
538 |
raise errors.OpPrereqError("You must specify spindles in instance disks" |
539 |
" when exclusive storage is active",
|
540 |
errors.ECODE_INVAL) |
541 |
|
542 |
|
543 |
class LUInstanceRecreateDisks(LogicalUnit): |
544 |
"""Recreate an instance's missing disks.
|
545 |
|
546 |
"""
|
547 |
HPATH = "instance-recreate-disks"
|
548 |
HTYPE = constants.HTYPE_INSTANCE |
549 |
REQ_BGL = False
|
550 |
|
551 |
_MODIFYABLE = compat.UniqueFrozenset([ |
552 |
constants.IDISK_SIZE, |
553 |
constants.IDISK_MODE, |
554 |
constants.IDISK_SPINDLES, |
555 |
]) |
556 |
|
557 |
# New or changed disk parameters may have different semantics
|
558 |
assert constants.IDISK_PARAMS == (_MODIFYABLE | frozenset([ |
559 |
constants.IDISK_ADOPT, |
560 |
|
561 |
# TODO: Implement support changing VG while recreating
|
562 |
constants.IDISK_VG, |
563 |
constants.IDISK_METAVG, |
564 |
constants.IDISK_PROVIDER, |
565 |
constants.IDISK_NAME, |
566 |
])) |
567 |
|
568 |
def _RunAllocator(self): |
569 |
"""Run the allocator based on input opcode.
|
570 |
|
571 |
"""
|
572 |
be_full = self.cfg.GetClusterInfo().FillBE(self.instance) |
573 |
|
574 |
# FIXME
|
575 |
# The allocator should actually run in "relocate" mode, but current
|
576 |
# allocators don't support relocating all the nodes of an instance at
|
577 |
# the same time. As a workaround we use "allocate" mode, but this is
|
578 |
# suboptimal for two reasons:
|
579 |
# - The instance name passed to the allocator is present in the list of
|
580 |
# existing instances, so there could be a conflict within the
|
581 |
# internal structures of the allocator. This doesn't happen with the
|
582 |
# current allocators, but it's a liability.
|
583 |
# - The allocator counts the resources used by the instance twice: once
|
584 |
# because the instance exists already, and once because it tries to
|
585 |
# allocate a new instance.
|
586 |
# The allocator could choose some of the nodes on which the instance is
|
587 |
# running, but that's not a problem. If the instance nodes are broken,
|
588 |
# they should be already be marked as drained or offline, and hence
|
589 |
# skipped by the allocator. If instance disks have been lost for other
|
590 |
# reasons, then recreating the disks on the same nodes should be fine.
|
591 |
disk_template = self.instance.disk_template
|
592 |
spindle_use = be_full[constants.BE_SPINDLE_USE] |
593 |
disks = [{ |
594 |
constants.IDISK_SIZE: d.size, |
595 |
constants.IDISK_MODE: d.mode, |
596 |
constants.IDISK_SPINDLES: d.spindles, |
597 |
} for d in self.instance.disks] |
598 |
req = iallocator.IAReqInstanceAlloc(name=self.op.instance_name,
|
599 |
disk_template=disk_template, |
600 |
tags=list(self.instance.GetTags()), |
601 |
os=self.instance.os,
|
602 |
nics=[{}], |
603 |
vcpus=be_full[constants.BE_VCPUS], |
604 |
memory=be_full[constants.BE_MAXMEM], |
605 |
spindle_use=spindle_use, |
606 |
disks=disks, |
607 |
hypervisor=self.instance.hypervisor,
|
608 |
node_whitelist=None)
|
609 |
ial = iallocator.IAllocator(self.cfg, self.rpc, req) |
610 |
|
611 |
ial.Run(self.op.iallocator)
|
612 |
|
613 |
assert req.RequiredNodes() == len(self.cfg.GetInstanceNodes(self.instance)) |
614 |
|
615 |
if not ial.success: |
616 |
raise errors.OpPrereqError("Can't compute nodes using iallocator '%s':" |
617 |
" %s" % (self.op.iallocator, ial.info), |
618 |
errors.ECODE_NORES) |
619 |
|
620 |
(self.op.node_uuids, self.op.nodes) = GetWantedNodes(self, ial.result) |
621 |
self.LogInfo("Selected nodes for instance %s via iallocator %s: %s", |
622 |
self.op.instance_name, self.op.iallocator, |
623 |
utils.CommaJoin(self.op.nodes))
|
624 |
|
625 |
def CheckArguments(self): |
626 |
if self.op.disks and ht.TNonNegativeInt(self.op.disks[0]): |
627 |
# Normalize and convert deprecated list of disk indices
|
628 |
self.op.disks = [(idx, {}) for idx in sorted(frozenset(self.op.disks))] |
629 |
|
630 |
duplicates = utils.FindDuplicates(map(compat.fst, self.op.disks)) |
631 |
if duplicates:
|
632 |
raise errors.OpPrereqError("Some disks have been specified more than" |
633 |
" once: %s" % utils.CommaJoin(duplicates),
|
634 |
errors.ECODE_INVAL) |
635 |
|
636 |
# We don't want _CheckIAllocatorOrNode selecting the default iallocator
|
637 |
# when neither iallocator nor nodes are specified
|
638 |
if self.op.iallocator or self.op.nodes: |
639 |
CheckIAllocatorOrNode(self, "iallocator", "nodes") |
640 |
|
641 |
for (idx, params) in self.op.disks: |
642 |
utils.ForceDictType(params, constants.IDISK_PARAMS_TYPES) |
643 |
unsupported = frozenset(params.keys()) - self._MODIFYABLE |
644 |
if unsupported:
|
645 |
raise errors.OpPrereqError("Parameters for disk %s try to change" |
646 |
" unmodifyable parameter(s): %s" %
|
647 |
(idx, utils.CommaJoin(unsupported)), |
648 |
errors.ECODE_INVAL) |
649 |
|
650 |
def ExpandNames(self): |
651 |
self._ExpandAndLockInstance()
|
652 |
self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
|
653 |
|
654 |
if self.op.nodes: |
655 |
(self.op.node_uuids, self.op.nodes) = GetWantedNodes(self, self.op.nodes) |
656 |
self.needed_locks[locking.LEVEL_NODE] = list(self.op.node_uuids) |
657 |
else:
|
658 |
self.needed_locks[locking.LEVEL_NODE] = []
|
659 |
if self.op.iallocator: |
660 |
# iallocator will select a new node in the same group
|
661 |
self.needed_locks[locking.LEVEL_NODEGROUP] = []
|
662 |
self.needed_locks[locking.LEVEL_NODE_ALLOC] = locking.ALL_SET
|
663 |
|
664 |
self.needed_locks[locking.LEVEL_NODE_RES] = []
|
665 |
|
666 |
def DeclareLocks(self, level): |
667 |
if level == locking.LEVEL_NODEGROUP:
|
668 |
assert self.op.iallocator is not None |
669 |
assert not self.op.nodes |
670 |
assert not self.needed_locks[locking.LEVEL_NODEGROUP] |
671 |
self.share_locks[locking.LEVEL_NODEGROUP] = 1 |
672 |
# Lock the primary group used by the instance optimistically; this
|
673 |
# requires going via the node before it's locked, requiring
|
674 |
# verification later on
|
675 |
self.needed_locks[locking.LEVEL_NODEGROUP] = \
|
676 |
self.cfg.GetInstanceNodeGroups(self.op.instance_uuid, primary_only=True) |
677 |
|
678 |
elif level == locking.LEVEL_NODE:
|
679 |
# If an allocator is used, then we lock all the nodes in the current
|
680 |
# instance group, as we don't know yet which ones will be selected;
|
681 |
# if we replace the nodes without using an allocator, locks are
|
682 |
# already declared in ExpandNames; otherwise, we need to lock all the
|
683 |
# instance nodes for disk re-creation
|
684 |
if self.op.iallocator: |
685 |
assert not self.op.nodes |
686 |
assert not self.needed_locks[locking.LEVEL_NODE] |
687 |
assert len(self.owned_locks(locking.LEVEL_NODEGROUP)) == 1 |
688 |
|
689 |
# Lock member nodes of the group of the primary node
|
690 |
for group_uuid in self.owned_locks(locking.LEVEL_NODEGROUP): |
691 |
self.needed_locks[locking.LEVEL_NODE].extend(
|
692 |
self.cfg.GetNodeGroup(group_uuid).members)
|
693 |
|
694 |
assert locking.NAL in self.owned_locks(locking.LEVEL_NODE_ALLOC) |
695 |
elif not self.op.nodes: |
696 |
self._LockInstancesNodes(primary_only=False) |
697 |
elif level == locking.LEVEL_NODE_RES:
|
698 |
# Copy node locks
|
699 |
self.needed_locks[locking.LEVEL_NODE_RES] = \
|
700 |
CopyLockList(self.needed_locks[locking.LEVEL_NODE])
|
701 |
|
702 |
def BuildHooksEnv(self): |
703 |
"""Build hooks env.
|
704 |
|
705 |
This runs on master, primary and secondary nodes of the instance.
|
706 |
|
707 |
"""
|
708 |
return BuildInstanceHookEnvByObject(self, self.instance) |
709 |
|
710 |
def BuildHooksNodes(self): |
711 |
"""Build hooks nodes.
|
712 |
|
713 |
"""
|
714 |
nl = [self.cfg.GetMasterNode()] + \
|
715 |
list(self.cfg.GetInstanceNodes(self.instance)) |
716 |
return (nl, nl)
|
717 |
|
718 |
def CheckPrereq(self): |
719 |
"""Check prerequisites.
|
720 |
|
721 |
This checks that the instance is in the cluster and is not running.
|
722 |
|
723 |
"""
|
724 |
instance = self.cfg.GetInstanceInfo(self.op.instance_uuid) |
725 |
assert instance is not None, \ |
726 |
"Cannot retrieve locked instance %s" % self.op.instance_name |
727 |
if self.op.node_uuids: |
728 |
inst_nodes = self.cfg.GetInstanceNodes(instance)
|
729 |
if len(self.op.node_uuids) != len(inst_nodes): |
730 |
raise errors.OpPrereqError("Instance %s currently has %d nodes, but" |
731 |
" %d replacement nodes were specified" %
|
732 |
(instance.name, len(inst_nodes),
|
733 |
len(self.op.node_uuids)), |
734 |
errors.ECODE_INVAL) |
735 |
assert instance.disk_template != constants.DT_DRBD8 or \ |
736 |
len(self.op.node_uuids) == 2 |
737 |
assert instance.disk_template != constants.DT_PLAIN or \ |
738 |
len(self.op.node_uuids) == 1 |
739 |
primary_node = self.op.node_uuids[0] |
740 |
else:
|
741 |
primary_node = instance.primary_node |
742 |
if not self.op.iallocator: |
743 |
CheckNodeOnline(self, primary_node)
|
744 |
|
745 |
if instance.disk_template == constants.DT_DISKLESS:
|
746 |
raise errors.OpPrereqError("Instance '%s' has no disks" % |
747 |
self.op.instance_name, errors.ECODE_INVAL)
|
748 |
|
749 |
# Verify if node group locks are still correct
|
750 |
owned_groups = self.owned_locks(locking.LEVEL_NODEGROUP)
|
751 |
if owned_groups:
|
752 |
# Node group locks are acquired only for the primary node (and only
|
753 |
# when the allocator is used)
|
754 |
CheckInstanceNodeGroups(self.cfg, instance.uuid, owned_groups,
|
755 |
primary_only=True)
|
756 |
|
757 |
# if we replace nodes *and* the old primary is offline, we don't
|
758 |
# check the instance state
|
759 |
old_pnode = self.cfg.GetNodeInfo(instance.primary_node)
|
760 |
if not ((self.op.iallocator or self.op.node_uuids) and old_pnode.offline): |
761 |
CheckInstanceState(self, instance, INSTANCE_NOT_RUNNING,
|
762 |
msg="cannot recreate disks")
|
763 |
|
764 |
if self.op.disks: |
765 |
self.disks = dict(self.op.disks) |
766 |
else:
|
767 |
self.disks = dict((idx, {}) for idx in range(len(instance.disks))) |
768 |
|
769 |
maxidx = max(self.disks.keys()) |
770 |
if maxidx >= len(instance.disks): |
771 |
raise errors.OpPrereqError("Invalid disk index '%s'" % maxidx, |
772 |
errors.ECODE_INVAL) |
773 |
|
774 |
if ((self.op.node_uuids or self.op.iallocator) and |
775 |
sorted(self.disks.keys()) != range(len(instance.disks))): |
776 |
raise errors.OpPrereqError("Can't recreate disks partially and" |
777 |
" change the nodes at the same time",
|
778 |
errors.ECODE_INVAL) |
779 |
|
780 |
self.instance = instance
|
781 |
|
782 |
if self.op.iallocator: |
783 |
self._RunAllocator()
|
784 |
# Release unneeded node and node resource locks
|
785 |
ReleaseLocks(self, locking.LEVEL_NODE, keep=self.op.node_uuids) |
786 |
ReleaseLocks(self, locking.LEVEL_NODE_RES, keep=self.op.node_uuids) |
787 |
ReleaseLocks(self, locking.LEVEL_NODE_ALLOC)
|
788 |
|
789 |
assert not self.glm.is_owned(locking.LEVEL_NODE_ALLOC) |
790 |
|
791 |
if self.op.node_uuids: |
792 |
node_uuids = self.op.node_uuids
|
793 |
else:
|
794 |
node_uuids = self.cfg.GetInstanceNodes(instance)
|
795 |
excl_stor = compat.any( |
796 |
rpc.GetExclusiveStorageForNodes(self.cfg, node_uuids).values()
|
797 |
) |
798 |
for new_params in self.disks.values(): |
799 |
CheckSpindlesExclusiveStorage(new_params, excl_stor, False)
|
800 |
|
801 |
def Exec(self, feedback_fn): |
802 |
"""Recreate the disks.
|
803 |
|
804 |
"""
|
805 |
assert (self.owned_locks(locking.LEVEL_NODE) == |
806 |
self.owned_locks(locking.LEVEL_NODE_RES))
|
807 |
|
808 |
to_skip = [] |
809 |
mods = [] # keeps track of needed changes
|
810 |
|
811 |
for idx, disk in enumerate(self.instance.disks): |
812 |
try:
|
813 |
changes = self.disks[idx]
|
814 |
except KeyError: |
815 |
# Disk should not be recreated
|
816 |
to_skip.append(idx) |
817 |
continue
|
818 |
|
819 |
# update secondaries for disks, if needed
|
820 |
if self.op.node_uuids and disk.dev_type == constants.DT_DRBD8: |
821 |
# need to update the nodes and minors
|
822 |
assert len(self.op.node_uuids) == 2 |
823 |
assert len(disk.logical_id) == 6 # otherwise disk internals |
824 |
# have changed
|
825 |
(_, _, old_port, _, _, old_secret) = disk.logical_id |
826 |
new_minors = self.cfg.AllocateDRBDMinor(self.op.node_uuids, |
827 |
self.instance.uuid)
|
828 |
new_id = (self.op.node_uuids[0], self.op.node_uuids[1], old_port, |
829 |
new_minors[0], new_minors[1], old_secret) |
830 |
assert len(disk.logical_id) == len(new_id) |
831 |
else:
|
832 |
new_id = None
|
833 |
|
834 |
mods.append((idx, new_id, changes)) |
835 |
|
836 |
# now that we have passed all asserts above, we can apply the mods
|
837 |
# in a single run (to avoid partial changes)
|
838 |
for idx, new_id, changes in mods: |
839 |
disk = self.instance.disks[idx]
|
840 |
if new_id is not None: |
841 |
assert disk.dev_type == constants.DT_DRBD8
|
842 |
disk.logical_id = new_id |
843 |
if changes:
|
844 |
disk.Update(size=changes.get(constants.IDISK_SIZE, None),
|
845 |
mode=changes.get(constants.IDISK_MODE, None),
|
846 |
spindles=changes.get(constants.IDISK_SPINDLES, None))
|
847 |
|
848 |
# change primary node, if needed
|
849 |
if self.op.node_uuids: |
850 |
self.instance.primary_node = self.op.node_uuids[0] |
851 |
self.LogWarning("Changing the instance's nodes, you will have to" |
852 |
" remove any disks left on the older nodes manually")
|
853 |
|
854 |
if self.op.node_uuids: |
855 |
self.cfg.Update(self.instance, feedback_fn) |
856 |
|
857 |
# All touched nodes must be locked
|
858 |
mylocks = self.owned_locks(locking.LEVEL_NODE)
|
859 |
inst_nodes = self.cfg.GetInstanceNodes(self.instance) |
860 |
assert mylocks.issuperset(frozenset(inst_nodes)) |
861 |
new_disks = CreateDisks(self, self.instance, to_skip=to_skip) |
862 |
|
863 |
# TODO: Release node locks before wiping, or explain why it's not possible
|
864 |
if self.cfg.GetClusterInfo().prealloc_wipe_disks: |
865 |
wipedisks = [(idx, disk, 0)
|
866 |
for (idx, disk) in enumerate(self.instance.disks) |
867 |
if idx not in to_skip] |
868 |
WipeOrCleanupDisks(self, self.instance, disks=wipedisks, |
869 |
cleanup=new_disks) |
870 |
|
871 |
|
872 |
def _PerformNodeInfoCall(lu, node_uuids, vg): |
873 |
"""Prepares the input and performs a node info call.
|
874 |
|
875 |
@type lu: C{LogicalUnit}
|
876 |
@param lu: a logical unit from which we get configuration data
|
877 |
@type node_uuids: list of string
|
878 |
@param node_uuids: list of node UUIDs to perform the call for
|
879 |
@type vg: string
|
880 |
@param vg: the volume group's name
|
881 |
|
882 |
"""
|
883 |
lvm_storage_units = [(constants.ST_LVM_VG, vg)] |
884 |
storage_units = rpc.PrepareStorageUnitsForNodes(lu.cfg, lvm_storage_units, |
885 |
node_uuids) |
886 |
hvname = lu.cfg.GetHypervisorType() |
887 |
hvparams = lu.cfg.GetClusterInfo().hvparams |
888 |
nodeinfo = lu.rpc.call_node_info(node_uuids, storage_units, |
889 |
[(hvname, hvparams[hvname])]) |
890 |
return nodeinfo
|
891 |
|
892 |
|
893 |
def _CheckVgCapacityForNode(node_name, node_info, vg, requested): |
894 |
"""Checks the vg capacity for a given node.
|
895 |
|
896 |
@type node_info: tuple (_, list of dicts, _)
|
897 |
@param node_info: the result of the node info call for one node
|
898 |
@type node_name: string
|
899 |
@param node_name: the name of the node
|
900 |
@type vg: string
|
901 |
@param vg: volume group name
|
902 |
@type requested: int
|
903 |
@param requested: the amount of disk in MiB to check for
|
904 |
@raise errors.OpPrereqError: if the node doesn't have enough disk,
|
905 |
or we cannot check the node
|
906 |
|
907 |
"""
|
908 |
(_, space_info, _) = node_info |
909 |
lvm_vg_info = utils.storage.LookupSpaceInfoByStorageType( |
910 |
space_info, constants.ST_LVM_VG) |
911 |
if not lvm_vg_info: |
912 |
raise errors.OpPrereqError("Can't retrieve storage information for LVM") |
913 |
vg_free = lvm_vg_info.get("storage_free", None) |
914 |
if not isinstance(vg_free, int): |
915 |
raise errors.OpPrereqError("Can't compute free disk space on node" |
916 |
" %s for vg %s, result was '%s'" %
|
917 |
(node_name, vg, vg_free), errors.ECODE_ENVIRON) |
918 |
if requested > vg_free:
|
919 |
raise errors.OpPrereqError("Not enough disk space on target node %s" |
920 |
" vg %s: required %d MiB, available %d MiB" %
|
921 |
(node_name, vg, requested, vg_free), |
922 |
errors.ECODE_NORES) |
923 |
|
924 |
|
925 |
def _CheckNodesFreeDiskOnVG(lu, node_uuids, vg, requested): |
926 |
"""Checks if nodes have enough free disk space in the specified VG.
|
927 |
|
928 |
This function checks if all given nodes have the needed amount of
|
929 |
free disk. In case any node has less disk or we cannot get the
|
930 |
information from the node, this function raises an OpPrereqError
|
931 |
exception.
|
932 |
|
933 |
@type lu: C{LogicalUnit}
|
934 |
@param lu: a logical unit from which we get configuration data
|
935 |
@type node_uuids: C{list}
|
936 |
@param node_uuids: the list of node UUIDs to check
|
937 |
@type vg: C{str}
|
938 |
@param vg: the volume group to check
|
939 |
@type requested: C{int}
|
940 |
@param requested: the amount of disk in MiB to check for
|
941 |
@raise errors.OpPrereqError: if the node doesn't have enough disk,
|
942 |
or we cannot check the node
|
943 |
|
944 |
"""
|
945 |
nodeinfo = _PerformNodeInfoCall(lu, node_uuids, vg) |
946 |
for node_uuid in node_uuids: |
947 |
node_name = lu.cfg.GetNodeName(node_uuid) |
948 |
info = nodeinfo[node_uuid] |
949 |
info.Raise("Cannot get current information from node %s" % node_name,
|
950 |
prereq=True, ecode=errors.ECODE_ENVIRON)
|
951 |
_CheckVgCapacityForNode(node_name, info.payload, vg, requested) |
952 |
|
953 |
|
954 |
def CheckNodesFreeDiskPerVG(lu, node_uuids, req_sizes): |
955 |
"""Checks if nodes have enough free disk space in all the VGs.
|
956 |
|
957 |
This function checks if all given nodes have the needed amount of
|
958 |
free disk. In case any node has less disk or we cannot get the
|
959 |
information from the node, this function raises an OpPrereqError
|
960 |
exception.
|
961 |
|
962 |
@type lu: C{LogicalUnit}
|
963 |
@param lu: a logical unit from which we get configuration data
|
964 |
@type node_uuids: C{list}
|
965 |
@param node_uuids: the list of node UUIDs to check
|
966 |
@type req_sizes: C{dict}
|
967 |
@param req_sizes: the hash of vg and corresponding amount of disk in
|
968 |
MiB to check for
|
969 |
@raise errors.OpPrereqError: if the node doesn't have enough disk,
|
970 |
or we cannot check the node
|
971 |
|
972 |
"""
|
973 |
for vg, req_size in req_sizes.items(): |
974 |
_CheckNodesFreeDiskOnVG(lu, node_uuids, vg, req_size) |
975 |
|
976 |
|
977 |
def _DiskSizeInBytesToMebibytes(lu, size): |
978 |
"""Converts a disk size in bytes to mebibytes.
|
979 |
|
980 |
Warns and rounds up if the size isn't an even multiple of 1 MiB.
|
981 |
|
982 |
"""
|
983 |
(mib, remainder) = divmod(size, 1024 * 1024) |
984 |
|
985 |
if remainder != 0: |
986 |
lu.LogWarning("Disk size is not an even multiple of 1 MiB; rounding up"
|
987 |
" to not overwrite existing data (%s bytes will not be"
|
988 |
" wiped)", (1024 * 1024) - remainder) |
989 |
mib += 1
|
990 |
|
991 |
return mib
|
992 |
|
993 |
|
994 |
def _CalcEta(time_taken, written, total_size): |
995 |
"""Calculates the ETA based on size written and total size.
|
996 |
|
997 |
@param time_taken: The time taken so far
|
998 |
@param written: amount written so far
|
999 |
@param total_size: The total size of data to be written
|
1000 |
@return: The remaining time in seconds
|
1001 |
|
1002 |
"""
|
1003 |
avg_time = time_taken / float(written)
|
1004 |
return (total_size - written) * avg_time
|
1005 |
|
1006 |
|
1007 |
def WipeDisks(lu, instance, disks=None): |
1008 |
"""Wipes instance disks.
|
1009 |
|
1010 |
@type lu: L{LogicalUnit}
|
1011 |
@param lu: the logical unit on whose behalf we execute
|
1012 |
@type instance: L{objects.Instance}
|
1013 |
@param instance: the instance whose disks we should create
|
1014 |
@type disks: None or list of tuple of (number, L{objects.Disk}, number)
|
1015 |
@param disks: Disk details; tuple contains disk index, disk object and the
|
1016 |
start offset
|
1017 |
|
1018 |
"""
|
1019 |
node_uuid = instance.primary_node |
1020 |
node_name = lu.cfg.GetNodeName(node_uuid) |
1021 |
|
1022 |
if disks is None: |
1023 |
disks = [(idx, disk, 0)
|
1024 |
for (idx, disk) in enumerate(instance.disks)] |
1025 |
|
1026 |
logging.info("Pausing synchronization of disks of instance '%s'",
|
1027 |
instance.name) |
1028 |
result = lu.rpc.call_blockdev_pause_resume_sync(node_uuid, |
1029 |
(map(compat.snd, disks),
|
1030 |
instance), |
1031 |
True)
|
1032 |
result.Raise("Failed to pause disk synchronization on node '%s'" % node_name)
|
1033 |
|
1034 |
for idx, success in enumerate(result.payload): |
1035 |
if not success: |
1036 |
logging.warn("Pausing synchronization of disk %s of instance '%s'"
|
1037 |
" failed", idx, instance.name)
|
1038 |
|
1039 |
try:
|
1040 |
for (idx, device, offset) in disks: |
1041 |
# The wipe size is MIN_WIPE_CHUNK_PERCENT % of the instance disk but
|
1042 |
# MAX_WIPE_CHUNK at max. Truncating to integer to avoid rounding errors.
|
1043 |
wipe_chunk_size = \ |
1044 |
int(min(constants.MAX_WIPE_CHUNK, |
1045 |
device.size / 100.0 * constants.MIN_WIPE_CHUNK_PERCENT))
|
1046 |
|
1047 |
size = device.size |
1048 |
last_output = 0
|
1049 |
start_time = time.time() |
1050 |
|
1051 |
if offset == 0: |
1052 |
info_text = ""
|
1053 |
else:
|
1054 |
info_text = (" (from %s to %s)" %
|
1055 |
(utils.FormatUnit(offset, "h"),
|
1056 |
utils.FormatUnit(size, "h")))
|
1057 |
|
1058 |
lu.LogInfo("* Wiping disk %s%s", idx, info_text)
|
1059 |
|
1060 |
logging.info("Wiping disk %d for instance %s on node %s using"
|
1061 |
" chunk size %s", idx, instance.name, node_name,
|
1062 |
wipe_chunk_size) |
1063 |
|
1064 |
while offset < size:
|
1065 |
wipe_size = min(wipe_chunk_size, size - offset)
|
1066 |
|
1067 |
logging.debug("Wiping disk %d, offset %s, chunk %s",
|
1068 |
idx, offset, wipe_size) |
1069 |
|
1070 |
result = lu.rpc.call_blockdev_wipe(node_uuid, (device, instance), |
1071 |
offset, wipe_size) |
1072 |
result.Raise("Could not wipe disk %d at offset %d for size %d" %
|
1073 |
(idx, offset, wipe_size)) |
1074 |
|
1075 |
now = time.time() |
1076 |
offset += wipe_size |
1077 |
if now - last_output >= 60: |
1078 |
eta = _CalcEta(now - start_time, offset, size) |
1079 |
lu.LogInfo(" - done: %.1f%% ETA: %s",
|
1080 |
offset / float(size) * 100, utils.FormatSeconds(eta)) |
1081 |
last_output = now |
1082 |
finally:
|
1083 |
logging.info("Resuming synchronization of disks for instance '%s'",
|
1084 |
instance.name) |
1085 |
|
1086 |
result = lu.rpc.call_blockdev_pause_resume_sync(node_uuid, |
1087 |
(map(compat.snd, disks),
|
1088 |
instance), |
1089 |
False)
|
1090 |
|
1091 |
if result.fail_msg:
|
1092 |
lu.LogWarning("Failed to resume disk synchronization on node '%s': %s",
|
1093 |
node_name, result.fail_msg) |
1094 |
else:
|
1095 |
for idx, success in enumerate(result.payload): |
1096 |
if not success: |
1097 |
lu.LogWarning("Resuming synchronization of disk %s of instance '%s'"
|
1098 |
" failed", idx, instance.name)
|
1099 |
|
1100 |
|
1101 |
def WipeOrCleanupDisks(lu, instance, disks=None, cleanup=None): |
1102 |
"""Wrapper for L{WipeDisks} that handles errors.
|
1103 |
|
1104 |
@type lu: L{LogicalUnit}
|
1105 |
@param lu: the logical unit on whose behalf we execute
|
1106 |
@type instance: L{objects.Instance}
|
1107 |
@param instance: the instance whose disks we should wipe
|
1108 |
@param disks: see L{WipeDisks}
|
1109 |
@param cleanup: the result returned by L{CreateDisks}, used for cleanup in
|
1110 |
case of error
|
1111 |
@raise errors.OpPrereqError: in case of failure
|
1112 |
|
1113 |
"""
|
1114 |
try:
|
1115 |
WipeDisks(lu, instance, disks=disks) |
1116 |
except errors.OpExecError:
|
1117 |
logging.warning("Wiping disks for instance '%s' failed",
|
1118 |
instance.name) |
1119 |
_UndoCreateDisks(lu, cleanup, instance) |
1120 |
raise
|
1121 |
|
1122 |
|
1123 |
def ExpandCheckDisks(instance, disks): |
1124 |
"""Return the instance disks selected by the disks list
|
1125 |
|
1126 |
@type disks: list of L{objects.Disk} or None
|
1127 |
@param disks: selected disks
|
1128 |
@rtype: list of L{objects.Disk}
|
1129 |
@return: selected instance disks to act on
|
1130 |
|
1131 |
"""
|
1132 |
if disks is None: |
1133 |
return instance.disks
|
1134 |
else:
|
1135 |
if not set(disks).issubset(instance.disks): |
1136 |
raise errors.ProgrammerError("Can only act on disks belonging to the" |
1137 |
" target instance: expected a subset of %r,"
|
1138 |
" got %r" % (instance.disks, disks))
|
1139 |
return disks
|
1140 |
|
1141 |
|
1142 |
def WaitForSync(lu, instance, disks=None, oneshot=False): |
1143 |
"""Sleep and poll for an instance's disk to sync.
|
1144 |
|
1145 |
"""
|
1146 |
if not instance.disks or disks is not None and not disks: |
1147 |
return True |
1148 |
|
1149 |
disks = ExpandCheckDisks(instance, disks) |
1150 |
|
1151 |
if not oneshot: |
1152 |
lu.LogInfo("Waiting for instance %s to sync disks", instance.name)
|
1153 |
|
1154 |
node_uuid = instance.primary_node |
1155 |
node_name = lu.cfg.GetNodeName(node_uuid) |
1156 |
|
1157 |
# TODO: Convert to utils.Retry
|
1158 |
|
1159 |
retries = 0
|
1160 |
degr_retries = 10 # in seconds, as we sleep 1 second each time |
1161 |
while True: |
1162 |
max_time = 0
|
1163 |
done = True
|
1164 |
cumul_degraded = False
|
1165 |
rstats = lu.rpc.call_blockdev_getmirrorstatus(node_uuid, (disks, instance)) |
1166 |
msg = rstats.fail_msg |
1167 |
if msg:
|
1168 |
lu.LogWarning("Can't get any data from node %s: %s", node_name, msg)
|
1169 |
retries += 1
|
1170 |
if retries >= 10: |
1171 |
raise errors.RemoteError("Can't contact node %s for mirror data," |
1172 |
" aborting." % node_name)
|
1173 |
time.sleep(6)
|
1174 |
continue
|
1175 |
rstats = rstats.payload |
1176 |
retries = 0
|
1177 |
for i, mstat in enumerate(rstats): |
1178 |
if mstat is None: |
1179 |
lu.LogWarning("Can't compute data for node %s/%s",
|
1180 |
node_name, disks[i].iv_name) |
1181 |
continue
|
1182 |
|
1183 |
cumul_degraded = (cumul_degraded or
|
1184 |
(mstat.is_degraded and mstat.sync_percent is None)) |
1185 |
if mstat.sync_percent is not None: |
1186 |
done = False
|
1187 |
if mstat.estimated_time is not None: |
1188 |
rem_time = ("%s remaining (estimated)" %
|
1189 |
utils.FormatSeconds(mstat.estimated_time)) |
1190 |
max_time = mstat.estimated_time |
1191 |
else:
|
1192 |
rem_time = "no time estimate"
|
1193 |
lu.LogInfo("- device %s: %5.2f%% done, %s",
|
1194 |
disks[i].iv_name, mstat.sync_percent, rem_time) |
1195 |
|
1196 |
# if we're done but degraded, let's do a few small retries, to
|
1197 |
# make sure we see a stable and not transient situation; therefore
|
1198 |
# we force restart of the loop
|
1199 |
if (done or oneshot) and cumul_degraded and degr_retries > 0: |
1200 |
logging.info("Degraded disks found, %d retries left", degr_retries)
|
1201 |
degr_retries -= 1
|
1202 |
time.sleep(1)
|
1203 |
continue
|
1204 |
|
1205 |
if done or oneshot: |
1206 |
break
|
1207 |
|
1208 |
time.sleep(min(60, max_time)) |
1209 |
|
1210 |
if done:
|
1211 |
lu.LogInfo("Instance %s's disks are in sync", instance.name)
|
1212 |
|
1213 |
return not cumul_degraded |
1214 |
|
1215 |
|
1216 |
def ShutdownInstanceDisks(lu, instance, disks=None, ignore_primary=False): |
1217 |
"""Shutdown block devices of an instance.
|
1218 |
|
1219 |
This does the shutdown on all nodes of the instance.
|
1220 |
|
1221 |
If the ignore_primary is false, errors on the primary node are
|
1222 |
ignored.
|
1223 |
|
1224 |
"""
|
1225 |
all_result = True
|
1226 |
|
1227 |
if disks is None: |
1228 |
# only mark instance disks as inactive if all disks are affected
|
1229 |
lu.cfg.MarkInstanceDisksInactive(instance.uuid) |
1230 |
disks = ExpandCheckDisks(instance, disks) |
1231 |
|
1232 |
for disk in disks: |
1233 |
for node_uuid, top_disk in disk.ComputeNodeTree(instance.primary_node): |
1234 |
result = lu.rpc.call_blockdev_shutdown(node_uuid, (top_disk, instance)) |
1235 |
msg = result.fail_msg |
1236 |
if msg:
|
1237 |
lu.LogWarning("Could not shutdown block device %s on node %s: %s",
|
1238 |
disk.iv_name, lu.cfg.GetNodeName(node_uuid), msg) |
1239 |
if ((node_uuid == instance.primary_node and not ignore_primary) or |
1240 |
(node_uuid != instance.primary_node and not result.offline)): |
1241 |
all_result = False
|
1242 |
return all_result
|
1243 |
|
1244 |
|
1245 |
def _SafeShutdownInstanceDisks(lu, instance, disks=None): |
1246 |
"""Shutdown block devices of an instance.
|
1247 |
|
1248 |
This function checks if an instance is running, before calling
|
1249 |
_ShutdownInstanceDisks.
|
1250 |
|
1251 |
"""
|
1252 |
CheckInstanceState(lu, instance, INSTANCE_DOWN, msg="cannot shutdown disks")
|
1253 |
ShutdownInstanceDisks(lu, instance, disks=disks) |
1254 |
|
1255 |
|
1256 |
def AssembleInstanceDisks(lu, instance, disks=None, ignore_secondaries=False, |
1257 |
ignore_size=False):
|
1258 |
"""Prepare the block devices for an instance.
|
1259 |
|
1260 |
This sets up the block devices on all nodes.
|
1261 |
|
1262 |
@type lu: L{LogicalUnit}
|
1263 |
@param lu: the logical unit on whose behalf we execute
|
1264 |
@type instance: L{objects.Instance}
|
1265 |
@param instance: the instance for whose disks we assemble
|
1266 |
@type disks: list of L{objects.Disk} or None
|
1267 |
@param disks: which disks to assemble (or all, if None)
|
1268 |
@type ignore_secondaries: boolean
|
1269 |
@param ignore_secondaries: if true, errors on secondary nodes
|
1270 |
won't result in an error return from the function
|
1271 |
@type ignore_size: boolean
|
1272 |
@param ignore_size: if true, the current known size of the disk
|
1273 |
will not be used during the disk activation, useful for cases
|
1274 |
when the size is wrong
|
1275 |
@return: False if the operation failed, otherwise a list of
|
1276 |
(host, instance_visible_name, node_visible_name)
|
1277 |
with the mapping from node devices to instance devices
|
1278 |
|
1279 |
"""
|
1280 |
device_info = [] |
1281 |
disks_ok = True
|
1282 |
|
1283 |
if disks is None: |
1284 |
# only mark instance disks as active if all disks are affected
|
1285 |
lu.cfg.MarkInstanceDisksActive(instance.uuid) |
1286 |
|
1287 |
disks = ExpandCheckDisks(instance, disks) |
1288 |
|
1289 |
# With the two passes mechanism we try to reduce the window of
|
1290 |
# opportunity for the race condition of switching DRBD to primary
|
1291 |
# before handshaking occured, but we do not eliminate it
|
1292 |
|
1293 |
# The proper fix would be to wait (with some limits) until the
|
1294 |
# connection has been made and drbd transitions from WFConnection
|
1295 |
# into any other network-connected state (Connected, SyncTarget,
|
1296 |
# SyncSource, etc.)
|
1297 |
|
1298 |
# 1st pass, assemble on all nodes in secondary mode
|
1299 |
for idx, inst_disk in enumerate(disks): |
1300 |
for node_uuid, node_disk in inst_disk.ComputeNodeTree( |
1301 |
instance.primary_node): |
1302 |
if ignore_size:
|
1303 |
node_disk = node_disk.Copy() |
1304 |
node_disk.UnsetSize() |
1305 |
result = lu.rpc.call_blockdev_assemble(node_uuid, (node_disk, instance), |
1306 |
instance.name, False, idx)
|
1307 |
msg = result.fail_msg |
1308 |
if msg:
|
1309 |
secondary_nodes = lu.cfg.GetInstanceSecondaryNodes(instance) |
1310 |
is_offline_secondary = (node_uuid in secondary_nodes and |
1311 |
result.offline) |
1312 |
lu.LogWarning("Could not prepare block device %s on node %s"
|
1313 |
" (is_primary=False, pass=1): %s",
|
1314 |
inst_disk.iv_name, lu.cfg.GetNodeName(node_uuid), msg) |
1315 |
if not (ignore_secondaries or is_offline_secondary): |
1316 |
disks_ok = False
|
1317 |
|
1318 |
# FIXME: race condition on drbd migration to primary
|
1319 |
|
1320 |
# 2nd pass, do only the primary node
|
1321 |
for idx, inst_disk in enumerate(disks): |
1322 |
dev_path = None
|
1323 |
|
1324 |
for node_uuid, node_disk in inst_disk.ComputeNodeTree( |
1325 |
instance.primary_node): |
1326 |
if node_uuid != instance.primary_node:
|
1327 |
continue
|
1328 |
if ignore_size:
|
1329 |
node_disk = node_disk.Copy() |
1330 |
node_disk.UnsetSize() |
1331 |
result = lu.rpc.call_blockdev_assemble(node_uuid, (node_disk, instance), |
1332 |
instance.name, True, idx)
|
1333 |
msg = result.fail_msg |
1334 |
if msg:
|
1335 |
lu.LogWarning("Could not prepare block device %s on node %s"
|
1336 |
" (is_primary=True, pass=2): %s",
|
1337 |
inst_disk.iv_name, lu.cfg.GetNodeName(node_uuid), msg) |
1338 |
disks_ok = False
|
1339 |
else:
|
1340 |
dev_path, _ = result.payload |
1341 |
|
1342 |
device_info.append((lu.cfg.GetNodeName(instance.primary_node), |
1343 |
inst_disk.iv_name, dev_path)) |
1344 |
|
1345 |
if not disks_ok: |
1346 |
lu.cfg.MarkInstanceDisksInactive(instance.uuid) |
1347 |
|
1348 |
return disks_ok, device_info
|
1349 |
|
1350 |
|
1351 |
def StartInstanceDisks(lu, instance, force): |
1352 |
"""Start the disks of an instance.
|
1353 |
|
1354 |
"""
|
1355 |
disks_ok, _ = AssembleInstanceDisks(lu, instance, |
1356 |
ignore_secondaries=force) |
1357 |
if not disks_ok: |
1358 |
ShutdownInstanceDisks(lu, instance) |
1359 |
if force is not None and not force: |
1360 |
lu.LogWarning("",
|
1361 |
hint=("If the message above refers to a secondary node,"
|
1362 |
" you can retry the operation using '--force'"))
|
1363 |
raise errors.OpExecError("Disk consistency error") |
1364 |
|
1365 |
|
1366 |
class LUInstanceGrowDisk(LogicalUnit): |
1367 |
"""Grow a disk of an instance.
|
1368 |
|
1369 |
"""
|
1370 |
HPATH = "disk-grow"
|
1371 |
HTYPE = constants.HTYPE_INSTANCE |
1372 |
REQ_BGL = False
|
1373 |
|
1374 |
def ExpandNames(self): |
1375 |
self._ExpandAndLockInstance()
|
1376 |
self.needed_locks[locking.LEVEL_NODE] = []
|
1377 |
self.needed_locks[locking.LEVEL_NODE_RES] = []
|
1378 |
self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
1379 |
self.recalculate_locks[locking.LEVEL_NODE_RES] = constants.LOCKS_REPLACE
|
1380 |
|
1381 |
def DeclareLocks(self, level): |
1382 |
if level == locking.LEVEL_NODE:
|
1383 |
self._LockInstancesNodes()
|
1384 |
elif level == locking.LEVEL_NODE_RES:
|
1385 |
# Copy node locks
|
1386 |
self.needed_locks[locking.LEVEL_NODE_RES] = \
|
1387 |
CopyLockList(self.needed_locks[locking.LEVEL_NODE])
|
1388 |
|
1389 |
def BuildHooksEnv(self): |
1390 |
"""Build hooks env.
|
1391 |
|
1392 |
This runs on the master, the primary and all the secondaries.
|
1393 |
|
1394 |
"""
|
1395 |
env = { |
1396 |
"DISK": self.op.disk, |
1397 |
"AMOUNT": self.op.amount, |
1398 |
"ABSOLUTE": self.op.absolute, |
1399 |
} |
1400 |
env.update(BuildInstanceHookEnvByObject(self, self.instance)) |
1401 |
return env
|
1402 |
|
1403 |
def BuildHooksNodes(self): |
1404 |
"""Build hooks nodes.
|
1405 |
|
1406 |
"""
|
1407 |
nl = [self.cfg.GetMasterNode()] + \
|
1408 |
list(self.cfg.GetInstanceNodes(self.instance)) |
1409 |
return (nl, nl)
|
1410 |
|
1411 |
def CheckPrereq(self): |
1412 |
"""Check prerequisites.
|
1413 |
|
1414 |
This checks that the instance is in the cluster.
|
1415 |
|
1416 |
"""
|
1417 |
self.instance = self.cfg.GetInstanceInfo(self.op.instance_uuid) |
1418 |
assert self.instance is not None, \ |
1419 |
"Cannot retrieve locked instance %s" % self.op.instance_name |
1420 |
node_uuids = list(self.cfg.GetInstanceNodes(self.instance)) |
1421 |
for node_uuid in node_uuids: |
1422 |
CheckNodeOnline(self, node_uuid)
|
1423 |
self.node_es_flags = rpc.GetExclusiveStorageForNodes(self.cfg, node_uuids) |
1424 |
|
1425 |
if self.instance.disk_template not in constants.DTS_GROWABLE: |
1426 |
raise errors.OpPrereqError("Instance's disk layout does not support" |
1427 |
" growing", errors.ECODE_INVAL)
|
1428 |
|
1429 |
self.disk = self.instance.FindDisk(self.op.disk) |
1430 |
|
1431 |
if self.op.absolute: |
1432 |
self.target = self.op.amount |
1433 |
self.delta = self.target - self.disk.size |
1434 |
if self.delta < 0: |
1435 |
raise errors.OpPrereqError("Requested size (%s) is smaller than " |
1436 |
"current disk size (%s)" %
|
1437 |
(utils.FormatUnit(self.target, "h"), |
1438 |
utils.FormatUnit(self.disk.size, "h")), |
1439 |
errors.ECODE_STATE) |
1440 |
else:
|
1441 |
self.delta = self.op.amount |
1442 |
self.target = self.disk.size + self.delta |
1443 |
if self.delta < 0: |
1444 |
raise errors.OpPrereqError("Requested increment (%s) is negative" % |
1445 |
utils.FormatUnit(self.delta, "h"), |
1446 |
errors.ECODE_INVAL) |
1447 |
|
1448 |
self._CheckDiskSpace(node_uuids, self.disk.ComputeGrowth(self.delta)) |
1449 |
|
1450 |
def _CheckDiskSpace(self, node_uuids, req_vgspace): |
1451 |
template = self.instance.disk_template
|
1452 |
if (template not in (constants.DTS_NO_FREE_SPACE_CHECK) and |
1453 |
not any(self.node_es_flags.values())): |
1454 |
# TODO: check the free disk space for file, when that feature will be
|
1455 |
# supported
|
1456 |
# With exclusive storage we need to do something smarter than just looking
|
1457 |
# at free space, which, in the end, is basically a dry run. So we rely on
|
1458 |
# the dry run performed in Exec() instead.
|
1459 |
CheckNodesFreeDiskPerVG(self, node_uuids, req_vgspace)
|
1460 |
|
1461 |
def Exec(self, feedback_fn): |
1462 |
"""Execute disk grow.
|
1463 |
|
1464 |
"""
|
1465 |
assert set([self.instance.name]) == self.owned_locks(locking.LEVEL_INSTANCE) |
1466 |
assert (self.owned_locks(locking.LEVEL_NODE) == |
1467 |
self.owned_locks(locking.LEVEL_NODE_RES))
|
1468 |
|
1469 |
wipe_disks = self.cfg.GetClusterInfo().prealloc_wipe_disks
|
1470 |
|
1471 |
disks_ok, _ = AssembleInstanceDisks(self, self.instance, disks=[self.disk]) |
1472 |
if not disks_ok: |
1473 |
raise errors.OpExecError("Cannot activate block device to grow") |
1474 |
|
1475 |
feedback_fn("Growing disk %s of instance '%s' by %s to %s" %
|
1476 |
(self.op.disk, self.instance.name, |
1477 |
utils.FormatUnit(self.delta, "h"), |
1478 |
utils.FormatUnit(self.target, "h"))) |
1479 |
|
1480 |
# First run all grow ops in dry-run mode
|
1481 |
inst_nodes = self.cfg.GetInstanceNodes(self.instance) |
1482 |
for node_uuid in inst_nodes: |
1483 |
result = self.rpc.call_blockdev_grow(node_uuid,
|
1484 |
(self.disk, self.instance), |
1485 |
self.delta, True, True, |
1486 |
self.node_es_flags[node_uuid])
|
1487 |
result.Raise("Dry-run grow request failed to node %s" %
|
1488 |
self.cfg.GetNodeName(node_uuid))
|
1489 |
|
1490 |
if wipe_disks:
|
1491 |
# Get disk size from primary node for wiping
|
1492 |
result = self.rpc.call_blockdev_getdimensions(
|
1493 |
self.instance.primary_node, [([self.disk], self.instance)]) |
1494 |
result.Raise("Failed to retrieve disk size from node '%s'" %
|
1495 |
self.instance.primary_node)
|
1496 |
|
1497 |
(disk_dimensions, ) = result.payload |
1498 |
|
1499 |
if disk_dimensions is None: |
1500 |
raise errors.OpExecError("Failed to retrieve disk size from primary" |
1501 |
" node '%s'" % self.instance.primary_node) |
1502 |
(disk_size_in_bytes, _) = disk_dimensions |
1503 |
|
1504 |
old_disk_size = _DiskSizeInBytesToMebibytes(self, disk_size_in_bytes)
|
1505 |
|
1506 |
assert old_disk_size >= self.disk.size, \ |
1507 |
("Retrieved disk size too small (got %s, should be at least %s)" %
|
1508 |
(old_disk_size, self.disk.size))
|
1509 |
else:
|
1510 |
old_disk_size = None
|
1511 |
|
1512 |
# We know that (as far as we can test) operations across different
|
1513 |
# nodes will succeed, time to run it for real on the backing storage
|
1514 |
for node_uuid in inst_nodes: |
1515 |
result = self.rpc.call_blockdev_grow(node_uuid,
|
1516 |
(self.disk, self.instance), |
1517 |
self.delta, False, True, |
1518 |
self.node_es_flags[node_uuid])
|
1519 |
result.Raise("Grow request failed to node %s" %
|
1520 |
self.cfg.GetNodeName(node_uuid))
|
1521 |
|
1522 |
# And now execute it for logical storage, on the primary node
|
1523 |
node_uuid = self.instance.primary_node
|
1524 |
result = self.rpc.call_blockdev_grow(node_uuid, (self.disk, self.instance), |
1525 |
self.delta, False, False, |
1526 |
self.node_es_flags[node_uuid])
|
1527 |
result.Raise("Grow request failed to node %s" %
|
1528 |
self.cfg.GetNodeName(node_uuid))
|
1529 |
|
1530 |
self.disk.RecordGrow(self.delta) |
1531 |
self.cfg.Update(self.instance, feedback_fn) |
1532 |
|
1533 |
# Changes have been recorded, release node lock
|
1534 |
ReleaseLocks(self, locking.LEVEL_NODE)
|
1535 |
|
1536 |
# Downgrade lock while waiting for sync
|
1537 |
self.glm.downgrade(locking.LEVEL_INSTANCE)
|
1538 |
|
1539 |
assert wipe_disks ^ (old_disk_size is None) |
1540 |
|
1541 |
if wipe_disks:
|
1542 |
assert self.instance.disks[self.op.disk] == self.disk |
1543 |
|
1544 |
# Wipe newly added disk space
|
1545 |
WipeDisks(self, self.instance, |
1546 |
disks=[(self.op.disk, self.disk, old_disk_size)]) |
1547 |
|
1548 |
if self.op.wait_for_sync: |
1549 |
disk_abort = not WaitForSync(self, self.instance, disks=[self.disk]) |
1550 |
if disk_abort:
|
1551 |
self.LogWarning("Disk syncing has not returned a good status; check" |
1552 |
" the instance")
|
1553 |
if not self.instance.disks_active: |
1554 |
_SafeShutdownInstanceDisks(self, self.instance, disks=[self.disk]) |
1555 |
elif not self.instance.disks_active: |
1556 |
self.LogWarning("Not shutting down the disk even if the instance is" |
1557 |
" not supposed to be running because no wait for"
|
1558 |
" sync mode was requested")
|
1559 |
|
1560 |
assert self.owned_locks(locking.LEVEL_NODE_RES) |
1561 |
assert set([self.instance.name]) == self.owned_locks(locking.LEVEL_INSTANCE) |
1562 |
|
1563 |
|
1564 |
class LUInstanceReplaceDisks(LogicalUnit): |
1565 |
"""Replace the disks of an instance.
|
1566 |
|
1567 |
"""
|
1568 |
HPATH = "mirrors-replace"
|
1569 |
HTYPE = constants.HTYPE_INSTANCE |
1570 |
REQ_BGL = False
|
1571 |
|
1572 |
def CheckArguments(self): |
1573 |
"""Check arguments.
|
1574 |
|
1575 |
"""
|
1576 |
if self.op.mode == constants.REPLACE_DISK_CHG: |
1577 |
if self.op.remote_node is None and self.op.iallocator is None: |
1578 |
raise errors.OpPrereqError("When changing the secondary either an" |
1579 |
" iallocator script must be used or the"
|
1580 |
" new node given", errors.ECODE_INVAL)
|
1581 |
else:
|
1582 |
CheckIAllocatorOrNode(self, "iallocator", "remote_node") |
1583 |
|
1584 |
elif self.op.remote_node is not None or self.op.iallocator is not None: |
1585 |
# Not replacing the secondary
|
1586 |
raise errors.OpPrereqError("The iallocator and new node options can" |
1587 |
" only be used when changing the"
|
1588 |
" secondary node", errors.ECODE_INVAL)
|
1589 |
|
1590 |
def ExpandNames(self): |
1591 |
self._ExpandAndLockInstance()
|
1592 |
|
1593 |
assert locking.LEVEL_NODE not in self.needed_locks |
1594 |
assert locking.LEVEL_NODE_RES not in self.needed_locks |
1595 |
assert locking.LEVEL_NODEGROUP not in self.needed_locks |
1596 |
|
1597 |
assert self.op.iallocator is None or self.op.remote_node is None, \ |
1598 |
"Conflicting options"
|
1599 |
|
1600 |
if self.op.remote_node is not None: |
1601 |
(self.op.remote_node_uuid, self.op.remote_node) = \ |
1602 |
ExpandNodeUuidAndName(self.cfg, self.op.remote_node_uuid, |
1603 |
self.op.remote_node)
|
1604 |
|
1605 |
# Warning: do not remove the locking of the new secondary here
|
1606 |
# unless DRBD8Dev.AddChildren is changed to work in parallel;
|
1607 |
# currently it doesn't since parallel invocations of
|
1608 |
# FindUnusedMinor will conflict
|
1609 |
self.needed_locks[locking.LEVEL_NODE] = [self.op.remote_node_uuid] |
1610 |
self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
|
1611 |
else:
|
1612 |
self.needed_locks[locking.LEVEL_NODE] = []
|
1613 |
self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
1614 |
|
1615 |
if self.op.iallocator is not None: |
1616 |
# iallocator will select a new node in the same group
|
1617 |
self.needed_locks[locking.LEVEL_NODEGROUP] = []
|
1618 |
self.needed_locks[locking.LEVEL_NODE_ALLOC] = locking.ALL_SET
|
1619 |
|
1620 |
self.needed_locks[locking.LEVEL_NODE_RES] = []
|
1621 |
|
1622 |
self.replacer = TLReplaceDisks(self, self.op.instance_uuid, |
1623 |
self.op.instance_name, self.op.mode, |
1624 |
self.op.iallocator, self.op.remote_node_uuid, |
1625 |
self.op.disks, self.op.early_release, |
1626 |
self.op.ignore_ipolicy)
|
1627 |
|
1628 |
self.tasklets = [self.replacer] |
1629 |
|
1630 |
def DeclareLocks(self, level): |
1631 |
if level == locking.LEVEL_NODEGROUP:
|
1632 |
assert self.op.remote_node_uuid is None |
1633 |
assert self.op.iallocator is not None |
1634 |
assert not self.needed_locks[locking.LEVEL_NODEGROUP] |
1635 |
|
1636 |
self.share_locks[locking.LEVEL_NODEGROUP] = 1 |
1637 |
# Lock all groups used by instance optimistically; this requires going
|
1638 |
# via the node before it's locked, requiring verification later on
|
1639 |
self.needed_locks[locking.LEVEL_NODEGROUP] = \
|
1640 |
self.cfg.GetInstanceNodeGroups(self.op.instance_uuid) |
1641 |
|
1642 |
elif level == locking.LEVEL_NODE:
|
1643 |
if self.op.iallocator is not None: |
1644 |
assert self.op.remote_node_uuid is None |
1645 |
assert not self.needed_locks[locking.LEVEL_NODE] |
1646 |
assert locking.NAL in self.owned_locks(locking.LEVEL_NODE_ALLOC) |
1647 |
|
1648 |
# Lock member nodes of all locked groups
|
1649 |
self.needed_locks[locking.LEVEL_NODE] = \
|
1650 |
[node_uuid |
1651 |
for group_uuid in self.owned_locks(locking.LEVEL_NODEGROUP) |
1652 |
for node_uuid in self.cfg.GetNodeGroup(group_uuid).members] |
1653 |
else:
|
1654 |
assert not self.glm.is_owned(locking.LEVEL_NODE_ALLOC) |
1655 |
|
1656 |
self._LockInstancesNodes()
|
1657 |
|
1658 |
elif level == locking.LEVEL_NODE_RES:
|
1659 |
# Reuse node locks
|
1660 |
self.needed_locks[locking.LEVEL_NODE_RES] = \
|
1661 |
self.needed_locks[locking.LEVEL_NODE]
|
1662 |
|
1663 |
def BuildHooksEnv(self): |
1664 |
"""Build hooks env.
|
1665 |
|
1666 |
This runs on the master, the primary and all the secondaries.
|
1667 |
|
1668 |
"""
|
1669 |
instance = self.replacer.instance
|
1670 |
secondary_nodes = self.cfg.GetInstanceSecondaryNodes(instance)
|
1671 |
env = { |
1672 |
"MODE": self.op.mode, |
1673 |
"NEW_SECONDARY": self.op.remote_node, |
1674 |
"OLD_SECONDARY": self.cfg.GetNodeName(secondary_nodes[0]), |
1675 |
} |
1676 |
env.update(BuildInstanceHookEnvByObject(self, instance))
|
1677 |
return env
|
1678 |
|
1679 |
def BuildHooksNodes(self): |
1680 |
"""Build hooks nodes.
|
1681 |
|
1682 |
"""
|
1683 |
instance = self.replacer.instance
|
1684 |
nl = [ |
1685 |
self.cfg.GetMasterNode(),
|
1686 |
instance.primary_node, |
1687 |
] |
1688 |
if self.op.remote_node_uuid is not None: |
1689 |
nl.append(self.op.remote_node_uuid)
|
1690 |
return nl, nl
|
1691 |
|
1692 |
def CheckPrereq(self): |
1693 |
"""Check prerequisites.
|
1694 |
|
1695 |
"""
|
1696 |
assert (self.glm.is_owned(locking.LEVEL_NODEGROUP) or |
1697 |
self.op.iallocator is None) |
1698 |
|
1699 |
# Verify if node group locks are still correct
|
1700 |
owned_groups = self.owned_locks(locking.LEVEL_NODEGROUP)
|
1701 |
if owned_groups:
|
1702 |
CheckInstanceNodeGroups(self.cfg, self.op.instance_uuid, owned_groups) |
1703 |
|
1704 |
return LogicalUnit.CheckPrereq(self) |
1705 |
|
1706 |
|
1707 |
class LUInstanceActivateDisks(NoHooksLU): |
1708 |
"""Bring up an instance's disks.
|
1709 |
|
1710 |
"""
|
1711 |
REQ_BGL = False
|
1712 |
|
1713 |
def ExpandNames(self): |
1714 |
self._ExpandAndLockInstance()
|
1715 |
self.needed_locks[locking.LEVEL_NODE] = []
|
1716 |
self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
1717 |
|
1718 |
def DeclareLocks(self, level): |
1719 |
if level == locking.LEVEL_NODE:
|
1720 |
self._LockInstancesNodes()
|
1721 |
|
1722 |
def CheckPrereq(self): |
1723 |
"""Check prerequisites.
|
1724 |
|
1725 |
This checks that the instance is in the cluster.
|
1726 |
|
1727 |
"""
|
1728 |
self.instance = self.cfg.GetInstanceInfo(self.op.instance_uuid) |
1729 |
assert self.instance is not None, \ |
1730 |
"Cannot retrieve locked instance %s" % self.op.instance_name |
1731 |
CheckNodeOnline(self, self.instance.primary_node) |
1732 |
|
1733 |
def Exec(self, feedback_fn): |
1734 |
"""Activate the disks.
|
1735 |
|
1736 |
"""
|
1737 |
disks_ok, disks_info = \ |
1738 |
AssembleInstanceDisks(self, self.instance, |
1739 |
ignore_size=self.op.ignore_size)
|
1740 |
if not disks_ok: |
1741 |
raise errors.OpExecError("Cannot activate block devices") |
1742 |
|
1743 |
if self.op.wait_for_sync: |
1744 |
if not WaitForSync(self, self.instance): |
1745 |
self.cfg.MarkInstanceDisksInactive(self.instance.uuid) |
1746 |
raise errors.OpExecError("Some disks of the instance are degraded!") |
1747 |
|
1748 |
return disks_info
|
1749 |
|
1750 |
|
1751 |
class LUInstanceDeactivateDisks(NoHooksLU): |
1752 |
"""Shutdown an instance's disks.
|
1753 |
|
1754 |
"""
|
1755 |
REQ_BGL = False
|
1756 |
|
1757 |
def ExpandNames(self): |
1758 |
self._ExpandAndLockInstance()
|
1759 |
self.needed_locks[locking.LEVEL_NODE] = []
|
1760 |
self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
|
1761 |
|
1762 |
def DeclareLocks(self, level): |
1763 |
if level == locking.LEVEL_NODE:
|
1764 |
self._LockInstancesNodes()
|
1765 |
|
1766 |
def CheckPrereq(self): |
1767 |
"""Check prerequisites.
|
1768 |
|
1769 |
This checks that the instance is in the cluster.
|
1770 |
|
1771 |
"""
|
1772 |
self.instance = self.cfg.GetInstanceInfo(self.op.instance_uuid) |
1773 |
assert self.instance is not None, \ |
1774 |
"Cannot retrieve locked instance %s" % self.op.instance_name |
1775 |
|
1776 |
def Exec(self, feedback_fn): |
1777 |
"""Deactivate the disks
|
1778 |
|
1779 |
"""
|
1780 |
if self.op.force: |
1781 |
ShutdownInstanceDisks(self, self.instance) |
1782 |
else:
|
1783 |
_SafeShutdownInstanceDisks(self, self.instance) |
1784 |
|
1785 |
|
1786 |
def _CheckDiskConsistencyInner(lu, instance, dev, node_uuid, on_primary, |
1787 |
ldisk=False):
|
1788 |
"""Check that mirrors are not degraded.
|
1789 |
|
1790 |
@attention: The device has to be annotated already.
|
1791 |
|
1792 |
The ldisk parameter, if True, will change the test from the
|
1793 |
is_degraded attribute (which represents overall non-ok status for
|
1794 |
the device(s)) to the ldisk (representing the local storage status).
|
1795 |
|
1796 |
"""
|
1797 |
result = True
|
1798 |
|
1799 |
if on_primary or dev.AssembleOnSecondary(): |
1800 |
rstats = lu.rpc.call_blockdev_find(node_uuid, (dev, instance)) |
1801 |
msg = rstats.fail_msg |
1802 |
if msg:
|
1803 |
lu.LogWarning("Can't find disk on node %s: %s",
|
1804 |
lu.cfg.GetNodeName(node_uuid), msg) |
1805 |
result = False
|
1806 |
elif not rstats.payload: |
1807 |
lu.LogWarning("Can't find disk on node %s", lu.cfg.GetNodeName(node_uuid))
|
1808 |
result = False
|
1809 |
else:
|
1810 |
if ldisk:
|
1811 |
result = result and rstats.payload.ldisk_status == constants.LDS_OKAY
|
1812 |
else:
|
1813 |
result = result and not rstats.payload.is_degraded |
1814 |
|
1815 |
if dev.children:
|
1816 |
for child in dev.children: |
1817 |
result = result and _CheckDiskConsistencyInner(lu, instance, child,
|
1818 |
node_uuid, on_primary) |
1819 |
|
1820 |
return result
|
1821 |
|
1822 |
|
1823 |
def CheckDiskConsistency(lu, instance, dev, node_uuid, on_primary, ldisk=False): |
1824 |
"""Wrapper around L{_CheckDiskConsistencyInner}.
|
1825 |
|
1826 |
"""
|
1827 |
(disk,) = AnnotateDiskParams(instance, [dev], lu.cfg) |
1828 |
return _CheckDiskConsistencyInner(lu, instance, disk, node_uuid, on_primary,
|
1829 |
ldisk=ldisk) |
1830 |
|
1831 |
|
1832 |
def _BlockdevFind(lu, node_uuid, dev, instance): |
1833 |
"""Wrapper around call_blockdev_find to annotate diskparams.
|
1834 |
|
1835 |
@param lu: A reference to the lu object
|
1836 |
@param node_uuid: The node to call out
|
1837 |
@param dev: The device to find
|
1838 |
@param instance: The instance object the device belongs to
|
1839 |
@returns The result of the rpc call
|
1840 |
|
1841 |
"""
|
1842 |
(disk,) = AnnotateDiskParams(instance, [dev], lu.cfg) |
1843 |
return lu.rpc.call_blockdev_find(node_uuid, (disk, instance))
|
1844 |
|
1845 |
|
1846 |
def _GenerateUniqueNames(lu, exts): |
1847 |
"""Generate a suitable LV name.
|
1848 |
|
1849 |
This will generate a logical volume name for the given instance.
|
1850 |
|
1851 |
"""
|
1852 |
results = [] |
1853 |
for val in exts: |
1854 |
new_id = lu.cfg.GenerateUniqueID(lu.proc.GetECId()) |
1855 |
results.append("%s%s" % (new_id, val))
|
1856 |
return results
|
1857 |
|
1858 |
|
1859 |
class TLReplaceDisks(Tasklet): |
1860 |
"""Replaces disks for an instance.
|
1861 |
|
1862 |
Note: Locking is not within the scope of this class.
|
1863 |
|
1864 |
"""
|
1865 |
def __init__(self, lu, instance_uuid, instance_name, mode, iallocator_name, |
1866 |
remote_node_uuid, disks, early_release, ignore_ipolicy): |
1867 |
"""Initializes this class.
|
1868 |
|
1869 |
"""
|
1870 |
Tasklet.__init__(self, lu)
|
1871 |
|
1872 |
# Parameters
|
1873 |
self.instance_uuid = instance_uuid
|
1874 |
self.instance_name = instance_name
|
1875 |
self.mode = mode
|
1876 |
self.iallocator_name = iallocator_name
|
1877 |
self.remote_node_uuid = remote_node_uuid
|
1878 |
self.disks = disks
|
1879 |
self.early_release = early_release
|
1880 |
self.ignore_ipolicy = ignore_ipolicy
|
1881 |
|
1882 |
# Runtime data
|
1883 |
self.instance = None |
1884 |
self.new_node_uuid = None |
1885 |
self.target_node_uuid = None |
1886 |
self.other_node_uuid = None |
1887 |
self.remote_node_info = None |
1888 |
self.node_secondary_ip = None |
1889 |
|
1890 |
@staticmethod
|
1891 |
def _RunAllocator(lu, iallocator_name, instance_uuid, |
1892 |
relocate_from_node_uuids): |
1893 |
"""Compute a new secondary node using an IAllocator.
|
1894 |
|
1895 |
"""
|
1896 |
req = iallocator.IAReqRelocate( |
1897 |
inst_uuid=instance_uuid, |
1898 |
relocate_from_node_uuids=list(relocate_from_node_uuids))
|
1899 |
ial = iallocator.IAllocator(lu.cfg, lu.rpc, req) |
1900 |
|
1901 |
ial.Run(iallocator_name) |
1902 |
|
1903 |
if not ial.success: |
1904 |
raise errors.OpPrereqError("Can't compute nodes using iallocator '%s':" |
1905 |
" %s" % (iallocator_name, ial.info),
|
1906 |
errors.ECODE_NORES) |
1907 |
|
1908 |
remote_node_name = ial.result[0]
|
1909 |
remote_node = lu.cfg.GetNodeInfoByName(remote_node_name) |
1910 |
|
1911 |
if remote_node is None: |
1912 |
raise errors.OpPrereqError("Node %s not found in configuration" % |
1913 |
remote_node_name, errors.ECODE_NOENT) |
1914 |
|
1915 |
lu.LogInfo("Selected new secondary for instance '%s': %s",
|
1916 |
instance_uuid, remote_node_name) |
1917 |
|
1918 |
return remote_node.uuid
|
1919 |
|
1920 |
def _FindFaultyDisks(self, node_uuid): |
1921 |
"""Wrapper for L{FindFaultyInstanceDisks}.
|
1922 |
|
1923 |
"""
|
1924 |
return FindFaultyInstanceDisks(self.cfg, self.rpc, self.instance, |
1925 |
node_uuid, True)
|
1926 |
|
1927 |
def _CheckDisksActivated(self, instance): |
1928 |
"""Checks if the instance disks are activated.
|
1929 |
|
1930 |
@param instance: The instance to check disks
|
1931 |
@return: True if they are activated, False otherwise
|
1932 |
|
1933 |
"""
|
1934 |
node_uuids = self.cfg.GetInstanceNodes(instance)
|
1935 |
|
1936 |
for idx, dev in enumerate(instance.disks): |
1937 |
for node_uuid in node_uuids: |
1938 |
self.lu.LogInfo("Checking disk/%d on %s", idx, |
1939 |
self.cfg.GetNodeName(node_uuid))
|
1940 |
|
1941 |
result = _BlockdevFind(self, node_uuid, dev, instance)
|
1942 |
|
1943 |
if result.offline:
|
1944 |
continue
|
1945 |
elif result.fail_msg or not result.payload: |
1946 |
return False |
1947 |
|
1948 |
return True |
1949 |
|
1950 |
def CheckPrereq(self): |
1951 |
"""Check prerequisites.
|
1952 |
|
1953 |
This checks that the instance is in the cluster.
|
1954 |
|
1955 |
"""
|
1956 |
self.instance = self.cfg.GetInstanceInfo(self.instance_uuid) |
1957 |
assert self.instance is not None, \ |
1958 |
"Cannot retrieve locked instance %s" % self.instance_name |
1959 |
|
1960 |
if self.instance.disk_template != constants.DT_DRBD8: |
1961 |
raise errors.OpPrereqError("Can only run replace disks for DRBD8-based" |
1962 |
" instances", errors.ECODE_INVAL)
|
1963 |
|
1964 |
secondary_nodes = self.cfg.GetInstanceSeconaryNodes(self.instance) |
1965 |
if len(secondary_nodes) != 1: |
1966 |
raise errors.OpPrereqError("The instance has a strange layout," |
1967 |
" expected one secondary but found %d" %
|
1968 |
len(secondary_nodes),
|
1969 |
errors.ECODE_FAULT) |
1970 |
|
1971 |
secondary_node_uuid = secondary_nodes[0]
|
1972 |
|
1973 |
if self.iallocator_name is None: |
1974 |
remote_node_uuid = self.remote_node_uuid
|
1975 |
else:
|
1976 |
remote_node_uuid = self._RunAllocator(self.lu, self.iallocator_name, |
1977 |
self.instance.uuid,
|
1978 |
secondary_nodes) |
1979 |
|
1980 |
if remote_node_uuid is None: |
1981 |
self.remote_node_info = None |
1982 |
else:
|
1983 |
assert remote_node_uuid in self.lu.owned_locks(locking.LEVEL_NODE), \ |
1984 |
"Remote node '%s' is not locked" % remote_node_uuid
|
1985 |
|
1986 |
self.remote_node_info = self.cfg.GetNodeInfo(remote_node_uuid) |
1987 |
assert self.remote_node_info is not None, \ |
1988 |
"Cannot retrieve locked node %s" % remote_node_uuid
|
1989 |
|
1990 |
if remote_node_uuid == self.instance.primary_node: |
1991 |
raise errors.OpPrereqError("The specified node is the primary node of" |
1992 |
" the instance", errors.ECODE_INVAL)
|
1993 |
|
1994 |
if remote_node_uuid == secondary_node_uuid:
|
1995 |
raise errors.OpPrereqError("The specified node is already the" |
1996 |
" secondary node of the instance",
|
1997 |
errors.ECODE_INVAL) |
1998 |
|
1999 |
if self.disks and self.mode in (constants.REPLACE_DISK_AUTO, |
2000 |
constants.REPLACE_DISK_CHG): |
2001 |
raise errors.OpPrereqError("Cannot specify disks to be replaced", |
2002 |
errors.ECODE_INVAL) |
2003 |
|
2004 |
if self.mode == constants.REPLACE_DISK_AUTO: |
2005 |
if not self._CheckDisksActivated(self.instance): |
2006 |
raise errors.OpPrereqError("Please run activate-disks on instance %s" |
2007 |
" first" % self.instance_name, |
2008 |
errors.ECODE_STATE) |
2009 |
faulty_primary = self._FindFaultyDisks(self.instance.primary_node) |
2010 |
faulty_secondary = self._FindFaultyDisks(secondary_node_uuid)
|
2011 |
|
2012 |
if faulty_primary and faulty_secondary: |
2013 |
raise errors.OpPrereqError("Instance %s has faulty disks on more than" |
2014 |
" one node and can not be repaired"
|
2015 |
" automatically" % self.instance_name, |
2016 |
errors.ECODE_STATE) |
2017 |
|
2018 |
if faulty_primary:
|
2019 |
self.disks = faulty_primary
|
2020 |
self.target_node_uuid = self.instance.primary_node |
2021 |
self.other_node_uuid = secondary_node_uuid
|
2022 |
check_nodes = [self.target_node_uuid, self.other_node_uuid] |
2023 |
elif faulty_secondary:
|
2024 |
self.disks = faulty_secondary
|
2025 |
self.target_node_uuid = secondary_node_uuid
|
2026 |
self.other_node_uuid = self.instance.primary_node |
2027 |
check_nodes = [self.target_node_uuid, self.other_node_uuid] |
2028 |
else:
|
2029 |
self.disks = []
|
2030 |
check_nodes = [] |
2031 |
|
2032 |
else:
|
2033 |
# Non-automatic modes
|
2034 |
if self.mode == constants.REPLACE_DISK_PRI: |
2035 |
self.target_node_uuid = self.instance.primary_node |
2036 |
self.other_node_uuid = secondary_node_uuid
|
2037 |
check_nodes = [self.target_node_uuid, self.other_node_uuid] |
2038 |
|
2039 |
elif self.mode == constants.REPLACE_DISK_SEC: |
2040 |
self.target_node_uuid = secondary_node_uuid
|
2041 |
self.other_node_uuid = self.instance.primary_node |
2042 |
check_nodes = [self.target_node_uuid, self.other_node_uuid] |
2043 |
|
2044 |
elif self.mode == constants.REPLACE_DISK_CHG: |
2045 |
self.new_node_uuid = remote_node_uuid
|
2046 |
self.other_node_uuid = self.instance.primary_node |
2047 |
self.target_node_uuid = secondary_node_uuid
|
2048 |
check_nodes = [self.new_node_uuid, self.other_node_uuid] |
2049 |
|
2050 |
CheckNodeNotDrained(self.lu, remote_node_uuid)
|
2051 |
CheckNodeVmCapable(self.lu, remote_node_uuid)
|
2052 |
|
2053 |
old_node_info = self.cfg.GetNodeInfo(secondary_node_uuid)
|
2054 |
assert old_node_info is not None |
2055 |
if old_node_info.offline and not self.early_release: |
2056 |
# doesn't make sense to delay the release
|
2057 |
self.early_release = True |
2058 |
self.lu.LogInfo("Old secondary %s is offline, automatically enabling" |
2059 |
" early-release mode", secondary_node_uuid)
|
2060 |
|
2061 |
else:
|
2062 |
raise errors.ProgrammerError("Unhandled disk replace mode (%s)" % |
2063 |
self.mode)
|
2064 |
|
2065 |
# If not specified all disks should be replaced
|
2066 |
if not self.disks: |
2067 |
self.disks = range(len(self.instance.disks)) |
2068 |
|
2069 |
# TODO: This is ugly, but right now we can't distinguish between internal
|
2070 |
# submitted opcode and external one. We should fix that.
|
2071 |
if self.remote_node_info: |
2072 |
# We change the node, lets verify it still meets instance policy
|
2073 |
new_group_info = self.cfg.GetNodeGroup(self.remote_node_info.group) |
2074 |
cluster = self.cfg.GetClusterInfo()
|
2075 |
ipolicy = ganeti.masterd.instance.CalculateGroupIPolicy(cluster, |
2076 |
new_group_info) |
2077 |
CheckTargetNodeIPolicy(self, ipolicy, self.instance, |
2078 |
self.remote_node_info, self.cfg, |
2079 |
ignore=self.ignore_ipolicy)
|
2080 |
|
2081 |
for node_uuid in check_nodes: |
2082 |
CheckNodeOnline(self.lu, node_uuid)
|
2083 |
|
2084 |
touched_nodes = frozenset(node_uuid for node_uuid in [self.new_node_uuid, |
2085 |
self.other_node_uuid,
|
2086 |
self.target_node_uuid]
|
2087 |
if node_uuid is not None) |
2088 |
|
2089 |
# Release unneeded node and node resource locks
|
2090 |
ReleaseLocks(self.lu, locking.LEVEL_NODE, keep=touched_nodes)
|
2091 |
ReleaseLocks(self.lu, locking.LEVEL_NODE_RES, keep=touched_nodes)
|
2092 |
ReleaseLocks(self.lu, locking.LEVEL_NODE_ALLOC)
|
2093 |
|
2094 |
# Release any owned node group
|
2095 |
ReleaseLocks(self.lu, locking.LEVEL_NODEGROUP)
|
2096 |
|
2097 |
# Check whether disks are valid
|
2098 |
for disk_idx in self.disks: |
2099 |
self.instance.FindDisk(disk_idx)
|
2100 |
|
2101 |
# Get secondary node IP addresses
|
2102 |
self.node_secondary_ip = dict((uuid, node.secondary_ip) for (uuid, node) |
2103 |
in self.cfg.GetMultiNodeInfo(touched_nodes)) |
2104 |
|
2105 |
def Exec(self, feedback_fn): |
2106 |
"""Execute disk replacement.
|
2107 |
|
2108 |
This dispatches the disk replacement to the appropriate handler.
|
2109 |
|
2110 |
"""
|
2111 |
if __debug__:
|
2112 |
# Verify owned locks before starting operation
|
2113 |
owned_nodes = self.lu.owned_locks(locking.LEVEL_NODE)
|
2114 |
assert set(owned_nodes) == set(self.node_secondary_ip), \ |
2115 |
("Incorrect node locks, owning %s, expected %s" %
|
2116 |
(owned_nodes, self.node_secondary_ip.keys()))
|
2117 |
assert (self.lu.owned_locks(locking.LEVEL_NODE) == |
2118 |
self.lu.owned_locks(locking.LEVEL_NODE_RES))
|
2119 |
assert not self.lu.glm.is_owned(locking.LEVEL_NODE_ALLOC) |
2120 |
|
2121 |
owned_instances = self.lu.owned_locks(locking.LEVEL_INSTANCE)
|
2122 |
assert list(owned_instances) == [self.instance_name], \ |
2123 |
"Instance '%s' not locked" % self.instance_name |
2124 |
|
2125 |
assert not self.lu.glm.is_owned(locking.LEVEL_NODEGROUP), \ |
2126 |
"Should not own any node group lock at this point"
|
2127 |
|
2128 |
if not self.disks: |
2129 |
feedback_fn("No disks need replacement for instance '%s'" %
|
2130 |
self.instance.name)
|
2131 |
return
|
2132 |
|
2133 |
feedback_fn("Replacing disk(s) %s for instance '%s'" %
|
2134 |
(utils.CommaJoin(self.disks), self.instance.name)) |
2135 |
feedback_fn("Current primary node: %s" %
|
2136 |
self.cfg.GetNodeName(self.instance.primary_node)) |
2137 |
secondary_nodes = self.cfg.GetInstanceSecondaryNodes(self.instance) |
2138 |
feedback_fn("Current seconary node: %s" %
|
2139 |
utils.CommaJoin(self.cfg.GetNodeNames(secondary_nodes)))
|
2140 |
|
2141 |
activate_disks = not self.instance.disks_active |
2142 |
|
2143 |
# Activate the instance disks if we're replacing them on a down instance
|
2144 |
if activate_disks:
|
2145 |
StartInstanceDisks(self.lu, self.instance, True) |
2146 |
|
2147 |
try:
|
2148 |
# Should we replace the secondary node?
|
2149 |
if self.new_node_uuid is not None: |
2150 |
fn = self._ExecDrbd8Secondary
|
2151 |
else:
|
2152 |
fn = self._ExecDrbd8DiskOnly
|
2153 |
|
2154 |
result = fn(feedback_fn) |
2155 |
finally:
|
2156 |
# Deactivate the instance disks if we're replacing them on a
|
2157 |
# down instance
|
2158 |
if activate_disks:
|
2159 |
_SafeShutdownInstanceDisks(self.lu, self.instance) |
2160 |
|
2161 |
assert not self.lu.owned_locks(locking.LEVEL_NODE) |
2162 |
|
2163 |
if __debug__:
|
2164 |
# Verify owned locks
|
2165 |
owned_nodes = self.lu.owned_locks(locking.LEVEL_NODE_RES)
|
2166 |
nodes = frozenset(self.node_secondary_ip) |
2167 |
assert ((self.early_release and not owned_nodes) or |
2168 |
(not self.early_release and not (set(owned_nodes) - nodes))), \ |
2169 |
("Not owning the correct locks, early_release=%s, owned=%r,"
|
2170 |
" nodes=%r" % (self.early_release, owned_nodes, nodes)) |
2171 |
|
2172 |
return result
|
2173 |
|
2174 |
def _CheckVolumeGroup(self, node_uuids): |
2175 |
self.lu.LogInfo("Checking volume groups") |
2176 |
|
2177 |
vgname = self.cfg.GetVGName()
|
2178 |
|
2179 |
# Make sure volume group exists on all involved nodes
|
2180 |
results = self.rpc.call_vg_list(node_uuids)
|
2181 |
if not results: |
2182 |
raise errors.OpExecError("Can't list volume groups on the nodes") |
2183 |
|
2184 |
for node_uuid in node_uuids: |
2185 |
res = results[node_uuid] |
2186 |
res.Raise("Error checking node %s" % self.cfg.GetNodeName(node_uuid)) |
2187 |
if vgname not in res.payload: |
2188 |
raise errors.OpExecError("Volume group '%s' not found on node %s" % |
2189 |
(vgname, self.cfg.GetNodeName(node_uuid)))
|
2190 |
|
2191 |
def _CheckDisksExistence(self, node_uuids): |
2192 |
# Check disk existence
|
2193 |
for idx, dev in enumerate(self.instance.disks): |
2194 |
if idx not in self.disks: |
2195 |
continue
|
2196 |
|
2197 |
for node_uuid in node_uuids: |
2198 |
self.lu.LogInfo("Checking disk/%d on %s", idx, |
2199 |
self.cfg.GetNodeName(node_uuid))
|
2200 |
|
2201 |
result = _BlockdevFind(self, node_uuid, dev, self.instance) |
2202 |
|
2203 |
msg = result.fail_msg |
2204 |
if msg or not result.payload: |
2205 |
if not msg: |
2206 |
msg = "disk not found"
|
2207 |
if not self._CheckDisksActivated(self.instance): |
2208 |
extra_hint = ("\nDisks seem to be not properly activated. Try"
|
2209 |
" running activate-disks on the instance before"
|
2210 |
" using replace-disks.")
|
2211 |
else:
|
2212 |
extra_hint = ""
|
2213 |
raise errors.OpExecError("Can't find disk/%d on node %s: %s%s" % |
2214 |
(idx, self.cfg.GetNodeName(node_uuid), msg,
|
2215 |
extra_hint)) |
2216 |
|
2217 |
def _CheckDisksConsistency(self, node_uuid, on_primary, ldisk): |
2218 |
for idx, dev in enumerate(self.instance.disks): |
2219 |
if idx not in self.disks: |
2220 |
continue
|
2221 |
|
2222 |
self.lu.LogInfo("Checking disk/%d consistency on node %s" % |
2223 |
(idx, self.cfg.GetNodeName(node_uuid)))
|
2224 |
|
2225 |
if not CheckDiskConsistency(self.lu, self.instance, dev, node_uuid, |
2226 |
on_primary, ldisk=ldisk): |
2227 |
raise errors.OpExecError("Node %s has degraded storage, unsafe to" |
2228 |
" replace disks for instance %s" %
|
2229 |
(self.cfg.GetNodeName(node_uuid),
|
2230 |
self.instance.name))
|
2231 |
|
2232 |
def _CreateNewStorage(self, node_uuid): |
2233 |
"""Create new storage on the primary or secondary node.
|
2234 |
|
2235 |
This is only used for same-node replaces, not for changing the
|
2236 |
secondary node, hence we don't want to modify the existing disk.
|
2237 |
|
2238 |
"""
|
2239 |
iv_names = {} |
2240 |
|
2241 |
disks = AnnotateDiskParams(self.instance, self.instance.disks, self.cfg) |
2242 |
for idx, dev in enumerate(disks): |
2243 |
if idx not in self.disks: |
2244 |
continue
|
2245 |
|
2246 |
self.lu.LogInfo("Adding storage on %s for disk/%d", |
2247 |
self.cfg.GetNodeName(node_uuid), idx)
|
2248 |
|
2249 |
lv_names = [".disk%d_%s" % (idx, suffix) for suffix in ["data", "meta"]] |
2250 |
names = _GenerateUniqueNames(self.lu, lv_names)
|
2251 |
|
2252 |
(data_disk, meta_disk) = dev.children |
2253 |
vg_data = data_disk.logical_id[0]
|
2254 |
lv_data = objects.Disk(dev_type=constants.DT_PLAIN, size=dev.size, |
2255 |
logical_id=(vg_data, names[0]),
|
2256 |
params=data_disk.params) |
2257 |
vg_meta = meta_disk.logical_id[0]
|
2258 |
lv_meta = objects.Disk(dev_type=constants.DT_PLAIN, |
2259 |
size=constants.DRBD_META_SIZE, |
2260 |
logical_id=(vg_meta, names[1]),
|
2261 |
params=meta_disk.params) |
2262 |
|
2263 |
new_lvs = [lv_data, lv_meta] |
2264 |
old_lvs = [child.Copy() for child in dev.children] |
2265 |
iv_names[dev.iv_name] = (dev, old_lvs, new_lvs) |
2266 |
excl_stor = IsExclusiveStorageEnabledNodeUuid(self.lu.cfg, node_uuid)
|
2267 |
|
2268 |
# we pass force_create=True to force the LVM creation
|
2269 |
for new_lv in new_lvs: |
2270 |
try:
|
2271 |
_CreateBlockDevInner(self.lu, node_uuid, self.instance, new_lv, True, |
2272 |
GetInstanceInfoText(self.instance), False, |
2273 |
excl_stor) |
2274 |
except errors.DeviceCreationError, e:
|
2275 |
raise errors.OpExecError("Can't create block device: %s" % e.message) |
2276 |
|
2277 |
return iv_names
|
2278 |
|
2279 |
def _CheckDevices(self, node_uuid, iv_names): |
2280 |
for name, (dev, _, _) in iv_names.iteritems(): |
2281 |
result = _BlockdevFind(self, node_uuid, dev, self.instance) |
2282 |
|
2283 |
msg = result.fail_msg |
2284 |
if msg or not result.payload: |
2285 |
if not msg: |
2286 |
msg = "disk not found"
|
2287 |
raise errors.OpExecError("Can't find DRBD device %s: %s" % |
2288 |
(name, msg)) |
2289 |
|
2290 |
if result.payload.is_degraded:
|
2291 |
raise errors.OpExecError("DRBD device %s is degraded!" % name) |
2292 |
|
2293 |
def _RemoveOldStorage(self, node_uuid, iv_names): |
2294 |
for name, (_, old_lvs, _) in iv_names.iteritems(): |
2295 |
self.lu.LogInfo("Remove logical volumes for %s", name) |
2296 |
|
2297 |
for lv in old_lvs: |
2298 |
msg = self.rpc.call_blockdev_remove(node_uuid, (lv, self.instance)) \ |
2299 |
.fail_msg |
2300 |
if msg:
|
2301 |
self.lu.LogWarning("Can't remove old LV: %s", msg, |
2302 |
hint="remove unused LVs manually")
|
2303 |
|
2304 |
def _ExecDrbd8DiskOnly(self, feedback_fn): # pylint: disable=W0613 |
2305 |
"""Replace a disk on the primary or secondary for DRBD 8.
|
2306 |
|
2307 |
The algorithm for replace is quite complicated:
|
2308 |
|
2309 |
1. for each disk to be replaced:
|
2310 |
|
2311 |
1. create new LVs on the target node with unique names
|
2312 |
1. detach old LVs from the drbd device
|
2313 |
1. rename old LVs to name_replaced.<time_t>
|
2314 |
1. rename new LVs to old LVs
|
2315 |
1. attach the new LVs (with the old names now) to the drbd device
|
2316 |
|
2317 |
1. wait for sync across all devices
|
2318 |
|
2319 |
1. for each modified disk:
|
2320 |
|
2321 |
1. remove old LVs (which have the name name_replaces.<time_t>)
|
2322 |
|
2323 |
Failures are not very well handled.
|
2324 |
|
2325 |
"""
|
2326 |
steps_total = 6
|
2327 |
|
2328 |
# Step: check device activation
|
2329 |
self.lu.LogStep(1, steps_total, "Check device existence") |
2330 |
self._CheckDisksExistence([self.other_node_uuid, self.target_node_uuid]) |
2331 |
self._CheckVolumeGroup([self.target_node_uuid, self.other_node_uuid]) |
2332 |
|
2333 |
# Step: check other node consistency
|
2334 |
self.lu.LogStep(2, steps_total, "Check peer consistency") |
2335 |
self._CheckDisksConsistency(
|
2336 |
self.other_node_uuid, self.other_node_uuid == self.instance.primary_node, |
2337 |
False)
|
2338 |
|
2339 |
# Step: create new storage
|
2340 |
self.lu.LogStep(3, steps_total, "Allocate new storage") |
2341 |
iv_names = self._CreateNewStorage(self.target_node_uuid) |
2342 |
|
2343 |
# Step: for each lv, detach+rename*2+attach
|
2344 |
self.lu.LogStep(4, steps_total, "Changing drbd configuration") |
2345 |
for dev, old_lvs, new_lvs in iv_names.itervalues(): |
2346 |
self.lu.LogInfo("Detaching %s drbd from local storage", dev.iv_name) |
2347 |
|
2348 |
result = self.rpc.call_blockdev_removechildren(self.target_node_uuid, |
2349 |
(dev, self.instance),
|
2350 |
(old_lvs, self.instance))
|
2351 |
result.Raise("Can't detach drbd from local storage on node"
|
2352 |
" %s for device %s" %
|
2353 |
(self.cfg.GetNodeName(self.target_node_uuid), dev.iv_name)) |
2354 |
#dev.children = []
|
2355 |
#cfg.Update(instance)
|
2356 |
|
2357 |
# ok, we created the new LVs, so now we know we have the needed
|
2358 |
# storage; as such, we proceed on the target node to rename
|
2359 |
# old_lv to _old, and new_lv to old_lv; note that we rename LVs
|
2360 |
# using the assumption that logical_id == unique_id on that node
|
2361 |
|
2362 |
# FIXME(iustin): use a better name for the replaced LVs
|
2363 |
temp_suffix = int(time.time())
|
2364 |
ren_fn = lambda d, suff: (d.logical_id[0], |
2365 |
d.logical_id[1] + "_replaced-%s" % suff) |
2366 |
|
2367 |
# Build the rename list based on what LVs exist on the node
|
2368 |
rename_old_to_new = [] |
2369 |
for to_ren in old_lvs: |
2370 |
result = self.rpc.call_blockdev_find(self.target_node_uuid, |
2371 |
(to_ren, self.instance))
|
2372 |
if not result.fail_msg and result.payload: |
2373 |
# device exists
|
2374 |
rename_old_to_new.append((to_ren, ren_fn(to_ren, temp_suffix))) |
2375 |
|
2376 |
self.lu.LogInfo("Renaming the old LVs on the target node") |
2377 |
result = self.rpc.call_blockdev_rename(self.target_node_uuid, |
2378 |
rename_old_to_new) |
2379 |
result.Raise("Can't rename old LVs on node %s" %
|
2380 |
self.cfg.GetNodeName(self.target_node_uuid)) |
2381 |
|
2382 |
# Now we rename the new LVs to the old LVs
|
2383 |
self.lu.LogInfo("Renaming the new LVs on the target node") |
2384 |
rename_new_to_old = [(new, old.logical_id) |
2385 |
for old, new in zip(old_lvs, new_lvs)] |
2386 |
result = self.rpc.call_blockdev_rename(self.target_node_uuid, |
2387 |
rename_new_to_old) |
2388 |
result.Raise("Can't rename new LVs on node %s" %
|
2389 |
self.cfg.GetNodeName(self.target_node_uuid)) |
2390 |
|
2391 |
# Intermediate steps of in memory modifications
|
2392 |
for old, new in zip(old_lvs, new_lvs): |
2393 |
new.logical_id = old.logical_id |
2394 |
|
2395 |
# We need to modify old_lvs so that removal later removes the
|
2396 |
# right LVs, not the newly added ones; note that old_lvs is a
|
2397 |
# copy here
|
2398 |
for disk in old_lvs: |
2399 |
disk.logical_id = ren_fn(disk, temp_suffix) |
2400 |
|
2401 |
# Now that the new lvs have the old name, we can add them to the device
|
2402 |
self.lu.LogInfo("Adding new mirror component on %s", |
2403 |
self.cfg.GetNodeName(self.target_node_uuid)) |
2404 |
result = self.rpc.call_blockdev_addchildren(self.target_node_uuid, |
2405 |
(dev, self.instance),
|
2406 |
(new_lvs, self.instance))
|
2407 |
msg = result.fail_msg |
2408 |
if msg:
|
2409 |
for new_lv in new_lvs: |
2410 |
msg2 = self.rpc.call_blockdev_remove(self.target_node_uuid, |
2411 |
(new_lv, self.instance)).fail_msg
|
2412 |
if msg2:
|
2413 |
self.lu.LogWarning("Can't rollback device %s: %s", dev, msg2, |
2414 |
hint=("cleanup manually the unused logical"
|
2415 |
"volumes"))
|
2416 |
raise errors.OpExecError("Can't add local storage to drbd: %s" % msg) |
2417 |
|
2418 |
cstep = itertools.count(5)
|
2419 |
|
2420 |
if self.early_release: |
2421 |
self.lu.LogStep(cstep.next(), steps_total, "Removing old storage") |
2422 |
self._RemoveOldStorage(self.target_node_uuid, iv_names) |
2423 |
# TODO: Check if releasing locks early still makes sense
|
2424 |
ReleaseLocks(self.lu, locking.LEVEL_NODE_RES)
|
2425 |
else:
|
2426 |
# Release all resource locks except those used by the instance
|
2427 |
ReleaseLocks(self.lu, locking.LEVEL_NODE_RES,
|
2428 |
keep=self.node_secondary_ip.keys())
|
2429 |
|
2430 |
# Release all node locks while waiting for sync
|
2431 |
ReleaseLocks(self.lu, locking.LEVEL_NODE)
|
2432 |
|
2433 |
# TODO: Can the instance lock be downgraded here? Take the optional disk
|
2434 |
# shutdown in the caller into consideration.
|
2435 |
|
2436 |
# Wait for sync
|
2437 |
# This can fail as the old devices are degraded and _WaitForSync
|
2438 |
# does a combined result over all disks, so we don't check its return value
|
2439 |
self.lu.LogStep(cstep.next(), steps_total, "Sync devices") |
2440 |
WaitForSync(self.lu, self.instance) |
2441 |
|
2442 |
# Check all devices manually
|
2443 |
self._CheckDevices(self.instance.primary_node, iv_names) |
2444 |
|
2445 |
# Step: remove old storage
|
2446 |
if not self.early_release: |
2447 |
self.lu.LogStep(cstep.next(), steps_total, "Removing old storage") |
2448 |
self._RemoveOldStorage(self.target_node_uuid, iv_names) |
2449 |
|
2450 |
def _ExecDrbd8Secondary(self, feedback_fn): |
2451 |
"""Replace the secondary node for DRBD 8.
|
2452 |
|
2453 |
The algorithm for replace is quite complicated:
|
2454 |
- for all disks of the instance:
|
2455 |
- create new LVs on the new node with same names
|
2456 |
- shutdown the drbd device on the old secondary
|
2457 |
- disconnect the drbd network on the primary
|
2458 |
- create the drbd device on the new secondary
|
2459 |
- network attach the drbd on the primary, using an artifice:
|
2460 |
the drbd code for Attach() will connect to the network if it
|
2461 |
finds a device which is connected to the good local disks but
|
2462 |
not network enabled
|
2463 |
- wait for sync across all devices
|
2464 |
- remove all disks from the old secondary
|
2465 |
|
2466 |
Failures are not very well handled.
|
2467 |
|
2468 |
"""
|
2469 |
steps_total = 6
|
2470 |
|
2471 |
pnode = self.instance.primary_node
|
2472 |
|
2473 |
# Step: check device activation
|
2474 |
self.lu.LogStep(1, steps_total, "Check device existence") |
2475 |
self._CheckDisksExistence([self.instance.primary_node]) |
2476 |
self._CheckVolumeGroup([self.instance.primary_node]) |
2477 |
|
2478 |
# Step: check other node consistency
|
2479 |
self.lu.LogStep(2, steps_total, "Check peer consistency") |
2480 |
self._CheckDisksConsistency(self.instance.primary_node, True, True) |
2481 |
|
2482 |
# Step: create new storage
|
2483 |
self.lu.LogStep(3, steps_total, "Allocate new storage") |
2484 |
disks = AnnotateDiskParams(self.instance, self.instance.disks, self.cfg) |
2485 |
excl_stor = IsExclusiveStorageEnabledNodeUuid(self.lu.cfg,
|
2486 |
self.new_node_uuid)
|
2487 |
for idx, dev in enumerate(disks): |
2488 |
self.lu.LogInfo("Adding new local storage on %s for disk/%d" % |
2489 |
(self.cfg.GetNodeName(self.new_node_uuid), idx)) |
2490 |
# we pass force_create=True to force LVM creation
|
2491 |
for new_lv in dev.children: |
2492 |
try:
|
2493 |
_CreateBlockDevInner(self.lu, self.new_node_uuid, self.instance, |
2494 |
new_lv, True, GetInstanceInfoText(self.instance), |
2495 |
False, excl_stor)
|
2496 |
except errors.DeviceCreationError, e:
|
2497 |
raise errors.OpExecError("Can't create block device: %s" % e.message) |
2498 |
|
2499 |
# Step 4: dbrd minors and drbd setups changes
|
2500 |
# after this, we must manually remove the drbd minors on both the
|
2501 |
# error and the success paths
|
2502 |
self.lu.LogStep(4, steps_total, "Changing drbd configuration") |
2503 |
minors = self.cfg.AllocateDRBDMinor([self.new_node_uuid |
2504 |
for _ in self.instance.disks], |
2505 |
self.instance.uuid)
|
2506 |
logging.debug("Allocated minors %r", minors)
|
2507 |
|
2508 |
iv_names = {} |
2509 |
for idx, (dev, new_minor) in enumerate(zip(self.instance.disks, minors)): |
2510 |
self.lu.LogInfo("activating a new drbd on %s for disk/%d" % |
2511 |
(self.cfg.GetNodeName(self.new_node_uuid), idx)) |
2512 |
# create new devices on new_node; note that we create two IDs:
|
2513 |
# one without port, so the drbd will be activated without
|
2514 |
# networking information on the new node at this stage, and one
|
2515 |
# with network, for the latter activation in step 4
|
2516 |
(o_node1, o_node2, o_port, o_minor1, o_minor2, o_secret) = dev.logical_id |
2517 |
if self.instance.primary_node == o_node1: |
2518 |
p_minor = o_minor1 |
2519 |
else:
|
2520 |
assert self.instance.primary_node == o_node2, "Three-node instance?" |
2521 |
p_minor = o_minor2 |
2522 |
|
2523 |
new_alone_id = (self.instance.primary_node, self.new_node_uuid, None, |
2524 |
p_minor, new_minor, o_secret) |
2525 |
new_net_id = (self.instance.primary_node, self.new_node_uuid, o_port, |
2526 |
p_minor, new_minor, o_secret) |
2527 |
|
2528 |
iv_names[idx] = (dev, dev.children, new_net_id) |
2529 |
logging.debug("Allocated new_minor: %s, new_logical_id: %s", new_minor,
|
2530 |
new_net_id) |
2531 |
new_drbd = objects.Disk(dev_type=constants.DT_DRBD8, |
2532 |
logical_id=new_alone_id, |
2533 |
children=dev.children, |
2534 |
size=dev.size, |
2535 |
params={}) |
2536 |
(anno_new_drbd,) = AnnotateDiskParams(self.instance, [new_drbd],
|
2537 |
self.cfg)
|
2538 |
try:
|
2539 |
CreateSingleBlockDev(self.lu, self.new_node_uuid, self.instance, |
2540 |
anno_new_drbd, |
2541 |
GetInstanceInfoText(self.instance), False, |
2542 |
excl_stor) |
2543 |
except errors.GenericError:
|
2544 |
self.cfg.ReleaseDRBDMinors(self.instance.uuid) |
2545 |
raise
|
2546 |
|
2547 |
# We have new devices, shutdown the drbd on the old secondary
|
2548 |
for idx, dev in enumerate(self.instance.disks): |
2549 |
self.lu.LogInfo("Shutting down drbd for disk/%d on old node", idx) |
2550 |
msg = self.rpc.call_blockdev_shutdown(self.target_node_uuid, |
2551 |
(dev, self.instance)).fail_msg
|
2552 |
if msg:
|
2553 |
self.lu.LogWarning("Failed to shutdown drbd for disk/%d on old" |
2554 |
"node: %s" % (idx, msg),
|
2555 |
hint=("Please cleanup this device manually as"
|
2556 |
" soon as possible"))
|
2557 |
|
2558 |
self.lu.LogInfo("Detaching primary drbds from the network (=> standalone)") |
2559 |
result = self.rpc.call_drbd_disconnect_net(
|
2560 |
[pnode], (self.instance.disks, self.instance))[pnode] |
2561 |
|
2562 |
msg = result.fail_msg |
2563 |
if msg:
|
2564 |
# detaches didn't succeed (unlikely)
|
2565 |
self.cfg.ReleaseDRBDMinors(self.instance.uuid) |
2566 |
raise errors.OpExecError("Can't detach the disks from the network on" |
2567 |
" old node: %s" % (msg,))
|
2568 |
|
2569 |
# if we managed to detach at least one, we update all the disks of
|
2570 |
# the instance to point to the new secondary
|
2571 |
self.lu.LogInfo("Updating instance configuration") |
2572 |
for dev, _, new_logical_id in iv_names.itervalues(): |
2573 |
dev.logical_id = new_logical_id |
2574 |
|
2575 |
self.cfg.Update(self.instance, feedback_fn) |
2576 |
|
2577 |
# Release all node locks (the configuration has been updated)
|
2578 |
ReleaseLocks(self.lu, locking.LEVEL_NODE)
|
2579 |
|
2580 |
# and now perform the drbd attach
|
2581 |
self.lu.LogInfo("Attaching primary drbds to new secondary" |
2582 |
" (standalone => connected)")
|
2583 |
result = self.rpc.call_drbd_attach_net([self.instance.primary_node, |
2584 |
self.new_node_uuid],
|
2585 |
(self.instance.disks, self.instance), |
2586 |
self.instance.name,
|
2587 |
False)
|
2588 |
for to_node, to_result in result.items(): |
2589 |
msg = to_result.fail_msg |
2590 |
if msg:
|
2591 |
self.lu.LogWarning("Can't attach drbd disks on node %s: %s", |
2592 |
self.cfg.GetNodeName(to_node), msg,
|
2593 |
hint=("please do a gnt-instance info to see the"
|
2594 |
" status of disks"))
|
2595 |
|
2596 |
cstep = itertools.count(5)
|
2597 |
|
2598 |
if self.early_release: |
2599 |
self.lu.LogStep(cstep.next(), steps_total, "Removing old storage") |
2600 |
self._RemoveOldStorage(self.target_node_uuid, iv_names) |
2601 |
# TODO: Check if releasing locks early still makes sense
|
2602 |
ReleaseLocks(self.lu, locking.LEVEL_NODE_RES)
|
2603 |
else:
|
2604 |
# Release all resource locks except those used by the instance
|
2605 |
ReleaseLocks(self.lu, locking.LEVEL_NODE_RES,
|
2606 |
keep=self.node_secondary_ip.keys())
|
2607 |
|
2608 |
# TODO: Can the instance lock be downgraded here? Take the optional disk
|
2609 |
# shutdown in the caller into consideration.
|
2610 |
|
2611 |
# Wait for sync
|
2612 |
# This can fail as the old devices are degraded and _WaitForSync
|
2613 |
# does a combined result over all disks, so we don't check its return value
|
2614 |
self.lu.LogStep(cstep.next(), steps_total, "Sync devices") |
2615 |
WaitForSync(self.lu, self.instance) |
2616 |
|
2617 |
# Check all devices manually
|
2618 |
self._CheckDevices(self.instance.primary_node, iv_names) |
2619 |
|
2620 |
# Step: remove old storage
|
2621 |
if not self.early_release: |
2622 |
self.lu.LogStep(cstep.next(), steps_total, "Removing old storage") |
2623 |
self._RemoveOldStorage(self.target_node_uuid, iv_names) |