1From d10fb2c0ee60c97f4dfeab4506a347c26cb389df Mon Sep 17 00:00:00 2001
2From: Vojtech Trefny <vtrefny@redhat.com>
3Date: Tue, 7 Dec 2021 15:50:45 +0800
4Subject: [PATCH] lvm: Do not include duplicate entries in bd_lvm_lvs output
5
6We use "-o segtypes" for the "lvs" command which means multisegment
7LVs will be twice in the output.
8
9Signed-off-by: Vojtech Trefny <vtrefny@redhat.com>
10
11Upstream-Status: Backport [https://github.com/storaged-project/libblockdev/pull/671]
12Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
13---
14 src/plugins/lvm.c | 17 +++++++++++++++--
15 tests/lvm_test.py | 41 +++++++++++++++++++++++++++++++++++++++++
16 2 files changed, 56 insertions(+), 2 deletions(-)
17
18diff --git a/src/plugins/lvm.c b/src/plugins/lvm.c
19index 2be1dbd..acd5b84 100644
20--- a/src/plugins/lvm.c
21+++ b/src/plugins/lvm.c
22@@ -1810,8 +1810,21 @@ BDLVMLVdata** bd_lvm_lvs (const gchar *vg_name, GError **error) {
23         if (table && (num_items == 15)) {
24             /* valid line, try to parse and record it */
25             lvdata = get_lv_data_from_table (table, TRUE);
26-            if (lvdata)
27-                g_ptr_array_add (lvs, lvdata);
28+            if (lvdata) {
29+                /* ignore duplicate entries in lvs output, these are caused by multi segments LVs */
30+                for (gsize i = 0; i < lvs->len; i++) {
31+                    if (g_strcmp0 (((BDLVMLVdata *) g_ptr_array_index (lvs, i))->lv_name, lvdata->lv_name) == 0) {
32+                        g_debug("Duplicate LV entry for '%s' found in lvs output",
33+                                  lvdata->lv_name);
34+                        bd_lvm_lvdata_free (lvdata);
35+                        lvdata = NULL;
36+                        break;
37+                    }
38+                }
39+
40+                if (lvdata)
41+                    g_ptr_array_add (lvs, lvdata);
42+            }
43         } else
44             if (table)
45                 g_hash_table_destroy (table);
46diff --git a/tests/lvm_test.py b/tests/lvm_test.py
47index eb94c91..ab0de21 100644
48--- a/tests/lvm_test.py
49+++ b/tests/lvm_test.py
50@@ -915,6 +915,47 @@ class LvmTestLVs(LvmPVVGLVTestCase):
51         lvs = BlockDev.lvm_lvs("testVG")
52         self.assertEqual(len(lvs), 1)
53
54+class LvmTestLVsMultiSegment(LvmPVVGLVTestCase):
55+    def _clean_up(self):
56+        try:
57+            BlockDev.lvm_lvremove("testVG", "testLV2", True, None)
58+        except:
59+            pass
60+
61+        LvmPVVGLVTestCase._clean_up(self)
62+
63+    def test_lvs(self):
64+        """Verify that it's possible to gather info about LVs"""
65+
66+        succ = BlockDev.lvm_pvcreate(self.loop_dev, 0, 0, None)
67+        self.assertTrue(succ)
68+
69+        succ = BlockDev.lvm_vgcreate("testVG", [self.loop_dev], 0, None)
70+        self.assertTrue(succ)
71+
72+        succ = BlockDev.lvm_lvcreate("testVG", "testLV", 10 * 1024**2)
73+        self.assertTrue(succ)
74+
75+        lvs = BlockDev.lvm_lvs("testVG")
76+        self.assertEqual(len(lvs), 1)
77+        self.assertListEqual([lv.lv_name for lv in lvs], ["testLV"])
78+
79+        # add second LV
80+        succ = BlockDev.lvm_lvcreate("testVG", "testLV2", 10 * 1024**2)
81+        self.assertTrue(succ)
82+
83+        lvs = BlockDev.lvm_lvs("testVG")
84+        self.assertEqual(len(lvs), 2)
85+        self.assertListEqual([lv.lv_name for lv in lvs], ["testLV", "testLV2"])
86+
87+        # by resizing the first LV we will create two segments
88+        succ = BlockDev.lvm_lvresize("testVG", "testLV", 20 * 1024**2, None)
89+        self.assertTrue(succ)
90+
91+        lvs = BlockDev.lvm_lvs("testVG")
92+        self.assertEqual(len(lvs), 2)
93+        self.assertListEqual([lv.lv_name for lv in lvs], ["testLV", "testLV2"])
94+
95 class LvmPVVGthpoolTestCase(LvmPVVGTestCase):
96     def _clean_up(self):
97         try:
98--
992.27.0
100
101