Skip to content

Commit 2d5c88d

Browse files
Merge pull request #1131 from vojtechtrefny/3.8-devel_devices-fix-adding
Do not add new PVs to the LVM devices file if it doesn't exist and VG…
2 parents 19c53fd + 21e398a commit 2d5c88d

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

blivet/devicelibs/lvm.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@
8484
else:
8585
HAVE_LVMDEVICES = False
8686

87+
88+
LVM_DEVICES_FILE = "/etc/lvm/devices/system.devices"
89+
8790
# list of devices that LVM is allowed to use
8891
# with LVM >= 2.0.13 we'll use this for the --devices option and when creating
8992
# the /etc/lvm/devices/system.devices file

blivet/formats/lvmpv.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from ..errors import PhysicalVolumeError
3737
from . import DeviceFormat, register_device_format
3838
from .. import udev
39-
from ..static_data.lvm_info import pvs_info
39+
from ..static_data.lvm_info import pvs_info, vgs_info
4040

4141
import logging
4242
log = logging.getLogger("blivet")
@@ -121,10 +121,21 @@ def formattable(self):
121121
def supported(self):
122122
return super(LVMPhysicalVolume, self).supported and self._plugin.available
123123

124-
def lvmdevices_add(self):
124+
def lvmdevices_add(self, force=True):
125+
""" Add this PV to the LVM system devices file
126+
:keyword force: whether to add the PV even if the system devices file doesn't exist and
127+
VGs are present in the system
128+
:type force: bool
129+
"""
130+
125131
if not lvm.HAVE_LVMDEVICES:
126132
raise PhysicalVolumeError("LVM devices file feature is not supported")
127133

134+
if not os.path.exists(lvm.LVM_DEVICES_FILE) and vgs_info.cache and not force:
135+
log.debug("Not adding %s to devices file: %s doesn't exist and there are VGs present in the system",
136+
self.device, lvm.LVM_DEVICES_FILE)
137+
return
138+
128139
try:
129140
blockdev.lvm.devices_add(self.device)
130141
except blockdev.LVMError as e:
@@ -154,7 +165,7 @@ def _create(self, **kwargs):
154165
blockdev.lvm.pvcreate(self.device, data_alignment=self.data_alignment, extra=[ea_yes])
155166
except blockdev.LVMError as e:
156167
raise PhysicalVolumeError(e)
157-
self.lvmdevices_add()
168+
self.lvmdevices_add(force=False)
158169
else:
159170
try:
160171
blockdev.lvm.pvcreate(self.device, data_alignment=self.data_alignment, extra=[ea_yes])

tests/unit_tests/formats_tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .disklabel_test import *
33
from .init_test import *
44
from .luks_test import *
5+
from .lvmpv_test import *
56
from .methods_test import *
67
from .misc_test import *
78
from .selinux_test import *
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
try:
2+
from unittest.mock import patch
3+
except ImportError:
4+
from mock import patch
5+
6+
from contextlib import contextmanager
7+
8+
import unittest
9+
10+
from blivet.formats.lvmpv import LVMPhysicalVolume
11+
12+
13+
class LVMPVNodevTestCase(unittest.TestCase):
14+
15+
@contextmanager
16+
def patches(self):
17+
patchers = dict()
18+
mocks = dict()
19+
20+
patchers["blockdev"] = patch("blivet.formats.lvmpv.blockdev")
21+
patchers["lvm"] = patch("blivet.formats.lvmpv.lvm")
22+
patchers["vgs_info"] = patch("blivet.formats.lvmpv.vgs_info")
23+
patchers["os"] = patch("blivet.formats.lvmpv.os")
24+
25+
for name, patcher in patchers.items():
26+
mocks[name] = patcher.start()
27+
28+
yield mocks
29+
30+
for patcher in patchers.values():
31+
patcher.stop()
32+
33+
def test_lvm_devices(self):
34+
fmt = LVMPhysicalVolume(device="/dev/test")
35+
36+
with self.patches() as mock:
37+
# LVM devices file not enabled/supported -> devices_add should not be called
38+
mock["lvm"].HAVE_LVMDEVICES = False
39+
40+
fmt._create()
41+
42+
mock["blockdev"].lvm.devices_add.assert_not_called()
43+
44+
with self.patches() as mock:
45+
# LVM devices file enabled and devices file exists -> devices_add should be called
46+
mock["lvm"].HAVE_LVMDEVICES = True
47+
mock["os"].path.exists.return_value = True
48+
49+
fmt._create()
50+
51+
mock["blockdev"].lvm.devices_add.assert_called_with("/dev/test")
52+
53+
with self.patches() as mock:
54+
# LVM devices file enabled and devices file doesn't exist
55+
# and no existing VGs present -> devices_add should be called
56+
mock["lvm"].HAVE_LVMDEVICES = True
57+
mock["os"].path.exists.return_value = False
58+
mock["vgs_info"].cache = {}
59+
60+
fmt._create()
61+
62+
mock["blockdev"].lvm.devices_add.assert_called_with("/dev/test")
63+
64+
with self.patches() as mock:
65+
# LVM devices file enabled and devices file doesn't exist
66+
# and existing VGs present -> devices_add should not be called
67+
mock["lvm"].HAVE_LVMDEVICES = True
68+
mock["os"].path.exists.return_value = False
69+
mock["vgs_info"].cache = {"fake_vg_uuid": "fake_vg_data"}
70+
71+
fmt._create()
72+
73+
mock["blockdev"].lvm.devices_add.assert_not_called()

0 commit comments

Comments
 (0)