Skip to contentSkip to conversation

main - devices: rework libudev usage

PATCH David Teigland 1 participant 1 message Jul 13, 2021 Library code

lore
Share & tools
lore
#0 lore raw
Gitweb:        https://sourceware.org/git/?p=lvm2.git;a=commitdiff;h=90485650931d3fc04d00c92a729050c8743969e5
Commit:        90485650931d3fc04d00c92a729050c8743969e5
Parent:        db22a389cfb12eef77dfc2e1ce124ac83ed6c5a2
Author:        David Teigland <[email protected]>
AuthorDate:    Tue Jun 8 17:12:09 2021 -0500
Committer:     David Teigland <[email protected]>
CommitterDate: Tue Jul 13 11:11:23 2021 -0500

devices: rework libudev usage

related to config settings:
obtain_device_info_from_udev (controls if lvm gets
a list of devices from readdir /dev or from libudev)
external_device_info_source (controls if lvm asks
libudev for device information)

. Make the obtain_device_list_from_udev setting
affect only the choice of readdir /dev vs libudev. The setting no longer controls if udev is used for device type checks.

. Change obtain_device_list_from_udev default to 0.
This helps avoid boot timeouts due to slow libudev queries, avoids reported failures from udev_enumerate_scan_devices, and avoids delays from "device not initialized in udev database" errors. Even without errors, for a system booting with 1024 PVs, lvm2-pvscan times improve from about 100 sec to 15 sec, and the pvscan command from about 64 sec to about 4 sec.

. For external_device_info_source="none", remove all
libudev device info queries, and use only lvm native device info.

. For external_device_info_source="udev", first check
lvm native device info, then check libudev info.

. Remove sleep/retry loop when attempting libudev
queries for device info. udev info will simply
be skipped if it's not immediately available.

. Only set up a libdev connection if it will be used by
obtain_device_list_from_udev/external_device_info_source.

. For native multipath component detection, use
/etc/multipath/wwids. If a device has a wwid
matching an entry in the wwids file, then it's
considered a multipath component. This is
necessary to natively detect multipath
components when the mpath device is not set up.

---
 lib/Makefile.in                  |   1 +
 lib/cache/lvmcache.c             |   6 +-
 lib/commands/toolcontext.c       |  56 ++---
 lib/config/config_settings.h     |  24 +-
 lib/config/defaults.h            |   4 +-
 lib/device/dev-luks.c            |   2 +-
 lib/device/dev-md.c              |  75 ++----
 lib/device/dev-mpath.c           | 486 +++++++++++++++++++++++++++++++++++++++
 lib/device/dev-swap.c            |   2 +-
 lib/device/dev-type.c            | 197 +++-------------
 lib/device/dev-type.h            |  11 +-
 lib/device/device.h              |   3 +
 lib/device/device_id.c           |  38 +--
 lib/device/device_id.h           |   2 +
 lib/filters/filter-composite.c   |  25 +-
 lib/filters/filter-md.c          |   2 +-
 lib/filters/filter-mpath.c       | 362 +----------------------------
 lib/filters/filter-partitioned.c |   4 +-
 lib/filters/filter.h             |   2 +-
 lib/misc/lvm-wrappers.c          |  14 +-
 tools/lvmcmdline.c               |  14 +-
 tools/pvscan.c                   |  12 +-
 22 files changed, 659 insertions(+), 683 deletions(-)
lib/Makefile.in +1 -0
index 900ae2f83..8b3eac60a 100644
@@ -34,6 +34,7 @@ SOURCES =\
 	device/dev-ext.c \
 	device/dev-io.c \
 	device/dev-md.c \
+	device/dev-mpath.c \
 	device/dev-swap.c \
 	device/dev-type.c \
 	device/dev-luks.c \
lib/cache/lvmcache.c +3 -3
index e15b6ce85..f5df5e10c 100644
@@ -692,7 +692,7 @@ next:
 	 */
 
 	info = lvmcache_info_from_pvid(pvid, NULL, 0);
