Certain udev rules for block devices are problematic for osbuild.
One prominent example is LVM2 related rules that would trigger
a scan and auto-activation of logical volumes. This rules are
triggered for new block devices or when the backing file of an
loop devices changes. The rules will lead to a `lvm pvscan
--cache --activate ay` via the `lvm2-pvscan@.service` systemd
service. This will auto-activate all LVM2 logical volumes and
thus interfering with our own device handling in `devices/
org.osbuild.lvm2.lv`, where we only want to activate a single
logical volume.
Also, if the lvm2 devices get activated after the manual metadata
change done in `org.osbuild.lvm2.metadata` the volume group names
might conflict which results in all lvm2 based tooling to be very,
ver sad and also said stage to hang since the loopback device can
not be detached since the activate logical volumes keep it open.
To work-around this we therefore implement a udev rule inhibition
mechanism: on the osbuild side a lock file is created via the new
class called `UdevInhibitor` in `utils/udev.py`. A custom set of
udev rules in `10-osbuild-inhibitor.rules` is then acting on the
existence of that lock file and if present will opt-out of certain
further processing. See the udev rules file for more details.
In fact, we want this custom inhibition mechanism, for all block
devices that are under osbuild's control, since these rules are
there to provide automatisms and integrations with the host,
something we never want.
NB: this should not affect the detection of devices, since lvm2
does do a scan of devices when we call `lvdisplay` in `lvm2.lv`.
The call chain as of lvm2 git rev f773040:
_lvdisplay_single [tools/lvdisplay.c
process_each_lv [tools/toollib.c
lvmcache_label_scan [lib/cache/lvmcache.c
label_scan [ibidem, here is the device detection!
lvdisplay_full [lib/display/display.c
59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
"""userspace /dev device manager (udev) utilities"""
|
|
|
|
import contextlib
|
|
import pathlib
|
|
|
|
|
|
# The default lock dir to use
|
|
LOCKDIR = "/run/osbuild/locks/udev"
|
|
|
|
|
|
class UdevInhibitor:
|
|
"""
|
|
Inhibit execution of certain udev rules for block devices
|
|
|
|
This is the osbuild side of the custom mechanism that
|
|
allows us to inhibit certain udev rules for block devices.
|
|
|
|
For each device a lock file is created in a well known
|
|
directory (LOCKDIR). A custom udev rule set[1] checks
|
|
for the said lock file and inhibits other udev rules from
|
|
being executed.
|
|
See the aforementioned rules file for more information.
|
|
|
|
[1] 10-osbuild-inhibitor.rules
|
|
"""
|
|
|
|
def __init__(self, path: pathlib.Path):
|
|
self.path = path
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
def inhibit(self) -> None:
|
|
self.path.touch()
|
|
|
|
def release(self) -> None:
|
|
with contextlib.suppress(FileNotFoundError):
|
|
self.path.unlink()
|
|
|
|
@property
|
|
def active(self) -> bool:
|
|
return self.path.exists()
|
|
|
|
def __str__(self):
|
|
return f"UdevInhibtor at '{self.path}'"
|
|
|
|
@classmethod
|
|
def for_dm_name(cls, name: str, lockdir=LOCKDIR):
|
|
"""Inhibit a Device Mapper device with the given name"""
|
|
path = pathlib.Path(lockdir, f"dm-{name}")
|
|
ib = cls(path)
|
|
ib.inhibit()
|
|
return ib
|
|
|
|
@classmethod
|
|
def for_device(cls, major: int, minor: int, lockdir=LOCKDIR):
|
|
"""Inhibit a device given its major and minor number"""
|
|
path = pathlib.Path(lockdir, f"device-{major}-{minor}")
|
|
ib = cls(path)
|
|
ib.inhibit()
|
|
return ib
|