This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_drbd.py
252 lines (181 loc) · 7.37 KB
/
mod_drbd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import config
import glob
import os
import re
import subprocess
import sys
import time
import mod_logger
log = mod_logger.initlog(__name__)
import mod_utils
RE_DRBD_RESOURCE = re.compile("^[\ \t]*resource[\ \t]+([a-z0-9]+).*$")
RE_DRBD_DEVICE = re.compile("^[\ \t]*device[\ \t]+([a-z0-9/]+).*$")
RE_DRBD_ROLE = re.compile("^([^/]+)/([^\n]+)$")
RE_DRBD_MINOR = re.compile("^/dev/drbd([0-9]+)$")
RE_DRBD_PROC_LINE1 = re.compile("^[ \t]*([0-9]+): cs:([^ ]+) ro:([^/]+)/([^ ]+) ds:([^/]+)/([^ ]+) ([^ ]+) ([^ ]+)\n$")
RE_DRBD_PROC_LINE2 = re.compile("^[ \t]*ns:([0-9]+) nr:([0-9]+) dw:([0-9]+) dr:([0-9]+) al:([0-9]+) bm:([0-9]+) lo:([0-9]+) pe:([0-9]+) ua:([0-9]+) ap:([0-9]+) ep:([0-9]+) wo:([a-z]+) oos:([0-9]+)\n$")
RE_DRBD_MOUNT = re.compile("^([^\t ]+)[ \t]+([^\t ]+)[ \t]+([^\t ]+)[ \t]+([^\t ]+)[ \t]+([^\t ]+)[ \t]+([^\t ]+)[ \t]*$")
RE_FUSER = re.compile("^[ \t]+([^ \t]+)[ \t]+([0-9]+)[ \t]+([^ \t]+)[ \t]+(.*)\n$")
FSTAB = "/etc/fstab"
PROC_DRBD = "/proc/drbd"
PROC_MOUNTS = "/proc/mounts"
MOUNT_TIMEOUT = 3.0
UMOUNT_TIMEOUT = 3.0
FUSERKILL_TIMEOUT = 3.0
DRBD_SETROLE_TIMEOUT = 5.0
resources = list()
class Resource():
def __init__(self, name, device):
self.name = name
self.device = device
self.minor = self.getMinor()
self.connection_status = False
self.role_local = False
self.role_peer = False
self.diskstatus_local = False
self.diskstatus_peer = False
self.replation_protocol = False
self.io_flags = False
self.getMountPoint()
self.readStatus()
def getMinor(self):
match = RE_DRBD_MINOR.match(self.device)
if match:
return int(match.group(1))
return 99
def getMountPoint(self):
for line in open(FSTAB).readlines():
match = RE_DRBD_MOUNT.match(line)
if match:
if self.device == match.group(1):
self.mountpoint = match.group(2)
return True
log.fatal("mount point for device %s is not defined in %s" % (self.device, FSTAB))
return False
# Sample from /proc/drbd
# 2: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate C r-----
# ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
def readStatus(self):
if not os.path.isfile(PROC_DRBD):
log.fatal("failed to open %s", PROC_DRBD)
return False
for line in open(PROC_DRBD).readlines():
match1 = RE_DRBD_PROC_LINE1.match(line)
if match1:
# we have reached matched first status line
if self.minor == int(match1.group(1)):
# we have reached our device status line
self.connection_status = match1.group(2).lower()
self.role_local = match1.group(3).lower()
self.role_peer = match1.group(4).lower()
self.diskstatus_local = match1.group(5).lower()
self.diskstatus_peer = match1.group(6).lower()
self.replation_protocol = match1.group(7).lower()
self.io_flags = match1.group(8).lower()
return True
return False
def getConnectionStatus(self):
return self.readStatus() and self.connection_status
def getLocalRole(self):
return self.readStatus() and self.role_local
def getPeerRole(self):
return self.readStatus() and self.role_peer
def getLocalDiskStatus(self):
return self.readStatus() and self.diskstatus_local
def getPeerDiskStatus(self):
return self.readStatus() and self.diskstatus_peer
def isMounted(self):
for line in open(PROC_MOUNTS).readlines():
if line.startswith("%s " % (self.device)):
return True
return False
def isInUse(self):
res, output = mod_utils.execute("fuser -v -M -m %s" % (self.mountpoint))
in_use = False
for line in output:
if RE_FUSER.match(line):
in_use = True
return in_use
def mount(self):
if self.isMounted():
return True
res, output = mod_utils.execute("mount %s" % (self.device))
start = time.time()
if res:
# fail to mount
return False
while not self.isMounted() and time.time() - start <= MOUNT_TIMEOUT:
time.sleep(0.1)
if time.time() - start > MOUNT_TIMEOUT:
# timeout while waiting for device to appear in /proc/mounts
return False
return True
def umount(self):
if not self.isMounted():
return True
if self.isInUse():
start = time.time()
while self.isInUse() and time.time() - start <= FUSERKILL_TIMEOUT:
mod_utils.execute("fuser -k -M -m %s -TERM" % (self.mountpoint))
time.sleep(0.1)
if time.time() - start > FUSERKILL_TIMEOUT:
mod_utils.execute("fuser -k -M -m %s -KILL" % (self.mountpoint))
res, output = mod_utils.execute("umount %s" % (self.device))
start = time.time()
if res:
# fail to unmount
return False
while self.isMounted() and time.time() - start <= UMOUNT_TIMEOUT:
time.sleep(0.1)
if time.time() - start > UMOUNT_TIMEOUT:
# timeout while waiting for device to disapear from /proc/mounts
return False
return True
def setRole(self, role):
if role not in [ "primary", "secondary" ]:
return False
if not self.getLocalRole():
return False
elif self.getLocalRole() == role:
return True
res, output = mod_utils.execute("drbdadm %s %s" % (role, self.name))
start = time.time()
if res:
# fail to set role
return False
while self.getLocalRole() != role and time.time() - start <= DRBD_SETROLE_TIMEOUT:
time.sleep(0.1)
if time.time() - start > DRBD_SETROLE_TIMEOUT:
# timeout while waiting for resource role to change
return False
return True
def setPrimary(self):
if not self.setRole("primary"):
return False
return self.mount()
def setSecondary(self):
if not self.umount():
return False
return self.setRole("secondary")
def load():
global resources
for res in glob.glob("%s/*.res" % (config.drbd_dir)):
resource = False
device = False
for line in open(res).readlines():
matchresource = RE_DRBD_RESOURCE.match(line)
matchdevice = RE_DRBD_DEVICE.match(line)
if matchresource: resource = matchresource.group(1)
if matchdevice: device = matchdevice.group(1)
if resource and device and resource in config.drbd_resources:
resources.append(Resource(resource, device))
resource = False
device = False
def show():
global resources
print "DRBD resources :"
for resource in resources:
print " %s from %-10s (current local role is %s)" % (resource.device, resource.name, resource.getLocalRole())
print