Revision 13a6c760 lib/storage/filestorage.py
b/lib/storage/filestorage.py | ||
---|---|---|
23 | 23 |
|
24 | 24 |
""" |
25 | 25 |
|
26 |
import logging |
|
26 | 27 |
import os |
27 | 28 |
|
29 |
from ganeti import compat |
|
28 | 30 |
from ganeti import constants |
29 | 31 |
from ganeti import errors |
32 |
from ganeti import pathutils |
|
33 |
from ganeti import utils |
|
30 | 34 |
|
31 | 35 |
|
32 | 36 |
def GetFileStorageSpaceInfo(path): |
... | ... | |
50 | 54 |
except OSError, e: |
51 | 55 |
raise errors.CommandError("Failed to retrieve file system information about" |
52 | 56 |
" path: %s - %s" % (path, e.strerror)) |
57 |
|
|
58 |
|
|
59 |
def _GetForbiddenFileStoragePaths(): |
|
60 |
"""Builds a list of path prefixes which shouldn't be used for file storage. |
|
61 |
|
|
62 |
@rtype: frozenset |
|
63 |
|
|
64 |
""" |
|
65 |
paths = set([ |
|
66 |
"/boot", |
|
67 |
"/dev", |
|
68 |
"/etc", |
|
69 |
"/home", |
|
70 |
"/proc", |
|
71 |
"/root", |
|
72 |
"/sys", |
|
73 |
]) |
|
74 |
|
|
75 |
for prefix in ["", "/usr", "/usr/local"]: |
|
76 |
paths.update(map(lambda s: "%s/%s" % (prefix, s), |
|
77 |
["bin", "lib", "lib32", "lib64", "sbin"])) |
|
78 |
|
|
79 |
return compat.UniqueFrozenset(map(os.path.normpath, paths)) |
|
80 |
|
|
81 |
|
|
82 |
def _ComputeWrongFileStoragePaths(paths, |
|
83 |
_forbidden=_GetForbiddenFileStoragePaths()): |
|
84 |
"""Cross-checks a list of paths for prefixes considered bad. |
|
85 |
|
|
86 |
Some paths, e.g. "/bin", should not be used for file storage. |
|
87 |
|
|
88 |
@type paths: list |
|
89 |
@param paths: List of paths to be checked |
|
90 |
@rtype: list |
|
91 |
@return: Sorted list of paths for which the user should be warned |
|
92 |
|
|
93 |
""" |
|
94 |
def _Check(path): |
|
95 |
return (not os.path.isabs(path) or |
|
96 |
path in _forbidden or |
|
97 |
filter(lambda p: utils.IsBelowDir(p, path), _forbidden)) |
|
98 |
|
|
99 |
return utils.NiceSort(filter(_Check, map(os.path.normpath, paths))) |
|
100 |
|
|
101 |
|
|
102 |
def ComputeWrongFileStoragePaths(_filename=pathutils.FILE_STORAGE_PATHS_FILE): |
|
103 |
"""Returns a list of file storage paths whose prefix is considered bad. |
|
104 |
|
|
105 |
See L{_ComputeWrongFileStoragePaths}. |
|
106 |
|
|
107 |
""" |
|
108 |
return _ComputeWrongFileStoragePaths(_LoadAllowedFileStoragePaths(_filename)) |
|
109 |
|
|
110 |
|
|
111 |
def _CheckFileStoragePath(path, allowed): |
|
112 |
"""Checks if a path is in a list of allowed paths for file storage. |
|
113 |
|
|
114 |
@type path: string |
|
115 |
@param path: Path to check |
|
116 |
@type allowed: list |
|
117 |
@param allowed: List of allowed paths |
|
118 |
@raise errors.FileStoragePathError: If the path is not allowed |
|
119 |
|
|
120 |
""" |
|
121 |
if not os.path.isabs(path): |
|
122 |
raise errors.FileStoragePathError("File storage path must be absolute," |
|
123 |
" got '%s'" % path) |
|
124 |
|
|
125 |
for i in allowed: |
|
126 |
if not os.path.isabs(i): |
|
127 |
logging.info("Ignoring relative path '%s' for file storage", i) |
|
128 |
continue |
|
129 |
|
|
130 |
if utils.IsBelowDir(i, path): |
|
131 |
break |
|
132 |
else: |
|
133 |
raise errors.FileStoragePathError("Path '%s' is not acceptable for file" |
|
134 |
" storage" % path) |
|
135 |
|
|
136 |
|
|
137 |
def _LoadAllowedFileStoragePaths(filename): |
|
138 |
"""Loads file containing allowed file storage paths. |
|
139 |
|
|
140 |
@rtype: list |
|
141 |
@return: List of allowed paths (can be an empty list) |
|
142 |
|
|
143 |
""" |
|
144 |
try: |
|
145 |
contents = utils.ReadFile(filename) |
|
146 |
except EnvironmentError: |
|
147 |
return [] |
|
148 |
else: |
|
149 |
return utils.FilterEmptyLinesAndComments(contents) |
|
150 |
|
|
151 |
|
|
152 |
def CheckFileStoragePathAcceptance( |
|
153 |
path, _filename=pathutils.FILE_STORAGE_PATHS_FILE): |
|
154 |
"""Checks if a path is allowed for file storage. |
|
155 |
|
|
156 |
@type path: string |
|
157 |
@param path: Path to check |
|
158 |
@raise errors.FileStoragePathError: If the path is not allowed |
|
159 |
|
|
160 |
""" |
|
161 |
allowed = _LoadAllowedFileStoragePaths(_filename) |
|
162 |
|
|
163 |
if _ComputeWrongFileStoragePaths([path]): |
|
164 |
raise errors.FileStoragePathError("Path '%s' uses a forbidden prefix" % |
|
165 |
path) |
|
166 |
|
|
167 |
_CheckFileStoragePath(path, allowed) |
Also available in: Unified diff