Revision 23e3c9b7 lib/bdev.py
b/lib/bdev.py | ||
---|---|---|
29 | 29 |
import pyparsing as pyp |
30 | 30 |
import os |
31 | 31 |
import logging |
32 |
import itertools |
|
32 | 33 |
|
33 | 34 |
from ganeti import utils |
34 | 35 |
from ganeti import errors |
... | ... | |
100 | 101 |
return False |
101 | 102 |
|
102 | 103 |
|
104 |
def _GetForbiddenFileStoragePaths(): |
|
105 |
"""Builds a list of path prefixes which shouldn't be used for file storage. |
|
106 |
|
|
107 |
@rtype: frozenset |
|
108 |
|
|
109 |
""" |
|
110 |
paths = set([ |
|
111 |
"/boot", |
|
112 |
"/dev", |
|
113 |
"/etc", |
|
114 |
"/home", |
|
115 |
"/proc", |
|
116 |
"/root", |
|
117 |
"/sys", |
|
118 |
]) |
|
119 |
|
|
120 |
for prefix in ["", "/usr", "/usr/local"]: |
|
121 |
paths.update(map(lambda s: "%s/%s" % (prefix, s), |
|
122 |
["bin", "lib", "lib32", "lib64", "sbin"])) |
|
123 |
|
|
124 |
return frozenset(map(os.path.normpath, paths)) |
|
125 |
|
|
126 |
|
|
127 |
def _ComputeWrongFileStoragePaths(paths, |
|
128 |
_forbidden=_GetForbiddenFileStoragePaths()): |
|
129 |
"""Cross-checks a list of paths for prefixes considered bad. |
|
130 |
|
|
131 |
Some paths, e.g. "/bin", should not be used for file storage. |
|
132 |
|
|
133 |
@type paths: list |
|
134 |
@param paths: List of paths to be checked |
|
135 |
@rtype: list |
|
136 |
@return: Sorted list of paths for which the user should be warned |
|
137 |
|
|
138 |
""" |
|
139 |
def _Check(path): |
|
140 |
return (not os.path.isabs(path) or |
|
141 |
path in _forbidden or |
|
142 |
filter(lambda p: utils.IsBelowDir(p, path), _forbidden)) |
|
143 |
|
|
144 |
return utils.NiceSort(filter(_Check, map(os.path.normpath, paths))) |
|
145 |
|
|
146 |
|
|
147 |
def ComputeWrongFileStoragePaths(_filename=pathutils.FILE_STORAGE_PATHS_FILE): |
|
148 |
"""Returns a list of file storage paths whose prefix is considered bad. |
|
149 |
|
|
150 |
See L{_ComputeWrongFileStoragePaths}. |
|
151 |
|
|
152 |
""" |
|
153 |
return _ComputeWrongFileStoragePaths(_LoadAllowedFileStoragePaths(_filename)) |
|
154 |
|
|
155 |
|
|
103 | 156 |
def _CheckFileStoragePath(path, allowed): |
104 | 157 |
"""Checks if a path is in a list of allowed paths for file storage. |
105 | 158 |
|
... | ... | |
126 | 179 |
" storage" % path) |
127 | 180 |
|
128 | 181 |
|
129 |
def LoadAllowedFileStoragePaths(filename): |
|
182 |
def _LoadAllowedFileStoragePaths(filename):
|
|
130 | 183 |
"""Loads file containing allowed file storage paths. |
131 | 184 |
|
132 | 185 |
@rtype: list |
... | ... | |
149 | 202 |
@raise errors.FileStoragePathError: If the path is not allowed |
150 | 203 |
|
151 | 204 |
""" |
152 |
_CheckFileStoragePath(path, LoadAllowedFileStoragePaths(_filename)) |
|
205 |
allowed = _LoadAllowedFileStoragePaths(_filename) |
|
206 |
|
|
207 |
if _ComputeWrongFileStoragePaths([path]): |
|
208 |
raise errors.FileStoragePathError("Path '%s' uses a forbidden prefix" % |
|
209 |
path) |
|
210 |
|
|
211 |
_CheckFileStoragePath(path, allowed) |
|
153 | 212 |
|
154 | 213 |
|
155 | 214 |
class BlockDev(object): |
Also available in: Unified diff