summaryrefslogtreecommitdiff
path: root/lib/label
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/label
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/label')
-rw-r--r--lib/label/hints.c57
-rw-r--r--lib/label/label.c61
-rw-r--r--lib/label/label.h1
3 files changed, 94 insertions, 25 deletions
diff --git a/lib/label/hints.c b/lib/label/hints.c
index 4a51ec918..aed0ea2ff 100644
--- a/lib/label/hints.c
+++ b/lib/label/hints.c
@@ -145,6 +145,7 @@
#include "lib/activate/activate.h"
#include "lib/label/hints.h"
#include "lib/device/dev-type.h"
+#include "lib/device/device_id.h"
#include <sys/stat.h>
#include <fcntl.h>
@@ -166,8 +167,10 @@ static const char *_newhints_file = DEFAULT_RUN_DIR "/newhints";
* than they were built with. Increase the minor number
* when adding features that older lvm versions can just
* ignore while continuing to use the other content.
+ *
+ * MAJOR 2: add devices_file
*/
-#define HINTS_VERSION_MAJOR 1
+#define HINTS_VERSION_MAJOR 2
#define HINTS_VERSION_MINOR 1
#define HINT_LINE_LEN (PATH_MAX + NAME_LEN + ID_LEN + 64)
@@ -712,8 +715,9 @@ static int _read_hint_file(struct cmd_context *cmd, struct dm_list *hints, int *
break;
}
- if (hv_major > HINTS_VERSION_MAJOR) {
- log_debug("ignore hints with newer major version %d.%d", hv_major, hv_minor);
+ if (hv_major != HINTS_VERSION_MAJOR) {
+ log_debug("ignore hints with version %d.%d current %d.%d",
+ hv_major, hv_minor, HINTS_VERSION_MAJOR, HINTS_VERSION_MINOR);
*needs_refresh = 1;
break;
}
@@ -758,6 +762,25 @@ static int _read_hint_file(struct cmd_context *cmd, struct dm_list *hints, int *
continue;
}
+ keylen = strlen("devices_file:");
+ if (!strncmp(_hint_line, "devices_file:", keylen)) {
+ const char *df_hint = _hint_line + keylen;
+ const char *df_config = find_config_tree_str(cmd, devices_devicesfile_CFG, NULL);
+ /* when a devices file is not used, hints should have devices_file:. */
+ if (!cmd->enable_devices_file || !df_hint || !df_config) {
+ if (df_hint[0] != '.') {
+ log_debug("ignore hints with different devices_file: not enabled vs %s", df_hint);
+ *needs_refresh = 1;
+ break;
+ }
+ } else if (strcmp(df_hint, df_config)) {
+ log_debug("ignore hints with different devices_file: %s vs %s", df_hint, df_config);
+ *needs_refresh = 1;
+ break;
+ }
+ continue;
+ }
+
keylen = strlen("devs_hash:");
if (!strncmp(_hint_line, "devs_hash:", keylen)) {
if (sscanf(_hint_line + keylen, "%u %u", &read_hash, &read_count) != 2) {
@@ -827,8 +850,12 @@ static int _read_hint_file(struct cmd_context *cmd, struct dm_list *hints, int *
if (!(iter = dev_iter_create(NULL, 0)))
return 0;
while ((dev = dev_iter_get(cmd, iter))) {
+ if (cmd->enable_devices_file && !get_du_for_dev(cmd, dev))
+ continue;
+
if (!_dev_in_hint_hash(cmd, dev))
continue;
+
(void) dm_strncpy(devpath, dev_name(dev), sizeof(devpath));
calc_hash = calc_crc(calc_hash, (const uint8_t *)devpath, strlen(devpath));
calc_count++;
@@ -887,6 +914,7 @@ int write_hint_file(struct cmd_context *cmd, int newhints)
struct device *dev;
const char *vgname;
char *filter_str = NULL;
+ const char *config_devices_file = NULL;
uint32_t hash = INITIAL_CRC;
uint32_t count = 0;
time_t t;
@@ -947,6 +975,19 @@ int write_hint_file(struct cmd_context *cmd, int newhints)
fprintf(fp, "scan_lvs:%d\n", cmd->scan_lvs);
+ /*
+ * Only associate hints with the default/system devices file.
+ * If no default/system devices file is used, "." is set.
+ * If we are using a devices file other than the config setting
+ * (from --devicesfile), then we should not be using hints and
+ * shouldn't get here.
+ */
+ config_devices_file = find_config_tree_str(cmd, devices_devicesfile_CFG, NULL);
+ if (cmd->enable_devices_file && !cmd->devicesfile && config_devices_file)
+ fprintf(fp, "devices_file:%s\n", config_devices_file);
+ else
+ fprintf(fp, "devices_file:.\n");
+
/*
* iterate through all devs and write a line for each
* dev flagged DEV_SCAN_FOUND_LABEL
@@ -964,6 +1005,9 @@ int write_hint_file(struct cmd_context *cmd, int newhints)
* 2. add PVs to the hint file
*/
while ((dev = dev_iter_get(cmd, iter))) {
+ if (cmd->enable_devices_file && !get_du_for_dev(cmd, dev))
+ continue;
+
if (!_dev_in_hint_hash(cmd, dev)) {
if (dev->flags & DEV_SCAN_FOUND_LABEL) {
/* should never happen */
@@ -1328,7 +1372,7 @@ int get_hints(struct cmd_context *cmd, struct dm_list *hints_out, int *newhints,
}
/*
- * couln't read file for some reason, not normal, just skip using hints
+ * couldn't read file for some reason, not normal, just skip using hints
*/
if (!_read_hint_file(cmd, &hints_list, &needs_refresh)) {
log_debug("get_hints: read fail");
@@ -1353,7 +1397,6 @@ int get_hints(struct cmd_context *cmd, struct dm_list *hints_out, int *newhints,
/* create new hints after scan */
*newhints = NEWHINTS_REFRESH;
return 0;
-
}
/*
@@ -1385,8 +1428,8 @@ int get_hints(struct cmd_context *cmd, struct dm_list *hints_out, int *newhints,
_apply_hints(cmd, &hints_list, vgname, devs_in, devs_out);
- log_debug("get_hints: applied using %d other %d",
- dm_list_size(devs_out), dm_list_size(devs_in));
+ log_debug("get_hints: applied using %d other %d vgname %s",
+ dm_list_size(devs_out), dm_list_size(devs_in), vgname ?: "");
dm_list_splice(hints_out, &hints_list);
diff --git a/lib/label/label.c b/lib/label/label.c
index 0fc832c08..318cf4d6c 100644
--- a/lib/label/label.c
+++ b/lib/label/label.c
@@ -25,6 +25,7 @@
#include "lib/label/hints.h"
#include "lib/metadata/metadata.h"
#include "lib/format_text/layout.h"
+#include "lib/device/device_id.h"
#include <sys/stat.h>
#include <fcntl.h>
@@ -805,16 +806,6 @@ static int _scan_list(struct cmd_context *cmd, struct dev_filter *f,
}
}
- /*
- * This will search the system's /dev for new path names and
- * could help us reopen the device if it finds a new preferred
- * path name for this dev's major:minor. It does that by
- * inserting a new preferred path name on dev->aliases. open
- * uses the first name from that list.
- */
- log_debug_devs("Scanning refreshing device paths.");
- dev_cache_scan();
-
/* Put devs that failed to open back on the original list to retry. */
dm_list_splice(devs, &reopen_devs);
goto scan_more;
@@ -936,6 +927,12 @@ static void _prepare_open_file_limit(struct cmd_context *cmd, unsigned int num_d
#endif
}
+/*
+ * Currently the only caller is pvck which probably doesn't need
+ * deferred filters checked after the read... it wants to know if
+ * anything has the pvid, even a dev that might be filtered.
+ */
+
int label_scan_for_pvid(struct cmd_context *cmd, char *pvid, struct device **dev_out)
{
char buf[LABEL_SIZE] __attribute__((aligned(8)));
@@ -948,7 +945,20 @@ int label_scan_for_pvid(struct cmd_context *cmd, char *pvid, struct device **dev
dm_list_init(&devs);
- dev_cache_scan();
+ /*
+ * Creates a list of available devices, does not open or read any,
+ * and does not filter them.
+ */
+ if (!setup_devices(cmd)) {
+ log_error("Failed to set up devices.");
+ return 0;
+ }
+
+ /*
+ * Iterating over all available devices with cmd->filter filters
+ * devices; those returned from dev_iter_get are the devs that
+ * pass filters, and are those we can use.
+ */
if (!(iter = dev_iter_create(cmd->filter, 0))) {
log_error("Scanning failed to get devices.");
@@ -1022,6 +1032,7 @@ int label_scan(struct cmd_context *cmd)
struct device_list *devl, *devl2;
struct device *dev;
uint64_t max_metadata_size_bytes;
+ int device_ids_invalid = 0;
int using_hints;
int create_hints = 0; /* NEWHINTS_NONE */
@@ -1038,12 +1049,17 @@ int label_scan(struct cmd_context *cmd)
}
/*
- * dev_cache_scan() creates a list of devices on the system
- * (saved in in dev-cache) which we can iterate through to
- * search for LVM devs. The dev cache list either comes from
- * looking at dev nodes under /dev, or from udev.
+ * Creates a list of available devices, does not open or read any,
+ * and does not filter them. The list of all available devices
+ * is kept in "dev-cache", and comes from /dev entries or libudev.
+ * The list of devs found here needs to be filtered to get the
+ * list of devs we can use. The dev_iter calls using cmd->filter
+ * are what filters the devs.
*/
- dev_cache_scan();
+ if (!setup_devices(cmd)) {
+ log_error("Failed to set up devices.");
+ return 0;
+ }
/*
* If we know that there will be md components with an end
@@ -1196,15 +1212,26 @@ int label_scan(struct cmd_context *cmd)
if (!validate_hints(cmd, &hints_list)) {
log_debug("Will scan %d remaining devices", dm_list_size(&all_devs));
_scan_list(cmd, cmd->filter, &all_devs, 0, NULL);
+ /* scan_devs are the devs that have been scanned */
+ dm_list_splice(&scan_devs, &all_devs);
free_hints(&hints_list);
using_hints = 0;
create_hints = 0;
+ /* invalid hints means a new dev probably appeared and
+ we should search for any missing pvids again. */
+ unlink_searched_devnames(cmd);
} else {
/* The hints may be used by another device iteration. */
dm_list_splice(&cmd->hints, &hints_list);
}
}
+ /*
+ * Check if the devices_file content is up to date and
+ * if not update it.
+ */
+ device_ids_validate(cmd, &scan_devs, &device_ids_invalid, 0);
+
dm_list_iterate_items_safe(devl, devl2, &all_devs) {
dm_list_del(&devl->list);
free(devl);
@@ -1239,7 +1266,7 @@ int label_scan(struct cmd_context *cmd)
* (create_hints variable has NEWHINTS_X value which indicates
* the reason for creating the new hints.)
*/
- if (create_hints)
+ if (create_hints && !device_ids_invalid)
write_hint_file(cmd, create_hints);
return 1;
diff --git a/lib/label/label.h b/lib/label/label.h
index 78724c1ce..fae0f1bcc 100644
--- a/lib/label/label.h
+++ b/lib/label/label.h
@@ -112,7 +112,6 @@ void label_scan_invalidate(struct device *dev);
void label_scan_invalidate_lv(struct cmd_context *cmd, struct logical_volume *lv);
void label_scan_drop(struct cmd_context *cmd);
void label_scan_destroy(struct cmd_context *cmd);
-void label_scan_confirm(struct device *dev);
int label_scan_setup_bcache(void);
int label_scan_open(struct device *dev);
int label_scan_open_excl(struct device *dev);