-	if (info && dev_is_md_component(info->dev, NULL, 1)) {
+	if (info && dev_is_md_component(cmd, info->dev, NULL, 1)) {
 		/* does not go in del_cache_devs which become unused_duplicates */
 		log_debug_cache("PV %s drop MD component from scan selection %s", pvid, dev_name(info->dev));
 		lvmcache_del(info);
@@ -700,7 +700,7 @@ next:
 	}
 
 	dm_list_iterate_items_safe(devl, devl_safe, &altdevs) {
-		if (dev_is_md_component(devl->dev, NULL, 1)) {
+		if (dev_is_md_component(cmd, devl->dev, NULL, 1)) {
 			log_debug_cache("PV %s drop MD component from scan duplicates %s", pvid, dev_name(devl->dev));
 			dm_list_del(&devl->list);
 		}
@@ -1204,7 +1204,7 @@ void lvmcache_extra_md_component_checks(struct cmd_context *cmd)
 				  (unsigned long long)pvsize, (unsigned long long)devsize,
 				  device_hint ?: "none", dev_name(dev));
 
-			if (dev_is_md_component(dev, NULL, 1)) {
+			if (dev_is_md_component(cmd, dev, NULL, 1)) {
 				log_debug("dropping PV from md component %s", dev_name(dev));
 				dev->flags &= ~DEV_SCAN_FOUND_LABEL;
 				/* lvmcache_del will also delete vginfo if info was last one */
lib/commands/toolcontext.c +29 -27

89 lines not on this page · raw message

lib/config/config_settings.h +8 -16
index d66a9a3a7..fa87bea23 100644
@@ -238,22 +238,10 @@ cfg(devices_obtain_device_list_from_udev_CFG, "obtain_device_list_from_udev", de
 	"udev support for this setting to apply.\n")
 
 cfg(devices_external_device_info_source_CFG, "external_device_info_source", devices_CFG_SECTION, 0, CFG_TYPE_STRING, DEFAULT_EXTERNAL_DEVICE_INFO_SOURCE, vsn(2, 2, 116), NULL, 0, NULL,
-	"Select an external device information source.\n"
-	"Some information may already be available in the system and LVM can\n"
-	"use this information to determine the exact type or use of devices it\n"
-	"processes. Using an existing external device information source can\n"
-	"speed up device processing as LVM does not need to run its own native\n"
-	"routines to acquire this information. For example, this information\n"
-	"is used to drive LVM filtering like MD component detection, multipath\n"
-	"component detection, partition detection and others.\n"
-	"#\n"
-	"Accepted values:\n"
-	"  none\n"
-	"    No external device information source is used.\n"
-	"  udev\n"
-	"    Reuse existing udev database records. Applicable only if LVM is\n"
-	"    compiled with udev support.\n"
-	"#\n")
+	"Enable device information from udev.\n"
+	"If set to \"udev\", lvm will supplement its own native device information\n"
+	"with information from libudev. This can potentially improve the detection\n"
+	"of MD component devices and multipath component devices.\n")
 
 cfg(devices_hints_CFG, "hints", devices_CFG_SECTION, CFG_DEFAULT_COMMENTED, CFG_TYPE_STRING, DEFAULT_HINTS, vsn(2, 3, 2), NULL, 0, NULL,
 	"Use a local file to remember which devices have PVs on them.\n"
@@ -393,6 +381,10 @@ cfg(devices_scan_lvs_CFG, "scan_lvs", devices_CFG_SECTION, 0, CFG_TYPE_BOOL, DEF
 cfg(devices_multipath_component_detection_CFG, "multipath_component_detection", devices_CFG_SECTION, 0, CFG_TYPE_BOOL, DEFAULT_MULTIPATH_COMPONENT_DETECTION, vsn(2, 2, 89), NULL, 0, NULL,
 	"Ignore devices that are components of DM multipath devices.\n")
 
+cfg(devices_multipath_wwids_file_CFG, "multipath_wwids_file", devices_CFG_SECTION, CFG_DEFAULT_COMMENTED | CFG_ALLOW_EMPTY, CFG_TYPE_STRING, DEFAULT_WWIDS_FILE, vsn(2, 3, 13), NULL, 0, NULL,
+	"The path to the multipath wwids file used for multipath component detection.\n"
+	"Set this to an empty string to disable the use of the multipath wwids file.\n")
+
 cfg(devices_md_component_detection_CFG, "md_component_detection", devices_CFG_SECTION, 0, CFG_TYPE_BOOL, DEFAULT_MD_COMPONENT_DETECTION, vsn(1, 0, 18), NULL, 0, NULL,
 	"Enable detection and exclusion of MD component devices.\n"
 	"An MD component device is a block device that MD uses as part\n"
lib/config/defaults.h +3 -1
index d5e5b3b1a..66eece53a 100644
@@ -42,7 +42,7 @@
 #define DEFAULT_DEV_DIR "/dev"
 #define DEFAULT_PROC_DIR "/proc"
 #define DEFAULT_SYSTEM_ID_SOURCE "none"
-#define DEFAULT_OBTAIN_DEVICE_LIST_FROM_UDEV 1
+#define DEFAULT_OBTAIN_DEVICE_LIST_FROM_UDEV 0
 #define DEFAULT_EXTERNAL_DEVICE_INFO_SOURCE "none"
 #define DEFAULT_SYSFS_SCAN 1
 #define DEFAULT_MD_COMPONENT_DETECTION 1
@@ -327,4 +327,6 @@
 
 #define DEFAULT_SEARCH_FOR_DEVNAMES "auto"
 
+#define DEFAULT_WWIDS_FILE "/etc/multipath/wwids"
+
 #endif				/* _LVM_DEFAULTS_H */
lib/device/dev-luks.c +1 -1
index 344a55432..d8c422519 100644
@@ -18,7 +18,7 @@
 #define LUKS_SIGNATURE "LUKS\xba\xbe"
 #define LUKS_SIGNATURE_SIZE 6
 
-int dev_is_luks(struct device *dev, uint64_t *offset_found, int full)
+int dev_is_luks(struct cmd_context *cmd, struct device *dev, uint64_t *offset_found, int full)
 {
 	char buf[LUKS_SIGNATURE_SIZE];
 	int ret = -1;
lib/device/dev-md.c +25 -50

124 lines not on this page · raw message

lib/device/dev-mpath.c new file +486 -0

486 lines not on this page · raw message

lib/device/dev-swap.c +1 -1
index 564bb58ec..1d2a4c9d1 100644
@@ -35,7 +35,7 @@ static int _swap_detect_signature(const char *buf)
 	return 0;
 }
 
-int dev_is_swap(struct device *dev, uint64_t *offset_found, int full)
+int dev_is_swap(struct cmd_context *cmd, struct device *dev, uint64_t *offset_found, int full)
 {
 	char buf[10];
 	uint64_t size;
lib/device/dev-type.c +30 -167

263 lines not on this page · raw message

lib/device/dev-type.h +5 -6
index 097be72cd..f3521c6e0 100644
@@ -57,12 +57,11 @@ const char *dev_subsystem_name(struct dev_types *dt, struct device *dev);
 int major_is_scsi_device(struct dev_types *dt, int major);
 
 /* Signature/superblock recognition with position returned where found. */
-int dev_is_md_component(struct device *dev, uint64_t *sb, int full);
-int dev_is_swap(struct device *dev, uint64_t *signature, int full);
-int dev_is_luks(struct device *dev, uint64_t *signature, int full);
+int dev_is_md_component(struct cmd_context *cmd, struct device *dev, uint64_t *sb, int full);
+int dev_is_mpath_component(struct cmd_context *cmd, struct device *dev);
+int dev_is_swap(struct cmd_context *cmd, struct device *dev, uint64_t *signature, int full);
+int dev_is_luks(struct cmd_context *cmd, struct device *dev, uint64_t *signature, int full);
 int dasd_is_cdl_formatted(struct device *dev);
-int udev_dev_is_mpath_component(struct device *dev);
-int udev_dev_is_md_component(struct device *dev);
 
 int dev_is_lvm1(struct device *dev, char *buf, int buflen);
 int dev_is_pool(struct device *dev, char *buf, int buflen);
@@ -81,7 +80,7 @@ int dev_is_md_with_end_superblock(struct dev_types *dt, struct device *dev);
 
 /* Partitioning */
 int major_max_partitions(struct dev_types *dt, int major);
-int dev_is_partitioned(struct dev_types *dt, struct device *dev);
+int dev_is_partitioned(struct cmd_context *cmd, struct device *dev);
 int dev_get_primary_dev(struct dev_types *dt, struct device *dev, dev_t *result);
 int dev_get_partition_number(struct device *dev, int *num);
 
lib/device/device.h +3 -0
index ca8b3cdd8..a30a1288b 100644
@@ -205,4 +205,7 @@ void dev_destroy_file(struct device *dev);
 /* Return a valid device name from the alias list; NULL otherwise */
 const char *dev_name_confirmed(struct device *dev, int quiet);
 
+int dev_mpath_init(const char *config_wwids_file);
+void dev_mpath_exit(void);
+
 #endif
lib/device/device_id.c +19 -19
index 87a46a73a..baba48c05 100644
@@ -180,7 +180,7 @@ void free_dids(struct dm_list *ids)
 	}
 }
 
-static int _read_sys_block(struct cmd_context *cmd, struct device *dev, const char *suffix, char *sysbuf, int sysbufsize)
+int read_sys_block(struct cmd_context *cmd, struct device *dev, const char *suffix, char *sysbuf, int sysbufsize)
 {
 	char path[PATH_MAX];
 	dev_t devt = dev->dev;
@@ -246,7 +246,7 @@ static int _dev_has_mpath_uuid(struct cmd_context *cmd, struct device *dev, cons
 	char sysbuf[PATH_MAX] = { 0 };
 	const char *idname;
 
-	if (!_read_sys_block(cmd, dev, "dm/uuid", sysbuf, sizeof(sysbuf)))
+	if (!read_sys_block(cmd, dev, "dm/uuid", sysbuf, sizeof(sysbuf)))
 		return 0;
 
 	if (!_dm_uuid_has_prefix(sysbuf, "mpath-"))
@@ -265,7 +265,7 @@ static int _dev_has_crypt_uuid(struct cmd_context *cmd, struct device *dev, cons
 	char sysbuf[PATH_MAX] = { 0 };
 	const char *idname;
 
-	if (!_read_sys_block(cmd, dev, "dm/uuid", sysbuf, sizeof(sysbuf)))
+	if (!read_sys_block(cmd, dev, "dm/uuid", sysbuf, sizeof(sysbuf)))
 		return 0;
 
 	if (!_dm_uuid_has_prefix(sysbuf, "CRYPT-"))
@@ -284,7 +284,7 @@ static int _dev_has_lvmlv_uuid(struct cmd_context *cmd, struct device *dev, cons
 	char sysbuf[PATH_MAX] = { 0 };
 	const char *idname;
 
-	if (!_read_sys_block(cmd, dev, "dm/uuid", sysbuf, sizeof(sysbuf)))
+	if (!read_sys_block(cmd, dev, "dm/uuid", sysbuf, sizeof(sysbuf)))
 		return 0;
 
 	if (!_dm_uuid_has_prefix(sysbuf, "LVM-"))
@@ -304,10 +304,10 @@ const char *device_id_system_read(struct cmd_context *cmd, struct device *dev, u
 	const char *idname = NULL;
 
 	if (idtype == DEV_ID_TYPE_SYS_WWID) {
-		_read_sys_block(cmd, dev, "device/wwid", sysbuf, sizeof(sysbuf));
+		read_sys_block(cmd, dev, "device/wwid", sysbuf, sizeof(sysbuf));
 
 		if (!sysbuf[0])
-			_read_sys_block(cmd, dev, "wwid", sysbuf, sizeof(sysbuf));
+			read_sys_block(cmd, dev, "wwid", sysbuf, sizeof(sysbuf));
 
 		/* scsi_debug wwid begins "t10.Linux   scsi_debug ..." */
 		if (strstr(sysbuf, "scsi_debug"))
@@ -319,22 +319,22 @@ const char *device_id_system_read(struct cmd_context *cmd, struct device *dev, u
 	}
 
 	else if (idtype == DEV_ID_TYPE_SYS_SERIAL)
-		_read_sys_block(cmd, dev, "device/serial", sysbuf, sizeof(sysbuf));
+		read_sys_block(cmd, dev, "device/serial", sysbuf, sizeof(sysbuf));
 
 	else if (idtype == DEV_ID_TYPE_MPATH_UUID)
-		_read_sys_block(cmd, dev, "dm/uuid", sysbuf, sizeof(sysbuf));
+		read_sys_block(cmd, dev, "dm/uuid", sysbuf, sizeof(sysbuf));
 
 	else if (idtype == DEV_ID_TYPE_CRYPT_UUID)
-		_read_sys_block(cmd, dev, "dm/uuid", sysbuf, sizeof(sysbuf));
+		read_sys_block(cmd, dev, "dm/uuid", sysbuf, sizeof(sysbuf));
 
 	else if (idtype == DEV_ID_TYPE_LVMLV_UUID)
-		_read_sys_block(cmd, dev, "dm/uuid", sysbuf, sizeof(sysbuf));
+		read_sys_block(cmd, dev, "dm/uuid", sysbuf, sizeof(sysbuf));
 
 	else if (idtype == DEV_ID_TYPE_MD_UUID)
-		_read_sys_block(cmd, dev, "md/uuid", sysbuf, sizeof(sysbuf));
+		read_sys_block(cmd, dev, "md/uuid", sysbuf, sizeof(sysbuf));
 
 	else if (idtype == DEV_ID_TYPE_LOOP_FILE) {
-		_read_sys_block(cmd, dev, "loop/backing_file", sysbuf, sizeof(sysbuf));
+		read_sys_block(cmd, dev, "loop/backing_file", sysbuf, sizeof(sysbuf));
 		/* if backing file is deleted, fall back to devname */
 		if (strstr(sysbuf, "(deleted)"))
 			sysbuf[0] = '\0';
@@ -372,17 +372,17 @@ static int _dev_has_stable_id(struct cmd_context *cmd, struct device *dev)
 			return 1;
 	}
 
-	if (_read_sys_block(cmd, dev, "device/wwid", sysbuf, sizeof(sysbuf)))
+	if (read_sys_block(cmd, dev, "device/wwid", sysbuf, sizeof(sysbuf)))
 		return 1;
 
-	if (_read_sys_block(cmd, dev, "wwid", sysbuf, sizeof(sysbuf)))
+	if (read_sys_block(cmd, dev, "wwid", sysbuf, sizeof(sysbuf)))
 		return 1;
 
-	if (_read_sys_block(cmd, dev, "device/serial", sysbuf, sizeof(sysbuf)))
+	if (read_sys_block(cmd, dev, "device/serial", sysbuf, sizeof(sysbuf)))
 		return 1;
 
 	if ((MAJOR(dev->dev) == cmd->dev_types->device_mapper_major)) {
-		if (!_read_sys_block(cmd, dev, "dm/uuid", sysbuf, sizeof(sysbuf)))
+		if (!read_sys_block(cmd, dev, "dm/uuid", sysbuf, sizeof(sysbuf)))
 			goto_out;
 
 		if (_dm_uuid_has_prefix(sysbuf, "mpath-"))
@@ -394,11 +394,11 @@ static int _dev_has_stable_id(struct cmd_context *cmd, struct device *dev)
 	}
 
 	if ((MAJOR(dev->dev) == cmd->dev_types->md_major) &&
-	    _read_sys_block(cmd, dev, "md/uuid", sysbuf, sizeof(sysbuf)))
+	    read_sys_block(cmd, dev, "md/uuid", sysbuf, sizeof(sysbuf)))
 		return 1;
 
 	if ((MAJOR(dev->dev) == cmd->dev_types->loop_major) &&
-	    _read_sys_block(cmd, dev, "loop/backing_file", sysbuf, sizeof(sysbuf)))
+	    read_sys_block(cmd, dev, "loop/backing_file", sysbuf, sizeof(sysbuf)))
 		return 1;
  out:
 	/* DEV_ID_TYPE_DEVNAME would be used for this dev. */
@@ -1190,7 +1190,7 @@ id_done:
 			if (!label_scan_open(du_devid->dev))
 				log_warn("Cannot open %s", dev_name(du_devid->dev));
 
-			if (dev_is_partitioned(cmd->dev_types, du_devid->dev)) {
+			if (dev_is_partitioned(cmd, du_devid->dev)) {
 				/* Check if existing entry is whole device and new entry is a partition of it. */
 				ret1 = dev_get_primary_dev(cmd->dev_types, dev, &devt1);
 				if ((ret1 == 2) && (devt1 == du_devid->dev->dev))
lib/device/device_id.h +2 -0
index 1167373d9..a1a53f253 100644
@@ -52,4 +52,6 @@ void devices_file_exit(struct cmd_context *cmd);
 
 void unlink_searched_devnames(struct cmd_context *cmd);
 
+int read_sys_block(struct cmd_context *cmd, struct device *dev, const char *suffix, char *sysbuf, int sysbufsize);
+
 #endif
lib/filters/filter-composite.c +10 -15
index f4cbeefe8..46a6724f1 100644
@@ -21,29 +21,24 @@
 static int _and_p(struct cmd_context *cmd, struct dev_filter *f, struct device *dev, const char *use_filter_name)
 {
 	struct dev_filter **filters;
-	int ret;
+	int ret = 1;
+
+	dev_ext_enable(dev, external_device_info_source());
 
 	for (filters = (struct dev_filter **) f->private; *filters; ++filters) {
 		if (use_filter_name && strcmp((*filters)->name, use_filter_name))
 			continue;
 		ret = (*filters)->passes_filter(cmd, *filters, dev, use_filter_name);
 
-		if (!ret)
-			return 0;	/* No 'stack': a filter, not an error. */
+		if (!ret) {
+			ret = 0;	/* No 'stack': a filter, not an error. */
+			break;
+		}
 	}
 
-	return 1;
-}
-
-static int _and_p_with_dev_ext_info(struct cmd_context *cmd, struct dev_filter *f, struct device *dev, const char *use_filter_name)
-{
-	int r;
-
-	dev_ext_enable(dev, external_device_info_source());
-	r = _and_p(cmd, f, dev, use_filter_name);
 	dev_ext_disable(dev);
 
-	return r;
+	return ret;
 }
 
 static void _composite_destroy(struct dev_filter *f)
@@ -72,7 +67,7 @@ static void _wipe(struct cmd_context *cmd, struct dev_filter *f, struct device *
 	}
 }
 
-struct dev_filter *composite_filter_create(int n, int use_dev_ext_info, struct dev_filter **filters)
+struct dev_filter *composite_filter_create(int n, struct dev_filter **filters)
 {
 	struct dev_filter **filters_copy, *cft;
 
@@ -93,7 +88,7 @@ struct dev_filter *composite_filter_create(int n, int use_dev_ext_info, struct d
 		return NULL;
 	}
 
-	cft->passes_filter = use_dev_ext_info ? _and_p_with_dev_ext_info : _and_p;
+	cft->passes_filter = _and_p;
 	cft->destroy = _composite_destroy;
 	cft->wipe = _wipe;
 	cft->use_count = 0;
lib/filters/filter-md.c +1 -1
index f72c52123..865fde1a2 100644
@@ -98,7 +98,7 @@ static int _passes_md_filter(struct cmd_context *cmd, struct dev_filter *f __att
 	if (!md_filtering())
 		return 1;
 
-	ret = dev_is_md_component(dev, NULL, cmd->use_full_md_check);
+	ret = dev_is_md_component(cmd, dev, NULL, cmd->use_full_md_check);
 
 	if (ret == -EAGAIN) {
 		/* let pass, call again after scan */
lib/filters/filter-mpath.c +10 -352

402 lines not on this page · raw message

lib/filters/filter-partitioned.c +1 -3
index c0b5554c5..642553ef2 100644
@@ -22,7 +22,6 @@
 
 static int _passes_partitioned_filter(struct cmd_context *cmd, struct dev_filter *f, struct device *dev, const char *use_filter_name)
 {
-	struct dev_types *dt = (struct dev_types *) f->private;
 	int ret;
 
 	if (cmd->filter_nodata_only)
@@ -30,7 +29,7 @@ static int _passes_partitioned_filter(struct cmd_context *cmd, struct dev_filter
 
 	dev->filtered_flags &= ~DEV_FILTERED_PARTITIONED;
 
-	ret = dev_is_partitioned(dt, dev);
+	ret = dev_is_partitioned(cmd, dev);
 
 	if (ret == -EAGAIN) {
 		/* let pass, call again after scan */
@@ -72,7 +71,6 @@ struct dev_filter *partitioned_filter_create(struct dev_types *dt)
 	f->passes_filter = _passes_partitioned_filter;
 	f->destroy = _partitioned_filter_destroy;
 	f->use_count = 0;
-	f->private = dt;
 	f->name = "partitioned";
 
 	log_debug_devs("Partitioned filter initialised.");
lib/filters/filter.h +1 -1
index f9de65273..bff58c9a4 100644
@@ -20,7 +20,7 @@
 #include "lib/device/dev-cache.h"
 #include "lib/device/dev-type.h"
 
-struct dev_filter *composite_filter_create(int n, int use_dev_ext_info, struct dev_filter **filters);
+struct dev_filter *composite_filter_create(int n, struct dev_filter **filters);
 
 struct dev_filter *lvm_type_filter_create(struct dev_types *dt);
 struct dev_filter *md_filter_create(struct cmd_context *cmd, struct dev_types *dt);
lib/misc/lvm-wrappers.c +12 -2
index c36e2cfc7..2e0cfd514 100644
@@ -25,19 +25,29 @@ struct udev *_udev;
 int udev_init_library_context(void)
 {
 	if (_udev)
-		udev_unref(_udev);
+		return 1;
+
+	if (getenv("DM_DISABLE_UDEV"))
+		return 0;
 
 	if (!(_udev = udev_new())) {
 		log_error("Failed to create udev library context.");
 		return 0;
 	}
 
+	if (!udev_is_running()) {
+		udev_unref(_udev);
+		_udev = NULL;
+		return 0;
+	}
+
 	return 1;
 }
 
 void udev_fin_library_context(void)
 {
-	udev_unref(_udev);
+	if (_udev)
+		udev_unref(_udev);
 	_udev = NULL;
 }
 
tools/lvmcmdline.c +7 -7
index 490c9c73d..134697aa3 100644
@@ -3228,6 +3228,11 @@ int lvm_run_command(struct cmd_context *cmd, int argc, char **argv)
 
 	_init_md_checks(cmd);
 
+	if (!dev_mpath_init(find_config_tree_str_allow_empty(cmd, devices_multipath_wwids_file_CFG, NULL))) {
+		ret = ECMD_FAILED;
+		goto_out;
+	}
+
 	if (!_cmd_no_meta_proc(cmd) && !_init_lvmlockd(cmd)) {
 		ret = ECMD_FAILED;
 		goto_out;
@@ -3248,6 +3253,7 @@ int lvm_run_command(struct cmd_context *cmd, int argc, char **argv)
 
       out:
 
+	dev_mpath_exit();
 	hints_exit(cmd);
 	lvmcache_destroy(cmd, 1, 1);
 	label_scan_destroy(cmd);
@@ -3541,9 +3547,6 @@ struct cmd_context *init_lvm(unsigned set_connections,
 {
 	struct cmd_context *cmd;
 
-	if (!udev_init_library_context())
-		stack;
-
 	/*
 	 * It's not necessary to use name mangling for LVM:
 	 *   - the character set used for LV names is subset of udev character set
@@ -3551,9 +3554,7 @@ struct cmd_context *init_lvm(unsigned set_connections,
 	 */
 	dm_set_name_mangling_mode(DM_STRING_MANGLING_NONE);
 
-	if (!(cmd = create_toolcontext(0, NULL, 1, threaded,
-			set_connections, set_filters))) {
-		udev_fin_library_context();
+	if (!(cmd = create_toolcontext(0, NULL, 1, threaded, set_connections, set_filters))) {
 		return_NULL;
 	}
 
@@ -3561,7 +3562,6 @@ struct cmd_context *init_lvm(unsigned set_connections,
 
 	if (stored_errno()) {
 		destroy_toolcontext(cmd);
-		udev_fin_library_context();
 		return_NULL;
 	}
 
tools/pvscan.c +1 -11
index 464501ad5..40a93957c 100644
@@ -1193,16 +1193,6 @@ static int _online_devs(struct cmd_context *cmd, int do_all, struct dm_list *pvs
 
 		log_debug("online_devs %s %s", dev_name(dev), dev->pvid);
 
-		/*
-		 * This should already have been done by the filter, but make
-		 * another check directly with udev in case the filter was not
-		 * using udev and the native version didn't catch it.
-		 */
-		if (udev_dev_is_mpath_component(dev)) {
-			log_print("pvscan[%d] ignore multipath component %s.", getpid(), dev_name(dev));
-			continue;
-		}
-
 		if (!(info = lvmcache_info_from_pvid(dev->pvid, dev, 0))) {
 			if (!do_all)
 				log_print("pvscan[%d] ignore %s with no lvm info.", getpid(), dev_name(dev));
@@ -1257,7 +1247,7 @@ static int _online_devs(struct cmd_context *cmd, int do_all, struct dm_list *pvs
 			if (pv->device_hint && !strncmp(pv->device_hint, "/dev/md", 7))
 				do_full_check = 1;
 		}
-		if (do_full_check && dev_is_md_component(dev, NULL, 1)) {
+		if (do_full_check && dev_is_md_component(cmd, dev, NULL, 1)) {
 			log_print("pvscan[%d] ignore md component %s.", getpid(), dev_name(dev));
 			release_vg(vg);
 			continue;