1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-03 05:18:29 +03:00

vgextend: use the common toollib pv create

This commit is contained in:
David Teigland 2016-02-15 14:28:09 -06:00
parent 6a1c22aadf
commit 749a7cecf8

View File

@ -16,9 +16,7 @@
#include "tools.h" #include "tools.h"
struct vgextend_params { struct vgextend_params {
struct pvcreate_params pp; struct pvcreate_each_params pp;
int pv_count;
const char *const *pv_names;
}; };
static int _restore_pv(struct volume_group *vg, const char *pv_name) static int _restore_pv(struct volume_group *vg, const char *pv_name)
@ -49,14 +47,15 @@ static int _vgextend_restoremissing(struct cmd_context *cmd __attribute__((unuse
struct processing_handle *handle) struct processing_handle *handle)
{ {
struct vgextend_params *vp = (struct vgextend_params *) handle->custom_handle; struct vgextend_params *vp = (struct vgextend_params *) handle->custom_handle;
struct pvcreate_each_params *pp = &vp->pp;
int fixed = 0; int fixed = 0;
int i; int i;
if (!archive(vg)) if (!archive(vg))
return_0; return_0;
for (i = 0; i < vp->pv_count; i++) for (i = 0; i < pp->pv_count; i++)
if (_restore_pv(vg, vp->pv_names[i])) if (_restore_pv(vg, pp->pv_names[i]))
fixed++; fixed++;
if (!fixed) { if (!fixed) {
@ -78,7 +77,7 @@ static int _vgextend_single(struct cmd_context *cmd, const char *vg_name,
struct volume_group *vg, struct processing_handle *handle) struct volume_group *vg, struct processing_handle *handle)
{ {
struct vgextend_params *vp = (struct vgextend_params *) handle->custom_handle; struct vgextend_params *vp = (struct vgextend_params *) handle->custom_handle;
struct pvcreate_params *pp = &vp->pp; struct pvcreate_each_params *pp = &vp->pp;
uint32_t mda_copies; uint32_t mda_copies;
uint32_t mda_used; uint32_t mda_used;
int ret = ECMD_FAILED; int ret = ECMD_FAILED;
@ -94,12 +93,7 @@ static int _vgextend_single(struct cmd_context *cmd, const char *vg_name,
if (!archive(vg)) if (!archive(vg))
return_ECMD_FAILED; return_ECMD_FAILED;
if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE, NULL)) { if (!vg_extend_each_pv(vg, pp))
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; goto_out;
if (arg_count(cmd, metadataignore_ARG)) { if (arg_count(cmd, metadataignore_ARG)) {
@ -114,7 +108,7 @@ static int _vgextend_single(struct cmd_context *cmd, const char *vg_name,
} }
} }
log_verbose("Volume group \"%s\" will be extended by %d new physical volumes", vg_name, vp->pv_count); 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)) if (!vg_write(vg) || !vg_commit(vg))
goto_out; goto_out;
@ -123,19 +117,17 @@ static int _vgextend_single(struct cmd_context *cmd, const char *vg_name,
log_print_unless_silent("Volume group \"%s\" successfully extended", vg_name); log_print_unless_silent("Volume group \"%s\" successfully extended", vg_name);
ret = ECMD_PROCESSED; ret = ECMD_PROCESSED;
out: out:
unlock_vg(cmd, VG_ORPHANS);
return ret; return ret;
} }
int vgextend(struct cmd_context *cmd, int argc, char **argv) int vgextend(struct cmd_context *cmd, int argc, char **argv)
{ {
struct vgextend_params vp;
unsigned restoremissing = arg_is_set(cmd, restoremissing_ARG);
struct processing_handle *handle; struct processing_handle *handle;
const char *one_vgname; struct vgextend_params vp;
struct pvcreate_each_params *pp = &vp.pp;
unsigned restoremissing = arg_is_set(cmd, restoremissing_ARG);
const char *vg_name;
int ret; int ret;
if (!argc) { if (!argc) {
@ -144,27 +136,56 @@ int vgextend(struct cmd_context *cmd, int argc, char **argv)
return EINVALID_CMD_LINE; return EINVALID_CMD_LINE;
} }
one_vgname = skip_dev_dir(cmd, argv[0], NULL);
if (arg_count(cmd, metadatacopies_ARG)) { if (arg_count(cmd, metadatacopies_ARG)) {
log_error("Invalid option --metadatacopies, " log_error("Invalid option --metadatacopies, "
"use --pvmetadatacopies instead."); "use --pvmetadatacopies instead.");
return EINVALID_CMD_LINE; return EINVALID_CMD_LINE;
} }
pvcreate_params_set_defaults(&vp.pp); vg_name = skip_dev_dir(cmd, argv[0], NULL);
vp.pv_count = argc - 1; argc--;
vp.pv_names = (const char* const*)(argv + 1); argv++;
if (!pvcreate_params_validate(cmd, vp.pv_count, &vp.pp)) pvcreate_each_params_set_defaults(pp);
return_EINVALID_CMD_LINE;
if (!(handle = init_processing_handle(cmd))) { if (!pvcreate_each_params_from_args(cmd, pp))
log_error("Failed to initialize processing handle."); return EINVALID_CMD_LINE;
return ECMD_FAILED;
}
handle->custom_handle = &vp; 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 = 0;
/*
* Needed to change the set of orphan PVs.
* (disable afterward to prevent process_each_pv from doing
* a shared global lock since it's already acquired it ex.)
*/
if (!lockd_gl(cmd, "ex", 0))
return_ECMD_FAILED;
cmd->lockd_gl_disable = 1;
if (!(handle = init_processing_handle(cmd))) {
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;
}
}
/*
* pvcreate_each_device returns with the VG_ORPHANS write lock held,
* which was used to do pvcreate. Now to create the VG using those
* PVs, the VG lock will be taken (with the orphan lock already held.)
*/
/* /*
* It is always ok to add new PVs to a VG - even if there are * It is always ok to add new PVs to a VG - even if there are
@ -174,15 +195,15 @@ int vgextend(struct cmd_context *cmd, int argc, char **argv)
*/ */
cmd->handles_missing_pvs = 1; cmd->handles_missing_pvs = 1;
/* Needed to change the set of orphan PVs. */ handle->custom_handle = &vp;
if (!lockd_gl(cmd, "ex", 0))
return_ECMD_FAILED;
ret = process_each_vg(cmd, 0, NULL, one_vgname, ret = process_each_vg(cmd, 0, NULL, vg_name,
READ_FOR_UPDATE, handle, READ_FOR_UPDATE, handle,
restoremissing ? &_vgextend_restoremissing : &_vgextend_single); restoremissing ? &_vgextend_restoremissing : &_vgextend_single);
destroy_processing_handle(cmd, handle); destroy_processing_handle(cmd, handle);
if (!restoremissing)
unlock_vg(cmd, VG_ORPHANS);
return ret; return ret;
} }