root / lib / block / drbd.py @ 27c7d9c3
History | View | Annotate | Download (36.2 kB)
1 | 89ff748d | Thomas Thrainer | #
|
---|---|---|---|
2 | 89ff748d | Thomas Thrainer | #
|
3 | 89ff748d | Thomas Thrainer | |
4 | 89ff748d | Thomas Thrainer | # Copyright (C) 2006, 2007, 2010, 2011, 2012, 2013 Google Inc.
|
5 | 89ff748d | Thomas Thrainer | #
|
6 | 89ff748d | Thomas Thrainer | # This program is free software; you can redistribute it and/or modify
|
7 | 89ff748d | Thomas Thrainer | # it under the terms of the GNU General Public License as published by
|
8 | 89ff748d | Thomas Thrainer | # the Free Software Foundation; either version 2 of the License, or
|
9 | 89ff748d | Thomas Thrainer | # (at your option) any later version.
|
10 | 89ff748d | Thomas Thrainer | #
|
11 | 89ff748d | Thomas Thrainer | # This program is distributed in the hope that it will be useful, but
|
12 | 89ff748d | Thomas Thrainer | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 89ff748d | Thomas Thrainer | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 89ff748d | Thomas Thrainer | # General Public License for more details.
|
15 | 89ff748d | Thomas Thrainer | #
|
16 | 89ff748d | Thomas Thrainer | # You should have received a copy of the GNU General Public License
|
17 | 89ff748d | Thomas Thrainer | # along with this program; if not, write to the Free Software
|
18 | 89ff748d | Thomas Thrainer | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 89ff748d | Thomas Thrainer | # 02110-1301, USA.
|
20 | 89ff748d | Thomas Thrainer | |
21 | 89ff748d | Thomas Thrainer | |
22 | 89ff748d | Thomas Thrainer | """DRBD block device related functionality"""
|
23 | 89ff748d | Thomas Thrainer | |
24 | 89ff748d | Thomas Thrainer | import errno |
25 | 89ff748d | Thomas Thrainer | import logging |
26 | 89ff748d | Thomas Thrainer | import shlex |
27 | 89ff748d | Thomas Thrainer | import time |
28 | 89ff748d | Thomas Thrainer | |
29 | 89ff748d | Thomas Thrainer | from ganeti import constants |
30 | 89ff748d | Thomas Thrainer | from ganeti import utils |
31 | 89ff748d | Thomas Thrainer | from ganeti import errors |
32 | 89ff748d | Thomas Thrainer | from ganeti import netutils |
33 | 89ff748d | Thomas Thrainer | from ganeti import objects |
34 | 89ff748d | Thomas Thrainer | from ganeti.block import base |
35 | 27d69b25 | Thomas Thrainer | from ganeti.block.drbd_info import DRBD8Info |
36 | 5520d04d | Thomas Thrainer | from ganeti.block.drbd_info import DRBD83ShowInfo |
37 | 27c7d9c3 | Thomas Thrainer | from ganeti.block.drbd_info import DRBD84ShowInfo |
38 | 89ff748d | Thomas Thrainer | |
39 | 89ff748d | Thomas Thrainer | |
40 | 89ff748d | Thomas Thrainer | # Size of reads in _CanReadDevice
|
41 | 89ff748d | Thomas Thrainer | |
42 | 89ff748d | Thomas Thrainer | _DEVICE_READ_SIZE = 128 * 1024 |
43 | 89ff748d | Thomas Thrainer | |
44 | 89ff748d | Thomas Thrainer | |
45 | fd300bc7 | Thomas Thrainer | class DRBD8(base.BlockDev): |
46 | fd300bc7 | Thomas Thrainer | """DRBD v8.x block device.
|
47 | fd300bc7 | Thomas Thrainer |
|
48 | fd300bc7 | Thomas Thrainer | This implements the local host part of the DRBD device, i.e. it
|
49 | fd300bc7 | Thomas Thrainer | doesn't do anything to the supposed peer. If you need a fully
|
50 | fd300bc7 | Thomas Thrainer | connected DRBD pair, you need to use this class on both hosts.
|
51 | fd300bc7 | Thomas Thrainer |
|
52 | fd300bc7 | Thomas Thrainer | The unique_id for the drbd device is a (local_ip, local_port,
|
53 | fd300bc7 | Thomas Thrainer | remote_ip, remote_port, local_minor, secret) tuple, and it must have
|
54 | fd300bc7 | Thomas Thrainer | two children: the data device and the meta_device. The meta device
|
55 | fd300bc7 | Thomas Thrainer | is checked for valid size and is zeroed on create.
|
56 | 89ff748d | Thomas Thrainer |
|
57 | 89ff748d | Thomas Thrainer | """
|
58 | 89ff748d | Thomas Thrainer | _DRBD_MAJOR = 147
|
59 | 89ff748d | Thomas Thrainer | |
60 | 89ff748d | Thomas Thrainer | _USERMODE_HELPER_FILE = "/sys/module/drbd/parameters/usermode_helper"
|
61 | 89ff748d | Thomas Thrainer | |
62 | fd300bc7 | Thomas Thrainer | _MAX_MINORS = 255
|
63 | fd300bc7 | Thomas Thrainer | |
64 | fd300bc7 | Thomas Thrainer | # timeout constants
|
65 | fd300bc7 | Thomas Thrainer | _NET_RECONFIG_TIMEOUT = 60
|
66 | fd300bc7 | Thomas Thrainer | |
67 | fd300bc7 | Thomas Thrainer | # command line options for barriers
|
68 | fd300bc7 | Thomas Thrainer | _DISABLE_DISK_OPTION = "--no-disk-barrier" # -a |
69 | fd300bc7 | Thomas Thrainer | _DISABLE_DRAIN_OPTION = "--no-disk-drain" # -D |
70 | fd300bc7 | Thomas Thrainer | _DISABLE_FLUSH_OPTION = "--no-disk-flushes" # -i |
71 | fd300bc7 | Thomas Thrainer | _DISABLE_META_FLUSH_OPTION = "--no-md-flushes" # -m |
72 | fd300bc7 | Thomas Thrainer | |
73 | fd300bc7 | Thomas Thrainer | def __init__(self, unique_id, children, size, params): |
74 | fd300bc7 | Thomas Thrainer | if children and children.count(None) > 0: |
75 | fd300bc7 | Thomas Thrainer | children = [] |
76 | fd300bc7 | Thomas Thrainer | if len(children) not in (0, 2): |
77 | fd300bc7 | Thomas Thrainer | raise ValueError("Invalid configuration data %s" % str(children)) |
78 | fd300bc7 | Thomas Thrainer | if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 6: |
79 | fd300bc7 | Thomas Thrainer | raise ValueError("Invalid configuration data %s" % str(unique_id)) |
80 | fd300bc7 | Thomas Thrainer | (self._lhost, self._lport, |
81 | fd300bc7 | Thomas Thrainer | self._rhost, self._rport, |
82 | fd300bc7 | Thomas Thrainer | self._aminor, self._secret) = unique_id |
83 | fd300bc7 | Thomas Thrainer | if children:
|
84 | fd300bc7 | Thomas Thrainer | if not _CanReadDevice(children[1].dev_path): |
85 | fd300bc7 | Thomas Thrainer | logging.info("drbd%s: Ignoring unreadable meta device", self._aminor) |
86 | fd300bc7 | Thomas Thrainer | children = [] |
87 | fd300bc7 | Thomas Thrainer | super(DRBD8, self).__init__(unique_id, children, size, params) |
88 | fd300bc7 | Thomas Thrainer | self.major = self._DRBD_MAJOR |
89 | 2fe690f1 | Thomas Thrainer | |
90 | 2fe690f1 | Thomas Thrainer | drbd_info = DRBD8Info.CreateFromFile() |
91 | 2fe690f1 | Thomas Thrainer | version = drbd_info.GetVersion() |
92 | fd300bc7 | Thomas Thrainer | if version["k_major"] != 8: |
93 | fd300bc7 | Thomas Thrainer | base.ThrowError("Mismatch in DRBD kernel version and requested ganeti"
|
94 | fd300bc7 | Thomas Thrainer | " usage: kernel is %s.%s, ganeti wants 8.x",
|
95 | fd300bc7 | Thomas Thrainer | version["k_major"], version["k_minor"]) |
96 | fd300bc7 | Thomas Thrainer | |
97 | 27c7d9c3 | Thomas Thrainer | if version["k_minor"] <= 3: |
98 | 27c7d9c3 | Thomas Thrainer | self._show_info_cls = DRBD83ShowInfo
|
99 | 27c7d9c3 | Thomas Thrainer | else:
|
100 | 27c7d9c3 | Thomas Thrainer | self._show_info_cls = DRBD84ShowInfo
|
101 | 27c7d9c3 | Thomas Thrainer | |
102 | fd300bc7 | Thomas Thrainer | if (self._lhost is not None and self._lhost == self._rhost and |
103 | fd300bc7 | Thomas Thrainer | self._lport == self._rport): |
104 | fd300bc7 | Thomas Thrainer | raise ValueError("Invalid configuration data, same local/remote %s" % |
105 | fd300bc7 | Thomas Thrainer | (unique_id,)) |
106 | fd300bc7 | Thomas Thrainer | self.Attach()
|
107 | fd300bc7 | Thomas Thrainer | |
108 | 89ff748d | Thomas Thrainer | @staticmethod
|
109 | 89ff748d | Thomas Thrainer | def GetUsermodeHelper(filename=_USERMODE_HELPER_FILE): |
110 | 89ff748d | Thomas Thrainer | """Returns DRBD usermode_helper currently set.
|
111 | 89ff748d | Thomas Thrainer |
|
112 | 89ff748d | Thomas Thrainer | """
|
113 | 89ff748d | Thomas Thrainer | try:
|
114 | 89ff748d | Thomas Thrainer | helper = utils.ReadFile(filename).splitlines()[0]
|
115 | 89ff748d | Thomas Thrainer | except EnvironmentError, err: |
116 | 89ff748d | Thomas Thrainer | if err.errno == errno.ENOENT:
|
117 | 89ff748d | Thomas Thrainer | base.ThrowError("The file %s cannot be opened, check if the module"
|
118 | 89ff748d | Thomas Thrainer | " is loaded (%s)", filename, str(err)) |
119 | 89ff748d | Thomas Thrainer | else:
|
120 | 89ff748d | Thomas Thrainer | base.ThrowError("Can't read DRBD helper file %s: %s",
|
121 | 89ff748d | Thomas Thrainer | filename, str(err))
|
122 | 89ff748d | Thomas Thrainer | if not helper: |
123 | 89ff748d | Thomas Thrainer | base.ThrowError("Can't read any data from %s", filename)
|
124 | 89ff748d | Thomas Thrainer | return helper
|
125 | 89ff748d | Thomas Thrainer | |
126 | 89ff748d | Thomas Thrainer | @staticmethod
|
127 | 89ff748d | Thomas Thrainer | def _DevPath(minor): |
128 | 89ff748d | Thomas Thrainer | """Return the path to a drbd device for a given minor.
|
129 | 89ff748d | Thomas Thrainer |
|
130 | 89ff748d | Thomas Thrainer | """
|
131 | 89ff748d | Thomas Thrainer | return "/dev/drbd%d" % minor |
132 | 89ff748d | Thomas Thrainer | |
133 | 89ff748d | Thomas Thrainer | @classmethod
|
134 | 89ff748d | Thomas Thrainer | def GetUsedDevs(cls): |
135 | 89ff748d | Thomas Thrainer | """Compute the list of used DRBD devices.
|
136 | 89ff748d | Thomas Thrainer |
|
137 | 89ff748d | Thomas Thrainer | """
|
138 | 2fe690f1 | Thomas Thrainer | drbd_info = DRBD8Info.CreateFromFile() |
139 | 2fe690f1 | Thomas Thrainer | return filter(lambda m: not drbd_info.GetMinorStatus(m).is_unconfigured, |
140 | 2fe690f1 | Thomas Thrainer | drbd_info.GetMinors()) |
141 | 89ff748d | Thomas Thrainer | |
142 | 89ff748d | Thomas Thrainer | def _SetFromMinor(self, minor): |
143 | 89ff748d | Thomas Thrainer | """Set our parameters based on the given minor.
|
144 | 89ff748d | Thomas Thrainer |
|
145 | 89ff748d | Thomas Thrainer | This sets our minor variable and our dev_path.
|
146 | 89ff748d | Thomas Thrainer |
|
147 | 89ff748d | Thomas Thrainer | """
|
148 | 89ff748d | Thomas Thrainer | if minor is None: |
149 | 89ff748d | Thomas Thrainer | self.minor = self.dev_path = None |
150 | 89ff748d | Thomas Thrainer | self.attached = False |
151 | 89ff748d | Thomas Thrainer | else:
|
152 | 89ff748d | Thomas Thrainer | self.minor = minor
|
153 | 89ff748d | Thomas Thrainer | self.dev_path = self._DevPath(minor) |
154 | 89ff748d | Thomas Thrainer | self.attached = True |
155 | 89ff748d | Thomas Thrainer | |
156 | 89ff748d | Thomas Thrainer | @staticmethod
|
157 | 89ff748d | Thomas Thrainer | def _CheckMetaSize(meta_device): |
158 | 89ff748d | Thomas Thrainer | """Check if the given meta device looks like a valid one.
|
159 | 89ff748d | Thomas Thrainer |
|
160 | 89ff748d | Thomas Thrainer | This currently only checks the size, which must be around
|
161 | 89ff748d | Thomas Thrainer | 128MiB.
|
162 | 89ff748d | Thomas Thrainer |
|
163 | 89ff748d | Thomas Thrainer | """
|
164 | 89ff748d | Thomas Thrainer | result = utils.RunCmd(["blockdev", "--getsize", meta_device]) |
165 | 89ff748d | Thomas Thrainer | if result.failed:
|
166 | 89ff748d | Thomas Thrainer | base.ThrowError("Failed to get device size: %s - %s",
|
167 | 89ff748d | Thomas Thrainer | result.fail_reason, result.output) |
168 | 89ff748d | Thomas Thrainer | try:
|
169 | 89ff748d | Thomas Thrainer | sectors = int(result.stdout)
|
170 | 89ff748d | Thomas Thrainer | except (TypeError, ValueError): |
171 | 89ff748d | Thomas Thrainer | base.ThrowError("Invalid output from blockdev: '%s'", result.stdout)
|
172 | 89ff748d | Thomas Thrainer | num_bytes = sectors * 512
|
173 | 89ff748d | Thomas Thrainer | if num_bytes < 128 * 1024 * 1024: # less than 128MiB |
174 | 89ff748d | Thomas Thrainer | base.ThrowError("Meta device too small (%.2fMib)",
|
175 | 89ff748d | Thomas Thrainer | (num_bytes / 1024 / 1024)) |
176 | 89ff748d | Thomas Thrainer | # the maximum *valid* size of the meta device when living on top
|
177 | 89ff748d | Thomas Thrainer | # of LVM is hard to compute: it depends on the number of stripes
|
178 | 89ff748d | Thomas Thrainer | # and the PE size; e.g. a 2-stripe, 64MB PE will result in a 128MB
|
179 | 89ff748d | Thomas Thrainer | # (normal size), but an eight-stripe 128MB PE will result in a 1GB
|
180 | 89ff748d | Thomas Thrainer | # size meta device; as such, we restrict it to 1GB (a little bit
|
181 | 89ff748d | Thomas Thrainer | # too generous, but making assumptions about PE size is hard)
|
182 | 89ff748d | Thomas Thrainer | if num_bytes > 1024 * 1024 * 1024: |
183 | 89ff748d | Thomas Thrainer | base.ThrowError("Meta device too big (%.2fMiB)",
|
184 | 89ff748d | Thomas Thrainer | (num_bytes / 1024 / 1024)) |
185 | 89ff748d | Thomas Thrainer | |
186 | 89ff748d | Thomas Thrainer | @classmethod
|
187 | 89ff748d | Thomas Thrainer | def _InitMeta(cls, minor, dev_path): |
188 | 89ff748d | Thomas Thrainer | """Initialize a meta device.
|
189 | 89ff748d | Thomas Thrainer |
|
190 | 89ff748d | Thomas Thrainer | This will not work if the given minor is in use.
|
191 | 89ff748d | Thomas Thrainer |
|
192 | 89ff748d | Thomas Thrainer | """
|
193 | 89ff748d | Thomas Thrainer | # Zero the metadata first, in order to make sure drbdmeta doesn't
|
194 | 89ff748d | Thomas Thrainer | # try to auto-detect existing filesystems or similar (see
|
195 | 89ff748d | Thomas Thrainer | # http://code.google.com/p/ganeti/issues/detail?id=182); we only
|
196 | 89ff748d | Thomas Thrainer | # care about the first 128MB of data in the device, even though it
|
197 | 89ff748d | Thomas Thrainer | # can be bigger
|
198 | 89ff748d | Thomas Thrainer | result = utils.RunCmd([constants.DD_CMD, |
199 | 89ff748d | Thomas Thrainer | "if=/dev/zero", "of=%s" % dev_path, |
200 | 89ff748d | Thomas Thrainer | "bs=1048576", "count=128", "oflag=direct"]) |
201 | 89ff748d | Thomas Thrainer | if result.failed:
|
202 | 89ff748d | Thomas Thrainer | base.ThrowError("Can't wipe the meta device: %s", result.output)
|
203 | 89ff748d | Thomas Thrainer | |
204 | 89ff748d | Thomas Thrainer | result = utils.RunCmd(["drbdmeta", "--force", cls._DevPath(minor), |
205 | 89ff748d | Thomas Thrainer | "v08", dev_path, "0", "create-md"]) |
206 | 89ff748d | Thomas Thrainer | if result.failed:
|
207 | 89ff748d | Thomas Thrainer | base.ThrowError("Can't initialize meta device: %s", result.output)
|
208 | 89ff748d | Thomas Thrainer | |
209 | 2fe690f1 | Thomas Thrainer | def _FindUnusedMinor(self): |
210 | 89ff748d | Thomas Thrainer | """Find an unused DRBD device.
|
211 | 89ff748d | Thomas Thrainer |
|
212 | 89ff748d | Thomas Thrainer | This is specific to 8.x as the minors are allocated dynamically,
|
213 | 89ff748d | Thomas Thrainer | so non-existing numbers up to a max minor count are actually free.
|
214 | 89ff748d | Thomas Thrainer |
|
215 | 89ff748d | Thomas Thrainer | """
|
216 | 89ff748d | Thomas Thrainer | |
217 | 89ff748d | Thomas Thrainer | highest = None
|
218 | 2fe690f1 | Thomas Thrainer | drbd_info = DRBD8Info.CreateFromFile() |
219 | 2fe690f1 | Thomas Thrainer | for minor in drbd_info.GetMinors(): |
220 | 2fe690f1 | Thomas Thrainer | status = drbd_info.GetMinorStatus(minor) |
221 | 2fe690f1 | Thomas Thrainer | if not status.is_in_use: |
222 | 2fe690f1 | Thomas Thrainer | return minor
|
223 | 2fe690f1 | Thomas Thrainer | highest = max(highest, minor)
|
224 | 2fe690f1 | Thomas Thrainer | |
225 | 89ff748d | Thomas Thrainer | if highest is None: # there are no minors in use at all |
226 | 89ff748d | Thomas Thrainer | return 0 |
227 | 2fe690f1 | Thomas Thrainer | if highest >= self._MAX_MINORS: |
228 | 89ff748d | Thomas Thrainer | logging.error("Error: no free drbd minors!")
|
229 | 89ff748d | Thomas Thrainer | raise errors.BlockDeviceError("Can't find a free DRBD minor") |
230 | 2fe690f1 | Thomas Thrainer | |
231 | 89ff748d | Thomas Thrainer | return highest + 1 |
232 | 89ff748d | Thomas Thrainer | |
233 | 89ff748d | Thomas Thrainer | @classmethod
|
234 | 89ff748d | Thomas Thrainer | def _GetShowData(cls, minor): |
235 | 89ff748d | Thomas Thrainer | """Return the `drbdsetup show` data for a minor.
|
236 | 89ff748d | Thomas Thrainer |
|
237 | 89ff748d | Thomas Thrainer | """
|
238 | 89ff748d | Thomas Thrainer | result = utils.RunCmd(["drbdsetup", cls._DevPath(minor), "show"]) |
239 | 89ff748d | Thomas Thrainer | if result.failed:
|
240 | 89ff748d | Thomas Thrainer | logging.error("Can't display the drbd config: %s - %s",
|
241 | 89ff748d | Thomas Thrainer | result.fail_reason, result.output) |
242 | 89ff748d | Thomas Thrainer | return None |
243 | 89ff748d | Thomas Thrainer | return result.stdout
|
244 | 89ff748d | Thomas Thrainer | |
245 | 27c7d9c3 | Thomas Thrainer | def _GetShowInfo(self, minor): |
246 | 27c7d9c3 | Thomas Thrainer | return self._show_info_cls.GetDevInfo(self._GetShowData(minor)) |
247 | 27c7d9c3 | Thomas Thrainer | |
248 | 89ff748d | Thomas Thrainer | def _MatchesLocal(self, info): |
249 | 89ff748d | Thomas Thrainer | """Test if our local config matches with an existing device.
|
250 | 89ff748d | Thomas Thrainer |
|
251 | 89ff748d | Thomas Thrainer | The parameter should be as returned from `_GetDevInfo()`. This
|
252 | 89ff748d | Thomas Thrainer | method tests if our local backing device is the same as the one in
|
253 | 89ff748d | Thomas Thrainer | the info parameter, in effect testing if we look like the given
|
254 | 89ff748d | Thomas Thrainer | device.
|
255 | 89ff748d | Thomas Thrainer |
|
256 | 89ff748d | Thomas Thrainer | """
|
257 | 89ff748d | Thomas Thrainer | if self._children: |
258 | 89ff748d | Thomas Thrainer | backend, meta = self._children
|
259 | 89ff748d | Thomas Thrainer | else:
|
260 | 89ff748d | Thomas Thrainer | backend = meta = None
|
261 | 89ff748d | Thomas Thrainer | |
262 | 89ff748d | Thomas Thrainer | if backend is not None: |
263 | 89ff748d | Thomas Thrainer | retval = ("local_dev" in info and info["local_dev"] == backend.dev_path) |
264 | 89ff748d | Thomas Thrainer | else:
|
265 | 89ff748d | Thomas Thrainer | retval = ("local_dev" not in info) |
266 | 89ff748d | Thomas Thrainer | |
267 | 89ff748d | Thomas Thrainer | if meta is not None: |
268 | 89ff748d | Thomas Thrainer | retval = retval and ("meta_dev" in info and |
269 | 89ff748d | Thomas Thrainer | info["meta_dev"] == meta.dev_path)
|
270 | 89ff748d | Thomas Thrainer | retval = retval and ("meta_index" in info and |
271 | 89ff748d | Thomas Thrainer | info["meta_index"] == 0) |
272 | 89ff748d | Thomas Thrainer | else:
|
273 | 89ff748d | Thomas Thrainer | retval = retval and ("meta_dev" not in info and |
274 | 89ff748d | Thomas Thrainer | "meta_index" not in info) |
275 | 89ff748d | Thomas Thrainer | return retval
|
276 | 89ff748d | Thomas Thrainer | |
277 | 89ff748d | Thomas Thrainer | def _MatchesNet(self, info): |
278 | 89ff748d | Thomas Thrainer | """Test if our network config matches with an existing device.
|
279 | 89ff748d | Thomas Thrainer |
|
280 | 89ff748d | Thomas Thrainer | The parameter should be as returned from `_GetDevInfo()`. This
|
281 | 89ff748d | Thomas Thrainer | method tests if our network configuration is the same as the one
|
282 | 89ff748d | Thomas Thrainer | in the info parameter, in effect testing if we look like the given
|
283 | 89ff748d | Thomas Thrainer | device.
|
284 | 89ff748d | Thomas Thrainer |
|
285 | 89ff748d | Thomas Thrainer | """
|
286 | 89ff748d | Thomas Thrainer | if (((self._lhost is None and not ("local_addr" in info)) and |
287 | 89ff748d | Thomas Thrainer | (self._rhost is None and not ("remote_addr" in info)))): |
288 | 89ff748d | Thomas Thrainer | return True |
289 | 89ff748d | Thomas Thrainer | |
290 | 89ff748d | Thomas Thrainer | if self._lhost is None: |
291 | 89ff748d | Thomas Thrainer | return False |
292 | 89ff748d | Thomas Thrainer | |
293 | 89ff748d | Thomas Thrainer | if not ("local_addr" in info and |
294 | 89ff748d | Thomas Thrainer | "remote_addr" in info): |
295 | 89ff748d | Thomas Thrainer | return False |
296 | 89ff748d | Thomas Thrainer | |
297 | 89ff748d | Thomas Thrainer | retval = (info["local_addr"] == (self._lhost, self._lport)) |
298 | 89ff748d | Thomas Thrainer | retval = (retval and
|
299 | 89ff748d | Thomas Thrainer | info["remote_addr"] == (self._rhost, self._rport)) |
300 | 89ff748d | Thomas Thrainer | return retval
|
301 | 89ff748d | Thomas Thrainer | |
302 | 89ff748d | Thomas Thrainer | def _AssembleLocal(self, minor, backend, meta, size): |
303 | 89ff748d | Thomas Thrainer | """Configure the local part of a DRBD device.
|
304 | 89ff748d | Thomas Thrainer |
|
305 | 89ff748d | Thomas Thrainer | """
|
306 | 89ff748d | Thomas Thrainer | args = ["drbdsetup", self._DevPath(minor), "disk", |
307 | 89ff748d | Thomas Thrainer | backend, meta, "0",
|
308 | 89ff748d | Thomas Thrainer | "-e", "detach", |
309 | 89ff748d | Thomas Thrainer | "--create-device"]
|
310 | 89ff748d | Thomas Thrainer | if size:
|
311 | 89ff748d | Thomas Thrainer | args.extend(["-d", "%sm" % size]) |
312 | 89ff748d | Thomas Thrainer | |
313 | 2fe690f1 | Thomas Thrainer | drbd_info = DRBD8Info.CreateFromFile() |
314 | 2fe690f1 | Thomas Thrainer | version = drbd_info.GetVersion() |
315 | 89ff748d | Thomas Thrainer | vmaj = version["k_major"]
|
316 | 89ff748d | Thomas Thrainer | vmin = version["k_minor"]
|
317 | 89ff748d | Thomas Thrainer | vrel = version["k_point"]
|
318 | 89ff748d | Thomas Thrainer | |
319 | 89ff748d | Thomas Thrainer | barrier_args = \ |
320 | 89ff748d | Thomas Thrainer | self._ComputeDiskBarrierArgs(vmaj, vmin, vrel,
|
321 | 89ff748d | Thomas Thrainer | self.params[constants.LDP_BARRIERS],
|
322 | 89ff748d | Thomas Thrainer | self.params[constants.LDP_NO_META_FLUSH])
|
323 | 89ff748d | Thomas Thrainer | args.extend(barrier_args) |
324 | 89ff748d | Thomas Thrainer | |
325 | 89ff748d | Thomas Thrainer | if self.params[constants.LDP_DISK_CUSTOM]: |
326 | 89ff748d | Thomas Thrainer | args.extend(shlex.split(self.params[constants.LDP_DISK_CUSTOM]))
|
327 | 89ff748d | Thomas Thrainer | |
328 | 89ff748d | Thomas Thrainer | result = utils.RunCmd(args) |
329 | 89ff748d | Thomas Thrainer | if result.failed:
|
330 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't attach local disk: %s",
|
331 | 89ff748d | Thomas Thrainer | minor, result.output) |
332 | 89ff748d | Thomas Thrainer | |
333 | 89ff748d | Thomas Thrainer | @classmethod
|
334 | 89ff748d | Thomas Thrainer | def _ComputeDiskBarrierArgs(cls, vmaj, vmin, vrel, disabled_barriers, |
335 | 89ff748d | Thomas Thrainer | disable_meta_flush): |
336 | 89ff748d | Thomas Thrainer | """Compute the DRBD command line parameters for disk barriers
|
337 | 89ff748d | Thomas Thrainer |
|
338 | 89ff748d | Thomas Thrainer | Returns a list of the disk barrier parameters as requested via the
|
339 | 89ff748d | Thomas Thrainer | disabled_barriers and disable_meta_flush arguments, and according to the
|
340 | 89ff748d | Thomas Thrainer | supported ones in the DRBD version vmaj.vmin.vrel
|
341 | 89ff748d | Thomas Thrainer |
|
342 | 89ff748d | Thomas Thrainer | If the desired option is unsupported, raises errors.BlockDeviceError.
|
343 | 89ff748d | Thomas Thrainer |
|
344 | 89ff748d | Thomas Thrainer | """
|
345 | 89ff748d | Thomas Thrainer | disabled_barriers_set = frozenset(disabled_barriers)
|
346 | 89ff748d | Thomas Thrainer | if not disabled_barriers_set in constants.DRBD_VALID_BARRIER_OPT: |
347 | 89ff748d | Thomas Thrainer | raise errors.BlockDeviceError("%s is not a valid option set for DRBD" |
348 | 89ff748d | Thomas Thrainer | " barriers" % disabled_barriers)
|
349 | 89ff748d | Thomas Thrainer | |
350 | 89ff748d | Thomas Thrainer | args = [] |
351 | 89ff748d | Thomas Thrainer | |
352 | 89ff748d | Thomas Thrainer | # The following code assumes DRBD 8.x, with x < 4 and x != 1 (DRBD 8.1.x
|
353 | 89ff748d | Thomas Thrainer | # does not exist)
|
354 | 89ff748d | Thomas Thrainer | if not vmaj == 8 and vmin in (0, 2, 3): |
355 | 89ff748d | Thomas Thrainer | raise errors.BlockDeviceError("Unsupported DRBD version: %d.%d.%d" % |
356 | 89ff748d | Thomas Thrainer | (vmaj, vmin, vrel)) |
357 | 89ff748d | Thomas Thrainer | |
358 | 89ff748d | Thomas Thrainer | def _AppendOrRaise(option, min_version): |
359 | 89ff748d | Thomas Thrainer | """Helper for DRBD options"""
|
360 | 89ff748d | Thomas Thrainer | if min_version is not None and vrel >= min_version: |
361 | 89ff748d | Thomas Thrainer | args.append(option) |
362 | 89ff748d | Thomas Thrainer | else:
|
363 | 89ff748d | Thomas Thrainer | raise errors.BlockDeviceError("Could not use the option %s as the" |
364 | 89ff748d | Thomas Thrainer | " DRBD version %d.%d.%d does not support"
|
365 | 89ff748d | Thomas Thrainer | " it." % (option, vmaj, vmin, vrel))
|
366 | 89ff748d | Thomas Thrainer | |
367 | 89ff748d | Thomas Thrainer | # the minimum version for each feature is encoded via pairs of (minor
|
368 | 89ff748d | Thomas Thrainer | # version -> x) where x is version in which support for the option was
|
369 | 89ff748d | Thomas Thrainer | # introduced.
|
370 | 89ff748d | Thomas Thrainer | meta_flush_supported = disk_flush_supported = { |
371 | 89ff748d | Thomas Thrainer | 0: 12, |
372 | 89ff748d | Thomas Thrainer | 2: 7, |
373 | 89ff748d | Thomas Thrainer | 3: 0, |
374 | 89ff748d | Thomas Thrainer | } |
375 | 89ff748d | Thomas Thrainer | |
376 | 89ff748d | Thomas Thrainer | disk_drain_supported = { |
377 | 89ff748d | Thomas Thrainer | 2: 7, |
378 | 89ff748d | Thomas Thrainer | 3: 0, |
379 | 89ff748d | Thomas Thrainer | } |
380 | 89ff748d | Thomas Thrainer | |
381 | 89ff748d | Thomas Thrainer | disk_barriers_supported = { |
382 | 89ff748d | Thomas Thrainer | 3: 0, |
383 | 89ff748d | Thomas Thrainer | } |
384 | 89ff748d | Thomas Thrainer | |
385 | 89ff748d | Thomas Thrainer | # meta flushes
|
386 | 89ff748d | Thomas Thrainer | if disable_meta_flush:
|
387 | 89ff748d | Thomas Thrainer | _AppendOrRaise(cls._DISABLE_META_FLUSH_OPTION, |
388 | 89ff748d | Thomas Thrainer | meta_flush_supported.get(vmin, None))
|
389 | 89ff748d | Thomas Thrainer | |
390 | 89ff748d | Thomas Thrainer | # disk flushes
|
391 | 89ff748d | Thomas Thrainer | if constants.DRBD_B_DISK_FLUSH in disabled_barriers_set: |
392 | 89ff748d | Thomas Thrainer | _AppendOrRaise(cls._DISABLE_FLUSH_OPTION, |
393 | 89ff748d | Thomas Thrainer | disk_flush_supported.get(vmin, None))
|
394 | 89ff748d | Thomas Thrainer | |
395 | 89ff748d | Thomas Thrainer | # disk drain
|
396 | 89ff748d | Thomas Thrainer | if constants.DRBD_B_DISK_DRAIN in disabled_barriers_set: |
397 | 89ff748d | Thomas Thrainer | _AppendOrRaise(cls._DISABLE_DRAIN_OPTION, |
398 | 89ff748d | Thomas Thrainer | disk_drain_supported.get(vmin, None))
|
399 | 89ff748d | Thomas Thrainer | |
400 | 89ff748d | Thomas Thrainer | # disk barriers
|
401 | 89ff748d | Thomas Thrainer | if constants.DRBD_B_DISK_BARRIERS in disabled_barriers_set: |
402 | 89ff748d | Thomas Thrainer | _AppendOrRaise(cls._DISABLE_DISK_OPTION, |
403 | 89ff748d | Thomas Thrainer | disk_barriers_supported.get(vmin, None))
|
404 | 89ff748d | Thomas Thrainer | |
405 | 89ff748d | Thomas Thrainer | return args
|
406 | 89ff748d | Thomas Thrainer | |
407 | 89ff748d | Thomas Thrainer | def _AssembleNet(self, minor, net_info, protocol, |
408 | 89ff748d | Thomas Thrainer | dual_pri=False, hmac=None, secret=None): |
409 | 89ff748d | Thomas Thrainer | """Configure the network part of the device.
|
410 | 89ff748d | Thomas Thrainer |
|
411 | 89ff748d | Thomas Thrainer | """
|
412 | 89ff748d | Thomas Thrainer | lhost, lport, rhost, rport = net_info |
413 | 89ff748d | Thomas Thrainer | if None in net_info: |
414 | 89ff748d | Thomas Thrainer | # we don't want network connection and actually want to make
|
415 | 89ff748d | Thomas Thrainer | # sure its shutdown
|
416 | 89ff748d | Thomas Thrainer | self._ShutdownNet(minor)
|
417 | 89ff748d | Thomas Thrainer | return
|
418 | 89ff748d | Thomas Thrainer | |
419 | 89ff748d | Thomas Thrainer | # Workaround for a race condition. When DRBD is doing its dance to
|
420 | 89ff748d | Thomas Thrainer | # establish a connection with its peer, it also sends the
|
421 | 89ff748d | Thomas Thrainer | # synchronization speed over the wire. In some cases setting the
|
422 | 89ff748d | Thomas Thrainer | # sync speed only after setting up both sides can race with DRBD
|
423 | 89ff748d | Thomas Thrainer | # connecting, hence we set it here before telling DRBD anything
|
424 | 89ff748d | Thomas Thrainer | # about its peer.
|
425 | 89ff748d | Thomas Thrainer | sync_errors = self._SetMinorSyncParams(minor, self.params) |
426 | 89ff748d | Thomas Thrainer | if sync_errors:
|
427 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't set the synchronization parameters: %s" %
|
428 | 89ff748d | Thomas Thrainer | (minor, utils.CommaJoin(sync_errors))) |
429 | 89ff748d | Thomas Thrainer | |
430 | 89ff748d | Thomas Thrainer | if netutils.IP6Address.IsValid(lhost):
|
431 | 89ff748d | Thomas Thrainer | if not netutils.IP6Address.IsValid(rhost): |
432 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't connect ip %s to ip %s" %
|
433 | 89ff748d | Thomas Thrainer | (minor, lhost, rhost)) |
434 | 89ff748d | Thomas Thrainer | family = "ipv6"
|
435 | 89ff748d | Thomas Thrainer | elif netutils.IP4Address.IsValid(lhost):
|
436 | 89ff748d | Thomas Thrainer | if not netutils.IP4Address.IsValid(rhost): |
437 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't connect ip %s to ip %s" %
|
438 | 89ff748d | Thomas Thrainer | (minor, lhost, rhost)) |
439 | 89ff748d | Thomas Thrainer | family = "ipv4"
|
440 | 89ff748d | Thomas Thrainer | else:
|
441 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: Invalid ip %s" % (minor, lhost))
|
442 | 89ff748d | Thomas Thrainer | |
443 | 89ff748d | Thomas Thrainer | args = ["drbdsetup", self._DevPath(minor), "net", |
444 | 89ff748d | Thomas Thrainer | "%s:%s:%s" % (family, lhost, lport),
|
445 | 89ff748d | Thomas Thrainer | "%s:%s:%s" % (family, rhost, rport), protocol,
|
446 | 89ff748d | Thomas Thrainer | "-A", "discard-zero-changes", |
447 | 89ff748d | Thomas Thrainer | "-B", "consensus", |
448 | 89ff748d | Thomas Thrainer | "--create-device",
|
449 | 89ff748d | Thomas Thrainer | ] |
450 | 89ff748d | Thomas Thrainer | if dual_pri:
|
451 | 89ff748d | Thomas Thrainer | args.append("-m")
|
452 | 89ff748d | Thomas Thrainer | if hmac and secret: |
453 | 89ff748d | Thomas Thrainer | args.extend(["-a", hmac, "-x", secret]) |
454 | 89ff748d | Thomas Thrainer | |
455 | 89ff748d | Thomas Thrainer | if self.params[constants.LDP_NET_CUSTOM]: |
456 | 89ff748d | Thomas Thrainer | args.extend(shlex.split(self.params[constants.LDP_NET_CUSTOM]))
|
457 | 89ff748d | Thomas Thrainer | |
458 | 89ff748d | Thomas Thrainer | result = utils.RunCmd(args) |
459 | 89ff748d | Thomas Thrainer | if result.failed:
|
460 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't setup network: %s - %s",
|
461 | 89ff748d | Thomas Thrainer | minor, result.fail_reason, result.output) |
462 | 89ff748d | Thomas Thrainer | |
463 | 89ff748d | Thomas Thrainer | def _CheckNetworkConfig(): |
464 | 27c7d9c3 | Thomas Thrainer | info = self._GetShowInfo(minor)
|
465 | 89ff748d | Thomas Thrainer | if not "local_addr" in info or not "remote_addr" in info: |
466 | 89ff748d | Thomas Thrainer | raise utils.RetryAgain()
|
467 | 89ff748d | Thomas Thrainer | |
468 | 89ff748d | Thomas Thrainer | if (info["local_addr"] != (lhost, lport) or |
469 | 89ff748d | Thomas Thrainer | info["remote_addr"] != (rhost, rport)):
|
470 | 89ff748d | Thomas Thrainer | raise utils.RetryAgain()
|
471 | 89ff748d | Thomas Thrainer | |
472 | 89ff748d | Thomas Thrainer | try:
|
473 | 89ff748d | Thomas Thrainer | utils.Retry(_CheckNetworkConfig, 1.0, 10.0) |
474 | 89ff748d | Thomas Thrainer | except utils.RetryTimeout:
|
475 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: timeout while configuring network", minor)
|
476 | 89ff748d | Thomas Thrainer | |
477 | 89ff748d | Thomas Thrainer | def AddChildren(self, devices): |
478 | 89ff748d | Thomas Thrainer | """Add a disk to the DRBD device.
|
479 | 89ff748d | Thomas Thrainer |
|
480 | 89ff748d | Thomas Thrainer | """
|
481 | 89ff748d | Thomas Thrainer | if self.minor is None: |
482 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't attach to dbrd8 during AddChildren",
|
483 | 89ff748d | Thomas Thrainer | self._aminor)
|
484 | 89ff748d | Thomas Thrainer | if len(devices) != 2: |
485 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: need two devices for AddChildren", self.minor) |
486 | 27c7d9c3 | Thomas Thrainer | info = self._GetShowInfo(self.minor) |
487 | 89ff748d | Thomas Thrainer | if "local_dev" in info: |
488 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: already attached to a local disk", self.minor) |
489 | 89ff748d | Thomas Thrainer | backend, meta = devices |
490 | 89ff748d | Thomas Thrainer | if backend.dev_path is None or meta.dev_path is None: |
491 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: children not ready during AddChildren",
|
492 | 89ff748d | Thomas Thrainer | self.minor)
|
493 | 89ff748d | Thomas Thrainer | backend.Open() |
494 | 89ff748d | Thomas Thrainer | meta.Open() |
495 | 89ff748d | Thomas Thrainer | self._CheckMetaSize(meta.dev_path)
|
496 | 89ff748d | Thomas Thrainer | self._InitMeta(self._FindUnusedMinor(), meta.dev_path) |
497 | 89ff748d | Thomas Thrainer | |
498 | 89ff748d | Thomas Thrainer | self._AssembleLocal(self.minor, backend.dev_path, meta.dev_path, self.size) |
499 | 89ff748d | Thomas Thrainer | self._children = devices
|
500 | 89ff748d | Thomas Thrainer | |
501 | 89ff748d | Thomas Thrainer | def RemoveChildren(self, devices): |
502 | 89ff748d | Thomas Thrainer | """Detach the drbd device from local storage.
|
503 | 89ff748d | Thomas Thrainer |
|
504 | 89ff748d | Thomas Thrainer | """
|
505 | 89ff748d | Thomas Thrainer | if self.minor is None: |
506 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't attach to drbd8 during RemoveChildren",
|
507 | 89ff748d | Thomas Thrainer | self._aminor)
|
508 | 89ff748d | Thomas Thrainer | # early return if we don't actually have backing storage
|
509 | 27c7d9c3 | Thomas Thrainer | info = self._GetShowInfo(self.minor) |
510 | 89ff748d | Thomas Thrainer | if "local_dev" not in info: |
511 | 89ff748d | Thomas Thrainer | return
|
512 | 89ff748d | Thomas Thrainer | if len(self._children) != 2: |
513 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: we don't have two children: %s", self.minor, |
514 | 89ff748d | Thomas Thrainer | self._children)
|
515 | 89ff748d | Thomas Thrainer | if self._children.count(None) == 2: # we don't actually have children :) |
516 | 89ff748d | Thomas Thrainer | logging.warning("drbd%d: requested detach while detached", self.minor) |
517 | 89ff748d | Thomas Thrainer | return
|
518 | 89ff748d | Thomas Thrainer | if len(devices) != 2: |
519 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: we need two children in RemoveChildren",
|
520 | 89ff748d | Thomas Thrainer | self.minor)
|
521 | 89ff748d | Thomas Thrainer | for child, dev in zip(self._children, devices): |
522 | 89ff748d | Thomas Thrainer | if dev != child.dev_path:
|
523 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: mismatch in local storage (%s != %s) in"
|
524 | 89ff748d | Thomas Thrainer | " RemoveChildren", self.minor, dev, child.dev_path) |
525 | 89ff748d | Thomas Thrainer | |
526 | 89ff748d | Thomas Thrainer | self._ShutdownLocal(self.minor) |
527 | 89ff748d | Thomas Thrainer | self._children = []
|
528 | 89ff748d | Thomas Thrainer | |
529 | 2fe690f1 | Thomas Thrainer | def _SetMinorSyncParams(self, minor, params): |
530 | 89ff748d | Thomas Thrainer | """Set the parameters of the DRBD syncer.
|
531 | 89ff748d | Thomas Thrainer |
|
532 | 89ff748d | Thomas Thrainer | This is the low-level implementation.
|
533 | 89ff748d | Thomas Thrainer |
|
534 | 89ff748d | Thomas Thrainer | @type minor: int
|
535 | 89ff748d | Thomas Thrainer | @param minor: the drbd minor whose settings we change
|
536 | 89ff748d | Thomas Thrainer | @type params: dict
|
537 | 89ff748d | Thomas Thrainer | @param params: LD level disk parameters related to the synchronization
|
538 | 89ff748d | Thomas Thrainer | @rtype: list
|
539 | 89ff748d | Thomas Thrainer | @return: a list of error messages
|
540 | 89ff748d | Thomas Thrainer |
|
541 | 89ff748d | Thomas Thrainer | """
|
542 | 89ff748d | Thomas Thrainer | |
543 | 2fe690f1 | Thomas Thrainer | args = ["drbdsetup", self._DevPath(minor), "syncer"] |
544 | 89ff748d | Thomas Thrainer | if params[constants.LDP_DYNAMIC_RESYNC]:
|
545 | 2fe690f1 | Thomas Thrainer | drbd_info = DRBD8Info.CreateFromFile() |
546 | 2fe690f1 | Thomas Thrainer | version = drbd_info.GetVersion() |
547 | 89ff748d | Thomas Thrainer | vmin = version["k_minor"]
|
548 | 89ff748d | Thomas Thrainer | vrel = version["k_point"]
|
549 | 89ff748d | Thomas Thrainer | |
550 | 89ff748d | Thomas Thrainer | # By definition we are using 8.x, so just check the rest of the version
|
551 | 89ff748d | Thomas Thrainer | # number
|
552 | 89ff748d | Thomas Thrainer | if vmin != 3 or vrel < 9: |
553 | 89ff748d | Thomas Thrainer | msg = ("The current DRBD version (8.%d.%d) does not support the "
|
554 | 89ff748d | Thomas Thrainer | "dynamic resync speed controller" % (vmin, vrel))
|
555 | 89ff748d | Thomas Thrainer | logging.error(msg) |
556 | 89ff748d | Thomas Thrainer | return [msg]
|
557 | 89ff748d | Thomas Thrainer | |
558 | 89ff748d | Thomas Thrainer | if params[constants.LDP_PLAN_AHEAD] == 0: |
559 | 89ff748d | Thomas Thrainer | msg = ("A value of 0 for c-plan-ahead disables the dynamic sync speed"
|
560 | 89ff748d | Thomas Thrainer | " controller at DRBD level. If you want to disable it, please"
|
561 | 89ff748d | Thomas Thrainer | " set the dynamic-resync disk parameter to False.")
|
562 | 89ff748d | Thomas Thrainer | logging.error(msg) |
563 | 89ff748d | Thomas Thrainer | return [msg]
|
564 | 89ff748d | Thomas Thrainer | |
565 | 89ff748d | Thomas Thrainer | # add the c-* parameters to args
|
566 | 89ff748d | Thomas Thrainer | args.extend(["--c-plan-ahead", params[constants.LDP_PLAN_AHEAD],
|
567 | 89ff748d | Thomas Thrainer | "--c-fill-target", params[constants.LDP_FILL_TARGET],
|
568 | 89ff748d | Thomas Thrainer | "--c-delay-target", params[constants.LDP_DELAY_TARGET],
|
569 | 89ff748d | Thomas Thrainer | "--c-max-rate", params[constants.LDP_MAX_RATE],
|
570 | 89ff748d | Thomas Thrainer | "--c-min-rate", params[constants.LDP_MIN_RATE],
|
571 | 89ff748d | Thomas Thrainer | ]) |
572 | 89ff748d | Thomas Thrainer | |
573 | 89ff748d | Thomas Thrainer | else:
|
574 | 89ff748d | Thomas Thrainer | args.extend(["-r", "%d" % params[constants.LDP_RESYNC_RATE]]) |
575 | 89ff748d | Thomas Thrainer | |
576 | 89ff748d | Thomas Thrainer | args.append("--create-device")
|
577 | 89ff748d | Thomas Thrainer | result = utils.RunCmd(args) |
578 | 89ff748d | Thomas Thrainer | if result.failed:
|
579 | 89ff748d | Thomas Thrainer | msg = ("Can't change syncer rate: %s - %s" %
|
580 | 89ff748d | Thomas Thrainer | (result.fail_reason, result.output)) |
581 | 89ff748d | Thomas Thrainer | logging.error(msg) |
582 | 89ff748d | Thomas Thrainer | return [msg]
|
583 | 89ff748d | Thomas Thrainer | |
584 | 89ff748d | Thomas Thrainer | return []
|
585 | 89ff748d | Thomas Thrainer | |
586 | 89ff748d | Thomas Thrainer | def SetSyncParams(self, params): |
587 | 89ff748d | Thomas Thrainer | """Set the synchronization parameters of the DRBD syncer.
|
588 | 89ff748d | Thomas Thrainer |
|
589 | 89ff748d | Thomas Thrainer | @type params: dict
|
590 | 89ff748d | Thomas Thrainer | @param params: LD level disk parameters related to the synchronization
|
591 | 89ff748d | Thomas Thrainer | @rtype: list
|
592 | 89ff748d | Thomas Thrainer | @return: a list of error messages, emitted both by the current node and by
|
593 | 89ff748d | Thomas Thrainer | children. An empty list means no errors
|
594 | 89ff748d | Thomas Thrainer |
|
595 | 89ff748d | Thomas Thrainer | """
|
596 | 89ff748d | Thomas Thrainer | if self.minor is None: |
597 | 89ff748d | Thomas Thrainer | err = "Not attached during SetSyncParams"
|
598 | 89ff748d | Thomas Thrainer | logging.info(err) |
599 | 89ff748d | Thomas Thrainer | return [err]
|
600 | 89ff748d | Thomas Thrainer | |
601 | 89ff748d | Thomas Thrainer | children_result = super(DRBD8, self).SetSyncParams(params) |
602 | 89ff748d | Thomas Thrainer | children_result.extend(self._SetMinorSyncParams(self.minor, params)) |
603 | 89ff748d | Thomas Thrainer | return children_result
|
604 | 89ff748d | Thomas Thrainer | |
605 | 89ff748d | Thomas Thrainer | def PauseResumeSync(self, pause): |
606 | 89ff748d | Thomas Thrainer | """Pauses or resumes the sync of a DRBD device.
|
607 | 89ff748d | Thomas Thrainer |
|
608 | 89ff748d | Thomas Thrainer | @param pause: Wether to pause or resume
|
609 | 89ff748d | Thomas Thrainer | @return: the success of the operation
|
610 | 89ff748d | Thomas Thrainer |
|
611 | 89ff748d | Thomas Thrainer | """
|
612 | 89ff748d | Thomas Thrainer | if self.minor is None: |
613 | 89ff748d | Thomas Thrainer | logging.info("Not attached during PauseSync")
|
614 | 89ff748d | Thomas Thrainer | return False |
615 | 89ff748d | Thomas Thrainer | |
616 | 89ff748d | Thomas Thrainer | children_result = super(DRBD8, self).PauseResumeSync(pause) |
617 | 89ff748d | Thomas Thrainer | |
618 | 89ff748d | Thomas Thrainer | if pause:
|
619 | 89ff748d | Thomas Thrainer | cmd = "pause-sync"
|
620 | 89ff748d | Thomas Thrainer | else:
|
621 | 89ff748d | Thomas Thrainer | cmd = "resume-sync"
|
622 | 89ff748d | Thomas Thrainer | |
623 | 89ff748d | Thomas Thrainer | result = utils.RunCmd(["drbdsetup", self.dev_path, cmd]) |
624 | 89ff748d | Thomas Thrainer | if result.failed:
|
625 | 89ff748d | Thomas Thrainer | logging.error("Can't %s: %s - %s", cmd,
|
626 | 89ff748d | Thomas Thrainer | result.fail_reason, result.output) |
627 | 89ff748d | Thomas Thrainer | return not result.failed and children_result |
628 | 89ff748d | Thomas Thrainer | |
629 | 89ff748d | Thomas Thrainer | def GetProcStatus(self): |
630 | 89ff748d | Thomas Thrainer | """Return device data from /proc.
|
631 | 89ff748d | Thomas Thrainer |
|
632 | 89ff748d | Thomas Thrainer | """
|
633 | 89ff748d | Thomas Thrainer | if self.minor is None: |
634 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: GetStats() called while not attached",
|
635 | 89ff748d | Thomas Thrainer | self._aminor)
|
636 | 2fe690f1 | Thomas Thrainer | drbd_info = DRBD8Info.CreateFromFile() |
637 | 2fe690f1 | Thomas Thrainer | if not drbd_info.HasMinorStatus(self.minor): |
638 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't find myself in /proc", self.minor) |
639 | 2fe690f1 | Thomas Thrainer | return drbd_info.GetMinorStatus(self.minor) |
640 | 89ff748d | Thomas Thrainer | |
641 | 89ff748d | Thomas Thrainer | def GetSyncStatus(self): |
642 | 89ff748d | Thomas Thrainer | """Returns the sync status of the device.
|
643 | 89ff748d | Thomas Thrainer |
|
644 | 89ff748d | Thomas Thrainer |
|
645 | 89ff748d | Thomas Thrainer | If sync_percent is None, it means all is ok
|
646 | 89ff748d | Thomas Thrainer | If estimated_time is None, it means we can't estimate
|
647 | 89ff748d | Thomas Thrainer | the time needed, otherwise it's the time left in seconds.
|
648 | 89ff748d | Thomas Thrainer |
|
649 | 89ff748d | Thomas Thrainer |
|
650 | 89ff748d | Thomas Thrainer | We set the is_degraded parameter to True on two conditions:
|
651 | 89ff748d | Thomas Thrainer | network not connected or local disk missing.
|
652 | 89ff748d | Thomas Thrainer |
|
653 | 89ff748d | Thomas Thrainer | We compute the ldisk parameter based on whether we have a local
|
654 | 89ff748d | Thomas Thrainer | disk or not.
|
655 | 89ff748d | Thomas Thrainer |
|
656 | 89ff748d | Thomas Thrainer | @rtype: objects.BlockDevStatus
|
657 | 89ff748d | Thomas Thrainer |
|
658 | 89ff748d | Thomas Thrainer | """
|
659 | 89ff748d | Thomas Thrainer | if self.minor is None and not self.Attach(): |
660 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't Attach() in GetSyncStatus", self._aminor) |
661 | 89ff748d | Thomas Thrainer | |
662 | 89ff748d | Thomas Thrainer | stats = self.GetProcStatus()
|
663 | 89ff748d | Thomas Thrainer | is_degraded = not stats.is_connected or not stats.is_disk_uptodate |
664 | 89ff748d | Thomas Thrainer | |
665 | 89ff748d | Thomas Thrainer | if stats.is_disk_uptodate:
|
666 | 89ff748d | Thomas Thrainer | ldisk_status = constants.LDS_OKAY |
667 | 89ff748d | Thomas Thrainer | elif stats.is_diskless:
|
668 | 89ff748d | Thomas Thrainer | ldisk_status = constants.LDS_FAULTY |
669 | 89ff748d | Thomas Thrainer | else:
|
670 | 89ff748d | Thomas Thrainer | ldisk_status = constants.LDS_UNKNOWN |
671 | 89ff748d | Thomas Thrainer | |
672 | 89ff748d | Thomas Thrainer | return objects.BlockDevStatus(dev_path=self.dev_path, |
673 | 89ff748d | Thomas Thrainer | major=self.major,
|
674 | 89ff748d | Thomas Thrainer | minor=self.minor,
|
675 | 89ff748d | Thomas Thrainer | sync_percent=stats.sync_percent, |
676 | 89ff748d | Thomas Thrainer | estimated_time=stats.est_time, |
677 | 89ff748d | Thomas Thrainer | is_degraded=is_degraded, |
678 | 89ff748d | Thomas Thrainer | ldisk_status=ldisk_status) |
679 | 89ff748d | Thomas Thrainer | |
680 | 89ff748d | Thomas Thrainer | def Open(self, force=False): |
681 | 89ff748d | Thomas Thrainer | """Make the local state primary.
|
682 | 89ff748d | Thomas Thrainer |
|
683 | 89ff748d | Thomas Thrainer | If the 'force' parameter is given, the '-o' option is passed to
|
684 | 89ff748d | Thomas Thrainer | drbdsetup. Since this is a potentially dangerous operation, the
|
685 | 89ff748d | Thomas Thrainer | force flag should be only given after creation, when it actually
|
686 | 89ff748d | Thomas Thrainer | is mandatory.
|
687 | 89ff748d | Thomas Thrainer |
|
688 | 89ff748d | Thomas Thrainer | """
|
689 | 89ff748d | Thomas Thrainer | if self.minor is None and not self.Attach(): |
690 | 89ff748d | Thomas Thrainer | logging.error("DRBD cannot attach to a device during open")
|
691 | 89ff748d | Thomas Thrainer | return False |
692 | 89ff748d | Thomas Thrainer | cmd = ["drbdsetup", self.dev_path, "primary"] |
693 | 89ff748d | Thomas Thrainer | if force:
|
694 | 89ff748d | Thomas Thrainer | cmd.append("-o")
|
695 | 89ff748d | Thomas Thrainer | result = utils.RunCmd(cmd) |
696 | 89ff748d | Thomas Thrainer | if result.failed:
|
697 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't make drbd device primary: %s", self.minor, |
698 | 89ff748d | Thomas Thrainer | result.output) |
699 | 89ff748d | Thomas Thrainer | |
700 | 89ff748d | Thomas Thrainer | def Close(self): |
701 | 89ff748d | Thomas Thrainer | """Make the local state secondary.
|
702 | 89ff748d | Thomas Thrainer |
|
703 | 89ff748d | Thomas Thrainer | This will, of course, fail if the device is in use.
|
704 | 89ff748d | Thomas Thrainer |
|
705 | 89ff748d | Thomas Thrainer | """
|
706 | 89ff748d | Thomas Thrainer | if self.minor is None and not self.Attach(): |
707 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't Attach() in Close()", self._aminor) |
708 | 89ff748d | Thomas Thrainer | result = utils.RunCmd(["drbdsetup", self.dev_path, "secondary"]) |
709 | 89ff748d | Thomas Thrainer | if result.failed:
|
710 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't switch drbd device to secondary: %s",
|
711 | 89ff748d | Thomas Thrainer | self.minor, result.output)
|
712 | 89ff748d | Thomas Thrainer | |
713 | 89ff748d | Thomas Thrainer | def DisconnectNet(self): |
714 | 89ff748d | Thomas Thrainer | """Removes network configuration.
|
715 | 89ff748d | Thomas Thrainer |
|
716 | 89ff748d | Thomas Thrainer | This method shutdowns the network side of the device.
|
717 | 89ff748d | Thomas Thrainer |
|
718 | 89ff748d | Thomas Thrainer | The method will wait up to a hardcoded timeout for the device to
|
719 | 89ff748d | Thomas Thrainer | go into standalone after the 'disconnect' command before
|
720 | 89ff748d | Thomas Thrainer | re-configuring it, as sometimes it takes a while for the
|
721 | 89ff748d | Thomas Thrainer | disconnect to actually propagate and thus we might issue a 'net'
|
722 | 89ff748d | Thomas Thrainer | command while the device is still connected. If the device will
|
723 | 89ff748d | Thomas Thrainer | still be attached to the network and we time out, we raise an
|
724 | 89ff748d | Thomas Thrainer | exception.
|
725 | 89ff748d | Thomas Thrainer |
|
726 | 89ff748d | Thomas Thrainer | """
|
727 | 89ff748d | Thomas Thrainer | if self.minor is None: |
728 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: disk not attached in re-attach net",
|
729 | 89ff748d | Thomas Thrainer | self._aminor)
|
730 | 89ff748d | Thomas Thrainer | |
731 | 89ff748d | Thomas Thrainer | if None in (self._lhost, self._lport, self._rhost, self._rport): |
732 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: DRBD disk missing network info in"
|
733 | 89ff748d | Thomas Thrainer | " DisconnectNet()", self.minor) |
734 | 89ff748d | Thomas Thrainer | |
735 | 89ff748d | Thomas Thrainer | class _DisconnectStatus: |
736 | 89ff748d | Thomas Thrainer | def __init__(self, ever_disconnected): |
737 | 89ff748d | Thomas Thrainer | self.ever_disconnected = ever_disconnected
|
738 | 89ff748d | Thomas Thrainer | |
739 | 89ff748d | Thomas Thrainer | dstatus = _DisconnectStatus(base.IgnoreError(self._ShutdownNet, self.minor)) |
740 | 89ff748d | Thomas Thrainer | |
741 | 89ff748d | Thomas Thrainer | def _WaitForDisconnect(): |
742 | 89ff748d | Thomas Thrainer | if self.GetProcStatus().is_standalone: |
743 | 89ff748d | Thomas Thrainer | return
|
744 | 89ff748d | Thomas Thrainer | |
745 | 89ff748d | Thomas Thrainer | # retry the disconnect, it seems possible that due to a well-time
|
746 | 89ff748d | Thomas Thrainer | # disconnect on the peer, my disconnect command might be ignored and
|
747 | 89ff748d | Thomas Thrainer | # forgotten
|
748 | 89ff748d | Thomas Thrainer | dstatus.ever_disconnected = \ |
749 | 89ff748d | Thomas Thrainer | base.IgnoreError(self._ShutdownNet, self.minor) or \ |
750 | 89ff748d | Thomas Thrainer | dstatus.ever_disconnected |
751 | 89ff748d | Thomas Thrainer | |
752 | 89ff748d | Thomas Thrainer | raise utils.RetryAgain()
|
753 | 89ff748d | Thomas Thrainer | |
754 | 89ff748d | Thomas Thrainer | # Keep start time
|
755 | 89ff748d | Thomas Thrainer | start_time = time.time() |
756 | 89ff748d | Thomas Thrainer | |
757 | 89ff748d | Thomas Thrainer | try:
|
758 | 89ff748d | Thomas Thrainer | # Start delay at 100 milliseconds and grow up to 2 seconds
|
759 | 89ff748d | Thomas Thrainer | utils.Retry(_WaitForDisconnect, (0.1, 1.5, 2.0), |
760 | 89ff748d | Thomas Thrainer | self._NET_RECONFIG_TIMEOUT)
|
761 | 89ff748d | Thomas Thrainer | except utils.RetryTimeout:
|
762 | 89ff748d | Thomas Thrainer | if dstatus.ever_disconnected:
|
763 | 89ff748d | Thomas Thrainer | msg = ("drbd%d: device did not react to the"
|
764 | 89ff748d | Thomas Thrainer | " 'disconnect' command in a timely manner")
|
765 | 89ff748d | Thomas Thrainer | else:
|
766 | 89ff748d | Thomas Thrainer | msg = "drbd%d: can't shutdown network, even after multiple retries"
|
767 | 89ff748d | Thomas Thrainer | |
768 | 89ff748d | Thomas Thrainer | base.ThrowError(msg, self.minor)
|
769 | 89ff748d | Thomas Thrainer | |
770 | 89ff748d | Thomas Thrainer | reconfig_time = time.time() - start_time |
771 | 89ff748d | Thomas Thrainer | if reconfig_time > (self._NET_RECONFIG_TIMEOUT * 0.25): |
772 | 89ff748d | Thomas Thrainer | logging.info("drbd%d: DisconnectNet: detach took %.3f seconds",
|
773 | 89ff748d | Thomas Thrainer | self.minor, reconfig_time)
|
774 | 89ff748d | Thomas Thrainer | |
775 | 89ff748d | Thomas Thrainer | def AttachNet(self, multimaster): |
776 | 89ff748d | Thomas Thrainer | """Reconnects the network.
|
777 | 89ff748d | Thomas Thrainer |
|
778 | 89ff748d | Thomas Thrainer | This method connects the network side of the device with a
|
779 | 89ff748d | Thomas Thrainer | specified multi-master flag. The device needs to be 'Standalone'
|
780 | 89ff748d | Thomas Thrainer | but have valid network configuration data.
|
781 | 89ff748d | Thomas Thrainer |
|
782 | 89ff748d | Thomas Thrainer | Args:
|
783 | 89ff748d | Thomas Thrainer | - multimaster: init the network in dual-primary mode
|
784 | 89ff748d | Thomas Thrainer |
|
785 | 89ff748d | Thomas Thrainer | """
|
786 | 89ff748d | Thomas Thrainer | if self.minor is None: |
787 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: device not attached in AttachNet", self._aminor) |
788 | 89ff748d | Thomas Thrainer | |
789 | 89ff748d | Thomas Thrainer | if None in (self._lhost, self._lport, self._rhost, self._rport): |
790 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: missing network info in AttachNet()", self.minor) |
791 | 89ff748d | Thomas Thrainer | |
792 | 89ff748d | Thomas Thrainer | status = self.GetProcStatus()
|
793 | 89ff748d | Thomas Thrainer | |
794 | 89ff748d | Thomas Thrainer | if not status.is_standalone: |
795 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: device is not standalone in AttachNet",
|
796 | 89ff748d | Thomas Thrainer | self.minor)
|
797 | 89ff748d | Thomas Thrainer | |
798 | 89ff748d | Thomas Thrainer | self._AssembleNet(self.minor, |
799 | 89ff748d | Thomas Thrainer | (self._lhost, self._lport, self._rhost, self._rport), |
800 | 89ff748d | Thomas Thrainer | constants.DRBD_NET_PROTOCOL, dual_pri=multimaster, |
801 | 89ff748d | Thomas Thrainer | hmac=constants.DRBD_HMAC_ALG, secret=self._secret)
|
802 | 89ff748d | Thomas Thrainer | |
803 | 89ff748d | Thomas Thrainer | def Attach(self): |
804 | 89ff748d | Thomas Thrainer | """Check if our minor is configured.
|
805 | 89ff748d | Thomas Thrainer |
|
806 | 89ff748d | Thomas Thrainer | This doesn't do any device configurations - it only checks if the
|
807 | 89ff748d | Thomas Thrainer | minor is in a state different from Unconfigured.
|
808 | 89ff748d | Thomas Thrainer |
|
809 | 89ff748d | Thomas Thrainer | Note that this function will not change the state of the system in
|
810 | 89ff748d | Thomas Thrainer | any way (except in case of side-effects caused by reading from
|
811 | 89ff748d | Thomas Thrainer | /proc).
|
812 | 89ff748d | Thomas Thrainer |
|
813 | 89ff748d | Thomas Thrainer | """
|
814 | 89ff748d | Thomas Thrainer | used_devs = self.GetUsedDevs()
|
815 | 89ff748d | Thomas Thrainer | if self._aminor in used_devs: |
816 | 89ff748d | Thomas Thrainer | minor = self._aminor
|
817 | 89ff748d | Thomas Thrainer | else:
|
818 | 89ff748d | Thomas Thrainer | minor = None
|
819 | 89ff748d | Thomas Thrainer | |
820 | 89ff748d | Thomas Thrainer | self._SetFromMinor(minor)
|
821 | 89ff748d | Thomas Thrainer | return minor is not None |
822 | 89ff748d | Thomas Thrainer | |
823 | 89ff748d | Thomas Thrainer | def Assemble(self): |
824 | 89ff748d | Thomas Thrainer | """Assemble the drbd.
|
825 | 89ff748d | Thomas Thrainer |
|
826 | 89ff748d | Thomas Thrainer | Method:
|
827 | 89ff748d | Thomas Thrainer | - if we have a configured device, we try to ensure that it matches
|
828 | 89ff748d | Thomas Thrainer | our config
|
829 | 89ff748d | Thomas Thrainer | - if not, we create it from zero
|
830 | 89ff748d | Thomas Thrainer | - anyway, set the device parameters
|
831 | 89ff748d | Thomas Thrainer |
|
832 | 89ff748d | Thomas Thrainer | """
|
833 | 89ff748d | Thomas Thrainer | super(DRBD8, self).Assemble() |
834 | 89ff748d | Thomas Thrainer | |
835 | 89ff748d | Thomas Thrainer | self.Attach()
|
836 | 89ff748d | Thomas Thrainer | if self.minor is None: |
837 | 89ff748d | Thomas Thrainer | # local device completely unconfigured
|
838 | 89ff748d | Thomas Thrainer | self._FastAssemble()
|
839 | 89ff748d | Thomas Thrainer | else:
|
840 | 89ff748d | Thomas Thrainer | # we have to recheck the local and network status and try to fix
|
841 | 89ff748d | Thomas Thrainer | # the device
|
842 | 89ff748d | Thomas Thrainer | self._SlowAssemble()
|
843 | 89ff748d | Thomas Thrainer | |
844 | 89ff748d | Thomas Thrainer | sync_errors = self.SetSyncParams(self.params) |
845 | 89ff748d | Thomas Thrainer | if sync_errors:
|
846 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't set the synchronization parameters: %s" %
|
847 | 89ff748d | Thomas Thrainer | (self.minor, utils.CommaJoin(sync_errors)))
|
848 | 89ff748d | Thomas Thrainer | |
849 | 89ff748d | Thomas Thrainer | def _SlowAssemble(self): |
850 | 89ff748d | Thomas Thrainer | """Assembles the DRBD device from a (partially) configured device.
|
851 | 89ff748d | Thomas Thrainer |
|
852 | 89ff748d | Thomas Thrainer | In case of partially attached (local device matches but no network
|
853 | 89ff748d | Thomas Thrainer | setup), we perform the network attach. If successful, we re-test
|
854 | 89ff748d | Thomas Thrainer | the attach if can return success.
|
855 | 89ff748d | Thomas Thrainer |
|
856 | 89ff748d | Thomas Thrainer | """
|
857 | 89ff748d | Thomas Thrainer | # TODO: Rewrite to not use a for loop just because there is 'break'
|
858 | 89ff748d | Thomas Thrainer | # pylint: disable=W0631
|
859 | 89ff748d | Thomas Thrainer | net_data = (self._lhost, self._lport, self._rhost, self._rport) |
860 | 89ff748d | Thomas Thrainer | for minor in (self._aminor,): |
861 | 27c7d9c3 | Thomas Thrainer | info = self._GetShowInfo(minor)
|
862 | 89ff748d | Thomas Thrainer | match_l = self._MatchesLocal(info)
|
863 | 89ff748d | Thomas Thrainer | match_r = self._MatchesNet(info)
|
864 | 89ff748d | Thomas Thrainer | |
865 | 89ff748d | Thomas Thrainer | if match_l and match_r: |
866 | 89ff748d | Thomas Thrainer | # everything matches
|
867 | 89ff748d | Thomas Thrainer | break
|
868 | 89ff748d | Thomas Thrainer | |
869 | 89ff748d | Thomas Thrainer | if match_l and not match_r and "local_addr" not in info: |
870 | 89ff748d | Thomas Thrainer | # disk matches, but not attached to network, attach and recheck
|
871 | 89ff748d | Thomas Thrainer | self._AssembleNet(minor, net_data, constants.DRBD_NET_PROTOCOL,
|
872 | 89ff748d | Thomas Thrainer | hmac=constants.DRBD_HMAC_ALG, secret=self._secret)
|
873 | 27c7d9c3 | Thomas Thrainer | if self._MatchesNet(self._GetShowInfo(minor)): |
874 | 89ff748d | Thomas Thrainer | break
|
875 | 89ff748d | Thomas Thrainer | else:
|
876 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: network attach successful, but 'drbdsetup"
|
877 | 89ff748d | Thomas Thrainer | " show' disagrees", minor)
|
878 | 89ff748d | Thomas Thrainer | |
879 | 89ff748d | Thomas Thrainer | if match_r and "local_dev" not in info: |
880 | 89ff748d | Thomas Thrainer | # no local disk, but network attached and it matches
|
881 | 89ff748d | Thomas Thrainer | self._AssembleLocal(minor, self._children[0].dev_path, |
882 | 89ff748d | Thomas Thrainer | self._children[1].dev_path, self.size) |
883 | 27c7d9c3 | Thomas Thrainer | if self._MatchesNet(self._GetShowInfo(minor)): |
884 | 89ff748d | Thomas Thrainer | break
|
885 | 89ff748d | Thomas Thrainer | else:
|
886 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: disk attach successful, but 'drbdsetup"
|
887 | 89ff748d | Thomas Thrainer | " show' disagrees", minor)
|
888 | 89ff748d | Thomas Thrainer | |
889 | 89ff748d | Thomas Thrainer | # this case must be considered only if we actually have local
|
890 | 89ff748d | Thomas Thrainer | # storage, i.e. not in diskless mode, because all diskless
|
891 | 89ff748d | Thomas Thrainer | # devices are equal from the point of view of local
|
892 | 89ff748d | Thomas Thrainer | # configuration
|
893 | 89ff748d | Thomas Thrainer | if (match_l and "local_dev" in info and |
894 | 89ff748d | Thomas Thrainer | not match_r and "local_addr" in info): |
895 | 89ff748d | Thomas Thrainer | # strange case - the device network part points to somewhere
|
896 | 89ff748d | Thomas Thrainer | # else, even though its local storage is ours; as we own the
|
897 | 89ff748d | Thomas Thrainer | # drbd space, we try to disconnect from the remote peer and
|
898 | 89ff748d | Thomas Thrainer | # reconnect to our correct one
|
899 | 89ff748d | Thomas Thrainer | try:
|
900 | 89ff748d | Thomas Thrainer | self._ShutdownNet(minor)
|
901 | 89ff748d | Thomas Thrainer | except errors.BlockDeviceError, err:
|
902 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: device has correct local storage, wrong"
|
903 | 89ff748d | Thomas Thrainer | " remote peer and is unable to disconnect in order"
|
904 | 89ff748d | Thomas Thrainer | " to attach to the correct peer: %s", minor, str(err)) |
905 | 89ff748d | Thomas Thrainer | # note: _AssembleNet also handles the case when we don't want
|
906 | 89ff748d | Thomas Thrainer | # local storage (i.e. one or more of the _[lr](host|port) is
|
907 | 89ff748d | Thomas Thrainer | # None)
|
908 | 89ff748d | Thomas Thrainer | self._AssembleNet(minor, net_data, constants.DRBD_NET_PROTOCOL,
|
909 | 89ff748d | Thomas Thrainer | hmac=constants.DRBD_HMAC_ALG, secret=self._secret)
|
910 | 27c7d9c3 | Thomas Thrainer | if self._MatchesNet(self._GetShowInfo(minor)): |
911 | 89ff748d | Thomas Thrainer | break
|
912 | 89ff748d | Thomas Thrainer | else:
|
913 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: network attach successful, but 'drbdsetup"
|
914 | 89ff748d | Thomas Thrainer | " show' disagrees", minor)
|
915 | 89ff748d | Thomas Thrainer | |
916 | 89ff748d | Thomas Thrainer | else:
|
917 | 89ff748d | Thomas Thrainer | minor = None
|
918 | 89ff748d | Thomas Thrainer | |
919 | 89ff748d | Thomas Thrainer | self._SetFromMinor(minor)
|
920 | 89ff748d | Thomas Thrainer | if minor is None: |
921 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: cannot activate, unknown or unhandled reason",
|
922 | 89ff748d | Thomas Thrainer | self._aminor)
|
923 | 89ff748d | Thomas Thrainer | |
924 | 89ff748d | Thomas Thrainer | def _FastAssemble(self): |
925 | 89ff748d | Thomas Thrainer | """Assemble the drbd device from zero.
|
926 | 89ff748d | Thomas Thrainer |
|
927 | 89ff748d | Thomas Thrainer | This is run when in Assemble we detect our minor is unused.
|
928 | 89ff748d | Thomas Thrainer |
|
929 | 89ff748d | Thomas Thrainer | """
|
930 | 89ff748d | Thomas Thrainer | minor = self._aminor
|
931 | 89ff748d | Thomas Thrainer | if self._children and self._children[0] and self._children[1]: |
932 | 89ff748d | Thomas Thrainer | self._AssembleLocal(minor, self._children[0].dev_path, |
933 | 89ff748d | Thomas Thrainer | self._children[1].dev_path, self.size) |
934 | 89ff748d | Thomas Thrainer | if self._lhost and self._lport and self._rhost and self._rport: |
935 | 89ff748d | Thomas Thrainer | self._AssembleNet(minor,
|
936 | 89ff748d | Thomas Thrainer | (self._lhost, self._lport, self._rhost, self._rport), |
937 | 89ff748d | Thomas Thrainer | constants.DRBD_NET_PROTOCOL, |
938 | 89ff748d | Thomas Thrainer | hmac=constants.DRBD_HMAC_ALG, secret=self._secret)
|
939 | 89ff748d | Thomas Thrainer | self._SetFromMinor(minor)
|
940 | 89ff748d | Thomas Thrainer | |
941 | 89ff748d | Thomas Thrainer | @classmethod
|
942 | 89ff748d | Thomas Thrainer | def _ShutdownLocal(cls, minor): |
943 | 89ff748d | Thomas Thrainer | """Detach from the local device.
|
944 | 89ff748d | Thomas Thrainer |
|
945 | 89ff748d | Thomas Thrainer | I/Os will continue to be served from the remote device. If we
|
946 | 89ff748d | Thomas Thrainer | don't have a remote device, this operation will fail.
|
947 | 89ff748d | Thomas Thrainer |
|
948 | 89ff748d | Thomas Thrainer | """
|
949 | 89ff748d | Thomas Thrainer | result = utils.RunCmd(["drbdsetup", cls._DevPath(minor), "detach"]) |
950 | 89ff748d | Thomas Thrainer | if result.failed:
|
951 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't detach local disk: %s",
|
952 | 89ff748d | Thomas Thrainer | minor, result.output) |
953 | 89ff748d | Thomas Thrainer | |
954 | 89ff748d | Thomas Thrainer | @classmethod
|
955 | 89ff748d | Thomas Thrainer | def _ShutdownNet(cls, minor): |
956 | 89ff748d | Thomas Thrainer | """Disconnect from the remote peer.
|
957 | 89ff748d | Thomas Thrainer |
|
958 | 89ff748d | Thomas Thrainer | This fails if we don't have a local device.
|
959 | 89ff748d | Thomas Thrainer |
|
960 | 89ff748d | Thomas Thrainer | """
|
961 | 89ff748d | Thomas Thrainer | result = utils.RunCmd(["drbdsetup", cls._DevPath(minor), "disconnect"]) |
962 | 89ff748d | Thomas Thrainer | if result.failed:
|
963 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't shutdown network: %s",
|
964 | 89ff748d | Thomas Thrainer | minor, result.output) |
965 | 89ff748d | Thomas Thrainer | |
966 | 89ff748d | Thomas Thrainer | @classmethod
|
967 | 89ff748d | Thomas Thrainer | def _ShutdownAll(cls, minor): |
968 | 89ff748d | Thomas Thrainer | """Deactivate the device.
|
969 | 89ff748d | Thomas Thrainer |
|
970 | 89ff748d | Thomas Thrainer | This will, of course, fail if the device is in use.
|
971 | 89ff748d | Thomas Thrainer |
|
972 | 89ff748d | Thomas Thrainer | """
|
973 | 89ff748d | Thomas Thrainer | result = utils.RunCmd(["drbdsetup", cls._DevPath(minor), "down"]) |
974 | 89ff748d | Thomas Thrainer | if result.failed:
|
975 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't shutdown drbd device: %s",
|
976 | 89ff748d | Thomas Thrainer | minor, result.output) |
977 | 89ff748d | Thomas Thrainer | |
978 | 89ff748d | Thomas Thrainer | def Shutdown(self): |
979 | 89ff748d | Thomas Thrainer | """Shutdown the DRBD device.
|
980 | 89ff748d | Thomas Thrainer |
|
981 | 89ff748d | Thomas Thrainer | """
|
982 | 89ff748d | Thomas Thrainer | if self.minor is None and not self.Attach(): |
983 | 89ff748d | Thomas Thrainer | logging.info("drbd%d: not attached during Shutdown()", self._aminor) |
984 | 89ff748d | Thomas Thrainer | return
|
985 | 89ff748d | Thomas Thrainer | minor = self.minor
|
986 | 89ff748d | Thomas Thrainer | self.minor = None |
987 | 89ff748d | Thomas Thrainer | self.dev_path = None |
988 | 89ff748d | Thomas Thrainer | self._ShutdownAll(minor)
|
989 | 89ff748d | Thomas Thrainer | |
990 | 89ff748d | Thomas Thrainer | def Remove(self): |
991 | 89ff748d | Thomas Thrainer | """Stub remove for DRBD devices.
|
992 | 89ff748d | Thomas Thrainer |
|
993 | 89ff748d | Thomas Thrainer | """
|
994 | 89ff748d | Thomas Thrainer | self.Shutdown()
|
995 | 89ff748d | Thomas Thrainer | |
996 | fd300bc7 | Thomas Thrainer | def Rename(self, new_id): |
997 | fd300bc7 | Thomas Thrainer | """Rename a device.
|
998 | fd300bc7 | Thomas Thrainer |
|
999 | fd300bc7 | Thomas Thrainer | This is not supported for drbd devices.
|
1000 | fd300bc7 | Thomas Thrainer |
|
1001 | fd300bc7 | Thomas Thrainer | """
|
1002 | fd300bc7 | Thomas Thrainer | raise errors.ProgrammerError("Can't rename a drbd device") |
1003 | fd300bc7 | Thomas Thrainer | |
1004 | 89ff748d | Thomas Thrainer | @classmethod
|
1005 | 89ff748d | Thomas Thrainer | def Create(cls, unique_id, children, size, params, excl_stor): |
1006 | 89ff748d | Thomas Thrainer | """Create a new DRBD8 device.
|
1007 | 89ff748d | Thomas Thrainer |
|
1008 | 89ff748d | Thomas Thrainer | Since DRBD devices are not created per se, just assembled, this
|
1009 | 89ff748d | Thomas Thrainer | function only initializes the metadata.
|
1010 | 89ff748d | Thomas Thrainer |
|
1011 | 89ff748d | Thomas Thrainer | """
|
1012 | 89ff748d | Thomas Thrainer | if len(children) != 2: |
1013 | 89ff748d | Thomas Thrainer | raise errors.ProgrammerError("Invalid setup for the drbd device") |
1014 | 89ff748d | Thomas Thrainer | if excl_stor:
|
1015 | 89ff748d | Thomas Thrainer | raise errors.ProgrammerError("DRBD device requested with" |
1016 | 89ff748d | Thomas Thrainer | " exclusive_storage")
|
1017 | 89ff748d | Thomas Thrainer | # check that the minor is unused
|
1018 | 89ff748d | Thomas Thrainer | aminor = unique_id[4]
|
1019 | 2fe690f1 | Thomas Thrainer | |
1020 | 2fe690f1 | Thomas Thrainer | drbd_info = DRBD8Info.CreateFromFile() |
1021 | 2fe690f1 | Thomas Thrainer | if drbd_info.HasMinorStatus(aminor):
|
1022 | 2fe690f1 | Thomas Thrainer | status = drbd_info.GetMinorStatus(aminor) |
1023 | 89ff748d | Thomas Thrainer | in_use = status.is_in_use |
1024 | 89ff748d | Thomas Thrainer | else:
|
1025 | 89ff748d | Thomas Thrainer | in_use = False
|
1026 | 89ff748d | Thomas Thrainer | if in_use:
|
1027 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: minor is already in use at Create() time",
|
1028 | 89ff748d | Thomas Thrainer | aminor) |
1029 | 89ff748d | Thomas Thrainer | meta = children[1]
|
1030 | 89ff748d | Thomas Thrainer | meta.Assemble() |
1031 | 89ff748d | Thomas Thrainer | if not meta.Attach(): |
1032 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: can't attach to meta device '%s'",
|
1033 | 89ff748d | Thomas Thrainer | aminor, meta) |
1034 | 89ff748d | Thomas Thrainer | cls._CheckMetaSize(meta.dev_path) |
1035 | 89ff748d | Thomas Thrainer | cls._InitMeta(aminor, meta.dev_path) |
1036 | 89ff748d | Thomas Thrainer | return cls(unique_id, children, size, params)
|
1037 | 89ff748d | Thomas Thrainer | |
1038 | 89ff748d | Thomas Thrainer | def Grow(self, amount, dryrun, backingstore): |
1039 | 89ff748d | Thomas Thrainer | """Resize the DRBD device and its backing storage.
|
1040 | 89ff748d | Thomas Thrainer |
|
1041 | 89ff748d | Thomas Thrainer | """
|
1042 | 89ff748d | Thomas Thrainer | if self.minor is None: |
1043 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: Grow called while not attached", self._aminor) |
1044 | 89ff748d | Thomas Thrainer | if len(self._children) != 2 or None in self._children: |
1045 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: cannot grow diskless device", self.minor) |
1046 | 89ff748d | Thomas Thrainer | self._children[0].Grow(amount, dryrun, backingstore) |
1047 | 89ff748d | Thomas Thrainer | if dryrun or backingstore: |
1048 | 89ff748d | Thomas Thrainer | # DRBD does not support dry-run mode and is not backing storage,
|
1049 | 89ff748d | Thomas Thrainer | # so we'll return here
|
1050 | 89ff748d | Thomas Thrainer | return
|
1051 | 89ff748d | Thomas Thrainer | result = utils.RunCmd(["drbdsetup", self.dev_path, "resize", "-s", |
1052 | 89ff748d | Thomas Thrainer | "%dm" % (self.size + amount)]) |
1053 | 89ff748d | Thomas Thrainer | if result.failed:
|
1054 | 89ff748d | Thomas Thrainer | base.ThrowError("drbd%d: resize failed: %s", self.minor, result.output) |
1055 | 89ff748d | Thomas Thrainer | |
1056 | 89ff748d | Thomas Thrainer | |
1057 | 89ff748d | Thomas Thrainer | def _CanReadDevice(path): |
1058 | 89ff748d | Thomas Thrainer | """Check if we can read from the given device.
|
1059 | 89ff748d | Thomas Thrainer |
|
1060 | 89ff748d | Thomas Thrainer | This tries to read the first 128k of the device.
|
1061 | 89ff748d | Thomas Thrainer |
|
1062 | 89ff748d | Thomas Thrainer | """
|
1063 | 89ff748d | Thomas Thrainer | try:
|
1064 | 89ff748d | Thomas Thrainer | utils.ReadFile(path, size=_DEVICE_READ_SIZE) |
1065 | 89ff748d | Thomas Thrainer | return True |
1066 | 89ff748d | Thomas Thrainer | except EnvironmentError: |
1067 | 89ff748d | Thomas Thrainer | logging.warning("Can't read from device %s", path, exc_info=True) |
1068 | 89ff748d | Thomas Thrainer | return False |