diff --git a/include/Makefile.in b/include/Makefile.in index 926422faf..ceda195a0 100644 --- a/include/Makefile.in +++ b/include/Makefile.in @@ -35,7 +35,7 @@ clean: distclean: find . -maxdepth 1 -type l -exec $(RM) \{\} \; - $(RM) Makefile .include_symlinks + $(RM) Makefile .include_symlinks .symlinks_created .PHONY: clean distclean all diff --git a/lib/device/device.h b/lib/device/device.h index ee5f3b1b0..079c26ba5 100644 --- a/lib/device/device.h +++ b/lib/device/device.h @@ -32,5 +32,9 @@ int64_t dev_write(struct device *dev, /* FIXME: Alasdair lets add more query functions here for accessing the partition information, this can then be used by your filter. */ +#define LVM_DEFAULT_DIR_PREFIX "/dev/" +/* FIXME Allow config file override */ +static inline char *lvm_dir_prefix(void) { return LVM_DEFAULT_DIR_PREFIX; } + #endif diff --git a/tools/Makefile.in b/tools/Makefile.in index 388f7cd51..a81d35b7c 100644 --- a/tools/Makefile.in +++ b/tools/Makefile.in @@ -24,8 +24,10 @@ SOURCES=\ lvm.c\ lvremove.c\ pvcreate.c\ + pvchange.c\ pvdisplay.c\ pvscan.c\ + vgrename.c\ lvactivate.c TARGETS=\ diff --git a/tools/pvchange.c b/tools/pvchange.c new file mode 100644 index 000000000..c13c2c9fa --- /dev/null +++ b/tools/pvchange.c @@ -0,0 +1,181 @@ +/* + * 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" + +void pvchange_single_volume(struct physical_volume *pv); + +int pvchange(int argc, char **argv) +{ + int back_it_up = 0; + int change_msg = 0; + int done = 0; + int doit = 0; + int not_done = 0; + int allocation; + + int opt = 0; + int ret = 0; + + char *pv_name; + char *vg_name; + + struct io_space *ios; + struct physical_volume *pv = NULL; + struct volume_group *vg = NULL; + struct device *pv_dev; + struct list_head *pvh; + struct pv_list *pvl, *pvs_list; + + allocation = !strcmp(arg_str_value(allocation_ARG, "n"), "y"); + + if (arg_count(allocation_ARG) == 0) { + log_error("Please give the x option"); + return LVM_EINVALID_CMD_LINE; + } + + ios = active_ios(); + + if (!(arg_count(all_ARG)) && !argc) { + log_error("Please give a physical volume path"); + return LVM_EINVALID_CMD_LINE; + } + + if (arg_count(all_ARG) && argc) { + log_error("Option a and PhysicalVolumePath are exclusive"); + return LVM_EINVALID_CMD_LINE; + } + + if (arg_count(all_ARG)) { + log_verbose("Scanning for physical volume names"); + if (!(pvs_list = ios->get_pvs(ios))) { + return LVM_ECMD_FAILED; + } + + list_for_each(pvh, &pvs_list->list) { + pvl = list_entry(pvh, struct pv_list, list); + pv = &pvl->pv; + pvchange_single_volume(pv); + } + } else { + for (; opt < argc; opt++) { + pv_name = argv[opt]; + if (!(pv_dev = dev_cache_get(pv_name))) { + log_error("Device %s not found", pv_name); + continue; + } + if (!(pv = ios->pv_read(ios, pv_dev))) { + log_error("Failed to read physical volume %s", + pv_name); + continue; + } + pvchange_single_volume(pv); + } + } + + if (back_it_up) + if ((ret = do_autobackup(vg_name, vg))) + return ret; + + log_print("%d physical volume%s changed / %d physical volume%s " + "already o.k.", done, done != 1 ? "s" : "", not_done, + not_done != 1 ? "s" : ""); + + return 0; +} + +void pvchange_single_volume(struct physical_volume *pv) +{ + struct volume_group *vg; + + change_msg = 0; + + /* FIXME: Check these verbose messages appear in the library now */ + /* log_verbose("reading physical volume data %s from disk", pv_name); */ + + /* FIXME: Ensure these are tested in the library */ + /* log_error("physical volume %s has an invalid version", pv_name); */ + /* log_error("physical volume %s has invalid identity", pv_name); */ + + /* FIXME: Where do consistency checks fit? */ + /* log_verbose("checking physical volume %s consistency", pv_name); */ + + /* FIXME: Does the VG really have to be active to proceed? */ + log_verbose("finding volume group of physical volume %s", pv_name); + if (!(vg = ios->vg_read(ios, pv->vg_name))) { + log_print("unable to find volume group of %s (VG not active?)", + pv->dev->name); + doit = 0; + return; + } + + back_it_up = doit = 1; + + /* change allocatability for a PV */ + if (arg_count(allocation_ARG) > 0) { + if (allocation && (pv->status & STATUS_ALLOCATED)) { + log_error("physical volume %s is allocatable", pv_name); + not_done++; + return; + } else + change_msg = 1; + + if (!allocation && !(pv->status & STATUS_ALLOCATED)) { + log_error("physical volume %s is unallocatable", + pv_name); + not_done++; + return; + } else + change_msg = 1; + + if (allocation) { + log_verbose + ("setting physical volume %s allocatable", pv_name); + pv->status |= STATUS_ALLOCATED; + } else { + log_verbose + ("setting physical volume %s NOT allocatable", + pv_name); + pv->status &= ~STATUS_ALLOCATED; + } + } + + done++; + + if (doit == 1) { + log_verbose("checking physical volume %s is activite", + pv->dev->name); + if (!(pv->status & STATUS_ACTIVE)) { + log_verbose("Physical volume %s inactive", pv_name); + } + + log_verbose("Updating physical volume %s", pv->dev->name); + if (!(ios->pv_write(ios, pv))) { + log_error + ("Failed to store physical volume %s", + pv->dev->name); + /* Abort completely here? */ + return LVM_E_PV_WRITE; + } + + log_print("physical volume %s %s changed", pv_name, + (change_msg) ? "" : "not "); + } +} diff --git a/tools/stub.h b/tools/stub.h index ad4f00dce..c17cd56b3 100644 --- a/tools/stub.h +++ b/tools/stub.h @@ -30,7 +30,6 @@ int lvmsar(int argc, char **argv) {return 1;} int lvreduce(int argc, char **argv) {return 1;} int lvrename(int argc, char **argv) {return 1;} int lvscan(int argc, char **argv) {return 1;} -int pvchange(int argc, char **argv) {return 1;} int pvdata(int argc, char **argv) {return 1;} int vgcfgbackup(int argc, char **argv) {return 1;} int vgcfgrestore(int argc, char **argv) {return 1;} @@ -44,7 +43,6 @@ int vgimport(int argc, char **argv) {return 1;} int vgmerge(int argc, char **argv) {return 1;} int vgmknodes(int argc, char **argv) {return 1;} int vgreduce(int argc, char **argv) {return 1;} -int vgrename(int argc, char **argv) {return 1;} int vgremove(int argc, char **argv) {return 1;} int vgscan(int argc, char **argv) {return 1;} int vgsplit(int argc, char **argv) {return 1;} diff --git a/tools/vgrename.c b/tools/vgrename.c new file mode 100644 index 000000000..d386b6c0b --- /dev/null +++ b/tools/vgrename.c @@ -0,0 +1,157 @@ +/* + * 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" + +char *lv_change_vgname(char *vg_name, char *lv_name); + +int vgrename(int argc, char **argv) +{ + int l = 0; + int length = 0; + int p = 0; + + int ret = 0; + char *lv_name_ptr; + char *vg_name_old; + char *vg_name_new; + char vg_name_old_buf[NAME_LEN] = { 0, }; + char vg_name_new_buf[NAME_LEN] = { 0, }; + char *prefix; + + struct io_space *ios; + struct volume_group *vg_old, *vg_new; + + if (argc != 2) { + log_error("command line too short"); + return LVM_EINVALID_CMD_LINE; + } + + ios = active_ios(); + + prefix = lvm_dir_prefix(); + length = strlen(prefix); + + vg_name_old = argv[0]; + + if (strlen(vg_name_new = argv[1]) > NAME_LEN - length - 2) { + log_error("New logical volume path exceeds maximum length " + "of %d!", NAME_LEN - length - 2); + return LVM_ECMD_FAILED; + } + + if (vg_check_name(vg_name_new) < 0) { + return LVM_EINVALID_CMD_LINE; + } + + /* FIXME Handle prefix-related logic internally within ios functions? */ + if (strncmp(vg_name_old, prefix, length) != 0) { + sprintf(vg_name_old_buf, "%s%s", prefix, vg_name_old); + vg_name_old = vg_name_old_buf; + } + if (strncmp(vg_name_new, prefix, length) != 0) { + sprintf(vg_name_new_buf, "%s%s", prefix, vg_name_new); + vg_name_new = vg_name_new_buf; + } + + if (strcmp(vg_name_old, vg_name_new) == 0) { + log_error("volume group names must be different"); + return LVM_ECMD_FAILED; + } + + log_verbose("Checking existing volume group %s", vg_name_old); + if (!(vg_old = ios->vg_read(ios, vg_name_old))) { + log_error("volume group %s doesn't exist", vg_name_old); + return LVM_ECMD_FAILED; + } + if (vg_old->status & STATUS_ACTIVE) { + log_error("Volume group %s still active", vg_name_old); + } + + log_verbose("Checking new volume group %s", vg_name_new); + if ((vg_new = ios->vg_read(ios, vg_name_new))) { + log_error("New volume group %s already exists", vg_name_new); + return LVM_ECMD_FAILED; + } + + /* change the volume name in all structures */ + strcpy(vg_old->name, vg_name_new); + + /* FIXME: Are these necessary? Or can vg_write fix these implicitly? */ + for (p = 0; p < vg_old->pv_count; p++) + if (vg_old->pv[p]) + strcpy(vg_old->pv[p]->vg_name, vg_name_new); + + for (l = 0; l < vg_old->lv_count; l++) { + if (vg_old->lv[l] && + !(lv_name_ptr = + lv_change_vgname(vg_name_new, vg_old->lv[l]->name))) { + log_error("A new logical volume path exceeds " + "maximum of %d!", NAME_LEN - 2); + return LVM_ECMD_FAILED; + } + strcpy(vg_old->lv[l]->name, lv_name_ptr); + } + + if (vg_remove_dir_and_group_and_nodes(vg_name_old) < 0) { + log_error("removing volume group nodes and directory of \"%s\"", + vg_name_old); + return LVM_ECMD_FAILED; + } + + /* store it on disks */ + log_verbose("updating volume group name"); + if (ios->vg_write(ios, vg_old)) { + return LVM_ECMD_FAILED; + } + + log_verbose("creating volume group directory %s%s", prefix, + vg_name_new); + if (vg_create_dir_and_group_and_nodes(vg_old)) { + return LVM_ECMD_FAILED; + } + + if ((ret = do_autobackup(vg_name_new, vg_old))) + return LVM_ECMD_FAILED; + + log_print("Volume group %s successfully renamed to %s", + vg_name_old, vg_name_new); + + return 0; + +/* FIXME: Deallocations */ +} + +/* FIXME: Move this out */ + +char *lv_change_vgname(char *vg_name, char *lv_name) +{ + char *lv_name_ptr = NULL; + static char lv_name_buf[NAME_LEN] = { 0, }; + + /* check if lv_name includes a path */ + if ((lv_name_ptr = strrchr(lv_name, '/'))) { + lv_name_ptr++; + sprintf(lv_name_buf, "%s%s/%s%c", lvm_dir_prefix(), vg_name, + lv_name_ptr, 0); + } else + strncpy(lv_name_buf, lv_name, NAME_LEN - 1); + return lv_name_buf; +}