summaryrefslogtreecommitdiff
path: root/lib/cache
diff options
context:
space:
mode:
authorDavid Teigland <teigland@redhat.com>2020-06-23 13:25:41 -0500
committerDavid Teigland <teigland@redhat.com>2021-02-23 16:43:32 -0600
commit83fe6e720f42711ff183a66909b690b726702f58 (patch)
tree3acdf22ee84f2a25bf85d9cfd978522aef17f0b6 /lib/cache
parentf7d9542aed2615c2a3737ec055a15b79877ac7fc (diff)
downloadlvm2-83fe6e720f42711ff183a66909b690b726702f58.tar.gz
device usage based on devices file
The LVM devices file lists devices that lvm can use. The default file is /etc/lvm/devices/system.devices, and the lvmdevices(8) command is used to add or remove device entries. If the file does not exist, or if lvm.conf includes use_devicesfile=0, then lvm will not use a devices file. When the devices file is in use, the regex filter is not used, and the filter settings in lvm.conf or on the command line are ignored. LVM records devices in the devices file using hardware-specific IDs, such as the WWID, and attempts to use subsystem-specific IDs for virtual device types. These device IDs are also written in the VG metadata. When no hardware or virtual ID is available, lvm falls back using the unstable device name as the device ID. When devnames are used, lvm performs extra scanning to find devices if their devname changes, e.g. after reboot. When proper device IDs are used, an lvm command will not look at devices outside the devices file, but when devnames are used as a fallback, lvm will scan devices outside the devices file to locate PVs on renamed devices. A config setting search_for_devnames can be used to control the scanning for renamed devname entries. Related to the devices file, the new command option --devices <devnames> allows a list of devices to be specified for the command to use, overriding the devices file. The listed devices act as a sort of devices file in terms of limiting which devices lvm will see and use. Devices that are not listed will appear to be missing to the lvm command. Multiple devices files can be kept in /etc/lvm/devices, which allows lvm to be used with different sets of devices, e.g. system devices do not need to be exposed to a specific application, and the application can use lvm on its own set of devices that are not exposed to the system. The option --devicesfile <filename> is used to select the devices file to use with the command. Without the option set, the default system devices file is used. Setting --devicesfile "" causes lvm to not use a devices file. An existing, empty devices file means lvm will see no devices. The new command vgimportdevices adds PVs from a VG to the devices file and updates the VG metadata to include the device IDs. vgimportdevices -a will import all VGs into the system devices file. LVM commands run by dmeventd not use a devices file by default, and will look at all devices on the system. A devices file can be created for dmeventd (/etc/lvm/devices/dmeventd.devices) If this file exists, lvm commands run by dmeventd will use it. Internal implementaion: - device_ids_read - read the devices file . add struct dev_use (du) to cmd->use_devices for each devices file entry - dev_cache_scan - get /dev entries . add struct device (dev) to dev_cache for each device on the system - device_ids_match - match devices file entries to /dev entries . match each du on cmd->use_devices to a dev in dev_cache, using device ID . on match, set du->dev, dev->id, dev->flags MATCHED_USE_ID - label_scan - read lvm headers and metadata from devices . filters are applied, those that do not need data from the device . filter-deviceid skips devs without MATCHED_USE_ID, i.e. skips /dev entries that are not listed in the devices file . read lvm label from dev . filters are applied, those that use data from the device . read lvm metadata from dev . add info/vginfo structs for PVs/VGs (info is "lvmcache") - device_ids_find_renamed_devs - handle devices with unstable devname ID where devname changed . this step only needed when devs do not have proper device IDs, and their dev names change, e.g. after reboot sdb becomes sdc. . detect incorrect match because PVID in the devices file entry does not match the PVID found when the device was read above . undo incorrect match between du and dev above . search system devices for new location of PVID . update devices file with new devnames for PVIDs on renamed devices . label_scan the renamed devs - continue with command processing
Diffstat (limited to 'lib/cache')
-rw-r--r--lib/cache/lvmcache.c88
-rw-r--r--lib/cache/lvmcache.h2
2 files changed, 86 insertions, 4 deletions
diff --git a/lib/cache/lvmcache.c b/lib/cache/lvmcache.c
index e05530a53..172c10f30 100644
--- a/lib/cache/lvmcache.c
+++ b/lib/cache/lvmcache.c
@@ -18,6 +18,7 @@
#include "lib/cache/lvmcache.h"
#include "lib/commands/toolcontext.h"
#include "lib/device/dev-cache.h"
+#include "lib/device/device_id.h"
#include "lib/locking/locking.h"
#include "lib/metadata/metadata.h"
#include "lib/mm/memlock.h"
@@ -125,6 +126,19 @@ void lvmcache_unlock_vgname(const char *vgname)
}
}
+unsigned int lvmcache_vg_info_count(void)
+{
+ struct lvmcache_vginfo *vginfo;
+ unsigned int count = 0;
+
+ dm_list_iterate_items(vginfo, &_vginfos) {
+ if (is_orphan_vg(vginfo->vgname))
+ continue;
+ count++;
+ }
+ return count;
+}
+
int lvmcache_found_duplicate_vgnames(void)
{
return _found_duplicate_vgnames;
@@ -507,6 +521,25 @@ static const char *_get_pvsummary_device_hint(char *pvid)
return NULL;
}
+static const char *_get_pvsummary_device_id(char *pvid, const char **device_id_type)
+{
+ char pvid_s[ID_LEN + 1] __attribute__((aligned(8)));
+ struct lvmcache_vginfo *vginfo;
+ struct pv_list *pvl;
+
+ dm_list_iterate_items(vginfo, &_vginfos) {
+ dm_list_iterate_items(pvl, &vginfo->pvsummaries) {
+ (void) dm_strncpy(pvid_s, (char *) &pvl->pv->id, sizeof(pvid_s));
+ if (!strcmp(pvid_s, pvid)) {
+ *device_id_type = pvl->pv->device_id_type;
+ return pvl->pv->device_id;
+ }
+ }
+ }
+
+ return NULL;
+}
+
/*
* Check if any PVs in vg->pvs have the same PVID as any
* entries in _unused_duplicates.
@@ -612,6 +645,8 @@ static void _choose_duplicates(struct cmd_context *cmd,
struct device_list *devl, *devl_safe, *devl_add, *devl_del;
struct lvmcache_info *info;
struct device *dev1, *dev2;
+ const char *device_id = NULL, *device_id_type = NULL;
+ const char *idname1 = NULL, *idname2 = NULL;
uint32_t dev1_major, dev1_minor, dev2_major, dev2_minor;
uint64_t dev1_size, dev2_size, pvsummary_size;
int in_subsys1, in_subsys2;
@@ -620,6 +655,7 @@ static void _choose_duplicates(struct cmd_context *cmd,
int has_lv1, has_lv2;
int same_size1, same_size2;
int same_name1 = 0, same_name2 = 0;
+ int same_id1 = 0, same_id2 = 0;
int prev_unchosen1, prev_unchosen2;
int change;
@@ -750,6 +786,19 @@ next:
same_name2 = !strcmp(device_hint, dev_name(dev2));
}
+ if ((device_id = _get_pvsummary_device_id(devl->dev->pvid, &device_id_type))) {
+ uint16_t idtype = idtype_from_str(device_id_type);
+
+ if (idtype) {
+ idname1 = device_id_system_read(cmd, dev1, idtype);
+ idname2 = device_id_system_read(cmd, dev2, idtype);
+ }
+ if (idname1)
+ same_id1 = !strcmp(idname1, device_id);
+ if (idname2)
+ same_id2 = !strcmp(idname2, device_id);
+ }
+
has_lv1 = (dev1->flags & DEV_USED_FOR_LV) ? 1 : 0;
has_lv2 = (dev2->flags & DEV_USED_FOR_LV) ? 1 : 0;
@@ -768,6 +817,12 @@ next:
dev_name(dev2), dev2_major, dev2_minor,
device_hint ?: "none");
+ log_debug_cache("PV %s: device_id %s. %s is %s. %s is %s.",
+ devl->dev->pvid,
+ device_id ?: ".",
+ dev_name(dev1), idname1 ?: ".",
+ dev_name(dev2), idname2 ?: ".");
+
log_debug_cache("PV %s: size %llu. %s is %llu. %s is %llu.",
devl->dev->pvid,
(unsigned long long)pvsummary_size,
@@ -808,6 +863,13 @@ next:
} else if (prev_unchosen2 && !prev_unchosen1) {
/* keep 1 (NB when unchosen is set we unprefer) */
reason = "of previous preference";
+ } else if (same_id1 && !same_id2) {
+ /* keep 1 */
+ reason = "device id";
+ } else if (same_id2 && !same_id1) {
+ /* change to 2 */
+ change = 1;
+ reason = "device id";
} else if (has_lv1 && !has_lv2) {
/* keep 1 */
reason = "device is used by LV";
@@ -1177,9 +1239,12 @@ int lvmcache_label_scan(struct cmd_context *cmd)
{
struct dm_list del_cache_devs;
struct dm_list add_cache_devs;
+ struct dm_list renamed_devs;
struct lvmcache_info *info;
struct device_list *devl;
+ dm_list_init(&renamed_devs);
+
log_debug_cache("lvmcache label scan begin");
/*
@@ -1192,14 +1257,25 @@ int lvmcache_label_scan(struct cmd_context *cmd)
* Do the actual scanning. This populates lvmcache
* with infos/vginfos based on reading headers from
* each device, and a vg summary from each mda.
- *
- * Note that this will *skip* scanning a device if
- * an info struct already exists in lvmcache for
- * the device.
*/
label_scan(cmd);
/*
+ * When devnames are used as device ids (which is dispreferred),
+ * changing/unstable devnames can lead to entries in the devices file
+ * not being matched to a dev even if the PV is present on the system.
+ * Or, a devices file entry may have been matched to the wrong device
+ * (with the previous name) that does not have the PVID specified in
+ * the entry. This function detects that problem, scans labels on all
+ * devs on the system to find the missing PVIDs, and corrects the
+ * devices file. We then need to run label scan on these correct
+ * devices.
+ */
+ device_ids_find_renamed_devs(cmd, &renamed_devs, NULL, 0);
+ if (!dm_list_empty(&renamed_devs))
+ label_scan_devs(cmd, cmd->filter, &renamed_devs);
+
+ /*
* _choose_duplicates() returns:
*
* . del_cache_devs: a list of devs currently in lvmcache that should
@@ -2830,6 +2906,10 @@ const char *dev_filtered_reason(struct device *dev)
return "device is too small (pv_min_size)";
if (dev->filtered_flags & DEV_FILTERED_UNUSABLE)
return "device is not in a usable state";
+ if (dev->filtered_flags & DEV_FILTERED_DEVICES_FILE)
+ return "device is not in devices file";
+ if (dev->filtered_flags & DEV_FILTERED_DEVICES_LIST)
+ return "device is not in devices list";
/* flag has not been added here */
if (dev->filtered_flags)
diff --git a/lib/cache/lvmcache.h b/lib/cache/lvmcache.h
index d00bba575..76429fc5b 100644
--- a/lib/cache/lvmcache.h
+++ b/lib/cache/lvmcache.h
@@ -224,4 +224,6 @@ struct metadata_area *lvmcache_get_dev_mda(struct device *dev, int mda_num);
void lvmcache_extra_md_component_checks(struct cmd_context *cmd);
+unsigned int lvmcache_vg_info_count(void);
+
#endif