From 9b648ee7c70db098e11adc6134b66f6e7862d57a Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Tue, 4 Aug 2009 11:43:03 +0200
Subject: [PATCH] storage: Add function to modify fields
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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 | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/lib/storage.py b/lib/storage.py
index de275bd69..eefe124bb 100644
--- a/lib/storage.py
+++ b/lib/storage.py
@@ -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.
-- 
GitLab