1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-18 10:04:20 +03:00

Restructure lvresize, bringing it closer to lvcreate.

This commit is contained in:
Alasdair Kergon 2004-05-11 18:47:40 +00:00
parent 1a51586c0c
commit 241913fe60
2 changed files with 183 additions and 136 deletions

View File

@ -1,5 +1,6 @@
Version 2.00.16 - Version 2.00.16 -
============================= =============================
Restructure lvresize, bringing it closer to lvcreate.
A quick sanity check on vg_disk struct when read in. More checks needed. A quick sanity check on vg_disk struct when read in. More checks needed.
Only include visible LVs in active/open counts. Only include visible LVs in active/open counts.
Add virtual segment types, zero and error. A large sparse device can be Add virtual segment types, zero and error. A large sparse device can be

View File

@ -15,96 +15,115 @@
#include "tools.h" #include "tools.h"
int lvresize(struct cmd_context *cmd, int argc, char **argv) struct lvresize_params {
{
struct volume_group *vg;
struct logical_volume *lv;
struct snapshot *snap;
struct lvinfo info;
uint32_t extents = 0;
uint64_t size = 0;
uint32_t stripes = 0, ssize = 0, stripesize_extents = 0;
uint32_t seg_stripes = 0, seg_stripesize = 0, seg_size = 0;
uint32_t extents_used = 0;
uint32_t size_rest;
sign_t sign = SIGN_NONE;
char *lv_name;
const char *vg_name; const char *vg_name;
char *st, *lock_lvid; const char *lv_name;
const char *cmd_name;
uint32_t stripes;
uint32_t stripe_size;
struct segment_type *segtype;
struct list *pvh; struct list *pvh;
struct lv_list *lvl;
int opt = 0; /* size */
int consistent = 1; uint32_t extents;
struct lv_segment *seg; uint64_t size;
uint32_t seg_extents; sign_t sign;
uint32_t sz, str;
struct segment_type *segtype = NULL;
enum { enum {
LV_ANY = 0, LV_ANY = 0,
LV_REDUCE = 1, LV_REDUCE = 1,
LV_EXTEND = 2 LV_EXTEND = 2
} resize = LV_ANY; } resize;
int argc;
char **argv;
};
static int _read_params(struct cmd_context *cmd, int argc, char **argv,
struct lvresize_params *lp)
{
const char *cmd_name;
char *st;
lp->sign = SIGN_NONE;
lp->resize = LV_ANY;
cmd_name = command_name(cmd); cmd_name = command_name(cmd);
if (!strcmp(cmd_name, "lvreduce")) if (!strcmp(cmd_name, "lvreduce"))
resize = LV_REDUCE; lp->resize = LV_REDUCE;
if (!strcmp(cmd_name, "lvextend")) if (!strcmp(cmd_name, "lvextend"))
resize = LV_EXTEND; lp->resize = LV_EXTEND;
if (arg_count(cmd, extents_ARG) + arg_count(cmd, size_ARG) != 1) { if (arg_count(cmd, extents_ARG) + arg_count(cmd, size_ARG) != 1) {
log_error("Please specify either size or extents (not both)"); log_error("Please specify either size or extents (not both)");
return EINVALID_CMD_LINE; return 0;
} }
if (arg_count(cmd, extents_ARG)) { if (arg_count(cmd, extents_ARG)) {
extents = arg_uint_value(cmd, extents_ARG, 0); lp->extents = arg_uint_value(cmd, extents_ARG, 0);
sign = arg_sign_value(cmd, extents_ARG, SIGN_NONE); lp->sign = arg_sign_value(cmd, extents_ARG, SIGN_NONE);
} }
/* Size returned in kilobyte units; held in sectors */ /* Size returned in kilobyte units; held in sectors */
if (arg_count(cmd, size_ARG)) { if (arg_count(cmd, size_ARG)) {
size = arg_uint64_value(cmd, size_ARG, UINT64_C(0)) * 2; lp->size = arg_uint64_value(cmd, size_ARG, UINT64_C(0)) * 2;
sign = arg_sign_value(cmd, size_ARG, SIGN_NONE); lp->sign = arg_sign_value(cmd, size_ARG, SIGN_NONE);
} }
if (resize == LV_EXTEND && sign == SIGN_MINUS) { if (lp->resize == LV_EXTEND && lp->sign == SIGN_MINUS) {
log_error("Negative argument not permitted - use lvreduce"); log_error("Negative argument not permitted - use lvreduce");
return EINVALID_CMD_LINE; return 0;
} }
if (resize == LV_REDUCE && sign == SIGN_PLUS) { if (lp->resize == LV_REDUCE && lp->sign == SIGN_PLUS) {
log_error("Positive sign not permitted - use lvextend"); log_error("Positive sign not permitted - use lvextend");
return EINVALID_CMD_LINE; return 0;
} }
if (!argc) { if (!argc) {
log_error("Please provide the logical volume name"); log_error("Please provide the logical volume name");
return EINVALID_CMD_LINE; return 0;
} }
lv_name = argv[0]; lp->lv_name = argv[0];
argv++; argv++;
argc--; argc--;
if (!(vg_name = extract_vgname(cmd, lv_name))) { if (!(lp->vg_name = extract_vgname(cmd, lp->lv_name))) {
log_error("Please provide a volume group name"); log_error("Please provide a volume group name");
return EINVALID_CMD_LINE; return 0;
} }
if ((st = strrchr(lv_name, '/'))) if ((st = strrchr(lp->lv_name, '/')))
lv_name = st + 1; lp->lv_name = st + 1;
/* does VG exist? */ lp->argc = argc;
log_verbose("Finding volume group %s", vg_name); lp->argv = argv;
if (!lock_vol(cmd, vg_name, LCK_VG_WRITE)) {
log_error("Can't get lock for %s", vg_name);
return ECMD_FAILED;
}
if (!(vg = vg_read(cmd, vg_name, &consistent))) { return 1;
log_error("Volume group %s doesn't exist", vg_name); }
static int _lvresize(struct cmd_context *cmd, struct lvresize_params *lp)
{
struct volume_group *vg;
struct logical_volume *lv;
struct snapshot *snap;
struct lvinfo info;
uint32_t stripesize_extents = 0;
uint32_t seg_stripes = 0, seg_stripesize = 0, seg_size = 0;
uint32_t extents_used = 0;
uint32_t size_rest;
char *lock_lvid;
struct lv_list *lvl;
int consistent = 1;
struct lv_segment *seg;
uint32_t seg_extents;
uint32_t sz, str;
if (!(vg = vg_read(cmd, lp->vg_name, &consistent))) {
log_error("Volume group %s doesn't exist", lp->vg_name);
goto error; goto error;
} }
@ -114,20 +133,20 @@ int lvresize(struct cmd_context *cmd, int argc, char **argv)
} }
if (!(vg->status & LVM_WRITE)) { if (!(vg->status & LVM_WRITE)) {
log_error("Volume group %s is read-only", vg_name); log_error("Volume group %s is read-only", lp->vg_name);
goto error; goto error;
} }
/* does LV exist? */ /* does LV exist? */
if (!(lvl = find_lv_in_vg(vg, lv_name))) { if (!(lvl = find_lv_in_vg(vg, lp->lv_name))) {
log_error("Logical volume %s not found in volume group %s", log_error("Logical volume %s not found in volume group %s",
lv_name, vg_name); lp->lv_name, lp->vg_name);
goto error; goto error;
} }
if (arg_count(cmd, stripes_ARG)) { if (arg_count(cmd, stripes_ARG)) {
if (vg->fid->fmt->features & FMT_SEGMENTS) if (vg->fid->fmt->features & FMT_SEGMENTS)
stripes = arg_uint_value(cmd, stripes_ARG, 1); lp->stripes = arg_uint_value(cmd, stripes_ARG, 1);
else else
log_print("Varied striping not supported. Ignoring."); log_print("Varied striping not supported. Ignoring.");
} }
@ -138,7 +157,8 @@ int lvresize(struct cmd_context *cmd, int argc, char **argv)
goto error; goto error;
} }
if (vg->fid->fmt->features & FMT_SEGMENTS) if (vg->fid->fmt->features & FMT_SEGMENTS)
ssize = 2 * arg_uint_value(cmd, stripesize_ARG, 0); lp->stripe_size = 2 * arg_uint_value(cmd,
stripesize_ARG, 0);
else else
log_print("Varied stripesize not supported. Ignoring."); log_print("Varied stripesize not supported. Ignoring.");
} }
@ -150,62 +170,63 @@ int lvresize(struct cmd_context *cmd, int argc, char **argv)
goto error; goto error;
} }
if (size) { if (lp->size) {
if (size % vg->extent_size) { if (lp->size % vg->extent_size) {
if (sign == SIGN_MINUS) if (lp->sign == SIGN_MINUS)
size -= size % vg->extent_size; lp->size -= lp->size % vg->extent_size;
else else
size += vg->extent_size - lp->size += vg->extent_size -
(size % vg->extent_size); (lp->size % vg->extent_size);
log_print("Rounding up size to full physical extent %s", log_print("Rounding up size to full physical extent %s",
display_size(cmd, (uint64_t) size / 2, display_size(cmd, (uint64_t) lp->size / 2,
SIZE_SHORT)); SIZE_SHORT));
} }
extents = size / vg->extent_size; lp->extents = lp->size / vg->extent_size;
} }
if (sign == SIGN_PLUS) if (lp->sign == SIGN_PLUS)
extents += lv->le_count; lp->extents += lv->le_count;
if (sign == SIGN_MINUS) { if (lp->sign == SIGN_MINUS) {
if (extents >= lv->le_count) { if (lp->extents >= lv->le_count) {
log_error("Unable to reduce %s below 1 extent", log_error("Unable to reduce %s below 1 extent",
lv_name); lp->lv_name);
goto error_cmdline; goto error_cmdline;
} }
extents = lv->le_count - extents; lp->extents = lv->le_count - lp->extents;
} }
if (!extents) { if (!lp->extents) {
log_error("New size of 0 not permitted"); log_error("New size of 0 not permitted");
goto error_cmdline; goto error_cmdline;
} }
if (extents == lv->le_count) { if (lp->extents == lv->le_count) {
log_error("New size (%d extents) matches existing size " log_error("New size (%d extents) matches existing size "
"(%d extents)", extents, lv->le_count); "(%d extents)", lp->extents, lv->le_count);
goto error_cmdline; goto error_cmdline;
} }
seg_size = extents - lv->le_count; seg_size = lp->extents - lv->le_count;
/* Use segment type of last segment */ /* Use segment type of last segment */
list_iterate_items(seg, &lv->segments) { list_iterate_items(seg, &lv->segments) {
segtype = seg->segtype; lp->segtype = seg->segtype;
} }
/* FIXME Support LVs with mixed segment types */ /* FIXME Support LVs with mixed segment types */
if (segtype != (struct segment_type *) arg_ptr_value(cmd, type_ARG, if (lp->segtype != (struct segment_type *) arg_ptr_value(cmd, type_ARG,
segtype)) { lp->segtype)) {
log_error("VolumeType does not match (%s)", segtype->name); log_error("VolumeType does not match (%s)", lp->segtype->name);
goto error_cmdline; goto error_cmdline;
} }
/* If extending, find stripes, stripesize & size of last segment */ /* If extending, find stripes, stripesize & size of last segment */
if (extents > lv->le_count && !(stripes == 1 || (stripes > 1 && ssize))) { if ((lp->extents > lv->le_count) &&
!(lp->stripes == 1 || (lp->stripes > 1 && lp->stripe_size))) {
list_iterate_items(seg, &lv->segments) { list_iterate_items(seg, &lv->segments) {
if (!(seg->segtype->flags & SEG_AREAS_STRIPED)) if (!(seg->segtype->flags & SEG_AREAS_STRIPED))
continue; continue;
@ -214,8 +235,8 @@ int lvresize(struct cmd_context *cmd, int argc, char **argv)
str = seg->area_count; str = seg->area_count;
if ((seg_stripesize && seg_stripesize != sz if ((seg_stripesize && seg_stripesize != sz
&& !ssize) || && !lp->stripe_size) ||
(seg_stripes && seg_stripes != str && !stripes)) { (seg_stripes && seg_stripes != str && !lp->stripes)) {
log_error("Please specify number of " log_error("Please specify number of "
"stripes (-i) and stripesize (-I)"); "stripes (-i) and stripesize (-I)");
goto error_cmdline; goto error_cmdline;
@ -225,29 +246,29 @@ int lvresize(struct cmd_context *cmd, int argc, char **argv)
seg_stripes = str; seg_stripes = str;
} }
if (!stripes) if (!lp->stripes)
stripes = seg_stripes; lp->stripes = seg_stripes;
if (!ssize && stripes > 1) { if (!lp->stripe_size && lp->stripes > 1) {
if (seg_stripesize) { if (seg_stripesize) {
log_print("Using stripesize of last segment " log_print("Using stripesize of last segment "
"%dKB", seg_stripesize / 2); "%dKB", seg_stripesize / 2);
ssize = seg_stripesize; lp->stripe_size = seg_stripesize;
} else { } else {
ssize = find_config_int(cmd->cft->root, lp->stripe_size = find_config_int(cmd->cft->root,
"metadata/stripesize", "metadata/stripesize",
DEFAULT_STRIPESIZE) * 2; DEFAULT_STRIPESIZE) * 2;
log_print("Using default stripesize %dKB", log_print("Using default stripesize %dKB",
ssize / 2); lp->stripe_size / 2);
} }
} }
} }
/* If reducing, find stripes, stripesize & size of last segment */ /* If reducing, find stripes, stripesize & size of last segment */
if (extents < lv->le_count) { if (lp->extents < lv->le_count) {
extents_used = 0; extents_used = 0;
if (stripes || ssize) if (lp->stripes || lp->stripe_size)
log_error("Ignoring stripes and stripesize arguments " log_error("Ignoring stripes and stripesize arguments "
"when reducing"); "when reducing");
@ -259,62 +280,62 @@ int lvresize(struct cmd_context *cmd, int argc, char **argv)
seg_stripes = seg->area_count; seg_stripes = seg->area_count;
} }
if (extents <= extents_used + seg_extents) if (lp->extents <= extents_used + seg_extents)
break; break;
extents_used += seg_extents; extents_used += seg_extents;
} }
seg_size = extents - extents_used; seg_size = lp->extents - extents_used;
ssize = seg_stripesize; lp->stripe_size = seg_stripesize;
stripes = seg_stripes; lp->stripes = seg_stripes;
} }
if (stripes > 1 && !ssize) { if (lp->stripes > 1 && !lp->stripe_size) {
log_error("Stripesize for striped segment should not be 0!"); log_error("Stripesize for striped segment should not be 0!");
goto error_cmdline; goto error_cmdline;
} }
if ((stripes > 1)) { if ((lp->stripes > 1)) {
if (!(stripesize_extents = ssize / vg->extent_size)) if (!(stripesize_extents = lp->stripe_size / vg->extent_size))
stripesize_extents = 1; stripesize_extents = 1;
if ((size_rest = seg_size % (stripes * stripesize_extents))) { if ((size_rest = seg_size % (lp->stripes * stripesize_extents))) {
log_print("Rounding size (%d extents) down to stripe " log_print("Rounding size (%d extents) down to stripe "
"boundary size for segment (%d extents)", "boundary size for segment (%d extents)",
extents, extents - size_rest); lp->extents, lp->extents - size_rest);
extents = extents - size_rest; lp->extents = lp->extents - size_rest;
} }
} }
if (extents == lv->le_count) { if (lp->extents == lv->le_count) {
log_error("New size (%d extents) matches existing size " log_error("New size (%d extents) matches existing size "
"(%d extents)", extents, lv->le_count); "(%d extents)", lp->extents, lv->le_count);
goto error_cmdline; goto error_cmdline;
} }
if (extents < lv->le_count) { if (lp->extents < lv->le_count) {
if (resize == LV_EXTEND) { if (lp->resize == LV_EXTEND) {
log_error("New size given (%d extents) not larger " log_error("New size given (%d extents) not larger "
"than existing size (%d extents)", "than existing size (%d extents)",
extents, lv->le_count); lp->extents, lv->le_count);
goto error_cmdline; goto error_cmdline;
} else } else
resize = LV_REDUCE; lp->resize = LV_REDUCE;
} }
if (extents > lv->le_count) { if (lp->extents > lv->le_count) {
if (resize == LV_REDUCE) { if (lp->resize == LV_REDUCE) {
log_error("New size given (%d extents) not less than " log_error("New size given (%d extents) not less than "
"existing size (%d extents)", extents, "existing size (%d extents)", lp->extents,
lv->le_count); lv->le_count);
goto error_cmdline; goto error_cmdline;
} else } else
resize = LV_EXTEND; lp->resize = LV_EXTEND;
} }
if (resize == LV_REDUCE) { if (lp->resize == LV_REDUCE) {
if (argc) if (lp->argc)
log_print("Ignoring PVs on command line when reducing"); log_print("Ignoring PVs on command line when reducing");
memset(&info, 0, sizeof(info)); memset(&info, 0, sizeof(info));
@ -327,8 +348,8 @@ int lvresize(struct cmd_context *cmd, int argc, char **argv)
if (info.exists) { if (info.exists) {
log_print("WARNING: Reducing active%s logical volume " log_print("WARNING: Reducing active%s logical volume "
"to %s", info.open_count ? " and open" : "", "to %s", info.open_count ? " and open" : "",
display_size(cmd, (uint64_t) display_size(cmd, (uint64_t) lp->extents *
extents * (vg->extent_size / 2), (vg->extent_size / 2),
SIZE_SHORT)); SIZE_SHORT));
log_print("THIS MAY DESTROY YOUR DATA " log_print("THIS MAY DESTROY YOUR DATA "
@ -337,40 +358,45 @@ int lvresize(struct cmd_context *cmd, int argc, char **argv)
if (!arg_count(cmd, force_ARG)) { if (!arg_count(cmd, force_ARG)) {
if (yes_no_prompt("Do you really want to reduce %s?" if (yes_no_prompt("Do you really want to reduce %s?"
" [y/n]: ", lv_name) == 'n') { " [y/n]: ", lp->lv_name) == 'n') {
log_print("Logical volume %s NOT reduced", log_print("Logical volume %s NOT reduced",
lv_name); lp->lv_name);
goto error; goto error;
} }
} }
if (!archive(vg)) if (!archive(vg)) {
goto error;
if (!lv_reduce(vg->fid, lv, lv->le_count - extents))
goto error;
}
if (resize == LV_EXTEND) {
if (!(pvh = argc ? create_pv_list(cmd->mem, vg, argc - opt,
argv + opt) : &vg->pvs)) {
stack; stack;
goto error; goto error;
} }
if (!archive(vg)) if (!lv_reduce(vg->fid, lv, lv->le_count - lp->extents))
goto error; goto error;
}
log_print("Extending logical volume %s to %s", lv_name, if (lp->resize == LV_EXTEND) {
if (!(lp->pvh = lp->argc ? create_pv_list(cmd->mem, vg, lp->argc,
lp->argv) : &vg->pvs)) {
stack;
goto error;
}
if (!archive(vg)) {
stack;
goto error;
}
log_print("Extending logical volume %s to %s", lp->lv_name,
display_size(cmd, (uint64_t) display_size(cmd, (uint64_t)
extents * (vg->extent_size / 2), lp->extents * (vg->extent_size / 2),
SIZE_SHORT)); SIZE_SHORT));
if (!lv_extend(vg->fid, lv, segtype, stripes, ssize, 0u, if (!lv_extend(vg->fid, lv, lp->segtype, lp->stripes,
extents - lv->le_count, NULL, 0u, 0u, pvh, lp->stripe_size, 0u, lp->extents - lv->le_count,
lv->alloc)) NULL, 0u, 0u, lp->pvh, lv->alloc)) {
goto error; goto error;
} }
}
/* store vg on disk(s) */ /* store vg on disk(s) */
if (!vg_write(vg)) { if (!vg_write(vg)) {
@ -387,7 +413,7 @@ int lvresize(struct cmd_context *cmd, int argc, char **argv)
lock_lvid = lv->lvid.s; lock_lvid = lv->lvid.s;
if (!suspend_lv(cmd, lock_lvid)) { if (!suspend_lv(cmd, lock_lvid)) {
log_error("Failed to suspend %s", lv_name); log_error("Failed to suspend %s", lp->lv_name);
vg_revert(vg); vg_revert(vg);
goto error; goto error;
} }
@ -399,21 +425,41 @@ int lvresize(struct cmd_context *cmd, int argc, char **argv)
} }
if (!resume_lv(cmd, lock_lvid)) { if (!resume_lv(cmd, lock_lvid)) {
log_error("Problem reactivating %s", lv_name); log_error("Problem reactivating %s", lp->lv_name);
goto error; goto error;
} }
unlock_vg(cmd, vg_name); log_print("Logical volume %s successfully resized", lp->lv_name);
log_print("Logical volume %s successfully resized", lv_name);
return ECMD_PROCESSED; return ECMD_PROCESSED;
error: error:
unlock_vg(cmd, vg_name);
return ECMD_FAILED; return ECMD_FAILED;
error_cmdline: error_cmdline:
unlock_vg(cmd, vg_name);
return EINVALID_CMD_LINE; return EINVALID_CMD_LINE;
} }
int lvresize(struct cmd_context *cmd, int argc, char **argv)
{
struct lvresize_params lp;
int r;
memset(&lp, 0, sizeof(lp));
if (!_read_params(cmd, argc, argv, &lp))
return EINVALID_CMD_LINE;
log_verbose("Finding volume group %s", lp.vg_name);
if (!lock_vol(cmd, lp.vg_name, LCK_VG_WRITE)) {
log_error("Can't get lock for %s", lp.vg_name);
return ECMD_FAILED;
}
if (!(r = _lvresize(cmd, &lp)))
stack;
unlock_vg(cmd, lp.vg_name);
return r;
}