From a1556cfa77d6f92ce7e3c640a98d37dd7ad04813 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Thu, 18 Oct 2012 17:58:45 +0200
Subject: [PATCH] LVM: remove old tags when adding new ones

This patch adds a small helper function to clear an LV's tags, and
calls it at SetInfo time. We need this to be able to correctly track
instance renames, once we will call SetInfo at such times.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/bdev.py | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/lib/bdev.py b/lib/bdev.py
index 3901ba78d..8d41f53b6 100644
--- a/lib/bdev.py
+++ b/lib/bdev.py
@@ -813,12 +813,28 @@ class LogicalVolume(BlockDev):
 
     return (self._vg_name, snap_name)
 
+  def _RemoveOldInfo(self):
+    """Try to remove old tags from the lv.
+
+    """
+    result = utils.RunCmd(["lvs", "-o", "tags", "--noheadings", "--nosuffix",
+                           self.dev_path])
+    _CheckResult(result)
+
+    raw_tags = result.stdout.strip()
+    if raw_tags:
+      for tag in raw_tags.split(","):
+        _CheckResult(utils.RunCmd(["lvchange", "--deltag",
+                                   tag.strip(), self.dev_path]))
+
   def SetInfo(self, text):
     """Update metadata with info text.
 
     """
     BlockDev.SetInfo(self, text)
 
+    self._RemoveOldInfo()
+
     # Replace invalid characters
     text = re.sub("^[^A-Za-z0-9_+.]", "_", text)
     text = re.sub("[^-A-Za-z0-9_+.]", "_", text)
-- 
GitLab