Revision 620a85fd lib/storage.py

b/lib/storage.py
125 125
    else:
126 126
      dirsize = None
127 127

  
128
    if constants.SF_FREE in fields:
129
      fsfree = utils.GetFreeFilesystemSpace(path)
128
    if constants.SF_FREE in fields or constants.SF_SIZE in fields:
129
      fsstats = utils.GetFilesystemStats(path)
130 130
    else:
131
      fsfree = None
131
      fsstats = None
132 132

  
133 133
    # Make sure to update constants.VALID_STORAGE_FIELDS when changing fields.
134 134
    for field_name in fields:
......
139 139
        values.append(dirsize)
140 140

  
141 141
      elif field_name == constants.SF_FREE:
142
        values.append(fsfree)
142
        values.append(fsstats[1])
143

  
144
      elif field_name == constants.SF_SIZE:
145
        values.append(fsstats[0])
146

  
147
      elif field_name == constants.SF_ALLOCATABLE:
148
        values.append(True)
143 149

  
144 150
      else:
145 151
        raise errors.StorageError("Unknown field: %r" % field_name)
......
150 156
class _LvmBase(_Base):
151 157
  """Base class for LVM storage containers.
152 158

  
159
  @cvar LIST_FIELDS: list of tuples consisting of three elements: SF_*
160
      constants, lvm command output fields (list), and conversion
161
      function or static value (for static value, the lvm output field
162
      can be an empty list)
163

  
153 164
  """
154 165
  LIST_SEP = "|"
155 166
  LIST_COMMAND = None
......
200 211
      except IndexError:
201 212
        raise errors.StorageError("Unknown field: %r" % field_name)
202 213

  
203
      (_, lvm_name, _) = fields_def[idx]
214
      (_, lvm_names, _) = fields_def[idx]
204 215

  
205
      lvm_fields.append(lvm_name)
216
      lvm_fields.extend(lvm_names)
206 217

  
207 218
    return utils.UniqueSequence(lvm_fields)
208 219

  
......
231 242
      row = []
232 243

  
233 244
      for field_name in wanted_field_names:
234
        (_, lvm_name, convert_fn) = fields_def[field_to_idx[field_name]]
245
        (_, lvm_names, mapper) = fields_def[field_to_idx[field_name]]
235 246

  
236
        value = raw_data[lvm_name_to_idx[lvm_name]]
247
        values = [raw_data[lvm_name_to_idx[i]] for i in lvm_names]
237 248

  
238
        if convert_fn:
239
          value = convert_fn(value)
249
        if callable(mapper):
250
          # we got a function, call it with all the declared fields
251
          val = mapper(*values)
252
        elif len(values) == 1:
253
          # we don't have a function, but we had a single field
254
          # declared, pass it unchanged
255
          val = values[0]
256
        else:
257
          # let's make sure there are no fields declared (cannot map >
258
          # 1 field without a function)
259
          assert not values, "LVM storage has multi-fields without a function"
260
          val = mapper
240 261

  
241
        row.append(value)
262
        row.append(val)
242 263

  
243 264
      data.append(row)
244 265

  
......
297 318
      fields = line.strip().split(sep)
298 319

  
299 320
      if len(fields) != fieldcount:
321
        logging.warning("Invalid line returned from lvm command: %s", line)
300 322
        continue
301 323

  
302 324
      yield fields
......
318 340
  # Make sure to update constants.VALID_STORAGE_FIELDS when changing field
319 341
  # definitions.
320 342
  LIST_FIELDS = [
321
    (constants.SF_NAME, "pv_name", None),
322
    (constants.SF_SIZE, "pv_size", _ParseSize),
323
    (constants.SF_USED, "pv_used", _ParseSize),
324
    (constants.SF_FREE, "pv_free", _ParseSize),
325
    (constants.SF_ALLOCATABLE, "pv_attr", _GetAllocatable),
343
    (constants.SF_NAME, ["pv_name"], None),
344
    (constants.SF_SIZE, ["pv_size"], _ParseSize),
345
    (constants.SF_USED, ["pv_used"], _ParseSize),
346
    (constants.SF_FREE, ["pv_free"], _ParseSize),
347
    (constants.SF_ALLOCATABLE, ["pv_attr"], _GetAllocatable),
326 348
    ]
327 349

  
328 350
  def _SetAllocatable(self, name, allocatable):
......
372 394
  # Make sure to update constants.VALID_STORAGE_FIELDS when changing field
373 395
  # definitions.
374 396
  LIST_FIELDS = [
375
    (constants.SF_NAME, "vg_name", None),
376
    (constants.SF_SIZE, "vg_size", _ParseSize),
397
    (constants.SF_NAME, ["vg_name"], None),
398
    (constants.SF_SIZE, ["vg_size"], _ParseSize),
399
    (constants.SF_FREE, ["vg_free"], _ParseSize),
400
    (constants.SF_USED, ["vg_size", "vg_free"],
401
     lambda x, y: _ParseSize(x) - _ParseSize(y)),
402
    (constants.SF_ALLOCATABLE, [], True),
377 403
    ]
378 404

  
379 405
  def _RemoveMissing(self, name):

Also available in: Unified diff