diff options
author | David Teigland <teigland@redhat.com> | 2014-10-24 13:15:01 -0500 |
---|---|---|
committer | David Teigland <teigland@redhat.com> | 2014-12-08 11:58:40 -0600 |
commit | 14dae087936ba41a62d7c69790c7b35621c78cef (patch) | |
tree | abdaf4860b0f3850fef82d88a8a65eaa2eff4a2b | |
parent | 4c62215bd178c17d0776448bdc99740180c85402 (diff) | |
download | lvm2-14dae087936ba41a62d7c69790c7b35621c78cef.tar.gz |
pvchange: use process_each_pv
-rw-r--r-- | tools/pvchange.c | 136 |
1 files changed, 46 insertions, 90 deletions
diff --git a/tools/pvchange.c b/tools/pvchange.c index 76db91727..f616c0925 100644 --- a/tools/pvchange.c +++ b/tools/pvchange.c @@ -15,10 +15,16 @@ #include "tools.h" +struct pvchange_params { + unsigned done; + unsigned total; +}; + static int _pvchange_single(struct cmd_context *cmd, struct volume_group *vg, struct physical_volume *pv, void *handle __attribute__((unused))) { + struct pvchange_params *params = handle; const char *pv_name = pv_dev_name(pv); char uuid[64] __attribute__((aligned(8))); @@ -26,25 +32,27 @@ static int _pvchange_single(struct cmd_context *cmd, struct volume_group *vg, int mda_ignore = arg_int_value(cmd, metadataignore_ARG, 0); int tagargs = arg_count(cmd, addtag_ARG) + arg_count(cmd, deltag_ARG); + params->total++; + /* If in a VG, must change using volume group. */ if (!is_orphan(pv)) { if (tagargs && !(vg->fid->fmt->features & FMT_TAGS)) { log_error("Volume group containing %s does not " "support tags", pv_name); - return 0; + return ECMD_FAILED; } if (arg_count(cmd, uuid_ARG) && lvs_in_vg_activated(vg)) { log_error("Volume group containing %s has active " "logical volumes", pv_name); - return 0; + return ECMD_FAILED; } if (!archive(vg)) - return 0; + return ECMD_FAILED; } else { if (tagargs) { log_error("Can't change tag on Physical Volume %s not " "in volume group", pv_name); - return 0; + return ECMD_FAILED; } } @@ -53,20 +61,22 @@ static int _pvchange_single(struct cmd_context *cmd, struct volume_group *vg, !(pv->fmt->features & FMT_ORPHAN_ALLOCATABLE)) { log_error("Allocatability not supported by orphan " "%s format PV %s", pv->fmt->name, pv_name); - return 0; + return ECMD_FAILED; } /* change allocatability for a PV */ if (allocatable && (pv_status(pv) & ALLOCATABLE_PV)) { log_warn("Physical volume \"%s\" is already " "allocatable.", pv_name); - return 1; + params->done++; + return ECMD_PROCESSED; } if (!allocatable && !(pv_status(pv) & ALLOCATABLE_PV)) { log_warn("Physical volume \"%s\" is already " "unallocatable.", pv_name); - return 1; + params->done++; + return ECMD_PROCESSED; } if (allocatable) { @@ -83,10 +93,10 @@ static int _pvchange_single(struct cmd_context *cmd, struct volume_group *vg, if (tagargs) { /* tag or deltag */ if (arg_count(cmd, addtag_ARG) && !change_tag(cmd, NULL, NULL, pv, addtag_ARG)) - return_0; + return_ECMD_FAILED; if (arg_count(cmd, deltag_ARG) && !change_tag(cmd, NULL, NULL, pv, deltag_ARG)) - return_0; + return_ECMD_FAILED; } @@ -97,10 +107,10 @@ static int _pvchange_single(struct cmd_context *cmd, struct volume_group *vg, "of VG %s metadata? [y/n]: ", pv_vg_name(pv)) == 'n') { log_error("Physical volume %s not changed", pv_name); - return 0; + return ECMD_FAILED; } if (!pv_change_metadataignore(pv, mda_ignore)) - return_0; + return_ECMD_FAILED; } if (arg_count(cmd, uuid_ARG)) { @@ -109,15 +119,15 @@ static int _pvchange_single(struct cmd_context *cmd, struct volume_group *vg, if (!id_create(&pv->id)) { log_error("Failed to generate new random UUID for %s.", pv_name); - return 0; + return ECMD_FAILED; } if (!id_write_format(&pv->id, uuid, sizeof(uuid))) - return 0; + return ECMD_FAILED; log_verbose("Changing uuid of %s to %s.", pv_name, uuid); if (!is_orphan(pv) && (!pv_write(cmd, pv, 1))) { log_error("pv_write with new uuid failed " "for %s.", pv_name); - return 0; + return ECMD_FAILED; } } @@ -126,33 +136,27 @@ static int _pvchange_single(struct cmd_context *cmd, struct volume_group *vg, if (!vg_write(vg) || !vg_commit(vg)) { log_error("Failed to store physical volume \"%s\" in " "volume group \"%s\"", pv_name, vg->name); - return 0; + return ECMD_FAILED; } backup(vg); } else if (!(pv_write(cmd, pv, 0))) { log_error("Failed to store physical volume \"%s\"", pv_name); - return 0; + return ECMD_FAILED; } log_print_unless_silent("Physical volume \"%s\" changed", pv_name); - return 1; + params->done++; + return ECMD_PROCESSED; } int pvchange(struct cmd_context *cmd, int argc, char **argv) { - int opt = 0; - int done = 0; - int total = 0; - - struct volume_group *vg; - const char *vg_name; - char *pv_name; - - struct pv_list *pvl; - struct dm_list *vgnames; - struct dm_str_list *sll; + struct pvchange_params params; + const char *done_s; + const char *undone_s; + 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) + @@ -172,39 +176,10 @@ int pvchange(struct cmd_context *cmd, int argc, char **argv) return EINVALID_CMD_LINE; } - if (argc) { - log_verbose("Using physical volume(s) on command line"); - for (; opt < argc; opt++) { - total++; - pv_name = argv[opt]; - dm_unescape_colons_and_at_signs(pv_name, NULL, NULL); - vg_name = find_vgname_from_pvname(cmd, pv_name); - if (!vg_name) { - log_error("Failed to read physical volume %s", - pv_name); - continue; - } - vg = vg_read_for_update(cmd, vg_name, NULL, 0); - if (vg_read_error(vg)) { - release_vg(vg); - stack; - continue; - } - pvl = find_pv_in_vg(vg, pv_name); - if (!pvl || !pvl->pv) { - unlock_and_release_vg(cmd, vg, vg_name); - log_error("Unable to find %s in %s", - pv_name, vg_name); - continue; - } - - done += _pvchange_single(cmd, vg, - pvl->pv, NULL); - unlock_and_release_vg(cmd, vg, vg_name); - } - } else { - log_verbose("Scanning for physical volume names"); - /* FIXME: share code with toollib */ + params.done = 0; + params.total = 0; + + if (!argc) { /* * Take the global lock here so the lvmcache remains * consistent across orphan/non-orphan vg locks. If we don't @@ -215,36 +190,17 @@ int pvchange(struct cmd_context *cmd, int argc, char **argv) log_error("Unable to obtain global lock."); return ECMD_FAILED; } - - /* populate lvmcache */ - if (!lvmetad_vg_list_to_lvmcache(cmd)) - stack; - - if ((vgnames = get_vgnames(cmd, 1)) && - !dm_list_empty(vgnames)) { - dm_list_iterate_items(sll, vgnames) { - vg = vg_read_for_update(cmd, sll->str, NULL, 0); - if (vg_read_error(vg)) { - release_vg(vg); - stack; - continue; - } - dm_list_iterate_items(pvl, &vg->pvs) { - total++; - done += _pvchange_single(cmd, vg, - pvl->pv, - NULL); - } - unlock_and_release_vg(cmd, vg, sll->str); - } - } - unlock_vg(cmd, VG_GLOBAL); } - log_print_unless_silent("%d physical volume%s changed / %d physical volume%s " - "not changed", - done, done == 1 ? "" : "s", - total - done, (total - done) == 1 ? "" : "s"); + ret = process_each_pv(cmd, argc, argv, NULL, READ_FOR_UPDATE, ¶ms, + _pvchange_single); + + done_s = params.done == 1 ? "" : "s"; + undone_s = (params.total - params.done) == 1 ? "" : "s"; + + log_print_unless_silent("%d physical volume%s changed / %d physical volume%s not changed", + params.done, done_s, + params.total - params.done, undone_s); - return (total == done) ? ECMD_PROCESSED : ECMD_FAILED; + return ret; } |