Skip to content

Commit 177f1bb

Browse files
Merge pull request #1340 from vojtechtrefny/main_lvm-conf-auto-activation
lvm: Add a function to globally disable LVM auto-activation
2 parents d1357ef + b1109bd commit 177f1bb

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

blivet/devicelibs/lvm.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#
2222

2323
import math
24+
import os
2425
import re
2526

2627
from collections import namedtuple
@@ -83,6 +84,7 @@
8384

8485

8586
LVM_DEVICES_FILE = "/etc/lvm/devices/system.devices"
87+
LVM_LOCAL_CONF = "/etc/lvm/lvmlocal.conf"
8688

8789
if hasattr(blockdev.LVMTech, "CONFIG"):
8890
try:
@@ -289,3 +291,50 @@ def recommend_thpool_chunk_size(thpool_size):
289291

290292
def is_valid_cache_md_size(md_size):
291293
return md_size >= LVM_CACHE_MIN_METADATA_SIZE and md_size <= LVM_CACHE_MAX_METADATA_SIZE
294+
295+
296+
def disable_lvm_autoactivation(lvmconf=LVM_LOCAL_CONF):
297+
""" Disable LVM auto-activation *globally* by writing the configuration to
298+
:attr:`lvmconf` (defaults to /etc/lvm/lvmlocal.conf)
299+
"""
300+
if not os.path.exists(lvmconf):
301+
raise RuntimeError("Cannot disable LVM auto-activation, configuration file %s does not exist" % lvmconf)
302+
303+
with open(lvmconf, "r") as f:
304+
for line in f:
305+
if "event_activation" in line and not line.strip().startswith("#"):
306+
raise RuntimeError("LVM auto-activation is already configured in %s" % lvmconf)
307+
308+
with open(lvmconf, "a") as f:
309+
f.write("global { event_activation = 0 }")
310+
f.flush()
311+
312+
log.info("LVM auto-activation is now disabled in %s", lvmconf)
313+
314+
global AUTO_ACTIVATION
315+
AUTO_ACTIVATION = False
316+
317+
318+
def reenable_lvm_autoactivation(lvmconf=LVM_LOCAL_CONF):
319+
""" Enable LVM auto-activation previously disabled in :attr:`lvmconf`.
320+
This function is intended to revert configuration done by
321+
:func:`disable_lvm_autoactivation`.
322+
"""
323+
if not os.path.exists(lvmconf):
324+
raise RuntimeError("Cannot reenable LVM auto-activation, configuration file %s does not exist" % lvmconf)
325+
326+
with open(lvmconf, "r") as f:
327+
lines = f.readlines()
328+
329+
if "global { event_activation = 0 }" not in lines:
330+
raise RuntimeError("LVM auto-activation is not configured in %s" % lvmconf)
331+
332+
with open(lvmconf, "w+") as f:
333+
for line in lines:
334+
if line.strip("\n") != "global { event_activation = 0 }":
335+
f.write(line)
336+
337+
log.info("LVM auto-activation configuration is now removed from %s", lvmconf)
338+
339+
global AUTO_ACTIVATION
340+
AUTO_ACTIVATION = False
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .disk_test import *
22
from .edd_test import *
3+
from .lvm_test import *
34
from .mdraid_test import *
45
from .raid_test import *
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import unittest
2+
from unittest.mock import patch, mock_open
3+
4+
import blivet.devicelibs.lvm as lvm
5+
6+
7+
class LVMTestCase(unittest.TestCase):
8+
9+
def test_lvm_autoactivation(self):
10+
localconf = "global { event_activation = 0 }"
11+
12+
with patch("builtins.open", mock_open(read_data=localconf)):
13+
# already disabled
14+
with self.assertRaises(RuntimeError):
15+
lvm.disable_lvm_autoactivation()
16+
17+
localconf = ""
18+
with patch("builtins.open", mock_open(read_data=localconf)) as m:
19+
lvm.disable_lvm_autoactivation()
20+
m.assert_called_with("/etc/lvm/lvmlocal.conf", "a")
21+
handle = m()
22+
handle.write.assert_called_once_with("global { event_activation = 0 }")
23+
24+
localconf = "test\ntest"
25+
with patch("builtins.open", mock_open(read_data=localconf)) as m:
26+
# not disabled
27+
with self.assertRaises(RuntimeError):
28+
lvm.reenable_lvm_autoactivation()
29+
30+
localconf = "# global { event_activation = 0 }"
31+
with patch("builtins.open", mock_open(read_data=localconf)) as m:
32+
# not disabled
33+
with self.assertRaises(RuntimeError):
34+
lvm.reenable_lvm_autoactivation()
35+
36+
localconf = "test\nglobal { event_activation = 0 }"
37+
with patch("builtins.open", mock_open(read_data=localconf)) as m:
38+
lvm.reenable_lvm_autoactivation()
39+
m.assert_called_with("/etc/lvm/lvmlocal.conf", "w+")
40+
handle = m()
41+
handle.write.assert_called_once_with("test\n")

0 commit comments

Comments
 (0)