root / lib / bdev.py @ d1908b41
History | View | Annotate | Download (53 kB)
1 | 2f31098c | Iustin Pop | #
|
---|---|---|---|
2 | a8083063 | Iustin Pop | #
|
3 | a8083063 | Iustin Pop | |
4 | a8083063 | Iustin Pop | # Copyright (C) 2006, 2007 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 | a2cfdea2 | Iustin Pop | import pyparsing as pyp |
28 | 6f695a2e | Manuel Franceschini | import os |
29 | 468c5f77 | Iustin Pop | import logging |
30 | a8083063 | Iustin Pop | |
31 | a8083063 | Iustin Pop | from ganeti import utils |
32 | a8083063 | Iustin Pop | from ganeti import errors |
33 | fe96220b | Iustin Pop | from ganeti import constants |
34 | a8083063 | Iustin Pop | |
35 | a8083063 | Iustin Pop | |
36 | 82463074 | Iustin Pop | def _IgnoreError(fn, *args, **kwargs): |
37 | 82463074 | Iustin Pop | """Executes the given function, ignoring BlockDeviceErrors.
|
38 | 82463074 | Iustin Pop |
|
39 | 82463074 | Iustin Pop | This is used in order to simplify the execution of cleanup or
|
40 | 82463074 | Iustin Pop | rollback functions.
|
41 | 82463074 | Iustin Pop |
|
42 | 82463074 | Iustin Pop | @rtype: boolean
|
43 | 82463074 | Iustin Pop | @return: True when fn didn't raise an exception, False otherwise
|
44 | 82463074 | Iustin Pop |
|
45 | 82463074 | Iustin Pop | """
|
46 | 82463074 | Iustin Pop | try:
|
47 | 82463074 | Iustin Pop | fn(*args, **kwargs) |
48 | 82463074 | Iustin Pop | return True |
49 | 82463074 | Iustin Pop | except errors.BlockDeviceError, err:
|
50 | 82463074 | Iustin Pop | logging.warning("Caught BlockDeviceError but ignoring: %s" % str(err)) |
51 | 82463074 | Iustin Pop | return False |
52 | 82463074 | Iustin Pop | |
53 | 82463074 | Iustin Pop | |
54 | 82463074 | Iustin Pop | def _ThrowError(msg, *args): |
55 | 82463074 | Iustin Pop | """Log an error to the node daemon and the raise an exception.
|
56 | 82463074 | Iustin Pop |
|
57 | 82463074 | Iustin Pop | @type msg: string
|
58 | 82463074 | Iustin Pop | @param msg: the text of the exception
|
59 | 82463074 | Iustin Pop | @raise errors.BlockDeviceError
|
60 | 82463074 | Iustin Pop |
|
61 | 82463074 | Iustin Pop | """
|
62 | 82463074 | Iustin Pop | if args:
|
63 | 82463074 | Iustin Pop | msg = msg % args |
64 | 82463074 | Iustin Pop | logging.error(msg) |
65 | 82463074 | Iustin Pop | raise errors.BlockDeviceError(msg)
|
66 | 82463074 | Iustin Pop | |
67 | 82463074 | Iustin Pop | |
68 | a8083063 | Iustin Pop | class BlockDev(object): |
69 | a8083063 | Iustin Pop | """Block device abstract class.
|
70 | a8083063 | Iustin Pop |
|
71 | a8083063 | Iustin Pop | A block device can be in the following states:
|
72 | a8083063 | Iustin Pop | - not existing on the system, and by `Create()` it goes into:
|
73 | a8083063 | Iustin Pop | - existing but not setup/not active, and by `Assemble()` goes into:
|
74 | a8083063 | Iustin Pop | - active read-write and by `Open()` it goes into
|
75 | a8083063 | Iustin Pop | - online (=used, or ready for use)
|
76 | a8083063 | Iustin Pop |
|
77 | a8083063 | Iustin Pop | A device can also be online but read-only, however we are not using
|
78 | abdf0113 | Iustin Pop | the readonly state (LV has it, if needed in the future) and we are
|
79 | abdf0113 | Iustin Pop | usually looking at this like at a stack, so it's easier to
|
80 | abdf0113 | Iustin Pop | conceptualise the transition from not-existing to online and back
|
81 | a8083063 | Iustin Pop | like a linear one.
|
82 | a8083063 | Iustin Pop |
|
83 | a8083063 | Iustin Pop | The many different states of the device are due to the fact that we
|
84 | a8083063 | Iustin Pop | need to cover many device types:
|
85 | a8083063 | Iustin Pop | - logical volumes are created, lvchange -a y $lv, and used
|
86 | a8083063 | Iustin Pop | - drbd devices are attached to a local disk/remote peer and made primary
|
87 | a8083063 | Iustin Pop |
|
88 | a8083063 | Iustin Pop | A block device is identified by three items:
|
89 | a8083063 | Iustin Pop | - the /dev path of the device (dynamic)
|
90 | a8083063 | Iustin Pop | - a unique ID of the device (static)
|
91 | a8083063 | Iustin Pop | - it's major/minor pair (dynamic)
|
92 | a8083063 | Iustin Pop |
|
93 | a8083063 | Iustin Pop | Not all devices implement both the first two as distinct items. LVM
|
94 | a8083063 | Iustin Pop | logical volumes have their unique ID (the pair volume group, logical
|
95 | abdf0113 | Iustin Pop | volume name) in a 1-to-1 relation to the dev path. For DRBD devices,
|
96 | abdf0113 | Iustin Pop | the /dev path is again dynamic and the unique id is the pair (host1,
|
97 | abdf0113 | Iustin Pop | dev1), (host2, dev2).
|
98 | a8083063 | Iustin Pop |
|
99 | a8083063 | Iustin Pop | You can get to a device in two ways:
|
100 | a8083063 | Iustin Pop | - creating the (real) device, which returns you
|
101 | abdf0113 | Iustin Pop | an attached instance (lvcreate)
|
102 | a8083063 | Iustin Pop | - attaching of a python instance to an existing (real) device
|
103 | a8083063 | Iustin Pop |
|
104 | a8083063 | Iustin Pop | The second point, the attachement to a device, is different
|
105 | a8083063 | Iustin Pop | depending on whether the device is assembled or not. At init() time,
|
106 | a8083063 | Iustin Pop | we search for a device with the same unique_id as us. If found,
|
107 | a8083063 | Iustin Pop | good. It also means that the device is already assembled. If not,
|
108 | a8083063 | Iustin Pop | after assembly we'll have our correct major/minor.
|
109 | a8083063 | Iustin Pop |
|
110 | a8083063 | Iustin Pop | """
|
111 | a8083063 | Iustin Pop | def __init__(self, unique_id, children): |
112 | a8083063 | Iustin Pop | self._children = children
|
113 | a8083063 | Iustin Pop | self.dev_path = None |
114 | a8083063 | Iustin Pop | self.unique_id = unique_id
|
115 | a8083063 | Iustin Pop | self.major = None |
116 | a8083063 | Iustin Pop | self.minor = None |
117 | cb999543 | Iustin Pop | self.attached = False |
118 | a8083063 | Iustin Pop | |
119 | a8083063 | Iustin Pop | def Assemble(self): |
120 | a8083063 | Iustin Pop | """Assemble the device from its components.
|
121 | a8083063 | Iustin Pop |
|
122 | f87548b5 | Iustin Pop | Implementations of this method by child classes must ensure that:
|
123 | f87548b5 | Iustin Pop | - after the device has been assembled, it knows its major/minor
|
124 | f87548b5 | Iustin Pop | numbers; this allows other devices (usually parents) to probe
|
125 | f87548b5 | Iustin Pop | correctly for their children
|
126 | f87548b5 | Iustin Pop | - calling this method on an existing, in-use device is safe
|
127 | f87548b5 | Iustin Pop | - if the device is already configured (and in an OK state),
|
128 | f87548b5 | Iustin Pop | this method is idempotent
|
129 | a8083063 | Iustin Pop |
|
130 | a8083063 | Iustin Pop | """
|
131 | 1063abd1 | Iustin Pop | pass
|
132 | a8083063 | Iustin Pop | |
133 | a8083063 | Iustin Pop | def Attach(self): |
134 | a8083063 | Iustin Pop | """Find a device which matches our config and attach to it.
|
135 | a8083063 | Iustin Pop |
|
136 | a8083063 | Iustin Pop | """
|
137 | a8083063 | Iustin Pop | raise NotImplementedError |
138 | a8083063 | Iustin Pop | |
139 | a8083063 | Iustin Pop | def Close(self): |
140 | a8083063 | Iustin Pop | """Notifies that the device will no longer be used for I/O.
|
141 | a8083063 | Iustin Pop |
|
142 | a8083063 | Iustin Pop | """
|
143 | a8083063 | Iustin Pop | raise NotImplementedError |
144 | a8083063 | Iustin Pop | |
145 | a8083063 | Iustin Pop | @classmethod
|
146 | a8083063 | Iustin Pop | def Create(cls, unique_id, children, size): |
147 | a8083063 | Iustin Pop | """Create the device.
|
148 | a8083063 | Iustin Pop |
|
149 | a8083063 | Iustin Pop | If the device cannot be created, it will return None
|
150 | a8083063 | Iustin Pop | instead. Error messages go to the logging system.
|
151 | a8083063 | Iustin Pop |
|
152 | a8083063 | Iustin Pop | Note that for some devices, the unique_id is used, and for other,
|
153 | a8083063 | Iustin Pop | the children. The idea is that these two, taken together, are
|
154 | a8083063 | Iustin Pop | enough for both creation and assembly (later).
|
155 | a8083063 | Iustin Pop |
|
156 | a8083063 | Iustin Pop | """
|
157 | a8083063 | Iustin Pop | raise NotImplementedError |
158 | a8083063 | Iustin Pop | |
159 | a8083063 | Iustin Pop | def Remove(self): |
160 | a8083063 | Iustin Pop | """Remove this device.
|
161 | a8083063 | Iustin Pop |
|
162 | abdf0113 | Iustin Pop | This makes sense only for some of the device types: LV and file
|
163 | abdf0113 | Iustin Pop | storeage. Also note that if the device can't attach, the removal
|
164 | abdf0113 | Iustin Pop | can't be completed.
|
165 | a8083063 | Iustin Pop |
|
166 | a8083063 | Iustin Pop | """
|
167 | a8083063 | Iustin Pop | raise NotImplementedError |
168 | a8083063 | Iustin Pop | |
169 | f3e513ad | Iustin Pop | def Rename(self, new_id): |
170 | f3e513ad | Iustin Pop | """Rename this device.
|
171 | f3e513ad | Iustin Pop |
|
172 | f3e513ad | Iustin Pop | This may or may not make sense for a given device type.
|
173 | f3e513ad | Iustin Pop |
|
174 | f3e513ad | Iustin Pop | """
|
175 | f3e513ad | Iustin Pop | raise NotImplementedError |
176 | f3e513ad | Iustin Pop | |
177 | a8083063 | Iustin Pop | def Open(self, force=False): |
178 | a8083063 | Iustin Pop | """Make the device ready for use.
|
179 | a8083063 | Iustin Pop |
|
180 | a8083063 | Iustin Pop | This makes the device ready for I/O. For now, just the DRBD
|
181 | a8083063 | Iustin Pop | devices need this.
|
182 | a8083063 | Iustin Pop |
|
183 | a8083063 | Iustin Pop | The force parameter signifies that if the device has any kind of
|
184 | a8083063 | Iustin Pop | --force thing, it should be used, we know what we are doing.
|
185 | a8083063 | Iustin Pop |
|
186 | a8083063 | Iustin Pop | """
|
187 | a8083063 | Iustin Pop | raise NotImplementedError |
188 | a8083063 | Iustin Pop | |
189 | a8083063 | Iustin Pop | def Shutdown(self): |
190 | a8083063 | Iustin Pop | """Shut down the device, freeing its children.
|
191 | a8083063 | Iustin Pop |
|
192 | a8083063 | Iustin Pop | This undoes the `Assemble()` work, except for the child
|
193 | a8083063 | Iustin Pop | assembling; as such, the children on the device are still
|
194 | a8083063 | Iustin Pop | assembled after this call.
|
195 | a8083063 | Iustin Pop |
|
196 | a8083063 | Iustin Pop | """
|
197 | a8083063 | Iustin Pop | raise NotImplementedError |
198 | a8083063 | Iustin Pop | |
199 | a8083063 | Iustin Pop | def SetSyncSpeed(self, speed): |
200 | a8083063 | Iustin Pop | """Adjust the sync speed of the mirror.
|
201 | a8083063 | Iustin Pop |
|
202 | a8083063 | Iustin Pop | In case this is not a mirroring device, this is no-op.
|
203 | a8083063 | Iustin Pop |
|
204 | a8083063 | Iustin Pop | """
|
205 | a8083063 | Iustin Pop | result = True
|
206 | a8083063 | Iustin Pop | if self._children: |
207 | a8083063 | Iustin Pop | for child in self._children: |
208 | a8083063 | Iustin Pop | result = result and child.SetSyncSpeed(speed)
|
209 | a8083063 | Iustin Pop | return result
|
210 | a8083063 | Iustin Pop | |
211 | a8083063 | Iustin Pop | def GetSyncStatus(self): |
212 | a8083063 | Iustin Pop | """Returns the sync status of the device.
|
213 | a8083063 | Iustin Pop |
|
214 | a8083063 | Iustin Pop | If this device is a mirroring device, this function returns the
|
215 | a8083063 | Iustin Pop | status of the mirror.
|
216 | a8083063 | Iustin Pop |
|
217 | 0834c866 | Iustin Pop | If sync_percent is None, it means the device is not syncing.
|
218 | a8083063 | Iustin Pop |
|
219 | a8083063 | Iustin Pop | If estimated_time is None, it means we can't estimate
|
220 | 0834c866 | Iustin Pop | the time needed, otherwise it's the time left in seconds.
|
221 | 0834c866 | Iustin Pop |
|
222 | a8083063 | Iustin Pop | If is_degraded is True, it means the device is missing
|
223 | a8083063 | Iustin Pop | redundancy. This is usually a sign that something went wrong in
|
224 | a8083063 | Iustin Pop | the device setup, if sync_percent is None.
|
225 | a8083063 | Iustin Pop |
|
226 | 0834c866 | Iustin Pop | The ldisk parameter represents the degradation of the local
|
227 | 0834c866 | Iustin Pop | data. This is only valid for some devices, the rest will always
|
228 | 0834c866 | Iustin Pop | return False (not degraded).
|
229 | 0834c866 | Iustin Pop |
|
230 | c41eea6e | Iustin Pop | @rtype: tuple
|
231 | c41eea6e | Iustin Pop | @return: (sync_percent, estimated_time, is_degraded, ldisk)
|
232 | c41eea6e | Iustin Pop |
|
233 | a8083063 | Iustin Pop | """
|
234 | 0834c866 | Iustin Pop | return None, None, False, False |
235 | a8083063 | Iustin Pop | |
236 | a8083063 | Iustin Pop | |
237 | a8083063 | Iustin Pop | def CombinedSyncStatus(self): |
238 | a8083063 | Iustin Pop | """Calculate the mirror status recursively for our children.
|
239 | a8083063 | Iustin Pop |
|
240 | a8083063 | Iustin Pop | The return value is the same as for `GetSyncStatus()` except the
|
241 | a8083063 | Iustin Pop | minimum percent and maximum time are calculated across our
|
242 | a8083063 | Iustin Pop | children.
|
243 | a8083063 | Iustin Pop |
|
244 | a8083063 | Iustin Pop | """
|
245 | 0834c866 | Iustin Pop | min_percent, max_time, is_degraded, ldisk = self.GetSyncStatus()
|
246 | a8083063 | Iustin Pop | if self._children: |
247 | a8083063 | Iustin Pop | for child in self._children: |
248 | 0834c866 | Iustin Pop | c_percent, c_time, c_degraded, c_ldisk = child.GetSyncStatus() |
249 | a8083063 | Iustin Pop | if min_percent is None: |
250 | a8083063 | Iustin Pop | min_percent = c_percent |
251 | a8083063 | Iustin Pop | elif c_percent is not None: |
252 | a8083063 | Iustin Pop | min_percent = min(min_percent, c_percent)
|
253 | a8083063 | Iustin Pop | if max_time is None: |
254 | a8083063 | Iustin Pop | max_time = c_time |
255 | a8083063 | Iustin Pop | elif c_time is not None: |
256 | a8083063 | Iustin Pop | max_time = max(max_time, c_time)
|
257 | a8083063 | Iustin Pop | is_degraded = is_degraded or c_degraded
|
258 | 0834c866 | Iustin Pop | ldisk = ldisk or c_ldisk
|
259 | 0834c866 | Iustin Pop | return min_percent, max_time, is_degraded, ldisk
|
260 | a8083063 | Iustin Pop | |
261 | a8083063 | Iustin Pop | |
262 | a0c3fea1 | Michael Hanselmann | def SetInfo(self, text): |
263 | a0c3fea1 | Michael Hanselmann | """Update metadata with info text.
|
264 | a0c3fea1 | Michael Hanselmann |
|
265 | a0c3fea1 | Michael Hanselmann | Only supported for some device types.
|
266 | a0c3fea1 | Michael Hanselmann |
|
267 | a0c3fea1 | Michael Hanselmann | """
|
268 | a0c3fea1 | Michael Hanselmann | for child in self._children: |
269 | a0c3fea1 | Michael Hanselmann | child.SetInfo(text) |
270 | a0c3fea1 | Michael Hanselmann | |
271 | 1005d816 | Iustin Pop | def Grow(self, amount): |
272 | 1005d816 | Iustin Pop | """Grow the block device.
|
273 | 1005d816 | Iustin Pop |
|
274 | c41eea6e | Iustin Pop | @param amount: the amount (in mebibytes) to grow with
|
275 | 1005d816 | Iustin Pop |
|
276 | 1005d816 | Iustin Pop | """
|
277 | 1005d816 | Iustin Pop | raise NotImplementedError |
278 | a0c3fea1 | Michael Hanselmann | |
279 | a8083063 | Iustin Pop | def __repr__(self): |
280 | a8083063 | Iustin Pop | return ("<%s: unique_id: %s, children: %s, %s:%s, %s>" % |
281 | a8083063 | Iustin Pop | (self.__class__, self.unique_id, self._children, |
282 | a8083063 | Iustin Pop | self.major, self.minor, self.dev_path)) |
283 | a8083063 | Iustin Pop | |
284 | a8083063 | Iustin Pop | |
285 | a8083063 | Iustin Pop | class LogicalVolume(BlockDev): |
286 | a8083063 | Iustin Pop | """Logical Volume block device.
|
287 | a8083063 | Iustin Pop |
|
288 | a8083063 | Iustin Pop | """
|
289 | a8083063 | Iustin Pop | def __init__(self, unique_id, children): |
290 | a8083063 | Iustin Pop | """Attaches to a LV device.
|
291 | a8083063 | Iustin Pop |
|
292 | a8083063 | Iustin Pop | The unique_id is a tuple (vg_name, lv_name)
|
293 | a8083063 | Iustin Pop |
|
294 | a8083063 | Iustin Pop | """
|
295 | a8083063 | Iustin Pop | super(LogicalVolume, self).__init__(unique_id, children) |
296 | a8083063 | Iustin Pop | if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2: |
297 | a8083063 | Iustin Pop | raise ValueError("Invalid configuration data %s" % str(unique_id)) |
298 | a8083063 | Iustin Pop | self._vg_name, self._lv_name = unique_id |
299 | a8083063 | Iustin Pop | self.dev_path = "/dev/%s/%s" % (self._vg_name, self._lv_name) |
300 | 99e8295c | Iustin Pop | self._degraded = True |
301 | 99e8295c | Iustin Pop | self.major = self.minor = None |
302 | a8083063 | Iustin Pop | self.Attach()
|
303 | a8083063 | Iustin Pop | |
304 | a8083063 | Iustin Pop | @classmethod
|
305 | a8083063 | Iustin Pop | def Create(cls, unique_id, children, size): |
306 | a8083063 | Iustin Pop | """Create a new logical volume.
|
307 | a8083063 | Iustin Pop |
|
308 | a8083063 | Iustin Pop | """
|
309 | a8083063 | Iustin Pop | if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2: |
310 | 6c626518 | Iustin Pop | raise errors.ProgrammerError("Invalid configuration data %s" % |
311 | 6c626518 | Iustin Pop | str(unique_id))
|
312 | a8083063 | Iustin Pop | vg_name, lv_name = unique_id |
313 | a8083063 | Iustin Pop | pvs_info = cls.GetPVInfo(vg_name) |
314 | a8083063 | Iustin Pop | if not pvs_info: |
315 | 82463074 | Iustin Pop | _ThrowError("Can't compute PV info for vg %s", vg_name)
|
316 | a8083063 | Iustin Pop | pvs_info.sort() |
317 | a8083063 | Iustin Pop | pvs_info.reverse() |
318 | 5b7b5d49 | Guido Trotter | |
319 | 5b7b5d49 | Guido Trotter | pvlist = [ pv[1] for pv in pvs_info ] |
320 | 5b7b5d49 | Guido Trotter | free_size = sum([ pv[0] for pv in pvs_info ]) |
321 | 5b7b5d49 | Guido Trotter | |
322 | 5b7b5d49 | Guido Trotter | # The size constraint should have been checked from the master before
|
323 | 5b7b5d49 | Guido Trotter | # calling the create function.
|
324 | a8083063 | Iustin Pop | if free_size < size:
|
325 | 82463074 | Iustin Pop | _ThrowError("Not enough free space: required %s,"
|
326 | 82463074 | Iustin Pop | " available %s", size, free_size)
|
327 | a8083063 | Iustin Pop | result = utils.RunCmd(["lvcreate", "-L%dm" % size, "-n%s" % lv_name, |
328 | 5b7b5d49 | Guido Trotter | vg_name] + pvlist) |
329 | a8083063 | Iustin Pop | if result.failed:
|
330 | 82463074 | Iustin Pop | _ThrowError("LV create failed (%s): %s",
|
331 | 82463074 | Iustin Pop | result.fail_reason, result.output) |
332 | a8083063 | Iustin Pop | return LogicalVolume(unique_id, children)
|
333 | a8083063 | Iustin Pop | |
334 | a8083063 | Iustin Pop | @staticmethod
|
335 | a8083063 | Iustin Pop | def GetPVInfo(vg_name): |
336 | a8083063 | Iustin Pop | """Get the free space info for PVs in a volume group.
|
337 | a8083063 | Iustin Pop |
|
338 | c41eea6e | Iustin Pop | @param vg_name: the volume group name
|
339 | a8083063 | Iustin Pop |
|
340 | c41eea6e | Iustin Pop | @rtype: list
|
341 | c41eea6e | Iustin Pop | @return: list of tuples (free_space, name) with free_space in mebibytes
|
342 | 098c0958 | Michael Hanselmann |
|
343 | a8083063 | Iustin Pop | """
|
344 | a8083063 | Iustin Pop | command = ["pvs", "--noheadings", "--nosuffix", "--units=m", |
345 | a8083063 | Iustin Pop | "-opv_name,vg_name,pv_free,pv_attr", "--unbuffered", |
346 | a8083063 | Iustin Pop | "--separator=:"]
|
347 | a8083063 | Iustin Pop | result = utils.RunCmd(command) |
348 | a8083063 | Iustin Pop | if result.failed:
|
349 | 468c5f77 | Iustin Pop | logging.error("Can't get the PV information: %s - %s",
|
350 | 468c5f77 | Iustin Pop | result.fail_reason, result.output) |
351 | a8083063 | Iustin Pop | return None |
352 | a8083063 | Iustin Pop | data = [] |
353 | a8083063 | Iustin Pop | for line in result.stdout.splitlines(): |
354 | a8083063 | Iustin Pop | fields = line.strip().split(':')
|
355 | a8083063 | Iustin Pop | if len(fields) != 4: |
356 | 468c5f77 | Iustin Pop | logging.error("Can't parse pvs output: line '%s'", line)
|
357 | a8083063 | Iustin Pop | return None |
358 | a8083063 | Iustin Pop | # skip over pvs from another vg or ones which are not allocatable
|
359 | a8083063 | Iustin Pop | if fields[1] != vg_name or fields[3][0] != 'a': |
360 | a8083063 | Iustin Pop | continue
|
361 | a8083063 | Iustin Pop | data.append((float(fields[2]), fields[0])) |
362 | a8083063 | Iustin Pop | |
363 | a8083063 | Iustin Pop | return data
|
364 | a8083063 | Iustin Pop | |
365 | a8083063 | Iustin Pop | def Remove(self): |
366 | a8083063 | Iustin Pop | """Remove this logical volume.
|
367 | a8083063 | Iustin Pop |
|
368 | a8083063 | Iustin Pop | """
|
369 | a8083063 | Iustin Pop | if not self.minor and not self.Attach(): |
370 | a8083063 | Iustin Pop | # the LV does not exist
|
371 | 0c6c04ec | Iustin Pop | return
|
372 | a8083063 | Iustin Pop | result = utils.RunCmd(["lvremove", "-f", "%s/%s" % |
373 | a8083063 | Iustin Pop | (self._vg_name, self._lv_name)]) |
374 | a8083063 | Iustin Pop | if result.failed:
|
375 | 0c6c04ec | Iustin Pop | _ThrowError("Can't lvremove: %s - %s", result.fail_reason, result.output)
|
376 | a8083063 | Iustin Pop | |
377 | f3e513ad | Iustin Pop | def Rename(self, new_id): |
378 | f3e513ad | Iustin Pop | """Rename this logical volume.
|
379 | f3e513ad | Iustin Pop |
|
380 | f3e513ad | Iustin Pop | """
|
381 | f3e513ad | Iustin Pop | if not isinstance(new_id, (tuple, list)) or len(new_id) != 2: |
382 | f3e513ad | Iustin Pop | raise errors.ProgrammerError("Invalid new logical id '%s'" % new_id) |
383 | f3e513ad | Iustin Pop | new_vg, new_name = new_id |
384 | f3e513ad | Iustin Pop | if new_vg != self._vg_name: |
385 | f3e513ad | Iustin Pop | raise errors.ProgrammerError("Can't move a logical volume across" |
386 | f3e513ad | Iustin Pop | " volume groups (from %s to to %s)" %
|
387 | f3e513ad | Iustin Pop | (self._vg_name, new_vg))
|
388 | f3e513ad | Iustin Pop | result = utils.RunCmd(["lvrename", new_vg, self._lv_name, new_name]) |
389 | f3e513ad | Iustin Pop | if result.failed:
|
390 | 82463074 | Iustin Pop | _ThrowError("Failed to rename the logical volume: %s", result.output)
|
391 | be345db0 | Iustin Pop | self._lv_name = new_name
|
392 | be345db0 | Iustin Pop | self.dev_path = "/dev/%s/%s" % (self._vg_name, self._lv_name) |
393 | be345db0 | Iustin Pop | |
394 | a8083063 | Iustin Pop | def Attach(self): |
395 | a8083063 | Iustin Pop | """Attach to an existing LV.
|
396 | a8083063 | Iustin Pop |
|
397 | a8083063 | Iustin Pop | This method will try to see if an existing and active LV exists
|
398 | c99a3cc0 | Manuel Franceschini | which matches our name. If so, its major/minor will be
|
399 | a8083063 | Iustin Pop | recorded.
|
400 | a8083063 | Iustin Pop |
|
401 | a8083063 | Iustin Pop | """
|
402 | cb999543 | Iustin Pop | self.attached = False |
403 | 99e8295c | Iustin Pop | result = utils.RunCmd(["lvs", "--noheadings", "--separator=,", |
404 | 99e8295c | Iustin Pop | "-olv_attr,lv_kernel_major,lv_kernel_minor",
|
405 | 99e8295c | Iustin Pop | self.dev_path])
|
406 | a8083063 | Iustin Pop | if result.failed:
|
407 | 468c5f77 | Iustin Pop | logging.error("Can't find LV %s: %s, %s",
|
408 | 468c5f77 | Iustin Pop | self.dev_path, result.fail_reason, result.output)
|
409 | a8083063 | Iustin Pop | return False |
410 | 99e8295c | Iustin Pop | out = result.stdout.strip().rstrip(',')
|
411 | 99e8295c | Iustin Pop | out = out.split(",")
|
412 | 99e8295c | Iustin Pop | if len(out) != 3: |
413 | 468c5f77 | Iustin Pop | logging.error("Can't parse LVS output, len(%s) != 3", str(out)) |
414 | 99e8295c | Iustin Pop | return False |
415 | 99e8295c | Iustin Pop | |
416 | 99e8295c | Iustin Pop | status, major, minor = out[:3]
|
417 | 99e8295c | Iustin Pop | if len(status) != 6: |
418 | 468c5f77 | Iustin Pop | logging.error("lvs lv_attr is not 6 characters (%s)", status)
|
419 | 99e8295c | Iustin Pop | return False |
420 | 99e8295c | Iustin Pop | |
421 | 99e8295c | Iustin Pop | try:
|
422 | 99e8295c | Iustin Pop | major = int(major)
|
423 | 99e8295c | Iustin Pop | minor = int(minor)
|
424 | 99e8295c | Iustin Pop | except ValueError, err: |
425 | 468c5f77 | Iustin Pop | logging.error("lvs major/minor cannot be parsed: %s", str(err)) |
426 | 99e8295c | Iustin Pop | |
427 | 99e8295c | Iustin Pop | self.major = major
|
428 | 99e8295c | Iustin Pop | self.minor = minor
|
429 | 99e8295c | Iustin Pop | self._degraded = status[0] == 'v' # virtual volume, i.e. doesn't backing |
430 | 99e8295c | Iustin Pop | # storage
|
431 | cb999543 | Iustin Pop | self.attached = True |
432 | 99e8295c | Iustin Pop | return True |
433 | a8083063 | Iustin Pop | |
434 | a8083063 | Iustin Pop | def Assemble(self): |
435 | a8083063 | Iustin Pop | """Assemble the device.
|
436 | a8083063 | Iustin Pop |
|
437 | 5574047a | Iustin Pop | We alway run `lvchange -ay` on the LV to ensure it's active before
|
438 | 5574047a | Iustin Pop | use, as there were cases when xenvg was not active after boot
|
439 | 5574047a | Iustin Pop | (also possibly after disk issues).
|
440 | a8083063 | Iustin Pop |
|
441 | a8083063 | Iustin Pop | """
|
442 | 5574047a | Iustin Pop | result = utils.RunCmd(["lvchange", "-ay", self.dev_path]) |
443 | 5574047a | Iustin Pop | if result.failed:
|
444 | 1063abd1 | Iustin Pop | _ThrowError("Can't activate lv %s: %s", self.dev_path, result.output) |
445 | a8083063 | Iustin Pop | |
446 | a8083063 | Iustin Pop | def Shutdown(self): |
447 | a8083063 | Iustin Pop | """Shutdown the device.
|
448 | a8083063 | Iustin Pop |
|
449 | a8083063 | Iustin Pop | This is a no-op for the LV device type, as we don't deactivate the
|
450 | a8083063 | Iustin Pop | volumes on shutdown.
|
451 | a8083063 | Iustin Pop |
|
452 | a8083063 | Iustin Pop | """
|
453 | 746f7476 | Iustin Pop | pass
|
454 | a8083063 | Iustin Pop | |
455 | 9db6dbce | Iustin Pop | def GetSyncStatus(self): |
456 | 9db6dbce | Iustin Pop | """Returns the sync status of the device.
|
457 | 9db6dbce | Iustin Pop |
|
458 | 9db6dbce | Iustin Pop | If this device is a mirroring device, this function returns the
|
459 | 9db6dbce | Iustin Pop | status of the mirror.
|
460 | 9db6dbce | Iustin Pop |
|
461 | 9db6dbce | Iustin Pop | For logical volumes, sync_percent and estimated_time are always
|
462 | 9db6dbce | Iustin Pop | None (no recovery in progress, as we don't handle the mirrored LV
|
463 | 0834c866 | Iustin Pop | case). The is_degraded parameter is the inverse of the ldisk
|
464 | 0834c866 | Iustin Pop | parameter.
|
465 | 9db6dbce | Iustin Pop |
|
466 | 0834c866 | Iustin Pop | For the ldisk parameter, we check if the logical volume has the
|
467 | 0834c866 | Iustin Pop | 'virtual' type, which means it's not backed by existing storage
|
468 | 0834c866 | Iustin Pop | anymore (read from it return I/O error). This happens after a
|
469 | 0834c866 | Iustin Pop | physical disk failure and subsequent 'vgreduce --removemissing' on
|
470 | 0834c866 | Iustin Pop | the volume group.
|
471 | 9db6dbce | Iustin Pop |
|
472 | 99e8295c | Iustin Pop | The status was already read in Attach, so we just return it.
|
473 | 99e8295c | Iustin Pop |
|
474 | c41eea6e | Iustin Pop | @rtype: tuple
|
475 | c41eea6e | Iustin Pop | @return: (sync_percent, estimated_time, is_degraded, ldisk)
|
476 | c41eea6e | Iustin Pop |
|
477 | 9db6dbce | Iustin Pop | """
|
478 | 99e8295c | Iustin Pop | return None, None, self._degraded, self._degraded |
479 | 9db6dbce | Iustin Pop | |
480 | a8083063 | Iustin Pop | def Open(self, force=False): |
481 | a8083063 | Iustin Pop | """Make the device ready for I/O.
|
482 | a8083063 | Iustin Pop |
|
483 | a8083063 | Iustin Pop | This is a no-op for the LV device type.
|
484 | a8083063 | Iustin Pop |
|
485 | a8083063 | Iustin Pop | """
|
486 | fdbd668d | Iustin Pop | pass
|
487 | a8083063 | Iustin Pop | |
488 | a8083063 | Iustin Pop | def Close(self): |
489 | a8083063 | Iustin Pop | """Notifies that the device will no longer be used for I/O.
|
490 | a8083063 | Iustin Pop |
|
491 | a8083063 | Iustin Pop | This is a no-op for the LV device type.
|
492 | a8083063 | Iustin Pop |
|
493 | a8083063 | Iustin Pop | """
|
494 | fdbd668d | Iustin Pop | pass
|
495 | a8083063 | Iustin Pop | |
496 | a8083063 | Iustin Pop | def Snapshot(self, size): |
497 | a8083063 | Iustin Pop | """Create a snapshot copy of an lvm block device.
|
498 | a8083063 | Iustin Pop |
|
499 | a8083063 | Iustin Pop | """
|
500 | a8083063 | Iustin Pop | snap_name = self._lv_name + ".snap" |
501 | a8083063 | Iustin Pop | |
502 | a8083063 | Iustin Pop | # remove existing snapshot if found
|
503 | a8083063 | Iustin Pop | snap = LogicalVolume((self._vg_name, snap_name), None) |
504 | 0c6c04ec | Iustin Pop | _IgnoreError(snap.Remove) |
505 | a8083063 | Iustin Pop | |
506 | a8083063 | Iustin Pop | pvs_info = self.GetPVInfo(self._vg_name) |
507 | a8083063 | Iustin Pop | if not pvs_info: |
508 | 82463074 | Iustin Pop | _ThrowError("Can't compute PV info for vg %s", self._vg_name) |
509 | a8083063 | Iustin Pop | pvs_info.sort() |
510 | a8083063 | Iustin Pop | pvs_info.reverse() |
511 | a8083063 | Iustin Pop | free_size, pv_name = pvs_info[0]
|
512 | a8083063 | Iustin Pop | if free_size < size:
|
513 | 82463074 | Iustin Pop | _ThrowError("Not enough free space: required %s,"
|
514 | 82463074 | Iustin Pop | " available %s", size, free_size)
|
515 | a8083063 | Iustin Pop | |
516 | a8083063 | Iustin Pop | result = utils.RunCmd(["lvcreate", "-L%dm" % size, "-s", |
517 | a8083063 | Iustin Pop | "-n%s" % snap_name, self.dev_path]) |
518 | a8083063 | Iustin Pop | if result.failed:
|
519 | 82463074 | Iustin Pop | _ThrowError("command: %s error: %s - %s",
|
520 | 82463074 | Iustin Pop | result.cmd, result.fail_reason, result.output) |
521 | a8083063 | Iustin Pop | |
522 | a8083063 | Iustin Pop | return snap_name
|
523 | a8083063 | Iustin Pop | |
524 | a0c3fea1 | Michael Hanselmann | def SetInfo(self, text): |
525 | a0c3fea1 | Michael Hanselmann | """Update metadata with info text.
|
526 | a0c3fea1 | Michael Hanselmann |
|
527 | a0c3fea1 | Michael Hanselmann | """
|
528 | a0c3fea1 | Michael Hanselmann | BlockDev.SetInfo(self, text)
|
529 | a0c3fea1 | Michael Hanselmann | |
530 | a0c3fea1 | Michael Hanselmann | # Replace invalid characters
|
531 | a0c3fea1 | Michael Hanselmann | text = re.sub('^[^A-Za-z0-9_+.]', '_', text) |
532 | a0c3fea1 | Michael Hanselmann | text = re.sub('[^-A-Za-z0-9_+.]', '_', text) |
533 | a0c3fea1 | Michael Hanselmann | |
534 | a0c3fea1 | Michael Hanselmann | # Only up to 128 characters are allowed
|
535 | a0c3fea1 | Michael Hanselmann | text = text[:128]
|
536 | a0c3fea1 | Michael Hanselmann | |
537 | a0c3fea1 | Michael Hanselmann | result = utils.RunCmd(["lvchange", "--addtag", text, |
538 | a0c3fea1 | Michael Hanselmann | self.dev_path])
|
539 | a0c3fea1 | Michael Hanselmann | if result.failed:
|
540 | 82463074 | Iustin Pop | _ThrowError("Command: %s error: %s - %s", result.cmd, result.fail_reason,
|
541 | 82463074 | Iustin Pop | result.output) |
542 | 82463074 | Iustin Pop | |
543 | 1005d816 | Iustin Pop | def Grow(self, amount): |
544 | 1005d816 | Iustin Pop | """Grow the logical volume.
|
545 | 1005d816 | Iustin Pop |
|
546 | 1005d816 | Iustin Pop | """
|
547 | 1005d816 | Iustin Pop | # we try multiple algorithms since the 'best' ones might not have
|
548 | 1005d816 | Iustin Pop | # space available in the right place, but later ones might (since
|
549 | 1005d816 | Iustin Pop | # they have less constraints); also note that only recent LVM
|
550 | 1005d816 | Iustin Pop | # supports 'cling'
|
551 | 1005d816 | Iustin Pop | for alloc_policy in "contiguous", "cling", "normal": |
552 | 1005d816 | Iustin Pop | result = utils.RunCmd(["lvextend", "--alloc", alloc_policy, |
553 | 1005d816 | Iustin Pop | "-L", "+%dm" % amount, self.dev_path]) |
554 | 1005d816 | Iustin Pop | if not result.failed: |
555 | 1005d816 | Iustin Pop | return
|
556 | 82463074 | Iustin Pop | _ThrowError("Can't grow LV %s: %s", self.dev_path, result.output) |
557 | a0c3fea1 | Michael Hanselmann | |
558 | a0c3fea1 | Michael Hanselmann | |
559 | 6b90c22e | Iustin Pop | class DRBD8Status(object): |
560 | 6b90c22e | Iustin Pop | """A DRBD status representation class.
|
561 | 6b90c22e | Iustin Pop |
|
562 | 6b90c22e | Iustin Pop | Note that this doesn't support unconfigured devices (cs:Unconfigured).
|
563 | 6b90c22e | Iustin Pop |
|
564 | 6b90c22e | Iustin Pop | """
|
565 | 767d52d3 | Iustin Pop | UNCONF_RE = re.compile(r"\s*[0-9]+:\s*cs:Unconfigured$")
|
566 | 6b90c22e | Iustin Pop | LINE_RE = re.compile(r"\s*[0-9]+:\s*cs:(\S+)\s+st:([^/]+)/(\S+)"
|
567 | 6b90c22e | Iustin Pop | "\s+ds:([^/]+)/(\S+)\s+.*$")
|
568 | 6b90c22e | Iustin Pop | SYNC_RE = re.compile(r"^.*\ssync'ed:\s*([0-9.]+)%.*"
|
569 | 6b90c22e | Iustin Pop | "\sfinish: ([0-9]+):([0-9]+):([0-9]+)\s.*$")
|
570 | 6b90c22e | Iustin Pop | |
571 | 6b90c22e | Iustin Pop | def __init__(self, procline): |
572 | 767d52d3 | Iustin Pop | u = self.UNCONF_RE.match(procline)
|
573 | 767d52d3 | Iustin Pop | if u:
|
574 | 767d52d3 | Iustin Pop | self.cstatus = "Unconfigured" |
575 | 767d52d3 | Iustin Pop | self.lrole = self.rrole = self.ldisk = self.rdisk = None |
576 | 767d52d3 | Iustin Pop | else:
|
577 | 767d52d3 | Iustin Pop | m = self.LINE_RE.match(procline)
|
578 | 767d52d3 | Iustin Pop | if not m: |
579 | 767d52d3 | Iustin Pop | raise errors.BlockDeviceError("Can't parse input data '%s'" % procline) |
580 | 767d52d3 | Iustin Pop | self.cstatus = m.group(1) |
581 | 767d52d3 | Iustin Pop | self.lrole = m.group(2) |
582 | 767d52d3 | Iustin Pop | self.rrole = m.group(3) |
583 | 767d52d3 | Iustin Pop | self.ldisk = m.group(4) |
584 | 767d52d3 | Iustin Pop | self.rdisk = m.group(5) |
585 | 767d52d3 | Iustin Pop | |
586 | 767d52d3 | Iustin Pop | # end reading of data from the LINE_RE or UNCONF_RE
|
587 | 6b90c22e | Iustin Pop | |
588 | 6b90c22e | Iustin Pop | self.is_standalone = self.cstatus == "StandAlone" |
589 | 6b90c22e | Iustin Pop | self.is_wfconn = self.cstatus == "WFConnection" |
590 | 6b90c22e | Iustin Pop | self.is_connected = self.cstatus == "Connected" |
591 | 6b90c22e | Iustin Pop | self.is_primary = self.lrole == "Primary" |
592 | 6b90c22e | Iustin Pop | self.is_secondary = self.lrole == "Secondary" |
593 | 6b90c22e | Iustin Pop | self.peer_primary = self.rrole == "Primary" |
594 | 6b90c22e | Iustin Pop | self.peer_secondary = self.rrole == "Secondary" |
595 | 6b90c22e | Iustin Pop | self.both_primary = self.is_primary and self.peer_primary |
596 | 6b90c22e | Iustin Pop | self.both_secondary = self.is_secondary and self.peer_secondary |
597 | 6b90c22e | Iustin Pop | |
598 | 6b90c22e | Iustin Pop | self.is_diskless = self.ldisk == "Diskless" |
599 | 6b90c22e | Iustin Pop | self.is_disk_uptodate = self.ldisk == "UpToDate" |
600 | 6b90c22e | Iustin Pop | |
601 | 767d52d3 | Iustin Pop | self.is_in_resync = self.cstatus in ("SyncSource", "SyncTarget") |
602 | 767d52d3 | Iustin Pop | self.is_in_use = self.cstatus != "Unconfigured" |
603 | 6b93ec9d | Iustin Pop | |
604 | 6b90c22e | Iustin Pop | m = self.SYNC_RE.match(procline)
|
605 | 6b90c22e | Iustin Pop | if m:
|
606 | 6b90c22e | Iustin Pop | self.sync_percent = float(m.group(1)) |
607 | 6b90c22e | Iustin Pop | hours = int(m.group(2)) |
608 | 6b90c22e | Iustin Pop | minutes = int(m.group(3)) |
609 | 6b90c22e | Iustin Pop | seconds = int(m.group(4)) |
610 | 6b90c22e | Iustin Pop | self.est_time = hours * 3600 + minutes * 60 + seconds |
611 | 6b90c22e | Iustin Pop | else:
|
612 | 6b90c22e | Iustin Pop | self.sync_percent = None |
613 | 6b90c22e | Iustin Pop | self.est_time = None |
614 | 6b90c22e | Iustin Pop | |
615 | 6b90c22e | Iustin Pop | self.is_sync_target = self.peer_sync_source = self.cstatus == "SyncTarget" |
616 | 6b90c22e | Iustin Pop | self.peer_sync_target = self.is_sync_source = self.cstatus == "SyncSource" |
617 | 6b90c22e | Iustin Pop | self.is_resync = self.is_sync_target or self.is_sync_source |
618 | 6b90c22e | Iustin Pop | |
619 | 6b90c22e | Iustin Pop | |
620 | 0f7f32d9 | Iustin Pop | class BaseDRBD(BlockDev): |
621 | 0f7f32d9 | Iustin Pop | """Base DRBD class.
|
622 | a8083063 | Iustin Pop |
|
623 | 0f7f32d9 | Iustin Pop | This class contains a few bits of common functionality between the
|
624 | 0f7f32d9 | Iustin Pop | 0.7 and 8.x versions of DRBD.
|
625 | 0f7f32d9 | Iustin Pop |
|
626 | abdf0113 | Iustin Pop | """
|
627 | abdf0113 | Iustin Pop | _VERSION_RE = re.compile(r"^version: (\d+)\.(\d+)\.(\d+)"
|
628 | abdf0113 | Iustin Pop | r" \(api:(\d+)/proto:(\d+)(?:-(\d+))?\)")
|
629 | a8083063 | Iustin Pop | |
630 | abdf0113 | Iustin Pop | _DRBD_MAJOR = 147
|
631 | abdf0113 | Iustin Pop | _ST_UNCONFIGURED = "Unconfigured"
|
632 | abdf0113 | Iustin Pop | _ST_WFCONNECTION = "WFConnection"
|
633 | abdf0113 | Iustin Pop | _ST_CONNECTED = "Connected"
|
634 | a8083063 | Iustin Pop | |
635 | 6b90c22e | Iustin Pop | _STATUS_FILE = "/proc/drbd"
|
636 | 6b90c22e | Iustin Pop | |
637 | abdf0113 | Iustin Pop | @staticmethod
|
638 | 6b90c22e | Iustin Pop | def _GetProcData(filename=_STATUS_FILE): |
639 | abdf0113 | Iustin Pop | """Return data from /proc/drbd.
|
640 | a8083063 | Iustin Pop |
|
641 | a8083063 | Iustin Pop | """
|
642 | abdf0113 | Iustin Pop | try:
|
643 | f6eaed12 | Iustin Pop | stat = open(filename, "r") |
644 | f6eaed12 | Iustin Pop | try:
|
645 | f6eaed12 | Iustin Pop | data = stat.read().splitlines() |
646 | f6eaed12 | Iustin Pop | finally:
|
647 | f6eaed12 | Iustin Pop | stat.close() |
648 | f6eaed12 | Iustin Pop | except EnvironmentError, err: |
649 | f6eaed12 | Iustin Pop | if err.errno == errno.ENOENT:
|
650 | f6eaed12 | Iustin Pop | _ThrowError("The file %s cannot be opened, check if the module"
|
651 | f6eaed12 | Iustin Pop | " is loaded (%s)", filename, str(err)) |
652 | f6eaed12 | Iustin Pop | else:
|
653 | f6eaed12 | Iustin Pop | _ThrowError("Can't read the DRBD proc file %s: %s", filename, str(err)) |
654 | abdf0113 | Iustin Pop | if not data: |
655 | 82463074 | Iustin Pop | _ThrowError("Can't read any data from %s", filename)
|
656 | abdf0113 | Iustin Pop | return data
|
657 | a8083063 | Iustin Pop | |
658 | abdf0113 | Iustin Pop | @staticmethod
|
659 | abdf0113 | Iustin Pop | def _MassageProcData(data): |
660 | abdf0113 | Iustin Pop | """Transform the output of _GetProdData into a nicer form.
|
661 | a8083063 | Iustin Pop |
|
662 | c41eea6e | Iustin Pop | @return: a dictionary of minor: joined lines from /proc/drbd
|
663 | c41eea6e | Iustin Pop | for that minor
|
664 | a8083063 | Iustin Pop |
|
665 | a8083063 | Iustin Pop | """
|
666 | abdf0113 | Iustin Pop | lmatch = re.compile("^ *([0-9]+):.*$")
|
667 | abdf0113 | Iustin Pop | results = {} |
668 | abdf0113 | Iustin Pop | old_minor = old_line = None
|
669 | abdf0113 | Iustin Pop | for line in data: |
670 | abdf0113 | Iustin Pop | lresult = lmatch.match(line) |
671 | abdf0113 | Iustin Pop | if lresult is not None: |
672 | abdf0113 | Iustin Pop | if old_minor is not None: |
673 | abdf0113 | Iustin Pop | results[old_minor] = old_line |
674 | abdf0113 | Iustin Pop | old_minor = int(lresult.group(1)) |
675 | abdf0113 | Iustin Pop | old_line = line |
676 | abdf0113 | Iustin Pop | else:
|
677 | abdf0113 | Iustin Pop | if old_minor is not None: |
678 | abdf0113 | Iustin Pop | old_line += " " + line.strip()
|
679 | abdf0113 | Iustin Pop | # add last line
|
680 | abdf0113 | Iustin Pop | if old_minor is not None: |
681 | abdf0113 | Iustin Pop | results[old_minor] = old_line |
682 | abdf0113 | Iustin Pop | return results
|
683 | a8083063 | Iustin Pop | |
684 | abdf0113 | Iustin Pop | @classmethod
|
685 | abdf0113 | Iustin Pop | def _GetVersion(cls): |
686 | abdf0113 | Iustin Pop | """Return the DRBD version.
|
687 | a8083063 | Iustin Pop |
|
688 | abdf0113 | Iustin Pop | This will return a dict with keys:
|
689 | c41eea6e | Iustin Pop | - k_major
|
690 | c41eea6e | Iustin Pop | - k_minor
|
691 | c41eea6e | Iustin Pop | - k_point
|
692 | c41eea6e | Iustin Pop | - api
|
693 | c41eea6e | Iustin Pop | - proto
|
694 | c41eea6e | Iustin Pop | - proto2 (only on drbd > 8.2.X)
|
695 | a8083063 | Iustin Pop |
|
696 | a8083063 | Iustin Pop | """
|
697 | abdf0113 | Iustin Pop | proc_data = cls._GetProcData() |
698 | abdf0113 | Iustin Pop | first_line = proc_data[0].strip()
|
699 | abdf0113 | Iustin Pop | version = cls._VERSION_RE.match(first_line) |
700 | abdf0113 | Iustin Pop | if not version: |
701 | abdf0113 | Iustin Pop | raise errors.BlockDeviceError("Can't parse DRBD version from '%s'" % |
702 | abdf0113 | Iustin Pop | first_line) |
703 | a8083063 | Iustin Pop | |
704 | abdf0113 | Iustin Pop | values = version.groups() |
705 | abdf0113 | Iustin Pop | retval = {'k_major': int(values[0]), |
706 | abdf0113 | Iustin Pop | 'k_minor': int(values[1]), |
707 | abdf0113 | Iustin Pop | 'k_point': int(values[2]), |
708 | abdf0113 | Iustin Pop | 'api': int(values[3]), |
709 | abdf0113 | Iustin Pop | 'proto': int(values[4]), |
710 | abdf0113 | Iustin Pop | } |
711 | abdf0113 | Iustin Pop | if values[5] is not None: |
712 | abdf0113 | Iustin Pop | retval['proto2'] = values[5] |
713 | a8083063 | Iustin Pop | |
714 | abdf0113 | Iustin Pop | return retval
|
715 | abdf0113 | Iustin Pop | |
716 | abdf0113 | Iustin Pop | @staticmethod
|
717 | abdf0113 | Iustin Pop | def _DevPath(minor): |
718 | abdf0113 | Iustin Pop | """Return the path to a drbd device for a given minor.
|
719 | a8083063 | Iustin Pop |
|
720 | a8083063 | Iustin Pop | """
|
721 | abdf0113 | Iustin Pop | return "/dev/drbd%d" % minor |
722 | a8083063 | Iustin Pop | |
723 | abdf0113 | Iustin Pop | @classmethod
|
724 | 6d2e83d5 | Iustin Pop | def GetUsedDevs(cls): |
725 | abdf0113 | Iustin Pop | """Compute the list of used DRBD devices.
|
726 | a8083063 | Iustin Pop |
|
727 | a8083063 | Iustin Pop | """
|
728 | abdf0113 | Iustin Pop | data = cls._GetProcData() |
729 | a8083063 | Iustin Pop | |
730 | abdf0113 | Iustin Pop | used_devs = {} |
731 | abdf0113 | Iustin Pop | valid_line = re.compile("^ *([0-9]+): cs:([^ ]+).*$")
|
732 | abdf0113 | Iustin Pop | for line in data: |
733 | abdf0113 | Iustin Pop | match = valid_line.match(line) |
734 | abdf0113 | Iustin Pop | if not match: |
735 | abdf0113 | Iustin Pop | continue
|
736 | abdf0113 | Iustin Pop | minor = int(match.group(1)) |
737 | abdf0113 | Iustin Pop | state = match.group(2)
|
738 | abdf0113 | Iustin Pop | if state == cls._ST_UNCONFIGURED:
|
739 | abdf0113 | Iustin Pop | continue
|
740 | abdf0113 | Iustin Pop | used_devs[minor] = state, line |
741 | a8083063 | Iustin Pop | |
742 | abdf0113 | Iustin Pop | return used_devs
|
743 | a8083063 | Iustin Pop | |
744 | abdf0113 | Iustin Pop | def _SetFromMinor(self, minor): |
745 | abdf0113 | Iustin Pop | """Set our parameters based on the given minor.
|
746 | 0834c866 | Iustin Pop |
|
747 | abdf0113 | Iustin Pop | This sets our minor variable and our dev_path.
|
748 | a8083063 | Iustin Pop |
|
749 | a8083063 | Iustin Pop | """
|
750 | abdf0113 | Iustin Pop | if minor is None: |
751 | abdf0113 | Iustin Pop | self.minor = self.dev_path = None |
752 | cb999543 | Iustin Pop | self.attached = False |
753 | a8083063 | Iustin Pop | else:
|
754 | abdf0113 | Iustin Pop | self.minor = minor
|
755 | abdf0113 | Iustin Pop | self.dev_path = self._DevPath(minor) |
756 | cb999543 | Iustin Pop | self.attached = True |
757 | a8083063 | Iustin Pop | |
758 | a8083063 | Iustin Pop | @staticmethod
|
759 | abdf0113 | Iustin Pop | def _CheckMetaSize(meta_device): |
760 | abdf0113 | Iustin Pop | """Check if the given meta device looks like a valid one.
|
761 | a8083063 | Iustin Pop |
|
762 | abdf0113 | Iustin Pop | This currently only check the size, which must be around
|
763 | abdf0113 | Iustin Pop | 128MiB.
|
764 | a8083063 | Iustin Pop |
|
765 | a8083063 | Iustin Pop | """
|
766 | abdf0113 | Iustin Pop | result = utils.RunCmd(["blockdev", "--getsize", meta_device]) |
767 | abdf0113 | Iustin Pop | if result.failed:
|
768 | 9c793cfb | Iustin Pop | _ThrowError("Failed to get device size: %s - %s",
|
769 | 9c793cfb | Iustin Pop | result.fail_reason, result.output) |
770 | a8083063 | Iustin Pop | try:
|
771 | abdf0113 | Iustin Pop | sectors = int(result.stdout)
|
772 | abdf0113 | Iustin Pop | except ValueError: |
773 | 9c793cfb | Iustin Pop | _ThrowError("Invalid output from blockdev: '%s'", result.stdout)
|
774 | abdf0113 | Iustin Pop | bytes = sectors * 512
|
775 | abdf0113 | Iustin Pop | if bytes < 128 * 1024 * 1024: # less than 128MiB |
776 | 9c793cfb | Iustin Pop | _ThrowError("Meta device too small (%.2fMib)", (bytes / 1024 / 1024)) |
777 | abdf0113 | Iustin Pop | if bytes > (128 + 32) * 1024 * 1024: # account for an extra (big) PE on LVM |
778 | 9c793cfb | Iustin Pop | _ThrowError("Meta device too big (%.2fMiB)", (bytes / 1024 / 1024)) |
779 | a8083063 | Iustin Pop | |
780 | abdf0113 | Iustin Pop | def Rename(self, new_id): |
781 | abdf0113 | Iustin Pop | """Rename a device.
|
782 | a8083063 | Iustin Pop |
|
783 | abdf0113 | Iustin Pop | This is not supported for drbd devices.
|
784 | a8083063 | Iustin Pop |
|
785 | a8083063 | Iustin Pop | """
|
786 | abdf0113 | Iustin Pop | raise errors.ProgrammerError("Can't rename a drbd device") |
787 | a8083063 | Iustin Pop | |
788 | f3e513ad | Iustin Pop | |
789 | a2cfdea2 | Iustin Pop | class DRBD8(BaseDRBD): |
790 | a2cfdea2 | Iustin Pop | """DRBD v8.x block device.
|
791 | a2cfdea2 | Iustin Pop |
|
792 | a2cfdea2 | Iustin Pop | This implements the local host part of the DRBD device, i.e. it
|
793 | a2cfdea2 | Iustin Pop | doesn't do anything to the supposed peer. If you need a fully
|
794 | a2cfdea2 | Iustin Pop | connected DRBD pair, you need to use this class on both hosts.
|
795 | a2cfdea2 | Iustin Pop |
|
796 | a2cfdea2 | Iustin Pop | The unique_id for the drbd device is the (local_ip, local_port,
|
797 | a2cfdea2 | Iustin Pop | remote_ip, remote_port) tuple, and it must have two children: the
|
798 | a2cfdea2 | Iustin Pop | data device and the meta_device. The meta device is checked for
|
799 | a2cfdea2 | Iustin Pop | valid size and is zeroed on create.
|
800 | a2cfdea2 | Iustin Pop |
|
801 | a2cfdea2 | Iustin Pop | """
|
802 | a2cfdea2 | Iustin Pop | _MAX_MINORS = 255
|
803 | a2cfdea2 | Iustin Pop | _PARSE_SHOW = None
|
804 | a2cfdea2 | Iustin Pop | |
805 | cf8df3f3 | Iustin Pop | # timeout constants
|
806 | cf8df3f3 | Iustin Pop | _NET_RECONFIG_TIMEOUT = 60
|
807 | cf8df3f3 | Iustin Pop | |
808 | a2cfdea2 | Iustin Pop | def __init__(self, unique_id, children): |
809 | fc1dc9d7 | Iustin Pop | if children and children.count(None) > 0: |
810 | fc1dc9d7 | Iustin Pop | children = [] |
811 | a2cfdea2 | Iustin Pop | super(DRBD8, self).__init__(unique_id, children) |
812 | a2cfdea2 | Iustin Pop | self.major = self._DRBD_MAJOR |
813 | c3f9340c | Guido Trotter | version = self._GetVersion()
|
814 | c3f9340c | Guido Trotter | if version['k_major'] != 8 : |
815 | 82463074 | Iustin Pop | _ThrowError("Mismatch in DRBD kernel version and requested ganeti"
|
816 | 82463074 | Iustin Pop | " usage: kernel is %s.%s, ganeti wants 8.x",
|
817 | 82463074 | Iustin Pop | version['k_major'], version['k_minor']) |
818 | a2cfdea2 | Iustin Pop | |
819 | b00b95dd | Iustin Pop | if len(children) not in (0, 2): |
820 | a2cfdea2 | Iustin Pop | raise ValueError("Invalid configuration data %s" % str(children)) |
821 | f9518d38 | Iustin Pop | if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 6: |
822 | a2cfdea2 | Iustin Pop | raise ValueError("Invalid configuration data %s" % str(unique_id)) |
823 | ffa1c0dc | Iustin Pop | (self._lhost, self._lport, |
824 | ffa1c0dc | Iustin Pop | self._rhost, self._rport, |
825 | f9518d38 | Iustin Pop | self._aminor, self._secret) = unique_id |
826 | ffa1c0dc | Iustin Pop | if (self._lhost is not None and self._lhost == self._rhost and |
827 | ffa1c0dc | Iustin Pop | self._lport == self._rport): |
828 | ffa1c0dc | Iustin Pop | raise ValueError("Invalid configuration data, same local/remote %s" % |
829 | ffa1c0dc | Iustin Pop | (unique_id,)) |
830 | a2cfdea2 | Iustin Pop | self.Attach()
|
831 | a2cfdea2 | Iustin Pop | |
832 | a2cfdea2 | Iustin Pop | @classmethod
|
833 | a2cfdea2 | Iustin Pop | def _InitMeta(cls, minor, dev_path): |
834 | a2cfdea2 | Iustin Pop | """Initialize a meta device.
|
835 | a2cfdea2 | Iustin Pop |
|
836 | a2cfdea2 | Iustin Pop | This will not work if the given minor is in use.
|
837 | a2cfdea2 | Iustin Pop |
|
838 | a2cfdea2 | Iustin Pop | """
|
839 | a2cfdea2 | Iustin Pop | result = utils.RunCmd(["drbdmeta", "--force", cls._DevPath(minor), |
840 | a2cfdea2 | Iustin Pop | "v08", dev_path, "0", "create-md"]) |
841 | a2cfdea2 | Iustin Pop | if result.failed:
|
842 | 82463074 | Iustin Pop | _ThrowError("Can't initialize meta device: %s", result.output)
|
843 | a2cfdea2 | Iustin Pop | |
844 | a2cfdea2 | Iustin Pop | @classmethod
|
845 | a2cfdea2 | Iustin Pop | def _FindUnusedMinor(cls): |
846 | a2cfdea2 | Iustin Pop | """Find an unused DRBD device.
|
847 | a2cfdea2 | Iustin Pop |
|
848 | a2cfdea2 | Iustin Pop | This is specific to 8.x as the minors are allocated dynamically,
|
849 | a2cfdea2 | Iustin Pop | so non-existing numbers up to a max minor count are actually free.
|
850 | a2cfdea2 | Iustin Pop |
|
851 | a2cfdea2 | Iustin Pop | """
|
852 | a2cfdea2 | Iustin Pop | data = cls._GetProcData() |
853 | a2cfdea2 | Iustin Pop | |
854 | a2cfdea2 | Iustin Pop | unused_line = re.compile("^ *([0-9]+): cs:Unconfigured$")
|
855 | a2cfdea2 | Iustin Pop | used_line = re.compile("^ *([0-9]+): cs:")
|
856 | a2cfdea2 | Iustin Pop | highest = None
|
857 | a2cfdea2 | Iustin Pop | for line in data: |
858 | a2cfdea2 | Iustin Pop | match = unused_line.match(line) |
859 | a2cfdea2 | Iustin Pop | if match:
|
860 | a2cfdea2 | Iustin Pop | return int(match.group(1)) |
861 | a2cfdea2 | Iustin Pop | match = used_line.match(line) |
862 | a2cfdea2 | Iustin Pop | if match:
|
863 | a2cfdea2 | Iustin Pop | minor = int(match.group(1)) |
864 | a2cfdea2 | Iustin Pop | highest = max(highest, minor)
|
865 | a2cfdea2 | Iustin Pop | if highest is None: # there are no minors in use at all |
866 | a2cfdea2 | Iustin Pop | return 0 |
867 | a2cfdea2 | Iustin Pop | if highest >= cls._MAX_MINORS:
|
868 | 468c5f77 | Iustin Pop | logging.error("Error: no free drbd minors!")
|
869 | a2cfdea2 | Iustin Pop | raise errors.BlockDeviceError("Can't find a free DRBD minor") |
870 | a2cfdea2 | Iustin Pop | return highest + 1 |
871 | a2cfdea2 | Iustin Pop | |
872 | a2cfdea2 | Iustin Pop | @classmethod
|
873 | a2cfdea2 | Iustin Pop | def _GetShowParser(cls): |
874 | a2cfdea2 | Iustin Pop | """Return a parser for `drbd show` output.
|
875 | a2cfdea2 | Iustin Pop |
|
876 | a2cfdea2 | Iustin Pop | This will either create or return an already-create parser for the
|
877 | a2cfdea2 | Iustin Pop | output of the command `drbd show`.
|
878 | a2cfdea2 | Iustin Pop |
|
879 | a2cfdea2 | Iustin Pop | """
|
880 | a2cfdea2 | Iustin Pop | if cls._PARSE_SHOW is not None: |
881 | a2cfdea2 | Iustin Pop | return cls._PARSE_SHOW
|
882 | a2cfdea2 | Iustin Pop | |
883 | a2cfdea2 | Iustin Pop | # pyparsing setup
|
884 | a2cfdea2 | Iustin Pop | lbrace = pyp.Literal("{").suppress()
|
885 | a2cfdea2 | Iustin Pop | rbrace = pyp.Literal("}").suppress()
|
886 | a2cfdea2 | Iustin Pop | semi = pyp.Literal(";").suppress()
|
887 | a2cfdea2 | Iustin Pop | # this also converts the value to an int
|
888 | c522ea02 | Iustin Pop | number = pyp.Word(pyp.nums).setParseAction(lambda s, l, t: int(t[0])) |
889 | a2cfdea2 | Iustin Pop | |
890 | a2cfdea2 | Iustin Pop | comment = pyp.Literal ("#") + pyp.Optional(pyp.restOfLine)
|
891 | a2cfdea2 | Iustin Pop | defa = pyp.Literal("_is_default").suppress()
|
892 | a2cfdea2 | Iustin Pop | dbl_quote = pyp.Literal('"').suppress()
|
893 | a2cfdea2 | Iustin Pop | |
894 | a2cfdea2 | Iustin Pop | keyword = pyp.Word(pyp.alphanums + '-')
|
895 | a2cfdea2 | Iustin Pop | |
896 | a2cfdea2 | Iustin Pop | # value types
|
897 | a2cfdea2 | Iustin Pop | value = pyp.Word(pyp.alphanums + '_-/.:')
|
898 | a2cfdea2 | Iustin Pop | quoted = dbl_quote + pyp.CharsNotIn('"') + dbl_quote
|
899 | a2cfdea2 | Iustin Pop | addr_port = (pyp.Word(pyp.nums + '.') + pyp.Literal(':').suppress() + |
900 | a2cfdea2 | Iustin Pop | number) |
901 | a2cfdea2 | Iustin Pop | # meta device, extended syntax
|
902 | a2cfdea2 | Iustin Pop | meta_value = ((value ^ quoted) + pyp.Literal('[').suppress() +
|
903 | a2cfdea2 | Iustin Pop | number + pyp.Word(']').suppress())
|
904 | a2cfdea2 | Iustin Pop | |
905 | a2cfdea2 | Iustin Pop | # a statement
|
906 | a2cfdea2 | Iustin Pop | stmt = (~rbrace + keyword + ~lbrace + |
907 | 63012024 | Guido Trotter | pyp.Optional(addr_port ^ value ^ quoted ^ meta_value) + |
908 | a2cfdea2 | Iustin Pop | pyp.Optional(defa) + semi + |
909 | a2cfdea2 | Iustin Pop | pyp.Optional(pyp.restOfLine).suppress()) |
910 | a2cfdea2 | Iustin Pop | |
911 | a2cfdea2 | Iustin Pop | # an entire section
|
912 | a2cfdea2 | Iustin Pop | section_name = pyp.Word(pyp.alphas + '_')
|
913 | a2cfdea2 | Iustin Pop | section = section_name + lbrace + pyp.ZeroOrMore(pyp.Group(stmt)) + rbrace |
914 | a2cfdea2 | Iustin Pop | |
915 | a2cfdea2 | Iustin Pop | bnf = pyp.ZeroOrMore(pyp.Group(section ^ stmt)) |
916 | a2cfdea2 | Iustin Pop | bnf.ignore(comment) |
917 | a2cfdea2 | Iustin Pop | |
918 | a2cfdea2 | Iustin Pop | cls._PARSE_SHOW = bnf |
919 | a2cfdea2 | Iustin Pop | |
920 | a2cfdea2 | Iustin Pop | return bnf
|
921 | a2cfdea2 | Iustin Pop | |
922 | a2cfdea2 | Iustin Pop | @classmethod
|
923 | 3840729d | Iustin Pop | def _GetShowData(cls, minor): |
924 | 3840729d | Iustin Pop | """Return the `drbdsetup show` data for a minor.
|
925 | a2cfdea2 | Iustin Pop |
|
926 | a2cfdea2 | Iustin Pop | """
|
927 | a2cfdea2 | Iustin Pop | result = utils.RunCmd(["drbdsetup", cls._DevPath(minor), "show"]) |
928 | a2cfdea2 | Iustin Pop | if result.failed:
|
929 | 468c5f77 | Iustin Pop | logging.error("Can't display the drbd config: %s - %s",
|
930 | 468c5f77 | Iustin Pop | result.fail_reason, result.output) |
931 | 3840729d | Iustin Pop | return None |
932 | 3840729d | Iustin Pop | return result.stdout
|
933 | 3840729d | Iustin Pop | |
934 | 3840729d | Iustin Pop | @classmethod
|
935 | 3840729d | Iustin Pop | def _GetDevInfo(cls, out): |
936 | 3840729d | Iustin Pop | """Parse details about a given DRBD minor.
|
937 | 3840729d | Iustin Pop |
|
938 | 3840729d | Iustin Pop | This return, if available, the local backing device (as a path)
|
939 | 3840729d | Iustin Pop | and the local and remote (ip, port) information from a string
|
940 | 3840729d | Iustin Pop | containing the output of the `drbdsetup show` command as returned
|
941 | 3840729d | Iustin Pop | by _GetShowData.
|
942 | 3840729d | Iustin Pop |
|
943 | 3840729d | Iustin Pop | """
|
944 | 3840729d | Iustin Pop | data = {} |
945 | a2cfdea2 | Iustin Pop | if not out: |
946 | a2cfdea2 | Iustin Pop | return data
|
947 | a2cfdea2 | Iustin Pop | |
948 | a2cfdea2 | Iustin Pop | bnf = cls._GetShowParser() |
949 | a2cfdea2 | Iustin Pop | # run pyparse
|
950 | a2cfdea2 | Iustin Pop | |
951 | a2cfdea2 | Iustin Pop | try:
|
952 | a2cfdea2 | Iustin Pop | results = bnf.parseString(out) |
953 | a2cfdea2 | Iustin Pop | except pyp.ParseException, err:
|
954 | 82463074 | Iustin Pop | _ThrowError("Can't parse drbdsetup show output: %s", str(err)) |
955 | a2cfdea2 | Iustin Pop | |
956 | a2cfdea2 | Iustin Pop | # and massage the results into our desired format
|
957 | a2cfdea2 | Iustin Pop | for section in results: |
958 | a2cfdea2 | Iustin Pop | sname = section[0]
|
959 | a2cfdea2 | Iustin Pop | if sname == "_this_host": |
960 | a2cfdea2 | Iustin Pop | for lst in section[1:]: |
961 | a2cfdea2 | Iustin Pop | if lst[0] == "disk": |
962 | a2cfdea2 | Iustin Pop | data["local_dev"] = lst[1] |
963 | a2cfdea2 | Iustin Pop | elif lst[0] == "meta-disk": |
964 | a2cfdea2 | Iustin Pop | data["meta_dev"] = lst[1] |
965 | a2cfdea2 | Iustin Pop | data["meta_index"] = lst[2] |
966 | a2cfdea2 | Iustin Pop | elif lst[0] == "address": |
967 | a2cfdea2 | Iustin Pop | data["local_addr"] = tuple(lst[1:]) |
968 | a2cfdea2 | Iustin Pop | elif sname == "_remote_host": |
969 | a2cfdea2 | Iustin Pop | for lst in section[1:]: |
970 | a2cfdea2 | Iustin Pop | if lst[0] == "address": |
971 | a2cfdea2 | Iustin Pop | data["remote_addr"] = tuple(lst[1:]) |
972 | a2cfdea2 | Iustin Pop | return data
|
973 | a2cfdea2 | Iustin Pop | |
974 | a2cfdea2 | Iustin Pop | def _MatchesLocal(self, info): |
975 | a2cfdea2 | Iustin Pop | """Test if our local config matches with an existing device.
|
976 | a2cfdea2 | Iustin Pop |
|
977 | a2cfdea2 | Iustin Pop | The parameter should be as returned from `_GetDevInfo()`. This
|
978 | a2cfdea2 | Iustin Pop | method tests if our local backing device is the same as the one in
|
979 | a2cfdea2 | Iustin Pop | the info parameter, in effect testing if we look like the given
|
980 | a2cfdea2 | Iustin Pop | device.
|
981 | a2cfdea2 | Iustin Pop |
|
982 | a2cfdea2 | Iustin Pop | """
|
983 | b00b95dd | Iustin Pop | if self._children: |
984 | b00b95dd | Iustin Pop | backend, meta = self._children
|
985 | b00b95dd | Iustin Pop | else:
|
986 | b00b95dd | Iustin Pop | backend = meta = None
|
987 | b00b95dd | Iustin Pop | |
988 | a2cfdea2 | Iustin Pop | if backend is not None: |
989 | b00b95dd | Iustin Pop | retval = ("local_dev" in info and info["local_dev"] == backend.dev_path) |
990 | a2cfdea2 | Iustin Pop | else:
|
991 | a2cfdea2 | Iustin Pop | retval = ("local_dev" not in info) |
992 | b00b95dd | Iustin Pop | |
993 | a2cfdea2 | Iustin Pop | if meta is not None: |
994 | b00b95dd | Iustin Pop | retval = retval and ("meta_dev" in info and |
995 | b00b95dd | Iustin Pop | info["meta_dev"] == meta.dev_path)
|
996 | b00b95dd | Iustin Pop | retval = retval and ("meta_index" in info and |
997 | b00b95dd | Iustin Pop | info["meta_index"] == 0) |
998 | a2cfdea2 | Iustin Pop | else:
|
999 | a2cfdea2 | Iustin Pop | retval = retval and ("meta_dev" not in info and |
1000 | a2cfdea2 | Iustin Pop | "meta_index" not in info) |
1001 | a2cfdea2 | Iustin Pop | return retval
|
1002 | a2cfdea2 | Iustin Pop | |
1003 | a2cfdea2 | Iustin Pop | def _MatchesNet(self, info): |
1004 | a2cfdea2 | Iustin Pop | """Test if our network config matches with an existing device.
|
1005 | a2cfdea2 | Iustin Pop |
|
1006 | a2cfdea2 | Iustin Pop | The parameter should be as returned from `_GetDevInfo()`. This
|
1007 | a2cfdea2 | Iustin Pop | method tests if our network configuration is the same as the one
|
1008 | a2cfdea2 | Iustin Pop | in the info parameter, in effect testing if we look like the given
|
1009 | a2cfdea2 | Iustin Pop | device.
|
1010 | a2cfdea2 | Iustin Pop |
|
1011 | a2cfdea2 | Iustin Pop | """
|
1012 | a2cfdea2 | Iustin Pop | if (((self._lhost is None and not ("local_addr" in info)) and |
1013 | a2cfdea2 | Iustin Pop | (self._rhost is None and not ("remote_addr" in info)))): |
1014 | a2cfdea2 | Iustin Pop | return True |
1015 | a2cfdea2 | Iustin Pop | |
1016 | a2cfdea2 | Iustin Pop | if self._lhost is None: |
1017 | a2cfdea2 | Iustin Pop | return False |
1018 | a2cfdea2 | Iustin Pop | |
1019 | a2cfdea2 | Iustin Pop | if not ("local_addr" in info and |
1020 | a2cfdea2 | Iustin Pop | "remote_addr" in info): |
1021 | a2cfdea2 | Iustin Pop | return False |
1022 | a2cfdea2 | Iustin Pop | |
1023 | a2cfdea2 | Iustin Pop | retval = (info["local_addr"] == (self._lhost, self._lport)) |
1024 | a2cfdea2 | Iustin Pop | retval = (retval and
|
1025 | a2cfdea2 | Iustin Pop | info["remote_addr"] == (self._rhost, self._rport)) |
1026 | a2cfdea2 | Iustin Pop | return retval
|
1027 | a2cfdea2 | Iustin Pop | |
1028 | a2cfdea2 | Iustin Pop | @classmethod
|
1029 | a2cfdea2 | Iustin Pop | def _AssembleLocal(cls, minor, backend, meta): |
1030 | a2cfdea2 | Iustin Pop | """Configure the local part of a DRBD device.
|
1031 | a2cfdea2 | Iustin Pop |
|
1032 | a2cfdea2 | Iustin Pop | """
|
1033 | 333411a7 | Guido Trotter | args = ["drbdsetup", cls._DevPath(minor), "disk", |
1034 | 333411a7 | Guido Trotter | backend, meta, "0", "-e", "detach", "--create-device"] |
1035 | 333411a7 | Guido Trotter | result = utils.RunCmd(args) |
1036 | a2cfdea2 | Iustin Pop | if result.failed:
|
1037 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: can't attach local disk: %s", minor, result.output)
|
1038 | a2cfdea2 | Iustin Pop | |
1039 | a2cfdea2 | Iustin Pop | @classmethod
|
1040 | a2cfdea2 | Iustin Pop | def _AssembleNet(cls, minor, net_info, protocol, |
1041 | a2cfdea2 | Iustin Pop | dual_pri=False, hmac=None, secret=None): |
1042 | a2cfdea2 | Iustin Pop | """Configure the network part of the device.
|
1043 | a2cfdea2 | Iustin Pop |
|
1044 | a2cfdea2 | Iustin Pop | """
|
1045 | a2cfdea2 | Iustin Pop | lhost, lport, rhost, rport = net_info |
1046 | 52857176 | Iustin Pop | if None in net_info: |
1047 | 52857176 | Iustin Pop | # we don't want network connection and actually want to make
|
1048 | 52857176 | Iustin Pop | # sure its shutdown
|
1049 | 1063abd1 | Iustin Pop | cls._ShutdownNet(minor) |
1050 | 1063abd1 | Iustin Pop | return
|
1051 | 52857176 | Iustin Pop | |
1052 | 7d585316 | Iustin Pop | # Workaround for a race condition. When DRBD is doing its dance to
|
1053 | 7d585316 | Iustin Pop | # establish a connection with its peer, it also sends the
|
1054 | 7d585316 | Iustin Pop | # synchronization speed over the wire. In some cases setting the
|
1055 | 7d585316 | Iustin Pop | # sync speed only after setting up both sides can race with DRBD
|
1056 | 7d585316 | Iustin Pop | # connecting, hence we set it here before telling DRBD anything
|
1057 | 7d585316 | Iustin Pop | # about its peer.
|
1058 | 7d585316 | Iustin Pop | cls._SetMinorSyncSpeed(minor, constants.SYNC_SPEED) |
1059 | 7d585316 | Iustin Pop | |
1060 | a2cfdea2 | Iustin Pop | args = ["drbdsetup", cls._DevPath(minor), "net", |
1061 | f38478b2 | Iustin Pop | "%s:%s" % (lhost, lport), "%s:%s" % (rhost, rport), protocol, |
1062 | f38478b2 | Iustin Pop | "-A", "discard-zero-changes", |
1063 | f38478b2 | Iustin Pop | "-B", "consensus", |
1064 | ab6cc81c | Iustin Pop | "--create-device",
|
1065 | f38478b2 | Iustin Pop | ] |
1066 | a2cfdea2 | Iustin Pop | if dual_pri:
|
1067 | a2cfdea2 | Iustin Pop | args.append("-m")
|
1068 | a2cfdea2 | Iustin Pop | if hmac and secret: |
1069 | a2cfdea2 | Iustin Pop | args.extend(["-a", hmac, "-x", secret]) |
1070 | a2cfdea2 | Iustin Pop | result = utils.RunCmd(args) |
1071 | a2cfdea2 | Iustin Pop | if result.failed:
|
1072 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: can't setup network: %s - %s",
|
1073 | 1063abd1 | Iustin Pop | minor, result.fail_reason, result.output) |
1074 | a2cfdea2 | Iustin Pop | |
1075 | a2cfdea2 | Iustin Pop | timeout = time.time() + 10
|
1076 | a2cfdea2 | Iustin Pop | ok = False
|
1077 | a2cfdea2 | Iustin Pop | while time.time() < timeout:
|
1078 | 3840729d | Iustin Pop | info = cls._GetDevInfo(cls._GetShowData(minor)) |
1079 | a2cfdea2 | Iustin Pop | if not "local_addr" in info or not "remote_addr" in info: |
1080 | a2cfdea2 | Iustin Pop | time.sleep(1)
|
1081 | a2cfdea2 | Iustin Pop | continue
|
1082 | a2cfdea2 | Iustin Pop | if (info["local_addr"] != (lhost, lport) or |
1083 | a2cfdea2 | Iustin Pop | info["remote_addr"] != (rhost, rport)):
|
1084 | a2cfdea2 | Iustin Pop | time.sleep(1)
|
1085 | a2cfdea2 | Iustin Pop | continue
|
1086 | a2cfdea2 | Iustin Pop | ok = True
|
1087 | a2cfdea2 | Iustin Pop | break
|
1088 | a2cfdea2 | Iustin Pop | if not ok: |
1089 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: timeout while configuring network", minor)
|
1090 | a2cfdea2 | Iustin Pop | |
1091 | b00b95dd | Iustin Pop | def AddChildren(self, devices): |
1092 | b00b95dd | Iustin Pop | """Add a disk to the DRBD device.
|
1093 | b00b95dd | Iustin Pop |
|
1094 | b00b95dd | Iustin Pop | """
|
1095 | b00b95dd | Iustin Pop | if self.minor is None: |
1096 | 82463074 | Iustin Pop | _ThrowError("drbd%d: can't attach to dbrd8 during AddChildren",
|
1097 | 82463074 | Iustin Pop | self._aminor)
|
1098 | b00b95dd | Iustin Pop | if len(devices) != 2: |
1099 | 82463074 | Iustin Pop | _ThrowError("drbd%d: need two devices for AddChildren", self.minor) |
1100 | 3840729d | Iustin Pop | info = self._GetDevInfo(self._GetShowData(self.minor)) |
1101 | 03ece5f3 | Iustin Pop | if "local_dev" in info: |
1102 | 82463074 | Iustin Pop | _ThrowError("drbd%d: already attached to a local disk", self.minor) |
1103 | b00b95dd | Iustin Pop | backend, meta = devices |
1104 | b00b95dd | Iustin Pop | if backend.dev_path is None or meta.dev_path is None: |
1105 | 82463074 | Iustin Pop | _ThrowError("drbd%d: children not ready during AddChildren", self.minor) |
1106 | b00b95dd | Iustin Pop | backend.Open() |
1107 | b00b95dd | Iustin Pop | meta.Open() |
1108 | 9c793cfb | Iustin Pop | self._CheckMetaSize(meta.dev_path)
|
1109 | b00b95dd | Iustin Pop | self._InitMeta(self._FindUnusedMinor(), meta.dev_path) |
1110 | b00b95dd | Iustin Pop | |
1111 | 1063abd1 | Iustin Pop | self._AssembleLocal(self.minor, backend.dev_path, meta.dev_path) |
1112 | b00b95dd | Iustin Pop | self._children = devices
|
1113 | b00b95dd | Iustin Pop | |
1114 | b00b95dd | Iustin Pop | def RemoveChildren(self, devices): |
1115 | b00b95dd | Iustin Pop | """Detach the drbd device from local storage.
|
1116 | b00b95dd | Iustin Pop |
|
1117 | b00b95dd | Iustin Pop | """
|
1118 | b00b95dd | Iustin Pop | if self.minor is None: |
1119 | 82463074 | Iustin Pop | _ThrowError("drbd%d: can't attach to drbd8 during RemoveChildren",
|
1120 | 82463074 | Iustin Pop | self._aminor)
|
1121 | 03ece5f3 | Iustin Pop | # early return if we don't actually have backing storage
|
1122 | 3840729d | Iustin Pop | info = self._GetDevInfo(self._GetShowData(self.minor)) |
1123 | 03ece5f3 | Iustin Pop | if "local_dev" not in info: |
1124 | 03ece5f3 | Iustin Pop | return
|
1125 | b00b95dd | Iustin Pop | if len(self._children) != 2: |
1126 | 82463074 | Iustin Pop | _ThrowError("drbd%d: we don't have two children: %s", self.minor, |
1127 | 82463074 | Iustin Pop | self._children)
|
1128 | e739bd57 | Iustin Pop | if self._children.count(None) == 2: # we don't actually have children :) |
1129 | 82463074 | Iustin Pop | logging.warning("drbd%d: requested detach while detached", self.minor) |
1130 | e739bd57 | Iustin Pop | return
|
1131 | b00b95dd | Iustin Pop | if len(devices) != 2: |
1132 | 82463074 | Iustin Pop | _ThrowError("drbd%d: we need two children in RemoveChildren", self.minor) |
1133 | e739bd57 | Iustin Pop | for child, dev in zip(self._children, devices): |
1134 | e739bd57 | Iustin Pop | if dev != child.dev_path:
|
1135 | 82463074 | Iustin Pop | _ThrowError("drbd%d: mismatch in local storage (%s != %s) in"
|
1136 | 82463074 | Iustin Pop | " RemoveChildren", self.minor, dev, child.dev_path) |
1137 | b00b95dd | Iustin Pop | |
1138 | 1063abd1 | Iustin Pop | self._ShutdownLocal(self.minor) |
1139 | b00b95dd | Iustin Pop | self._children = []
|
1140 | b00b95dd | Iustin Pop | |
1141 | 7d585316 | Iustin Pop | @classmethod
|
1142 | 7d585316 | Iustin Pop | def _SetMinorSyncSpeed(cls, minor, kbytes): |
1143 | a2cfdea2 | Iustin Pop | """Set the speed of the DRBD syncer.
|
1144 | a2cfdea2 | Iustin Pop |
|
1145 | 7d585316 | Iustin Pop | This is the low-level implementation.
|
1146 | 7d585316 | Iustin Pop |
|
1147 | 7d585316 | Iustin Pop | @type minor: int
|
1148 | 7d585316 | Iustin Pop | @param minor: the drbd minor whose settings we change
|
1149 | 7d585316 | Iustin Pop | @type kbytes: int
|
1150 | 7d585316 | Iustin Pop | @param kbytes: the speed in kbytes/second
|
1151 | 7d585316 | Iustin Pop | @rtype: boolean
|
1152 | 7d585316 | Iustin Pop | @return: the success of the operation
|
1153 | 7d585316 | Iustin Pop |
|
1154 | a2cfdea2 | Iustin Pop | """
|
1155 | 7d585316 | Iustin Pop | result = utils.RunCmd(["drbdsetup", cls._DevPath(minor), "syncer", |
1156 | 7d585316 | Iustin Pop | "-r", "%d" % kbytes, "--create-device"]) |
1157 | a2cfdea2 | Iustin Pop | if result.failed:
|
1158 | 468c5f77 | Iustin Pop | logging.error("Can't change syncer rate: %s - %s",
|
1159 | 468c5f77 | Iustin Pop | result.fail_reason, result.output) |
1160 | 7d585316 | Iustin Pop | return not result.failed |
1161 | 7d585316 | Iustin Pop | |
1162 | 7d585316 | Iustin Pop | def SetSyncSpeed(self, kbytes): |
1163 | 7d585316 | Iustin Pop | """Set the speed of the DRBD syncer.
|
1164 | 7d585316 | Iustin Pop |
|
1165 | 7d585316 | Iustin Pop | @type kbytes: int
|
1166 | 7d585316 | Iustin Pop | @param kbytes: the speed in kbytes/second
|
1167 | 7d585316 | Iustin Pop | @rtype: boolean
|
1168 | 7d585316 | Iustin Pop | @return: the success of the operation
|
1169 | 7d585316 | Iustin Pop |
|
1170 | 7d585316 | Iustin Pop | """
|
1171 | 7d585316 | Iustin Pop | if self.minor is None: |
1172 | 7d585316 | Iustin Pop | logging.info("Not attached during SetSyncSpeed")
|
1173 | 7d585316 | Iustin Pop | return False |
1174 | 7d585316 | Iustin Pop | children_result = super(DRBD8, self).SetSyncSpeed(kbytes) |
1175 | 7d585316 | Iustin Pop | return self._SetMinorSyncSpeed(self.minor, kbytes) and children_result |
1176 | a2cfdea2 | Iustin Pop | |
1177 | 6b90c22e | Iustin Pop | def GetProcStatus(self): |
1178 | 6b90c22e | Iustin Pop | """Return device data from /proc.
|
1179 | 6b90c22e | Iustin Pop |
|
1180 | 6b90c22e | Iustin Pop | """
|
1181 | 6b90c22e | Iustin Pop | if self.minor is None: |
1182 | 82463074 | Iustin Pop | _ThrowError("drbd%d: GetStats() called while not attached", self._aminor) |
1183 | 6b90c22e | Iustin Pop | proc_info = self._MassageProcData(self._GetProcData()) |
1184 | 6b90c22e | Iustin Pop | if self.minor not in proc_info: |
1185 | 82463074 | Iustin Pop | _ThrowError("drbd%d: can't find myself in /proc", self.minor) |
1186 | 6b90c22e | Iustin Pop | return DRBD8Status(proc_info[self.minor]) |
1187 | 6b90c22e | Iustin Pop | |
1188 | a2cfdea2 | Iustin Pop | def GetSyncStatus(self): |
1189 | a2cfdea2 | Iustin Pop | """Returns the sync status of the device.
|
1190 | a2cfdea2 | Iustin Pop |
|
1191 | a2cfdea2 | Iustin Pop |
|
1192 | a2cfdea2 | Iustin Pop | If sync_percent is None, it means all is ok
|
1193 | a2cfdea2 | Iustin Pop | If estimated_time is None, it means we can't esimate
|
1194 | 0834c866 | Iustin Pop | the time needed, otherwise it's the time left in seconds.
|
1195 | 0834c866 | Iustin Pop |
|
1196 | 0834c866 | Iustin Pop |
|
1197 | 0834c866 | Iustin Pop | We set the is_degraded parameter to True on two conditions:
|
1198 | 0834c866 | Iustin Pop | network not connected or local disk missing.
|
1199 | 0834c866 | Iustin Pop |
|
1200 | 0834c866 | Iustin Pop | We compute the ldisk parameter based on wheter we have a local
|
1201 | 0834c866 | Iustin Pop | disk or not.
|
1202 | a2cfdea2 | Iustin Pop |
|
1203 | c41eea6e | Iustin Pop | @rtype: tuple
|
1204 | c41eea6e | Iustin Pop | @return: (sync_percent, estimated_time, is_degraded, ldisk)
|
1205 | c41eea6e | Iustin Pop |
|
1206 | a2cfdea2 | Iustin Pop | """
|
1207 | a2cfdea2 | Iustin Pop | if self.minor is None and not self.Attach(): |
1208 | 82463074 | Iustin Pop | _ThrowError("drbd%d: can't Attach() in GetSyncStatus", self._aminor) |
1209 | 6b90c22e | Iustin Pop | stats = self.GetProcStatus()
|
1210 | 6b90c22e | Iustin Pop | ldisk = not stats.is_disk_uptodate
|
1211 | 6b90c22e | Iustin Pop | is_degraded = not stats.is_connected
|
1212 | 6b90c22e | Iustin Pop | return stats.sync_percent, stats.est_time, is_degraded or ldisk, ldisk |
1213 | a2cfdea2 | Iustin Pop | |
1214 | a2cfdea2 | Iustin Pop | def Open(self, force=False): |
1215 | a2cfdea2 | Iustin Pop | """Make the local state primary.
|
1216 | a2cfdea2 | Iustin Pop |
|
1217 | f860ff4e | Guido Trotter | If the 'force' parameter is given, the '-o' option is passed to
|
1218 | f860ff4e | Guido Trotter | drbdsetup. Since this is a potentially dangerous operation, the
|
1219 | a2cfdea2 | Iustin Pop | force flag should be only given after creation, when it actually
|
1220 | f860ff4e | Guido Trotter | is mandatory.
|
1221 | a2cfdea2 | Iustin Pop |
|
1222 | a2cfdea2 | Iustin Pop | """
|
1223 | a2cfdea2 | Iustin Pop | if self.minor is None and not self.Attach(): |
1224 | 468c5f77 | Iustin Pop | logging.error("DRBD cannot attach to a device during open")
|
1225 | a2cfdea2 | Iustin Pop | return False |
1226 | a2cfdea2 | Iustin Pop | cmd = ["drbdsetup", self.dev_path, "primary"] |
1227 | a2cfdea2 | Iustin Pop | if force:
|
1228 | a2cfdea2 | Iustin Pop | cmd.append("-o")
|
1229 | a2cfdea2 | Iustin Pop | result = utils.RunCmd(cmd) |
1230 | a2cfdea2 | Iustin Pop | if result.failed:
|
1231 | 82463074 | Iustin Pop | _ThrowError("drbd%d: can't make drbd device primary: %s", self.minor, |
1232 | 82463074 | Iustin Pop | result.output) |
1233 | a2cfdea2 | Iustin Pop | |
1234 | a2cfdea2 | Iustin Pop | def Close(self): |
1235 | a2cfdea2 | Iustin Pop | """Make the local state secondary.
|
1236 | a2cfdea2 | Iustin Pop |
|
1237 | a2cfdea2 | Iustin Pop | This will, of course, fail if the device is in use.
|
1238 | a2cfdea2 | Iustin Pop |
|
1239 | a2cfdea2 | Iustin Pop | """
|
1240 | a2cfdea2 | Iustin Pop | if self.minor is None and not self.Attach(): |
1241 | 82463074 | Iustin Pop | _ThrowError("drbd%d: can't Attach() in Close()", self._aminor) |
1242 | a2cfdea2 | Iustin Pop | result = utils.RunCmd(["drbdsetup", self.dev_path, "secondary"]) |
1243 | a2cfdea2 | Iustin Pop | if result.failed:
|
1244 | 82463074 | Iustin Pop | _ThrowError("drbd%d: can't switch drbd device to secondary: %s",
|
1245 | 82463074 | Iustin Pop | self.minor, result.output)
|
1246 | a2cfdea2 | Iustin Pop | |
1247 | cf8df3f3 | Iustin Pop | def DisconnectNet(self): |
1248 | cf8df3f3 | Iustin Pop | """Removes network configuration.
|
1249 | cf8df3f3 | Iustin Pop |
|
1250 | cf8df3f3 | Iustin Pop | This method shutdowns the network side of the device.
|
1251 | cf8df3f3 | Iustin Pop |
|
1252 | cf8df3f3 | Iustin Pop | The method will wait up to a hardcoded timeout for the device to
|
1253 | cf8df3f3 | Iustin Pop | go into standalone after the 'disconnect' command before
|
1254 | cf8df3f3 | Iustin Pop | re-configuring it, as sometimes it takes a while for the
|
1255 | cf8df3f3 | Iustin Pop | disconnect to actually propagate and thus we might issue a 'net'
|
1256 | cf8df3f3 | Iustin Pop | command while the device is still connected. If the device will
|
1257 | cf8df3f3 | Iustin Pop | still be attached to the network and we time out, we raise an
|
1258 | cf8df3f3 | Iustin Pop | exception.
|
1259 | cf8df3f3 | Iustin Pop |
|
1260 | cf8df3f3 | Iustin Pop | """
|
1261 | cf8df3f3 | Iustin Pop | if self.minor is None: |
1262 | 82463074 | Iustin Pop | _ThrowError("drbd%d: disk not attached in re-attach net", self._aminor) |
1263 | cf8df3f3 | Iustin Pop | |
1264 | cf8df3f3 | Iustin Pop | if None in (self._lhost, self._lport, self._rhost, self._rport): |
1265 | 82463074 | Iustin Pop | _ThrowError("drbd%d: DRBD disk missing network info in"
|
1266 | 82463074 | Iustin Pop | " DisconnectNet()", self.minor) |
1267 | cf8df3f3 | Iustin Pop | |
1268 | 1063abd1 | Iustin Pop | ever_disconnected = _IgnoreError(self._ShutdownNet, self.minor) |
1269 | cf8df3f3 | Iustin Pop | timeout_limit = time.time() + self._NET_RECONFIG_TIMEOUT
|
1270 | cf8df3f3 | Iustin Pop | sleep_time = 0.100 # we start the retry time at 100 miliseconds |
1271 | cf8df3f3 | Iustin Pop | while time.time() < timeout_limit:
|
1272 | cf8df3f3 | Iustin Pop | status = self.GetProcStatus()
|
1273 | cf8df3f3 | Iustin Pop | if status.is_standalone:
|
1274 | cf8df3f3 | Iustin Pop | break
|
1275 | cf8df3f3 | Iustin Pop | # retry the disconnect, it seems possible that due to a
|
1276 | cf8df3f3 | Iustin Pop | # well-time disconnect on the peer, my disconnect command might
|
1277 | cf8df3f3 | Iustin Pop | # be ingored and forgotten
|
1278 | 1063abd1 | Iustin Pop | ever_disconnected = _IgnoreError(self._ShutdownNet, self.minor) or \ |
1279 | 1063abd1 | Iustin Pop | ever_disconnected |
1280 | cf8df3f3 | Iustin Pop | time.sleep(sleep_time) |
1281 | cf8df3f3 | Iustin Pop | sleep_time = min(2, sleep_time * 1.5) |
1282 | cf8df3f3 | Iustin Pop | |
1283 | cf8df3f3 | Iustin Pop | if not status.is_standalone: |
1284 | cf8df3f3 | Iustin Pop | if ever_disconnected:
|
1285 | 82463074 | Iustin Pop | msg = ("drbd%d: device did not react to the"
|
1286 | cf8df3f3 | Iustin Pop | " 'disconnect' command in a timely manner")
|
1287 | cf8df3f3 | Iustin Pop | else:
|
1288 | 82463074 | Iustin Pop | msg = "drbd%d: can't shutdown network, even after multiple retries"
|
1289 | 82463074 | Iustin Pop | _ThrowError(msg, self.minor)
|
1290 | cf8df3f3 | Iustin Pop | |
1291 | cf8df3f3 | Iustin Pop | reconfig_time = time.time() - timeout_limit + self._NET_RECONFIG_TIMEOUT
|
1292 | cf8df3f3 | Iustin Pop | if reconfig_time > 15: # hardcoded alert limit |
1293 | 82463074 | Iustin Pop | logging.info("drbd%d: DisconnectNet: detach took %.3f seconds",
|
1294 | 82463074 | Iustin Pop | self.minor, reconfig_time)
|
1295 | cf8df3f3 | Iustin Pop | |
1296 | cf8df3f3 | Iustin Pop | def AttachNet(self, multimaster): |
1297 | cf8df3f3 | Iustin Pop | """Reconnects the network.
|
1298 | cf8df3f3 | Iustin Pop |
|
1299 | cf8df3f3 | Iustin Pop | This method connects the network side of the device with a
|
1300 | cf8df3f3 | Iustin Pop | specified multi-master flag. The device needs to be 'Standalone'
|
1301 | cf8df3f3 | Iustin Pop | but have valid network configuration data.
|
1302 | cf8df3f3 | Iustin Pop |
|
1303 | cf8df3f3 | Iustin Pop | Args:
|
1304 | cf8df3f3 | Iustin Pop | - multimaster: init the network in dual-primary mode
|
1305 | cf8df3f3 | Iustin Pop |
|
1306 | cf8df3f3 | Iustin Pop | """
|
1307 | cf8df3f3 | Iustin Pop | if self.minor is None: |
1308 | 82463074 | Iustin Pop | _ThrowError("drbd%d: device not attached in AttachNet", self._aminor) |
1309 | cf8df3f3 | Iustin Pop | |
1310 | cf8df3f3 | Iustin Pop | if None in (self._lhost, self._lport, self._rhost, self._rport): |
1311 | 82463074 | Iustin Pop | _ThrowError("drbd%d: missing network info in AttachNet()", self.minor) |
1312 | cf8df3f3 | Iustin Pop | |
1313 | cf8df3f3 | Iustin Pop | status = self.GetProcStatus()
|
1314 | cf8df3f3 | Iustin Pop | |
1315 | cf8df3f3 | Iustin Pop | if not status.is_standalone: |
1316 | 82463074 | Iustin Pop | _ThrowError("drbd%d: device is not standalone in AttachNet", self.minor) |
1317 | cf8df3f3 | Iustin Pop | |
1318 | 1063abd1 | Iustin Pop | self._AssembleNet(self.minor, |
1319 | 1063abd1 | Iustin Pop | (self._lhost, self._lport, self._rhost, self._rport), |
1320 | 1063abd1 | Iustin Pop | constants.DRBD_NET_PROTOCOL, dual_pri=multimaster, |
1321 | 1063abd1 | Iustin Pop | hmac=constants.DRBD_HMAC_ALG, secret=self._secret)
|
1322 | cf8df3f3 | Iustin Pop | |
1323 | a2cfdea2 | Iustin Pop | def Attach(self): |
1324 | 2d0c8319 | Iustin Pop | """Check if our minor is configured.
|
1325 | 2d0c8319 | Iustin Pop |
|
1326 | 2d0c8319 | Iustin Pop | This doesn't do any device configurations - it only checks if the
|
1327 | 2d0c8319 | Iustin Pop | minor is in a state different from Unconfigured.
|
1328 | 2d0c8319 | Iustin Pop |
|
1329 | 2d0c8319 | Iustin Pop | Note that this function will not change the state of the system in
|
1330 | 2d0c8319 | Iustin Pop | any way (except in case of side-effects caused by reading from
|
1331 | 2d0c8319 | Iustin Pop | /proc).
|
1332 | 2d0c8319 | Iustin Pop |
|
1333 | 2d0c8319 | Iustin Pop | """
|
1334 | 6d2e83d5 | Iustin Pop | used_devs = self.GetUsedDevs()
|
1335 | 2d0c8319 | Iustin Pop | if self._aminor in used_devs: |
1336 | 2d0c8319 | Iustin Pop | minor = self._aminor
|
1337 | 2d0c8319 | Iustin Pop | else:
|
1338 | 2d0c8319 | Iustin Pop | minor = None
|
1339 | 2d0c8319 | Iustin Pop | |
1340 | 2d0c8319 | Iustin Pop | self._SetFromMinor(minor)
|
1341 | 2d0c8319 | Iustin Pop | return minor is not None |
1342 | 2d0c8319 | Iustin Pop | |
1343 | 2d0c8319 | Iustin Pop | def Assemble(self): |
1344 | 2d0c8319 | Iustin Pop | """Assemble the drbd.
|
1345 | 2d0c8319 | Iustin Pop |
|
1346 | 2d0c8319 | Iustin Pop | Method:
|
1347 | 2d0c8319 | Iustin Pop | - if we have a configured device, we try to ensure that it matches
|
1348 | 2d0c8319 | Iustin Pop | our config
|
1349 | 2d0c8319 | Iustin Pop | - if not, we create it from zero
|
1350 | 2d0c8319 | Iustin Pop |
|
1351 | 2d0c8319 | Iustin Pop | """
|
1352 | 1063abd1 | Iustin Pop | super(DRBD8, self).Assemble() |
1353 | 2d0c8319 | Iustin Pop | |
1354 | 2d0c8319 | Iustin Pop | self.Attach()
|
1355 | 2d0c8319 | Iustin Pop | if self.minor is None: |
1356 | 2d0c8319 | Iustin Pop | # local device completely unconfigured
|
1357 | 1063abd1 | Iustin Pop | self._FastAssemble()
|
1358 | 2d0c8319 | Iustin Pop | else:
|
1359 | 2d0c8319 | Iustin Pop | # we have to recheck the local and network status and try to fix
|
1360 | 2d0c8319 | Iustin Pop | # the device
|
1361 | 1063abd1 | Iustin Pop | self._SlowAssemble()
|
1362 | 2d0c8319 | Iustin Pop | |
1363 | 2d0c8319 | Iustin Pop | def _SlowAssemble(self): |
1364 | 2d0c8319 | Iustin Pop | """Assembles the DRBD device from a (partially) configured device.
|
1365 | a2cfdea2 | Iustin Pop |
|
1366 | a2cfdea2 | Iustin Pop | In case of partially attached (local device matches but no network
|
1367 | a2cfdea2 | Iustin Pop | setup), we perform the network attach. If successful, we re-test
|
1368 | a2cfdea2 | Iustin Pop | the attach if can return success.
|
1369 | a2cfdea2 | Iustin Pop |
|
1370 | a2cfdea2 | Iustin Pop | """
|
1371 | 1063abd1 | Iustin Pop | net_data = (self._lhost, self._lport, self._rhost, self._rport) |
1372 | a1578d63 | Iustin Pop | for minor in (self._aminor,): |
1373 | 3840729d | Iustin Pop | info = self._GetDevInfo(self._GetShowData(minor)) |
1374 | a2cfdea2 | Iustin Pop | match_l = self._MatchesLocal(info)
|
1375 | a2cfdea2 | Iustin Pop | match_r = self._MatchesNet(info)
|
1376 | 1063abd1 | Iustin Pop | |
1377 | a2cfdea2 | Iustin Pop | if match_l and match_r: |
1378 | 1063abd1 | Iustin Pop | # everything matches
|
1379 | a2cfdea2 | Iustin Pop | break
|
1380 | 1063abd1 | Iustin Pop | |
1381 | a2cfdea2 | Iustin Pop | if match_l and not match_r and "local_addr" not in info: |
1382 | 1063abd1 | Iustin Pop | # disk matches, but not attached to network, attach and recheck
|
1383 | 1063abd1 | Iustin Pop | self._AssembleNet(minor, net_data, constants.DRBD_NET_PROTOCOL,
|
1384 | 1063abd1 | Iustin Pop | hmac=constants.DRBD_HMAC_ALG, secret=self._secret)
|
1385 | 1063abd1 | Iustin Pop | if self._MatchesNet(self._GetDevInfo(self._GetShowData(minor))): |
1386 | 1063abd1 | Iustin Pop | break
|
1387 | 1063abd1 | Iustin Pop | else:
|
1388 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: network attach successful, but 'drbdsetup"
|
1389 | 1063abd1 | Iustin Pop | " show' disagrees", minor)
|
1390 | 1063abd1 | Iustin Pop | |
1391 | fc1dc9d7 | Iustin Pop | if match_r and "local_dev" not in info: |
1392 | 1063abd1 | Iustin Pop | # no local disk, but network attached and it matches
|
1393 | 1063abd1 | Iustin Pop | self._AssembleLocal(minor, self._children[0].dev_path, |
1394 | 1063abd1 | Iustin Pop | self._children[1].dev_path) |
1395 | 1063abd1 | Iustin Pop | if self._MatchesNet(self._GetDevInfo(self._GetShowData(minor))): |
1396 | 1063abd1 | Iustin Pop | break
|
1397 | 1063abd1 | Iustin Pop | else:
|
1398 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: disk attach successful, but 'drbdsetup"
|
1399 | 1063abd1 | Iustin Pop | " show' disagrees", minor)
|
1400 | bf25af3b | Iustin Pop | |
1401 | bf25af3b | Iustin Pop | # this case must be considered only if we actually have local
|
1402 | bf25af3b | Iustin Pop | # storage, i.e. not in diskless mode, because all diskless
|
1403 | bf25af3b | Iustin Pop | # devices are equal from the point of view of local
|
1404 | bf25af3b | Iustin Pop | # configuration
|
1405 | bf25af3b | Iustin Pop | if (match_l and "local_dev" in info and |
1406 | bf25af3b | Iustin Pop | not match_r and "local_addr" in info): |
1407 | 9cdbe77f | Iustin Pop | # strange case - the device network part points to somewhere
|
1408 | 9cdbe77f | Iustin Pop | # else, even though its local storage is ours; as we own the
|
1409 | 9cdbe77f | Iustin Pop | # drbd space, we try to disconnect from the remote peer and
|
1410 | 9cdbe77f | Iustin Pop | # reconnect to our correct one
|
1411 | 1063abd1 | Iustin Pop | try:
|
1412 | 1063abd1 | Iustin Pop | self._ShutdownNet(minor)
|
1413 | 1063abd1 | Iustin Pop | except errors.BlockDeviceError, err:
|
1414 | 33bc6f01 | Iustin Pop | _ThrowError("drbd%d: device has correct local storage, wrong"
|
1415 | 33bc6f01 | Iustin Pop | " remote peer and is unable to disconnect in order"
|
1416 | 33bc6f01 | Iustin Pop | " to attach to the correct peer: %s", minor, str(err)) |
1417 | 9cdbe77f | Iustin Pop | # note: _AssembleNet also handles the case when we don't want
|
1418 | 9cdbe77f | Iustin Pop | # local storage (i.e. one or more of the _[lr](host|port) is
|
1419 | 9cdbe77f | Iustin Pop | # None)
|
1420 | 1063abd1 | Iustin Pop | self._AssembleNet(minor, net_data, constants.DRBD_NET_PROTOCOL,
|
1421 | 1063abd1 | Iustin Pop | hmac=constants.DRBD_HMAC_ALG, secret=self._secret)
|
1422 | 1063abd1 | Iustin Pop | if self._MatchesNet(self._GetDevInfo(self._GetShowData(minor))): |
1423 | 9cdbe77f | Iustin Pop | break
|
1424 | 1063abd1 | Iustin Pop | else:
|
1425 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: network attach successful, but 'drbdsetup"
|
1426 | 1063abd1 | Iustin Pop | " show' disagrees", minor)
|
1427 | 9cdbe77f | Iustin Pop | |
1428 | a2cfdea2 | Iustin Pop | else:
|
1429 | a2cfdea2 | Iustin Pop | minor = None
|
1430 | a2cfdea2 | Iustin Pop | |
1431 | a2cfdea2 | Iustin Pop | self._SetFromMinor(minor)
|
1432 | 1063abd1 | Iustin Pop | if minor is None: |
1433 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: cannot activate, unknown or unhandled reason",
|
1434 | 1063abd1 | Iustin Pop | self._aminor)
|
1435 | a2cfdea2 | Iustin Pop | |
1436 | 2d0c8319 | Iustin Pop | def _FastAssemble(self): |
1437 | 2d0c8319 | Iustin Pop | """Assemble the drbd device from zero.
|
1438 | a2cfdea2 | Iustin Pop |
|
1439 | 2d0c8319 | Iustin Pop | This is run when in Assemble we detect our minor is unused.
|
1440 | a2cfdea2 | Iustin Pop |
|
1441 | a2cfdea2 | Iustin Pop | """
|
1442 | a1578d63 | Iustin Pop | minor = self._aminor
|
1443 | fc1dc9d7 | Iustin Pop | if self._children and self._children[0] and self._children[1]: |
1444 | 1063abd1 | Iustin Pop | self._AssembleLocal(minor, self._children[0].dev_path, |
1445 | 1063abd1 | Iustin Pop | self._children[1].dev_path) |
1446 | a2cfdea2 | Iustin Pop | if self._lhost and self._lport and self._rhost and self._rport: |
1447 | 1063abd1 | Iustin Pop | self._AssembleNet(minor,
|
1448 | 1063abd1 | Iustin Pop | (self._lhost, self._lport, self._rhost, self._rport), |
1449 | 1063abd1 | Iustin Pop | constants.DRBD_NET_PROTOCOL, |
1450 | 1063abd1 | Iustin Pop | hmac=constants.DRBD_HMAC_ALG, secret=self._secret)
|
1451 | a2cfdea2 | Iustin Pop | self._SetFromMinor(minor)
|
1452 | a2cfdea2 | Iustin Pop | |
1453 | a2cfdea2 | Iustin Pop | @classmethod
|
1454 | b00b95dd | Iustin Pop | def _ShutdownLocal(cls, minor): |
1455 | b00b95dd | Iustin Pop | """Detach from the local device.
|
1456 | b00b95dd | Iustin Pop |
|
1457 | b00b95dd | Iustin Pop | I/Os will continue to be served from the remote device. If we
|
1458 | b00b95dd | Iustin Pop | don't have a remote device, this operation will fail.
|
1459 | b00b95dd | Iustin Pop |
|
1460 | b00b95dd | Iustin Pop | """
|
1461 | b00b95dd | Iustin Pop | result = utils.RunCmd(["drbdsetup", cls._DevPath(minor), "detach"]) |
1462 | b00b95dd | Iustin Pop | if result.failed:
|
1463 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: can't detach local disk: %s", minor, result.output)
|
1464 | b00b95dd | Iustin Pop | |
1465 | b00b95dd | Iustin Pop | @classmethod
|
1466 | f3e513ad | Iustin Pop | def _ShutdownNet(cls, minor): |
1467 | f3e513ad | Iustin Pop | """Disconnect from the remote peer.
|
1468 | f3e513ad | Iustin Pop |
|
1469 | f3e513ad | Iustin Pop | This fails if we don't have a local device.
|
1470 | f3e513ad | Iustin Pop |
|
1471 | f3e513ad | Iustin Pop | """
|
1472 | f3e513ad | Iustin Pop | result = utils.RunCmd(["drbdsetup", cls._DevPath(minor), "disconnect"]) |
1473 | a8459f1c | Iustin Pop | if result.failed:
|
1474 | 1063abd1 | Iustin Pop | _ThrowError("drbd%d: can't shutdown network: %s", minor, result.output)
|
1475 | f3e513ad | Iustin Pop | |
1476 | f3e513ad | Iustin Pop | @classmethod
|
1477 | a2cfdea2 | Iustin Pop | def _ShutdownAll(cls, minor): |
1478 | a2cfdea2 | Iustin Pop | """Deactivate the device.
|
1479 | a2cfdea2 | Iustin Pop |
|
1480 | a2cfdea2 | Iustin Pop | This will, of course, fail if the device is in use.
|
1481 | a2cfdea2 | Iustin Pop |
|
1482 | a2cfdea2 | Iustin Pop | """
|
1483 | a2cfdea2 | Iustin Pop | result = utils.RunCmd(["drbdsetup", cls._DevPath(minor), "down"]) |
1484 | a2cfdea2 | Iustin Pop | if result.failed:
|
1485 | 33bc6f01 | Iustin Pop | _ThrowError("drbd%d: can't shutdown drbd device: %s",
|
1486 | 33bc6f01 | Iustin Pop | minor, result.output) |
1487 | a2cfdea2 | Iustin Pop | |
1488 | a2cfdea2 | Iustin Pop | def Shutdown(self): |
1489 | a2cfdea2 | Iustin Pop | """Shutdown the DRBD device.
|
1490 | a2cfdea2 | Iustin Pop |
|
1491 | a2cfdea2 | Iustin Pop | """
|
1492 | a2cfdea2 | Iustin Pop | if self.minor is None and not self.Attach(): |
1493 | 746f7476 | Iustin Pop | logging.info("drbd%d: not attached during Shutdown()", self._aminor) |
1494 | 746f7476 | Iustin Pop | return
|
1495 | 746f7476 | Iustin Pop | minor = self.minor
|
1496 | a2cfdea2 | Iustin Pop | self.minor = None |
1497 | a2cfdea2 | Iustin Pop | self.dev_path = None |
1498 | 746f7476 | Iustin Pop | self._ShutdownAll(minor)
|
1499 | a2cfdea2 | Iustin Pop | |
1500 | a2cfdea2 | Iustin Pop | def Remove(self): |
1501 | a2cfdea2 | Iustin Pop | """Stub remove for DRBD devices.
|
1502 | a2cfdea2 | Iustin Pop |
|
1503 | a2cfdea2 | Iustin Pop | """
|
1504 | 0c6c04ec | Iustin Pop | self.Shutdown()
|
1505 | a2cfdea2 | Iustin Pop | |
1506 | a2cfdea2 | Iustin Pop | @classmethod
|
1507 | a2cfdea2 | Iustin Pop | def Create(cls, unique_id, children, size): |
1508 | a2cfdea2 | Iustin Pop | """Create a new DRBD8 device.
|
1509 | a2cfdea2 | Iustin Pop |
|
1510 | a2cfdea2 | Iustin Pop | Since DRBD devices are not created per se, just assembled, this
|
1511 | a2cfdea2 | Iustin Pop | function only initializes the metadata.
|
1512 | a2cfdea2 | Iustin Pop |
|
1513 | a2cfdea2 | Iustin Pop | """
|
1514 | a2cfdea2 | Iustin Pop | if len(children) != 2: |
1515 | a2cfdea2 | Iustin Pop | raise errors.ProgrammerError("Invalid setup for the drbd device") |
1516 | 767d52d3 | Iustin Pop | # check that the minor is unused
|
1517 | 767d52d3 | Iustin Pop | aminor = unique_id[4]
|
1518 | 767d52d3 | Iustin Pop | proc_info = cls._MassageProcData(cls._GetProcData()) |
1519 | 767d52d3 | Iustin Pop | if aminor in proc_info: |
1520 | 767d52d3 | Iustin Pop | status = DRBD8Status(proc_info[aminor]) |
1521 | 767d52d3 | Iustin Pop | in_use = status.is_in_use |
1522 | 767d52d3 | Iustin Pop | else:
|
1523 | 767d52d3 | Iustin Pop | in_use = False
|
1524 | 767d52d3 | Iustin Pop | if in_use:
|
1525 | 33bc6f01 | Iustin Pop | _ThrowError("drbd%d: minor is already in use at Create() time", aminor)
|
1526 | a2cfdea2 | Iustin Pop | meta = children[1]
|
1527 | a2cfdea2 | Iustin Pop | meta.Assemble() |
1528 | a2cfdea2 | Iustin Pop | if not meta.Attach(): |
1529 | 33bc6f01 | Iustin Pop | _ThrowError("drbd%d: can't attach to meta device '%s'",
|
1530 | 33bc6f01 | Iustin Pop | aminor, meta) |
1531 | 9c793cfb | Iustin Pop | cls._CheckMetaSize(meta.dev_path) |
1532 | 3b559640 | Iustin Pop | cls._InitMeta(aminor, meta.dev_path) |
1533 | a2cfdea2 | Iustin Pop | return cls(unique_id, children)
|
1534 | a2cfdea2 | Iustin Pop | |
1535 | 1005d816 | Iustin Pop | def Grow(self, amount): |
1536 | 1005d816 | Iustin Pop | """Resize the DRBD device and its backing storage.
|
1537 | 1005d816 | Iustin Pop |
|
1538 | 1005d816 | Iustin Pop | """
|
1539 | 1005d816 | Iustin Pop | if self.minor is None: |
1540 | 82463074 | Iustin Pop | _ThrowError("drbd%d: Grow called while not attached", self._aminor) |
1541 | 1005d816 | Iustin Pop | if len(self._children) != 2 or None in self._children: |
1542 | 82463074 | Iustin Pop | _ThrowError("drbd%d: cannot grow diskless device", self.minor) |
1543 | 1005d816 | Iustin Pop | self._children[0].Grow(amount) |
1544 | 1005d816 | Iustin Pop | result = utils.RunCmd(["drbdsetup", self.dev_path, "resize"]) |
1545 | 1005d816 | Iustin Pop | if result.failed:
|
1546 | 82463074 | Iustin Pop | _ThrowError("drbd%d: resize failed: %s", self.minor, result.output) |
1547 | 1005d816 | Iustin Pop | |
1548 | a8083063 | Iustin Pop | |
1549 | 6f695a2e | Manuel Franceschini | class FileStorage(BlockDev): |
1550 | 6f695a2e | Manuel Franceschini | """File device.
|
1551 | abdf0113 | Iustin Pop |
|
1552 | 6f695a2e | Manuel Franceschini | This class represents the a file storage backend device.
|
1553 | 6f695a2e | Manuel Franceschini |
|
1554 | 6f695a2e | Manuel Franceschini | The unique_id for the file device is a (file_driver, file_path) tuple.
|
1555 | abdf0113 | Iustin Pop |
|
1556 | 6f695a2e | Manuel Franceschini | """
|
1557 | 6f695a2e | Manuel Franceschini | def __init__(self, unique_id, children): |
1558 | 6f695a2e | Manuel Franceschini | """Initalizes a file device backend.
|
1559 | 6f695a2e | Manuel Franceschini |
|
1560 | 6f695a2e | Manuel Franceschini | """
|
1561 | 6f695a2e | Manuel Franceschini | if children:
|
1562 | 6f695a2e | Manuel Franceschini | raise errors.BlockDeviceError("Invalid setup for file device") |
1563 | 6f695a2e | Manuel Franceschini | super(FileStorage, self).__init__(unique_id, children) |
1564 | 6f695a2e | Manuel Franceschini | if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2: |
1565 | 6f695a2e | Manuel Franceschini | raise ValueError("Invalid configuration data %s" % str(unique_id)) |
1566 | 6f695a2e | Manuel Franceschini | self.driver = unique_id[0] |
1567 | 6f695a2e | Manuel Franceschini | self.dev_path = unique_id[1] |
1568 | ecb091e3 | Iustin Pop | self.Attach()
|
1569 | 6f695a2e | Manuel Franceschini | |
1570 | 6f695a2e | Manuel Franceschini | def Assemble(self): |
1571 | 6f695a2e | Manuel Franceschini | """Assemble the device.
|
1572 | 6f695a2e | Manuel Franceschini |
|
1573 | 6f695a2e | Manuel Franceschini | Checks whether the file device exists, raises BlockDeviceError otherwise.
|
1574 | 6f695a2e | Manuel Franceschini |
|
1575 | 6f695a2e | Manuel Franceschini | """
|
1576 | 6f695a2e | Manuel Franceschini | if not os.path.exists(self.dev_path): |
1577 | 1063abd1 | Iustin Pop | _ThrowError("File device '%s' does not exist" % self.dev_path) |
1578 | 6f695a2e | Manuel Franceschini | |
1579 | 6f695a2e | Manuel Franceschini | def Shutdown(self): |
1580 | 6f695a2e | Manuel Franceschini | """Shutdown the device.
|
1581 | 6f695a2e | Manuel Franceschini |
|
1582 | 6f695a2e | Manuel Franceschini | This is a no-op for the file type, as we don't deacivate
|
1583 | 6f695a2e | Manuel Franceschini | the file on shutdown.
|
1584 | 6f695a2e | Manuel Franceschini |
|
1585 | 6f695a2e | Manuel Franceschini | """
|
1586 | 746f7476 | Iustin Pop | pass
|
1587 | 6f695a2e | Manuel Franceschini | |
1588 | 6f695a2e | Manuel Franceschini | def Open(self, force=False): |
1589 | 6f695a2e | Manuel Franceschini | """Make the device ready for I/O.
|
1590 | 6f695a2e | Manuel Franceschini |
|
1591 | 6f695a2e | Manuel Franceschini | This is a no-op for the file type.
|
1592 | 6f695a2e | Manuel Franceschini |
|
1593 | 6f695a2e | Manuel Franceschini | """
|
1594 | 6f695a2e | Manuel Franceschini | pass
|
1595 | 6f695a2e | Manuel Franceschini | |
1596 | 6f695a2e | Manuel Franceschini | def Close(self): |
1597 | 6f695a2e | Manuel Franceschini | """Notifies that the device will no longer be used for I/O.
|
1598 | 6f695a2e | Manuel Franceschini |
|
1599 | 6f695a2e | Manuel Franceschini | This is a no-op for the file type.
|
1600 | 6f695a2e | Manuel Franceschini |
|
1601 | 6f695a2e | Manuel Franceschini | """
|
1602 | 6f695a2e | Manuel Franceschini | pass
|
1603 | 6f695a2e | Manuel Franceschini | |
1604 | 6f695a2e | Manuel Franceschini | def Remove(self): |
1605 | 6f695a2e | Manuel Franceschini | """Remove the file backing the block device.
|
1606 | 6f695a2e | Manuel Franceschini |
|
1607 | c41eea6e | Iustin Pop | @rtype: boolean
|
1608 | c41eea6e | Iustin Pop | @return: True if the removal was successful
|
1609 | 6f695a2e | Manuel Franceschini |
|
1610 | 6f695a2e | Manuel Franceschini | """
|
1611 | 6f695a2e | Manuel Franceschini | try:
|
1612 | 6f695a2e | Manuel Franceschini | os.remove(self.dev_path)
|
1613 | 6f695a2e | Manuel Franceschini | except OSError, err: |
1614 | 0c6c04ec | Iustin Pop | if err.errno != errno.ENOENT:
|
1615 | 0c6c04ec | Iustin Pop | _ThrowError("Can't remove file '%s': %s", self.dev_path, err) |
1616 | 6f695a2e | Manuel Franceschini | |
1617 | 6f695a2e | Manuel Franceschini | def Attach(self): |
1618 | 6f695a2e | Manuel Franceschini | """Attach to an existing file.
|
1619 | 6f695a2e | Manuel Franceschini |
|
1620 | 6f695a2e | Manuel Franceschini | Check if this file already exists.
|
1621 | 6f695a2e | Manuel Franceschini |
|
1622 | c41eea6e | Iustin Pop | @rtype: boolean
|
1623 | c41eea6e | Iustin Pop | @return: True if file exists
|
1624 | 6f695a2e | Manuel Franceschini |
|
1625 | 6f695a2e | Manuel Franceschini | """
|
1626 | ecb091e3 | Iustin Pop | self.attached = os.path.exists(self.dev_path) |
1627 | ecb091e3 | Iustin Pop | return self.attached |
1628 | 6f695a2e | Manuel Franceschini | |
1629 | 6f695a2e | Manuel Franceschini | @classmethod
|
1630 | 6f695a2e | Manuel Franceschini | def Create(cls, unique_id, children, size): |
1631 | 6f695a2e | Manuel Franceschini | """Create a new file.
|
1632 | 6f695a2e | Manuel Franceschini |
|
1633 | c41eea6e | Iustin Pop | @param size: the size of file in MiB
|
1634 | 6f695a2e | Manuel Franceschini |
|
1635 | c41eea6e | Iustin Pop | @rtype: L{bdev.FileStorage}
|
1636 | c41eea6e | Iustin Pop | @return: an instance of FileStorage
|
1637 | 6f695a2e | Manuel Franceschini |
|
1638 | 6f695a2e | Manuel Franceschini | """
|
1639 | 6f695a2e | Manuel Franceschini | if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2: |
1640 | 6f695a2e | Manuel Franceschini | raise ValueError("Invalid configuration data %s" % str(unique_id)) |
1641 | 6f695a2e | Manuel Franceschini | dev_path = unique_id[1]
|
1642 | aed77cea | Guido Trotter | if os.path.exists(dev_path):
|
1643 | aed77cea | Guido Trotter | _ThrowError("File already existing: %s", dev_path)
|
1644 | 6f695a2e | Manuel Franceschini | try:
|
1645 | 6f695a2e | Manuel Franceschini | f = open(dev_path, 'w') |
1646 | 6f695a2e | Manuel Franceschini | f.truncate(size * 1024 * 1024) |
1647 | 6f695a2e | Manuel Franceschini | f.close() |
1648 | 6c626518 | Iustin Pop | except IOError, err: |
1649 | 82463074 | Iustin Pop | _ThrowError("Error in file creation: %", str(err)) |
1650 | 6f695a2e | Manuel Franceschini | |
1651 | 6f695a2e | Manuel Franceschini | return FileStorage(unique_id, children)
|
1652 | 6f695a2e | Manuel Franceschini | |
1653 | 6f695a2e | Manuel Franceschini | |
1654 | a8083063 | Iustin Pop | DEV_MAP = { |
1655 | fe96220b | Iustin Pop | constants.LD_LV: LogicalVolume, |
1656 | a1f445d3 | Iustin Pop | constants.LD_DRBD8: DRBD8, |
1657 | 6f695a2e | Manuel Franceschini | constants.LD_FILE: FileStorage, |
1658 | a8083063 | Iustin Pop | } |
1659 | a8083063 | Iustin Pop | |
1660 | a8083063 | Iustin Pop | |
1661 | a8083063 | Iustin Pop | def FindDevice(dev_type, unique_id, children): |
1662 | a8083063 | Iustin Pop | """Search for an existing, assembled device.
|
1663 | a8083063 | Iustin Pop |
|
1664 | a8083063 | Iustin Pop | This will succeed only if the device exists and is assembled, but it
|
1665 | a8083063 | Iustin Pop | does not do any actions in order to activate the device.
|
1666 | a8083063 | Iustin Pop |
|
1667 | a8083063 | Iustin Pop | """
|
1668 | a8083063 | Iustin Pop | if dev_type not in DEV_MAP: |
1669 | a8083063 | Iustin Pop | raise errors.ProgrammerError("Invalid block device type '%s'" % dev_type) |
1670 | a8083063 | Iustin Pop | device = DEV_MAP[dev_type](unique_id, children) |
1671 | cb999543 | Iustin Pop | if not device.attached: |
1672 | a8083063 | Iustin Pop | return None |
1673 | ecb091e3 | Iustin Pop | return device
|
1674 | a8083063 | Iustin Pop | |
1675 | a8083063 | Iustin Pop | |
1676 | f96e3c4f | Iustin Pop | def Assemble(dev_type, unique_id, children): |
1677 | a8083063 | Iustin Pop | """Try to attach or assemble an existing device.
|
1678 | a8083063 | Iustin Pop |
|
1679 | f96e3c4f | Iustin Pop | This will attach to assemble the device, as needed, to bring it
|
1680 | f96e3c4f | Iustin Pop | fully up. It must be safe to run on already-assembled devices.
|
1681 | a8083063 | Iustin Pop |
|
1682 | a8083063 | Iustin Pop | """
|
1683 | a8083063 | Iustin Pop | if dev_type not in DEV_MAP: |
1684 | a8083063 | Iustin Pop | raise errors.ProgrammerError("Invalid block device type '%s'" % dev_type) |
1685 | a8083063 | Iustin Pop | device = DEV_MAP[dev_type](unique_id, children) |
1686 | 1063abd1 | Iustin Pop | device.Assemble() |
1687 | a8083063 | Iustin Pop | return device
|
1688 | a8083063 | Iustin Pop | |
1689 | a8083063 | Iustin Pop | |
1690 | a8083063 | Iustin Pop | def Create(dev_type, unique_id, children, size): |
1691 | a8083063 | Iustin Pop | """Create a device.
|
1692 | a8083063 | Iustin Pop |
|
1693 | a8083063 | Iustin Pop | """
|
1694 | a8083063 | Iustin Pop | if dev_type not in DEV_MAP: |
1695 | a8083063 | Iustin Pop | raise errors.ProgrammerError("Invalid block device type '%s'" % dev_type) |
1696 | a8083063 | Iustin Pop | device = DEV_MAP[dev_type].Create(unique_id, children, size) |
1697 | a8083063 | Iustin Pop | return device |