1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-22 17:35:59 +03:00
lvm2/tools/vgextend.c
David Teigland 116aa71d50 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:

- read the devices file
  . add struct dev_use (du) struct to cmd->use_devices for each entry
- dev_cache_scan
  . add struct device (dev) to dev_cache
- match devices file entries to devices
  . match each du on cmd->use_devices to a dev in dev_cache
  . sets du->dev, dev->id, dev->flags MATCHED_USE_ID
- label_scan
  . filters applied that do not require data from device
  . filter-deviceid skips devs without MATCHED_USE_ID, i.e.
    devs that are not in the devices file
  . read label from dev
  . filters applied that require data from device
  . read metadata from dev
  . lvmcache info/vginfo structs created for PVs/VGs
- 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.
  . detect incorrect match because PVID in the devices file entry
    does not match the PVID found when the device was read
  . undo incorrect device match between du and dev
  . 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

man page updates
2021-02-23 11:17:34 -06:00

207 lines
5.1 KiB
C

/*
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
* Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
*
* This file is part of LVM2.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v.2.1.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "tools.h"
struct vgextend_params {
struct pvcreate_params pp;
};
static int _restore_pv(struct volume_group *vg, const char *pv_name)
{
struct pv_list *pvl = NULL;
pvl = find_pv_in_vg(vg, pv_name);
if (!pvl) {
log_warn("WARNING: PV %s not found in VG %s", pv_name, vg->name);
return 0;
}
if (!pvl->pv->dev) {
log_warn("WARNING: The PV %s is still missing.", pv_name);
return 0;
}
if (pvl->pv->status & MISSING_PV)
goto clear_flag;
/*
* when the PV has no used PE's vg_read clears the MISSING_PV flag
* and sets this so we know.
*/
if (pvl->pv->unused_missing_cleared)
goto clear_flag;
log_warn("WARNING: PV %s was not missing in VG %s", pv_name, vg->name);
return 0;
clear_flag:
pvl->pv->status &= ~MISSING_PV;
return 1;
}
static int _vgextend_restoremissing(struct cmd_context *cmd __attribute__((unused)),
const char *vg_name, struct volume_group *vg,
struct processing_handle *handle)
{
struct vgextend_params *vp = (struct vgextend_params *) handle->custom_handle;
struct pvcreate_params *pp = &vp->pp;
int fixed = 0;
unsigned i;
if (!archive(vg))
return_0;
for (i = 0; i < pp->pv_count; i++)
if (_restore_pv(vg, pp->pv_names[i]))
fixed++;
if (!fixed) {
log_error("No PV has been restored.");
return ECMD_FAILED;
}
if (!vg_write(vg) || !vg_commit(vg))
return_ECMD_FAILED;
backup(vg);
log_print_unless_silent("Volume group \"%s\" successfully extended", vg_name);
return ECMD_PROCESSED;
}
static int _vgextend_single(struct cmd_context *cmd, const char *vg_name,
struct volume_group *vg, struct processing_handle *handle)
{
struct vgextend_params *vp = (struct vgextend_params *) handle->custom_handle;
struct pvcreate_params *pp = &vp->pp;
uint32_t mda_copies;
uint32_t mda_used;
int ret = ECMD_FAILED;
if (arg_is_set(cmd, metadataignore_ARG) &&
(pp->force == PROMPT) && !pp->yes &&
(vg_mda_copies(vg) != VGMETADATACOPIES_UNMANAGED) &&
(yes_no_prompt("Override preferred number of copies of VG %s metadata? [y/n]: ", vg_name) == 'n')) {
log_error("Volume group %s not changed", vg_name);
return ECMD_FAILED;
}
if (!archive(vg))
return_ECMD_FAILED;
if (!vg_extend_each_pv(vg, pp))
goto_out;
if (arg_is_set(cmd, metadataignore_ARG)) {
mda_copies = vg_mda_copies(vg);
mda_used = vg_mda_used_count(vg);
if ((mda_copies != VGMETADATACOPIES_UNMANAGED) &&
(mda_copies != mda_used)) {
log_warn("WARNING: Changing preferred number of copies of VG %s metadata from %" PRIu32 " to %" PRIu32,
vg_name, mda_copies, mda_used);
vg_set_mda_copies(vg, mda_used);
}
}
log_verbose("Volume group \"%s\" will be extended by %d new physical volumes", vg_name, pp->pv_count);
if (!vg_write(vg) || !vg_commit(vg))
goto_out;
backup(vg);
log_print_unless_silent("Volume group \"%s\" successfully extended", vg_name);
ret = ECMD_PROCESSED;
out:
return ret;
}
int vgextend(struct cmd_context *cmd, int argc, char **argv)
{
struct processing_handle *handle;
struct vgextend_params vp;
struct pvcreate_params *pp = &vp.pp;
unsigned restoremissing = arg_is_set(cmd, restoremissing_ARG);
const char *vg_name;
int ret;
if (!argc) {
log_error("Please enter volume group name and "
"physical volume(s)");
return EINVALID_CMD_LINE;
}
vg_name = skip_dev_dir(cmd, argv[0], NULL);
argc--;
argv++;
pvcreate_params_set_defaults(pp);
if (!pvcreate_params_from_args(cmd, pp))
return EINVALID_CMD_LINE;
pp->pv_count = argc;
pp->pv_names = argv;
/* Don't create a new PV on top of an existing PV like pvcreate does. */
pp->preserve_existing = 1;
/* pvcreate within vgextend cannot be forced. */
pp->force = PROMPT;
if (!lock_global(cmd, "ex"))
return_ECMD_FAILED;
clear_hint_file(cmd);
cmd->edit_devices_file = 1;
lvmcache_label_scan(cmd);
if (!(handle = init_processing_handle(cmd, NULL))) {
log_error("Failed to initialize processing handle.");
return ECMD_FAILED;
}
if (!restoremissing) {
if (!pvcreate_each_device(cmd, handle, pp)) {
destroy_processing_handle(cmd, handle);
return_ECMD_FAILED;
}
}
unlock_devices_file(cmd);
/*
* It is always ok to add new PVs to a VG - even if there are
* missing PVs. No LVs are affected by this operation, but
* repair processes - particularly for RAID segtypes - can
* be facilitated.
*/
cmd->handles_missing_pvs = 1;
handle->custom_handle = &vp;
ret = process_each_vg(cmd, 0, NULL, vg_name, NULL,
READ_FOR_UPDATE | PROCESS_SKIP_SCAN, 0, handle,
restoremissing ? &_vgextend_restoremissing : &_vgextend_single);
destroy_processing_handle(cmd, handle);
return ret;
}