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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
|
#
# lvm.py - lvm probing control
#
# Copyright (C) 2002 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): Jeremy Katz <katzj@redhat.com>
#
import iutil
import os,sys
import string
import math
import isys
from flags import flags
import logging
log = logging.getLogger("anaconda")
from constants import *
MAX_LV_SLOTS=256
output = "/tmp/lvmout"
lvmDevicePresent = 0
from lvmErrors import *
def has_lvm():
global lvmDevicePresent
if not (os.access("/usr/sbin/lvm", os.X_OK) or
os.access("/sbin/lvm", os.X_OK)):
return
f = open("/proc/devices", "r")
lines = f.readlines()
f.close()
for line in lines:
try:
(dev, name) = line[:-1].split(' ', 2)
except:
continue
if name == "device-mapper":
lvmDevicePresent = 1
break
return lvmDevicePresent
# now check to see if lvm is available
has_lvm()
def lvmExec(*args):
try:
return iutil.execWithRedirect("lvm", args, stdout = output,
stderr = output, searchPath = 1)
except Exception, e:
log.error("error running lvm command: %s" %(e,))
raise LvmError, args[0]
def lvmCapture(*args):
try:
lvmout = iutil.execWithCapture("lvm", args, stderr = output)
lines = []
for line in lvmout.split("\n"):
lines.append(line.strip().split(':'))
return lines
except Exception, e:
log.error("error running lvm command: %s" %(e,))
raise LvmError, args[0]
def vgscan():
"""Runs vgscan."""
global lvmDevicePresent
if flags.test or lvmDevicePresent == 0:
return
rc = lvmExec("vgscan", "-v")
if rc:
log.error("running vgscan failed: %s" %(rc,))
# lvmDevicePresent = 0
def vgmknodes(volgroup=None):
# now make the device nodes
args = ["vgmknodes", "-v"]
if volgroup:
args.append(volgroup)
rc = lvmExec(*args)
if rc:
log.error("running vgmknodes failed: %s" %(rc,))
# lvmDevicePresent = 0
def vgcheckactive(volgroup = None):
"""Check if volume groups are active
volgroup - optional parameter to inquire about a specific volume group.
"""
global lvmDevicePresent
if flags.test or lvmDevicePresent == 0:
return False
args = ["lvs", "--noheadings", "--units", "b", "--nosuffix",
"--separator", ":", "--options", "vg_name,lv_name,attr"]
for line in lvmCapture(*args):
try:
(vg, lv, attr) = line
except:
continue
log.info("lv %s/%s, attr is %s" %(vg, lv, attr))
if attr.find("a") == -1:
continue
if volgroup is None or volgroup == vg:
return True
return False
def vgactivate(volgroup = None):
"""Activate volume groups by running vgchange -ay.
volgroup - optional single volume group to activate
"""
global lvmDevicePresent
if flags.test or lvmDevicePresent == 0:
return
args = ["vgchange", "-ay", "-v"]
if volgroup:
args.append(volgroup)
rc = lvmExec(*args)
if rc:
log.error("running vgchange failed: %s" %(rc,))
# lvmDevicePresent = 0
vgmknodes(volgroup)
def vgdeactivate(volgroup = None):
"""Deactivate volume groups by running vgchange -an.
volgroup - optional single volume group to deactivate
"""
global lvmDevicePresent
if flags.test or lvmDevicePresent == 0:
return
args = ["vgchange", "-an", "-v"]
if volgroup:
args.append(volgroup)
rc = lvmExec(*args)
if rc:
log.error("running vgchange failed: %s" %(rc,))
# lvmDevicePresent = 0
def lvcreate(lvname, vgname, size):
"""Creates a new logical volume.
lvname - name of logical volume to create.
vgname - name of volume group lv will be in.
size - size of lv, in megabytes.
"""
global lvmDevicePresent
if flags.test or lvmDevicePresent == 0:
return
writeForceConf()
vgscan()
args = ["lvcreate", "-v", "-L", "%dM" %(size,), "-n", lvname, "-An", vgname]
try:
rc = lvmExec(*args)
except:
rc = 1
if rc:
raise LVCreateError(vgname, lvname, size)
unlinkConf()
def lvremove(lvname, vgname):
"""Removes a logical volume.
lvname - name of logical volume to remove.
vgname - name of volume group lv is in.
"""
global lvmDevicePresent
if flags.test or lvmDevicePresent == 0:
return
args = ["lvremove", "-f", "-v"]
dev = "/dev/%s/%s" %(vgname, lvname)
args.append(dev)
try:
rc = lvmExec(*args)
except:
rc = 1
if rc:
raise LVRemoveError(vgname, lvname)
def lvresize(lvname, vgname, size):
global lvmDevicePresent
if flags.test or lvmDevicePresent == 0:
return
args = ["lvresize", "-An", "-L", "%dM" %(size,), "-v",
"/dev/%s/%s" %(vgname, lvname,)]
try:
rc = lvmExec(*args)
except:
rc = 1
if rc:
raise LVResizeError(vgname, lvname)
def vgcreate(vgname, PESize, nodes):
"""Creates a new volume group."
vgname - name of volume group to create.
PESize - Physical Extent size, in kilobytes.
nodes - LVM Physical Volumes on which to put the new VG.
"""
global lvmDevicePresent
if flags.test or lvmDevicePresent == 0:
return
# rescan now that we've recreated pvs. ugh.
writeForceConf()
vgscan()
args = ["vgcreate", "-v", "-An", "-s", "%sk" % (PESize,), vgname ]
args.extend(nodes)
try:
rc = lvmExec(*args)
except:
rc = 1
if rc:
raise VGCreateError(vgname, PESize, nodes)
unlinkConf()
def vgremove(vgname):
"""Removes a volume group. Deactivates the volume group first
vgname - name of volume group.
"""
global lvmDevicePresent
if flags.test or lvmDevicePresent == 0:
return
# find the Physical Volumes which make up this Volume Group, so we
# can prune and recreate them.
pvs = []
for pv in pvlist():
if pv[1] == vgname:
pvs.append(pv[0])
# we'll try to deactivate... if it fails, we'll probably fail on
# the removal too... but it's worth a shot
try:
vgdeactivate(vgname)
except:
pass
args = ["vgremove", "-v", vgname]
log.info(string.join(args, ' '))
try:
rc = lvmExec(*args)
except:
rc = 1
if rc:
raise VGRemoveError, vgname
# now iterate all the PVs we've just freed up, so we reclaim the metadata
# space. This is an LVM bug, AFAICS.
for pvname in pvs:
args = ["pvremove", "-ff", "-y", "-v", pvname]
log.info(string.join(args, ' '))
try:
rc = lvmExec(*args)
except:
rc = 1
if rc:
raise PVRemoveError, pvname
args = ["pvcreate", "-ff", "-y", "-v", pvname]
log.info(string.join(args, ' '))
try:
rc = lvmExec(*args)
except:
rc = 1
if rc:
raise PVCreateError, pvname
wipeOtherMetadataFromPV(pvname)
def pvcreate(node):
"""Initializes a new Physical Volume."
node - path to device node on which to create the new PV."
"""
global lvmDevicePresent
if flags.test or lvmDevicePresent == 0:
return
# rescan now that we've recreated pvs. ugh.
writeForceConf()
args = ["pvcreate", "-ff", "-y", "-v", node ]
try:
rc = lvmExec(*args)
except:
rc = 1
if rc:
raise PVCreateError(node)
unlinkConf()
wipeOtherMetadataFromPV(node)
def lvlist():
global lvmDevicePresent
if lvmDevicePresent == 0:
return []
lvs = []
# field names for "options" are in LVM2.2.01.01/lib/report/columns.h
args = ["lvdisplay", "-C", "--noheadings", "--units", "b",
"--nosuffix", "--separator", ":", "--options",
"vg_name,lv_name,lv_size,origin"
]
lvscanout = iutil.execWithCapture("lvm", args, stderr = "/dev/tty6")
for line in lvmCapture(*args):
try:
(vg, lv, size, origin) = line
size = long(math.floor(long(size) / (1024 * 1024)))
if origin == '':
origin = None
except:
continue
logmsg = "lv is %s/%s, size of %s" % (vg, lv, size)
if origin:
logmsg += ", snapshot from %s" % (origin,)
log.info(logmsg)
lvs.append( (vg, lv, size, origin) )
return lvs
def pvlist():
global lvmDevicePresent
if lvmDevicePresent == 0:
return []
pvs = []
args = ["pvdisplay", "-C", "--noheadings", "--units", "b",
"--nosuffix", "--separator", ":", "--options",
"pv_name,vg_name,dev_size"
]
for line in lvmCapture(*args):
try:
(dev, vg, size) = line
size = long(math.floor(long(size) / (1024 * 1024)))
except:
continue
log.info("pv is %s in vg %s, size is %s" %(dev, vg, size))
pvs.append( (dev, vg, size) )
return pvs
def vglist():
global lvmDevicePresent
if lvmDevicePresent == 0:
return []
vgs = []
args = ["vgdisplay", "-C", "--noheadings", "--units", "b",
"--nosuffix", "--separator", ":", "--options",
"vg_name,vg_size,vg_extent_size,vg_free"
]
for line in lvmCapture(*args):
try:
(vg, size, pesize, free) = line
size = long(math.floor(long(size) / (1024 * 1024)))
pesize = long(pesize)/1024
free = math.floor(long(free) / (1024 * 1024))
except:
continue
log.info("vg %s, size is %s, pesize is %s" %(vg, size, pesize))
vgs.append( (vg, size, pesize, free) )
return vgs
def partialvgs():
global lvmDevicePresent
if lvmDevicePresent == 0:
return []
vgs = []
args = ["vgdisplay", "-C", "-P", "--noheadings", "--units", "b",
"--nosuffix", "--separator", ":"]
for line in lvmCapture(*args):
try:
(vg, numpv, numlv, numsn, attr, size, free) = line
except:
continue
if attr.find("p") != -1:
log.info("vg %s, attr is %s" %(vg, attr))
vgs.append(vg)
return vgs
# FIXME: this is a hack. we really need to have a --force option.
def unlinkConf():
lvmroot = "/etc/lvm"
if os.path.exists("%s/lvm.conf" %(lvmroot,)):
os.unlink("%s/lvm.conf" %(lvmroot,))
def writeForceConf():
"""Write out an /etc/lvm/lvm.conf that doesn't do much (any?) filtering"""
lvmroot = "/etc/lvm"
try:
os.unlink("/etc/lvm/.cache")
except:
pass
if not os.path.isdir(lvmroot):
os.mkdir(lvmroot)
unlinkConf()
f = open("%s/lvm.conf" %(lvmroot,), "w+")
f.write("""
# anaconda hacked lvm.conf to avoid filtering breaking things
devices {
sysfs_scan = 0
md_component_detection = 1
}
""")
# FIXME: another hack. we need to wipe the raid metadata since pvcreate
# doesn't
def wipeOtherMetadataFromPV(node):
try:
isys.wipeRaidSB(node)
except Exception, e:
log.critical("error wiping raidsb from %s: %s", node, e)
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 clampLVSizeRequest(size, pe, roundup=0):
"""Given a size and a PE, returns the actual size of logical volumne.
size - size (in MB) of logical volume request
pe - PE size (in KB)
roundup - round sizes up or not
"""
if roundup:
func = math.ceil
else:
func = math.floor
return (long(func((size*1024L)/pe))*pe)/1024
def clampPVSize(pvsize, pesize):
"""Given a PV size and a PE, returns the usable space of the PV.
Takes into account both overhead of the physical volume and 'clamping'
to the PE size.
pvsize - size (in MB) of PV request
pesize - PE size (in KB)
"""
# we want Kbytes as a float for our math
pvsize *= 1024.0
return long((math.floor(pvsize / pesize) * pesize) / 1024)
def getMaxLVSize(pe):
"""Given a PE size in KB, returns maximum size (in MB) of a logical volume.
pe - PE size in KB
"""
if os.uname()[2][:4]=="2.4.":
return pe*64 #2.4 kernel - LVM1, max size is 2TiB and depends on extent size/count
else: #newer kernel - LVM2, size limited by number of sectors
if productArch 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 createSuggestedVGName(partitions):
"""Given list of partition requests, come up with a reasonable VG name
partitions - list of requests
"""
i = 0
while 1:
tmpname = "VolGroup%02d" % (i,)
if not partitions.isVolumeGroupNameInUse(tmpname):
break
i = i + 1
if i>99:
tmpname = ""
return tmpname
def createSuggestedLVName(logreqs):
"""Given list of LV requests, come up with a reasonable LV name
partitions - list of LV requests for this VG
"""
i = 0
lnames = []
for lv in logreqs:
lnames.append(lv.logicalVolumeName)
while 1:
tmpname = "LogVol%02d" % (i,)
if (logreqs is None) or (tmpname not in lnames):
break
i = i + 1
if i>99:
tmpname = ""
return tmpname
def getVGUsedSpace(vgreq, requests, diskset):
vgused = 0
for request in requests.requests:
if request.type == REQUEST_LV and request.volumeGroup == vgreq.uniqueID:
size = int(request.getActualSize(requests, diskset))
vgused = vgused + size
return vgused
def getVGFreeSpace(vgreq, requests, diskset):
used = getVGUsedSpace(vgreq, requests, diskset)
log.debug("used space is %s" % (used,))
total = vgreq.getActualSize(requests, diskset)
log.debug("actual space is %s" % (total,))
return total - used
|