|
21 | 21 | #
|
22 | 22 |
|
23 | 23 | import math
|
| 24 | +import os |
24 | 25 | import re
|
25 | 26 |
|
26 | 27 | from collections import namedtuple
|
|
83 | 84 |
|
84 | 85 |
|
85 | 86 | LVM_DEVICES_FILE = "/etc/lvm/devices/system.devices"
|
| 87 | +LVM_LOCAL_CONF = "/etc/lvm/lvmlocal.conf" |
86 | 88 |
|
87 | 89 | if hasattr(blockdev.LVMTech, "CONFIG"):
|
88 | 90 | try:
|
@@ -289,3 +291,50 @@ def recommend_thpool_chunk_size(thpool_size):
|
289 | 291 |
|
290 | 292 | def is_valid_cache_md_size(md_size):
|
291 | 293 | 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 |
0 commit comments