From dac2bfe6a4a901b91bf3c6c5ebf39ce5a14c8f20 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Thu, 23 May 2024 01:36:50 +0200 Subject: [PATCH] device_manager: add device_get_uuid Function that is working with DM target is located within lib/activate directory. This function is able to use cached dm_device_list when possible to quickly resolve checks for device's UUID. Function can fully replace get_dm_uuid_from_sysfs() and instead of syscalls for open/read/close get the UUID with single ioctl. When there is cached dm devs list, we can get many UUID from a single syscall. --- lib/activate/activate.h | 2 ++ lib/activate/dev_manager.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/activate/activate.h b/lib/activate/activate.h index e550ec5b0..ca8b6e695 100644 --- a/lib/activate/activate.h +++ b/lib/activate/activate.h @@ -109,6 +109,8 @@ int target_version(const char *target_name, uint32_t *maj, int get_device_list(const struct volume_group *vg, struct dm_list **devs, unsigned *devs_features); +int device_get_uuid(struct cmd_context *cmd, int major, int minor, + char *uuid_buf, size_t uuid_buf_size); int raid4_is_supported(struct cmd_context *cmd, const struct segment_type *segtype); int lvm_dm_prefix_check(int major, int minor, const char *prefix); diff --git a/lib/activate/dev_manager.c b/lib/activate/dev_manager.c index 52766f58a..34fd49665 100644 --- a/lib/activate/dev_manager.c +++ b/lib/activate/dev_manager.c @@ -880,6 +880,36 @@ int device_is_usable(struct cmd_context *cmd, struct device *dev, struct dev_usa return r; } +/* Read UUID from a given DM device into buf_uuid */ +int device_get_uuid(struct cmd_context *cmd, int major, int minor, + char *uuid_buf, size_t uuid_buf_size) +{ + struct dm_task *dmt; + struct dm_info info; + const char *uuid; + int r = 0; + + if (cmd->cache_dm_devs) { + if (dm_device_list_find_by_dev(cmd->cache_dm_devs, major, minor, NULL, &uuid)) { + dm_strncpy(uuid_buf, uuid, uuid_buf_size); + return 1; + } + uuid_buf[0] = 0; + return 0; + } + + if (!(dmt = _setup_task_run(DM_DEVICE_INFO, &info, NULL, NULL, NULL, + major, minor, 0, 0, 0))) + return_0; + + if (info.exists && (uuid = dm_task_get_uuid(dmt))) + r = dm_strncpy(uuid_buf, uuid, uuid_buf_size); + + dm_task_destroy(dmt); + + return r; +} + /* * If active LVs were activated by a version of LVM2 before 2.02.00 we must * perform additional checks to find them because they do not have the LVM-