]> git.ipfire.org Git - people/ms/ipfire-3.x.git/blob - pkgs/core/pomona/src/storage/devicelibs/lvm.py
20fa02ac2559c7eceb13906d7109f2e3c6f73178
[people/ms/ipfire-3.x.git] / pkgs / core / pomona / src / storage / devicelibs / lvm.py
1 #!/usr/bin/python
2
3 import os
4 import re
5
6 import util
7
8 MAX_LV_SLOTS = 256
9
10 def has_lvm():
11 has_lvm = False
12 for path in os.environ["PATH"].split(":"):
13 if os.access("%s/lvm" % path, os.X_OK):
14 has_lvm = True
15 break
16
17 if has_lvm:
18 has_lvm = False
19 for line in open("/proc/devices").readlines():
20 if "device-mapper" in line.split():
21 has_lvm = True
22 break
23
24 return has_lvm
25
26 # Start config_args handling code
27 #
28 # Theoretically we can handle all that can be handled with the LVM --config
29 # argument. For every time we call an lvm_cc (lvm compose config) funciton
30 # we regenerate the config_args with all global info.
31 config_args = [] # Holds the final argument list
32 config_args_data = { "filterRejects": [], # regular expressions to reject.
33 "filterAccepts": [] } # regexp to accept
34
35 def _composeConfig():
36 """lvm command accepts lvm.conf type arguments preceded by --config. """
37 global config_args, config_args_data
38 config_args = []
39
40 filter_string = ""
41 rejects = config_args_data["filterRejects"]
42 # we don't need the accept for now.
43 # accepts = config_args_data["filterAccepts"]
44 # if len(accepts) > 0:
45 # for i in range(len(rejects)):
46 # filter_string = filter_string + ("\"a|%s|\", " % accpets[i])
47
48 if len(rejects) > 0:
49 for i in range(len(rejects)):
50 filter_string = filter_string + ("\"r|%s|\"," % rejects[i])
51
52 filter_string = " filter=[%s] " % filter_string.strip(",")
53
54 # As we add config strings we should check them all.
55 if filter_string == "":
56 # Nothing was really done.
57 return
58
59 # devices_string can have (inside the brackets) "dir", "scan",
60 # "preferred_names", "filter", "cache_dir", "write_cache_state",
61 # "types", "sysfs_scan", "md_component_detection". see man lvm.conf.
62 devices_string = " devices {%s} " % (filter_string) # strings can be added
63 config_string = devices_string # more strings can be added.
64 config_args = ["--config", config_string]
65
66 def lvm_cc_addFilterRejectRegexp(regexp):
67 """ Add a regular expression to the --config string."""
68 global config_args_data
69 config_args_data["filterRejects"].append(regexp)
70
71 # compoes config once more.
72 _composeConfig()
73
74 def lvm_cc_resetFilter():
75 global config_args_data
76 config_args_data["filterRejects"] = []
77 config_args_data["filterAccepts"] = []
78 # End config_args handling code.
79
80 # Names that should not be used int the creation of VGs
81 lvm_vg_blacklist = []
82 def blacklistVG(name):
83 global lvm_vg_blacklist
84 lvm_vg_blacklist.append(name)
85
86 def getPossiblePhysicalExtents(floor=0):
87 """Returns a list of integers representing the possible values for
88 the physical extent of a volume group. Value is in KB.
89
90 floor - size (in KB) of smallest PE we care about.
91 """
92
93 possiblePE = []
94 curpe = 8
95 while curpe <= 16384*1024:
96 if curpe >= floor:
97 possiblePE.append(curpe)
98 curpe = curpe * 2
99
100 return possiblePE
101
102 def getMaxLVSize():
103 """ Return the maximum size (in MB) of a logical volume. """
104 if util.getArch() in ("x86_64",): #64bit architectures
105 return (8*1024*1024*1024*1024) #Max is 8EiB (very large number..)
106 else:
107 return (16*1024*1024) #Max is 16TiB
108
109 def safeLvmName(name):
110 tmp = name.strip()
111 tmp = tmp.replace("/", "_")
112 tmp = re.sub("[^0-9a-zA-Z._]", "", tmp)
113 tmp = tmp.lstrip("_")
114
115 return tmp