storage: Add function to modify fields
authorMichael Hanselmann <hansmi@google.com>
Tue, 4 Aug 2009 09:43:03 +0000 (11:43 +0200)
committerMichael Hanselmann <hansmi@google.com>
Tue, 4 Aug 2009 09:44:38 +0000 (11:44 +0200)
This allows the “allocatable” flag on LVM PVs to be changed.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>

lib/storage.py

index de275bd..eefe124 100644 (file)
@@ -50,6 +50,20 @@ class _Base:
     """
     raise NotImplementedError()
 
+  def Modify(self, name, changes):
+    """Modifies an entity within the storage unit.
+
+    @type name: string
+    @param name: Entity name
+    @type changes: dict
+    @param changes: New field values
+
+    """
+    # Don't raise an error if no changes are requested
+    if changes:
+      raise errors.ProgrammerError("Unable to modify the following"
+                                   "fields: %r" % (changes.keys(), ))
+
 
 class FileStorage(_Base):
   """File storage unit.
@@ -298,6 +312,43 @@ class LvmPvStorage(_LvmBase):
     (constants.SF_ALLOCATABLE, "pv_attr", _GetAllocatable),
     ]
 
+  def _SetAllocatable(self, name, allocatable):
+    """Sets the "allocatable" flag on a physical volume.
+
+    @type name: string
+    @param name: Physical volume name
+    @type allocatable: bool
+    @param allocatable: Whether to set the "allocatable" flag
+
+    """
+    args = ["pvchange", "--allocatable"]
+
+    if allocatable:
+      args.append("y")
+    else:
+      args.append("n")
+
+    args.append(name)
+
+    result = utils.RunCmd(args)
+    if result.failed:
+      raise errors.StorageError("Failed to modify physical volume,"
+                                " pvchange output: %s" %
+                                result.output)
+
+  def Modify(self, name, changes):
+    """Modifies flags on a physical volume.
+
+    See L{_Base.Modify}.
+
+    """
+    if constants.SF_ALLOCATABLE in changes:
+      self._SetAllocatable(name, changes[constants.SF_ALLOCATABLE])
+      del changes[constants.SF_ALLOCATABLE]
+
+    # Other changes will be handled (and maybe refused) by the base class.
+    return _LvmBase.Modify(self, name, changes)
+
 
 class LvmVgStorage(_LvmBase):
   """LVM Volume Group storage unit.