1 |
gordonr |
1.1 |
#!/usr/bin/python |
2 |
|
|
# |
3 |
|
|
# raid.py - raid probing control |
4 |
|
|
# |
5 |
|
|
# Erik Troan <ewt@redhat.com> |
6 |
|
|
# |
7 |
|
|
# Copyright 1999-2002 Red Hat, Inc. |
8 |
|
|
# |
9 |
|
|
# This software may be freely redistributed under the terms of the GNU |
10 |
|
|
# library public license. |
11 |
|
|
# |
12 |
|
|
# You should have received a copy of the GNU Library Public License |
13 |
|
|
# along with this program; if not, write to the Free Software |
14 |
|
|
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
15 |
|
|
# |
16 |
|
|
"""Raid probing control.""" |
17 |
|
|
|
18 |
|
|
import parted |
19 |
|
|
import isys |
20 |
|
|
import os |
21 |
|
|
import partitioning |
22 |
|
|
import partedUtils |
23 |
|
|
|
24 |
|
|
from rhpl.log import log |
25 |
|
|
|
26 |
|
|
# these arches can have their /boot on RAID and not have their |
27 |
|
|
# boot loader blow up |
28 |
|
|
raidBootArches = [ "i386", "x86_64" ] |
29 |
|
|
|
30 |
|
|
def scanForRaid(drives): |
31 |
|
|
"""Scans for raid devices on drives. |
32 |
|
|
|
33 |
|
|
drives is a list of device names. |
34 |
|
|
Returns a list of (mdMinor, devices, level, totalDisks) tuples. |
35 |
|
|
""" |
36 |
|
|
|
37 |
|
|
raidSets = {} |
38 |
|
|
raidDevices = {} |
39 |
|
|
|
40 |
|
|
for d in drives: |
41 |
|
|
parts = [] |
42 |
|
|
isys.makeDevInode(d, "/tmp/" + d) |
43 |
|
|
try: |
44 |
|
|
dev = parted.PedDevice.get("/tmp/" + d) |
45 |
|
|
disk = parted.PedDisk.new(dev) |
46 |
|
|
|
47 |
|
|
raidParts = partedUtils.get_raid_partitions(disk) |
48 |
|
|
for part in raidParts: |
49 |
|
|
parts.append(partedUtils.get_partition_name(part)) |
50 |
|
|
except: |
51 |
|
|
pass |
52 |
|
|
|
53 |
|
|
os.remove("/tmp/" + d) |
54 |
|
|
for dev in parts: |
55 |
|
|
try: |
56 |
|
|
(major, minor, raidSet, level, nrDisks, totalDisks, mdMinor) =\ |
57 |
|
|
isys.raidsb(dev) |
58 |
|
|
except ValueError: |
59 |
|
|
# bad magic, this can't be part of our raid set |
60 |
|
|
log("reading raid sb failed for %s",dev) |
61 |
|
|
continue |
62 |
|
|
|
63 |
|
|
if raidSets.has_key(raidSet): |
64 |
|
|
(knownLevel, knownDisks, knownMinor, knownDevices) = \ |
65 |
|
|
raidSets[raidSet] |
66 |
|
|
if knownLevel != level or knownDisks != totalDisks or \ |
67 |
|
|
knownMinor != mdMinor: |
68 |
|
|
# Raise hell |
69 |
|
|
log("raid set inconsistency for md%d: " |
70 |
|
|
"all drives in this raid set do not " |
71 |
|
|
"agree on raid parameters. Skipping raid device", |
72 |
|
|
mdMinor) |
73 |
|
|
continue |
74 |
|
|
knownDevices.append(dev) |
75 |
|
|
raidSets[raidSet] = (knownLevel, knownDisks, knownMinor, |
76 |
|
|
knownDevices) |
77 |
|
|
else: |
78 |
|
|
raidSets[raidSet] = (level, totalDisks, mdMinor, [dev,]) |
79 |
|
|
|
80 |
|
|
if raidDevices.has_key(mdMinor): |
81 |
|
|
if (raidDevices[mdMinor] != raidSet): |
82 |
|
|
log("raid set inconsistency for md%d: " |
83 |
|
|
"found members of multiple raid sets " |
84 |
|
|
"that claim to be md%d. Using only the first " |
85 |
|
|
"array found.", mdMinor, mdMinor) |
86 |
|
|
continue |
87 |
|
|
else: |
88 |
|
|
raidDevices[mdMinor] = raidSet |
89 |
|
|
|
90 |
|
|
raidList = [] |
91 |
|
|
for key in raidSets.keys(): |
92 |
|
|
(level, totalDisks, mdMinor, devices) = raidSets[key] |
93 |
|
|
if len(devices) < totalDisks: |
94 |
|
|
log("missing components of raid device md%d. The " |
95 |
|
|
"raid device needs %d drive(s) and only %d (was/were) found. " |
96 |
|
|
"This raid device will be started in degraded mode.", mdMinor, |
97 |
|
|
totalDisks, len(devices)) |
98 |
|
|
# continue |
99 |
|
|
raidList.append((mdMinor, devices, level, totalDisks)) |
100 |
|
|
|
101 |
|
|
return raidList |
102 |
|
|
|
103 |
|
|
def startAllRaid(driveList): |
104 |
|
|
"""Do a raid start on raid devices and return a list like scanForRaid.""" |
105 |
|
|
rc = [] |
106 |
|
|
mdList = scanForRaid(driveList) |
107 |
|
|
for mdDevice, deviceList, level, numActive in mdList: |
108 |
|
|
devName = "md%d" % (mdDevice,) |
109 |
|
|
isys.raidstart(devName, deviceList[0]) |
110 |
|
|
rc.append((devName, deviceList, level, numActive)) |
111 |
|
|
return rc |
112 |
|
|
|
113 |
|
|
def stopAllRaid(mdList): |
114 |
|
|
"""Do a raid stop on each of the raid device tuples given.""" |
115 |
|
|
for dev, devices, level, numActive in mdList: |
116 |
|
|
isys.raidstop(dev) |
117 |
|
|
|
118 |
|
|
def isRaid6(raidlevel): |
119 |
|
|
"""Return whether raidlevel is a valid descriptor of RAID6.""" |
120 |
|
|
if raidlevel == "RAID6": |
121 |
|
|
return 1 |
122 |
|
|
elif raidlevel == 6: |
123 |
|
|
return 1 |
124 |
|
|
elif raidlevel == "6": |
125 |
|
|
return 1 |
126 |
|
|
return 0 |
127 |
|
|
|
128 |
|
|
def isRaid5(raidlevel): |
129 |
|
|
"""Return whether raidlevel is a valid descriptor of RAID5.""" |
130 |
|
|
if raidlevel == "RAID5": |
131 |
|
|
return 1 |
132 |
|
|
elif raidlevel == 5: |
133 |
|
|
return 1 |
134 |
|
|
elif raidlevel == "5": |
135 |
|
|
return 1 |
136 |
|
|
return 0 |
137 |
|
|
|
138 |
|
|
def isRaid1(raidlevel): |
139 |
|
|
"""Return whether raidlevel is a valid descriptor of RAID1.""" |
140 |
|
|
if raidlevel == "RAID1": |
141 |
|
|
return 1 |
142 |
|
|
elif raidlevel == 1: |
143 |
|
|
return 1 |
144 |
|
|
elif raidlevel == "1": |
145 |
|
|
return 1 |
146 |
|
|
return 0 |
147 |
|
|
|
148 |
|
|
def isRaid0(raidlevel): |
149 |
|
|
"""Return whether raidlevel is a valid descriptor of RAID0.""" |
150 |
|
|
if raidlevel == "RAID0": |
151 |
|
|
return 1 |
152 |
|
|
elif raidlevel == 0: |
153 |
|
|
return 1 |
154 |
|
|
elif raidlevel == "0": |
155 |
|
|
return 1 |
156 |
|
|
return 0 |
157 |
|
|
|
158 |
|
|
def get_raid_min_members(raidlevel): |
159 |
|
|
"""Return the minimum number of raid members required for raid level""" |
160 |
|
|
if isRaid0(raidlevel): |
161 |
|
|
return 2 |
162 |
|
|
elif isRaid1(raidlevel): |
163 |
|
|
return 1 |
164 |
|
|
elif isRaid5(raidlevel): |
165 |
|
|
return 3 |
166 |
|
|
elif isRaid6(raidlevel): |
167 |
|
|
return 4 |
168 |
|
|
else: |
169 |
|
|
raise ValueError, "invalid raidlevel in get_raid_min_members" |
170 |
|
|
|
171 |
|
|
def get_raid_max_spares(raidlevel, nummembers): |
172 |
|
|
"""Return the maximum number of raid spares for raidlevel.""" |
173 |
|
|
if isRaid0(raidlevel): |
174 |
|
|
return 0 |
175 |
|
|
elif isRaid1(raidlevel) or isRaid5(raidlevel) or isRaid6(raidlevel): |
176 |
|
|
return max(0, nummembers - get_raid_min_members(raidlevel)) |
177 |
|
|
else: |
178 |
|
|
raise ValueError, "invalid raidlevel in get_raid_max_spares" |
179 |
|
|
|
180 |
|
|
def register_raid_device(mdname, newdevices, newlevel, newnumActive): |
181 |
|
|
"""Register a new RAID device in the mdlist.""" |
182 |
|
|
for dev, devices, level, numActive in partedUtils.DiskSet.mdList: |
183 |
|
|
if mdname == dev: |
184 |
|
|
if (devices != newdevices or level != newlevel or |
185 |
|
|
numActive != newnumActive): |
186 |
|
|
raise ValueError, "%s is already in the mdList!" % (mdname,) |
187 |
|
|
else: |
188 |
|
|
return |
189 |
|
|
partedUtils.DiskSet.mdList.append((mdname, newdevices[:], newlevel, |
190 |
|
|
newnumActive)) |
191 |
|
|
|
192 |
|
|
def lookup_raid_device(mdname): |
193 |
|
|
"""Return the requested RAID device information.""" |
194 |
|
|
for dev, devices, level, numActive in partedUtils.DiskSet.mdList: |
195 |
|
|
if mdname == dev: |
196 |
|
|
return (dev, devices, level, numActive) |
197 |
|
|
raise KeyError, "md device not found" |
198 |
|
|
|
199 |
|
|
def getRaidLevels(): |
200 |
|
|
avail = [] |
201 |
|
|
try: |
202 |
|
|
f = open("/proc/mdstat", "r") |
203 |
|
|
except: |
204 |
|
|
pass |
205 |
|
|
else: |
206 |
|
|
for l in f.readlines(): |
207 |
|
|
if not l.startswith("Personalities"): |
208 |
|
|
continue |
209 |
|
|
for tok in l.split(): |
210 |
|
|
for lev in ("RAID0", "RAID1", "RAID5", "RAID6"): |
211 |
|
|
if tok.upper().find(lev) != -1: |
212 |
|
|
avail.append(lev) |
213 |
|
|
|
214 |
|
|
f.close() |
215 |
|
|
|
216 |
|
|
return avail |
217 |
|
|
|
218 |
|
|
|
219 |
|
|
|