summaryrefslogtreecommitdiff
path: root/tools/vgextend.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/vgextend.c')
-rw-r--r--tools/vgextend.c189
1 files changed, 112 insertions, 77 deletions
diff --git a/tools/vgextend.c b/tools/vgextend.c
index 2dc169980..581c21127 100644
--- a/tools/vgextend.c
+++ b/tools/vgextend.c
@@ -15,7 +15,13 @@
#include "tools.h"
-static int _restore_pv(struct volume_group *vg, char *pv_name)
+struct vgextend_params {
+ struct pvcreate_params pp;
+ int pv_count;
+ const char *const *pv_names;
+};
+
+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);
@@ -38,13 +44,92 @@ static int _restore_pv(struct volume_group *vg, char *pv_name)
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;
+ int fixed = 0;
+ int i;
+
+ for (i = 0; i < vp->pv_count; i++)
+ if (_restore_pv(vg, vp->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_count(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 (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE, NULL)) {
+ log_error("Can't get lock for orphan PVs");
+ return ECMD_FAILED;
+ }
+
+ if (!vg_extend(vg, vp->pv_count, vp->pv_names, pp))
+ goto_out;
+
+ if (arg_count(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, vp->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:
+ unlock_vg(cmd, VG_ORPHANS);
+
+ return ret;
+}
+
int vgextend(struct cmd_context *cmd, int argc, char **argv)
{
- const char *vg_name;
- struct volume_group *vg = NULL;
- int r = ECMD_FAILED;
- struct pvcreate_params pp;
- int fixed = 0, i = 0;
+ struct vgextend_params vp;
+ unsigned restoremissing = arg_is_set(cmd, restoremissing_ARG);
+ struct processing_handle *handle;
+ int ret;
if (!argc) {
log_error("Please enter volume group name and "
@@ -52,19 +137,25 @@ int vgextend(struct cmd_context *cmd, int argc, char **argv)
return EINVALID_CMD_LINE;
}
- vg_name = skip_dev_dir(cmd, argv[0], NULL);
- argc--;
- argv++;
-
if (arg_count(cmd, metadatacopies_ARG)) {
log_error("Invalid option --metadatacopies, "
"use --pvmetadatacopies instead.");
return EINVALID_CMD_LINE;
}
- pvcreate_params_set_defaults(&pp);
- if (!pvcreate_params_validate(cmd, argc, argv, &pp)) {
- return EINVALID_CMD_LINE;
- }
+
+ pvcreate_params_set_defaults(&vp.pp);
+ vp.pv_count = argc - 1;
+ vp.pv_names = (const char* const*)(argv + 1);
+
+ if (!pvcreate_params_validate(cmd, vp.pv_count, &vp.pp))
+ return_EINVALID_CMD_LINE;
+
+ if (!(handle = init_processing_handle(cmd))) {
+ log_error("Failed to initialize processing handle.");
+ return ECMD_FAILED;
+ }
+
+ handle->custom_handle = &vp;
/*
* It is always ok to add new PVs to a VG - even if there are
@@ -74,71 +165,15 @@ int vgextend(struct cmd_context *cmd, int argc, char **argv)
*/
cmd->handles_missing_pvs = 1;
- log_verbose("Checking for volume group \"%s\"", vg_name);
- vg = vg_read_for_update(cmd, vg_name, NULL, 0);
- if (vg_read_error(vg)) {
- release_vg(vg);
+ /* Needed to change the set of orphan PVs. */
+ if (!lockd_gl(cmd, "ex", 0))
return_ECMD_FAILED;
- }
- if (!archive(vg))
- goto_bad;
+ ret = process_each_vg(cmd, argc, argv,
+ READ_FOR_UPDATE | ONE_VGNAME_ARG, handle,
+ restoremissing ? &_vgextend_restoremissing : &_vgextend_single);
- if (arg_count(cmd, restoremissing_ARG)) {
- for (i = 0; i < argc; ++i) {
- if (_restore_pv(vg, argv[i]))
- ++ fixed;
- }
- if (!fixed) {
- log_error("No PV has been restored.");
- goto bad;
- }
- } else { /* no --restore, normal vgextend */
- if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE, NULL)) {
- log_error("Can't get lock for orphan PVs");
- unlock_and_release_vg(cmd, vg, vg_name);
- return ECMD_FAILED;
- }
-
- if (arg_count(cmd, metadataignore_ARG) &&
- (vg_mda_copies(vg) != VGMETADATACOPIES_UNMANAGED) &&
- (pp.force == PROMPT) && !pp.yes &&
- 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);
- goto bad;
- }
-
- /* extend vg */
- if (!vg_extend(vg, argc, (const char* const*)argv, &pp))
- goto_bad;
-
- if (arg_count(cmd, metadataignore_ARG) &&
- (vg_mda_copies(vg) != VGMETADATACOPIES_UNMANAGED) &&
- (vg_mda_copies(vg) != vg_mda_used_count(vg))) {
- log_warn("WARNING: Changing preferred number of copies of VG %s "
- "metadata from %"PRIu32" to %"PRIu32, vg_name,
- vg_mda_copies(vg), vg_mda_used_count(vg));
- vg_set_mda_copies(vg, vg_mda_used_count(vg));
- }
-
- /* ret > 0 */
- log_verbose("Volume group \"%s\" will be extended by %d new "
- "physical volumes", vg_name, argc);
- }
-
- /* store vg on disk(s) */
- if (!vg_write(vg) || !vg_commit(vg))
- goto_bad;
-
- backup(vg);
- log_print_unless_silent("Volume group \"%s\" successfully extended", vg_name);
- r = ECMD_PROCESSED;
+ destroy_processing_handle(cmd, handle);
-bad:
- if (!arg_count(cmd, restoremissing_ARG))
- unlock_vg(cmd, VG_ORPHANS);
- unlock_and_release_vg(cmd, vg, vg_name);
- return r;
+ return ret;
}