root / lib / bdev.py @ 23e3c9b7
History | View | Annotate | Download (90.5 kB)
1 | 2f31098c | Iustin Pop | #
|
---|---|---|---|
2 | a8083063 | Iustin Pop | #
|
3 | a8083063 | Iustin Pop | |
4 | 0304f0ec | Iustin Pop | # Copyright (C) 2006, 2007, 2010, 2011, 2012 Google Inc.
|
5 | a8083063 | Iustin Pop | #
|
6 | a8083063 | Iustin Pop | # This program is free software; you can redistribute it and/or modify
|
7 | a8083063 | Iustin Pop | # it under the terms of the GNU General Public License as published by
|
8 | a8083063 | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or
|
9 | a8083063 | Iustin Pop | # (at your option) any later version.
|
10 | a8083063 | Iustin Pop | #
|
11 | a8083063 | Iustin Pop | # This program is distributed in the hope that it will be useful, but
|
12 | a8083063 | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | a8083063 | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | a8083063 | Iustin Pop | # General Public License for more details.
|
15 | a8083063 | Iustin Pop | #
|
16 | a8083063 | Iustin Pop | # You should have received a copy of the GNU General Public License
|
17 | a8083063 | Iustin Pop | # along with this program; if not, write to the Free Software
|
18 | a8083063 | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | a8083063 | Iustin Pop | # 02110-1301, USA.
|
20 | a8083063 | Iustin Pop | |
21 | a8083063 | Iustin Pop | |
22 | a8083063 | Iustin Pop | """Block device abstraction"""
|
23 | a8083063 | Iustin Pop | |
24 | a8083063 | Iustin Pop | import re |
25 | a8083063 | Iustin Pop | import time |
26 | a8083063 | Iustin Pop | import errno |
27 | ad1dd4c7 | Andrea Spadaccini | import shlex |
28 | b6135bbc | Apollon Oikonomopoulos | import stat |
29 | a2cfdea2 | Iustin Pop | import pyparsing as pyp |
30 | 6f695a2e | Manuel Franceschini | import os |
31 | 468c5f77 | Iustin Pop | import logging |
32 | 23e3c9b7 | Michael Hanselmann | import itertools |
33 | a8083063 | Iustin Pop | |
34 | a8083063 | Iustin Pop | from ganeti import utils |
35 | a8083063 | Iustin Pop | from ganeti import errors |
36 | fe96220b | Iustin Pop | from ganeti import constants |
37 | 96acbc09 | Michael Hanselmann | from ganeti import objects |
38 | cea881e5 | Michael Hanselmann | from ganeti import compat |
39 | a744b676 | Manuel Franceschini | from ganeti import netutils |
40 | fbdac0d9 | Michael Hanselmann | from ganeti import pathutils |
41 | a8083063 | Iustin Pop | |
42 | a8083063 | Iustin Pop | |
43 | 310fbb64 | Iustin Pop | # Size of reads in _CanReadDevice
|
44 | 310fbb64 | Iustin Pop | _DEVICE_READ_SIZE = 128 * 1024 |
45 | 310fbb64 | Iustin Pop | |
46 | 310fbb64 | Iustin Pop | |
47 | 82463074 | Iustin Pop | def _IgnoreError(fn, *args, **kwargs): |
48 | 82463074 | Iustin Pop | """Executes the given function, ignoring BlockDeviceErrors.
|
49 | 82463074 | Iustin Pop |
|
50 | 82463074 | Iustin Pop | This is used in order to simplify the execution of cleanup or
|
51 | 82463074 | Iustin Pop | rollback functions.
|
52 | 82463074 | Iustin Pop |
|
53 | 82463074 | Iustin Pop | @rtype: boolean
|
54 | 82463074 | Iustin Pop | @return: True when fn didn't raise an exception, False otherwise
|
55 | 82463074 | Iustin Pop |
|
56 | 82463074 | Iustin Pop | """
|
57 | 82463074 | Iustin Pop | try:
|
58 | 82463074 | Iustin Pop | fn(*args, **kwargs) |
59 | 82463074 | Iustin Pop | return True |
60 | 82463074 | Iustin Pop | except errors.BlockDeviceError, err:
|
61 | 099c52ad | Iustin Pop | logging.warning("Caught BlockDeviceError but ignoring: %s", str(err)) |
62 | 82463074 | Iustin Pop | return False |
63 | 82463074 | Iustin Pop | |
64 | 82463074 | Iustin Pop | |
65 | 82463074 | Iustin Pop | def _ThrowError(msg, *args): |
66 | 82463074 | Iustin Pop | """Log an error to the node daemon and the raise an exception.
|
67 | 82463074 | Iustin Pop |
|
68 | 82463074 | Iustin Pop | @type msg: string
|
69 | 82463074 | Iustin Pop | @param msg: the text of the exception
|
70 | 82463074 | Iustin Pop | @raise errors.BlockDeviceError
|
71 | 82463074 | Iustin Pop |
|
72 | 82463074 | Iustin Pop | """
|
73 | 82463074 | Iustin Pop | if args:
|
74 | 82463074 | Iustin Pop | msg = msg % args |
75 | 82463074 | Iustin Pop | logging.error(msg) |
76 | 82463074 | Iustin Pop | raise errors.BlockDeviceError(msg)
|
77 | 82463074 | Iustin Pop | |
78 | 82463074 | Iustin Pop | |
79 | e398546b | Iustin Pop | def _CheckResult(result): |
80 | e398546b | Iustin Pop | """Throws an error if the given result is a failed one.
|
81 | e398546b | Iustin Pop |
|
82 | e398546b | Iustin Pop | @param result: result from RunCmd
|
83 | e398546b | Iustin Pop |
|
84 | e398546b | Iustin Pop | """
|
85 | e398546b | Iustin Pop | if result.failed:
|
86 | e398546b | Iustin Pop | _ThrowError("Command: %s error: %s - %s", result.cmd, result.fail_reason,
|
87 | e398546b | Iustin Pop | result.output) |
88 | e398546b | Iustin Pop | |
89 | e398546b | Iustin Pop | |
90 | 310fbb64 | Iustin Pop | def _CanReadDevice(path): |
91 | 310fbb64 | Iustin Pop | """Check if we can read from the given device.
|
92 | 310fbb64 | Iustin Pop |
|
93 | 310fbb64 | Iustin Pop | This tries to read the first 128k of the device.
|
94 | 310fbb64 | Iustin Pop |
|
95 | 310fbb64 | Iustin Pop | """
|
96 | 310fbb64 | Iustin Pop | try:
|
97 | 310fbb64 | Iustin Pop | utils.ReadFile(path, size=_DEVICE_READ_SIZE) |
98 | 310fbb64 | Iustin Pop | return True |
99 | 1122eb25 | Iustin Pop | except EnvironmentError: |
100 | 310fbb64 | Iustin Pop | logging.warning("Can't read from device %s", path, exc_info=True) |
101 | 310fbb64 | Iustin Pop | return False |
102 | 310fbb64 | Iustin Pop | |
103 | 310fbb64 | Iustin Pop | |
104 | 23e3c9b7 | Michael Hanselmann | def _GetForbiddenFileStoragePaths(): |
105 | 23e3c9b7 | Michael Hanselmann | """Builds a list of path prefixes which shouldn't be used for file storage.
|
106 | 23e3c9b7 | Michael Hanselmann |
|
107 | 23e3c9b7 | Michael Hanselmann | @rtype: frozenset
|
108 | 23e3c9b7 | Michael Hanselmann |
|
109 | 23e3c9b7 | Michael Hanselmann | """
|
110 | 23e3c9b7 | Michael Hanselmann | paths = set([
|
111 | 23e3c9b7 | Michael Hanselmann | "/boot",
|
112 | 23e3c9b7 | Michael Hanselmann | "/dev",
|
113 | 23e3c9b7 | Michael Hanselmann | "/etc",
|
114 | 23e3c9b7 | Michael Hanselmann | "/home",
|
115 | 23e3c9b7 | Michael Hanselmann | "/proc",
|
116 | 23e3c9b7 | Michael Hanselmann | "/root",
|
117 | 23e3c9b7 | Michael Hanselmann | "/sys",
|
118 | 23e3c9b7 | Michael Hanselmann | ]) |
119 | 23e3c9b7 | Michael Hanselmann | |
120 | 23e3c9b7 | Michael Hanselmann | for prefix in ["", "/usr", "/usr/local"]: |
121 | 23e3c9b7 | Michael Hanselmann | paths.update(map(lambda s: "%s/%s" % (prefix, s), |
122 | 23e3c9b7 | Michael Hanselmann | ["bin", "lib", "lib32", "lib64", "sbin"])) |
123 | 23e3c9b7 | Michael Hanselmann | |
124 | 23e3c9b7 | Michael Hanselmann | return frozenset(map(os.path.normpath, paths)) |
125 | 23e3c9b7 | Michael Hanselmann | |
126 | 23e3c9b7 | Michael Hanselmann | |
127 | 23e3c9b7 | Michael Hanselmann | def _ComputeWrongFileStoragePaths(paths, |
128 | 23e3c9b7 | Michael Hanselmann | _forbidden=_GetForbiddenFileStoragePaths()): |
129 | 23e3c9b7 | Michael Hanselmann | """Cross-checks a list of paths for prefixes considered bad.
|
130 | 23e3c9b7 | Michael Hanselmann |
|
131 | 23e3c9b7 | Michael Hanselmann | Some paths, e.g. "/bin", should not be used for file storage.
|
132 | 23e3c9b7 | Michael Hanselmann |
|
133 | 23e3c9b7 | Michael Hanselmann | @type paths: list
|
134 | 23e3c9b7 | Michael Hanselmann | @param paths: List of paths to be checked
|
135 | 23e3c9b7 | Michael Hanselmann | @rtype: list
|
136 | 23e3c9b7 | Michael Hanselmann | @return: Sorted list of paths for which the user should be warned
|
137 | 23e3c9b7 | Michael Hanselmann |
|
138 | 23e3c9b7 | Michael Hanselmann | """
|
139 | 23e3c9b7 | Michael Hanselmann | def _Check(path): |
140 | 23e3c9b7 | Michael Hanselmann | return (not os.path.isabs(path) or |
141 | 23e3c9b7 | Michael Hanselmann | path in _forbidden or |
142 | 23e3c9b7 | Michael Hanselmann | filter(lambda p: utils.IsBelowDir(p, path), _forbidden)) |
143 | 23e3c9b7 | Michael Hanselmann | |
144 | 23e3c9b7 | Michael Hanselmann | return utils.NiceSort(filter(_Check, map(os.path.normpath, paths))) |
145 | 23e3c9b7 | Michael Hanselmann | |
146 | 23e3c9b7 | Michael Hanselmann | |
147 | 23e3c9b7 | Michael Hanselmann | def ComputeWrongFileStoragePaths(_filename=pathutils.FILE_STORAGE_PATHS_FILE): |
148 | 23e3c9b7 | Michael Hanselmann | """Returns a list of file storage paths whose prefix is considered bad.
|
149 | 23e3c9b7 | Michael Hanselmann |
|
150 | 23e3c9b7 | Michael Hanselmann | See L{_ComputeWrongFileStoragePaths}.
|
151 | 23e3c9b7 | Michael Hanselmann |
|
152 | 23e3c9b7 | Michael Hanselmann | """
|
153 | 23e3c9b7 | Michael Hanselmann | return _ComputeWrongFileStoragePaths(_LoadAllowedFileStoragePaths(_filename))
|
154 | 23e3c9b7 | Michael Hanselmann | |
155 | 23e3c9b7 | Michael Hanselmann | |
156 | fbdac0d9 | Michael Hanselmann | def _CheckFileStoragePath(path, allowed): |
157 | fbdac0d9 | Michael Hanselmann | """Checks if a path is in a list of allowed paths for file storage.
|
158 | fbdac0d9 | Michael Hanselmann |
|
159 | fbdac0d9 | Michael Hanselmann | @type path: string
|
160 | fbdac0d9 | Michael Hanselmann | @param path: Path to check
|
161 | fbdac0d9 | Michael Hanselmann | @type allowed: list
|
162 | fbdac0d9 | Michael Hanselmann | @param allowed: List of allowed paths
|
163 | fbdac0d9 | Michael Hanselmann | @raise errors.FileStoragePathError: If the path is not allowed
|
164 | fbdac0d9 | Michael Hanselmann |
|
165 | fbdac0d9 | Michael Hanselmann | """
|
166 | fbdac0d9 | Michael Hanselmann | if not os.path.isabs(path): |
167 | fbdac0d9 | Michael Hanselmann | raise errors.FileStoragePathError("File storage path must be absolute," |
168 | fbdac0d9 | Michael Hanselmann | " got '%s'" % path)
|
169 | fbdac0d9 | Michael Hanselmann | |
170 | fbdac0d9 | Michael Hanselmann | for i in allowed: |
171 | fbdac0d9 | Michael Hanselmann | if not os.path.isabs(i): |
172 | fbdac0d9 | Michael Hanselmann | logging.info("Ignoring relative path '%s' for file storage", i)
|
173 | fbdac0d9 | Michael Hanselmann | continue
|
174 | fbdac0d9 | Michael Hanselmann | |
175 | fbdac0d9 | Michael Hanselmann | if utils.IsBelowDir(i, path):
|
176 | fbdac0d9 | Michael Hanselmann | break
|
177 | fbdac0d9 | Michael Hanselmann | else:
|
178 | fbdac0d9 | Michael Hanselmann | raise errors.FileStoragePathError("Path '%s' is not acceptable for file" |
179 | fbdac0d9 | Michael Hanselmann | " storage" % path)
|
180 | fbdac0d9 | Michael Hanselmann | |
181 | fbdac0d9 | Michael Hanselmann | |
182 | 23e3c9b7 | Michael Hanselmann | def _LoadAllowedFileStoragePaths(filename): |
183 | fbdac0d9 | Michael Hanselmann | """Loads file containing allowed file storage paths.
|
184 | fbdac0d9 | Michael Hanselmann |
|
185 | fbdac0d9 | Michael Hanselmann | @rtype: list
|
186 | fbdac0d9 | Michael Hanselmann | @return: List of allowed paths (can be an empty list)
|
187 | fbdac0d9 | Michael Hanselmann |
|
188 | fbdac0d9 | Michael Hanselmann | """
|
189 | fbdac0d9 | Michael Hanselmann | try:
|
190 | fbdac0d9 | Michael Hanselmann | contents = utils.ReadFile(filename) |
191 | fbdac0d9 | Michael Hanselmann | except EnvironmentError: |
192 | fbdac0d9 | Michael Hanselmann | return []
|
193 | fbdac0d9 | Michael Hanselmann | else:
|
194 | fbdac0d9 | Michael Hanselmann | return utils.FilterEmptyLinesAndComments(contents)
|
195 | fbdac0d9 | Michael Hanselmann | |
196 | fbdac0d9 | Michael Hanselmann | |
197 | fbdac0d9 | Michael Hanselmann | def CheckFileStoragePath(path, _filename=pathutils.FILE_STORAGE_PATHS_FILE): |
198 | fbdac0d9 | Michael Hanselmann | """Checks if a path is allowed for file storage.
|
199 | fbdac0d9 | Michael Hanselmann |
|
200 | fbdac0d9 | Michael Hanselmann | @type path: string
|
201 | fbdac0d9 | Michael Hanselmann | @param path: Path to check
|
202 | fbdac0d9 | Michael Hanselmann | @raise errors.FileStoragePathError: If the path is not allowed
|
203 | fbdac0d9 | Michael Hanselmann |
|
204 | fbdac0d9 | Michael Hanselmann | """
|
205 | 23e3c9b7 | Michael Hanselmann | allowed = _LoadAllowedFileStoragePaths(_filename) |
206 | 23e3c9b7 | Michael Hanselmann | |
207 | 23e3c9b7 | Michael Hanselmann | if _ComputeWrongFileStoragePaths([path]):
|
208 | 23e3c9b7 | Michael Hanselmann | raise errors.FileStoragePathError("Path '%s' uses a forbidden prefix" % |
209 | 23e3c9b7 | Michael Hanselmann | path) |
210 | 23e3c9b7 | Michael Hanselmann | |
211 | 23e3c9b7 | Michael Hanselmann | _CheckFileStoragePath(path, allowed) |
212 | fbdac0d9 | Michael Hanselmann | |
213 | fbdac0d9 | Michael Hanselmann | |
214 | a8083063 | Iustin Pop | class BlockDev(object): |
215 | a8083063 | Iustin Pop | """Block device abstract class.
|
216 | a8083063 | Iustin Pop |
|
217 | a8083063 | Iustin Pop | A block device can be in the following states:
|
218 | a8083063 | Iustin Pop | - not existing on the system, and by `Create()` it goes into:
|
219 | a8083063 | Iustin Pop | - existing but not setup/not active, and by `Assemble()` goes into:
|
220 | a8083063 | Iustin Pop | - active read-write and by `Open()` it goes into
|
221 | a8083063 | Iustin Pop | - online (=used, or ready for use)
|
222 | a8083063 | Iustin Pop |
|
223 | a8083063 | Iustin Pop | A device can also be online but read-only, however we are not using
|
224 | abdf0113 | Iustin Pop | the readonly state (LV has it, if needed in the future) and we are
|
225 | abdf0113 | Iustin Pop | usually looking at this like at a stack, so it's easier to
|
226 | abdf0113 | Iustin Pop | conceptualise the transition from not-existing to online and back
|
227 | a8083063 | Iustin Pop | like a linear one.
|
228 | a8083063 | Iustin Pop |
|
229 | a8083063 | Iustin Pop | The many different states of the device are due to the fact that we
|
230 | a8083063 | Iustin Pop | need to cover many device types:
|
231 | a8083063 | Iustin Pop | - logical volumes are created, lvchange -a y $lv, and used
|
232 | a8083063 | Iustin Pop | - drbd devices are attached to a local disk/remote peer and made primary
|
233 | a8083063 | Iustin Pop |
|
234 | a8083063 | Iustin Pop | A block device is identified by three items:
|
235 | a8083063 | Iustin Pop | - the /dev path of the device (dynamic)
|
236 | a8083063 | Iustin Pop | - a unique ID of the device (static)
|
237 | a8083063 | Iustin Pop | - it's major/minor pair (dynamic)
|
238 | a8083063 | Iustin Pop |
|
239 | a8083063 | Iustin Pop | Not all devices implement both the first two as distinct items. LVM
|
240 | a8083063 | Iustin Pop | logical volumes have their unique ID (the pair volume group, logical
|
241 | abdf0113 | Iustin Pop | volume name) in a 1-to-1 relation to the dev path. For DRBD devices,
|
242 | abdf0113 | Iustin Pop | the /dev path is again dynamic and the unique id is the pair (host1,
|
243 | abdf0113 | Iustin Pop | dev1), (host2, dev2).
|
244 | a8083063 | Iustin Pop |
|
245 | a8083063 | Iustin Pop | You can get to a device in two ways:
|
246 | a8083063 | Iustin Pop | - creating the (real) device, which returns you
|
247 | abdf0113 | Iustin Pop | an attached instance (lvcreate)
|
248 | a8083063 | Iustin Pop | - attaching of a python instance to an existing (real) device
|
249 | a8083063 | Iustin Pop |
|
250 | a8083063 | Iustin Pop | The second point, the attachement to a device, is different
|
251 | a8083063 | Iustin Pop | depending on whether the device is assembled or not. At init() time,
|
252 | a8083063 | Iustin Pop | we search for a device with the same unique_id as us. If found,
|
253 | a8083063 | Iustin Pop | good. It also means that the device is already assembled. If not,
|
254 | a8083063 | Iustin Pop | after assembly we'll have our correct major/minor.
|
255 | a8083063 | Iustin Pop |
|
256 | a8083063 | Iustin Pop | """
|
257 | 94dcbdb0 | Andrea Spadaccini | def __init__(self, unique_id, children, size, params): |
258 | a8083063 | Iustin Pop | self._children = children
|
259 | a8083063 | Iustin Pop | self.dev_path = None |
260 | a8083063 | Iustin Pop | self.unique_id = unique_id
|
261 | a8083063 | Iustin Pop | self.major = None |
262 | a8083063 | Iustin Pop | self.minor = None |
263 | cb999543 | Iustin Pop | self.attached = False |
264 | 464f8daf | Iustin Pop | self.size = size
|
265 | 94dcbdb0 | Andrea Spadaccini | self.params = params
|
266 | a8083063 | Iustin Pop | |
267 | a8083063 | Iustin Pop | def Assemble(self): |
268 | a8083063 | Iustin Pop | """Assemble the device from its components.
|
269 | a8083063 | Iustin Pop |
|
270 | f87548b5 | Iustin Pop | Implementations of this method by child classes must ensure that:
|
271 | f87548b5 | Iustin Pop | - after the device has been assembled, it knows its major/minor
|
272 | f87548b5 | Iustin Pop | numbers; this allows other devices (usually parents) to probe
|
273 | f87548b5 | Iustin Pop | correctly for their children
|
274 | f87548b5 | Iustin Pop | - calling this method on an existing, in-use device is safe
|
275 | f87548b5 | Iustin Pop | - if the device is already configured (and in an OK state),
|
276 | f87548b5 | Iustin Pop | this method is idempotent
|
277 | a8083063 | Iustin Pop |
|
278 | a8083063 | Iustin Pop | """
|
279 | 1063abd1 | Iustin Pop | pass
|
280 | a8083063 | Iustin Pop | |
281 | a8083063 | Iustin Pop | def Attach(self): |
282 | a8083063 | Iustin Pop | """Find a device which matches our config and attach to it.
|
283 | a8083063 | Iustin Pop |
|
284 | a8083063 | Iustin Pop | """
|
285 | a8083063 | Iustin Pop | raise NotImplementedError |
286 | a8083063 | Iustin Pop | |
287 | a8083063 | Iustin Pop | def Close(self): |
288 | a8083063 | Iustin Pop | """Notifies that the device will no longer be used for I/O.
|
289 | a8083063 | Iustin Pop |
|
290 | a8083063 | Iustin Pop | """
|
291 | a8083063 | Iustin Pop | raise NotImplementedError |
292 | a8083063 | Iustin Pop | |
293 | a8083063 | Iustin Pop | @classmethod
|
294 | 94dcbdb0 | Andrea Spadaccini | def Create(cls, unique_id, children, size, params): |
295 | a8083063 | Iustin Pop | """Create the device.
|
296 | a8083063 | Iustin Pop |
|
297 | a8083063 | Iustin Pop | If the device cannot be created, it will return None
|
298 | a8083063 | Iustin Pop | instead. Error messages go to the logging system.
|
299 | a8083063 | Iustin Pop |
|
300 | a8083063 | Iustin Pop | Note that for some devices, the unique_id is used, and for other,
|
301 | a8083063 | Iustin Pop | the children. The idea is that these two, taken together, are
|
302 | a8083063 | Iustin Pop | enough for both creation and assembly (later).
|
303 | a8083063 | Iustin Pop |
|
304 | a8083063 | Iustin Pop | """
|
305 | a8083063 | Iustin Pop | raise NotImplementedError |
306 | a8083063 | Iustin Pop | |
307 | a8083063 | Iustin Pop | def Remove(self): |
308 | a8083063 | Iustin Pop | """Remove this device.
|
309 | a8083063 | Iustin Pop |
|
310 | abdf0113 | Iustin Pop | This makes sense only for some of the device types: LV and file
|
311 | 5bbd3f7f | Michael Hanselmann | storage. Also note that if the device can't attach, the removal
|
312 | abdf0113 | Iustin Pop | can't be completed.
|
313 | a8083063 | Iustin Pop |
|
314 | a8083063 | Iustin Pop | """
|
315 | a8083063 | Iustin Pop | raise NotImplementedError |
316 | a8083063 | Iustin Pop | |
317 | f3e513ad | Iustin Pop | def Rename(self, new_id): |
318 | f3e513ad | Iustin Pop | """Rename this device.
|
319 | f3e513ad | Iustin Pop |
|
320 | f3e513ad | Iustin Pop | This may or may not make sense for a given device type.
|
321 | f3e513ad | Iustin Pop |
|
322 | f3e513ad | Iustin Pop | """
|
323 | f3e513ad | Iustin Pop | raise NotImplementedError |
324 | f3e513ad | Iustin Pop | |
325 | a8083063 | Iustin Pop | def Open(self, force=False): |
326 | a8083063 | Iustin Pop | """Make the device ready for use.
|
327 | a8083063 | Iustin Pop |
|
328 | a8083063 | Iustin Pop | This makes the device ready for I/O. For now, just the DRBD
|
329 | a8083063 | Iustin Pop | devices need this.
|
330 | a8083063 | Iustin Pop |
|
331 | a8083063 | Iustin Pop | The force parameter signifies that if the device has any kind of
|
332 | a8083063 | Iustin Pop | --force thing, it should be used, we know what we are doing.
|
333 | a8083063 | Iustin Pop |
|
334 | a8083063 | Iustin Pop | """
|
335 | a8083063 | Iustin Pop | raise NotImplementedError |
336 | a8083063 | Iustin Pop | |
337 | a8083063 | Iustin Pop | def Shutdown(self): |
338 | a8083063 | Iustin Pop | """Shut down the device, freeing its children.
|
339 | a8083063 | Iustin Pop |
|
340 | a8083063 | Iustin Pop | This undoes the `Assemble()` work, except for the child
|
341 | a8083063 | Iustin Pop | assembling; as such, the children on the device are still
|
342 | a8083063 | Iustin Pop | assembled after this call.
|
343 | a8083063 | Iustin Pop |
|
344 | a8083063 | Iustin Pop | """
|
345 | a8083063 | Iustin Pop | raise NotImplementedError |
346 | a8083063 | Iustin Pop | |
347 | f2f57b6e | Andrea Spadaccini | def SetSyncParams(self, params): |
348 | f2f57b6e | Andrea Spadaccini | """Adjust the synchronization parameters of the mirror.
|
349 | a8083063 | Iustin Pop |
|
350 | a8083063 | Iustin Pop | In case this is not a mirroring device, this is no-op.
|
351 | a8083063 | Iustin Pop |
|
352 | f2f57b6e | Andrea Spadaccini | @param params: dictionary of LD level disk parameters related to the
|
353 | f2f57b6e | Andrea Spadaccini | synchronization.
|
354 | 8584e922 | Andrea Spadaccini | @rtype: list
|
355 | 8584e922 | Andrea Spadaccini | @return: a list of error messages, emitted both by the current node and by
|
356 | 8584e922 | Andrea Spadaccini | children. An empty list means no errors.
|
357 | f2f57b6e | Andrea Spadaccini |
|
358 | a8083063 | Iustin Pop | """
|
359 | 8584e922 | Andrea Spadaccini | result = [] |
360 | a8083063 | Iustin Pop | if self._children: |
361 | a8083063 | Iustin Pop | for child in self._children: |
362 | 8584e922 | Andrea Spadaccini | result.extend(child.SetSyncParams(params)) |
363 | a8083063 | Iustin Pop | return result
|
364 | a8083063 | Iustin Pop | |
365 | a3fffcc6 | René Nussbaumer | def PauseResumeSync(self, pause): |
366 | a3fffcc6 | René Nussbaumer | """Pause/Resume the sync of the mirror.
|
367 | a3fffcc6 | René Nussbaumer |
|
368 | a3fffcc6 | René Nussbaumer | In case this is not a mirroring device, this is no-op.
|
369 | a3fffcc6 | René Nussbaumer |
|
370 | f2f57b6e | Andrea Spadaccini | @param pause: Whether to pause or resume
|
371 | a3fffcc6 | René Nussbaumer |
|
372 | a3fffcc6 | René Nussbaumer | """
|
373 | a3fffcc6 | René Nussbaumer | result = True
|
374 | a3fffcc6 | René Nussbaumer | if self._children: |
375 | a3fffcc6 | René Nussbaumer | for child in self._children: |
376 | a3fffcc6 | René Nussbaumer | result = result and child.PauseResumeSync(pause)
|
377 | a3fffcc6 | René Nussbaumer | return result
|
378 | a3fffcc6 | René Nussbaumer | |
379 | a8083063 | Iustin Pop | def GetSyncStatus(self): |
380 | a8083063 | Iustin Pop | """Returns the sync status of the device.
|
381 | a8083063 | Iustin Pop |
|
382 | a8083063 | Iustin Pop | If this device is a mirroring device, this function returns the
|
383 | a8083063 | Iustin Pop | status of the mirror.
|
384 | a8083063 | Iustin Pop |
|
385 | 0834c866 | Iustin Pop | If sync_percent is None, it means the device is not syncing.
|
386 | a8083063 | Iustin Pop |
|
387 | a8083063 | Iustin Pop | If estimated_time is None, it means we can't estimate
|
388 | 0834c866 | Iustin Pop | the time needed, otherwise it's the time left in seconds.
|
389 | 0834c866 | Iustin Pop |
|
390 | a8083063 | Iustin Pop | If is_degraded is True, it means the device is missing
|
391 | a8083063 | Iustin Pop | redundancy. This is usually a sign that something went wrong in
|
392 | a8083063 | Iustin Pop | the device setup, if sync_percent is None.
|
393 | a8083063 | Iustin Pop |
|
394 | 0834c866 | Iustin Pop | The ldisk parameter represents the degradation of the local
|
395 | 0834c866 | Iustin Pop | data. This is only valid for some devices, the rest will always
|
396 | 0834c866 | Iustin Pop | return False (not degraded).
|
397 | 0834c866 | Iustin Pop |
|
398 | 96acbc09 | Michael Hanselmann | @rtype: objects.BlockDevStatus
|
399 | c41eea6e | Iustin Pop |
|
400 | a8083063 | Iustin Pop | """
|
401 | 96acbc09 | Michael Hanselmann | return objects.BlockDevStatus(dev_path=self.dev_path, |
402 | 96acbc09 | Michael Hanselmann | major=self.major,
|
403 | 96acbc09 | Michael Hanselmann | minor=self.minor,
|
404 | 96acbc09 | Michael Hanselmann | sync_percent=None,
|
405 | 96acbc09 | Michael Hanselmann | estimated_time=None,
|
406 | 96acbc09 | Michael Hanselmann | is_degraded=False,
|
407 | f208978a | Michael Hanselmann | ldisk_status=constants.LDS_OKAY) |
408 | a8083063 | Iustin Pop | |
409 | a8083063 | Iustin Pop | def CombinedSyncStatus(self): |
410 | a8083063 | Iustin Pop | """Calculate the mirror status recursively for our children.
|
411 | a8083063 | Iustin Pop |
|
412 | a8083063 | Iustin Pop | The return value is the same as for `GetSyncStatus()` except the
|
413 | a8083063 | Iustin Pop | minimum percent and maximum time are calculated across our
|
414 | a8083063 | Iustin Pop | children.
|
415 | a8083063 | Iustin Pop |
|
416 | 96acbc09 | Michael Hanselmann | @rtype: objects.BlockDevStatus
|
417 | 96acbc09 | Michael Hanselmann |
|
418 | a8083063 | Iustin Pop | """
|
419 | 96acbc09 | Michael Hanselmann | status = self.GetSyncStatus()
|
420 | 96acbc09 | Michael Hanselmann | |
421 | 96acbc09 | Michael Hanselmann | min_percent = status.sync_percent |
422 | 96acbc09 | Michael Hanselmann | max_time = status.estimated_time |
423 | 96acbc09 | Michael Hanselmann | is_degraded = status.is_degraded |
424 | f208978a | Michael Hanselmann | ldisk_status = status.ldisk_status |
425 | 96acbc09 | Michael Hanselmann | |
426 | a8083063 | Iustin Pop | if self._children: |
427 | a8083063 | Iustin Pop | for child in self._children: |
428 | 96acbc09 | Michael Hanselmann | child_status = child.GetSyncStatus() |
429 | 96acbc09 | Michael Hanselmann | |
430 | a8083063 | Iustin Pop | if min_percent is None: |
431 | 96acbc09 | Michael Hanselmann | min_percent = child_status.sync_percent |
432 | 96acbc09 | Michael Hanselmann | elif child_status.sync_percent is not None: |
433 | 96acbc09 | Michael Hanselmann | min_percent = min(min_percent, child_status.sync_percent)
|
434 | 96acbc09 | Michael Hanselmann | |
435 | a8083063 | Iustin Pop | if max_time is None: |
436 | 96acbc09 | Michael Hanselmann | max_time = child_status.estimated_time |
437 | 96acbc09 | Michael Hanselmann | elif child_status.estimated_time is not None: |
438 | 96acbc09 | Michael Hanselmann | max_time = max(max_time, child_status.estimated_time)
|
439 | 96acbc09 | Michael Hanselmann | |
440 | 96acbc09 | Michael Hanselmann | is_degraded = is_degraded or child_status.is_degraded
|
441 | f208978a | Michael Hanselmann | |
442 | f208978a | Michael Hanselmann | if ldisk_status is None: |
443 | f208978a | Michael Hanselmann | ldisk_status = child_status.ldisk_status |
444 | f208978a | Michael Hanselmann | elif child_status.ldisk_status is not None: |
445 | f208978a | Michael Hanselmann | ldisk_status = max(ldisk_status, child_status.ldisk_status)
|
446 | 96acbc09 | Michael Hanselmann | |
447 | 96acbc09 | Michael Hanselmann | return objects.BlockDevStatus(dev_path=self.dev_path, |
448 | 96acbc09 | Michael Hanselmann | major=self.major,
|
449 | 96acbc09 | Michael Hanselmann | minor=self.minor,
|
450 | 96acbc09 | Michael Hanselmann | sync_percent=min_percent, |
451 | 96acbc09 | Michael Hanselmann | estimated_time=max_time, |
452 | 96acbc09 | Michael Hanselmann | is_degraded=is_degraded, |
453 | f208978a | Michael Hanselmann | ldisk_status=ldisk_status) |
454 | a8083063 | Iustin Pop | |
455 | a0c3fea1 | Michael Hanselmann | def SetInfo(self, text): |
456 | a0c3fea1 | Michael Hanselmann | """Update metadata with info text.
|
457 | a0c3fea1 | Michael Hanselmann |
|
458 | a0c3fea1 | Michael Hanselmann | Only supported for some device types.
|
459 | a0c3fea1 | Michael Hanselmann |
|
460 | a0c3fea1 | Michael Hanselmann | """
|
461 | a0c3fea1 | Michael Hanselmann | for child in self._children: |
462 | a0c3fea1 | Michael Hanselmann | child.SetInfo(text) |
463 | a0c3fea1 | Michael Hanselmann | |
464 | cad0723b | Iustin Pop | def Grow(self, amount, dryrun, backingstore): |
465 | 1005d816 | Iustin Pop | """Grow the block device.
|
466 | 1005d816 | Iustin Pop |
|
467 | 7fe23d47 | Iustin Pop | @type amount: integer
|
468 | c41eea6e | Iustin Pop | @param amount: the amount (in mebibytes) to grow with
|
469 | 7fe23d47 | Iustin Pop | @type dryrun: boolean
|
470 | 7fe23d47 | Iustin Pop | @param dryrun: whether to execute the operation in simulation mode
|
471 | 7fe23d47 | Iustin Pop | only, without actually increasing the size
|
472 | cad0723b | Iustin Pop | @param backingstore: whether to execute the operation on backing storage
|
473 | cad0723b | Iustin Pop | only, or on "logical" storage only; e.g. DRBD is logical storage,
|
474 | cad0723b | Iustin Pop | whereas LVM, file, RBD are backing storage
|
475 | 1005d816 | Iustin Pop |
|
476 | 1005d816 | Iustin Pop | """
|
477 | 1005d816 | Iustin Pop | raise NotImplementedError |
478 | a0c3fea1 | Michael Hanselmann | |
479 | fcff3897 | Iustin Pop | def GetActualSize(self): |
480 | fcff3897 | Iustin Pop | """Return the actual disk size.
|
481 | fcff3897 | Iustin Pop |
|
482 | fcff3897 | Iustin Pop | @note: the device needs to be active when this is called
|
483 | fcff3897 | Iustin Pop |
|
484 | fcff3897 | Iustin Pop | """
|
485 | fcff3897 | Iustin Pop | assert self.attached, "BlockDevice not attached in GetActualSize()" |
486 | fcff3897 | Iustin Pop | result = utils.RunCmd(["blockdev", "--getsize64", self.dev_path]) |
487 | fcff3897 | Iustin Pop | if result.failed:
|
488 | fcff3897 | Iustin Pop | _ThrowError("blockdev failed (%s): %s",
|
489 | fcff3897 | Iustin Pop | result.fail_reason, result.output) |
490 | fcff3897 | Iustin Pop | try:
|
491 | fcff3897 | Iustin Pop | sz = int(result.output.strip())
|
492 | fcff3897 | Iustin Pop | except (ValueError, TypeError), err: |
493 | fcff3897 | Iustin Pop | _ThrowError("Failed to parse blockdev output: %s", str(err)) |
494 | fcff3897 | Iustin Pop | return sz
|
495 | fcff3897 | Iustin Pop | |
496 | a8083063 | Iustin Pop | def __repr__(self): |
497 | a8083063 | Iustin Pop | return ("<%s: unique_id: %s, children: %s, %s:%s, %s>" % |
498 | a8083063 | Iustin Pop | (self.__class__, self.unique_id, self._children, |
499 | a8083063 | Iustin Pop | self.major, self.minor, self.dev_path)) |
500 | a8083063 | Iustin Pop | |
501 | a8083063 | Iustin Pop | |
502 | a8083063 | Iustin Pop | class LogicalVolume(BlockDev): |
503 | a8083063 | Iustin Pop | """Logical Volume block device.
|
504 | a8083063 | Iustin Pop |
|
505 | a8083063 | Iustin Pop | """
|
506 | 6136f8f0 | Iustin Pop | _VALID_NAME_RE = re.compile("^[a-zA-Z0-9+_.-]*$")
|
507 | 6136f8f0 | Iustin Pop | _INVALID_NAMES = frozenset([".", "..", "snapshot", "pvmove"]) |
508 | 6136f8f0 | Iustin Pop | _INVALID_SUBSTRINGS = frozenset(["_mlog", "_mimage"]) |
509 | 6136f8f0 | Iustin Pop | |
510 | 94dcbdb0 | Andrea Spadaccini | def __init__(self, unique_id, children, size, params): |
511 | a8083063 | Iustin Pop | """Attaches to a LV device.
|
512 | a8083063 | Iustin Pop |
|
513 | a8083063 | Iustin Pop | The unique_id is a tuple (vg_name, lv_name)
|
514 | a8083063 | Iustin Pop |
|
515 | a8083063 | Iustin Pop | """
|
516 | 94dcbdb0 | Andrea Spadaccini | super(LogicalVolume, self).__init__(unique_id, children, size, params) |
517 | a8083063 | Iustin Pop | if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2: |
518 | a8083063 | Iustin Pop | raise ValueError("Invalid configuration data %s" % str(unique_id)) |
519 | a8083063 | Iustin Pop | self._vg_name, self._lv_name = unique_id |
520 | 6136f8f0 | Iustin Pop | self._ValidateName(self._vg_name) |
521 | 6136f8f0 | Iustin Pop | self._ValidateName(self._lv_name) |
522 | 6136f8f0 | Iustin Pop | self.dev_path = utils.PathJoin("/dev", self._vg_name, self._lv_name) |
523 | 99e8295c | Iustin Pop | self._degraded = True |
524 | 38256320 | Iustin Pop | self.major = self.minor = self.pe_size = self.stripe_count = None |
525 | a8083063 | Iustin Pop | self.Attach()
|
526 | a8083063 | Iustin Pop | |
527 | a8083063 | Iustin Pop | @classmethod
|
528 | 94dcbdb0 | Andrea Spadaccini | def Create(cls, unique_id, children, size, params): |
529 | a8083063 | Iustin Pop | """Create a new logical volume.
|
530 | a8083063 | Iustin Pop |
|
531 | a8083063 | Iustin Pop | """
|
532 | a8083063 | Iustin Pop | if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2: |
533 | 6c626518 | Iustin Pop | raise errors.ProgrammerError("Invalid configuration data %s" % |
534 | 6c626518 | Iustin Pop | str(unique_id))
|
535 | a8083063 | Iustin Pop | vg_name, lv_name = unique_id |
536 | 6136f8f0 | Iustin Pop | cls._ValidateName(vg_name) |
537 | 6136f8f0 | Iustin Pop | cls._ValidateName(lv_name) |
538 | 2070598f | Iustin Pop | pvs_info = cls.GetPVInfo([vg_name]) |
539 | a8083063 | Iustin Pop | if not pvs_info: |
540 | 82463074 | Iustin Pop | _ThrowError("Can't compute PV info for vg %s", vg_name)
|
541 | a8083063 | Iustin Pop | pvs_info.sort() |
542 | a8083063 | Iustin Pop | pvs_info.reverse() |
543 | 5b7b5d49 | Guido Trotter | |
544 | e687ec01 | Michael Hanselmann | pvlist = [pv[1] for pv in pvs_info] |
545 | 403f5172 | Guido Trotter | if compat.any(":" in v for v in pvlist): |
546 | 01b6558a | Iustin Pop | _ThrowError("Some of your PVs have the invalid character ':' in their"
|
547 | 01b6558a | Iustin Pop | " name, this is not supported - please filter them out"
|
548 | 01b6558a | Iustin Pop | " in lvm.conf using either 'filter' or 'preferred_names'")
|
549 | e687ec01 | Michael Hanselmann | free_size = sum([pv[0] for pv in pvs_info]) |
550 | fecbe9d5 | Iustin Pop | current_pvs = len(pvlist)
|
551 | ac00bf1b | Andrea Spadaccini | desired_stripes = params[constants.LDP_STRIPES] |
552 | 43e11798 | Andrea Spadaccini | stripes = min(current_pvs, desired_stripes)
|
553 | 43e11798 | Andrea Spadaccini | if stripes < desired_stripes:
|
554 | 43e11798 | Andrea Spadaccini | logging.warning("Could not use %d stripes for VG %s, as only %d PVs are"
|
555 | 43e11798 | Andrea Spadaccini | " available.", desired_stripes, vg_name, current_pvs)
|
556 | 5b7b5d49 | Guido Trotter | |
557 | 5b7b5d49 | Guido Trotter | # The size constraint should have been checked from the master before
|
558 | 5b7b5d49 | Guido Trotter | # calling the create function.
|
559 | a8083063 | Iustin Pop | if free_size < size:
|
560 | 82463074 | Iustin Pop | _ThrowError("Not enough free space: required %s,"
|
561 | 82463074 | Iustin Pop | " available %s", size, free_size)
|
562 | fecbe9d5 | Iustin Pop | cmd = ["lvcreate", "-L%dm" % size, "-n%s" % lv_name] |
563 | fecbe9d5 | Iustin Pop | # If the free space is not well distributed, we won't be able to
|
564 | fecbe9d5 | Iustin Pop | # create an optimally-striped volume; in that case, we want to try
|
565 | fecbe9d5 | Iustin Pop | # with N, N-1, ..., 2, and finally 1 (non-stripped) number of
|
566 | fecbe9d5 | Iustin Pop | # stripes
|
567 | fecbe9d5 | Iustin Pop | for stripes_arg in range(stripes, 0, -1): |
568 | fecbe9d5 | Iustin Pop | result = utils.RunCmd(cmd + ["-i%d" % stripes_arg] + [vg_name] + pvlist)
|
569 | fecbe9d5 | Iustin Pop | if not result.failed: |
570 | fecbe9d5 | Iustin Pop | break
|
571 | a8083063 | Iustin Pop | if result.failed:
|
572 | 82463074 | Iustin Pop | _ThrowError("LV create failed (%s): %s",
|
573 | 82463074 | Iustin Pop | result.fail_reason, result.output) |
574 | 94dcbdb0 | Andrea Spadaccini | return LogicalVolume(unique_id, children, size, params)
|
575 | a8083063 | Iustin Pop | |
576 | a8083063 | Iustin Pop | @staticmethod
|
577 | 197478f2 | René Nussbaumer | def _GetVolumeInfo(lvm_cmd, fields): |
578 | 197478f2 | René Nussbaumer | """Returns LVM Volumen infos using lvm_cmd
|
579 | 197478f2 | René Nussbaumer |
|
580 | 197478f2 | René Nussbaumer | @param lvm_cmd: Should be one of "pvs", "vgs" or "lvs"
|
581 | 197478f2 | René Nussbaumer | @param fields: Fields to return
|
582 | 197478f2 | René Nussbaumer | @return: A list of dicts each with the parsed fields
|
583 | 197478f2 | René Nussbaumer |
|
584 | 197478f2 | René Nussbaumer | """
|
585 | 197478f2 | René Nussbaumer | if not fields: |
586 | 197478f2 | René Nussbaumer | raise errors.ProgrammerError("No fields specified") |
587 | 197478f2 | René Nussbaumer | |
588 | 197478f2 | René Nussbaumer | sep = "|"
|
589 | 197478f2 | René Nussbaumer | cmd = [lvm_cmd, "--noheadings", "--nosuffix", "--units=m", "--unbuffered", |
590 | 197478f2 | René Nussbaumer | "--separator=%s" % sep, "-o%s" % ",".join(fields)] |
591 | 197478f2 | René Nussbaumer | |
592 | 197478f2 | René Nussbaumer | result = utils.RunCmd(cmd) |
593 | 197478f2 | René Nussbaumer | if result.failed:
|
594 | 197478f2 | René Nussbaumer | raise errors.CommandError("Can't get the volume information: %s - %s" % |
595 | 197478f2 | René Nussbaumer | (result.fail_reason, result.output)) |
596 | 197478f2 | René Nussbaumer | |
597 | 197478f2 | René Nussbaumer | data = [] |
598 | 197478f2 | René Nussbaumer | for line in result.stdout.splitlines(): |
599 | 197478f2 | René Nussbaumer | splitted_fields = line.strip().split(sep) |
600 | 197478f2 | René Nussbaumer | |
601 | 197478f2 | René Nussbaumer | if len(fields) != len(splitted_fields): |
602 | 197478f2 | René Nussbaumer | raise errors.CommandError("Can't parse %s output: line '%s'" % |
603 | 197478f2 | René Nussbaumer | (lvm_cmd, line)) |
604 | 197478f2 | René Nussbaumer | |
605 | 197478f2 | René Nussbaumer | data.append(splitted_fields) |
606 | 197478f2 | René Nussbaumer | |
607 | 197478f2 | René Nussbaumer | return data
|
608 | 197478f2 | René Nussbaumer | |
609 | 197478f2 | René Nussbaumer | @classmethod
|
610 | 197478f2 | René Nussbaumer | def GetPVInfo(cls, vg_names, filter_allocatable=True): |
611 | a8083063 | Iustin Pop | """Get the free space info for PVs in a volume group.
|
612 | a8083063 | Iustin Pop |
|
613 | 2070598f | Iustin Pop | @param vg_names: list of volume group names, if empty all will be returned
|
614 | 2070598f | Iustin Pop | @param filter_allocatable: whether to skip over unallocatable PVs
|
615 | a8083063 | Iustin Pop |
|
616 | c41eea6e | Iustin Pop | @rtype: list
|
617 | c41eea6e | Iustin Pop | @return: list of tuples (free_space, name) with free_space in mebibytes
|
618 | 098c0958 | Michael Hanselmann |
|
619 | a8083063 | Iustin Pop | """
|
620 | 197478f2 | René Nussbaumer | try:
|
621 | 197478f2 | René Nussbaumer | info = cls._GetVolumeInfo("pvs", ["pv_name", "vg_name", "pv_free", |
622 | 197478f2 | René Nussbaumer | "pv_attr"])
|
623 | 197478f2 | René Nussbaumer | except errors.GenericError, err:
|
624 | 197478f2 | René Nussbaumer | logging.error("Can't get PV information: %s", err)
|
625 | a8083063 | Iustin Pop | return None |
626 | 197478f2 | René Nussbaumer | |
627 | a8083063 | Iustin Pop | data = [] |
628 | 197478f2 | René Nussbaumer | for pv_name, vg_name, pv_free, pv_attr in info: |
629 | 2070598f | Iustin Pop | # (possibly) skip over pvs which are not allocatable
|
630 | 197478f2 | René Nussbaumer | if filter_allocatable and pv_attr[0] != "a": |
631 | a8083063 | Iustin Pop | continue
|
632 | 2070598f | Iustin Pop | # (possibly) skip over pvs which are not in the right volume group(s)
|
633 | 197478f2 | René Nussbaumer | if vg_names and vg_name not in vg_names: |
634 | 2070598f | Iustin Pop | continue
|
635 | 197478f2 | René Nussbaumer | data.append((float(pv_free), pv_name, vg_name))
|
636 | 197478f2 | René Nussbaumer | |
637 | 197478f2 | René Nussbaumer | return data
|
638 | 197478f2 | René Nussbaumer | |
639 | 197478f2 | René Nussbaumer | @classmethod
|
640 | 197478f2 | René Nussbaumer | def GetVGInfo(cls, vg_names, filter_readonly=True): |
641 | 197478f2 | René Nussbaumer | """Get the free space info for specific VGs.
|
642 | 197478f2 | René Nussbaumer |
|
643 | 197478f2 | René Nussbaumer | @param vg_names: list of volume group names, if empty all will be returned
|
644 | 197478f2 | René Nussbaumer | @param filter_readonly: whether to skip over readonly VGs
|
645 | 197478f2 | René Nussbaumer |
|
646 | 197478f2 | René Nussbaumer | @rtype: list
|
647 | 673cd9c4 | René Nussbaumer | @return: list of tuples (free_space, total_size, name) with free_space in
|
648 | 673cd9c4 | René Nussbaumer | MiB
|
649 | 197478f2 | René Nussbaumer |
|
650 | 197478f2 | René Nussbaumer | """
|
651 | 197478f2 | René Nussbaumer | try:
|
652 | 673cd9c4 | René Nussbaumer | info = cls._GetVolumeInfo("vgs", ["vg_name", "vg_free", "vg_attr", |
653 | 673cd9c4 | René Nussbaumer | "vg_size"])
|
654 | 197478f2 | René Nussbaumer | except errors.GenericError, err:
|
655 | 197478f2 | René Nussbaumer | logging.error("Can't get VG information: %s", err)
|
656 | 197478f2 | René Nussbaumer | return None |
657 | 197478f2 | René Nussbaumer | |
658 | 197478f2 | René Nussbaumer | data = [] |
659 | 673cd9c4 | René Nussbaumer | for vg_name, vg_free, vg_attr, vg_size in info: |
660 | 197478f2 | René Nussbaumer | # (possibly) skip over vgs which are not writable
|
661 | 197478f2 | René Nussbaumer | if filter_readonly and vg_attr[0] == "r": |
662 | 197478f2 | René Nussbaumer | continue
|
663 | 197478f2 | René Nussbaumer | # (possibly) skip over vgs which are not in the right volume group(s)
|
664 | 197478f2 | René Nussbaumer | if vg_names and vg_name not in vg_names: |
665 | 197478f2 | René Nussbaumer | continue
|
666 | 673cd9c4 | René Nussbaumer | data.append((float(vg_free), float(vg_size), vg_name)) |
667 | a8083063 | Iustin Pop | |
668 | a8083063 | Iustin Pop | return data
|
669 | a8083063 | Iustin Pop | |
670 | 6136f8f0 | Iustin Pop | @classmethod
|
671 | 6136f8f0 | Iustin Pop | def _ValidateName(cls, name): |
672 | 6136f8f0 | Iustin Pop | """Validates that a given name is valid as VG or LV name.
|
673 | 6136f8f0 | Iustin Pop |
|
674 | 6136f8f0 | Iustin Pop | The list of valid characters and restricted names is taken out of
|
675 | 6136f8f0 | Iustin Pop | the lvm(8) manpage, with the simplification that we enforce both
|
676 | 6136f8f0 | Iustin Pop | VG and LV restrictions on the names.
|
677 | 6136f8f0 | Iustin Pop |
|
678 | 6136f8f0 | Iustin Pop | """
|
679 | 6136f8f0 | Iustin Pop | if (not cls._VALID_NAME_RE.match(name) or |
680 | 6136f8f0 | Iustin Pop | name in cls._INVALID_NAMES or |
681 | 403f5172 | Guido Trotter | compat.any(substring in name for substring in cls._INVALID_SUBSTRINGS)): |
682 | 6136f8f0 | Iustin Pop | _ThrowError("Invalid LVM name '%s'", name)
|
683 | 6136f8f0 | Iustin Pop | |
684 | a8083063 | Iustin Pop | def Remove(self): |
685 | a8083063 | Iustin Pop | """Remove this logical volume.
|
686 | a8083063 | Iustin Pop |
|
687 | a8083063 | Iustin Pop | """
|
688 | a8083063 | Iustin Pop | if not self.minor and not self.Attach(): |
689 | a8083063 | Iustin Pop | # the LV does not exist
|
690 | 0c6c04ec | Iustin Pop | return
|
691 | a8083063 | Iustin Pop | result = utils.RunCmd(["lvremove", "-f", "%s/%s" % |
692 | a8083063 | Iustin Pop | (self._vg_name, self._lv_name)]) |
693 | a8083063 | Iustin Pop | if result.failed:
|
694 | 0c6c04ec | Iustin Pop | _ThrowError("Can't lvremove: %s - %s", result.fail_reason, result.output)
|
695 | a8083063 | Iustin Pop | |
696 | f3e513ad | Iustin Pop | def Rename(self, new_id): |
697 | f3e513ad | Iustin Pop | """Rename this logical volume.
|
698 | f3e513ad | Iustin Pop |
|
699 | f3e513ad | Iustin Pop | """
|
700 | f3e513ad | Iustin Pop | if not isinstance(new_id, (tuple, list)) or len(new_id) != 2: |
701 | f3e513ad | Iustin Pop | raise errors.ProgrammerError("Invalid new logical id '%s'" % new_id) |
702 | f3e513ad | Iustin Pop | new_vg, new_name = new_id |
703 | f3e513ad | Iustin Pop | if new_vg != self._vg_name: |
704 | f3e513ad | Iustin Pop | raise errors.ProgrammerError("Can't move a logical volume across" |
705 | f3e513ad | Iustin Pop | " volume groups (from %s to to %s)" %
|
706 | f3e513ad | Iustin Pop | (self._vg_name, new_vg))
|
707 | f3e513ad | Iustin Pop | result = utils.RunCmd(["lvrename", new_vg, self._lv_name, new_name]) |
708 | f3e513ad | Iustin Pop | if result.failed:
|
709 | 82463074 | Iustin Pop | _ThrowError("Failed to rename the logical volume: %s", result.output)
|
710 | be345db0 | Iustin Pop | self._lv_name = new_name
|
711 | 6136f8f0 | Iustin Pop | self.dev_path = utils.PathJoin("/dev", self._vg_name, self._lv_name) |
712 | be345db0 | Iustin Pop | |
713 | a8083063 | Iustin Pop | def Attach(self): |
714 | a8083063 | Iustin Pop | """Attach to an existing LV.
|
715 | a8083063 | Iustin Pop |
|
716 | a8083063 | Iustin Pop | This method will try to see if an existing and active LV exists
|
717 | c99a3cc0 | Manuel Franceschini | which matches our name. If so, its major/minor will be
|
718 | a8083063 | Iustin Pop | recorded.
|
719 | a8083063 | Iustin Pop |
|
720 | a8083063 | Iustin Pop | """
|
721 | cb999543 | Iustin Pop | self.attached = False |
722 | 99e8295c | Iustin Pop | result = utils.RunCmd(["lvs", "--noheadings", "--separator=,", |
723 | 38256320 | Iustin Pop | "--units=m", "--nosuffix", |
724 | 38256320 | Iustin Pop | "-olv_attr,lv_kernel_major,lv_kernel_minor,"
|
725 | 38256320 | Iustin Pop | "vg_extent_size,stripes", self.dev_path]) |
726 | a8083063 | Iustin Pop | if result.failed:
|
727 | 468c5f77 | Iustin Pop | logging.error("Can't find LV %s: %s, %s",
|
728 | 468c5f77 | Iustin Pop | self.dev_path, result.fail_reason, result.output)
|
729 | a8083063 | Iustin Pop | return False |
730 | 38256320 | Iustin Pop | # the output can (and will) have multiple lines for multi-segment
|
731 | 38256320 | Iustin Pop | # LVs, as the 'stripes' parameter is a segment one, so we take
|
732 | 38256320 | Iustin Pop | # only the last entry, which is the one we're interested in; note
|
733 | 38256320 | Iustin Pop | # that with LVM2 anyway the 'stripes' value must be constant
|
734 | 38256320 | Iustin Pop | # across segments, so this is a no-op actually
|
735 | 38256320 | Iustin Pop | out = result.stdout.splitlines() |
736 | 38256320 | Iustin Pop | if not out: # totally empty result? splitlines() returns at least |
737 | 38256320 | Iustin Pop | # one line for any non-empty string
|
738 | 38256320 | Iustin Pop | logging.error("Can't parse LVS output, no lines? Got '%s'", str(out)) |
739 | 38256320 | Iustin Pop | return False |
740 | d0c8c01d | Iustin Pop | out = out[-1].strip().rstrip(",") |
741 | 99e8295c | Iustin Pop | out = out.split(",")
|
742 | 38256320 | Iustin Pop | if len(out) != 5: |
743 | 38256320 | Iustin Pop | logging.error("Can't parse LVS output, len(%s) != 5", str(out)) |
744 | 99e8295c | Iustin Pop | return False |
745 | 99e8295c | Iustin Pop | |
746 | 38256320 | Iustin Pop | status, major, minor, pe_size, stripes = out |
747 | 0304f0ec | Iustin Pop | if len(status) < 6: |
748 | 0304f0ec | Iustin Pop | logging.error("lvs lv_attr is not at least 6 characters (%s)", status)
|
749 | 99e8295c | Iustin Pop | return False |
750 | 99e8295c | Iustin Pop | |
751 | 99e8295c | Iustin Pop | try:
|
752 | 99e8295c | Iustin Pop | major = int(major)
|
753 | 99e8295c | Iustin Pop | minor = int(minor)
|
754 | 691744c4 | Iustin Pop | except (TypeError, ValueError), err: |
755 | 468c5f77 | Iustin Pop | logging.error("lvs major/minor cannot be parsed: %s", str(err)) |
756 | 99e8295c | Iustin Pop | |
757 | 38256320 | Iustin Pop | try:
|
758 | 38256320 | Iustin Pop | pe_size = int(float(pe_size)) |
759 | 38256320 | Iustin Pop | except (TypeError, ValueError), err: |
760 | 38256320 | Iustin Pop | logging.error("Can't parse vg extent size: %s", err)
|
761 | 38256320 | Iustin Pop | return False |
762 | 38256320 | Iustin Pop | |
763 | 38256320 | Iustin Pop | try:
|
764 | 38256320 | Iustin Pop | stripes = int(stripes)
|
765 | 38256320 | Iustin Pop | except (TypeError, ValueError), err: |
766 | 38256320 | Iustin Pop | logging.error("Can't parse the number of stripes: %s", err)
|
767 | 38256320 | Iustin Pop | return False |
768 | 38256320 | Iustin Pop | |
769 | 99e8295c | Iustin Pop | self.major = major
|
770 | 99e8295c | Iustin Pop | self.minor = minor
|
771 | 38256320 | Iustin Pop | self.pe_size = pe_size
|
772 | 38256320 | Iustin Pop | self.stripe_count = stripes
|
773 | d0c8c01d | Iustin Pop | self._degraded = status[0] == "v" # virtual volume, i.e. doesn't backing |
774 | 99e8295c | Iustin Pop | # storage
|
775 | cb999543 | Iustin Pop | self.attached = True |
776 | 99e8295c | Iustin Pop | return True |
777 | a8083063 | Iustin Pop | |
778 | a8083063 | Iustin Pop | def Assemble(self): |
779 | a8083063 | Iustin Pop | """Assemble the device.
|
780 | a8083063 | Iustin Pop |
|
781 | 5bbd3f7f | Michael Hanselmann | We always run `lvchange -ay` on the LV to ensure it's active before
|
782 | 5574047a | Iustin Pop | use, as there were cases when xenvg was not active after boot
|
783 | 5574047a | Iustin Pop | (also possibly after disk issues).
|
784 | a8083063 | Iustin Pop |
|
785 | a8083063 | Iustin Pop | """
|
786 | 5574047a | Iustin Pop | result = utils.RunCmd(["lvchange", "-ay", self.dev_path]) |
787 | 5574047a | Iustin Pop | if result.failed:
|
788 | 1063abd1 | Iustin Pop | _ThrowError("Can't activate lv %s: %s", self.dev_path, result.output) |
789 | a8083063 | Iustin Pop | |
790 | a8083063 | Iustin Pop | def Shutdown(self): |
791 | a8083063 | Iustin Pop | """Shutdown the device.
|
792 | a8083063 | Iustin Pop |
|
793 | a8083063 | Iustin Pop | This is a no-op for the LV device type, as we don't deactivate the
|
794 | a8083063 | Iustin Pop | volumes on shutdown.
|
795 | a8083063 | Iustin Pop |
|
796 | a8083063 | Iustin Pop | """
|
797 | 746f7476 | Iustin Pop | pass
|
798 | a8083063 | Iustin Pop | |
799 | 9db6dbce | Iustin Pop | def GetSyncStatus(self): |
800 | 9db6dbce | Iustin Pop | """Returns the sync status of the device.
|
801 | 9db6dbce | Iustin Pop |
|
802 | 9db6dbce | Iustin Pop | If this device is a mirroring device, this function returns the
|
803 | 9db6dbce | Iustin Pop | status of the mirror.
|
804 | 9db6dbce | Iustin Pop |
|
805 | 9db6dbce | Iustin Pop | For logical volumes, sync_percent and estimated_time are always
|
806 | 9db6dbce | Iustin Pop | None (no recovery in progress, as we don't handle the mirrored LV
|
807 | 0834c866 | Iustin Pop | case). The is_degraded parameter is the inverse of the ldisk
|
808 | 0834c866 | Iustin Pop | parameter.
|
809 | 9db6dbce | Iustin Pop |
|
810 | 0834c866 | Iustin Pop | For the ldisk parameter, we check if the logical volume has the
|
811 | 0834c866 | Iustin Pop | 'virtual' type, which means it's not backed by existing storage
|
812 | 0834c866 | Iustin Pop | anymore (read from it return I/O error). This happens after a
|
813 | 0834c866 | Iustin Pop | physical disk failure and subsequent 'vgreduce --removemissing' on
|
814 | 0834c866 | Iustin Pop | the volume group.
|
815 | 9db6dbce | Iustin Pop |
|
816 | 99e8295c | Iustin Pop | The status was already read in Attach, so we just return it.
|
817 | 99e8295c | Iustin Pop |
|
818 | 96acbc09 | Michael Hanselmann | @rtype: objects.BlockDevStatus
|
819 | c41eea6e | Iustin Pop |
|
820 | 9db6dbce | Iustin Pop | """
|
821 | f208978a | Michael Hanselmann | if self._degraded: |
822 | f208978a | Michael Hanselmann | ldisk_status = constants.LDS_FAULTY |
823 | f208978a | Michael Hanselmann | else:
|
824 | f208978a | Michael Hanselmann | ldisk_status = constants.LDS_OKAY |
825 | f208978a | Michael Hanselmann | |
826 | 96acbc09 | Michael Hanselmann | return objects.BlockDevStatus(dev_path=self.dev_path, |
827 | 96acbc09 | Michael Hanselmann | major=self.major,
|
828 | 96acbc09 | Michael Hanselmann | minor=self.minor,
|
829 | 96acbc09 | Michael Hanselmann | sync_percent=None,
|
830 | 96acbc09 | Michael Hanselmann | estimated_time=None,
|
831 | 96acbc09 | Michael Hanselmann | is_degraded=self._degraded,
|
832 | f208978a | Michael Hanselmann | ldisk_status=ldisk_status) |
833 | 9db6dbce | Iustin Pop | |
834 | a8083063 | Iustin Pop | def Open(self, force=False): |
835 | a8083063 | Iustin Pop | """Make the device ready for I/O.
|
836 | a8083063 | Iustin Pop |
|
837 | a8083063 | Iustin Pop | This is a no-op for the LV device type.
|
838 | a8083063 | Iustin Pop |
|
839 | a8083063 | Iustin Pop | """
|
840 | fdbd668d | Iustin Pop | pass
|
841 | a8083063 | Iustin Pop | |
842 | a8083063 | Iustin Pop | def Close(self): |
843 | a8083063 | Iustin Pop | """Notifies that the device will no longer be used for I/O.
|
844 | a8083063 | Iustin Pop |
|
845 | a8083063 | Iustin Pop | This is a no-op for the LV device type.
|
846 | a8083063 | Iustin Pop |
|
847 | a8083063 | Iustin Pop | """
|
848 | fdbd668d | Iustin Pop | pass
|
849 | a8083063 | Iustin Pop | |
850 | a8083063 | Iustin Pop | def Snapshot(self, size): |
851 | a8083063 | Iustin Pop | """Create a snapshot copy of an lvm block device.
|
852 | a8083063 | Iustin Pop |
|
853 | 800ac399 | Iustin Pop | @returns: tuple (vg, lv)
|
854 | 800ac399 | Iustin Pop |
|
855 | a8083063 | Iustin Pop | """
|
856 | a8083063 | Iustin Pop | snap_name = self._lv_name + ".snap" |
857 | a8083063 | Iustin Pop | |
858 | a8083063 | Iustin Pop | # remove existing snapshot if found
|
859 | 94dcbdb0 | Andrea Spadaccini | snap = LogicalVolume((self._vg_name, snap_name), None, size, self.params) |
860 | 0c6c04ec | Iustin Pop | _IgnoreError(snap.Remove) |
861 | a8083063 | Iustin Pop | |
862 | 197478f2 | René Nussbaumer | vg_info = self.GetVGInfo([self._vg_name]) |
863 | 197478f2 | René Nussbaumer | if not vg_info: |
864 | 197478f2 | René Nussbaumer | _ThrowError("Can't compute VG info for vg %s", self._vg_name) |
865 | 673cd9c4 | René Nussbaumer | free_size, _, _ = vg_info[0]
|
866 | a8083063 | Iustin Pop | if free_size < size:
|
867 | 82463074 | Iustin Pop | _ThrowError("Not enough free space: required %s,"
|
868 | 82463074 | Iustin Pop | " available %s", size, free_size)
|
869 | a8083063 | Iustin Pop | |
870 | e398546b | Iustin Pop | _CheckResult(utils.RunCmd(["lvcreate", "-L%dm" % size, "-s", |
871 | e398546b | Iustin Pop | "-n%s" % snap_name, self.dev_path])) |
872 | a8083063 | Iustin Pop | |
873 | 800ac399 | Iustin Pop | return (self._vg_name, snap_name) |
874 | a8083063 | Iustin Pop | |
875 | a1556cfa | Iustin Pop | def _RemoveOldInfo(self): |
876 | a1556cfa | Iustin Pop | """Try to remove old tags from the lv.
|
877 | a1556cfa | Iustin Pop |
|
878 | a1556cfa | Iustin Pop | """
|
879 | a1556cfa | Iustin Pop | result = utils.RunCmd(["lvs", "-o", "tags", "--noheadings", "--nosuffix", |
880 | a1556cfa | Iustin Pop | self.dev_path])
|
881 | a1556cfa | Iustin Pop | _CheckResult(result) |
882 | a1556cfa | Iustin Pop | |
883 | a1556cfa | Iustin Pop | raw_tags = result.stdout.strip() |
884 | a1556cfa | Iustin Pop | if raw_tags:
|
885 | a1556cfa | Iustin Pop | for tag in raw_tags.split(","): |
886 | a1556cfa | Iustin Pop | _CheckResult(utils.RunCmd(["lvchange", "--deltag", |
887 | a1556cfa | Iustin Pop | tag.strip(), self.dev_path]))
|
888 | a1556cfa | Iustin Pop | |
889 | a0c3fea1 | Michael Hanselmann | def SetInfo(self, text): |
890 | a0c3fea1 | Michael Hanselmann | """Update metadata with info text.
|
891 | a0c3fea1 | Michael Hanselmann |
|
892 | a0c3fea1 | Michael Hanselmann | """
|
893 | a0c3fea1 | Michael Hanselmann | BlockDev.SetInfo(self, text)
|
894 | a0c3fea1 | Michael Hanselmann | |
895 | a1556cfa | Iustin Pop | self._RemoveOldInfo()
|
896 | a1556cfa | Iustin Pop | |
897 | a0c3fea1 | Michael Hanselmann | # Replace invalid characters
|
898 | d0c8c01d | Iustin Pop | text = re.sub("^[^A-Za-z0-9_+.]", "_", text) |
899 | d0c8c01d | Iustin Pop | text = re.sub("[^-A-Za-z0-9_+.]", "_", text) |
900 | a0c3fea1 | Michael Hanselmann | |
901 | a0c3fea1 | Michael Hanselmann | # Only up to 128 characters are allowed
|
902 | a0c3fea1 | Michael Hanselmann | text = text[:128]
|
903 | a0c3fea1 | Michael Hanselmann | |
904 | e398546b | Iustin Pop | _CheckResult(utils.RunCmd(["lvchange", "--addtag", text, self.dev_path])) |
905 | 82463074 | Iustin Pop | |
906 | cad0723b | Iustin Pop | def Grow(self, amount, dryrun, backingstore): |
907 | 1005d816 | Iustin Pop | """Grow the logical volume.
|
908 | 1005d816 | Iustin Pop |
|
909 | 1005d816 | Iustin Pop | """
|
910 | cad0723b | Iustin Pop | if not backingstore: |
911 | cad0723b | Iustin Pop | return
|
912 | 38256320 | Iustin Pop | if self.pe_size is None or self.stripe_count is None: |
913 | 38256320 | Iustin Pop | if not self.Attach(): |
914 | 38256320 | Iustin Pop | _ThrowError("Can't attach to LV during Grow()")
|
915 | 38256320 | Iustin Pop | full_stripe_size = self.pe_size * self.stripe_count |
916 | 38256320 | Iustin Pop | rest = amount % full_stripe_size |
917 | 38256320 | Iustin Pop | if rest != 0: |
918 | 38256320 | Iustin Pop | amount += full_stripe_size - rest |
919 | 7fe23d47 | Iustin Pop | cmd = ["lvextend", "-L", "+%dm" % amount] |
920 | 7fe23d47 | Iustin Pop | if dryrun:
|
921 | 7fe23d47 | Iustin Pop | cmd.append("--test")
|
922 | 1005d816 | Iustin Pop | # we try multiple algorithms since the 'best' ones might not have
|
923 | 1005d816 | Iustin Pop | # space available in the right place, but later ones might (since
|
924 | 1005d816 | Iustin Pop | # they have less constraints); also note that only recent LVM
|
925 | 1005d816 | Iustin Pop | # supports 'cling'
|
926 | 1005d816 | Iustin Pop | for alloc_policy in "contiguous", "cling", "normal": |
927 | 7fe23d47 | Iustin Pop | result = utils.RunCmd(cmd + ["--alloc", alloc_policy, self.dev_path]) |
928 | 1005d816 | Iustin Pop | if not result.failed: |
929 | 1005d816 | Iustin Pop | return
|
930 | 82463074 | Iustin Pop | _ThrowError("Can't grow LV %s: %s", self.dev_path, result.output) |
931 | a0c3fea1 | Michael Hanselmann | |
932 | a0c3fea1 | Michael Hanselmann | |
933 | 6b90c22e | Iustin Pop | class DRBD8Status(object): |
934 | 6b90c22e | Iustin Pop | """A DRBD status representation class.
|
935 | 6b90c22e | Iustin Pop |
|
936 | 6b90c22e | Iustin Pop | Note that this doesn't support unconfigured devices (cs:Unconfigured).
|
937 | 6b90c22e | Iustin Pop |
|
938 | 6b90c22e | Iustin Pop | """
|
939 | 767d52d3 | Iustin Pop | UNCONF_RE = re.compile(r"\s*[0-9]+:\s*cs:Unconfigured$")
|
940 | 01e2ce3a | Iustin Pop | LINE_RE = re.compile(r"\s*[0-9]+:\s*cs:(\S+)\s+(?:st|ro):([^/]+)/(\S+)"
|
941 | 6b90c22e | Iustin Pop | "\s+ds:([^/]+)/(\S+)\s+.*$")
|
942 | 6b90c22e | Iustin Pop | SYNC_RE = re.compile(r"^.*\ssync'ed:\s*([0-9.]+)%.*"
|
943 | 7a380ddf | René Nussbaumer | # Due to a bug in drbd in the kernel, introduced in
|
944 | 7a380ddf | René Nussbaumer | # commit 4b0715f096 (still unfixed as of 2011-08-22)
|
945 | 7a380ddf | René Nussbaumer | "(?:\s|M)"
|
946 | 7a380ddf | René Nussbaumer | "finish: ([0-9]+):([0-9]+):([0-9]+)\s.*$")
|
947 | 6b90c22e | Iustin Pop | |
948 | 3c003d9d | Iustin Pop | CS_UNCONFIGURED = "Unconfigured"
|
949 | 3c003d9d | Iustin Pop | CS_STANDALONE = "StandAlone"
|
950 | 3c003d9d | Iustin Pop | CS_WFCONNECTION = "WFConnection"
|
951 | 3c003d9d | Iustin Pop | CS_WFREPORTPARAMS = "WFReportParams"
|
952 | 3c003d9d | Iustin Pop | CS_CONNECTED = "Connected"
|
953 | 3c003d9d | Iustin Pop | CS_STARTINGSYNCS = "StartingSyncS"
|
954 | 3c003d9d | Iustin Pop | CS_STARTINGSYNCT = "StartingSyncT"
|
955 | 3c003d9d | Iustin Pop | CS_WFBITMAPS = "WFBitMapS"
|
956 | 3c003d9d | Iustin Pop | CS_WFBITMAPT = "WFBitMapT"
|
957 | 3c003d9d | Iustin Pop | CS_WFSYNCUUID = "WFSyncUUID"
|
958 | 3c003d9d | Iustin Pop | CS_SYNCSOURCE = "SyncSource"
|
959 | 3c003d9d | Iustin Pop | CS_SYNCTARGET = "SyncTarget"
|
960 | 3c003d9d | Iustin Pop | CS_PAUSEDSYNCS = "PausedSyncS"
|
961 | 3c003d9d | Iustin Pop | CS_PAUSEDSYNCT = "PausedSyncT"
|
962 | 3c003d9d | Iustin Pop | CSET_SYNC = frozenset([
|
963 | 3c003d9d | Iustin Pop | CS_WFREPORTPARAMS, |
964 | 3c003d9d | Iustin Pop | CS_STARTINGSYNCS, |
965 | 3c003d9d | Iustin Pop | CS_STARTINGSYNCT, |
966 | 3c003d9d | Iustin Pop | CS_WFBITMAPS, |
967 | 3c003d9d | Iustin Pop | CS_WFBITMAPT, |
968 | 3c003d9d | Iustin Pop | CS_WFSYNCUUID, |
969 | 3c003d9d | Iustin Pop | CS_SYNCSOURCE, |
970 | 3c003d9d | Iustin Pop | CS_SYNCTARGET, |
971 | 3c003d9d | Iustin Pop | CS_PAUSEDSYNCS, |
972 | 3c003d9d | Iustin Pop | CS_PAUSEDSYNCT, |
973 | 3c003d9d | Iustin Pop | ]) |
974 | 3c003d9d | Iustin Pop | |
975 | 3c003d9d | Iustin Pop | DS_DISKLESS = "Diskless"
|
976 | 3c003d9d | Iustin Pop | DS_ATTACHING = "Attaching" # transient state |
977 | 3c003d9d | Iustin Pop | DS_FAILED = "Failed" # transient state, next: diskless |
978 | 3c003d9d | Iustin Pop | DS_NEGOTIATING = "Negotiating" # transient state |
979 | 3c003d9d | Iustin Pop | DS_INCONSISTENT = "Inconsistent" # while syncing or after creation |
980 | 3c003d9d | Iustin Pop | DS_OUTDATED = "Outdated"
|
981 | 3c003d9d | Iustin Pop | DS_DUNKNOWN = "DUnknown" # shown for peer disk when not connected |
982 | 3c003d9d | Iustin Pop | DS_CONSISTENT = "Consistent"
|
983 | 3c003d9d | Iustin Pop | DS_UPTODATE = "UpToDate" # normal state |
984 | 3c003d9d | Iustin Pop | |
985 | 3c003d9d | Iustin Pop | RO_PRIMARY = "Primary"
|
986 | 3c003d9d | Iustin Pop | RO_SECONDARY = "Secondary"
|
987 | 3c003d9d | Iustin Pop | RO_UNKNOWN = "Unknown"
|
988 | 3c003d9d | Iustin Pop | |
989 | 6b90c22e | Iustin Pop | def __init__(self, procline): |
990 | 767d52d3 | Iustin Pop | u = self.UNCONF_RE.match(procline)
|
991 | 767d52d3 | Iustin Pop | if u:
|
992 | 3c003d9d | Iustin Pop | self.cstatus = self.CS_UNCONFIGURED |
993 | 767d52d3 | Iustin Pop | self.lrole = self.rrole = self.ldisk = self.rdisk = None |
994 | 767d52d3 | Iustin Pop | else:
|
995 | 767d52d3 | Iustin Pop | m = self.LINE_RE.match(procline)
|
996 | 767d52d3 | Iustin Pop | if not m: |
997 | 767d52d3 | Iustin Pop | raise errors.BlockDeviceError("Can't parse input data '%s'" % procline) |
998 | 767d52d3 | Iustin Pop | self.cstatus = m.group(1) |
999 | 767d52d3 | Iustin Pop | self.lrole = m.group(2) |
1000 | 767d52d3 | Iustin Pop | self.rrole = m.group(3) |
1001 | 767d52d3 | Iustin Pop | self.ldisk = m.group(4) |
1002 | 767d52d3 | Iustin Pop | self.rdisk = m.group(5) |
1003 | 767d52d3 | Iustin Pop | |
1004 | 767d52d3 | Iustin Pop | # end reading of data from the LINE_RE or UNCONF_RE
|
1005 | 6b90c22e | Iustin Pop | |
1006 | 3c003d9d | Iustin Pop | self.is_standalone = self.cstatus == self.CS_STANDALONE |
1007 | 3c003d9d | Iustin Pop | self.is_wfconn = self.cstatus == self.CS_WFCONNECTION |
1008 | 3c003d9d | Iustin Pop | self.is_connected = self.cstatus == self.CS_CONNECTED |
1009 | 3c003d9d | Iustin Pop | self.is_primary = self.lrole == self.RO_PRIMARY |
1010 | 3c003d9d | Iustin Pop | self.is_secondary = self.lrole == self.RO_SECONDARY |
1011 | 3c003d9d | Iustin Pop | self.peer_primary = self.rrole == self.RO_PRIMARY |
1012 | 3c003d9d | Iustin Pop | self.peer_secondary = self.rrole == self.RO_SECONDARY |
1013 | 6b90c22e | Iustin Pop | self.both_primary = self.is_primary and self.peer_primary |
1014 | 6b90c22e | Iustin Pop | self.both_secondary = self.is_secondary and self.peer_secondary |
1015 | 6b90c22e | Iustin Pop | |
1016 | 3c003d9d | Iustin Pop | self.is_diskless = self.ldisk == self.DS_DISKLESS |
1017 | 3c003d9d | Iustin Pop | self.is_disk_uptodate = self.ldisk == self.DS_UPTODATE |
1018 | 6b90c22e | Iustin Pop | |
1019 | 3c003d9d | Iustin Pop | self.is_in_resync = self.cstatus in self.CSET_SYNC |
1020 | 3c003d9d | Iustin Pop | self.is_in_use = self.cstatus != self.CS_UNCONFIGURED |
1021 | 6b93ec9d | Iustin Pop | |
1022 | 6b90c22e | Iustin Pop | m = self.SYNC_RE.match(procline)
|
1023 | 6b90c22e | Iustin Pop | if m:
|
1024 | 6b90c22e | Iustin Pop | self.sync_percent = float(m.group(1)) |
1025 | 6b90c22e | Iustin Pop | hours = int(m.group(2)) |
1026 | 6b90c22e | Iustin Pop | minutes = int(m.group(3)) |
1027 | 6b90c22e | Iustin Pop | seconds = int(m.group(4)) |
1028 | 6b90c22e | Iustin Pop | self.est_time = hours * 3600 + minutes * 60 + seconds |
1029 | 6b90c22e | Iustin Pop | else:
|
1030 | 3c003d9d | Iustin Pop | # we have (in this if branch) no percent information, but if
|
1031 | 3c003d9d | Iustin Pop | # we're resyncing we need to 'fake' a sync percent information,
|
1032 | 3c003d9d | Iustin Pop | # as this is how cmdlib determines if it makes sense to wait for
|
1033 | 3c003d9d | Iustin Pop | # resyncing or not
|
1034 | 3c003d9d | Iustin Pop | if self.is_in_resync: |
1035 | 3c003d9d | Iustin Pop | self.sync_percent = 0 |
1036 | 3c003d9d | Iustin Pop | else:
|
1037 | 3c003d9d | Iustin Pop | self.sync_percent = None |
1038 | 6b90c22e | Iustin Pop | self.est_time = None |
1039 | 6b90c22e | Iustin Pop | |
1040 | 6b90c22e | Iustin Pop | |
1041 | b459a848 | Andrea Spadaccini | class BaseDRBD(BlockDev): # pylint: disable=W0223 |
1042 | 0f7f32d9 | Iustin Pop | """Base DRBD class.
|
1043 | a8083063 | Iustin Pop |
|
1044 | 0f7f32d9 | Iustin Pop | This class contains a few bits of common functionality between the
|
1045 | 0f7f32d9 | Iustin Pop | 0.7 and 8.x versions of DRBD.
|
1046 | 0f7f32d9 | Iustin Pop |
|
1047 | abdf0113 | Iustin Pop | """
|
1048 | fcee765d | Manuel Franceschini | _VERSION_RE = re.compile(r"^version: (\d+)\.(\d+)\.(\d+)(?:\.\d+)?"
|
1049 | abdf0113 | Iustin Pop | r" \(api:(\d+)/proto:(\d+)(?:-(\d+))?\)")
|
1050 | 9122e60a | Iustin Pop | _VALID_LINE_RE = re.compile("^ *([0-9]+): cs:([^ ]+).*$")
|
1051 | 9122e60a | Iustin Pop | _UNUSED_LINE_RE = re.compile("^ *([0-9]+): cs:Unconfigured$")
|
1052 | a8083063 | Iustin Pop | |
1053 | abdf0113 | Iustin Pop | _DRBD_MAJOR = 147
|
1054 | abdf0113 | Iustin Pop | _ST_UNCONFIGURED = "Unconfigured"
|
1055 | abdf0113 | Iustin Pop | _ST_WFCONNECTION = "WFConnection"
|
1056 | abdf0113 | Iustin Pop | _ST_CONNECTED = "Connected"
|
1057 | a8083063 | Iustin Pop | |
1058 | 6b90c22e | Iustin Pop | _STATUS_FILE = "/proc/drbd"
|
1059 | 549071a0 | Luca Bigliardi | _USERMODE_HELPER_FILE = "/sys/module/drbd/parameters/usermode_helper"
|
1060 | 6b90c22e | Iustin Pop | |
1061 | abdf0113 | Iustin Pop | @staticmethod
|
1062 | 6b90c22e | Iustin Pop | def _GetProcData(filename=_STATUS_FILE): |
1063 | abdf0113 | Iustin Pop | """Return data from /proc/drbd.
|
1064 | a8083063 | Iustin Pop |
|
1065 | a8083063 | Iustin Pop | """
|
1066 | abdf0113 | Iustin Pop | try:
|
1067 | 13998ef2 | Michael Hanselmann | data = utils.ReadFile(filename).splitlines() |
1068 | f6eaed12 | Iustin Pop | except EnvironmentError, err: |
1069 | f6eaed12 | Iustin Pop | if err.errno == errno.ENOENT:
|
1070 | f6eaed12 | Iustin Pop | _ThrowError("The file %s cannot be opened, check if the module"
|
1071 | f6eaed12 | Iustin Pop | " is loaded (%s)", filename, str(err)) |
1072 | f6eaed12 | Iustin Pop | else:
|
1073 | f6eaed12 | Iustin Pop | _ThrowError("Can't read the DRBD proc file %s: %s", filename, str(err)) |
1074 | abdf0113 | Iustin Pop | if not data: |
1075 | 82463074 | Iustin Pop | _ThrowError("Can't read any data from %s", filename)
|
1076 | abdf0113 | Iustin Pop | return data
|
1077 | a8083063 | Iustin Pop | |
1078 | 9122e60a | Iustin Pop | @classmethod
|
1079 | 9122e60a | Iustin Pop | def _MassageProcData(cls, data): |
1080 | abdf0113 | Iustin Pop | """Transform the output of _GetProdData into a nicer form.
|
1081 | a8083063 | Iustin Pop |
|
1082 | c41eea6e | Iustin Pop | @return: a dictionary of minor: joined lines from /proc/drbd
|
1083 | c41eea6e | Iustin Pop | for that minor
|
1084 | a8083063 | Iustin Pop |
|
1085 | a8083063 | Iustin Pop | """
|
1086 | abdf0113 | Iustin Pop | results = {} |
1087 | abdf0113 | Iustin Pop | old_minor = old_line = None
|
1088 | abdf0113 | Iustin Pop | for line in data: |
1089 | 67d101d4 | Iustin Pop | if not line: # completely empty lines, as can be returned by drbd8.0+ |
1090 | 67d101d4 | Iustin Pop | continue
|
1091 | 9122e60a | Iustin Pop | lresult = cls._VALID_LINE_RE.match(line) |
1092 | abdf0113 | Iustin Pop | if lresult is not None: |
1093 | abdf0113 | Iustin Pop | if old_minor is not None: |
1094 | abdf0113 | Iustin Pop | results[old_minor] = old_line |
1095 | abdf0113 | Iustin Pop | old_minor = int(lresult.group(1)) |
1096 | abdf0113 | Iustin Pop | old_line = line |
1097 | abdf0113 | Iustin Pop | else:
|
1098 | abdf0113 | Iustin Pop | if old_minor is not None: |
1099 | abdf0113 | Iustin Pop | old_line += " " + line.strip()
|
1100 | abdf0113 | Iustin Pop | # add last line
|
1101 | abdf0113 | Iustin Pop | if old_minor is not None: |
1102 | abdf0113 | Iustin Pop | results[old_minor] = old_line |
1103 | abdf0113 | Iustin Pop | return results
|
1104 | a8083063 | Iustin Pop | |
1105 | abdf0113 | Iustin Pop | @classmethod
|
1106 | fcee765d | Manuel Franceschini | def _GetVersion(cls, proc_data): |
1107 | abdf0113 | Iustin Pop | """Return the DRBD version.
|
1108 | a8083063 | Iustin Pop |
|
1109 | abdf0113 | Iustin Pop | This will return a dict with keys:
|
1110 | c41eea6e | Iustin Pop | - k_major
|
1111 | c41eea6e | Iustin Pop | - k_minor
|
1112 | c41eea6e | Iustin Pop | - k_point
|
1113 | c41eea6e | Iustin Pop | - api
|
1114 | c41eea6e | Iustin Pop | - proto
|
1115 | c41eea6e | Iustin Pop | - proto2 (only on drbd > 8.2.X)
|
1116 | a8083063 | Iustin Pop |
|
1117 | a8083063 | Iustin Pop | """
|
1118 | abdf0113 | Iustin Pop | first_line = proc_data[0].strip()
|
1119 | abdf0113 | Iustin Pop | version = cls._VERSION_RE.match(first_line) |
1120 | abdf0113 | Iustin Pop | if not version: |
1121 | abdf0113 | Iustin Pop | raise errors.BlockDeviceError("Can't parse DRBD version from '%s'" % |
1122 | abdf0113 | Iustin Pop | first_line) |
1123 | a8083063 | Iustin Pop | |
1124 | abdf0113 | Iustin Pop | values = version.groups() |
1125 | 5ae4945a | Iustin Pop | retval = { |
1126 | 5ae4945a | Iustin Pop | "k_major": int(values[0]), |
1127 | 5ae4945a | Iustin Pop | "k_minor": int(values[1]), |
1128 | 5ae4945a | Iustin Pop | "k_point": int(values[2]), |
1129 | 5ae4945a | Iustin Pop | "api": int(values[3]), |
1130 | 5ae4945a | Iustin Pop | "proto": int(values[4]), |
1131 | 5ae4945a | Iustin Pop | } |
1132 | abdf0113 | Iustin Pop | if values[5] is not None: |
1133 | d0c8c01d | Iustin Pop | retval["proto2"] = values[5] |
1134 | a8083063 | Iustin Pop | |
1135 | abdf0113 | Iustin Pop | return retval
|
1136 | abdf0113 | Iustin Pop | |
1137 | abdf0113 | Iustin Pop | @staticmethod
|
1138 | 549071a0 | Luca Bigliardi | def GetUsermodeHelper(filename=_USERMODE_HELPER_FILE): |
1139 | 549071a0 | Luca Bigliardi | """Returns DRBD usermode_helper currently set.
|
1140 | 549071a0 | Luca Bigliardi |
|
1141 | 549071a0 | Luca Bigliardi | """
|
1142 | 549071a0 | Luca Bigliardi | try:
|
1143 | 549071a0 | Luca Bigliardi | helper = utils.ReadFile(filename).splitlines()[0]
|
1144 | 549071a0 | Luca Bigliardi | except EnvironmentError, err: |
1145 | 549071a0 | Luca Bigliardi | if err.errno == errno.ENOENT:
|
1146 | 549071a0 | Luca Bigliardi | _ThrowError("The file %s cannot be opened, check if the module"
|
1147 | 549071a0 | Luca Bigliardi | " is loaded (%s)", filename, str(err)) |
1148 | 549071a0 | Luca Bigliardi | else:
|
1149 | 549071a0 | Luca Bigliardi | _ThrowError("Can't read DRBD helper file %s: %s", filename, str(err)) |
1150 | 549071a0 | Luca Bigliardi | if not helper: |
1151 | 549071a0 | Luca Bigliardi | _ThrowError("Can't read any data from %s", filename)
|
1152 | 549071a0 | Luca Bigliardi | return helper
|
1153 | 549071a0 | Luca Bigliardi | |
1154 | 549071a0 | Luca Bigliardi | @staticmethod
|
1155 | abdf0113 | Iustin Pop | def _DevPath(minor): |
1156 | abdf0113 | Iustin Pop | """Return the path to a drbd device for a given minor.
|
1157 | a8083063 | Iustin Pop |
|
1158 | a8083063 | Iustin Pop | """
|
1159 | abdf0113 | Iustin Pop | return "/dev/drbd%d" % minor |
1160 | a8083063 | Iustin Pop | |
1161 | abdf0113 | Iustin Pop | @classmethod
|
1162 | 6d2e83d5 | Iustin Pop | def GetUsedDevs(cls): |
1163 | abdf0113 | Iustin Pop | """Compute the list of used DRBD devices.
|
1164 | a8083063 | Iustin Pop |
|
1165 | a8083063 | Iustin Pop | """
|
1166 | abdf0113 | Iustin Pop | data = cls._GetProcData() |
1167 | a8083063 | Iustin Pop | |
1168 | abdf0113 | Iustin Pop | used_devs = {} |
1169 | abdf0113 | Iustin Pop | for line in data: |
1170 | 9122e60a | Iustin Pop | match = cls._VALID_LINE_RE.match(line) |
1171 | abdf0113 | Iustin Pop | if not match: |
1172 | abdf0113 | Iustin Pop | continue
|
1173 | abdf0113 | Iustin Pop | minor = int(match.group(1)) |
1174 | abdf0113 | Iustin Pop | state = match.group(2)
|
1175 | abdf0113 | Iustin Pop | if state == cls._ST_UNCONFIGURED:
|
1176 | abdf0113 | Iustin Pop | continue
|
1177 | abdf0113 | Iustin Pop | used_devs[minor] = state, line |
1178 | a8083063 | Iustin Pop | |
1179 | abdf0113 | Iustin Pop | return used_devs
|
1180 | a8083063 | Iustin Pop | |
1181 | abdf0113 | Iustin Pop | def _SetFromMinor(self, minor): |
1182 | abdf0113 | Iustin Pop | """Set our parameters based on the given minor.
|
1183 | 0834c866 | Iustin Pop |
|
1184 | abdf0113 | Iustin Pop | This sets our minor variable and our dev_path.
|
1185 | a8083063 | Iustin Pop |
|
1186 | a8083063 | Iustin Pop | """
|
1187 | abdf0113 | Iustin Pop | if minor is None: |
1188 | abdf0113 | Iustin Pop | self.minor = self.dev_path = None |
1189 | cb999543 | Iustin Pop | self.attached = False |
1190 | a8083063 | Iustin Pop | else:
|
1191 | abdf0113 | Iustin Pop | self.minor = minor
|
1192 | abdf0113 | Iustin Pop | self.dev_path = self._DevPath(minor) |
1193 | cb999543 | Iustin Pop | self.attached = True |
1194 | a8083063 | Iustin Pop | |
1195 | a8083063 | Iustin Pop | @staticmethod
|
1196 | abdf0113 | Iustin Pop | def _CheckMetaSize(meta_device): |
1197 | abdf0113 | Iustin Pop | """Check if the given meta device looks like a valid one.
|
1198 | a8083063 | Iustin Pop |
|
1199 | 3bc145d8 | Bernardo Dal Seno | This currently only checks the size, which must be around
|
1200 | abdf0113 | Iustin Pop | 128MiB.
|
1201 | a8083063 | Iustin Pop |
|
1202 | a8083063 | Iustin Pop | """
|
1203 | abdf0113 | Iustin Pop | result = utils.RunCmd(["blockdev", "--getsize", meta_device]) |
1204 | abdf0113 | Iustin Pop | if result.failed:
|
1205 | 9c793cfb | Iustin Pop | _ThrowError("Failed to get device size: %s - %s",
|
1206 | 9c793cfb | Iustin Pop | result.fail_reason, result.output) |
1207 | a8083063 | Iustin Pop | try:
|
1208 | abdf0113 | Iustin Pop | sectors = int(result.stdout)
|
1209 | 691744c4 | Iustin Pop | except (TypeError, ValueError): |
1210 | 9c793cfb | Iustin Pop | _ThrowError("Invalid output from blockdev: '%s'", result.stdout)
|
1211 | c04bc777 | Iustin Pop | num_bytes = sectors * 512
|
1212 | c04bc777 | Iustin Pop | if num_bytes < 128 * 1024 * 1024: # less than 128MiB |
1213 | c04bc777 | Iustin Pop | _ThrowError("Meta device too small (%.2fMib)", (num_bytes / 1024 / 1024)) |
1214 | 1dc10972 | Iustin Pop | # the maximum *valid* size of the meta device when living on top
|
1215 | 1dc10972 | Iustin Pop | # of LVM is hard to compute: it depends on the number of stripes
|
1216 | 1dc10972 | Iustin Pop | # and the PE size; e.g. a 2-stripe, 64MB PE will result in a 128MB
|
1217 | 1dc10972 | Iustin Pop | # (normal size), but an eight-stripe 128MB PE will result in a 1GB
|
1218 | 1dc10972 | Iustin Pop | # size meta device; as such, we restrict it to 1GB (a little bit
|
1219 | 1dc10972 | Iustin Pop | # too generous, but making assumptions about PE size is hard)
|
1220 | c04bc777 | Iustin Pop | if num_bytes > 1024 * 1024 * 1024: |
1221 | c04bc777 | Iustin Pop | _ThrowError("Meta device too big (%.2fMiB)", (num_bytes / 1024 / 1024)) |
1222 | a8083063 | Iustin Pop | |
1223 | abdf0113 | Iustin Pop | def Rename(self, new_id): |
1224 | abdf0113 | Iustin Pop | """Rename a device.
|
1225 | a8083063 | Iustin Pop |
|
1226 | abdf0113 | Iustin Pop | This is not supported for drbd devices.
|
1227 | a8083063 | Iustin Pop |
|
1228 | a8083063 | Iustin Pop | """
|
1229 | abdf0113 | Iustin Pop | raise errors.ProgrammerError("Can't rename a drbd device") |
1230 | a8083063 | Iustin Pop | |
1231 | f3e513ad | Iustin Pop | |
1232 | a2cfdea2 | Iustin Pop | class DRBD8(BaseDRBD): |
1233 | a2cfdea2 | Iustin Pop | """DRBD v8.x block device.
|
1234 | a2cfdea2 | Iustin Pop |
|
1235 | a2cfdea2 | Iustin Pop | This implements the local host part of the DRBD device, i.e. it
|
1236 | a2cfdea2 | Iustin Pop | doesn't do anything to the supposed peer. If you need a fully
|
1237 | a2cfdea2 | Iustin Pop | connected DRBD pair, you need to use this class on both hosts.
|
1238 | a2cfdea2 | Iustin Pop |
|
1239 | 5c755a4d | Iustin Pop | The unique_id for the drbd device is a (local_ip, local_port,
|
1240 | 5c755a4d | Iustin Pop | remote_ip, remote_port, local_minor, secret) tuple, and it must have
|
1241 | 5c755a4d | Iustin Pop | two children: the data device and the meta_device. The meta device
|
1242 | 5c755a4d | Iustin Pop | is checked for valid size and is zeroed on create.
|
1243 | a2cfdea2 | Iustin Pop |
|
1244 | a2cfdea2 | Iustin Pop | """
|
1245 | a2cfdea2 | Iustin Pop | _MAX_MINORS = 255
|
1246 | a2cfdea2 | Iustin Pop | _PARSE_SHOW = None
|
1247 | a2cfdea2 | Iustin Pop | |
1248 | cf8df3f3 | Iustin Pop | # timeout constants
|
1249 | cf8df3f3 | Iustin Pop | _NET_RECONFIG_TIMEOUT = 60
|
1250 | cf8df3f3 | Iustin Pop | |
1251 | 8a69b3a8 | Andrea Spadaccini | # command line options for barriers
|
1252 | 8a69b3a8 | Andrea Spadaccini | _DISABLE_DISK_OPTION = "--no-disk-barrier" # -a |
1253 | 8a69b3a8 | Andrea Spadaccini | _DISABLE_DRAIN_OPTION = "--no-disk-drain" # -D |
1254 | 8a69b3a8 | Andrea Spadaccini | _DISABLE_FLUSH_OPTION = "--no-disk-flushes" # -i |
1255 | 8a69b3a8 | Andrea Spadaccini | _DISABLE_META_FLUSH_OPTION = "--no-md-flushes" # -m |
1256 | 8a69b3a8 | Andrea Spadaccini | |
1257 | 94dcbdb0 | Andrea Spadaccini | def __init__(self, unique_id, children, size, params): |
1258 | fc1dc9d7 | Iustin Pop | if children and children.count(None) > 0: |
1259 | fc1dc9d7 | Iustin Pop | children = [] |
1260 | 310fbb64 | Iustin Pop | if len(children) not in (0, 2): |
1261 | 310fbb64 | Iustin Pop | raise ValueError("Invalid configuration data %s" % str(children)) |
1262 | 310fbb64 | Iustin Pop | if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 6: |
1263 | 310fbb64 | Iustin Pop | raise ValueError("Invalid configuration data %s" % str(unique_id)) |
1264 | 310fbb64 | Iustin Pop | (self._lhost, self._lport, |
1265 | 310fbb64 | Iustin Pop | self._rhost, self._rport, |
1266 | 310fbb64 | Iustin Pop | self._aminor, self._secret) = unique_id |
1267 | 310fbb64 | Iustin Pop | if children:
|
1268 | 310fbb64 | Iustin Pop | if not _CanReadDevice(children[1].dev_path): |
1269 | 310fbb64 | Iustin Pop | logging.info("drbd%s: Ignoring unreadable meta device", self._aminor) |
1270 | 310fbb64 | Iustin Pop | children = [] |
1271 | 94dcbdb0 | Andrea Spadaccini | super(DRBD8, self).__init__(unique_id, children, size, params) |
1272 | a2cfdea2 | Iustin Pop | self.major = self._DRBD_MAJOR |
1273 | fcee765d | Manuel Franceschini | version = self._GetVersion(self._GetProcData()) |
1274 | e687ec01 | Michael Hanselmann | if version["k_major"] != 8: |
1275 | 82463074 | Iustin Pop | _ThrowError("Mismatch in DRBD kernel version and requested ganeti"
|
1276 | 82463074 | Iustin Pop | " usage: kernel is %s.%s, ganeti wants 8.x",
|
1277 | d0c8c01d | Iustin Pop | version["k_major"], version["k_minor"]) |
1278 | a2cfdea2 | Iustin Pop | |
1279 | ffa1c0dc | Iustin Pop | if (self._lhost is not None and self._lhost == self._rhost and |
1280 | ffa1c0dc | Iustin Pop | self._lport == self._rport): |
1281 | ffa1c0dc | Iustin Pop | raise ValueError("Invalid configuration data, same local/remote %s" % |
1282 | ffa1c0dc | Iustin Pop | (unique_id,)) |
1283 | a2cfdea2 | Iustin Pop | self.Attach()
|
1284 | a2cfdea2 | Iustin Pop | |
1285 | a2cfdea2 | Iustin Pop | @classmethod
|
1286 | a2cfdea2 | Iustin Pop | def _InitMeta(cls, minor, dev_path): |
1287 | a2cfdea2 | Iustin Pop | """Initialize a meta device.
|
1288 | a2cfdea2 | Iustin Pop |
|
1289 | a2cfdea2 | Iustin Pop | This will not work if the given minor is in use.
|
1290 | a2cfdea2 | Iustin Pop |
|
1291 | a2cfdea2 | Iustin Pop | """
|
1292 | 18e4dee6 | Iustin Pop | # Zero the metadata first, in order to make sure drbdmeta doesn't
|
1293 | 18e4dee6 | Iustin Pop | # try to auto-detect existing filesystems or similar (see
|
1294 | 18e4dee6 | Iustin Pop | # http://code.google.com/p/ganeti/issues/detail?id=182); we only
|
1295 | 18e4dee6 | Iustin Pop | # care about the first 128MB of data in the device, even though it
|
1296 | 18e4dee6 | Iustin Pop | # can be bigger
|
1297 | 18e4dee6 | Iustin Pop | result = utils.RunCmd([constants.DD_CMD, |
1298 | 18e4dee6 | Iustin Pop | "if=/dev/zero", "of=%s" % dev_path, |
1299 | 18e4dee6 | Iustin Pop | "bs=1048576", "count=128", "oflag=direct"]) |
1300 | 18e4dee6 | Iustin Pop | if result.failed:
|
1301 | 18e4dee6 | Iustin Pop | _ThrowError("Can't wipe the meta device: %s", result.output)
|
1302 | 18e4dee6 | Iustin Pop | |
1303 | a2cfdea2 | Iustin Pop | result = utils.RunCmd(["drbdmeta", "--force", cls._DevPath(minor), |
1304 | a2cfdea2 | Iustin Pop | "v08", dev_path, "0", "create-md"]) |
1305 | a2cfdea2 | Iustin Pop | if result.failed:
|
1306 | 82463074 | Iustin Pop | _ThrowError("Can't initialize meta device: %s", result.output)
|
1307 | a2cfdea2 | Iustin Pop | |
1308 | a2cfdea2 | Iustin Pop | @classmethod
|
1309 | a2cfdea2 | Iustin Pop | def _FindUnusedMinor(cls): |
1310 | a2cfdea2 | Iustin Pop | """Find an unused DRBD device.
|
1311 | a2cfdea2 | Iustin Pop |
|
1312 | a2cfdea2 | Iustin Pop | This is specific to 8.x as the minors are allocated dynamically,
|
1313 | a2cfdea2 | Iustin Pop | so non-existing numbers up to a max minor count are actually free.
|
1314 | a2cfdea2 | Iustin Pop |
|
1315 | a2cfdea2 | Iustin Pop | """
|
1316 | a2cfdea2 | Iustin Pop | data = cls._GetProcData() |
1317 | a2cfdea2 | Iustin Pop | |
1318 | a2cfdea2 | Iustin Pop | highest = None
|
1319 | a2cfdea2 | Iustin Pop | for line in data: |
1320 | 9122e60a | Iustin Pop | match = cls._UNUSED_LINE_RE.match(line) |
1321 | a2cfdea2 | Iustin Pop | if match:
|
1322 | a2cfdea2 | Iustin Pop | return int(match.group(1)) |
1323 | 9122e60a | Iustin Pop | match = cls._VALID_LINE_RE.match(line) |
1324 | a2cfdea2 | Iustin Pop | if match:
|
1325 | a2cfdea2 | Iustin Pop | minor = int(match.group(1)) |
1326 | a2cfdea2 | Iustin Pop | highest = max(highest, minor)
|
1327 | a2cfdea2 | Iustin Pop | if highest is None: # there are no minors in use at all |
1328 | a2cfdea2 | Iustin Pop | return 0 |
1329 | a2cfdea2 | Iustin Pop | if highest >= cls._MAX_MINORS:
|
1330 | 468c5f77 | Iustin Pop | logging.error("Error: no free drbd minors!")
|
1331 | a2cfdea2 | Iustin Pop | raise errors.BlockDeviceError("Can't find a free DRBD minor") |
1332 | a2cfdea2 | Iustin Pop | return highest + 1 |
1333 | a2cfdea2 | Iustin Pop | |
1334 | a2cfdea2 | Iustin Pop | @classmethod
|
1335 | a2cfdea2 | Iustin Pop | def _GetShowParser(cls): |
1336 | a2cfdea2 | Iustin Pop | """Return a parser for `drbd show` output.
|
1337 | a2cfdea2 | Iustin Pop |
|
1338 | 3bc145d8 | Bernardo Dal Seno | This will either create or return an already-created parser for the
|
1339 | a2cfdea2 | Iustin Pop | output of the command `drbd show`.
|
1340 | a2cfdea2 | Iustin Pop |
|
1341 | a2cfdea2 | Iustin Pop | """
|
1342 | a2cfdea2 | Iustin Pop | if cls._PARSE_SHOW is not None: |
1343 | a2cfdea2 | Iustin Pop | return cls._PARSE_SHOW
|
1344 | a2cfdea2 | Iustin Pop | |
1345 | a2cfdea2 | Iustin Pop | # pyparsing setup
|
1346 | a2cfdea2 | Iustin Pop | lbrace = pyp.Literal("{").suppress()
|
1347 | a2cfdea2 | Iustin Pop | rbrace = pyp.Literal("}").suppress()
|
1348 | 5a672c30 | Manuel Franceschini | lbracket = pyp.Literal("[").suppress()
|
1349 | 5a672c30 | Manuel Franceschini | rbracket = pyp.Literal("]").suppress()
|
1350 | a2cfdea2 | Iustin Pop | semi = pyp.Literal(";").suppress()
|
1351 | 5a672c30 | Manuel Franceschini | colon = pyp.Literal(":").suppress()
|
1352 | a2cfdea2 | Iustin Pop | # this also converts the value to an int
|
1353 | c522ea02 | Iustin Pop | number = pyp.Word(pyp.nums).setParseAction(lambda s, l, t: int(t[0])) |
1354 | a2cfdea2 | Iustin Pop | |
1355 | e687ec01 | Michael Hanselmann | comment = pyp.Literal("#") + pyp.Optional(pyp.restOfLine)
|
1356 | a2cfdea2 | Iustin Pop | defa = pyp.Literal("_is_default").suppress()
|
1357 | a2cfdea2 | Iustin Pop | dbl_quote = pyp.Literal('"').suppress()
|
1358 | a2cfdea2 | Iustin Pop | |
1359 | 3ccb3a64 | Michael Hanselmann | keyword = pyp.Word(pyp.alphanums + "-")
|
1360 | a2cfdea2 | Iustin Pop | |
1361 | a2cfdea2 | Iustin Pop | # value types
|
1362 | 3ccb3a64 | Michael Hanselmann | value = pyp.Word(pyp.alphanums + "_-/.:")
|
1363 | a2cfdea2 | Iustin Pop | quoted = dbl_quote + pyp.CharsNotIn('"') + dbl_quote
|
1364 | 5a672c30 | Manuel Franceschini | ipv4_addr = (pyp.Optional(pyp.Literal("ipv4")).suppress() +
|
1365 | 5a672c30 | Manuel Franceschini | pyp.Word(pyp.nums + ".") + colon + number)
|
1366 | 5a672c30 | Manuel Franceschini | ipv6_addr = (pyp.Optional(pyp.Literal("ipv6")).suppress() +
|
1367 | 5a672c30 | Manuel Franceschini | pyp.Optional(lbracket) + pyp.Word(pyp.hexnums + ":") +
|
1368 | 5a672c30 | Manuel Franceschini | pyp.Optional(rbracket) + colon + number) |
1369 | a2cfdea2 | Iustin Pop | # meta device, extended syntax
|
1370 | 5a672c30 | Manuel Franceschini | meta_value = ((value ^ quoted) + lbracket + number + rbracket) |
1371 | 01e2ce3a | Iustin Pop | # device name, extended syntax
|
1372 | 01e2ce3a | Iustin Pop | device_value = pyp.Literal("minor").suppress() + number
|
1373 | a2cfdea2 | Iustin Pop | |
1374 | a2cfdea2 | Iustin Pop | # a statement
|
1375 | a2cfdea2 | Iustin Pop | stmt = (~rbrace + keyword + ~lbrace + |
1376 | 5a672c30 | Manuel Franceschini | pyp.Optional(ipv4_addr ^ ipv6_addr ^ value ^ quoted ^ meta_value ^ |
1377 | 01e2ce3a | Iustin Pop | device_value) + |
1378 | a2cfdea2 | Iustin Pop | pyp.Optional(defa) + semi + |
1379 | a2cfdea2 | Iustin Pop | pyp.Optional(pyp.restOfLine).suppress()) |
1380 | a2cfdea2 | Iustin Pop | |
1381 | a2cfdea2 | Iustin Pop | # an entire section
|
1382 | d0c8c01d | Iustin Pop | section_name = pyp.Word(pyp.alphas + "_")
|
1383 | a2cfdea2 | Iustin Pop | section = section_name + lbrace + pyp.ZeroOrMore(pyp.Group(stmt)) + rbrace |
1384 | a2cfdea2 | Iustin Pop | |
1385 | a2cfdea2 | Iustin Pop | bnf = pyp.ZeroOrMore(pyp.Group(section ^ stmt)) |
1386 | a2cfdea2 | Iustin Pop | bnf.ignore(comment) |
1387 | a2cfdea2 | Iustin Pop | |
1388 | a2cfdea2 | Iustin Pop | cls._PARSE_SHOW = bnf |
1389 | a2cfdea2 | Iustin Pop | |
1390 | a2cfdea2 | Iustin Pop | return bnf
|
1391 | a2cfdea2 | Iustin Pop | |
1392 | a2cfdea2 | Iustin Pop | @classmethod
|
1393 | 3840729d | Iustin Pop | def _GetShowData(cls, minor): |
1394 | 3840729d | Iustin Pop | """Return the `drbdsetup show` data for a minor.
|
1395 | a2cfdea2 | Iustin Pop |
|
1396 | a2cfdea2 | Iustin Pop | """
|
1397 | a2cfdea2 | Iustin Pop | result = utils.RunCmd(["drbdsetup", cls._DevPath(minor), "show"]) |
1398 | a2cfdea2 | Iustin Pop | if result.failed:
|
1399 | 468c5f77 | Iustin Pop | logging.error("Can't display the drbd config: %s - %s",
|
1400 | 468c5f77 | Iustin Pop | result.fail_reason, result.output) |
1401 | 3840729d | Iustin Pop | return None |
1402 | 3840729d | Iustin Pop | return result.stdout
|
1403 | 3840729d | Iustin Pop | |
1404 | 3840729d | Iustin Pop | @classmethod
|
1405 | 3840729d | Iustin Pop | def _GetDevInfo(cls, out): |
1406 | 3840729d | Iustin Pop | """Parse details about a given DRBD minor.
|
1407 | 3840729d | Iustin Pop |
|
1408 | 3840729d | Iustin Pop | This return, if available, the local backing device (as a path)
|
1409 | 3840729d | Iustin Pop | and the local and remote (ip, port) information from a string
|
1410 | 3840729d | Iustin Pop | containing the output of the `drbdsetup show` command as returned
|
1411 | 3840729d | Iustin Pop | by _GetShowData.
|
1412 | 3840729d | Iustin Pop |
|
1413 | 3840729d | Iustin Pop | """
|
1414 | 3840729d | Iustin Pop | data = {} |
1415 | a2cfdea2 | Iustin Pop | if not out: |
1416 | a2cfdea2 | Iustin Pop | return data
|
1417 | a2cfdea2 | Iustin Pop | |
1418 | a2cfdea2 | Iustin Pop | bnf = cls._GetShowParser() |
1419 | a2cfdea2 | Iustin Pop | # run pyparse
|
1420 | a2cfdea2 | Iustin Pop | |
1421 | a2cfdea2 | Iustin Pop | try:
|
1422 | a2cfdea2 | Iustin Pop | results = bnf.parseString(out) |
1423 | a2cfdea2 | Iustin Pop | except pyp.ParseException, err:
|
1424 | 82463074 | Iustin Pop | _ThrowError("Can't parse drbdsetup show output: %s", str(err)) |
1425 | a2cfdea2 | Iustin Pop | |
1426 | a2cfdea2 | Iustin Pop | # and massage the results into our desired format
|
1427 | a2cfdea2 | Iustin Pop | for section in results: |
1428 | a2cfdea2 | Iustin Pop | sname = section[0]
|
1429 | a2cfdea2 | Iustin Pop | if sname == "_this_host": |
1430 | a2cfdea2 | Iustin Pop | for lst in section[1:]: |
1431 | a2cfdea2 | Iustin Pop | if lst[0] == "disk": |
1432 | a2cfdea2 | Iustin Pop | data["local_dev"] = lst[1] |
1433 | a2cfdea2 | Iustin Pop | elif lst[0] == "meta-disk": |
1434 | a2cfdea2 | Iustin Pop | data["meta_dev"] = lst[1] |
1435 | a2cfdea2 | Iustin Pop | data["meta_index"] = lst[2] |
1436 | a2cfdea2 | Iustin Pop | elif lst[0] == "address": |
1437 | a2cfdea2 | Iustin Pop | data["local_addr"] = tuple(lst[1:]) |
1438 | a2cfdea2 | Iustin Pop | elif sname == "_remote_host": |
1439 | a2cfdea2 | Iustin Pop | for lst in section[1:]: |
1440 | a2cfdea2 | Iustin Pop | if lst[0] == "address": |
1441 | a2cfdea2 | Iustin Pop | data["remote_addr"] = tuple(lst[1:]) |
1442 | a2cfdea2 | Iustin Pop | return data
|
1443 | a2cfdea2 | Iustin Pop | |
1444 | a2cfdea2 | Iustin Pop | def _MatchesLocal(self, info): |
1445 | a2cfdea2 | Iustin Pop | """Test if our local config matches with an existing device.
|
1446 | a2cfdea2 | Iustin Pop |
|
1447 | a2cfdea2 | Iustin Pop | The parameter should be as returned from `_GetDevInfo()`. This
|
1448 | a2cfdea2 | Iustin Pop | method tests if our local backing device is the same as the one in
|
1449 | a2cfdea2 | Iustin Pop | the info parameter, in effect testing if we look like the given
|
1450 | a2cfdea2 | Iustin Pop | device.
|
1451 | a2cfdea2 | Iustin Pop |
|
1452 | a2cfdea2 | Iustin Pop | """
|
1453 | b00b95dd | Iustin Pop | if self._children: |
1454 | b00b95dd | Iustin Pop | backend, meta = self._children
|
1455 | b00b95dd | Iustin Pop | else:
|
1456 | b00b95dd | Iustin Pop | backend = meta = None
|
1457 | b00b95dd | Iustin Pop | |
1458 | a2cfdea2 | Iustin Pop | if backend is not None: |
1459 | b00b95dd | Iustin Pop | retval = ("local_dev" in info and info["local_dev"] == backend.dev_path) |
1460 | a2cfdea2 | Iustin Pop | else:
|
1461 | a2cfdea2 | Iustin Pop | retval = ("local_dev" not in info) |
1462 | b00b95dd | Iustin Pop | |
1463 | a2cfdea2 | Iustin Pop | if meta is not None: |
1464 | b00b95dd | Iustin Pop | retval = retval and ("meta_dev" in info and |
1465 | b00b95dd | Iustin Pop | info["meta_dev"] == meta.dev_path)
|
1466 | b00b95dd | Iustin Pop | retval = retval and ("meta_index" in info and |
1467 | b00b95dd | Iustin Pop | info["meta_index"] == 0) |
1468 | a2cfdea2 | Iustin Pop | else:
|
1469 | a2cfdea2 | Iustin Pop | retval = retval and ("meta_dev" not in info and |
1470 | a2cfdea2 | Iustin Pop | "meta_index" not in info) |
1471 | a2cfdea2 | Iustin Pop | return retval
|
1472 | a2cfdea2 | Iustin Pop | |
1473 | a2cfdea2 | Iustin Pop | def _MatchesNet(self, info): |
1474 | a2cfdea2 | Iustin Pop | """Test if our network config matches with an existing device.
|
1475 | a2cfdea2 | Iustin Pop |
|
1476 | a2cfdea2 | Iustin Pop | The parameter should be as returned from `_GetDevInfo()`. This
|
1477 | a2cfdea2 | Iustin Pop | method tests if our network configuration is the same as the one
|
1478 | a2cfdea2 | Iustin Pop | in the info parameter, in effect testing if we look like the given
|
1479 | a2cfdea2 | Iustin Pop | device.
|
1480 | a2cfdea2 | Iustin Pop |
|
1481 | a2cfdea2 | Iustin Pop | """
|
1482 | a2cfdea2 | Iustin Pop | if (((self._lhost is None and not ("local_addr" in info)) and |
1483 | a2cfdea2 | Iustin Pop | (self._rhost is None and not ("remote_addr" in info)))): |
1484 | a2cfdea2 | Iustin Pop | return True |
1485 | a2cfdea2 | Iustin Pop | |
1486 | a2cfdea2 | Iustin Pop | if self._lhost is None: |
1487 | a2cfdea2 | Iustin Pop | return False |
1488 | a2cfdea2 | Iustin Pop | |
1489 | a2cfdea2 | Iustin Pop | if not ("local_addr" in info and |
1490 | a2cfdea2 | Iustin Pop | "remote_addr" in info): |
1491 | a2cfdea2 | Iustin Pop | return False |
1492 | a2cfdea2 | Iustin Pop | |
1493 | a2cfdea2 | Iustin Pop | retval = (info["local_addr"] == (self._lhost, self._lport)) |
1494 | a2cfdea2 | Iustin Pop | retval = (retval and
|
1495 | a2cfdea2 | Iustin Pop | info["remote_addr"] == (self._rhost, self._rport)) |
1496 | a2cfdea2 | Iustin Pop | return retval
|
1497 | a2cfdea2 | Iustin Pop | |
1498 | 8a69b3a8 | Andrea Spadaccini | def _AssembleLocal(self, minor, backend, meta, size): |
1499 | a2cfdea2 | Iustin Pop | """Configure the local part of a DRBD device.
|
1500 | a2cfdea2 | Iustin Pop |
|
1501 | a2cfdea2 | Iustin Pop | """
|
1502 | 8a69b3a8 | Andrea Spadaccini | args = ["drbdsetup", self._DevPath(minor), "disk", |
1503 | f069addf | Iustin Pop | backend, meta, "0",
|
1504 | f069addf | Iustin Pop | "-e", "detach", |
1505 | f069addf | Iustin Pop | "--create-device"]
|
1506 | 60bca04a | Iustin Pop | if size:
|
1507 | 60bca04a | Iustin Pop | args.extend(["-d", "%sm" % size]) |
1508 | 8a69b3a8 | Andrea Spadaccini | |
1509 | 8a69b3a8 | Andrea Spadaccini | version = self._GetVersion(self._GetProcData()) |
1510 | 8a69b3a8 | Andrea Spadaccini | vmaj = version["k_major"]
|
1511 | 8a69b3a8 | Andrea Spadaccini | vmin = version["k_minor"]
|
1512 | 8a69b3a8 | Andrea Spadaccini | vrel = version["k_point"]
|
1513 | 8a69b3a8 | Andrea Spadaccini | |
1514 | 8a69b3a8 | Andrea Spadaccini | barrier_args = \ |
1515 | 8a69b3a8 | Andrea Spadaccini | self._ComputeDiskBarrierArgs(vmaj, vmin, vrel,
|
1516 | ac00bf1b | Andrea Spadaccini | self.params[constants.LDP_BARRIERS],
|
1517 | ac00bf1b | Andrea Spadaccini | self.params[constants.LDP_NO_META_FLUSH])
|
1518 | 8a69b3a8 | Andrea Spadaccini | args.extend(barrier_args) |
1519 | 8a69b3a8 | Andrea Spadaccini | |
1520 | ad1dd4c7 | Andrea Spadaccini | if self.params[constants.LDP_DISK_CUSTOM]: |
1521 | ad1dd4c7 | Andrea Spadaccini | args.extend(shlex.split(self.params[constants.LDP_DISK_CUSTOM]))
|
1522 | ad1dd4c7 | Andrea Spadaccini | |
1523 | 333411a7 | Guido Trotter | result = utils.RunCmd(args) |
1524 | a2cfdea2 | Iustin Pop | if result.failed:
|
1525 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: can't attach local disk: %s", minor, result.output)
|
1526 | a2cfdea2 | Iustin Pop | |
1527 | 8a69b3a8 | Andrea Spadaccini | @classmethod
|
1528 | 8a69b3a8 | Andrea Spadaccini | def _ComputeDiskBarrierArgs(cls, vmaj, vmin, vrel, disabled_barriers, |
1529 | 5ae4945a | Iustin Pop | disable_meta_flush): |
1530 | 8a69b3a8 | Andrea Spadaccini | """Compute the DRBD command line parameters for disk barriers
|
1531 | 8a69b3a8 | Andrea Spadaccini |
|
1532 | 8a69b3a8 | Andrea Spadaccini | Returns a list of the disk barrier parameters as requested via the
|
1533 | 8a69b3a8 | Andrea Spadaccini | disabled_barriers and disable_meta_flush arguments, and according to the
|
1534 | 8a69b3a8 | Andrea Spadaccini | supported ones in the DRBD version vmaj.vmin.vrel
|
1535 | 8a69b3a8 | Andrea Spadaccini |
|
1536 | 8a69b3a8 | Andrea Spadaccini | If the desired option is unsupported, raises errors.BlockDeviceError.
|
1537 | 8a69b3a8 | Andrea Spadaccini |
|
1538 | 8a69b3a8 | Andrea Spadaccini | """
|
1539 | 8a69b3a8 | Andrea Spadaccini | disabled_barriers_set = frozenset(disabled_barriers)
|
1540 | 8a69b3a8 | Andrea Spadaccini | if not disabled_barriers_set in constants.DRBD_VALID_BARRIER_OPT: |
1541 | 8a69b3a8 | Andrea Spadaccini | raise errors.BlockDeviceError("%s is not a valid option set for DRBD" |
1542 | 8a69b3a8 | Andrea Spadaccini | " barriers" % disabled_barriers)
|
1543 | 8a69b3a8 | Andrea Spadaccini | |
1544 | 8a69b3a8 | Andrea Spadaccini | args = [] |
1545 | 8a69b3a8 | Andrea Spadaccini | |
1546 | 8a69b3a8 | Andrea Spadaccini | # The following code assumes DRBD 8.x, with x < 4 and x != 1 (DRBD 8.1.x
|
1547 | 8a69b3a8 | Andrea Spadaccini | # does not exist)
|
1548 | 8a69b3a8 | Andrea Spadaccini | if not vmaj == 8 and vmin in (0, 2, 3): |
1549 | 8a69b3a8 | Andrea Spadaccini | raise errors.BlockDeviceError("Unsupported DRBD version: %d.%d.%d" % |
1550 | 8a69b3a8 | Andrea Spadaccini | (vmaj, vmin, vrel)) |
1551 | 8a69b3a8 | Andrea Spadaccini | |
1552 | 8a69b3a8 | Andrea Spadaccini | def _AppendOrRaise(option, min_version): |
1553 | 8a69b3a8 | Andrea Spadaccini | """Helper for DRBD options"""
|
1554 | 8a69b3a8 | Andrea Spadaccini | if min_version is not None and vrel >= min_version: |
1555 | 8a69b3a8 | Andrea Spadaccini | args.append(option) |
1556 | 8a69b3a8 | Andrea Spadaccini | else:
|
1557 | 8a69b3a8 | Andrea Spadaccini | raise errors.BlockDeviceError("Could not use the option %s as the" |
1558 | 8a69b3a8 | Andrea Spadaccini | " DRBD version %d.%d.%d does not support"
|
1559 | 8a69b3a8 | Andrea Spadaccini | " it." % (option, vmaj, vmin, vrel))
|
1560 | 8a69b3a8 | Andrea Spadaccini | |
1561 | 8a69b3a8 | Andrea Spadaccini | # the minimum version for each feature is encoded via pairs of (minor
|
1562 | 8a69b3a8 | Andrea Spadaccini | # version -> x) where x is version in which support for the option was
|
1563 | 8a69b3a8 | Andrea Spadaccini | # introduced.
|
1564 | 8a69b3a8 | Andrea Spadaccini | meta_flush_supported = disk_flush_supported = { |
1565 | 8a69b3a8 | Andrea Spadaccini | 0: 12, |
1566 | 8a69b3a8 | Andrea Spadaccini | 2: 7, |
1567 | 8a69b3a8 | Andrea Spadaccini | 3: 0, |
1568 | 8a69b3a8 | Andrea Spadaccini | } |
1569 | 8a69b3a8 | Andrea Spadaccini | |
1570 | 8a69b3a8 | Andrea Spadaccini | disk_drain_supported = { |
1571 | 8a69b3a8 | Andrea Spadaccini | 2: 7, |
1572 | 8a69b3a8 | Andrea Spadaccini | 3: 0, |
1573 | 8a69b3a8 | Andrea Spadaccini | } |
1574 | 8a69b3a8 | Andrea Spadaccini | |
1575 | 8a69b3a8 | Andrea Spadaccini | disk_barriers_supported = { |
1576 | 8a69b3a8 | Andrea Spadaccini | 3: 0, |
1577 | 8a69b3a8 | Andrea Spadaccini | } |
1578 | 8a69b3a8 | Andrea Spadaccini | |
1579 | 8a69b3a8 | Andrea Spadaccini | # meta flushes
|
1580 | 8a69b3a8 | Andrea Spadaccini | if disable_meta_flush:
|
1581 | 8a69b3a8 | Andrea Spadaccini | _AppendOrRaise(cls._DISABLE_META_FLUSH_OPTION, |
1582 | 8a69b3a8 | Andrea Spadaccini | meta_flush_supported.get(vmin, None))
|
1583 | 8a69b3a8 | Andrea Spadaccini | |
1584 | 8a69b3a8 | Andrea Spadaccini | # disk flushes
|
1585 | 8a69b3a8 | Andrea Spadaccini | if constants.DRBD_B_DISK_FLUSH in disabled_barriers_set: |
1586 | 8a69b3a8 | Andrea Spadaccini | _AppendOrRaise(cls._DISABLE_FLUSH_OPTION, |
1587 | 8a69b3a8 | Andrea Spadaccini | disk_flush_supported.get(vmin, None))
|
1588 | 8a69b3a8 | Andrea Spadaccini | |
1589 | 8a69b3a8 | Andrea Spadaccini | # disk drain
|
1590 | 8a69b3a8 | Andrea Spadaccini | if constants.DRBD_B_DISK_DRAIN in disabled_barriers_set: |
1591 | 8a69b3a8 | Andrea Spadaccini | _AppendOrRaise(cls._DISABLE_DRAIN_OPTION, |
1592 | 8a69b3a8 | Andrea Spadaccini | disk_drain_supported.get(vmin, None))
|
1593 | 8a69b3a8 | Andrea Spadaccini | |
1594 | 8a69b3a8 | Andrea Spadaccini | # disk barriers
|
1595 | 8a69b3a8 | Andrea Spadaccini | if constants.DRBD_B_DISK_BARRIERS in disabled_barriers_set: |
1596 | 8a69b3a8 | Andrea Spadaccini | _AppendOrRaise(cls._DISABLE_DISK_OPTION, |
1597 | 8a69b3a8 | Andrea Spadaccini | disk_barriers_supported.get(vmin, None))
|
1598 | 8a69b3a8 | Andrea Spadaccini | |
1599 | 8a69b3a8 | Andrea Spadaccini | return args
|
1600 | 8a69b3a8 | Andrea Spadaccini | |
1601 | 6e9814a1 | Andrea Spadaccini | def _AssembleNet(self, minor, net_info, protocol, |
1602 | a2cfdea2 | Iustin Pop | dual_pri=False, hmac=None, secret=None): |
1603 | a2cfdea2 | Iustin Pop | """Configure the network part of the device.
|
1604 | a2cfdea2 | Iustin Pop |
|
1605 | a2cfdea2 | Iustin Pop | """
|
1606 | a2cfdea2 | Iustin Pop | lhost, lport, rhost, rport = net_info |
1607 | 52857176 | Iustin Pop | if None in net_info: |
1608 | 52857176 | Iustin Pop | # we don't want network connection and actually want to make
|
1609 | 52857176 | Iustin Pop | # sure its shutdown
|
1610 | 6e9814a1 | Andrea Spadaccini | self._ShutdownNet(minor)
|
1611 | 1063abd1 | Iustin Pop | return
|
1612 | 52857176 | Iustin Pop | |
1613 | 7d585316 | Iustin Pop | # Workaround for a race condition. When DRBD is doing its dance to
|
1614 | 7d585316 | Iustin Pop | # establish a connection with its peer, it also sends the
|
1615 | 7d585316 | Iustin Pop | # synchronization speed over the wire. In some cases setting the
|
1616 | 7d585316 | Iustin Pop | # sync speed only after setting up both sides can race with DRBD
|
1617 | 7d585316 | Iustin Pop | # connecting, hence we set it here before telling DRBD anything
|
1618 | 7d585316 | Iustin Pop | # about its peer.
|
1619 | 8584e922 | Andrea Spadaccini | sync_errors = self._SetMinorSyncParams(minor, self.params) |
1620 | 8584e922 | Andrea Spadaccini | if sync_errors:
|
1621 | 8584e922 | Andrea Spadaccini | _ThrowError("drbd%d: can't set the synchronization parameters: %s" %
|
1622 | 8584e922 | Andrea Spadaccini | (minor, utils.CommaJoin(sync_errors))) |
1623 | 7d585316 | Iustin Pop | |
1624 | 8b312c1d | Manuel Franceschini | if netutils.IP6Address.IsValid(lhost):
|
1625 | 8b312c1d | Manuel Franceschini | if not netutils.IP6Address.IsValid(rhost): |
1626 | 5a672c30 | Manuel Franceschini | _ThrowError("drbd%d: can't connect ip %s to ip %s" %
|
1627 | 5a672c30 | Manuel Franceschini | (minor, lhost, rhost)) |
1628 | 5a672c30 | Manuel Franceschini | family = "ipv6"
|
1629 | 8b312c1d | Manuel Franceschini | elif netutils.IP4Address.IsValid(lhost):
|
1630 | 8b312c1d | Manuel Franceschini | if not netutils.IP4Address.IsValid(rhost): |
1631 | 5a672c30 | Manuel Franceschini | _ThrowError("drbd%d: can't connect ip %s to ip %s" %
|
1632 | 5a672c30 | Manuel Franceschini | (minor, lhost, rhost)) |
1633 | 5a672c30 | Manuel Franceschini | family = "ipv4"
|
1634 | 5a672c30 | Manuel Franceschini | else:
|
1635 | 5a672c30 | Manuel Franceschini | _ThrowError("drbd%d: Invalid ip %s" % (minor, lhost))
|
1636 | 5a672c30 | Manuel Franceschini | |
1637 | 6e9814a1 | Andrea Spadaccini | args = ["drbdsetup", self._DevPath(minor), "net", |
1638 | 5a672c30 | Manuel Franceschini | "%s:%s:%s" % (family, lhost, lport),
|
1639 | 5a672c30 | Manuel Franceschini | "%s:%s:%s" % (family, rhost, rport), protocol,
|
1640 | f38478b2 | Iustin Pop | "-A", "discard-zero-changes", |
1641 | f38478b2 | Iustin Pop | "-B", "consensus", |
1642 | ab6cc81c | Iustin Pop | "--create-device",
|
1643 | f38478b2 | Iustin Pop | ] |
1644 | a2cfdea2 | Iustin Pop | if dual_pri:
|
1645 | a2cfdea2 | Iustin Pop | args.append("-m")
|
1646 | a2cfdea2 | Iustin Pop | if hmac and secret: |
1647 | a2cfdea2 | Iustin Pop | args.extend(["-a", hmac, "-x", secret]) |
1648 | ad1dd4c7 | Andrea Spadaccini | |
1649 | ad1dd4c7 | Andrea Spadaccini | if self.params[constants.LDP_NET_CUSTOM]: |
1650 | ad1dd4c7 | Andrea Spadaccini | args.extend(shlex.split(self.params[constants.LDP_NET_CUSTOM]))
|
1651 | ad1dd4c7 | Andrea Spadaccini | |
1652 | a2cfdea2 | Iustin Pop | result = utils.RunCmd(args) |
1653 | a2cfdea2 | Iustin Pop | if result.failed:
|
1654 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: can't setup network: %s - %s",
|
1655 | 1063abd1 | Iustin Pop | minor, result.fail_reason, result.output) |
1656 | a2cfdea2 | Iustin Pop | |
1657 | def8e2f6 | Michael Hanselmann | def _CheckNetworkConfig(): |
1658 | 6e9814a1 | Andrea Spadaccini | info = self._GetDevInfo(self._GetShowData(minor)) |
1659 | a2cfdea2 | Iustin Pop | if not "local_addr" in info or not "remote_addr" in info: |
1660 | def8e2f6 | Michael Hanselmann | raise utils.RetryAgain()
|
1661 | def8e2f6 | Michael Hanselmann | |
1662 | a2cfdea2 | Iustin Pop | if (info["local_addr"] != (lhost, lport) or |
1663 | a2cfdea2 | Iustin Pop | info["remote_addr"] != (rhost, rport)):
|
1664 | def8e2f6 | Michael Hanselmann | raise utils.RetryAgain()
|
1665 | def8e2f6 | Michael Hanselmann | |
1666 | def8e2f6 | Michael Hanselmann | try:
|
1667 | def8e2f6 | Michael Hanselmann | utils.Retry(_CheckNetworkConfig, 1.0, 10.0) |
1668 | def8e2f6 | Michael Hanselmann | except utils.RetryTimeout:
|
1669 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: timeout while configuring network", minor)
|
1670 | a2cfdea2 | Iustin Pop | |
1671 | b00b95dd | Iustin Pop | def AddChildren(self, devices): |
1672 | b00b95dd | Iustin Pop | """Add a disk to the DRBD device.
|
1673 | b00b95dd | Iustin Pop |
|
1674 | b00b95dd | Iustin Pop | """
|
1675 | b00b95dd | Iustin Pop | if self.minor is None: |
1676 | 82463074 | Iustin Pop | _ThrowError("drbd%d: can't attach to dbrd8 during AddChildren",
|
1677 | 82463074 | Iustin Pop | self._aminor)
|
1678 | b00b95dd | Iustin Pop | if len(devices) != 2: |
1679 | 82463074 | Iustin Pop | _ThrowError("drbd%d: need two devices for AddChildren", self.minor) |
1680 | 3840729d | Iustin Pop | info = self._GetDevInfo(self._GetShowData(self.minor)) |
1681 | 03ece5f3 | Iustin Pop | if "local_dev" in info: |
1682 | 82463074 | Iustin Pop | _ThrowError("drbd%d: already attached to a local disk", self.minor) |
1683 | b00b95dd | Iustin Pop | backend, meta = devices |
1684 | b00b95dd | Iustin Pop | if backend.dev_path is None or meta.dev_path is None: |
1685 | 82463074 | Iustin Pop | _ThrowError("drbd%d: children not ready during AddChildren", self.minor) |
1686 | b00b95dd | Iustin Pop | backend.Open() |
1687 | b00b95dd | Iustin Pop | meta.Open() |
1688 | 9c793cfb | Iustin Pop | self._CheckMetaSize(meta.dev_path)
|
1689 | b00b95dd | Iustin Pop | self._InitMeta(self._FindUnusedMinor(), meta.dev_path) |
1690 | b00b95dd | Iustin Pop | |
1691 | f069addf | Iustin Pop | self._AssembleLocal(self.minor, backend.dev_path, meta.dev_path, self.size) |
1692 | b00b95dd | Iustin Pop | self._children = devices
|
1693 | b00b95dd | Iustin Pop | |
1694 | b00b95dd | Iustin Pop | def RemoveChildren(self, devices): |
1695 | b00b95dd | Iustin Pop | """Detach the drbd device from local storage.
|
1696 | b00b95dd | Iustin Pop |
|
1697 | b00b95dd | Iustin Pop | """
|
1698 | b00b95dd | Iustin Pop | if self.minor is None: |
1699 | 82463074 | Iustin Pop | _ThrowError("drbd%d: can't attach to drbd8 during RemoveChildren",
|
1700 | 82463074 | Iustin Pop | self._aminor)
|
1701 | 03ece5f3 | Iustin Pop | # early return if we don't actually have backing storage
|
1702 | 3840729d | Iustin Pop | info = self._GetDevInfo(self._GetShowData(self.minor)) |
1703 | 03ece5f3 | Iustin Pop | if "local_dev" not in info: |
1704 | 03ece5f3 | Iustin Pop | return
|
1705 | b00b95dd | Iustin Pop | if len(self._children) != 2: |
1706 | 82463074 | Iustin Pop | _ThrowError("drbd%d: we don't have two children: %s", self.minor, |
1707 | 82463074 | Iustin Pop | self._children)
|
1708 | e739bd57 | Iustin Pop | if self._children.count(None) == 2: # we don't actually have children :) |
1709 | 82463074 | Iustin Pop | logging.warning("drbd%d: requested detach while detached", self.minor) |
1710 | e739bd57 | Iustin Pop | return
|
1711 | b00b95dd | Iustin Pop | if len(devices) != 2: |
1712 | 82463074 | Iustin Pop | _ThrowError("drbd%d: we need two children in RemoveChildren", self.minor) |
1713 | e739bd57 | Iustin Pop | for child, dev in zip(self._children, devices): |
1714 | e739bd57 | Iustin Pop | if dev != child.dev_path:
|
1715 | 82463074 | Iustin Pop | _ThrowError("drbd%d: mismatch in local storage (%s != %s) in"
|
1716 | 82463074 | Iustin Pop | " RemoveChildren", self.minor, dev, child.dev_path) |
1717 | b00b95dd | Iustin Pop | |
1718 | 1063abd1 | Iustin Pop | self._ShutdownLocal(self.minor) |
1719 | b00b95dd | Iustin Pop | self._children = []
|
1720 | b00b95dd | Iustin Pop | |
1721 | 7d585316 | Iustin Pop | @classmethod
|
1722 | f2f57b6e | Andrea Spadaccini | def _SetMinorSyncParams(cls, minor, params): |
1723 | f2f57b6e | Andrea Spadaccini | """Set the parameters of the DRBD syncer.
|
1724 | a2cfdea2 | Iustin Pop |
|
1725 | 7d585316 | Iustin Pop | This is the low-level implementation.
|
1726 | 7d585316 | Iustin Pop |
|
1727 | 7d585316 | Iustin Pop | @type minor: int
|
1728 | 7d585316 | Iustin Pop | @param minor: the drbd minor whose settings we change
|
1729 | f2f57b6e | Andrea Spadaccini | @type params: dict
|
1730 | f2f57b6e | Andrea Spadaccini | @param params: LD level disk parameters related to the synchronization
|
1731 | 8584e922 | Andrea Spadaccini | @rtype: list
|
1732 | 8584e922 | Andrea Spadaccini | @return: a list of error messages
|
1733 | 7d585316 | Iustin Pop |
|
1734 | a2cfdea2 | Iustin Pop | """
|
1735 | f2f57b6e | Andrea Spadaccini | |
1736 | f2f57b6e | Andrea Spadaccini | args = ["drbdsetup", cls._DevPath(minor), "syncer"] |
1737 | f2f57b6e | Andrea Spadaccini | if params[constants.LDP_DYNAMIC_RESYNC]:
|
1738 | f2f57b6e | Andrea Spadaccini | version = cls._GetVersion(cls._GetProcData()) |
1739 | f2f57b6e | Andrea Spadaccini | vmin = version["k_minor"]
|
1740 | f2f57b6e | Andrea Spadaccini | vrel = version["k_point"]
|
1741 | f2f57b6e | Andrea Spadaccini | |
1742 | f2f57b6e | Andrea Spadaccini | # By definition we are using 8.x, so just check the rest of the version
|
1743 | f2f57b6e | Andrea Spadaccini | # number
|
1744 | f2f57b6e | Andrea Spadaccini | if vmin != 3 or vrel < 9: |
1745 | 8584e922 | Andrea Spadaccini | msg = ("The current DRBD version (8.%d.%d) does not support the "
|
1746 | 8584e922 | Andrea Spadaccini | "dynamic resync speed controller" % (vmin, vrel))
|
1747 | 8584e922 | Andrea Spadaccini | logging.error(msg) |
1748 | 8584e922 | Andrea Spadaccini | return [msg]
|
1749 | 8584e922 | Andrea Spadaccini | |
1750 | 8584e922 | Andrea Spadaccini | if params[constants.LDP_PLAN_AHEAD] == 0: |
1751 | 8584e922 | Andrea Spadaccini | msg = ("A value of 0 for c-plan-ahead disables the dynamic sync speed"
|
1752 | 8584e922 | Andrea Spadaccini | " controller at DRBD level. If you want to disable it, please"
|
1753 | 8584e922 | Andrea Spadaccini | " set the dynamic-resync disk parameter to False.")
|
1754 | 8584e922 | Andrea Spadaccini | logging.error(msg) |
1755 | 8584e922 | Andrea Spadaccini | return [msg]
|
1756 | f2f57b6e | Andrea Spadaccini | |
1757 | f2f57b6e | Andrea Spadaccini | # add the c-* parameters to args
|
1758 | 8584e922 | Andrea Spadaccini | args.extend(["--c-plan-ahead", params[constants.LDP_PLAN_AHEAD],
|
1759 | 8584e922 | Andrea Spadaccini | "--c-fill-target", params[constants.LDP_FILL_TARGET],
|
1760 | 8584e922 | Andrea Spadaccini | "--c-delay-target", params[constants.LDP_DELAY_TARGET],
|
1761 | 8584e922 | Andrea Spadaccini | "--c-max-rate", params[constants.LDP_MAX_RATE],
|
1762 | 8584e922 | Andrea Spadaccini | "--c-min-rate", params[constants.LDP_MIN_RATE],
|
1763 | 5ae4945a | Iustin Pop | ]) |
1764 | f2f57b6e | Andrea Spadaccini | |
1765 | f2f57b6e | Andrea Spadaccini | else:
|
1766 | f2f57b6e | Andrea Spadaccini | args.extend(["-r", "%d" % params[constants.LDP_RESYNC_RATE]]) |
1767 | f2f57b6e | Andrea Spadaccini | |
1768 | f2f57b6e | Andrea Spadaccini | args.append("--create-device")
|
1769 | f2f57b6e | Andrea Spadaccini | result = utils.RunCmd(args) |
1770 | a2cfdea2 | Iustin Pop | if result.failed:
|
1771 | 8584e922 | Andrea Spadaccini | msg = ("Can't change syncer rate: %s - %s" %
|
1772 | 8584e922 | Andrea Spadaccini | (result.fail_reason, result.output)) |
1773 | 8584e922 | Andrea Spadaccini | logging.error(msg) |
1774 | e0b57a5c | Michael Hanselmann | return [msg]
|
1775 | 8584e922 | Andrea Spadaccini | |
1776 | 8584e922 | Andrea Spadaccini | return []
|
1777 | 7d585316 | Iustin Pop | |
1778 | f2f57b6e | Andrea Spadaccini | def SetSyncParams(self, params): |
1779 | f2f57b6e | Andrea Spadaccini | """Set the synchronization parameters of the DRBD syncer.
|
1780 | 7d585316 | Iustin Pop |
|
1781 | f2f57b6e | Andrea Spadaccini | @type params: dict
|
1782 | f2f57b6e | Andrea Spadaccini | @param params: LD level disk parameters related to the synchronization
|
1783 | 8584e922 | Andrea Spadaccini | @rtype: list
|
1784 | 8584e922 | Andrea Spadaccini | @return: a list of error messages, emitted both by the current node and by
|
1785 | 8584e922 | Andrea Spadaccini | children. An empty list means no errors
|
1786 | 7d585316 | Iustin Pop |
|
1787 | 7d585316 | Iustin Pop | """
|
1788 | 7d585316 | Iustin Pop | if self.minor is None: |
1789 | 8584e922 | Andrea Spadaccini | err = "Not attached during SetSyncParams"
|
1790 | 8584e922 | Andrea Spadaccini | logging.info(err) |
1791 | 8584e922 | Andrea Spadaccini | return [err]
|
1792 | 8584e922 | Andrea Spadaccini | |
1793 | f2f57b6e | Andrea Spadaccini | children_result = super(DRBD8, self).SetSyncParams(params) |
1794 | 8584e922 | Andrea Spadaccini | children_result.extend(self._SetMinorSyncParams(self.minor, params)) |
1795 | 8584e922 | Andrea Spadaccini | return children_result
|
1796 | a2cfdea2 | Iustin Pop | |
1797 | a3fffcc6 | René Nussbaumer | def PauseResumeSync(self, pause): |
1798 | a3fffcc6 | René Nussbaumer | """Pauses or resumes the sync of a DRBD device.
|
1799 | a3fffcc6 | René Nussbaumer |
|
1800 | a3fffcc6 | René Nussbaumer | @param pause: Wether to pause or resume
|
1801 | a3fffcc6 | René Nussbaumer | @return: the success of the operation
|
1802 | a3fffcc6 | René Nussbaumer |
|
1803 | a3fffcc6 | René Nussbaumer | """
|
1804 | a3fffcc6 | René Nussbaumer | if self.minor is None: |
1805 | a3fffcc6 | René Nussbaumer | logging.info("Not attached during PauseSync")
|
1806 | a3fffcc6 | René Nussbaumer | return False |
1807 | a3fffcc6 | René Nussbaumer | |
1808 | a3fffcc6 | René Nussbaumer | children_result = super(DRBD8, self).PauseResumeSync(pause) |
1809 | a3fffcc6 | René Nussbaumer | |
1810 | a3fffcc6 | René Nussbaumer | if pause:
|
1811 | a3fffcc6 | René Nussbaumer | cmd = "pause-sync"
|
1812 | a3fffcc6 | René Nussbaumer | else:
|
1813 | a3fffcc6 | René Nussbaumer | cmd = "resume-sync"
|
1814 | a3fffcc6 | René Nussbaumer | |
1815 | a3fffcc6 | René Nussbaumer | result = utils.RunCmd(["drbdsetup", self.dev_path, cmd]) |
1816 | a3fffcc6 | René Nussbaumer | if result.failed:
|
1817 | a3fffcc6 | René Nussbaumer | logging.error("Can't %s: %s - %s", cmd,
|
1818 | a3fffcc6 | René Nussbaumer | result.fail_reason, result.output) |
1819 | a3fffcc6 | René Nussbaumer | return not result.failed and children_result |
1820 | a3fffcc6 | René Nussbaumer | |
1821 | 6b90c22e | Iustin Pop | def GetProcStatus(self): |
1822 | 6b90c22e | Iustin Pop | """Return device data from /proc.
|
1823 | 6b90c22e | Iustin Pop |
|
1824 | 6b90c22e | Iustin Pop | """
|
1825 | 6b90c22e | Iustin Pop | if self.minor is None: |
1826 | 82463074 | Iustin Pop | _ThrowError("drbd%d: GetStats() called while not attached", self._aminor) |
1827 | 6b90c22e | Iustin Pop | proc_info = self._MassageProcData(self._GetProcData()) |
1828 | 6b90c22e | Iustin Pop | if self.minor not in proc_info: |
1829 | 82463074 | Iustin Pop | _ThrowError("drbd%d: can't find myself in /proc", self.minor) |
1830 | 6b90c22e | Iustin Pop | return DRBD8Status(proc_info[self.minor]) |
1831 | 6b90c22e | Iustin Pop | |
1832 | a2cfdea2 | Iustin Pop | def GetSyncStatus(self): |
1833 | a2cfdea2 | Iustin Pop | """Returns the sync status of the device.
|
1834 | a2cfdea2 | Iustin Pop |
|
1835 | a2cfdea2 | Iustin Pop |
|
1836 | a2cfdea2 | Iustin Pop | If sync_percent is None, it means all is ok
|
1837 | 5bbd3f7f | Michael Hanselmann | If estimated_time is None, it means we can't estimate
|
1838 | 0834c866 | Iustin Pop | the time needed, otherwise it's the time left in seconds.
|
1839 | 0834c866 | Iustin Pop |
|
1840 | 0834c866 | Iustin Pop |
|
1841 | 0834c866 | Iustin Pop | We set the is_degraded parameter to True on two conditions:
|
1842 | 0834c866 | Iustin Pop | network not connected or local disk missing.
|
1843 | 0834c866 | Iustin Pop |
|
1844 | 5bbd3f7f | Michael Hanselmann | We compute the ldisk parameter based on whether we have a local
|
1845 | 0834c866 | Iustin Pop | disk or not.
|
1846 | a2cfdea2 | Iustin Pop |
|
1847 | 96acbc09 | Michael Hanselmann | @rtype: objects.BlockDevStatus
|
1848 | c41eea6e | Iustin Pop |
|
1849 | a2cfdea2 | Iustin Pop | """
|
1850 | a2cfdea2 | Iustin Pop | if self.minor is None and not self.Attach(): |
1851 | 82463074 | Iustin Pop | _ThrowError("drbd%d: can't Attach() in GetSyncStatus", self._aminor) |
1852 | 96acbc09 | Michael Hanselmann | |
1853 | 6b90c22e | Iustin Pop | stats = self.GetProcStatus()
|
1854 | f208978a | Michael Hanselmann | is_degraded = not stats.is_connected or not stats.is_disk_uptodate |
1855 | f208978a | Michael Hanselmann | |
1856 | f208978a | Michael Hanselmann | if stats.is_disk_uptodate:
|
1857 | f208978a | Michael Hanselmann | ldisk_status = constants.LDS_OKAY |
1858 | f208978a | Michael Hanselmann | elif stats.is_diskless:
|
1859 | f208978a | Michael Hanselmann | ldisk_status = constants.LDS_FAULTY |
1860 | f208978a | Michael Hanselmann | else:
|
1861 | f208978a | Michael Hanselmann | ldisk_status = constants.LDS_UNKNOWN |
1862 | 96acbc09 | Michael Hanselmann | |
1863 | 96acbc09 | Michael Hanselmann | return objects.BlockDevStatus(dev_path=self.dev_path, |
1864 | 96acbc09 | Michael Hanselmann | major=self.major,
|
1865 | 96acbc09 | Michael Hanselmann | minor=self.minor,
|
1866 | 96acbc09 | Michael Hanselmann | sync_percent=stats.sync_percent, |
1867 | 96acbc09 | Michael Hanselmann | estimated_time=stats.est_time, |
1868 | f208978a | Michael Hanselmann | is_degraded=is_degraded, |
1869 | f208978a | Michael Hanselmann | ldisk_status=ldisk_status) |
1870 | a2cfdea2 | Iustin Pop | |
1871 | a2cfdea2 | Iustin Pop | def Open(self, force=False): |
1872 | a2cfdea2 | Iustin Pop | """Make the local state primary.
|
1873 | a2cfdea2 | Iustin Pop |
|
1874 | f860ff4e | Guido Trotter | If the 'force' parameter is given, the '-o' option is passed to
|
1875 | f860ff4e | Guido Trotter | drbdsetup. Since this is a potentially dangerous operation, the
|
1876 | a2cfdea2 | Iustin Pop | force flag should be only given after creation, when it actually
|
1877 | f860ff4e | Guido Trotter | is mandatory.
|
1878 | a2cfdea2 | Iustin Pop |
|
1879 | a2cfdea2 | Iustin Pop | """
|
1880 | a2cfdea2 | Iustin Pop | if self.minor is None and not self.Attach(): |
1881 | 468c5f77 | Iustin Pop | logging.error("DRBD cannot attach to a device during open")
|
1882 | a2cfdea2 | Iustin Pop | return False |
1883 | a2cfdea2 | Iustin Pop | cmd = ["drbdsetup", self.dev_path, "primary"] |
1884 | a2cfdea2 | Iustin Pop | if force:
|
1885 | a2cfdea2 | Iustin Pop | cmd.append("-o")
|
1886 | a2cfdea2 | Iustin Pop | result = utils.RunCmd(cmd) |
1887 | a2cfdea2 | Iustin Pop | if result.failed:
|
1888 | 82463074 | Iustin Pop | _ThrowError("drbd%d: can't make drbd device primary: %s", self.minor, |
1889 | 82463074 | Iustin Pop | result.output) |
1890 | a2cfdea2 | Iustin Pop | |
1891 | a2cfdea2 | Iustin Pop | def Close(self): |
1892 | a2cfdea2 | Iustin Pop | """Make the local state secondary.
|
1893 | a2cfdea2 | Iustin Pop |
|
1894 | a2cfdea2 | Iustin Pop | This will, of course, fail if the device is in use.
|
1895 | a2cfdea2 | Iustin Pop |
|
1896 | a2cfdea2 | Iustin Pop | """
|
1897 | a2cfdea2 | Iustin Pop | if self.minor is None and not self.Attach(): |
1898 | 82463074 | Iustin Pop | _ThrowError("drbd%d: can't Attach() in Close()", self._aminor) |
1899 | a2cfdea2 | Iustin Pop | result = utils.RunCmd(["drbdsetup", self.dev_path, "secondary"]) |
1900 | a2cfdea2 | Iustin Pop | if result.failed:
|
1901 | 82463074 | Iustin Pop | _ThrowError("drbd%d: can't switch drbd device to secondary: %s",
|
1902 | 82463074 | Iustin Pop | self.minor, result.output)
|
1903 | a2cfdea2 | Iustin Pop | |
1904 | cf8df3f3 | Iustin Pop | def DisconnectNet(self): |
1905 | cf8df3f3 | Iustin Pop | """Removes network configuration.
|
1906 | cf8df3f3 | Iustin Pop |
|
1907 | cf8df3f3 | Iustin Pop | This method shutdowns the network side of the device.
|
1908 | cf8df3f3 | Iustin Pop |
|
1909 | cf8df3f3 | Iustin Pop | The method will wait up to a hardcoded timeout for the device to
|
1910 | cf8df3f3 | Iustin Pop | go into standalone after the 'disconnect' command before
|
1911 | cf8df3f3 | Iustin Pop | re-configuring it, as sometimes it takes a while for the
|
1912 | cf8df3f3 | Iustin Pop | disconnect to actually propagate and thus we might issue a 'net'
|
1913 | cf8df3f3 | Iustin Pop | command while the device is still connected. If the device will
|
1914 | cf8df3f3 | Iustin Pop | still be attached to the network and we time out, we raise an
|
1915 | cf8df3f3 | Iustin Pop | exception.
|
1916 | cf8df3f3 | Iustin Pop |
|
1917 | cf8df3f3 | Iustin Pop | """
|
1918 | cf8df3f3 | Iustin Pop | if self.minor is None: |
1919 | 82463074 | Iustin Pop | _ThrowError("drbd%d: disk not attached in re-attach net", self._aminor) |
1920 | cf8df3f3 | Iustin Pop | |
1921 | cf8df3f3 | Iustin Pop | if None in (self._lhost, self._lport, self._rhost, self._rport): |
1922 | 82463074 | Iustin Pop | _ThrowError("drbd%d: DRBD disk missing network info in"
|
1923 | 82463074 | Iustin Pop | " DisconnectNet()", self.minor) |
1924 | cf8df3f3 | Iustin Pop | |
1925 | def8e2f6 | Michael Hanselmann | class _DisconnectStatus: |
1926 | def8e2f6 | Michael Hanselmann | def __init__(self, ever_disconnected): |
1927 | def8e2f6 | Michael Hanselmann | self.ever_disconnected = ever_disconnected
|
1928 | cf8df3f3 | Iustin Pop | |
1929 | def8e2f6 | Michael Hanselmann | dstatus = _DisconnectStatus(_IgnoreError(self._ShutdownNet, self.minor)) |
1930 | def8e2f6 | Michael Hanselmann | |
1931 | def8e2f6 | Michael Hanselmann | def _WaitForDisconnect(): |
1932 | def8e2f6 | Michael Hanselmann | if self.GetProcStatus().is_standalone: |
1933 | def8e2f6 | Michael Hanselmann | return
|
1934 | def8e2f6 | Michael Hanselmann | |
1935 | def8e2f6 | Michael Hanselmann | # retry the disconnect, it seems possible that due to a well-time
|
1936 | def8e2f6 | Michael Hanselmann | # disconnect on the peer, my disconnect command might be ignored and
|
1937 | def8e2f6 | Michael Hanselmann | # forgotten
|
1938 | def8e2f6 | Michael Hanselmann | dstatus.ever_disconnected = \ |
1939 | def8e2f6 | Michael Hanselmann | _IgnoreError(self._ShutdownNet, self.minor) or dstatus.ever_disconnected |
1940 | def8e2f6 | Michael Hanselmann | |
1941 | def8e2f6 | Michael Hanselmann | raise utils.RetryAgain()
|
1942 | def8e2f6 | Michael Hanselmann | |
1943 | def8e2f6 | Michael Hanselmann | # Keep start time
|
1944 | def8e2f6 | Michael Hanselmann | start_time = time.time() |
1945 | def8e2f6 | Michael Hanselmann | |
1946 | def8e2f6 | Michael Hanselmann | try:
|
1947 | def8e2f6 | Michael Hanselmann | # Start delay at 100 milliseconds and grow up to 2 seconds
|
1948 | def8e2f6 | Michael Hanselmann | utils.Retry(_WaitForDisconnect, (0.1, 1.5, 2.0), |
1949 | def8e2f6 | Michael Hanselmann | self._NET_RECONFIG_TIMEOUT)
|
1950 | def8e2f6 | Michael Hanselmann | except utils.RetryTimeout:
|
1951 | def8e2f6 | Michael Hanselmann | if dstatus.ever_disconnected:
|
1952 | 82463074 | Iustin Pop | msg = ("drbd%d: device did not react to the"
|
1953 | cf8df3f3 | Iustin Pop | " 'disconnect' command in a timely manner")
|
1954 | cf8df3f3 | Iustin Pop | else:
|
1955 | 82463074 | Iustin Pop | msg = "drbd%d: can't shutdown network, even after multiple retries"
|
1956 | def8e2f6 | Michael Hanselmann | |
1957 | 82463074 | Iustin Pop | _ThrowError(msg, self.minor)
|
1958 | cf8df3f3 | Iustin Pop | |
1959 | def8e2f6 | Michael Hanselmann | reconfig_time = time.time() - start_time |
1960 | def8e2f6 | Michael Hanselmann | if reconfig_time > (self._NET_RECONFIG_TIMEOUT * 0.25): |
1961 | 82463074 | Iustin Pop | logging.info("drbd%d: DisconnectNet: detach took %.3f seconds",
|
1962 | 82463074 | Iustin Pop | self.minor, reconfig_time)
|
1963 | cf8df3f3 | Iustin Pop | |
1964 | cf8df3f3 | Iustin Pop | def AttachNet(self, multimaster): |
1965 | cf8df3f3 | Iustin Pop | """Reconnects the network.
|
1966 | cf8df3f3 | Iustin Pop |
|
1967 | cf8df3f3 | Iustin Pop | This method connects the network side of the device with a
|
1968 | cf8df3f3 | Iustin Pop | specified multi-master flag. The device needs to be 'Standalone'
|
1969 | cf8df3f3 | Iustin Pop | but have valid network configuration data.
|
1970 | cf8df3f3 | Iustin Pop |
|
1971 | cf8df3f3 | Iustin Pop | Args:
|
1972 | cf8df3f3 | Iustin Pop | - multimaster: init the network in dual-primary mode
|
1973 | cf8df3f3 | Iustin Pop |
|
1974 | cf8df3f3 | Iustin Pop | """
|
1975 | cf8df3f3 | Iustin Pop | if self.minor is None: |
1976 | 82463074 | Iustin Pop | _ThrowError("drbd%d: device not attached in AttachNet", self._aminor) |
1977 | cf8df3f3 | Iustin Pop | |
1978 | cf8df3f3 | Iustin Pop | if None in (self._lhost, self._lport, self._rhost, self._rport): |
1979 | 82463074 | Iustin Pop | _ThrowError("drbd%d: missing network info in AttachNet()", self.minor) |
1980 | cf8df3f3 | Iustin Pop | |
1981 | cf8df3f3 | Iustin Pop | status = self.GetProcStatus()
|
1982 | cf8df3f3 | Iustin Pop | |
1983 | cf8df3f3 | Iustin Pop | if not status.is_standalone: |
1984 | 82463074 | Iustin Pop | _ThrowError("drbd%d: device is not standalone in AttachNet", self.minor) |
1985 | cf8df3f3 | Iustin Pop | |
1986 | 1063abd1 | Iustin Pop | self._AssembleNet(self.minor, |
1987 | 1063abd1 | Iustin Pop | (self._lhost, self._lport, self._rhost, self._rport), |
1988 | 1063abd1 | Iustin Pop | constants.DRBD_NET_PROTOCOL, dual_pri=multimaster, |
1989 | 1063abd1 | Iustin Pop | hmac=constants.DRBD_HMAC_ALG, secret=self._secret)
|
1990 | cf8df3f3 | Iustin Pop | |
1991 | a2cfdea2 | Iustin Pop | def Attach(self): |
1992 | 2d0c8319 | Iustin Pop | """Check if our minor is configured.
|
1993 | 2d0c8319 | Iustin Pop |
|
1994 | 2d0c8319 | Iustin Pop | This doesn't do any device configurations - it only checks if the
|
1995 | 2d0c8319 | Iustin Pop | minor is in a state different from Unconfigured.
|
1996 | 2d0c8319 | Iustin Pop |
|
1997 | 2d0c8319 | Iustin Pop | Note that this function will not change the state of the system in
|
1998 | 2d0c8319 | Iustin Pop | any way (except in case of side-effects caused by reading from
|
1999 | 2d0c8319 | Iustin Pop | /proc).
|
2000 | 2d0c8319 | Iustin Pop |
|
2001 | 2d0c8319 | Iustin Pop | """
|
2002 | 6d2e83d5 | Iustin Pop | used_devs = self.GetUsedDevs()
|
2003 | 2d0c8319 | Iustin Pop | if self._aminor in used_devs: |
2004 | 2d0c8319 | Iustin Pop | minor = self._aminor
|
2005 | 2d0c8319 | Iustin Pop | else:
|
2006 | 2d0c8319 | Iustin Pop | minor = None
|
2007 | 2d0c8319 | Iustin Pop | |
2008 | 2d0c8319 | Iustin Pop | self._SetFromMinor(minor)
|
2009 | 2d0c8319 | Iustin Pop | return minor is not None |
2010 | 2d0c8319 | Iustin Pop | |
2011 | 2d0c8319 | Iustin Pop | def Assemble(self): |
2012 | 2d0c8319 | Iustin Pop | """Assemble the drbd.
|
2013 | 2d0c8319 | Iustin Pop |
|
2014 | 2d0c8319 | Iustin Pop | Method:
|
2015 | 2d0c8319 | Iustin Pop | - if we have a configured device, we try to ensure that it matches
|
2016 | 2d0c8319 | Iustin Pop | our config
|
2017 | 2d0c8319 | Iustin Pop | - if not, we create it from zero
|
2018 | d529599f | Andrea Spadaccini | - anyway, set the device parameters
|
2019 | 2d0c8319 | Iustin Pop |
|
2020 | 2d0c8319 | Iustin Pop | """
|
2021 | 1063abd1 | Iustin Pop | super(DRBD8, self).Assemble() |
2022 | 2d0c8319 | Iustin Pop | |
2023 | 2d0c8319 | Iustin Pop | self.Attach()
|
2024 | 2d0c8319 | Iustin Pop | if self.minor is None: |
2025 | 2d0c8319 | Iustin Pop | # local device completely unconfigured
|
2026 | 1063abd1 | Iustin Pop | self._FastAssemble()
|
2027 | 2d0c8319 | Iustin Pop | else:
|
2028 | 2d0c8319 | Iustin Pop | # we have to recheck the local and network status and try to fix
|
2029 | 2d0c8319 | Iustin Pop | # the device
|
2030 | 1063abd1 | Iustin Pop | self._SlowAssemble()
|
2031 | 2d0c8319 | Iustin Pop | |
2032 | 8584e922 | Andrea Spadaccini | sync_errors = self.SetSyncParams(self.params) |
2033 | 8584e922 | Andrea Spadaccini | if sync_errors:
|
2034 | 8584e922 | Andrea Spadaccini | _ThrowError("drbd%d: can't set the synchronization parameters: %s" %
|
2035 | 8584e922 | Andrea Spadaccini | (self.minor, utils.CommaJoin(sync_errors)))
|
2036 | d529599f | Andrea Spadaccini | |
2037 | 2d0c8319 | Iustin Pop | def _SlowAssemble(self): |
2038 | 2d0c8319 | Iustin Pop | """Assembles the DRBD device from a (partially) configured device.
|
2039 | a2cfdea2 | Iustin Pop |
|
2040 | a2cfdea2 | Iustin Pop | In case of partially attached (local device matches but no network
|
2041 | a2cfdea2 | Iustin Pop | setup), we perform the network attach. If successful, we re-test
|
2042 | a2cfdea2 | Iustin Pop | the attach if can return success.
|
2043 | a2cfdea2 | Iustin Pop |
|
2044 | a2cfdea2 | Iustin Pop | """
|
2045 | 527a15ac | Iustin Pop | # TODO: Rewrite to not use a for loop just because there is 'break'
|
2046 | b459a848 | Andrea Spadaccini | # pylint: disable=W0631
|
2047 | 1063abd1 | Iustin Pop | net_data = (self._lhost, self._lport, self._rhost, self._rport) |
2048 | a1578d63 | Iustin Pop | for minor in (self._aminor,): |
2049 | 3840729d | Iustin Pop | info = self._GetDevInfo(self._GetShowData(minor)) |
2050 | a2cfdea2 | Iustin Pop | match_l = self._MatchesLocal(info)
|
2051 | a2cfdea2 | Iustin Pop | match_r = self._MatchesNet(info)
|
2052 | 1063abd1 | Iustin Pop | |
2053 | a2cfdea2 | Iustin Pop | if match_l and match_r: |
2054 | 1063abd1 | Iustin Pop | # everything matches
|
2055 | a2cfdea2 | Iustin Pop | break
|
2056 | 1063abd1 | Iustin Pop | |
2057 | a2cfdea2 | Iustin Pop | if match_l and not match_r and "local_addr" not in info: |
2058 | 1063abd1 | Iustin Pop | # disk matches, but not attached to network, attach and recheck
|
2059 | 1063abd1 | Iustin Pop | self._AssembleNet(minor, net_data, constants.DRBD_NET_PROTOCOL,
|
2060 | 1063abd1 | Iustin Pop | hmac=constants.DRBD_HMAC_ALG, secret=self._secret)
|
2061 | 1063abd1 | Iustin Pop | if self._MatchesNet(self._GetDevInfo(self._GetShowData(minor))): |
2062 | 1063abd1 | Iustin Pop | break
|
2063 | 1063abd1 | Iustin Pop | else:
|
2064 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: network attach successful, but 'drbdsetup"
|
2065 | 1063abd1 | Iustin Pop | " show' disagrees", minor)
|
2066 | 1063abd1 | Iustin Pop | |
2067 | fc1dc9d7 | Iustin Pop | if match_r and "local_dev" not in info: |
2068 | 1063abd1 | Iustin Pop | # no local disk, but network attached and it matches
|
2069 | 1063abd1 | Iustin Pop | self._AssembleLocal(minor, self._children[0].dev_path, |
2070 | f069addf | Iustin Pop | self._children[1].dev_path, self.size) |
2071 | 1063abd1 | Iustin Pop | if self._MatchesNet(self._GetDevInfo(self._GetShowData(minor))): |
2072 | 1063abd1 | Iustin Pop | break
|
2073 | 1063abd1 | Iustin Pop | else:
|
2074 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: disk attach successful, but 'drbdsetup"
|
2075 | 1063abd1 | Iustin Pop | " show' disagrees", minor)
|
2076 | bf25af3b | Iustin Pop | |
2077 | bf25af3b | Iustin Pop | # this case must be considered only if we actually have local
|
2078 | bf25af3b | Iustin Pop | # storage, i.e. not in diskless mode, because all diskless
|
2079 | bf25af3b | Iustin Pop | # devices are equal from the point of view of local
|
2080 | bf25af3b | Iustin Pop | # configuration
|
2081 | bf25af3b | Iustin Pop | if (match_l and "local_dev" in info and |
2082 | bf25af3b | Iustin Pop | not match_r and "local_addr" in info): |
2083 | 9cdbe77f | Iustin Pop | # strange case - the device network part points to somewhere
|
2084 | 9cdbe77f | Iustin Pop | # else, even though its local storage is ours; as we own the
|
2085 | 9cdbe77f | Iustin Pop | # drbd space, we try to disconnect from the remote peer and
|
2086 | 9cdbe77f | Iustin Pop | # reconnect to our correct one
|
2087 | 1063abd1 | Iustin Pop | try:
|
2088 | 1063abd1 | Iustin Pop | self._ShutdownNet(minor)
|
2089 | 1063abd1 | Iustin Pop | except errors.BlockDeviceError, err:
|
2090 | 33bc6f01 | Iustin Pop | _ThrowError("drbd%d: device has correct local storage, wrong"
|
2091 | 33bc6f01 | Iustin Pop | " remote peer and is unable to disconnect in order"
|
2092 | 33bc6f01 | Iustin Pop | " to attach to the correct peer: %s", minor, str(err)) |
2093 | 9cdbe77f | Iustin Pop | # note: _AssembleNet also handles the case when we don't want
|
2094 | 9cdbe77f | Iustin Pop | # local storage (i.e. one or more of the _[lr](host|port) is
|
2095 | 9cdbe77f | Iustin Pop | # None)
|
2096 | 1063abd1 | Iustin Pop | self._AssembleNet(minor, net_data, constants.DRBD_NET_PROTOCOL,
|
2097 | 1063abd1 | Iustin Pop | hmac=constants.DRBD_HMAC_ALG, secret=self._secret)
|
2098 | 1063abd1 | Iustin Pop | if self._MatchesNet(self._GetDevInfo(self._GetShowData(minor))): |
2099 | 9cdbe77f | Iustin Pop | break
|
2100 | 1063abd1 | Iustin Pop | else:
|
2101 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: network attach successful, but 'drbdsetup"
|
2102 | 1063abd1 | Iustin Pop | " show' disagrees", minor)
|
2103 | 9cdbe77f | Iustin Pop | |
2104 | a2cfdea2 | Iustin Pop | else:
|
2105 | a2cfdea2 | Iustin Pop | minor = None
|
2106 | a2cfdea2 | Iustin Pop | |
2107 | a2cfdea2 | Iustin Pop | self._SetFromMinor(minor)
|
2108 | 1063abd1 | Iustin Pop | if minor is None: |
2109 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: cannot activate, unknown or unhandled reason",
|
2110 | 1063abd1 | Iustin Pop | self._aminor)
|
2111 | a2cfdea2 | Iustin Pop | |
2112 | 2d0c8319 | Iustin Pop | def _FastAssemble(self): |
2113 | 2d0c8319 | Iustin Pop | """Assemble the drbd device from zero.
|
2114 | a2cfdea2 | Iustin Pop |
|
2115 | 2d0c8319 | Iustin Pop | This is run when in Assemble we detect our minor is unused.
|
2116 | a2cfdea2 | Iustin Pop |
|
2117 | a2cfdea2 | Iustin Pop | """
|
2118 | a1578d63 | Iustin Pop | minor = self._aminor
|
2119 | fc1dc9d7 | Iustin Pop | if self._children and self._children[0] and self._children[1]: |
2120 | 1063abd1 | Iustin Pop | self._AssembleLocal(minor, self._children[0].dev_path, |
2121 | f069addf | Iustin Pop | self._children[1].dev_path, self.size) |
2122 | a2cfdea2 | Iustin Pop | if self._lhost and self._lport and self._rhost and self._rport: |
2123 | 1063abd1 | Iustin Pop | self._AssembleNet(minor,
|
2124 | 1063abd1 | Iustin Pop | (self._lhost, self._lport, self._rhost, self._rport), |
2125 | 1063abd1 | Iustin Pop | constants.DRBD_NET_PROTOCOL, |
2126 | 1063abd1 | Iustin Pop | hmac=constants.DRBD_HMAC_ALG, secret=self._secret)
|
2127 | a2cfdea2 | Iustin Pop | self._SetFromMinor(minor)
|
2128 | a2cfdea2 | Iustin Pop | |
2129 | a2cfdea2 | Iustin Pop | @classmethod
|
2130 | b00b95dd | Iustin Pop | def _ShutdownLocal(cls, minor): |
2131 | b00b95dd | Iustin Pop | """Detach from the local device.
|
2132 | b00b95dd | Iustin Pop |
|
2133 | b00b95dd | Iustin Pop | I/Os will continue to be served from the remote device. If we
|
2134 | b00b95dd | Iustin Pop | don't have a remote device, this operation will fail.
|
2135 | b00b95dd | Iustin Pop |
|
2136 | b00b95dd | Iustin Pop | """
|
2137 | b00b95dd | Iustin Pop | result = utils.RunCmd(["drbdsetup", cls._DevPath(minor), "detach"]) |
2138 | b00b95dd | Iustin Pop | if result.failed:
|
2139 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: can't detach local disk: %s", minor, result.output)
|
2140 | b00b95dd | Iustin Pop | |
2141 | b00b95dd | Iustin Pop | @classmethod
|
2142 | f3e513ad | Iustin Pop | def _ShutdownNet(cls, minor): |
2143 | f3e513ad | Iustin Pop | """Disconnect from the remote peer.
|
2144 | f3e513ad | Iustin Pop |
|
2145 | f3e513ad | Iustin Pop | This fails if we don't have a local device.
|
2146 | f3e513ad | Iustin Pop |
|
2147 | f3e513ad | Iustin Pop | """
|
2148 | f3e513ad | Iustin Pop | result = utils.RunCmd(["drbdsetup", cls._DevPath(minor), "disconnect"]) |
2149 | a8459f1c | Iustin Pop | if result.failed:
|
2150 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: can't shutdown network: %s", minor, result.output)
|
2151 | f3e513ad | Iustin Pop | |
2152 | f3e513ad | Iustin Pop | @classmethod
|
2153 | a2cfdea2 | Iustin Pop | def _ShutdownAll(cls, minor): |
2154 | a2cfdea2 | Iustin Pop | """Deactivate the device.
|
2155 | a2cfdea2 | Iustin Pop |
|
2156 | a2cfdea2 | Iustin Pop | This will, of course, fail if the device is in use.
|
2157 | a2cfdea2 | Iustin Pop |
|
2158 | a2cfdea2 | Iustin Pop | """
|
2159 | a2cfdea2 | Iustin Pop | result = utils.RunCmd(["drbdsetup", cls._DevPath(minor), "down"]) |
2160 | a2cfdea2 | Iustin Pop | if result.failed:
|
2161 | 33bc6f01 | Iustin Pop | _ThrowError("drbd%d: can't shutdown drbd device: %s",
|
2162 | 33bc6f01 | Iustin Pop | minor, result.output) |
2163 | a2cfdea2 | Iustin Pop | |
2164 | a2cfdea2 | Iustin Pop | def Shutdown(self): |
2165 | a2cfdea2 | Iustin Pop | """Shutdown the DRBD device.
|
2166 | a2cfdea2 | Iustin Pop |
|
2167 | a2cfdea2 | Iustin Pop | """
|
2168 | a2cfdea2 | Iustin Pop | if self.minor is None and not self.Attach(): |
2169 | 746f7476 | Iustin Pop | logging.info("drbd%d: not attached during Shutdown()", self._aminor) |
2170 | 746f7476 | Iustin Pop | return
|
2171 | 746f7476 | Iustin Pop | minor = self.minor
|
2172 | a2cfdea2 | Iustin Pop | self.minor = None |
2173 | a2cfdea2 | Iustin Pop | self.dev_path = None |
2174 | 746f7476 | Iustin Pop | self._ShutdownAll(minor)
|
2175 | a2cfdea2 | Iustin Pop | |
2176 | a2cfdea2 | Iustin Pop | def Remove(self): |
2177 | a2cfdea2 | Iustin Pop | """Stub remove for DRBD devices.
|
2178 | a2cfdea2 | Iustin Pop |
|
2179 | a2cfdea2 | Iustin Pop | """
|
2180 | 0c6c04ec | Iustin Pop | self.Shutdown()
|
2181 | a2cfdea2 | Iustin Pop | |
2182 | a2cfdea2 | Iustin Pop | @classmethod
|
2183 | 94dcbdb0 | Andrea Spadaccini | def Create(cls, unique_id, children, size, params): |
2184 | a2cfdea2 | Iustin Pop | """Create a new DRBD8 device.
|
2185 | a2cfdea2 | Iustin Pop |
|
2186 | a2cfdea2 | Iustin Pop | Since DRBD devices are not created per se, just assembled, this
|
2187 | a2cfdea2 | Iustin Pop | function only initializes the metadata.
|
2188 | a2cfdea2 | Iustin Pop |
|
2189 | a2cfdea2 | Iustin Pop | """
|
2190 | a2cfdea2 | Iustin Pop | if len(children) != 2: |
2191 | a2cfdea2 | Iustin Pop | raise errors.ProgrammerError("Invalid setup for the drbd device") |
2192 | 767d52d3 | Iustin Pop | # check that the minor is unused
|
2193 | 767d52d3 | Iustin Pop | aminor = unique_id[4]
|
2194 | 767d52d3 | Iustin Pop | proc_info = cls._MassageProcData(cls._GetProcData()) |
2195 | 767d52d3 | Iustin Pop | if aminor in proc_info: |
2196 | 767d52d3 | Iustin Pop | status = DRBD8Status(proc_info[aminor]) |
2197 | 767d52d3 | Iustin Pop | in_use = status.is_in_use |
2198 | 767d52d3 | Iustin Pop | else:
|
2199 | 767d52d3 | Iustin Pop | in_use = False
|
2200 | 767d52d3 | Iustin Pop | if in_use:
|
2201 | 33bc6f01 | Iustin Pop | _ThrowError("drbd%d: minor is already in use at Create() time", aminor)
|
2202 | a2cfdea2 | Iustin Pop | meta = children[1]
|
2203 | a2cfdea2 | Iustin Pop | meta.Assemble() |
2204 | a2cfdea2 | Iustin Pop | if not meta.Attach(): |
2205 | 33bc6f01 | Iustin Pop | _ThrowError("drbd%d: can't attach to meta device '%s'",
|
2206 | 33bc6f01 | Iustin Pop | aminor, meta) |
2207 | 9c793cfb | Iustin Pop | cls._CheckMetaSize(meta.dev_path) |
2208 | 3b559640 | Iustin Pop | cls._InitMeta(aminor, meta.dev_path) |
2209 | 94dcbdb0 | Andrea Spadaccini | return cls(unique_id, children, size, params)
|
2210 | a2cfdea2 | Iustin Pop | |
2211 | cad0723b | Iustin Pop | def Grow(self, amount, dryrun, backingstore): |
2212 | 1005d816 | Iustin Pop | """Resize the DRBD device and its backing storage.
|
2213 | 1005d816 | Iustin Pop |
|
2214 | 1005d816 | Iustin Pop | """
|
2215 | 1005d816 | Iustin Pop | if self.minor is None: |
2216 | 82463074 | Iustin Pop | _ThrowError("drbd%d: Grow called while not attached", self._aminor) |
2217 | 1005d816 | Iustin Pop | if len(self._children) != 2 or None in self._children: |
2218 | 82463074 | Iustin Pop | _ThrowError("drbd%d: cannot grow diskless device", self.minor) |
2219 | cad0723b | Iustin Pop | self._children[0].Grow(amount, dryrun, backingstore) |
2220 | cad0723b | Iustin Pop | if dryrun or backingstore: |
2221 | cad0723b | Iustin Pop | # DRBD does not support dry-run mode and is not backing storage,
|
2222 | cad0723b | Iustin Pop | # so we'll return here
|
2223 | 7fe23d47 | Iustin Pop | return
|
2224 | 38256320 | Iustin Pop | result = utils.RunCmd(["drbdsetup", self.dev_path, "resize", "-s", |
2225 | 38256320 | Iustin Pop | "%dm" % (self.size + amount)]) |
2226 | 1005d816 | Iustin Pop | if result.failed:
|
2227 | 82463074 | Iustin Pop | _ThrowError("drbd%d: resize failed: %s", self.minor, result.output) |
2228 | 1005d816 | Iustin Pop | |
2229 | a8083063 | Iustin Pop | |
2230 | 6f695a2e | Manuel Franceschini | class FileStorage(BlockDev): |
2231 | 6f695a2e | Manuel Franceschini | """File device.
|
2232 | abdf0113 | Iustin Pop |
|
2233 | 6f695a2e | Manuel Franceschini | This class represents the a file storage backend device.
|
2234 | 6f695a2e | Manuel Franceschini |
|
2235 | 6f695a2e | Manuel Franceschini | The unique_id for the file device is a (file_driver, file_path) tuple.
|
2236 | abdf0113 | Iustin Pop |
|
2237 | 6f695a2e | Manuel Franceschini | """
|
2238 | 94dcbdb0 | Andrea Spadaccini | def __init__(self, unique_id, children, size, params): |
2239 | 6f695a2e | Manuel Franceschini | """Initalizes a file device backend.
|
2240 | 6f695a2e | Manuel Franceschini |
|
2241 | 6f695a2e | Manuel Franceschini | """
|
2242 | 6f695a2e | Manuel Franceschini | if children:
|
2243 | 6f695a2e | Manuel Franceschini | raise errors.BlockDeviceError("Invalid setup for file device") |
2244 | 94dcbdb0 | Andrea Spadaccini | super(FileStorage, self).__init__(unique_id, children, size, params) |
2245 | 6f695a2e | Manuel Franceschini | if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2: |
2246 | 6f695a2e | Manuel Franceschini | raise ValueError("Invalid configuration data %s" % str(unique_id)) |
2247 | 6f695a2e | Manuel Franceschini | self.driver = unique_id[0] |
2248 | 6f695a2e | Manuel Franceschini | self.dev_path = unique_id[1] |
2249 | ecb091e3 | Iustin Pop | self.Attach()
|
2250 | 6f695a2e | Manuel Franceschini | |
2251 | 6f695a2e | Manuel Franceschini | def Assemble(self): |
2252 | 6f695a2e | Manuel Franceschini | """Assemble the device.
|
2253 | 6f695a2e | Manuel Franceschini |
|
2254 | 6f695a2e | Manuel Franceschini | Checks whether the file device exists, raises BlockDeviceError otherwise.
|
2255 | 6f695a2e | Manuel Franceschini |
|
2256 | 6f695a2e | Manuel Franceschini | """
|
2257 | 6f695a2e | Manuel Franceschini | if not os.path.exists(self.dev_path): |
2258 | 1063abd1 | Iustin Pop | _ThrowError("File device '%s' does not exist" % self.dev_path) |
2259 | 6f695a2e | Manuel Franceschini | |
2260 | 6f695a2e | Manuel Franceschini | def Shutdown(self): |
2261 | 6f695a2e | Manuel Franceschini | """Shutdown the device.
|
2262 | 6f695a2e | Manuel Franceschini |
|
2263 | 5bbd3f7f | Michael Hanselmann | This is a no-op for the file type, as we don't deactivate
|
2264 | 6f695a2e | Manuel Franceschini | the file on shutdown.
|
2265 | 6f695a2e | Manuel Franceschini |
|
2266 | 6f695a2e | Manuel Franceschini | """
|
2267 | 746f7476 | Iustin Pop | pass
|
2268 | 6f695a2e | Manuel Franceschini | |
2269 | 6f695a2e | Manuel Franceschini | def Open(self, force=False): |
2270 | 6f695a2e | Manuel Franceschini | """Make the device ready for I/O.
|
2271 | 6f695a2e | Manuel Franceschini |
|
2272 | 6f695a2e | Manuel Franceschini | This is a no-op for the file type.
|
2273 | 6f695a2e | Manuel Franceschini |
|
2274 | 6f695a2e | Manuel Franceschini | """
|
2275 | 6f695a2e | Manuel Franceschini | pass
|
2276 | 6f695a2e | Manuel Franceschini | |
2277 | 6f695a2e | Manuel Franceschini | def Close(self): |
2278 | 6f695a2e | Manuel Franceschini | """Notifies that the device will no longer be used for I/O.
|
2279 | 6f695a2e | Manuel Franceschini |
|
2280 | 6f695a2e | Manuel Franceschini | This is a no-op for the file type.
|
2281 | 6f695a2e | Manuel Franceschini |
|
2282 | 6f695a2e | Manuel Franceschini | """
|
2283 | 6f695a2e | Manuel Franceschini | pass
|
2284 | 6f695a2e | Manuel Franceschini | |
2285 | 6f695a2e | Manuel Franceschini | def Remove(self): |
2286 | 6f695a2e | Manuel Franceschini | """Remove the file backing the block device.
|
2287 | 6f695a2e | Manuel Franceschini |
|
2288 | c41eea6e | Iustin Pop | @rtype: boolean
|
2289 | c41eea6e | Iustin Pop | @return: True if the removal was successful
|
2290 | 6f695a2e | Manuel Franceschini |
|
2291 | 6f695a2e | Manuel Franceschini | """
|
2292 | 6f695a2e | Manuel Franceschini | try:
|
2293 | 6f695a2e | Manuel Franceschini | os.remove(self.dev_path)
|
2294 | 6f695a2e | Manuel Franceschini | except OSError, err: |
2295 | 0c6c04ec | Iustin Pop | if err.errno != errno.ENOENT:
|
2296 | 0c6c04ec | Iustin Pop | _ThrowError("Can't remove file '%s': %s", self.dev_path, err) |
2297 | 6f695a2e | Manuel Franceschini | |
2298 | bbe4cc16 | Iustin Pop | def Rename(self, new_id): |
2299 | bbe4cc16 | Iustin Pop | """Renames the file.
|
2300 | bbe4cc16 | Iustin Pop |
|
2301 | bbe4cc16 | Iustin Pop | """
|
2302 | bbe4cc16 | Iustin Pop | # TODO: implement rename for file-based storage
|
2303 | bbe4cc16 | Iustin Pop | _ThrowError("Rename is not supported for file-based storage")
|
2304 | bbe4cc16 | Iustin Pop | |
2305 | cad0723b | Iustin Pop | def Grow(self, amount, dryrun, backingstore): |
2306 | bbe4cc16 | Iustin Pop | """Grow the file
|
2307 | bbe4cc16 | Iustin Pop |
|
2308 | bbe4cc16 | Iustin Pop | @param amount: the amount (in mebibytes) to grow with
|
2309 | bbe4cc16 | Iustin Pop |
|
2310 | bbe4cc16 | Iustin Pop | """
|
2311 | cad0723b | Iustin Pop | if not backingstore: |
2312 | cad0723b | Iustin Pop | return
|
2313 | 91e2d9ec | Guido Trotter | # Check that the file exists
|
2314 | 91e2d9ec | Guido Trotter | self.Assemble()
|
2315 | 91e2d9ec | Guido Trotter | current_size = self.GetActualSize()
|
2316 | 91e2d9ec | Guido Trotter | new_size = current_size + amount * 1024 * 1024 |
2317 | 91e2d9ec | Guido Trotter | assert new_size > current_size, "Cannot Grow with a negative amount" |
2318 | 7fe23d47 | Iustin Pop | # We can't really simulate the growth
|
2319 | 7fe23d47 | Iustin Pop | if dryrun:
|
2320 | 7fe23d47 | Iustin Pop | return
|
2321 | 91e2d9ec | Guido Trotter | try:
|
2322 | 91e2d9ec | Guido Trotter | f = open(self.dev_path, "a+") |
2323 | 91e2d9ec | Guido Trotter | f.truncate(new_size) |
2324 | 91e2d9ec | Guido Trotter | f.close() |
2325 | 91e2d9ec | Guido Trotter | except EnvironmentError, err: |
2326 | 91e2d9ec | Guido Trotter | _ThrowError("Error in file growth: %", str(err)) |
2327 | bbe4cc16 | Iustin Pop | |
2328 | 6f695a2e | Manuel Franceschini | def Attach(self): |
2329 | 6f695a2e | Manuel Franceschini | """Attach to an existing file.
|
2330 | 6f695a2e | Manuel Franceschini |
|
2331 | 6f695a2e | Manuel Franceschini | Check if this file already exists.
|
2332 | 6f695a2e | Manuel Franceschini |
|
2333 | c41eea6e | Iustin Pop | @rtype: boolean
|
2334 | c41eea6e | Iustin Pop | @return: True if file exists
|
2335 | 6f695a2e | Manuel Franceschini |
|
2336 | 6f695a2e | Manuel Franceschini | """
|
2337 | ecb091e3 | Iustin Pop | self.attached = os.path.exists(self.dev_path) |
2338 | ecb091e3 | Iustin Pop | return self.attached |
2339 | 6f695a2e | Manuel Franceschini | |
2340 | fcff3897 | Iustin Pop | def GetActualSize(self): |
2341 | fcff3897 | Iustin Pop | """Return the actual disk size.
|
2342 | fcff3897 | Iustin Pop |
|
2343 | fcff3897 | Iustin Pop | @note: the device needs to be active when this is called
|
2344 | fcff3897 | Iustin Pop |
|
2345 | fcff3897 | Iustin Pop | """
|
2346 | fcff3897 | Iustin Pop | assert self.attached, "BlockDevice not attached in GetActualSize()" |
2347 | fcff3897 | Iustin Pop | try:
|
2348 | fcff3897 | Iustin Pop | st = os.stat(self.dev_path)
|
2349 | fcff3897 | Iustin Pop | return st.st_size
|
2350 | fcff3897 | Iustin Pop | except OSError, err: |
2351 | fcff3897 | Iustin Pop | _ThrowError("Can't stat %s: %s", self.dev_path, err) |
2352 | fcff3897 | Iustin Pop | |
2353 | 6f695a2e | Manuel Franceschini | @classmethod
|
2354 | 94dcbdb0 | Andrea Spadaccini | def Create(cls, unique_id, children, size, params): |
2355 | 6f695a2e | Manuel Franceschini | """Create a new file.
|
2356 | 6f695a2e | Manuel Franceschini |
|
2357 | c41eea6e | Iustin Pop | @param size: the size of file in MiB
|
2358 | 6f695a2e | Manuel Franceschini |
|
2359 | c41eea6e | Iustin Pop | @rtype: L{bdev.FileStorage}
|
2360 | c41eea6e | Iustin Pop | @return: an instance of FileStorage
|
2361 | 6f695a2e | Manuel Franceschini |
|
2362 | 6f695a2e | Manuel Franceschini | """
|
2363 | 6f695a2e | Manuel Franceschini | if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2: |
2364 | 6f695a2e | Manuel Franceschini | raise ValueError("Invalid configuration data %s" % str(unique_id)) |
2365 | 6f695a2e | Manuel Franceschini | dev_path = unique_id[1]
|
2366 | 6f695a2e | Manuel Franceschini | try:
|
2367 | cdeefd9b | Guido Trotter | fd = os.open(dev_path, os.O_RDWR | os.O_CREAT | os.O_EXCL) |
2368 | cdeefd9b | Guido Trotter | f = os.fdopen(fd, "w")
|
2369 | 6f695a2e | Manuel Franceschini | f.truncate(size * 1024 * 1024) |
2370 | 6f695a2e | Manuel Franceschini | f.close() |
2371 | cdeefd9b | Guido Trotter | except EnvironmentError, err: |
2372 | cdeefd9b | Guido Trotter | if err.errno == errno.EEXIST:
|
2373 | cdeefd9b | Guido Trotter | _ThrowError("File already existing: %s", dev_path)
|
2374 | 82463074 | Iustin Pop | _ThrowError("Error in file creation: %", str(err)) |
2375 | 6f695a2e | Manuel Franceschini | |
2376 | 94dcbdb0 | Andrea Spadaccini | return FileStorage(unique_id, children, size, params)
|
2377 | 6f695a2e | Manuel Franceschini | |
2378 | 6f695a2e | Manuel Franceschini | |
2379 | b6135bbc | Apollon Oikonomopoulos | class PersistentBlockDevice(BlockDev): |
2380 | b6135bbc | Apollon Oikonomopoulos | """A block device with persistent node
|
2381 | b6135bbc | Apollon Oikonomopoulos |
|
2382 | b6135bbc | Apollon Oikonomopoulos | May be either directly attached, or exposed through DM (e.g. dm-multipath).
|
2383 | b6135bbc | Apollon Oikonomopoulos | udev helpers are probably required to give persistent, human-friendly
|
2384 | b6135bbc | Apollon Oikonomopoulos | names.
|
2385 | b6135bbc | Apollon Oikonomopoulos |
|
2386 | b6135bbc | Apollon Oikonomopoulos | For the time being, pathnames are required to lie under /dev.
|
2387 | b6135bbc | Apollon Oikonomopoulos |
|
2388 | b6135bbc | Apollon Oikonomopoulos | """
|
2389 | 94dcbdb0 | Andrea Spadaccini | def __init__(self, unique_id, children, size, params): |
2390 | b6135bbc | Apollon Oikonomopoulos | """Attaches to a static block device.
|
2391 | b6135bbc | Apollon Oikonomopoulos |
|
2392 | b6135bbc | Apollon Oikonomopoulos | The unique_id is a path under /dev.
|
2393 | b6135bbc | Apollon Oikonomopoulos |
|
2394 | b6135bbc | Apollon Oikonomopoulos | """
|
2395 | 94dcbdb0 | Andrea Spadaccini | super(PersistentBlockDevice, self).__init__(unique_id, children, size, |
2396 | 94dcbdb0 | Andrea Spadaccini | params) |
2397 | b6135bbc | Apollon Oikonomopoulos | if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2: |
2398 | b6135bbc | Apollon Oikonomopoulos | raise ValueError("Invalid configuration data %s" % str(unique_id)) |
2399 | b6135bbc | Apollon Oikonomopoulos | self.dev_path = unique_id[1] |
2400 | d0c8c01d | Iustin Pop | if not os.path.realpath(self.dev_path).startswith("/dev/"): |
2401 | b6135bbc | Apollon Oikonomopoulos | raise ValueError("Full path '%s' lies outside /dev" % |
2402 | b6135bbc | Apollon Oikonomopoulos | os.path.realpath(self.dev_path))
|
2403 | b6135bbc | Apollon Oikonomopoulos | # TODO: this is just a safety guard checking that we only deal with devices
|
2404 | b6135bbc | Apollon Oikonomopoulos | # we know how to handle. In the future this will be integrated with
|
2405 | b6135bbc | Apollon Oikonomopoulos | # external storage backends and possible values will probably be collected
|
2406 | b6135bbc | Apollon Oikonomopoulos | # from the cluster configuration.
|
2407 | b6135bbc | Apollon Oikonomopoulos | if unique_id[0] != constants.BLOCKDEV_DRIVER_MANUAL: |
2408 | b6135bbc | Apollon Oikonomopoulos | raise ValueError("Got persistent block device of invalid type: %s" % |
2409 | b6135bbc | Apollon Oikonomopoulos | unique_id[0])
|
2410 | b6135bbc | Apollon Oikonomopoulos | |
2411 | b6135bbc | Apollon Oikonomopoulos | self.major = self.minor = None |
2412 | b6135bbc | Apollon Oikonomopoulos | self.Attach()
|
2413 | b6135bbc | Apollon Oikonomopoulos | |
2414 | b6135bbc | Apollon Oikonomopoulos | @classmethod
|
2415 | 94dcbdb0 | Andrea Spadaccini | def Create(cls, unique_id, children, size, params): |
2416 | b6135bbc | Apollon Oikonomopoulos | """Create a new device
|
2417 | b6135bbc | Apollon Oikonomopoulos |
|
2418 | b6135bbc | Apollon Oikonomopoulos | This is a noop, we only return a PersistentBlockDevice instance
|
2419 | b6135bbc | Apollon Oikonomopoulos |
|
2420 | b6135bbc | Apollon Oikonomopoulos | """
|
2421 | 94dcbdb0 | Andrea Spadaccini | return PersistentBlockDevice(unique_id, children, 0, params) |
2422 | b6135bbc | Apollon Oikonomopoulos | |
2423 | b6135bbc | Apollon Oikonomopoulos | def Remove(self): |
2424 | b6135bbc | Apollon Oikonomopoulos | """Remove a device
|
2425 | b6135bbc | Apollon Oikonomopoulos |
|
2426 | b6135bbc | Apollon Oikonomopoulos | This is a noop
|
2427 | b6135bbc | Apollon Oikonomopoulos |
|
2428 | b6135bbc | Apollon Oikonomopoulos | """
|
2429 | b6135bbc | Apollon Oikonomopoulos | pass
|
2430 | b6135bbc | Apollon Oikonomopoulos | |
2431 | b6135bbc | Apollon Oikonomopoulos | def Rename(self, new_id): |
2432 | b6135bbc | Apollon Oikonomopoulos | """Rename this device.
|
2433 | b6135bbc | Apollon Oikonomopoulos |
|
2434 | b6135bbc | Apollon Oikonomopoulos | """
|
2435 | b6135bbc | Apollon Oikonomopoulos | _ThrowError("Rename is not supported for PersistentBlockDev storage")
|
2436 | b6135bbc | Apollon Oikonomopoulos | |
2437 | b6135bbc | Apollon Oikonomopoulos | def Attach(self): |
2438 | b6135bbc | Apollon Oikonomopoulos | """Attach to an existing block device.
|
2439 | b6135bbc | Apollon Oikonomopoulos |
|
2440 | b6135bbc | Apollon Oikonomopoulos |
|
2441 | b6135bbc | Apollon Oikonomopoulos | """
|
2442 | b6135bbc | Apollon Oikonomopoulos | self.attached = False |
2443 | b6135bbc | Apollon Oikonomopoulos | try:
|
2444 | b6135bbc | Apollon Oikonomopoulos | st = os.stat(self.dev_path)
|
2445 | b6135bbc | Apollon Oikonomopoulos | except OSError, err: |
2446 | b6135bbc | Apollon Oikonomopoulos | logging.error("Error stat()'ing %s: %s", self.dev_path, str(err)) |
2447 | b6135bbc | Apollon Oikonomopoulos | return False |
2448 | b6135bbc | Apollon Oikonomopoulos | |
2449 | b6135bbc | Apollon Oikonomopoulos | if not stat.S_ISBLK(st.st_mode): |
2450 | b6135bbc | Apollon Oikonomopoulos | logging.error("%s is not a block device", self.dev_path) |
2451 | b6135bbc | Apollon Oikonomopoulos | return False |
2452 | b6135bbc | Apollon Oikonomopoulos | |
2453 | b6135bbc | Apollon Oikonomopoulos | self.major = os.major(st.st_rdev)
|
2454 | b6135bbc | Apollon Oikonomopoulos | self.minor = os.minor(st.st_rdev)
|
2455 | b6135bbc | Apollon Oikonomopoulos | self.attached = True |
2456 | b6135bbc | Apollon Oikonomopoulos | |
2457 | b6135bbc | Apollon Oikonomopoulos | return True |
2458 | b6135bbc | Apollon Oikonomopoulos | |
2459 | b6135bbc | Apollon Oikonomopoulos | def Assemble(self): |
2460 | b6135bbc | Apollon Oikonomopoulos | """Assemble the device.
|
2461 | b6135bbc | Apollon Oikonomopoulos |
|
2462 | b6135bbc | Apollon Oikonomopoulos | """
|
2463 | b6135bbc | Apollon Oikonomopoulos | pass
|
2464 | b6135bbc | Apollon Oikonomopoulos | |
2465 | b6135bbc | Apollon Oikonomopoulos | def Shutdown(self): |
2466 | b6135bbc | Apollon Oikonomopoulos | """Shutdown the device.
|
2467 | b6135bbc | Apollon Oikonomopoulos |
|
2468 | b6135bbc | Apollon Oikonomopoulos | """
|
2469 | b6135bbc | Apollon Oikonomopoulos | pass
|
2470 | b6135bbc | Apollon Oikonomopoulos | |
2471 | b6135bbc | Apollon Oikonomopoulos | def Open(self, force=False): |
2472 | b6135bbc | Apollon Oikonomopoulos | """Make the device ready for I/O.
|
2473 | b6135bbc | Apollon Oikonomopoulos |
|
2474 | b6135bbc | Apollon Oikonomopoulos | """
|
2475 | b6135bbc | Apollon Oikonomopoulos | pass
|
2476 | b6135bbc | Apollon Oikonomopoulos | |
2477 | b6135bbc | Apollon Oikonomopoulos | def Close(self): |
2478 | b6135bbc | Apollon Oikonomopoulos | """Notifies that the device will no longer be used for I/O.
|
2479 | b6135bbc | Apollon Oikonomopoulos |
|
2480 | b6135bbc | Apollon Oikonomopoulos | """
|
2481 | b6135bbc | Apollon Oikonomopoulos | pass
|
2482 | b6135bbc | Apollon Oikonomopoulos | |
2483 | cad0723b | Iustin Pop | def Grow(self, amount, dryrun, backingstore): |
2484 | b6135bbc | Apollon Oikonomopoulos | """Grow the logical volume.
|
2485 | b6135bbc | Apollon Oikonomopoulos |
|
2486 | b6135bbc | Apollon Oikonomopoulos | """
|
2487 | b6135bbc | Apollon Oikonomopoulos | _ThrowError("Grow is not supported for PersistentBlockDev storage")
|
2488 | b6135bbc | Apollon Oikonomopoulos | |
2489 | b6135bbc | Apollon Oikonomopoulos | |
2490 | 7181fba0 | Constantinos Venetsanopoulos | class RADOSBlockDevice(BlockDev): |
2491 | 7181fba0 | Constantinos Venetsanopoulos | """A RADOS Block Device (rbd).
|
2492 | 7181fba0 | Constantinos Venetsanopoulos |
|
2493 | 7181fba0 | Constantinos Venetsanopoulos | This class implements the RADOS Block Device for the backend. You need
|
2494 | 7181fba0 | Constantinos Venetsanopoulos | the rbd kernel driver, the RADOS Tools and a working RADOS cluster for
|
2495 | 7181fba0 | Constantinos Venetsanopoulos | this to be functional.
|
2496 | 7181fba0 | Constantinos Venetsanopoulos |
|
2497 | 7181fba0 | Constantinos Venetsanopoulos | """
|
2498 | 7181fba0 | Constantinos Venetsanopoulos | def __init__(self, unique_id, children, size, params): |
2499 | 7181fba0 | Constantinos Venetsanopoulos | """Attaches to an rbd device.
|
2500 | 7181fba0 | Constantinos Venetsanopoulos |
|
2501 | 7181fba0 | Constantinos Venetsanopoulos | """
|
2502 | 7181fba0 | Constantinos Venetsanopoulos | super(RADOSBlockDevice, self).__init__(unique_id, children, size, params) |
2503 | 7181fba0 | Constantinos Venetsanopoulos | if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2: |
2504 | 7181fba0 | Constantinos Venetsanopoulos | raise ValueError("Invalid configuration data %s" % str(unique_id)) |
2505 | 7181fba0 | Constantinos Venetsanopoulos | |
2506 | 7181fba0 | Constantinos Venetsanopoulos | self.driver, self.rbd_name = unique_id |
2507 | 7181fba0 | Constantinos Venetsanopoulos | |
2508 | 7181fba0 | Constantinos Venetsanopoulos | self.major = self.minor = None |
2509 | 7181fba0 | Constantinos Venetsanopoulos | self.Attach()
|
2510 | 7181fba0 | Constantinos Venetsanopoulos | |
2511 | 7181fba0 | Constantinos Venetsanopoulos | @classmethod
|
2512 | 7181fba0 | Constantinos Venetsanopoulos | def Create(cls, unique_id, children, size, params): |
2513 | 7181fba0 | Constantinos Venetsanopoulos | """Create a new rbd device.
|
2514 | 7181fba0 | Constantinos Venetsanopoulos |
|
2515 | 7181fba0 | Constantinos Venetsanopoulos | Provision a new rbd volume inside a RADOS pool.
|
2516 | 7181fba0 | Constantinos Venetsanopoulos |
|
2517 | 7181fba0 | Constantinos Venetsanopoulos | """
|
2518 | 7181fba0 | Constantinos Venetsanopoulos | if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2: |
2519 | 7181fba0 | Constantinos Venetsanopoulos | raise errors.ProgrammerError("Invalid configuration data %s" % |
2520 | 7181fba0 | Constantinos Venetsanopoulos | str(unique_id))
|
2521 | 7181fba0 | Constantinos Venetsanopoulos | rbd_pool = params[constants.LDP_POOL] |
2522 | 7181fba0 | Constantinos Venetsanopoulos | rbd_name = unique_id[1]
|
2523 | 7181fba0 | Constantinos Venetsanopoulos | |
2524 | 7181fba0 | Constantinos Venetsanopoulos | # Provision a new rbd volume (Image) inside the RADOS cluster.
|
2525 | 7181fba0 | Constantinos Venetsanopoulos | cmd = [constants.RBD_CMD, "create", "-p", rbd_pool, |
2526 | 7181fba0 | Constantinos Venetsanopoulos | rbd_name, "--size", "%s" % size] |
2527 | 7181fba0 | Constantinos Venetsanopoulos | result = utils.RunCmd(cmd) |
2528 | 7181fba0 | Constantinos Venetsanopoulos | if result.failed:
|
2529 | 7181fba0 | Constantinos Venetsanopoulos | _ThrowError("rbd creation failed (%s): %s",
|
2530 | 7181fba0 | Constantinos Venetsanopoulos | result.fail_reason, result.output) |
2531 | 7181fba0 | Constantinos Venetsanopoulos | |
2532 | 7181fba0 | Constantinos Venetsanopoulos | return RADOSBlockDevice(unique_id, children, size, params)
|
2533 | 7181fba0 | Constantinos Venetsanopoulos | |
2534 | 7181fba0 | Constantinos Venetsanopoulos | def Remove(self): |
2535 | 7181fba0 | Constantinos Venetsanopoulos | """Remove the rbd device.
|
2536 | 7181fba0 | Constantinos Venetsanopoulos |
|
2537 | 7181fba0 | Constantinos Venetsanopoulos | """
|
2538 | 7181fba0 | Constantinos Venetsanopoulos | rbd_pool = self.params[constants.LDP_POOL]
|
2539 | 7181fba0 | Constantinos Venetsanopoulos | rbd_name = self.unique_id[1] |
2540 | 7181fba0 | Constantinos Venetsanopoulos | |
2541 | 7181fba0 | Constantinos Venetsanopoulos | if not self.minor and not self.Attach(): |
2542 | 7181fba0 | Constantinos Venetsanopoulos | # The rbd device doesn't exist.
|
2543 | 7181fba0 | Constantinos Venetsanopoulos | return
|
2544 | 7181fba0 | Constantinos Venetsanopoulos | |
2545 | 7181fba0 | Constantinos Venetsanopoulos | # First shutdown the device (remove mappings).
|
2546 | 7181fba0 | Constantinos Venetsanopoulos | self.Shutdown()
|
2547 | 7181fba0 | Constantinos Venetsanopoulos | |
2548 | 7181fba0 | Constantinos Venetsanopoulos | # Remove the actual Volume (Image) from the RADOS cluster.
|
2549 | 7181fba0 | Constantinos Venetsanopoulos | cmd = [constants.RBD_CMD, "rm", "-p", rbd_pool, rbd_name] |
2550 | 7181fba0 | Constantinos Venetsanopoulos | result = utils.RunCmd(cmd) |
2551 | 7181fba0 | Constantinos Venetsanopoulos | if result.failed:
|
2552 | 7181fba0 | Constantinos Venetsanopoulos | _ThrowError("Can't remove Volume from cluster with rbd rm: %s - %s",
|
2553 | 7181fba0 | Constantinos Venetsanopoulos | result.fail_reason, result.output) |
2554 | 7181fba0 | Constantinos Venetsanopoulos | |
2555 | 7181fba0 | Constantinos Venetsanopoulos | def Rename(self, new_id): |
2556 | 7181fba0 | Constantinos Venetsanopoulos | """Rename this device.
|
2557 | 7181fba0 | Constantinos Venetsanopoulos |
|
2558 | 7181fba0 | Constantinos Venetsanopoulos | """
|
2559 | 7181fba0 | Constantinos Venetsanopoulos | pass
|
2560 | 7181fba0 | Constantinos Venetsanopoulos | |
2561 | 7181fba0 | Constantinos Venetsanopoulos | def Attach(self): |
2562 | 7181fba0 | Constantinos Venetsanopoulos | """Attach to an existing rbd device.
|
2563 | 7181fba0 | Constantinos Venetsanopoulos |
|
2564 | 7181fba0 | Constantinos Venetsanopoulos | This method maps the rbd volume that matches our name with
|
2565 | 7181fba0 | Constantinos Venetsanopoulos | an rbd device and then attaches to this device.
|
2566 | 7181fba0 | Constantinos Venetsanopoulos |
|
2567 | 7181fba0 | Constantinos Venetsanopoulos | """
|
2568 | 7181fba0 | Constantinos Venetsanopoulos | self.attached = False |
2569 | 7181fba0 | Constantinos Venetsanopoulos | |
2570 | 7181fba0 | Constantinos Venetsanopoulos | # Map the rbd volume to a block device under /dev
|
2571 | 7181fba0 | Constantinos Venetsanopoulos | self.dev_path = self._MapVolumeToBlockdev(self.unique_id) |
2572 | 7181fba0 | Constantinos Venetsanopoulos | |
2573 | 7181fba0 | Constantinos Venetsanopoulos | try:
|
2574 | 7181fba0 | Constantinos Venetsanopoulos | st = os.stat(self.dev_path)
|
2575 | 7181fba0 | Constantinos Venetsanopoulos | except OSError, err: |
2576 | 7181fba0 | Constantinos Venetsanopoulos | logging.error("Error stat()'ing %s: %s", self.dev_path, str(err)) |
2577 | 7181fba0 | Constantinos Venetsanopoulos | return False |
2578 | 7181fba0 | Constantinos Venetsanopoulos | |
2579 | 7181fba0 | Constantinos Venetsanopoulos | if not stat.S_ISBLK(st.st_mode): |
2580 | 7181fba0 | Constantinos Venetsanopoulos | logging.error("%s is not a block device", self.dev_path) |
2581 | 7181fba0 | Constantinos Venetsanopoulos | return False |
2582 | 7181fba0 | Constantinos Venetsanopoulos | |
2583 | 7181fba0 | Constantinos Venetsanopoulos | self.major = os.major(st.st_rdev)
|
2584 | 7181fba0 | Constantinos Venetsanopoulos | self.minor = os.minor(st.st_rdev)
|
2585 | 7181fba0 | Constantinos Venetsanopoulos | self.attached = True |
2586 | 7181fba0 | Constantinos Venetsanopoulos | |
2587 | 7181fba0 | Constantinos Venetsanopoulos | return True |
2588 | 7181fba0 | Constantinos Venetsanopoulos | |
2589 | 7181fba0 | Constantinos Venetsanopoulos | def _MapVolumeToBlockdev(self, unique_id): |
2590 | 7181fba0 | Constantinos Venetsanopoulos | """Maps existing rbd volumes to block devices.
|
2591 | 7181fba0 | Constantinos Venetsanopoulos |
|
2592 | 7181fba0 | Constantinos Venetsanopoulos | This method should be idempotent if the mapping already exists.
|
2593 | 7181fba0 | Constantinos Venetsanopoulos |
|
2594 | 7181fba0 | Constantinos Venetsanopoulos | @rtype: string
|
2595 | 7181fba0 | Constantinos Venetsanopoulos | @return: the block device path that corresponds to the volume
|
2596 | 7181fba0 | Constantinos Venetsanopoulos |
|
2597 | 7181fba0 | Constantinos Venetsanopoulos | """
|
2598 | 7181fba0 | Constantinos Venetsanopoulos | pool = self.params[constants.LDP_POOL]
|
2599 | 7181fba0 | Constantinos Venetsanopoulos | name = unique_id[1]
|
2600 | 7181fba0 | Constantinos Venetsanopoulos | |
2601 | 7181fba0 | Constantinos Venetsanopoulos | # Check if the mapping already exists.
|
2602 | 7181fba0 | Constantinos Venetsanopoulos | showmap_cmd = [constants.RBD_CMD, "showmapped", "-p", pool] |
2603 | 7181fba0 | Constantinos Venetsanopoulos | result = utils.RunCmd(showmap_cmd) |
2604 | 7181fba0 | Constantinos Venetsanopoulos | if result.failed:
|
2605 | 7181fba0 | Constantinos Venetsanopoulos | _ThrowError("rbd showmapped failed (%s): %s",
|
2606 | 7181fba0 | Constantinos Venetsanopoulos | result.fail_reason, result.output) |
2607 | 7181fba0 | Constantinos Venetsanopoulos | |
2608 | 7181fba0 | Constantinos Venetsanopoulos | rbd_dev = self._ParseRbdShowmappedOutput(result.output, name)
|
2609 | 7181fba0 | Constantinos Venetsanopoulos | |
2610 | 7181fba0 | Constantinos Venetsanopoulos | if rbd_dev:
|
2611 | 7181fba0 | Constantinos Venetsanopoulos | # The mapping exists. Return it.
|
2612 | 7181fba0 | Constantinos Venetsanopoulos | return rbd_dev
|
2613 | 7181fba0 | Constantinos Venetsanopoulos | |
2614 | 7181fba0 | Constantinos Venetsanopoulos | # The mapping doesn't exist. Create it.
|
2615 | 7181fba0 | Constantinos Venetsanopoulos | map_cmd = [constants.RBD_CMD, "map", "-p", pool, name] |
2616 | 7181fba0 | Constantinos Venetsanopoulos | result = utils.RunCmd(map_cmd) |
2617 | 7181fba0 | Constantinos Venetsanopoulos | if result.failed:
|
2618 | 7181fba0 | Constantinos Venetsanopoulos | _ThrowError("rbd map failed (%s): %s",
|
2619 | 7181fba0 | Constantinos Venetsanopoulos | result.fail_reason, result.output) |
2620 | 7181fba0 | Constantinos Venetsanopoulos | |
2621 | 7181fba0 | Constantinos Venetsanopoulos | # Find the corresponding rbd device.
|
2622 | 7181fba0 | Constantinos Venetsanopoulos | showmap_cmd = [constants.RBD_CMD, "showmapped", "-p", pool] |
2623 | 7181fba0 | Constantinos Venetsanopoulos | result = utils.RunCmd(showmap_cmd) |
2624 | 7181fba0 | Constantinos Venetsanopoulos | if result.failed:
|
2625 | 7181fba0 | Constantinos Venetsanopoulos | _ThrowError("rbd map succeeded, but showmapped failed (%s): %s",
|
2626 | 7181fba0 | Constantinos Venetsanopoulos | result.fail_reason, result.output) |
2627 | 7181fba0 | Constantinos Venetsanopoulos | |
2628 | 7181fba0 | Constantinos Venetsanopoulos | rbd_dev = self._ParseRbdShowmappedOutput(result.output, name)
|
2629 | 7181fba0 | Constantinos Venetsanopoulos | |
2630 | 7181fba0 | Constantinos Venetsanopoulos | if not rbd_dev: |
2631 | 7181fba0 | Constantinos Venetsanopoulos | _ThrowError("rbd map succeeded, but could not find the rbd block"
|
2632 | 7181fba0 | Constantinos Venetsanopoulos | " device in output of showmapped, for volume: %s", name)
|
2633 | 7181fba0 | Constantinos Venetsanopoulos | |
2634 | 7181fba0 | Constantinos Venetsanopoulos | # The device was successfully mapped. Return it.
|
2635 | 7181fba0 | Constantinos Venetsanopoulos | return rbd_dev
|
2636 | 7181fba0 | Constantinos Venetsanopoulos | |
2637 | 7181fba0 | Constantinos Venetsanopoulos | @staticmethod
|
2638 | 7181fba0 | Constantinos Venetsanopoulos | def _ParseRbdShowmappedOutput(output, volume_name): |
2639 | 7181fba0 | Constantinos Venetsanopoulos | """Parse the output of `rbd showmapped'.
|
2640 | 7181fba0 | Constantinos Venetsanopoulos |
|
2641 | 7181fba0 | Constantinos Venetsanopoulos | This method parses the output of `rbd showmapped' and returns
|
2642 | 7181fba0 | Constantinos Venetsanopoulos | the rbd block device path (e.g. /dev/rbd0) that matches the
|
2643 | 7181fba0 | Constantinos Venetsanopoulos | given rbd volume.
|
2644 | 7181fba0 | Constantinos Venetsanopoulos |
|
2645 | 7181fba0 | Constantinos Venetsanopoulos | @type output: string
|
2646 | 7181fba0 | Constantinos Venetsanopoulos | @param output: the whole output of `rbd showmapped'
|
2647 | 7181fba0 | Constantinos Venetsanopoulos | @type volume_name: string
|
2648 | 7181fba0 | Constantinos Venetsanopoulos | @param volume_name: the name of the volume whose device we search for
|
2649 | 7181fba0 | Constantinos Venetsanopoulos | @rtype: string or None
|
2650 | 7181fba0 | Constantinos Venetsanopoulos | @return: block device path if the volume is mapped, else None
|
2651 | 7181fba0 | Constantinos Venetsanopoulos |
|
2652 | 7181fba0 | Constantinos Venetsanopoulos | """
|
2653 | 7181fba0 | Constantinos Venetsanopoulos | allfields = 5
|
2654 | 7181fba0 | Constantinos Venetsanopoulos | volumefield = 2
|
2655 | 7181fba0 | Constantinos Venetsanopoulos | devicefield = 4
|
2656 | 7181fba0 | Constantinos Venetsanopoulos | |
2657 | 7181fba0 | Constantinos Venetsanopoulos | field_sep = "\t"
|
2658 | 7181fba0 | Constantinos Venetsanopoulos | |
2659 | 7181fba0 | Constantinos Venetsanopoulos | lines = output.splitlines() |
2660 | 7181fba0 | Constantinos Venetsanopoulos | splitted_lines = map(lambda l: l.split(field_sep), lines) |
2661 | 7181fba0 | Constantinos Venetsanopoulos | |
2662 | 7181fba0 | Constantinos Venetsanopoulos | # Check empty output.
|
2663 | 7181fba0 | Constantinos Venetsanopoulos | if not splitted_lines: |
2664 | 7181fba0 | Constantinos Venetsanopoulos | _ThrowError("rbd showmapped returned empty output")
|
2665 | 7181fba0 | Constantinos Venetsanopoulos | |
2666 | 7181fba0 | Constantinos Venetsanopoulos | # Check showmapped header line, to determine number of fields.
|
2667 | 7181fba0 | Constantinos Venetsanopoulos | field_cnt = len(splitted_lines[0]) |
2668 | 7181fba0 | Constantinos Venetsanopoulos | if field_cnt != allfields:
|
2669 | 7181fba0 | Constantinos Venetsanopoulos | _ThrowError("Cannot parse rbd showmapped output because its format"
|
2670 | 7181fba0 | Constantinos Venetsanopoulos | " seems to have changed; expected %s fields, found %s",
|
2671 | 7181fba0 | Constantinos Venetsanopoulos | allfields, field_cnt) |
2672 | 7181fba0 | Constantinos Venetsanopoulos | |
2673 | 7181fba0 | Constantinos Venetsanopoulos | matched_lines = \ |
2674 | 7181fba0 | Constantinos Venetsanopoulos | filter(lambda l: len(l) == allfields and l[volumefield] == volume_name, |
2675 | 7181fba0 | Constantinos Venetsanopoulos | splitted_lines) |
2676 | 7181fba0 | Constantinos Venetsanopoulos | |
2677 | 7181fba0 | Constantinos Venetsanopoulos | if len(matched_lines) > 1: |
2678 | 7181fba0 | Constantinos Venetsanopoulos | _ThrowError("The rbd volume %s is mapped more than once."
|
2679 | 7181fba0 | Constantinos Venetsanopoulos | " This shouldn't happen, try to unmap the extra"
|
2680 | 7181fba0 | Constantinos Venetsanopoulos | " devices manually.", volume_name)
|
2681 | 7181fba0 | Constantinos Venetsanopoulos | |
2682 | 7181fba0 | Constantinos Venetsanopoulos | if matched_lines:
|
2683 | 7181fba0 | Constantinos Venetsanopoulos | # rbd block device found. Return it.
|
2684 | 7181fba0 | Constantinos Venetsanopoulos | rbd_dev = matched_lines[0][devicefield]
|
2685 | 7181fba0 | Constantinos Venetsanopoulos | return rbd_dev
|
2686 | 7181fba0 | Constantinos Venetsanopoulos | |
2687 | 7181fba0 | Constantinos Venetsanopoulos | # The given volume is not mapped.
|
2688 | 7181fba0 | Constantinos Venetsanopoulos | return None |
2689 | 7181fba0 | Constantinos Venetsanopoulos | |
2690 | 7181fba0 | Constantinos Venetsanopoulos | def Assemble(self): |
2691 | 7181fba0 | Constantinos Venetsanopoulos | """Assemble the device.
|
2692 | 7181fba0 | Constantinos Venetsanopoulos |
|
2693 | 7181fba0 | Constantinos Venetsanopoulos | """
|
2694 | 7181fba0 | Constantinos Venetsanopoulos | pass
|
2695 | 7181fba0 | Constantinos Venetsanopoulos | |
2696 | 7181fba0 | Constantinos Venetsanopoulos | def Shutdown(self): |
2697 | 7181fba0 | Constantinos Venetsanopoulos | """Shutdown the device.
|
2698 | 7181fba0 | Constantinos Venetsanopoulos |
|
2699 | 7181fba0 | Constantinos Venetsanopoulos | """
|
2700 | 7181fba0 | Constantinos Venetsanopoulos | if not self.minor and not self.Attach(): |
2701 | 7181fba0 | Constantinos Venetsanopoulos | # The rbd device doesn't exist.
|
2702 | 7181fba0 | Constantinos Venetsanopoulos | return
|
2703 | 7181fba0 | Constantinos Venetsanopoulos | |
2704 | 7181fba0 | Constantinos Venetsanopoulos | # Unmap the block device from the Volume.
|
2705 | 7181fba0 | Constantinos Venetsanopoulos | self._UnmapVolumeFromBlockdev(self.unique_id) |
2706 | 7181fba0 | Constantinos Venetsanopoulos | |
2707 | 7181fba0 | Constantinos Venetsanopoulos | self.minor = None |
2708 | 7181fba0 | Constantinos Venetsanopoulos | self.dev_path = None |
2709 | 7181fba0 | Constantinos Venetsanopoulos | |
2710 | 7181fba0 | Constantinos Venetsanopoulos | def _UnmapVolumeFromBlockdev(self, unique_id): |
2711 | 7181fba0 | Constantinos Venetsanopoulos | """Unmaps the rbd device from the Volume it is mapped.
|
2712 | 7181fba0 | Constantinos Venetsanopoulos |
|
2713 | 7181fba0 | Constantinos Venetsanopoulos | Unmaps the rbd device from the Volume it was previously mapped to.
|
2714 | 7181fba0 | Constantinos Venetsanopoulos | This method should be idempotent if the Volume isn't mapped.
|
2715 | 7181fba0 | Constantinos Venetsanopoulos |
|
2716 | 7181fba0 | Constantinos Venetsanopoulos | """
|
2717 | 7181fba0 | Constantinos Venetsanopoulos | pool = self.params[constants.LDP_POOL]
|
2718 | 7181fba0 | Constantinos Venetsanopoulos | name = unique_id[1]
|
2719 | 7181fba0 | Constantinos Venetsanopoulos | |
2720 | 7181fba0 | Constantinos Venetsanopoulos | # Check if the mapping already exists.
|
2721 | 7181fba0 | Constantinos Venetsanopoulos | showmap_cmd = [constants.RBD_CMD, "showmapped", "-p", pool] |
2722 | 7181fba0 | Constantinos Venetsanopoulos | result = utils.RunCmd(showmap_cmd) |
2723 | 7181fba0 | Constantinos Venetsanopoulos | if result.failed:
|
2724 | 7181fba0 | Constantinos Venetsanopoulos | _ThrowError("rbd showmapped failed [during unmap](%s): %s",
|
2725 | 7181fba0 | Constantinos Venetsanopoulos | result.fail_reason, result.output) |
2726 | 7181fba0 | Constantinos Venetsanopoulos | |
2727 | 7181fba0 | Constantinos Venetsanopoulos | rbd_dev = self._ParseRbdShowmappedOutput(result.output, name)
|
2728 | 7181fba0 | Constantinos Venetsanopoulos | |
2729 | 7181fba0 | Constantinos Venetsanopoulos | if rbd_dev:
|
2730 | 7181fba0 | Constantinos Venetsanopoulos | # The mapping exists. Unmap the rbd device.
|
2731 | 7181fba0 | Constantinos Venetsanopoulos | unmap_cmd = [constants.RBD_CMD, "unmap", "%s" % rbd_dev] |
2732 | 7181fba0 | Constantinos Venetsanopoulos | result = utils.RunCmd(unmap_cmd) |
2733 | 7181fba0 | Constantinos Venetsanopoulos | if result.failed:
|
2734 | 7181fba0 | Constantinos Venetsanopoulos | _ThrowError("rbd unmap failed (%s): %s",
|
2735 | 7181fba0 | Constantinos Venetsanopoulos | result.fail_reason, result.output) |
2736 | 7181fba0 | Constantinos Venetsanopoulos | |
2737 | 7181fba0 | Constantinos Venetsanopoulos | def Open(self, force=False): |
2738 | 7181fba0 | Constantinos Venetsanopoulos | """Make the device ready for I/O.
|
2739 | 7181fba0 | Constantinos Venetsanopoulos |
|
2740 | 7181fba0 | Constantinos Venetsanopoulos | """
|
2741 | 7181fba0 | Constantinos Venetsanopoulos | pass
|
2742 | 7181fba0 | Constantinos Venetsanopoulos | |
2743 | 7181fba0 | Constantinos Venetsanopoulos | def Close(self): |
2744 | 7181fba0 | Constantinos Venetsanopoulos | """Notifies that the device will no longer be used for I/O.
|
2745 | 7181fba0 | Constantinos Venetsanopoulos |
|
2746 | 7181fba0 | Constantinos Venetsanopoulos | """
|
2747 | 7181fba0 | Constantinos Venetsanopoulos | pass
|
2748 | 7181fba0 | Constantinos Venetsanopoulos | |
2749 | cad0723b | Iustin Pop | def Grow(self, amount, dryrun, backingstore): |
2750 | 7181fba0 | Constantinos Venetsanopoulos | """Grow the Volume.
|
2751 | 7181fba0 | Constantinos Venetsanopoulos |
|
2752 | 7181fba0 | Constantinos Venetsanopoulos | @type amount: integer
|
2753 | 7181fba0 | Constantinos Venetsanopoulos | @param amount: the amount (in mebibytes) to grow with
|
2754 | 7181fba0 | Constantinos Venetsanopoulos | @type dryrun: boolean
|
2755 | 7181fba0 | Constantinos Venetsanopoulos | @param dryrun: whether to execute the operation in simulation mode
|
2756 | 7181fba0 | Constantinos Venetsanopoulos | only, without actually increasing the size
|
2757 | 7181fba0 | Constantinos Venetsanopoulos |
|
2758 | 7181fba0 | Constantinos Venetsanopoulos | """
|
2759 | cad0723b | Iustin Pop | if not backingstore: |
2760 | cad0723b | Iustin Pop | return
|
2761 | 7181fba0 | Constantinos Venetsanopoulos | if not self.Attach(): |
2762 | 7181fba0 | Constantinos Venetsanopoulos | _ThrowError("Can't attach to rbd device during Grow()")
|
2763 | 7181fba0 | Constantinos Venetsanopoulos | |
2764 | 7181fba0 | Constantinos Venetsanopoulos | if dryrun:
|
2765 | 7181fba0 | Constantinos Venetsanopoulos | # the rbd tool does not support dry runs of resize operations.
|
2766 | 7181fba0 | Constantinos Venetsanopoulos | # Since rbd volumes are thinly provisioned, we assume
|
2767 | 7181fba0 | Constantinos Venetsanopoulos | # there is always enough free space for the operation.
|
2768 | 7181fba0 | Constantinos Venetsanopoulos | return
|
2769 | 7181fba0 | Constantinos Venetsanopoulos | |
2770 | 7181fba0 | Constantinos Venetsanopoulos | rbd_pool = self.params[constants.LDP_POOL]
|
2771 | 7181fba0 | Constantinos Venetsanopoulos | rbd_name = self.unique_id[1] |
2772 | 7181fba0 | Constantinos Venetsanopoulos | new_size = self.size + amount
|
2773 | 7181fba0 | Constantinos Venetsanopoulos | |
2774 | 7181fba0 | Constantinos Venetsanopoulos | # Resize the rbd volume (Image) inside the RADOS cluster.
|
2775 | 7181fba0 | Constantinos Venetsanopoulos | cmd = [constants.RBD_CMD, "resize", "-p", rbd_pool, |
2776 | 7181fba0 | Constantinos Venetsanopoulos | rbd_name, "--size", "%s" % new_size] |
2777 | 7181fba0 | Constantinos Venetsanopoulos | result = utils.RunCmd(cmd) |
2778 | 7181fba0 | Constantinos Venetsanopoulos | if result.failed:
|
2779 | 7181fba0 | Constantinos Venetsanopoulos | _ThrowError("rbd resize failed (%s): %s",
|
2780 | 7181fba0 | Constantinos Venetsanopoulos | result.fail_reason, result.output) |
2781 | 7181fba0 | Constantinos Venetsanopoulos | |
2782 | 7181fba0 | Constantinos Venetsanopoulos | |
2783 | a8083063 | Iustin Pop | DEV_MAP = { |
2784 | fe96220b | Iustin Pop | constants.LD_LV: LogicalVolume, |
2785 | a1f445d3 | Iustin Pop | constants.LD_DRBD8: DRBD8, |
2786 | b6135bbc | Apollon Oikonomopoulos | constants.LD_BLOCKDEV: PersistentBlockDevice, |
2787 | 7181fba0 | Constantinos Venetsanopoulos | constants.LD_RBD: RADOSBlockDevice, |
2788 | a8083063 | Iustin Pop | } |
2789 | a8083063 | Iustin Pop | |
2790 | 4b97f902 | Apollon Oikonomopoulos | if constants.ENABLE_FILE_STORAGE or constants.ENABLE_SHARED_FILE_STORAGE: |
2791 | cb7c0198 | Iustin Pop | DEV_MAP[constants.LD_FILE] = FileStorage |
2792 | cb7c0198 | Iustin Pop | |
2793 | a8083063 | Iustin Pop | |
2794 | 94dcbdb0 | Andrea Spadaccini | def _VerifyDiskType(dev_type): |
2795 | 94dcbdb0 | Andrea Spadaccini | if dev_type not in DEV_MAP: |
2796 | 94dcbdb0 | Andrea Spadaccini | raise errors.ProgrammerError("Invalid block device type '%s'" % dev_type) |
2797 | 94dcbdb0 | Andrea Spadaccini | |
2798 | 94dcbdb0 | Andrea Spadaccini | |
2799 | 5ff82cc9 | René Nussbaumer | def _VerifyDiskParams(disk): |
2800 | 5ff82cc9 | René Nussbaumer | """Verifies if all disk parameters are set.
|
2801 | 5ff82cc9 | René Nussbaumer |
|
2802 | 5ff82cc9 | René Nussbaumer | """
|
2803 | 5ff82cc9 | René Nussbaumer | missing = set(constants.DISK_LD_DEFAULTS[disk.dev_type]) - set(disk.params) |
2804 | 5ff82cc9 | René Nussbaumer | if missing:
|
2805 | 5ff82cc9 | René Nussbaumer | raise errors.ProgrammerError("Block device is missing disk parameters: %s" % |
2806 | 5ff82cc9 | René Nussbaumer | missing) |
2807 | 5ff82cc9 | René Nussbaumer | |
2808 | 5ff82cc9 | René Nussbaumer | |
2809 | 94dcbdb0 | Andrea Spadaccini | def FindDevice(disk, children): |
2810 | a8083063 | Iustin Pop | """Search for an existing, assembled device.
|
2811 | a8083063 | Iustin Pop |
|
2812 | a8083063 | Iustin Pop | This will succeed only if the device exists and is assembled, but it
|
2813 | a8083063 | Iustin Pop | does not do any actions in order to activate the device.
|
2814 | a8083063 | Iustin Pop |
|
2815 | 94dcbdb0 | Andrea Spadaccini | @type disk: L{objects.Disk}
|
2816 | 94dcbdb0 | Andrea Spadaccini | @param disk: the disk object to find
|
2817 | 94dcbdb0 | Andrea Spadaccini | @type children: list of L{bdev.BlockDev}
|
2818 | 94dcbdb0 | Andrea Spadaccini | @param children: the list of block devices that are children of the device
|
2819 | 94dcbdb0 | Andrea Spadaccini | represented by the disk parameter
|
2820 | 94dcbdb0 | Andrea Spadaccini |
|
2821 | a8083063 | Iustin Pop | """
|
2822 | 94dcbdb0 | Andrea Spadaccini | _VerifyDiskType(disk.dev_type) |
2823 | 94dcbdb0 | Andrea Spadaccini | device = DEV_MAP[disk.dev_type](disk.physical_id, children, disk.size, |
2824 | c7c6606d | René Nussbaumer | disk.params) |
2825 | cb999543 | Iustin Pop | if not device.attached: |
2826 | a8083063 | Iustin Pop | return None |
2827 | ecb091e3 | Iustin Pop | return device
|
2828 | a8083063 | Iustin Pop | |
2829 | a8083063 | Iustin Pop | |
2830 | 94dcbdb0 | Andrea Spadaccini | def Assemble(disk, children): |
2831 | a8083063 | Iustin Pop | """Try to attach or assemble an existing device.
|
2832 | a8083063 | Iustin Pop |
|
2833 | f96e3c4f | Iustin Pop | This will attach to assemble the device, as needed, to bring it
|
2834 | f96e3c4f | Iustin Pop | fully up. It must be safe to run on already-assembled devices.
|
2835 | a8083063 | Iustin Pop |
|
2836 | 94dcbdb0 | Andrea Spadaccini | @type disk: L{objects.Disk}
|
2837 | 94dcbdb0 | Andrea Spadaccini | @param disk: the disk object to assemble
|
2838 | 94dcbdb0 | Andrea Spadaccini | @type children: list of L{bdev.BlockDev}
|
2839 | 94dcbdb0 | Andrea Spadaccini | @param children: the list of block devices that are children of the device
|
2840 | 94dcbdb0 | Andrea Spadaccini | represented by the disk parameter
|
2841 | 94dcbdb0 | Andrea Spadaccini |
|
2842 | a8083063 | Iustin Pop | """
|
2843 | 94dcbdb0 | Andrea Spadaccini | _VerifyDiskType(disk.dev_type) |
2844 | 5ff82cc9 | René Nussbaumer | _VerifyDiskParams(disk) |
2845 | 94dcbdb0 | Andrea Spadaccini | device = DEV_MAP[disk.dev_type](disk.physical_id, children, disk.size, |
2846 | c7c6606d | René Nussbaumer | disk.params) |
2847 | 1063abd1 | Iustin Pop | device.Assemble() |
2848 | a8083063 | Iustin Pop | return device
|
2849 | a8083063 | Iustin Pop | |
2850 | a8083063 | Iustin Pop | |
2851 | 94dcbdb0 | Andrea Spadaccini | def Create(disk, children): |
2852 | a8083063 | Iustin Pop | """Create a device.
|
2853 | a8083063 | Iustin Pop |
|
2854 | 94dcbdb0 | Andrea Spadaccini | @type disk: L{objects.Disk}
|
2855 | 94dcbdb0 | Andrea Spadaccini | @param disk: the disk object to create
|
2856 | 94dcbdb0 | Andrea Spadaccini | @type children: list of L{bdev.BlockDev}
|
2857 | 94dcbdb0 | Andrea Spadaccini | @param children: the list of block devices that are children of the device
|
2858 | 94dcbdb0 | Andrea Spadaccini | represented by the disk parameter
|
2859 | 94dcbdb0 | Andrea Spadaccini |
|
2860 | a8083063 | Iustin Pop | """
|
2861 | 94dcbdb0 | Andrea Spadaccini | _VerifyDiskType(disk.dev_type) |
2862 | 5ff82cc9 | René Nussbaumer | _VerifyDiskParams(disk) |
2863 | 94dcbdb0 | Andrea Spadaccini | device = DEV_MAP[disk.dev_type].Create(disk.physical_id, children, disk.size, |
2864 | c7c6606d | René Nussbaumer | disk.params) |
2865 | a8083063 | Iustin Pop | return device |