From 03a8a07d6791f44a621a5a6775f63c046105cdee Mon Sep 17 00:00:00 2001 From: Alasdair Kergon Date: Tue, 13 Nov 2001 14:17:50 +0000 Subject: [PATCH] Merge lvreduce & lvextend into lvresize. --- lib/device/dev-cache.c | 2 +- lib/metadata/lv_manip.c | 31 ++--- lib/metadata/metadata.h | 1 + make.tmpl.in | 5 +- tools/Makefile.in | 1 + tools/commands.h | 13 ++ tools/lvextend.c | 164 +---------------------- tools/lvm.c | 61 ++++----- tools/lvreduce.c | 176 +------------------------ tools/lvresize.c | 281 ++++++++++++++++++++++++++++++++++++++++ tools/tools.h | 18 ++- 11 files changed, 355 insertions(+), 398 deletions(-) create mode 100644 tools/lvresize.c diff --git a/lib/device/dev-cache.c b/lib/device/dev-cache.c index d9507cff7..dc68667a3 100644 --- a/lib/device/dev-cache.c +++ b/lib/device/dev-cache.c @@ -237,7 +237,7 @@ static int _insert(const char *path, int rec) } else { /* add a device */ if (!S_ISBLK(info.st_mode)) { - //log_debug("%s: Not a block device", path); + log_debug("%s: Not a block device", path); return 0; } diff --git a/lib/metadata/lv_manip.c b/lib/metadata/lv_manip.c index bf3edd939..6b85bb2ff 100644 --- a/lib/metadata/lv_manip.c +++ b/lib/metadata/lv_manip.c @@ -116,7 +116,7 @@ static int _alloc_simple(struct logical_volume *lv, } } - done: + done: if (allocated != lv->le_count) { log_error("Insufficient free logical extents to " "allocate logical volume %s: %u required", @@ -169,7 +169,7 @@ static int _allocate(struct volume_group *vg, struct logical_volume *lv, vg->free_count -= lv->le_count - allocated; } - out: + out: pool_destroy(scratch); return r; } @@ -227,8 +227,7 @@ struct logical_volume *lv_create(const char *name, lv->size = extents * vg->extent_size; lv->le_count = extents; - if (!(lv->map = pool_zalloc(cmd->mem, - sizeof(*lv->map) * extents))) { + if (!(lv->map = pool_zalloc(cmd->mem, sizeof(*lv->map) * extents))) { stack; goto bad; } @@ -247,7 +246,7 @@ struct logical_volume *lv_create(const char *name, return lv; - bad: + bad: if (ll) pool_free(cmd->mem, ll); @@ -256,21 +255,16 @@ struct logical_volume *lv_create(const char *name, int lv_reduce(struct logical_volume *lv, uint32_t extents) { - // FIXME: merge with Alasdair's version in tools - if (extents % lv->stripes) { - log_error("For a striped volume you must reduce by a " - "multiple of the number of stripes"); - return 0; + int i; + + extents = lv->le_count - extents; + + for (i = extents; i < lv->le_count; i++) { + lv->map[i].pv->pe_allocated--; } - if (lv->le_count <= extents) { - log_error("Attempt to reduce by so many extents there would " - "be nothing left of the logical volume."); - return 0; - } + lv->le_count = extents; - /* Hmmm ... I think all we need to do is ... */ - lv->le_count -= extents; return 1; } @@ -318,12 +312,11 @@ int lv_extend(struct logical_volume *lv, uint32_t extents, pool_free(cmd->mem, new_lv); return 1; - bad: + bad: pool_free(cmd->mem, new_map); return 0; } -/* FIXME: I don't like the way the lvh is passed in here - EJT */ int lv_remove(struct volume_group *vg, struct list *lvh) { int i; diff --git a/lib/metadata/metadata.h b/lib/metadata/metadata.h index cee821793..113d853c7 100644 --- a/lib/metadata/metadata.h +++ b/lib/metadata/metadata.h @@ -134,6 +134,7 @@ struct cmd_context { /* misc. vars needed by format handler */ char *dev_dir; struct dev_filter *filter; + struct config_file *cf; }; struct format_instance { diff --git a/make.tmpl.in b/make.tmpl.in index 3098e87fb..43ae7639c 100644 --- a/make.tmpl.in +++ b/make.tmpl.in @@ -53,12 +53,15 @@ SUFFIXES=.c .d .o CFLAGS+=-Wall #CFLAGS+=-O2 CFLAGS+=-g -fno-omit-frame-pointer + #CFLAGS+=-pg +#LD_FLAGS=-pg + CFLAGS+=-DDEBUG_MEM -DDEBUG #CFLAGS+=-DBOUNDS_CHECK + INCLUDES+=-I. -I$(top_srcdir)/include INC_LNS=$(top_srcdir)/include/.symlinks_created -LD_FLAGS=-pg ifeq ("@READLINE@", "yes") CFLAGS += -DREADLINE_SUPPORT diff --git a/tools/Makefile.in b/tools/Makefile.in index 335058544..f6b884040 100644 --- a/tools/Makefile.in +++ b/tools/Makefile.in @@ -29,6 +29,7 @@ SOURCES=\ lvmchange.c \ lvreduce.c \ lvremove.c \ + lvresize.c \ lvscan.c \ pvchange.c \ pvcreate.c \ diff --git a/tools/commands.h b/tools/commands.h index 39d1e6ee9..d9ac0b905 100644 --- a/tools/commands.h +++ b/tools/commands.h @@ -199,6 +199,19 @@ xx(lvrename, autobackup_ARG) +xx(lvresize, + "Resize a logical volume", + "lvresize\n" + "\t[-A/--autobackup y/n]\n" + "\t[-d/--debug]\n" + "\t[-h/-?/--help]\n" + "\t{-l/--extents [+/-]LogicalExtentsNumber |\n" + "\t -L/--size [+/-]LogicalVolumeSize[kKmMgGtT]}\n" + "\t[-v/--verbose]\n" + "\tLogicalVolume[Path] [ PhysicalVolumePath... ]\n", + + autobackup_ARG, extents_ARG, size_ARG) + xx(lvscan, "List all logical volumes in all volume groups", "lvscan " diff --git a/tools/lvextend.c b/tools/lvextend.c index 84aa372ef..48e036651 100644 --- a/tools/lvextend.c +++ b/tools/lvextend.c @@ -22,167 +22,5 @@ int lvextend(int argc, char **argv) { - struct volume_group *vg; - struct logical_volume *lv; - uint32_t extents = 0; - uint32_t size = 0; - sign_t sign = SIGN_NONE; - char *lv_name, *vg_name; - char *st; - char *dummy; - struct list *lvh, *pvh, *pvl; - int opt = 0; - - if (arg_count(extents_ARG) + arg_count(size_ARG) != 1) { - log_error("Please specify either size or extents (not both)"); - return EINVALID_CMD_LINE; - } - - if (arg_count(extents_ARG)) { - extents = arg_int_value(extents_ARG, 0); - sign = arg_sign_value(extents_ARG, SIGN_NONE); - } - - if (arg_count(size_ARG)) { - size = arg_int_value(size_ARG, 0); - sign = arg_sign_value(extents_ARG, SIGN_NONE); - } - - if (sign == SIGN_MINUS) { - log_error("Negative argument not permitted - use lvreduce"); - return EINVALID_CMD_LINE; - } - - if (!argc) { - log_error("Please provide the logical volume name"); - return EINVALID_CMD_LINE; - } - - lv_name = argv[0]; - argv++; - argc--; - - if (!(vg_name = extract_vgname(fid, lv_name))) { - log_error("Please provide a volume group name"); - return EINVALID_CMD_LINE; - } - - if ((st = strrchr(lv_name, '/'))) - lv_name = st + 1; - - /* does VG exist? */ - log_verbose("Finding volume group %s", vg_name); - if (!(vg = fid->ops->vg_read(fid, vg_name))) { - log_error("Volume group %s doesn't exist", vg_name); - return ECMD_FAILED; - } - - if (!(vg->status & ACTIVE)) { - log_error("Volume group %s must be active before changing a " - "logical volume", vg_name); - return ECMD_FAILED; - } - - /* does LV exist? */ - if (!(lvh = find_lv_in_vg(vg, lv_name))) { - log_error("Logical volume %s not found in volume group %s", - lv_name, vg_name); - return ECMD_FAILED; - } - - lv = &list_item(lvh, struct lv_list)->lv; - - if (!(lv->status & ACTIVE)) { - log_error("Logical volume %s must be active before change", - lv_name); - return ECMD_FAILED; - } - - if (argc) { - /* Build up list of PVs */ - if (!(pvh = pool_alloc(fid->cmd->mem, sizeof (struct list)))) { - log_error("pvh list allocation failed"); - return ECMD_FAILED; - } - list_init(pvh); - for (; opt < argc; opt++) { - if (!(pvl = find_pv_in_vg(vg, argv[opt]))) { - log_error("Physical Volume %s not found in " - "Volume Group %s", argv[opt], - vg->name); - return EINVALID_CMD_LINE; - } - if (list_item(pvl, struct pv_list)->pv.pe_count == - list_item(pvl, struct pv_list)->pv.pe_allocated) { - log_error("No free extents on physical volume" - " %s", argv[opt]); - continue; - /* FIXME Buy check not empty at end! */ - } - list_add(pvh, pvl); - } - } else { - /* Use full list from VG */ - pvh = &vg->pvs; - } - - if (size) { - /* No of 512-byte sectors */ - extents = size * 2; - - if (extents % vg->extent_size) { - char *s1; - - extents += vg->extent_size - - (extents % vg->extent_size); - log_print("Rounding up size to full physical extent %s", - (s1 = display_size(extents / 2, SIZE_SHORT))); - dbg_free(s1); - } - - extents /= vg->extent_size; - } - - if (sign == SIGN_PLUS) - extents += lv->le_count; - - if (extents <= lv->le_count) { - log_error("New size given (%d extents) not larger than " - "existing size (%d extents)", extents, lv->le_count); - return EINVALID_CMD_LINE; - } - - if (!extents) { - log_error("New size of 0 not permitted"); - return EINVALID_CMD_LINE; - } - - log_print("Extending logical volume %s to %s", lv_name, - (dummy = - display_size(extents * vg->extent_size / 2, SIZE_SHORT))); - dbg_free(dummy); - - lv_extend(lv, extents - lv->le_count, pvh); - /* where parm is always *increase* not actual */ - -/********* FIXME Suspend lv ***********/ - - /* store vg on disk(s) */ - if (!fid->ops->vg_write(fid, vg)) - return ECMD_FAILED; - - /* FIXME Ensure it always displays errors? */ - if (!lv_reactivate(lv)) - return ECMD_FAILED; - -/********* FIXME Resume *********/ - -/********* FIXME Backup - if ((ret = do_autobackup(vg_name, vg))) - return ret; -************/ - - log_print("Logical volume %s successfully extended", lv_name); - - return 0; + return lvresize(argc, argv); } diff --git a/tools/lvm.c b/tools/lvm.c index 461067e0e..1ee0a762a 100644 --- a/tools/lvm.c +++ b/tools/lvm.c @@ -35,7 +35,7 @@ #include #endif -/* define the table of valid switches */ +/* define exported table of valid switches */ struct arg the_args[ARG_COUNT + 1] = { #define xx(a, b, c, d) {b, "--" c, d, 0, NULL}, @@ -44,17 +44,6 @@ struct arg the_args[ARG_COUNT + 1] = { }; -/* a register of the lvm commands */ -struct command { - const char *name; - const char *desc; - const char *usage; - command_fn fn; - - int num_args; - int *valid_args; -}; - static int _array_size; static int _num_commands; static struct command *_commands; @@ -62,11 +51,13 @@ static struct command *_commands; /* Exported LVM1 disk format */ struct format_instance *fid; +/* Export command being processed */ +struct command *the_command; + struct cmd_context *cmd; /* Whether or not to dump persistent filter state */ static int dump_filter; -static struct config_file *_cf; static int _interactive; static FILE *_log; @@ -544,20 +535,19 @@ static void display_help() static int run_command(int argc, char **argv) { int ret = 0; - struct command *com; - if (!(com = find_command(argv[0]))) + if (!(the_command = find_command(argv[0]))) return ENO_SUCH_CMD; - if (!process_command_line(com, &argc, &argv)) { + if (!process_command_line(the_command, &argc, &argv)) { log_error("Error during parsing of command line."); return EINVALID_CMD_LINE; } - if ((ret = process_common_commands(com))) + if ((ret = process_common_commands(the_command))) return ret; - ret = com->fn(argc, argv); + ret = the_command->fn(argc, argv); /* * free off any memory the command used. @@ -565,7 +555,7 @@ static int run_command(int argc, char **argv) pool_empty(cmd->mem); if (ret == EINVALID_CMD_LINE && !_interactive) - usage(com->name); + usage(the_command->name); return ret; } @@ -598,11 +588,6 @@ static int split(char *str, int *argc, char **argv, int max) return *argc; } -struct config_file *active_config_file(void) -{ - return _cf; -} - static void __init_log(struct config_file *cf) { char *open_mode = "a"; @@ -628,7 +613,7 @@ static void __init_log(struct config_file *cf) init_verbose(verbose_level); } -static int dev_cache_setup(void) +static int dev_cache_setup(struct config_file *cf) { struct config_node *cn; struct config_value *cv; @@ -638,7 +623,7 @@ static int dev_cache_setup(void) return 0; } - if (!(cn = find_config_node(_cf->root, "devices/scan", '/'))) { + if (!(cn = find_config_node(cf->root, "devices/scan", '/'))) { if (!dev_cache_add_dir("/dev")) { log_error("Failed to add /dev to internal " "device cache"); @@ -666,7 +651,7 @@ static int dev_cache_setup(void) return 1; } -static struct dev_filter *filter_components_setup(void) +static struct dev_filter *filter_components_setup(struct config_file *cf) { struct config_node *cn; struct dev_filter *f1, *f2, *f3; @@ -674,7 +659,7 @@ static struct dev_filter *filter_components_setup(void) if (!(f2 = lvm_type_filter_create())) return 0; - if (!(cn = find_config_node(_cf->root, "devices/filter", '/'))) { + if (!(cn = find_config_node(cf->root, "devices/filter", '/'))) { log_debug("devices/filter not found in config file: no regex " "filter installed"); return f2; @@ -693,7 +678,7 @@ static struct dev_filter *filter_components_setup(void) return f3; } -static struct dev_filter *filter_setup(void) +static struct dev_filter *filter_setup(struct config_file *cf) { const char *lvm_cache; struct dev_filter *f3, *f4; @@ -701,10 +686,10 @@ static struct dev_filter *filter_setup(void) dump_filter = 0; - if (!(f3 = filter_components_setup())) + if (!(f3 = filter_components_setup(cmd->cf))) return 0; - lvm_cache = find_config_str(_cf->root, "devices/cache", '/', + lvm_cache = find_config_str(cf->root, "devices/cache", '/', "/etc/lvm/.cache"); if (!(f4 = persistent_filter_create(f3, lvm_cache))) { @@ -713,7 +698,7 @@ static struct dev_filter *filter_setup(void) } /* Should we ever dump persistent filter state? */ - if (find_config_int(_cf->root, "devices/write_cache_state", '/', 1)) + if (find_config_int(cf->root, "devices/write_cache_state", '/', 1)) dump_filter = 1; if (!stat(lvm_cache, &st) && !persistent_filter_load(f4)) @@ -737,7 +722,7 @@ static int init(void) /* FIXME: Override from config file. (Append trailing slash if reqd) */ cmd->dev_dir = "/dev/"; - if (!(_cf = create_config_file())) { + if (!(cmd->cf = create_config_file())) { stack; goto out; } @@ -751,19 +736,19 @@ static int init(void) e = e ? e : "/etc/lvm/lvm.conf"; if (stat(e, &info) != -1) { /* we've found a config file */ - if (!read_config(_cf, e)) { + if (!read_config(cmd->cf, e)) { log_error("Failed to load config file %s", e); goto out; } - __init_log(_cf); + __init_log(cmd->cf); } - if (!dev_cache_setup()) { + if (!dev_cache_setup(cmd->cf)) { goto out; } - if (!(cmd->filter = filter_setup())) { + if (!(cmd->filter = filter_setup(cmd->cf))) { log_error("Failed to set up internal device filters"); goto out; } @@ -802,7 +787,7 @@ static void fin(void) cmd->filter->destroy(cmd->filter); pool_destroy(cmd->mem); dev_cache_exit(); - destroy_config_file(_cf); + destroy_config_file(cmd->cf); dbg_free(cmd); __fin_commands(); dump_memory(); diff --git a/tools/lvreduce.c b/tools/lvreduce.c index caa122215..3425de2f4 100644 --- a/tools/lvreduce.c +++ b/tools/lvreduce.c @@ -22,179 +22,5 @@ int lvreduce(int argc, char **argv) { - struct volume_group *vg; - struct logical_volume *lv; - struct list *lvh; - uint32_t extents = 0; - uint32_t size = 0; - sign_t sign = SIGN_NONE; - char *lv_name, *vg_name; - char *st; - int i; - - if (arg_count(extents_ARG) + arg_count(size_ARG) != 1) { - log_error("Please specify either size or extents (not both)"); - return EINVALID_CMD_LINE; - } - - if (arg_count(extents_ARG)) { - extents = arg_int_value(extents_ARG, 0); - sign = arg_sign_value(extents_ARG, SIGN_NONE); - } - - if (arg_count(size_ARG)) { - size = arg_int_value(size_ARG, 0); - sign = arg_sign_value(extents_ARG, SIGN_NONE); - } - - if (sign == SIGN_PLUS) { - log_error("Positive sign not permitted - use lvextend"); - return EINVALID_CMD_LINE; - } - - if (!argc) { - log_error("Please provide the logical volume name"); - return EINVALID_CMD_LINE; - } - - lv_name = argv[0]; - argv++; - argc--; - - if (!(vg_name = extract_vgname(fid, lv_name))) { - log_error("Please provide a volume group name"); - return EINVALID_CMD_LINE; - } - - if ((st = strrchr(lv_name, '/'))) - lv_name = st + 1; - - /* does VG exist? */ - log_verbose("Finding volume group %s", vg_name); - if (!(vg = fid->ops->vg_read(fid, vg_name))) { - log_error("Volume group %s doesn't exist", vg_name); - return ECMD_FAILED; - } - - if (!(vg->status & ACTIVE)) { - log_error("Volume group %s must be active before changing a " - "logical volume", vg_name); - return ECMD_FAILED; - } - - /* does LV exist? */ - if (!(lvh = find_lv_in_vg(vg, lv_name))) { - log_error("Logical volume %s not found in volume group %s", - lv_name, vg_name); - return ECMD_FAILED; - } - - lv = &list_item(lvh, struct lv_list)->lv; - - if (!(lv->status & ACTIVE)) { - log_error("Logical volume %s must be active before reduction", - lv_name); - return ECMD_FAILED; - } - - if (size) { - /* No of 512-byte sectors */ - extents = size * 2; - - if (extents % vg->extent_size) { - char *s1; - - if (sign == SIGN_NONE) - extents += vg->extent_size - - (extents % vg->extent_size); - else - extents -= extents % vg->extent_size; - - log_print("Rounding up size to full physical extent %s", - (s1 = display_size(extents / 2, SIZE_SHORT))); - dbg_free(s1); - } - - extents /= vg->extent_size; - } - - if (!extents) { - log_error("New size of 0 not permitted"); - return EINVALID_CMD_LINE; - } - - if (sign == SIGN_MINUS) { - if (extents >= lv->le_count) { - log_error("Unable to reduce %s below 1 extent", - lv_name); - return EINVALID_CMD_LINE; - } - - extents = lv->le_count - extents; - } else { - if (extents >= lv->le_count) { - log_error("New size given (%d extents) not less than " - "existing size (%d extents)", extents, - lv->le_count); - return EINVALID_CMD_LINE; - } - } - -/************ FIXME Stripes - size_rest = new_size % (vg->lv[l]->lv_stripes * vg->pe_size); - if (size_rest != 0) { - log_print - ("rounding size %ld KB to stripe boundary size ", - new_size / 2); - new_size = new_size - size_rest; - printf("%ld KB\n", new_size / 2); - } -***********************/ - - if (lv->status & ACTIVE || lv_active(lv) > 0) { - char *dummy; - log_print("WARNING: Reducing active%s logical volume to %s", - (lv_open_count(lv) > 0) ? " and open" : "", - (dummy = - display_size(extents * vg->extent_size / 2, - SIZE_SHORT))); - log_print("THIS MAY DESTROY YOUR DATA (filesystem etc.)"); - dbg_free(dummy); - } - - if (!arg_count(force_ARG)) { - if (yes_no_prompt - ("Do you really want to reduce %s? [y/n]: ", lv_name) - == 'n') { - log_print("Logical volume %s NOT reduced", lv_name); - return ECMD_FAILED; - } - } - - for (i = extents; i < lv->le_count; i++) { - lv->map[i].pv->pe_allocated--; - } - - lv->le_count = extents; - -/********* FIXME Suspend lv ***********/ - - /* store vg on disk(s) */ - if (!fid->ops->vg_write(fid, vg)) - return ECMD_FAILED; - - /* FIXME Ensure it always displays errors? */ - if (!lv_reactivate(lv)) - return ECMD_FAILED; - -/********* FIXME Resume *********/ - -/********* FIXME Backup - if ((ret = do_autobackup(vg_name, vg))) - return ret; -************/ - - log_print("Logical volume %s reduced", lv_name); - - return 0; + return lvresize(argc, argv); } diff --git a/tools/lvresize.c b/tools/lvresize.c new file mode 100644 index 000000000..9bc63c68e --- /dev/null +++ b/tools/lvresize.c @@ -0,0 +1,281 @@ +/* + * Copyright (C) 2001 Sistina Software + * + * LVM is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * LVM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LVM; see the file COPYING. If not, write to + * the Free Software Foundation, 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + */ + +#include "tools.h" + +int lvresize(int argc, char **argv) +{ + struct volume_group *vg; + struct logical_volume *lv; + uint32_t extents = 0; + uint32_t size = 0; + sign_t sign = SIGN_NONE; + char *lv_name, *vg_name; + char *st; + char *dummy; + const char *cmd_name; + struct list *lvh, *pvh, *pvl; + int opt = 0; + + enum { + LV_ANY = 0, + LV_REDUCE = 1, + LV_EXTEND = 2 + } resize = LV_ANY; + + cmd_name = command_name(); + if (!strcmp(cmd_name, "lvreduce")) + resize = LV_REDUCE; + if (!strcmp(cmd_name, "lvextend")) + resize = LV_EXTEND; + + if (arg_count(extents_ARG) + arg_count(size_ARG) != 1) { + log_error("Please specify either size or extents (not both)"); + return EINVALID_CMD_LINE; + } + + if (arg_count(extents_ARG)) { + extents = arg_int_value(extents_ARG, 0); + sign = arg_sign_value(extents_ARG, SIGN_NONE); + } + + if (arg_count(size_ARG)) { + size = arg_int_value(size_ARG, 0); + sign = arg_sign_value(size_ARG, SIGN_NONE); + } + + if (resize == LV_EXTEND && sign == SIGN_MINUS) { + log_error("Negative argument not permitted - use lvreduce"); + return EINVALID_CMD_LINE; + } + + if (resize == LV_REDUCE && sign == SIGN_PLUS) { + log_error("Positive sign not permitted - use lvextend"); + return EINVALID_CMD_LINE; + } + + if (!argc) { + log_error("Please provide the logical volume name"); + return EINVALID_CMD_LINE; + } + + lv_name = argv[0]; + argv++; + argc--; + + if (!(vg_name = extract_vgname(fid, lv_name))) { + log_error("Please provide a volume group name"); + return EINVALID_CMD_LINE; + } + + if ((st = strrchr(lv_name, '/'))) + lv_name = st + 1; + + /* does VG exist? */ + log_verbose("Finding volume group %s", vg_name); + if (!(vg = fid->ops->vg_read(fid, vg_name))) { + log_error("Volume group %s doesn't exist", vg_name); + return ECMD_FAILED; + } + + if (!(vg->status & ACTIVE)) { + log_error("Volume group %s must be active before changing a " + "logical volume", vg_name); + return ECMD_FAILED; + } + + /* does LV exist? */ + if (!(lvh = find_lv_in_vg(vg, lv_name))) { + log_error("Logical volume %s not found in volume group %s", + lv_name, vg_name); + return ECMD_FAILED; + } + + lv = &list_item(lvh, struct lv_list)->lv; + + if (!(lv->status & ACTIVE)) { + log_error("Logical volume %s must be active before change", + lv_name); + return ECMD_FAILED; + } + + if (size) { + /* No of 512-byte sectors */ + extents = size * 2; + + if (extents % vg->extent_size) { + char *s1; + + if (sign == SIGN_MINUS) + extents -= extents % vg->extent_size; + else + extents += vg->extent_size - + (extents % vg->extent_size); + + log_print("Rounding up size to full physical extent %s", + (s1 = display_size(extents / 2, SIZE_SHORT))); + dbg_free(s1); + } + + extents /= vg->extent_size; + } + + if (sign == SIGN_PLUS) + extents += lv->le_count; + + if (sign == SIGN_MINUS) { + if (extents >= lv->le_count) { + log_error("Unable to reduce %s below 1 extent", + lv_name); + return EINVALID_CMD_LINE; + } + + extents = lv->le_count - extents; + } + +/************ FIXME Stripes (lvreduce) + size_rest = new_size % (vg->lv[l]->lv_stripes * vg->pe_size); + if (size_rest != 0) { + log_print + ("rounding size %ld KB to stripe boundary size ", + new_size / 2); + new_size = new_size - size_rest; + printf("%ld KB\n", new_size / 2); + } +***********************/ + + if (!extents) { + log_error("New size of 0 not permitted"); + return EINVALID_CMD_LINE; + } + + if (extents == lv->le_count) { + log_error("New size (%d extents) matches existing size " + "(%d extents)", extents, lv->le_count); + return EINVALID_CMD_LINE; + } + + if (extents < lv->le_count) { + if (resize == LV_EXTEND) { + log_error("New size given (%d extents) not larger " + "than existing size (%d extents)", + extents, lv->le_count); + return EINVALID_CMD_LINE; + } else + resize = LV_REDUCE; + } + + if (extents > lv->le_count) { + if (resize == LV_REDUCE) { + log_error("New size given (%d extents) not less than " + "existing size (%d extents)", extents, + lv->le_count); + return EINVALID_CMD_LINE; + } else + resize = LV_EXTEND; + } + + if (resize == LV_REDUCE) { + if (argc) + log_print("Ignoring PVs on command line when reducing"); + + if (lv->status & ACTIVE || lv_active(lv)) { + dummy = + display_size(extents * vg->extent_size / 2, + SIZE_SHORT); + log_print("WARNING: Reducing active%s logical volume " + "to %s", lv_open_count(lv) ? " and open" : "", + dummy); + + log_print("THIS MAY DESTROY YOUR DATA " + "(filesystem etc.)"); + dbg_free(dummy); + } + + if (!arg_count(force_ARG)) { + if (yes_no_prompt("Do you really want to reduce %s?" + " [y/n]: ", lv_name) == 'n') { + log_print("Logical volume %s NOT reduced", + lv_name); + return ECMD_FAILED; + } + } + + lv_reduce(lv, lv->le_count - extents); + } + + if (resize == LV_EXTEND && argc) { + /* Build up list of PVs */ + if (!(pvh = pool_alloc(fid->cmd->mem, sizeof(struct list)))) { + log_error("pvh list allocation failed"); + return ECMD_FAILED; + } + list_init(pvh); + for (; opt < argc; opt++) { + if (!(pvl = find_pv_in_vg(vg, argv[opt]))) { + log_error("Physical Volume %s not found in " + "Volume Group %s", argv[opt], + vg->name); + return EINVALID_CMD_LINE; + } + if (list_item(pvl, struct pv_list)->pv.pe_count == + list_item(pvl, struct pv_list)->pv.pe_allocated) { + log_error("No free extents on physical volume" + " %s", argv[opt]); + continue; + /* FIXME Buy check not empty at end! */ + } + list_add(pvh, pvl); + } + } + + if (resize == LV_EXTEND) { + if (!argc) { + /* Use full list from VG */ + pvh = &vg->pvs; + } + dummy = display_size(extents * vg->extent_size / 2, SIZE_SHORT); + log_print("Extending logical volume %s to %s", lv_name, dummy); + dbg_free(dummy); + + lv_extend(lv, extents - lv->le_count, pvh); + } + +/********* FIXME Suspend lv ***********/ + + /* store vg on disk(s) */ + if (!fid->ops->vg_write(fid, vg)) + return ECMD_FAILED; + + /* FIXME Ensure it always displays errors? */ + if (!lv_reactivate(lv)) + return ECMD_FAILED; + +/********* FIXME Resume *********/ + +/********* FIXME Backup + if ((ret = do_autobackup(vg_name, vg))) + return ret; +************/ + + log_print("Logical volume %s successfully resized", lv_name); + + return 0; +} diff --git a/tools/tools.h b/tools/tools.h index c019dab41..509695b5b 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -81,6 +81,19 @@ struct arg { extern struct arg the_args[ARG_COUNT + 1]; +/* a register of the lvm commands */ +struct command { + const char *name; + const char *desc; + const char *usage; + command_fn fn; + + int num_args; + int *valid_args; +}; + +extern struct command *the_command; + void usage(const char *name); /* the argument verify/normalise functions */ @@ -122,7 +135,10 @@ static inline int arg_count_increment(int a) return the_args[a].count++; } -struct config_file *active_config_file(void); +static inline const char *command_name(void) +{ + return the_command->name; +} extern struct format_instance *fid;