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
404
405
406
|
#
# lvm.py
# lvm functions
#
# Copyright (C) 2009 Red Hat, Inc. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Author(s): Dave Lehman <dlehman@redhat.com>
#
import os
import math
import re
import iutil
from ..errors import *
from constants import *
import gettext
_ = lambda x: gettext.ldgettext("anaconda", x)
MAX_LV_SLOTS = 256
def has_lvm():
has_lvm = False
for path in os.environ["PATH"].split(":"):
if os.access("%s/lvm" % path, os.X_OK):
has_lvm = True
break
if has_lvm:
has_lvm = False
for line in open("/proc/devices").readlines():
if "device-mapper" in line.split():
has_lvm = True
break
return has_lvm
# Start config_args handling code
#
# Theoretically we can handle all that can be handled with the LVM --config
# argument. For every time we call an lvm_cc (lvm compose config) funciton
# we regenerate the config_args with all global info.
config_args = [] # Holds the final argument list
config_args_data = { "filterRejects": [], # regular expressions to reject.
"filterAccepts": [] } # regexp to accept
def _composeConfig():
"""lvm command accepts lvm.conf type arguments preceded by --config. """
global config_args, config_args_data
config_args = []
filter_string = ""
rejects = config_args_data["filterRejects"]
# we don't need the accept for now.
# accepts = config_args_data["filterAccepts"]
# if len(accepts) > 0:
# for i in range(len(rejects)):
# filter_string = filter_string + ("\"a|%s|\", " % accpets[i])
if len(rejects) > 0:
for i in range(len(rejects)):
filter_string = filter_string + ("\"r|%s|\"," % rejects[i])
filter_string = " filter=[%s] " % filter_string.strip(",")
# As we add config strings we should check them all.
if filter_string == "":
# Nothing was really done.
return
# devices_string can have (inside the brackets) "dir", "scan",
# "preferred_names", "filter", "cache_dir", "write_cache_state",
# "types", "sysfs_scan", "md_component_detection". see man lvm.conf.
devices_string = " devices {%s} " % (filter_string) # strings can be added
config_string = devices_string # more strings can be added.
config_args = ["--config", config_string]
def lvm_cc_addFilterRejectRegexp(regexp):
""" Add a regular expression to the --config string."""
global config_args_data
config_args_data["filterRejects"].append(regexp)
# compoes config once more.
_composeConfig()
# End config_args handling code.
# Names that should not be used int the creation of VGs
lvm_vg_blacklist = []
def blacklistVG(name):
global lvm_vg_blacklist
lvm_vg_blacklist.append(name)
def getPossiblePhysicalExtents(floor=0):
"""Returns a list of integers representing the possible values for
the physical extent of a volume group. Value is in KB.
floor - size (in KB) of smallest PE we care about.
"""
possiblePE = []
curpe = 8
while curpe <= 16384*1024:
if curpe >= floor:
possiblePE.append(curpe)
curpe = curpe * 2
return possiblePE
def getMaxLVSize():
""" Return the maximum size (in MB) of a logical volume. """
if iutil.getArch() in ("x86_64", "ppc64"): #64bit architectures
return (8*1024*1024*1024*1024) #Max is 8EiB (very large number..)
else:
return (16*1024*1024) #Max is 16TiB
def safeLvmName(name):
tmp = name.strip()
tmp = tmp.replace("/", "_")
tmp = re.sub("[^0-9a-zA-Z._]", "", tmp)
tmp = tmp.lstrip("_")
return tmp
def clampSize(size, pesize, roundup=None):
if roundup:
round = math.ceil
else:
round = math.floor
return long(round(float(size)/float(pesize)) * pesize)
def pvcreate(device):
args = ["pvcreate"] + \
config_args + \
[device]
rc = iutil.execWithRedirect("lvm", args,
stdout = "/dev/tty5",
stderr = "/dev/tty5",
searchPath=1)
if rc:
raise LVMError("pvcreate failed for %s" % device)
def pvresize(device, size):
args = ["pvresize"] + \
["--setphysicalvolumesize", ("%dm" % size)] + \
config_args + \
[device]
rc = iutil.execWithRedirect("lvm", args,
stdout = "/dev/tty5",
stderr = "/dev/tty5",
searchPath=1)
if rc:
raise LVMError("pvresize failed for %s" % device)
def pvremove(device):
args = ["pvremove"] + \
config_args + \
[device]
rc = iutil.execWithRedirect("lvm", args,
stdout = "/dev/tty5",
stderr = "/dev/tty5",
searchPath=1)
if rc:
raise LVMError("pvremove failed for %s" % device)
def pvinfo(device):
"""
If the PV was created with '--metadacopies 0', lvm will do some
scanning of devices to determine from their metadata which VG
this PV belongs to.
pvs -o pv_name,pv_mda_count,vg_name,vg_uuid --config \
'devices { scan = "/dev" filter = ["a/loop0/", "r/.*/"] }'
"""
#cfg = "'devices { scan = \"/dev\" filter = [\"a/%s/\", \"r/.*/\"] }'"
args = ["pvs", "--noheadings"] + \
["--units", "m"] + \
["-o", "pv_name,pv_mda_count,vg_name,vg_uuid"] + \
config_args + \
[device]
rc = iutil.execWithCapture("lvm", args,
stderr = "/dev/tty5")
vals = rc.split()
if not vals:
raise LVMError("pvinfo failed for %s" % device)
# don't raise an exception if pv is not a part of any vg
pv_name = vals[0]
try:
vg_name, vg_uuid = vals[2], vals[3]
except IndexError:
vg_name, vg_uuid = "", ""
info = {'pv_name': pv_name,
'vg_name': vg_name,
'vg_uuid': vg_uuid}
return info
def vgcreate(vg_name, pv_list, pe_size):
argv = ["vgcreate"]
if pe_size:
argv.extend(["-s", "%dm" % pe_size])
argv.extend(config_args)
argv.append(vg_name)
argv.extend(pv_list)
rc = iutil.execWithRedirect("lvm", argv,
stdout = "/dev/tty5",
stderr = "/dev/tty5",
searchPath=1)
if rc:
raise LVMError("vgcreate failed for %s" % vg_name)
def vgremove(vg_name):
args = ["vgremove"] + \
config_args +\
[vg_name]
rc = iutil.execWithRedirect("lvm", args,
stdout = "/dev/tty5",
stderr = "/dev/tty5",
searchPath=1)
if rc:
raise LVMError("vgremove failed for %s" % vg_name)
def vgactivate(vg_name):
args = ["vgchange", "-a", "y"] + \
config_args + \
[vg_name]
rc = iutil.execWithRedirect("lvm", args,
stdout = "/dev/tty5",
stderr = "/dev/tty5",
searchPath=1)
if rc:
raise LVMError("vgactivate failed for %s" % vg_name)
def vgdeactivate(vg_name):
args = ["vgchange", "-a", "n"] + \
config_args + \
[vg_name]
rc = iutil.execWithRedirect("lvm", args,
stdout = "/dev/tty5",
stderr = "/dev/tty5",
searchPath=1)
if rc:
raise LVMError("vgdeactivate failed for %s" % vg_name)
def vgreduce(vg_name, pv_list, rm=False):
""" Reduce a VG.
rm -> with RemoveMissing option.
Use pv_list when rm=False, otherwise ignore pv_list and call vgreduce with
the --removemissing option.
"""
args = ["vgreduce"]
if rm:
args.extend(["--removemissing", vg_name])
else:
args.extend([vg_name] + pv_list)
rc = iutil.execWithRedirect("lvm", args,
stdout = "/dev/tty5",
stderr = "/dev/tty5",
searchPath=1)
if rc:
raise LVMError("vgreduce failed for %s" % vg_name)
def vginfo(vg_name):
args = ["vgs", "--noheadings", "--nosuffix"] + \
["--units", "m"] + \
["-o", "uuid,size,free,extent_size,extent_count,free_count,pv_count"] + \
config_args + \
[vg_name]
buf = iutil.execWithCapture("lvm",
args,
stderr="/dev/tty5")
info = buf.split()
if len(info) != 7:
raise LVMError(_("vginfo failed for %s" % vg_name))
d = {}
(d['uuid'],d['size'],d['free'],d['pe_size'],
d['pe_count'],d['pe_free'],d['pv_count']) = info
return d
def lvs(vg_name):
args = ["lvs", "--noheadings", "--nosuffix"] + \
["--units", "m"] + \
["-o", "lv_name,lv_uuid,lv_size"] + \
config_args + \
[vg_name]
buf = iutil.execWithCapture("lvm",
args,
stderr="/dev/tty5")
lvs = {}
for line in buf.splitlines():
line = line.strip()
if not line:
continue
(name, uuid, size) = line.split()
lvs[name] = {"size": size,
"uuid": uuid}
if not lvs:
raise LVMError(_("lvs failed for %s" % vg_name))
return lvs
def lvcreate(vg_name, lv_name, size):
args = ["lvcreate"] + \
["-L", "%dm" % size] + \
["-n", lv_name] + \
config_args + \
[vg_name]
rc = iutil.execWithRedirect("lvm", args,
stdout = "/dev/tty5",
stderr = "/dev/tty5",
searchPath=1)
if rc:
raise LVMError("lvcreate failed for %s/%s" % (vg_name, lv_name))
def lvremove(vg_name, lv_name):
args = ["lvremove"] + \
config_args + \
["%s/%s" % (vg_name, lv_name)]
rc = iutil.execWithRedirect("lvm", args,
stdout = "/dev/tty5",
stderr = "/dev/tty5",
searchPath=1)
if rc:
raise LVMError("lvremove failed for %s" % lv_name)
def lvresize(vg_name, lv_name, size):
args = ["lvresize"] + \
["-L", "%dm" % size] + \
config_args + \
["%s/%s" % (vg_name, lv_name)]
rc = iutil.execWithRedirect("lvm", args,
stdout = "/dev/tty5",
stderr = "/dev/tty5",
searchPath=1)
if rc:
raise LVMError("lvresize failed for %s" % lv_name)
def lvactivate(vg_name, lv_name):
# see if lvchange accepts paths of the form 'mapper/$vg-$lv'
args = ["lvchange", "-a", "y"] + \
config_args + \
["%s/%s" % (vg_name, lv_name)]
rc = iutil.execWithRedirect("lvm", args,
stdout = "/dev/tty5",
stderr = "/dev/tty5",
searchPath=1)
if rc:
raise LVMError("lvactivate failed for %s" % lv_name)
def lvdeactivate(vg_name, lv_name):
args = ["lvchange", "-a", "n"] + \
config_args + \
["%s/%s" % (vg_name, lv_name)]
rc = iutil.execWithRedirect("lvm", args,
stdout = "/dev/tty5",
stderr = "/dev/tty5",
searchPath=1)
if rc:
raise LVMError("lvdeactivate failed for %s" % lv_name)
|