Revision 13a6c760 lib/storage/bdev.py

b/lib/storage/bdev.py
35 35
from ganeti import compat
36 36
from ganeti import pathutils
37 37
from ganeti import serializer
38
from ganeti.storage import drbd
39 38
from ganeti.storage import base
39
from ganeti.storage import drbd
40
from ganeti.storage import filestorage
40 41

  
41 42

  
42 43
class RbdShowmappedJsonError(Exception):
......
57 58
                    result.cmd, result.fail_reason, result.output)
58 59

  
59 60

  
60
def _GetForbiddenFileStoragePaths():
61
  """Builds a list of path prefixes which shouldn't be used for file storage.
62

  
63
  @rtype: frozenset
64

  
65
  """
66
  paths = set([
67
    "/boot",
68
    "/dev",
69
    "/etc",
70
    "/home",
71
    "/proc",
72
    "/root",
73
    "/sys",
74
    ])
75

  
76
  for prefix in ["", "/usr", "/usr/local"]:
77
    paths.update(map(lambda s: "%s/%s" % (prefix, s),
78
                     ["bin", "lib", "lib32", "lib64", "sbin"]))
79

  
80
  return compat.UniqueFrozenset(map(os.path.normpath, paths))
81

  
82

  
83
def _ComputeWrongFileStoragePaths(paths,
84
                                  _forbidden=_GetForbiddenFileStoragePaths()):
85
  """Cross-checks a list of paths for prefixes considered bad.
86

  
87
  Some paths, e.g. "/bin", should not be used for file storage.
88

  
89
  @type paths: list
90
  @param paths: List of paths to be checked
91
  @rtype: list
92
  @return: Sorted list of paths for which the user should be warned
93

  
94
  """
95
  def _Check(path):
96
    return (not os.path.isabs(path) or
97
            path in _forbidden or
98
            filter(lambda p: utils.IsBelowDir(p, path), _forbidden))
99

  
100
  return utils.NiceSort(filter(_Check, map(os.path.normpath, paths)))
101

  
102

  
103
def ComputeWrongFileStoragePaths(_filename=pathutils.FILE_STORAGE_PATHS_FILE):
104
  """Returns a list of file storage paths whose prefix is considered bad.
105

  
106
  See L{_ComputeWrongFileStoragePaths}.
107

  
108
  """
109
  return _ComputeWrongFileStoragePaths(_LoadAllowedFileStoragePaths(_filename))
110

  
111

  
112
def _CheckFileStoragePath(path, allowed):
113
  """Checks if a path is in a list of allowed paths for file storage.
114

  
115
  @type path: string
116
  @param path: Path to check
117
  @type allowed: list
118
  @param allowed: List of allowed paths
119
  @raise errors.FileStoragePathError: If the path is not allowed
120

  
121
  """
122
  if not os.path.isabs(path):
123
    raise errors.FileStoragePathError("File storage path must be absolute,"
124
                                      " got '%s'" % path)
125

  
126
  for i in allowed:
127
    if not os.path.isabs(i):
128
      logging.info("Ignoring relative path '%s' for file storage", i)
129
      continue
130

  
131
    if utils.IsBelowDir(i, path):
132
      break
133
  else:
134
    raise errors.FileStoragePathError("Path '%s' is not acceptable for file"
135
                                      " storage" % path)
136

  
137

  
138
def _LoadAllowedFileStoragePaths(filename):
139
  """Loads file containing allowed file storage paths.
140

  
141
  @rtype: list
142
  @return: List of allowed paths (can be an empty list)
143

  
144
  """
145
  try:
146
    contents = utils.ReadFile(filename)
147
  except EnvironmentError:
148
    return []
149
  else:
150
    return utils.FilterEmptyLinesAndComments(contents)
151

  
152

  
153
def CheckFileStoragePath(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)
168

  
169

  
170 61
class LogicalVolume(base.BlockDev):
171 62
  """Logical Volume block device.
172 63

  
......
838 729
    self.driver = unique_id[0]
839 730
    self.dev_path = unique_id[1]
840 731

  
841
    CheckFileStoragePath(self.dev_path)
732
    filestorage.CheckFileStoragePathAcceptance(self.dev_path)
842 733

  
843 734
    self.Attach()
844 735

  
......
962 853

  
963 854
    dev_path = unique_id[1]
964 855

  
965
    CheckFileStoragePath(dev_path)
856
    filestorage.CheckFileStoragePathAcceptance(dev_path)
966 857

  
967 858
    try:
968 859
      fd = os.open(dev_path, os.O_RDWR | os.O_CREAT | os.O_EXCL)

Also available in: Unified diff