diff options
author | David Teigland <teigland@redhat.com> | 2016-05-16 14:35:43 -0500 |
---|---|---|
committer | David Teigland <teigland@redhat.com> | 2016-05-25 11:50:00 -0500 |
commit | aef9cb905835f9482707742780de029131659722 (patch) | |
tree | 29fdb7f2d1ad05e5e3a8be30181f7d6bb3668f85 | |
parent | 7fffcce9247cba8ecb40a1c7b0c6d18a5cde2165 (diff) | |
download | lvm2-dev-dct-vgimportclone-2.tar.gz |
pvchange: add importclone optiondev-dct-vgimportclone-2
This is a native implementation of vgimportclone.
pvchange --importclone PVs
All cloned PVs from the VG must be imported together and
named as args.
-rw-r--r-- | lib/Makefile.in | 1 | ||||
-rw-r--r-- | lib/commands/toolcontext.c | 9 | ||||
-rw-r--r-- | lib/filters/filter-internal.c | 81 | ||||
-rw-r--r-- | lib/filters/filter.h | 4 | ||||
-rw-r--r-- | lib/misc/lvm-globals.c | 11 | ||||
-rw-r--r-- | lib/misc/lvm-globals.h | 2 | ||||
-rw-r--r-- | tools/args.h | 2 | ||||
-rw-r--r-- | tools/commands.h | 2 | ||||
-rw-r--r-- | tools/pvchange.c | 420 |
9 files changed, 527 insertions, 5 deletions
diff --git a/lib/Makefile.in b/lib/Makefile.in index 467ef9077..451d96d8f 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -76,6 +76,7 @@ SOURCES =\ filters/filter-partitioned.c \ filters/filter-type.c \ filters/filter-usable.c \ + filters/filter-internal.c \ format_text/archive.c \ format_text/archiver.c \ format_text/export.c \ diff --git a/lib/commands/toolcontext.c b/lib/commands/toolcontext.c index 1e3f14a79..2df73c145 100644 --- a/lib/commands/toolcontext.c +++ b/lib/commands/toolcontext.c @@ -1053,7 +1053,7 @@ static int _init_dev_cache(struct cmd_context *cmd) return 1; } -#define MAX_FILTERS 8 +#define MAX_FILTERS 9 static struct dev_filter *_init_lvmetad_filter_chain(struct cmd_context *cmd) { @@ -1078,6 +1078,13 @@ static struct dev_filter *_init_lvmetad_filter_chain(struct cmd_context *cmd) nr_filt++; } + /* internal filter used by command processing. */ + if (!(filters[nr_filt] = internal_filter_create())) { + log_error("Failed to create internal device filter"); + goto bad; + } + nr_filt++; + /* global regex filter. Optional. */ if ((cn = find_config_tree_node(cmd, devices_global_filter_CFG, NULL))) { if (!(filters[nr_filt] = regex_filter_create(cn->v))) { diff --git a/lib/filters/filter-internal.c b/lib/filters/filter-internal.c new file mode 100644 index 000000000..513370942 --- /dev/null +++ b/lib/filters/filter-internal.c @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2006 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 "lib.h" +#include "filter.h" + +static DM_LIST_INIT(_allow_devs); + +int internal_filter_allow(struct dm_pool *mem, struct device *dev) +{ + struct device_list *devl; + + if (!(devl = dm_pool_alloc(mem, sizeof(*devl)))) { + log_error("device_list element allocation failed"); + return 0; + } + devl->dev = dev; + + dm_list_add(&_allow_devs, &devl->list); + return 1; +} + +void internal_filter_clear(void) +{ + dm_list_init(&_allow_devs); +} + +static int _ignore_internal(struct dev_filter *f __attribute__((unused)), + struct device *dev) +{ + struct device_list *devl; + + if (!internal_filtering()) + return 1; + + dm_list_iterate_items(devl, &_allow_devs) { + if (devl->dev == dev) + return 1; + } + + log_debug_devs("%s: Skipping to import clone devices.", dev_name(dev)); + return 0; +} + +static void _destroy(struct dev_filter *f) +{ + if (f->use_count) + log_error(INTERNAL_ERROR "Destroying internal filter while in use %u times.", f->use_count); + + dm_free(f); +} + +struct dev_filter *internal_filter_create(void) +{ + struct dev_filter *f; + + if (!(f = dm_zalloc(sizeof(*f)))) { + log_error("md filter allocation failed"); + return NULL; + } + + f->passes_filter = _ignore_internal; + f->destroy = _destroy; + f->use_count = 0; + + log_debug_devs("internal filter initialised."); + + return f; +} + diff --git a/lib/filters/filter.h b/lib/filters/filter.h index ebd25e2be..d75f6e11c 100644 --- a/lib/filters/filter.h +++ b/lib/filters/filter.h @@ -32,6 +32,10 @@ struct dev_filter *persistent_filter_create(struct dev_types *dt, const char *file); struct dev_filter *sysfs_filter_create(void); +struct dev_filter *internal_filter_create(void); +int internal_filter_allow(struct dm_pool *mem, struct device *dev); +void internal_filter_clear(void); + /* * patterns must be an array of strings of the form: * [ra]<sep><regex><sep>, eg, diff --git a/lib/misc/lvm-globals.c b/lib/misc/lvm-globals.c index b7af353ae..9c78f3e33 100644 --- a/lib/misc/lvm-globals.c +++ b/lib/misc/lvm-globals.c @@ -26,6 +26,7 @@ static int _verbose_level = VERBOSE_BASE_LEVEL; static int _silent = 0; static int _test = 0; static int _md_filtering = 0; +static int _internal_filtering = 0; static int _fwraid_filtering = 0; static int _pvmove = 0; static int _full_scan_done = 0; /* Restrict to one full scan during each cmd */ @@ -79,6 +80,11 @@ void init_md_filtering(int level) _md_filtering = level; } +void init_internal_filtering(int level) +{ + _internal_filtering = level; +} + void init_fwraid_filtering(int level) { _fwraid_filtering = level; @@ -242,6 +248,11 @@ int md_filtering(void) return _md_filtering; } +int internal_filtering(void) +{ + return _internal_filtering; +} + int fwraid_filtering(void) { return _fwraid_filtering; diff --git a/lib/misc/lvm-globals.h b/lib/misc/lvm-globals.h index 77c01b413..14a7d4366 100644 --- a/lib/misc/lvm-globals.h +++ b/lib/misc/lvm-globals.h @@ -26,6 +26,7 @@ void init_verbose(int level); void init_silent(int silent); void init_test(int level); void init_md_filtering(int level); +void init_internal_filtering(int level); void init_fwraid_filtering(int level); void init_pvmove(int level); void init_full_scan_done(int level); @@ -60,6 +61,7 @@ void set_sysfs_dir_path(const char *path); int test_mode(void); int md_filtering(void); +int internal_filtering(void); int fwraid_filtering(void); int pvmove_mode(void); int full_scan_done(void); diff --git a/tools/args.h b/tools/args.h index ef9dbc951..8fcaee8b0 100644 --- a/tools/args.h +++ b/tools/args.h @@ -49,6 +49,8 @@ arg(ignorelockingfailure_ARG, '\0', "ignorelockingfailure", NULL, 0) arg(ignoremonitoring_ARG, '\0', "ignoremonitoring", NULL, 0) arg(ignoreskippedcluster_ARG, '\0', "ignoreskippedcluster", NULL, 0) arg(ignoreunsupported_ARG, '\0', "ignoreunsupported", NULL, 0) +arg(importclone_ARG, '\0', "importclone", NULL, 0) +arg(importvg_ARG, '\0', "importvg", NULL, 0) arg(labelsector_ARG, '\0', "labelsector", int_arg, 0) arg(lockopt_ARG, '\0', "lockopt", string_arg, 0) arg(lockstart_ARG, '\0', "lockstart", NULL, 0) diff --git a/tools/commands.h b/tools/commands.h index da432a708..bf3e7ac67 100644 --- a/tools/commands.h +++ b/tools/commands.h @@ -741,7 +741,7 @@ xx(pvchange, all_ARG, allocatable_ARG, allocation_ARG, autobackup_ARG, deltag_ARG, addtag_ARG, force_ARG, ignoreskippedcluster_ARG, metadataignore_ARG, - select_ARG, test_ARG, uuid_ARG) + select_ARG, test_ARG, uuid_ARG, importclone_ARG, importvg_ARG) xx(pvresize, "Resize physical volume(s)", diff --git a/tools/pvchange.c b/tools/pvchange.c index e100b80c5..b005bb5a0 100644 --- a/tools/pvchange.c +++ b/tools/pvchange.c @@ -14,12 +14,414 @@ */ #include "tools.h" +#include "lvmcache.h" +#include "lvmetad-client.h" +#include "filter.h" struct pvchange_params { unsigned done; unsigned total; + + int import_vg; + int found_args; + struct dm_list arg_import; + const char *base_vgname; + const char *old_vgname; + const char *new_vgname; +}; + +struct pvchange_device { + struct dm_list list; + struct device *dev; + unsigned found_in_vg : 1; }; +static int _pvchange_import_check_pv_single(struct cmd_context *cmd, struct volume_group *vg, + struct physical_volume *pv, struct processing_handle *handle) +{ + struct pvchange_params *pp = (struct pvchange_params *) handle->custom_handle; + struct pvchange_device *pd; + + pp->total++; + + if (vg && is_orphan_vg(vg->name)) { + log_error("Cannot importclone on orphan PV %s.", dev_name(pv->dev)); + return ECMD_FAILED; + } + + if (!(pd = dm_pool_zalloc(cmd->mem, sizeof(*pd)))) { + log_error("alloc failed."); + return ECMD_FAILED; + } + + pd->dev = pv->dev; + dm_list_add(&pp->arg_import, &pd->list); + + log_debug("pvchange dev %s VG %s found to import", + dev_name(pd->dev), vg ? vg->name : "<none>"); + + pp->found_args++; + + return ECMD_PROCESSED; +} + +static int _pvchange_import_check_vg_single(struct cmd_context *cmd, const char *vg_name, + struct volume_group *vg, struct processing_handle *handle) +{ + char uuid[64] __attribute__((aligned(8))); + struct pvchange_params *pp = (struct pvchange_params *) handle->custom_handle; + struct pvchange_device *pd; + struct pv_list *pvl; + int devs_used_for_lv = 0; + int found; + + if (vg_is_exported(vg) && !pp->import_vg) { + log_error("VG %s is exported, use the --importvg option.", vg->name); + goto fail; + } + + if (vg_status(vg) & PARTIAL_VG) { + log_error("VG %s is partial, it must be complete.", vg->name); + goto fail; + } + + /* + * N.B. lvs_in_vg_activated() is not smart enough to distinguish + * between LVs that are active in the original VG vs the cloned VG + * that's being imported, so check DEV_USED_FOR_LV. + */ + dm_list_iterate_items(pvl, &vg->pvs) { + if (pvl->pv->dev->flags & DEV_USED_FOR_LV) { + log_error("Device %s has active LVs, deactivate first.", dev_name(pvl->pv->dev)); + devs_used_for_lv++; + } + } + + if (devs_used_for_lv) + goto fail; + + /* + * The arg_import list must match the PVs in VG. + */ + + dm_list_iterate_items(pvl, &vg->pvs) { + found = 0; + + dm_list_iterate_items(pd, &pp->arg_import) { + if (pvl->pv->dev != pd->dev) + continue; + pd->found_in_vg = 1; + found = 1; + break; + } + + if (!found) { + if (!id_write_format(&pvl->pv->id, uuid, sizeof(uuid))) + goto fail; + + /* all PVs in the VG must be imported together, pvl is missing from args. */ + log_error("PV with UUID %s is part of VG %s, but is not included in the devices to import.", + uuid, vg->name); + log_error("All PVs in the VG must be imported together."); + goto fail; + } + } + + dm_list_iterate_items(pd, &pp->arg_import) { + if (!pd->found_in_vg) { + /* device arg is not in the VG. */ + log_error("Device %s was not found in VG %s.", dev_name(pd->dev), vg->name); + log_error("The devices to import must match the devices in the VG."); + goto fail; + } + } + + return ECMD_PROCESSED; +fail: + return ECMD_FAILED; +} + +/* + * FIXME: It's ugly that we have to write the VG once to change each PV ID, and + * then write the VG again later to change the other things. The metadata + * writing functions don't seem to support writing everything at once. It + * should be possible to change the PV UUIDs in _pvchange_import_vg_single. + */ + +static int _pvchange_import_pv_single(struct cmd_context *cmd, struct volume_group *vg, + struct physical_volume *pv, struct processing_handle *handle) +{ + struct pvchange_params *pp = (struct pvchange_params *) handle->custom_handle; + struct pvchange_device *pd; + int found = 0; + + /* Check again that this PV is one we are importing. */ + dm_list_iterate_items(pd, &pp->arg_import) { + if (pd->dev == pv->dev) { + found = 1; + break; + } + } + + if (!found) { + log_error("PV device %s does not match any command arg.", dev_name(pv->dev)); + return ECMD_FAILED; + } + + if (!archive(vg)) + return_ECMD_FAILED; + + log_debug("pvchange import writing new uuid on dev %s.", dev_name(pv->dev)); + + /* Low level pv_write code needs old_id to be set! */ + memcpy(&pv->old_id, &pv->id, sizeof(pv->id)); + + if (!id_create(&pv->id)) { + log_error("Failed to generate new random UUID for %s.", dev_name(pv->dev)); + return ECMD_FAILED; + } + + if (!pv_write(cmd, pv, 1)) { + log_error("Failed to write PV for %s.", dev_name(pv->dev)); + return ECMD_FAILED; + } + + if (!vg_write(vg) || !vg_commit(vg)) { + log_error("Failed to change VG for %s.", dev_name(pv->dev)); + return ECMD_FAILED; + } + + return ECMD_PROCESSED; +} + +static int _pvchange_import_vg_single(struct cmd_context *cmd, const char *vg_name, + struct volume_group *vg, struct processing_handle *handle) +{ + struct pvchange_params *pp = (struct pvchange_params *) handle->custom_handle; + struct pv_list *pvl, *new_pvl; + struct lv_list *lvl; + + if (pp->import_vg) + vg->status &= ~EXPORTED_VG; + + if (!id_create(&vg->id)) + goto_bad; + + /* Low level vg_write code needs old_name to be set! */ + vg->old_name = vg->name; + + if (!(vg->name = dm_pool_strdup(vg->vgmem, pp->new_vgname))) + goto_bad; + + dm_list_iterate_items(pvl, &vg->pvs) { + if (!(new_pvl = dm_pool_zalloc(vg->vgmem, sizeof(*new_pvl)))) + goto_bad; + + new_pvl->pv = pvl->pv; + + if (!(pvl->pv->vg_name = dm_pool_strdup(vg->vgmem, pp->new_vgname))) + goto_bad; + + if (pp->import_vg) + new_pvl->pv->status &= ~EXPORTED_VG; + + /* + * FIXME: It would be nice to write new PV uuids here, but the + * metadata writing functions don't seem to be able to do this, + * so we have a separate loop that writes new PV IDs earlier. + */ + /* + memcpy(&new_pvl->pv->old_id, &new_pvl->pv->id, sizeof(new_pvl->pv->id)); + id_create(&new_pvl->pv->id); + dm_list_add(&vg->pv_write_list, &new_pvl->list); + */ + } + + dm_list_iterate_items(lvl, &vg->lvs) + memcpy(&lvl->lv->lvid, &vg->id, sizeof(vg->id)); + + if (!vg_write(vg) || !vg_commit(vg)) + goto_bad; + + return ECMD_PROCESSED; +bad: + return ECMD_FAILED; +} + +static int _pvchange_importclone(struct cmd_context *cmd, struct processing_handle *handle, + int argc, char **argv) +{ + struct pvchange_params *pp = (struct pvchange_params *) handle->custom_handle; + struct dm_list vgnameids_on_system; /* vgnameid_list */ + struct vgnameid_list *vgnl; + struct pvchange_device *pd; + struct lvmcache_info *info; + const char *vgname; + char base_vgname[NAME_LEN] = { 0 }; + char tmp_vgname[NAME_LEN] = { 0 }; + unsigned int vgname_count; + int ret; + + dm_list_init(&vgnameids_on_system); + dm_list_init(&pp->arg_import); + + pp->import_vg = arg_is_set(cmd, importvg_ARG); + + lvmetad_set_disabled(cmd, LVMETAD_DISABLE_REASON_DUPLICATES); + lvmetad_make_unused(cmd); + + if (!lock_vol(cmd, VG_GLOBAL, LCK_VG_WRITE, NULL)) { + log_error("Unable to obtain global lock."); + return ECMD_FAILED; + } + + if (!lockd_gl(cmd, "ex", 0)) + goto_bad; + cmd->lockd_gl_disable = 1; + + /* + * Find the devices being imported which are named on the command line. + * They may be in the list of unchosen duplicates. + */ + + log_debug("Finding devices to import."); + cmd->command->flags |= ENABLE_DUPLICATE_DEVS; + ret = process_each_pv(cmd, argc, argv, NULL, 0, READ_ALLOW_EXPORTED, handle, _pvchange_import_check_pv_single); + if (ret != ECMD_PROCESSED) + goto_bad; + + if (pp->found_args != argc) { + log_error("Failed to find all devices."); + goto_bad; + } + + /* + * Find the VG name of the PVs being imported, save as old_vgname. + * N.B. If pd->dev is a duplicate, then it may not match info->dev. + */ + + dm_list_iterate_items(pd, &pp->arg_import) { + if (!(info = lvmcache_info_from_pvid(pd->dev->pvid, 0))) { + log_error("Failed to find PVID for device %s in lvmcache.", dev_name(pd->dev)); + goto_bad; + } + + if (!(vgname = lvmcache_vgname_from_info(info))) { + log_error("Failed to find VG name for device %s in lvmcache.", dev_name(pd->dev)); + goto_bad; + } + + if (!pp->old_vgname) { + if (!(pp->old_vgname = dm_pool_strdup(cmd->mem, vgname))) + goto_bad; + } else { + if (strcmp(pp->old_vgname, vgname)) { + log_error("Devices must be from the same VG."); + goto_bad; + } + } + } + + /* + * Pick a new VG name, save as new_vgname. The new name begins with + * the basevgname or old_vgname, plus a $i suffix, if necessary, to + * make it unique. This requires comparing the old_vgname with all the + * VG names on the system. + */ + + if (pp->base_vgname) { + snprintf(base_vgname, sizeof(base_vgname) - 1, "%s", pp->base_vgname); + memcpy(tmp_vgname, base_vgname, NAME_LEN); + vgname_count = 0; + } else { + snprintf(base_vgname, sizeof(base_vgname) - 1, "%s", pp->old_vgname); + snprintf(tmp_vgname, sizeof(tmp_vgname) - 1, "%s1", pp->old_vgname); + vgname_count = 1; + } + + if (!get_vgnameids(cmd, &vgnameids_on_system, NULL, 0)) + goto_bad; + +retry_name: + dm_list_iterate_items(vgnl, &vgnameids_on_system) { + if (!strcmp(vgnl->vg_name, tmp_vgname)) { + vgname_count++; + snprintf(tmp_vgname, sizeof(tmp_vgname) - 1, "%s%u", base_vgname, vgname_count); + goto retry_name; + } + } + + if (!(pp->new_vgname = dm_pool_strdup(cmd->mem, tmp_vgname))) { + log_error("strdup failed."); + goto_bad; + } + log_debug("Using new VG name %s.", pp->new_vgname); + + /* + * Create a device filter so that we are only working with the devices + * in arg_import. With the original devs hidden (that arg_import were + * cloned from), we can read and write the cloned PVs and VG without + * touching the original PVs/VG. + */ + + init_internal_filtering(1); + dm_list_iterate_items(pd, &pp->arg_import) + internal_filter_allow(cmd->mem, pd->dev); + lvmcache_destroy(cmd, 1, 0); + dev_cache_full_scan(cmd->full_filter); + + /* + * Read old_vgname, and check that vg->pvs matches the list of devices + * being imported in arg_import, plus some other VG checks. + */ + + log_debug("Checking VG to import."); + ret = process_each_vg(cmd, 0, NULL, pp->old_vgname, NULL, 0, handle, _pvchange_import_check_vg_single); + if (ret != ECMD_PROCESSED) + goto_bad; + + /* + * Change the PV uuid of each device. + */ + + log_debug("Changing PVs."); + ret = process_each_pv(cmd, argc, argv, pp->old_vgname, 0, READ_FOR_UPDATE | READ_ALLOW_EXPORTED, handle, _pvchange_import_pv_single); + if (ret != ECMD_PROCESSED) + goto_bad; + + /* + * Change the VG name and uuid. + */ + + log_debug("Changing VG %s to %s.", pp->old_vgname, pp->new_vgname); + + /* We don't care if the new name comes before the old in lock order. */ + lvmcache_lock_ordering(0); + + if (!lock_vol(cmd, pp->new_vgname, LCK_VG_WRITE, NULL)) { + log_error("Can't get lock for new VG name %s", pp->new_vgname); + lvmcache_lock_ordering(1); + goto bad; + } + + ret = process_each_vg(cmd, 0, NULL, pp->old_vgname, NULL, READ_FOR_UPDATE | READ_ALLOW_EXPORTED, handle, _pvchange_import_vg_single); + if (ret != ECMD_PROCESSED) + goto_bad; + + unlock_vg(cmd, pp->new_vgname); + unlock_vg(cmd, VG_GLOBAL); + + internal_filter_clear(); + init_internal_filtering(0); + + pp->done = pp->found_args; + return ECMD_PROCESSED; + +bad: + unlock_vg(cmd, VG_GLOBAL); + return ECMD_FAILED; +} + static int _pvchange_single(struct cmd_context *cmd, struct volume_group *vg, struct physical_volume *pv, struct processing_handle *handle) { @@ -207,8 +609,11 @@ int pvchange(struct cmd_context *cmd, int argc, char **argv) struct processing_handle *handle = NULL; int ret; - if (!(arg_count(cmd, allocatable_ARG) + arg_is_set(cmd, addtag_ARG) + - arg_is_set(cmd, deltag_ARG) + arg_count(cmd, uuid_ARG) + + if (!(arg_count(cmd, allocatable_ARG) + + arg_is_set(cmd, addtag_ARG) + + arg_is_set(cmd, deltag_ARG) + + arg_is_set(cmd, importclone_ARG) + + arg_count(cmd, uuid_ARG) + arg_count(cmd, metadataignore_ARG))) { log_error("Please give one or more of -x, -uuid, " "--addtag, --deltag or --metadataignore"); @@ -236,6 +641,12 @@ int pvchange(struct cmd_context *cmd, int argc, char **argv) goto out; } + if (arg_is_set(cmd, importclone_ARG) && (!argc || arg_count(cmd, all_ARG))) { + log_error("Option --importclone requires PV args."); + ret = EINVALID_CMD_LINE; + goto out; + } + if (!argc) { /* * Take the global lock here so the lvmcache remains @@ -252,7 +663,10 @@ int pvchange(struct cmd_context *cmd, int argc, char **argv) set_pv_notify(cmd); - ret = process_each_pv(cmd, argc, argv, NULL, 0, READ_FOR_UPDATE | READ_ALLOW_EXPORTED, handle, _pvchange_single); + if (arg_is_set(cmd, importclone_ARG)) + ret = _pvchange_importclone(cmd, handle, argc, argv); + else + ret = process_each_pv(cmd, argc, argv, NULL, 0, READ_FOR_UPDATE | READ_ALLOW_EXPORTED, handle, _pvchange_single); if (!argc) unlock_vg(cmd, VG_GLOBAL); |