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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
#!/usr/bin/python
# Copyright (C) 2011 Gluster, Inc. <http://www.gluster.com>
# This file is part of Gluster Management Gateway.
#
import os
import sys
p1 = os.path.abspath(os.path.dirname(sys.argv[0]))
p2 = "%s/common" % os.path.dirname(p1)
if not p1 in sys.path:
sys.path.append(p1)
if not p2 in sys.path:
sys.path.append(p2)
import glob
from copy import deepcopy
import dbus
import Globals
import time
import Utils
import Disk
import Protocol
import FsTabUtils
ONE_MB_SIZE = 1048576
def _stripDev(device):
if Utils.isString(device) and device.startswith("/dev/"):
return device[5:]
return device
def _addDev(deviceName):
if Utils.isString(deviceName) and not deviceName.startswith("/dev/"):
return "/dev/" + deviceName
return deviceName
def getDeviceName(device):
if type(device) == type([]):
nameList = []
for d in device:
nameList.append(_stripDev(d))
return nameList
return _stripDev(device)
def getDevice(deviceName):
if Utils.isString(deviceName):
return _addDev(deviceName)
if type(deviceName) == type([]):
nameList = []
for d in deviceName:
nameList.append(_addDev(d))
return nameList
return _addDev(deviceName)
def getDiskPartitionByUuid(uuid):
uuidFile = "/dev/disk/by-uuid/%s" % uuid
if os.path.exists(uuidFile):
return getDeviceName(os.path.realpath(uuidFile))
return None
def getUuidByDiskPartition(device):
for uuidFile in glob.glob("/dev/disk/by-uuid/*"):
if os.path.realpath(uuidFile) == device:
return os.path.basename(uuidFile)
return None
def getDiskPartitionUuid(partition):
Utils.log("WARNING: getDiskPartitionUuid() is deprecated by getUuidByDiskPartition()")
return getUuidByDiskPartition(partition)
def getDiskPartitionByLabel(label):
## TODO: Finding needs to be enhanced
labelFile = "/dev/disk/by-label/%s" % label
if os.path.exists(labelFile):
if os.path.islink(labelFile):
return getDeviceName(os.path.realpath(labelFile))
return None
def getDeviceByLabel(label):
Utils.log("WARNING: getDeviceByLabel() is deprecated by getDiskPartitionByLabel()")
return getDiskPartitionByLabel(label)
def getDiskPartitionLabel(device):
rv = Utils.runCommand("e2label %s" % device, output=True, root=True)
if rv["Status"] == 0:
return rv["Stdout"].strip()
return False
def getRootPartition(fsTabFile=Globals.FSTAB_FILE):
fsTabEntryList = FsTabUtils.readFsTab(fsTabFile)
for fsTabEntry in fsTabEntryList:
if fsTabEntry["MountPoint"] == "/":
if fsTabEntry["Device"].startswith("UUID="):
return getDiskPartitionByUuid(fsTabEntry["Device"].split("UUID=")[-1])
if fsTabEntry["Device"].startswith("LABEL="):
partitionName = getDiskPartitionByLabel(fsTabEntry["Device"].split("LABEL=")[-1])
if partitionName:
return partitionName
return getDeviceName(fsTabEntry["Device"])
return None
def getRaidDisk():
array = []
arrayList = []
mdFound = False
try:
fp = open("/proc/mdstat")
for line in fp:
str = line.strip()
if str.startswith("md"):
array.append(str)
mdFound = True
continue
if mdFound:
if str:
array.append(str)
else:
arrayList.append(array)
array = []
mdFound = False
fp.close()
except IOError, e:
return None
raidList = {}
for array in arrayList:
raid = {}
tokens = array[0].split()
raid['Interface'] = tokens[3]
device = getDevice(tokens[0])
raid['MountPoint'] = getDeviceMountPoint(device)
if raid['MountPoint']:
raid['Type'] = "DATA"
raid['SpaceInUse'] = getDeviceUsedSpace(device)
else:
raid['SpaceInUse'] = None
rv = Utils.runCommand("blkid -c /dev/null %s" % (device), output=True, root=True)
raid['Uuid'] = None
raid['FsType'] = None
raid['Status'] = "UNINITIALIZED"
if isDiskInFormatting(device):
raid['Status'] = "INITIALIZING"
if not rv["Stderr"]:
words = rv["Stdout"].strip().split()
if words:
raid['Status'] = "INITIALIZED"
if len(words) > 2:
raid['Uuid'] = words[1].split("UUID=")[-1].split('"')[1]
raid['FsType'] = words[2].split("TYPE=")[-1].split('"')[1]
raid['Disks'] = [x.split('[')[0] for x in tokens[4:]]
raid['Size'] = float(array[1].split()[0]) / 1024.0
raidList[tokens[0]] = raid
return raidList
def getOsDisk():
Utils.log("WARNING: getOsDisk() is deprecated by getRootPartition()")
return getRootPartition()
def getDiskInfo(diskNameList=None):
procPartitionsDict = getProcPartitions()
diskDict = {}
for name, values in procPartitionsDict.iteritems():
values["Description"] = None
values["Uuid"] = None
values["FsType"] = None
values["MountPoint"] = None
values["SpaceInUse"] = None
values["Member"] = None
## extras ?!?!
values["Init"] = False
values["Status"] = None
values["Interface"] = None
values["DriveType"] = None
values["Type"] = None
values["FsVersion"] = None
values["ReadOnlyAccess"] = None
device = getDevice(name)
values["Uuid"] = getUuidByDiskPartition(device)
rv = Utils.runCommand("blkid -c /dev/null -o value %s" % device, output=True, root=True)
if rv["Status"] == 0:
lines = rv["Stdout"].strip().split("\n")
values["FsType"] = lines[-1].strip()
values["MountPoint"] = getDeviceMountPoint(device)
if values["FsType"]:
values["Init"] = True
if values["MountPoint"]:
rv = Utils.runCommand(["df", values["MountPoint"]], output=True)
if rv["Status"] == 0:
try:
values["SpaceInUse"] = long(rv["Stdout"].split("\n")[1].split()[2])
except IndexError, e:
pass
except ValueError, e:
pass
if os.path.isdir("/sys/block/%s" % name):
model = Utils.readFile("/sys/block/%s/device/model" % name)
vendor = Utils.readFile("/sys/block/%s/device/vendor" % name)
values["Description"] = "%s %s" % (model.strip(), vendor.strip())
values["Partitions"] = {}
diskDict[name] = values
for diskName in diskDict.keys():
del procPartitionsDict[diskName]
for partName, values in procPartitionsDict.iteritems():
if os.path.isdir("/sys/block/%s/%s" % (diskName, partName)):
diskDict[diskName]["Partitions"][partName] = values
procMdstatDict = getProcMdstat()
for name, values in procMdstatDict.iteritems():
try:
diskDict[name]["Description"] = "Software Raid Array - %s - %s" % (values["Type"], values["Status"])
diskDict[name]["Member"] = values["Member"]
except KeyError, e:
pass
diskNameList = getDeviceName(diskNameList)
if Utils.isString(diskNameList):
diskNameList = [diskNameList]
if not diskNameList:
return diskDict
outputDict = {}
for diskName in list(set(diskDict.keys()).intersection(set(diskNameList))):
outputDict[diskName] = diskDict[diskName]
return outputDict
def getDiskList(diskDeviceList=None):
return diskInfo["disks"]
def checkDiskMountPoint(diskMountPoint):
try:
fstabEntries = open(Globals.FSTAB_FILE).readlines()
except IOError, e:
fstabEntries = []
Utils.log("failed to read file %s: %s" % (Globals.FSTAB_FILE, str(e)))
found = False
for entry in fstabEntries:
entry = entry.strip()
if not entry:
continue
entries = entry.split()
if entries and len(entries) > 1 and entries[0].startswith("UUID=") and entries[1].upper() == diskMountPoint.upper():
return True
return False
def getMountPointByUuid(partitionUuid):
# check uuid in etc/fstab
try:
fstabEntries = open(Globals.FSTAB_FILE).readlines()
except IOError, e:
fstabEntries = []
Utils.log("failed to read file %s: %s" % (Globals.FSTAB_FILE, str(e)))
found = False
for entry in fstabEntries:
entry = entry.strip()
if not entry:
continue
if entry.split()[0] == "UUID=" + partitionUuid:
return entry.split()[1]
return None
def getDeviceUsedSpace(device):
rv = Utils.runCommand("df -kl %s" % (device), output=True, root=True)
if rv["Status"] == 0:
try:
return long(rv["Stdout"].split("\n")[1].split()[2]) / 1024
except IndexError, e:
pass
except ValueError, e:
pass
def getDiskSizeInfo(partition):
# get values from df output
total = None
used = None
free = None
command = "df -kl -t ext3 -t ext4 -t xfs"
rv = Utils.runCommand(command, output=True, root=True)
message = Utils.stripEmptyLines(rv["Stdout"])
if rv["Status"] != 0:
Utils.log("failed to get disk partition details")
return None, None, None
for line in rv["Stdout"].split("\n"):
tokens = line.split()
if len(tokens) < 4:
continue
if tokens[0] == partition:
total = int(tokens[1]) / 1024.0
used = int(tokens[2]) / 1024.0
free = int(tokens[3]) / 1024.0
break
if total:
return total, used, free
# get total size from parted output
for i in range(len(partition), 0, -1):
pos = i - 1
if not partition[pos].isdigit():
break
disk = partition[:pos+1]
partitionNumber = partition[pos+1:]
if not partitionNumber.isdigit():
return None, None, None
number = int(partitionNumber)
command = "parted -ms %s unit kb print" % disk
rv = Utils.runCommand(command, output=True, root=True)
if rv["Status"] != 0:
Utils.log("failed to get disk partition details")
return None, None, None
lines = rv["Stdout"].split(";\n")
if len(lines) < 3:
return None,None,None
for line in lines[2:]:
tokens = line.split(':')
if len(tokens) < 4:
continue
if tokens[0] == str(number):
total = int(tokens[3].split('kB')[0]) / 1024.0
break
return total, used, free
def isDataDiskPartitionFormatted(device):
#if getDiskPartitionLabel(device) != Globals.DATA_PARTITION_LABEL:
# return False
device = getDeviceName(device)
diskObj = Disk.Disk()
for disk in diskObj.getMountableDiskList():
if disk['device'].upper() == device.upper():
mountPoint = disk['mount_point']
if not mountPoint:
return False
if not os.path.exists(mountPoint):
return False
uuid = getUuidByDiskPartition(device)
if not uuid:
return False
for fsTabEntry in FsTabUtils.readFsTab():
if fsTabEntry["Device"] == ("UUID=%s" % uuid) and fsTabEntry["MountPoint"] == mountPoint:
return True
return False
def isDiskInFormatting(device):
DEVICE_FORMAT_LOCK_FILE = "/var/lock/%s.lock" % device
return os.path.exists(DEVICE_FORMAT_LOCK_FILE)
def isDiskInFormat(device):
Utils.log("WARNING: isDiskInFormat() is deprecated by isDataDiskPartitionFormatted()")
return isDataDiskPartitionFormatted(device)
def getDeviceMountPoint(device):
lines = Utils.readFile("/proc/mounts", lines=True)
uuid = getUuidByDiskPartition(device)
for line in lines:
tokens = line.split()
if tokens[0] == device or (uuid and tokens[0].endswith(uuid)):
return tokens[1]
return None
def getProcPartitions():
procPartitionsDict = {}
s = Utils.readFile("/proc/partitions", lines=True)
for line in s[2:]:
tokens = line.strip().split()
procPartitionsDict[tokens[3]] = {"Size" : long(tokens[2])}
return procPartitionsDict
def getProcMdstat():
raidArrayDict = {}
lines = Utils.readFile("/proc/mdstat", lines=True)
for line in lines[1:]:
tokens = line.strip().split()
if not tokens:
continue
if tokens[0].startswith("md"):
raidArrayDict[tokens[0]] = {"Status" : tokens[2], "Type" : tokens[3], "Member" : [token.split('[')[0] for token in tokens[4:]]}
return raidArrayDict
|