1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-06 17:18:29 +03:00
lvm2/tools/vgextend.c
David Teigland dc5e6a2cc1 device usage based on devices file
The devices file /etc/lvm/devices/system.devices
is a list of devices that lvm can use.
The option --devicesfile can specify a different file
name with a separate set of devices for lvm to use.
This option allows different applications to use
lvm on different sets of devices.

In most cases (with limited exceptions), lvm will not
read or use a device not listed in the devices file.
When the devices file is used, the filter-regex is
not used and the filter settings in lvm.conf are
ignored.  filter-deviceid is used when the devices
file is enabled and rejects any device that does not
match an entry in the devices file.

Set use_devicesfile = 0 in lvm.conf or set
--devicesfile "" on the command line to disable the
use of a devices file.  When disabled, lvm will see
and use any device on the system that passes the
regex filter.

A device_id, e.g. wwid or serial number from sysfs,
is a unique ID that identifies a device without
reading it.  Two devices with identical content
should have different device_ids in most common
cases.  The device_id is used in the devices file
and is included in VG metadata sections.

Each device_id has a device_id_type which indicates
where the device_id comes from, e.g. "sys_wwid"
means the device_id comes from the sysfs wwid file.
Others are sys_serial, mpath_uuid, loop_file, devname.
(devname is the device path which is a fallback when
no other proper device_id_type is available.)

filter-deviceid permits lvm to use only devices
on the system that have a device_id matching a
devices file entry.  Using the device_id, lvm can
determine the set of devices to use without reading
any devices, so the devices file will constrain lvm
in two ways:
1. it limits the devices that lvm will read.
2. it limits the devices that lvm will use.

In some uncommon cases, e.g. when devices have no
unique ID and device_id has to fall back to using
the devname, lvm may need to read all devices on the
system to determine which ones correspond to the
devices file entries.  In this case, the devices file
does not limit the devices that lvm reads, but it does
limit the devices that lvm uses.

pvcreate/vgcreate/vgextend are not constrained by
the devices file, and will look outside it to find
the new PV.  They assign the new PV a device_id
and add it to the devices file.  It is also possible
to explicitly add new PVs to the devices file before
using them in pvcreate/etc, in which case these commands
would not need to access devices outside the devices file.

vgimportdevices VG looks at all devices on the system
to find an existing VG and add its devices to the
devices file.  The command is not limited by an
existing devices file.  The command will also add
device_ids to the VG metadata if the VG does not yet
include device_ids.  vgimportdevices -a imports devices
for all accessible VGs.  Since vgimportdevices does not
limit itself to devices in an existing devices file, the
lvm.conf regex filter applies.  Adding --foreign will
import devices for foreign VGs, but device_ids are
not added to foreign VGs.  Incomplete VGs are not
imported.

The lvmdevices command manages the devices file.
The primary purpose is to edit the devices file,
but it will read PV headers to find/check PVIDs.
(It does not read, process or modify VG metadata.)

lvmdevices
. Displays devices file entries.
lvmdevices --check
. Checks devices file entries.
lvmdevices --update
. Updates devices file entries.
lvmdevices --adddev <devname>
. Adds devices_file entry (reads pv header).
lvmdevices --deldev <devname>
. Removes devices file entry.
lvmdevices --addpvid <pvid>
. Reads pv header of all devices to find <pvid>,
  and if found adds devices file entry.
lvmdevices --delpvid <pvid>
. Removes devices file entry.

The vgimportclone command has a new option --importdevices
that does the equivalent of vgimportdevices with the cloned
devices that are being imported.  The devices are "uncloned"
(new vgname and pvids) while at the same time adding the
devices to the devices file.  This allows cloned PVs to be
imported without duplicate PVs ever appearing on the system.

TODO:
device_id_type for other special devices (nbd, drbd, others?)
dmeventd run commands with --devicesfile dmeventd.devices

OTHER:
allow operations with duplicate pvs if device id and size match only one dev
shortsystemid crc of systemid and written in pv header
use shortsystemid for new filter and orphan PV ownership
command to set boot flag on devices file entries needed for boot
vgchange -ay option to use devices file entries with boot flag
2020-11-25 14:47:29 -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;
}