diff options
author | David Teigland <teigland@redhat.com> | 2014-10-16 16:04:48 -0500 |
---|---|---|
committer | David Teigland <teigland@redhat.com> | 2014-10-23 15:28:16 -0500 |
commit | 98d23a21c806b4c67c5d9bcf9207f5e00fe8e332 (patch) | |
tree | bd8180bfa1a449b81873e8dfc549d10f4d7d9585 | |
parent | c378359445aede111e282f629dee57fc92d0d65f (diff) | |
download | lvm2-98d23a21c806b4c67c5d9bcf9207f5e00fe8e332.tar.gz |
pvchange: use process_each_pv
-rw-r--r-- | tools/pvchange.c | 132 |
1 files changed, 44 insertions, 88 deletions
diff --git a/tools/pvchange.c b/tools/pvchange.c index c2adc3430..638d294e5 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,6 +32,8 @@ static int _pvchange_single(struct cmd_context *cmd, struct volume_group *vg, int tagargs = 0; int mda_ignore = 0; + params->total++; + tagargs = arg_count(cmd, addtag_ARG) + arg_count(cmd, deltag_ARG); if (arg_count(cmd, allocatable_ARG)) @@ -40,20 +48,20 @@ static int _pvchange_single(struct cmd_context *cmd, struct volume_group *vg, 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; } } @@ -62,20 +70,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) { @@ -92,10 +102,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; } @@ -106,10 +116,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)) { @@ -118,15 +128,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; } } @@ -135,33 +145,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) + @@ -181,39 +185,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; - } + params.done = 0; + params.total = 0; - 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 */ + if (!argc) { /* * Take the global lock here so the lvmcache remains * consistent across orphan/non-orphan vg locks. If we don't @@ -224,36 +199,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; + ret = process_each_pv(cmd, argc, argv, NULL, READ_FOR_UPDATE, ¶ms, + _pvchange_single); - 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); - } + 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", - done, done == 1 ? "" : "s", - total - done, (total - 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; } |