Home | History | Annotate | Line # | Download | only in tools
pvchange.c revision 1.1
      1 /*	$NetBSD: pvchange.c,v 1.1 2008/12/22 00:19:06 haad Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
      5  * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
      6  *
      7  * This file is part of LVM2.
      8  *
      9  * This copyrighted material is made available to anyone wishing to use,
     10  * modify, copy, or redistribute it subject to the terms and conditions
     11  * of the GNU Lesser General Public License v.2.1.
     12  *
     13  * You should have received a copy of the GNU Lesser General Public License
     14  * along with this program; if not, write to the Free Software Foundation,
     15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     16  */
     17 
     18 #include "tools.h"
     19 
     20 /* FIXME Locking.  PVs in VG. */
     21 
     22 static int _pvchange_single(struct cmd_context *cmd, struct physical_volume *pv,
     23 			    void *handle __attribute((unused)))
     24 {
     25 	struct volume_group *vg = NULL;
     26 	const char *vg_name = NULL;
     27 	struct pv_list *pvl;
     28 	uint64_t sector;
     29 	uint32_t orig_pe_alloc_count;
     30 	/* FIXME Next three only required for format1. */
     31 	uint32_t orig_pe_count, orig_pe_size;
     32 	uint64_t orig_pe_start;
     33 
     34 	const char *pv_name = pv_dev_name(pv);
     35 	const char *tag = NULL;
     36 	const char *orig_vg_name;
     37 	char uuid[64] __attribute((aligned(8)));
     38 
     39 	int allocatable = 0;
     40 	int tagarg = 0;
     41 
     42 	if (arg_count(cmd, addtag_ARG))
     43 		tagarg = addtag_ARG;
     44 	else if (arg_count(cmd, deltag_ARG))
     45 		tagarg = deltag_ARG;
     46 
     47 	if (arg_count(cmd, allocatable_ARG))
     48 		allocatable = !strcmp(arg_str_value(cmd, allocatable_ARG, "n"),
     49 				      "y");
     50 	else if (tagarg && !(tag = arg_str_value(cmd, tagarg, NULL))) {
     51 		log_error("Failed to get tag");
     52 		return 0;
     53 	}
     54 
     55 	/* If in a VG, must change using volume group. */
     56 	if (!is_orphan(pv)) {
     57 		vg_name = pv_vg_name(pv);
     58 
     59 		log_verbose("Finding volume group %s of physical volume %s",
     60 			    vg_name, pv_name);
     61 		if (!(vg = vg_lock_and_read(cmd, vg_name, NULL, LCK_VG_WRITE,
     62 					    CLUSTERED | EXPORTED_VG | LVM_WRITE,
     63 					    CORRECT_INCONSISTENT)))
     64 			return_0;
     65 
     66 		if (!(pvl = find_pv_in_vg(vg, pv_name))) {
     67 			unlock_vg(cmd, vg_name);
     68 			log_error
     69 			    ("Unable to find \"%s\" in volume group \"%s\"",
     70 			     pv_name, vg->name);
     71 			return 0;
     72 		}
     73 		if (tagarg && !(vg->fid->fmt->features & FMT_TAGS)) {
     74 			unlock_vg(cmd, vg_name);
     75 			log_error("Volume group containing %s does not "
     76 				  "support tags", pv_name);
     77 			return 0;
     78 		}
     79 		if (arg_count(cmd, uuid_ARG) && lvs_in_vg_activated(vg)) {
     80 			unlock_vg(cmd, vg_name);
     81 			log_error("Volume group containing %s has active "
     82 				  "logical volumes", pv_name);
     83 			return 0;
     84 		}
     85 		pv = pvl->pv;
     86 		if (!archive(vg))
     87 			return 0;
     88 	} else {
     89 		if (tagarg) {
     90 			log_error("Can't change tag on Physical Volume %s not "
     91 				  "in volume group", pv_name);
     92 			return 0;
     93 		}
     94 
     95 		vg_name = VG_ORPHANS;
     96 
     97 		if (!lock_vol(cmd, vg_name, LCK_VG_WRITE)) {
     98 			log_error("Can't get lock for orphans");
     99 			return 0;
    100 		}
    101 
    102 		if (!(pv = pv_read(cmd, pv_name, NULL, &sector, 1))) {
    103 			unlock_vg(cmd, vg_name);
    104 			log_error("Unable to read PV \"%s\"", pv_name);
    105 			return 0;
    106 		}
    107 	}
    108 
    109 	if (arg_count(cmd, allocatable_ARG)) {
    110 		if (is_orphan(pv) &&
    111 		    !(pv->fmt->features & FMT_ORPHAN_ALLOCATABLE)) {
    112 			log_error("Allocatability not supported by orphan "
    113 				  "%s format PV %s", pv->fmt->name, pv_name);
    114 			unlock_vg(cmd, vg_name);
    115 			return 0;
    116 		}
    117 
    118 		/* change allocatability for a PV */
    119 		if (allocatable && (pv_status(pv) & ALLOCATABLE_PV)) {
    120 			log_error("Physical volume \"%s\" is already "
    121 				  "allocatable", pv_name);
    122 			unlock_vg(cmd, vg_name);
    123 			return 1;
    124 		}
    125 
    126 		if (!allocatable && !(pv_status(pv) & ALLOCATABLE_PV)) {
    127 			log_error("Physical volume \"%s\" is already "
    128 				  "unallocatable", pv_name);
    129 			unlock_vg(cmd, vg_name);
    130 			return 1;
    131 		}
    132 
    133 		if (allocatable) {
    134 			log_verbose("Setting physical volume \"%s\" "
    135 				    "allocatable", pv_name);
    136 			pv->status |= ALLOCATABLE_PV;
    137 		} else {
    138 			log_verbose("Setting physical volume \"%s\" NOT "
    139 				    "allocatable", pv_name);
    140 			pv->status &= ~ALLOCATABLE_PV;
    141 		}
    142 	} else if (tagarg) {
    143 		/* tag or deltag */
    144 		if ((tagarg == addtag_ARG)) {
    145 			if (!str_list_add(cmd->mem, &pv->tags, tag)) {
    146 				log_error("Failed to add tag %s to physical "
    147 					  "volume %s", tag, pv_name);
    148 				unlock_vg(cmd, vg_name);
    149 				return 0;
    150 			}
    151 		} else {
    152 			if (!str_list_del(&pv->tags, tag)) {
    153 				log_error("Failed to remove tag %s from "
    154 					  "physical volume" "%s", tag, pv_name);
    155 				unlock_vg(cmd, vg_name);
    156 				return 0;
    157 			}
    158 		}
    159 	} else {
    160 		/* --uuid: Change PV ID randomly */
    161 		if (!id_create(&pv->id)) {
    162 			log_error("Failed to generate new random UUID for %s.",
    163 				  pv_name);
    164 			unlock_vg(cmd, vg_name);
    165 			return 0;
    166 		}
    167 		if (!id_write_format(&pv->id, uuid, sizeof(uuid))) {
    168 			unlock_vg(cmd, vg_name);
    169 			return_0;
    170 		}
    171 		log_verbose("Changing uuid of %s to %s.", pv_name, uuid);
    172 		if (!is_orphan(pv)) {
    173 			orig_vg_name = pv_vg_name(pv);
    174 			orig_pe_alloc_count = pv_pe_alloc_count(pv);
    175 
    176 			/* FIXME format1 pv_write doesn't preserve these. */
    177 			orig_pe_size = pv_pe_size(pv);
    178 			orig_pe_start = pv_pe_start(pv);
    179 			orig_pe_count = pv_pe_count(pv);
    180 
    181 			pv->vg_name = pv->fmt->orphan_vg_name;
    182 			pv->pe_alloc_count = 0;
    183 			if (!(pv_write(cmd, pv, NULL, INT64_C(-1)))) {
    184 				log_error("pv_write with new uuid failed "
    185 					  "for %s.", pv_name);
    186 				unlock_vg(cmd, vg_name);
    187 				return 0;
    188 			}
    189 			pv->vg_name = orig_vg_name;
    190 			pv->pe_alloc_count = orig_pe_alloc_count;
    191 
    192 			pv->pe_size = orig_pe_size;
    193 			pv->pe_start = orig_pe_start;
    194 			pv->pe_count = orig_pe_count;
    195 		}
    196 	}
    197 
    198 	log_verbose("Updating physical volume \"%s\"", pv_name);
    199 	if (!is_orphan(pv)) {
    200 		if (!vg_write(vg) || !vg_commit(vg)) {
    201 			unlock_vg(cmd, vg_name);
    202 			log_error("Failed to store physical volume \"%s\" in "
    203 				  "volume group \"%s\"", pv_name, vg->name);
    204 			return 0;
    205 		}
    206 		backup(vg);
    207 	} else if (!(pv_write(cmd, pv, NULL, INT64_C(-1)))) {
    208 		unlock_vg(cmd, vg_name);
    209 		log_error("Failed to store physical volume \"%s\"",
    210 			  pv_name);
    211 		return 0;
    212 	}
    213 
    214 	unlock_vg(cmd, vg_name);
    215 
    216 	log_print("Physical volume \"%s\" changed", pv_name);
    217 
    218 	return 1;
    219 }
    220 
    221 int pvchange(struct cmd_context *cmd, int argc, char **argv)
    222 {
    223 	int opt = 0;
    224 	int done = 0;
    225 	int total = 0;
    226 
    227 	struct physical_volume *pv;
    228 	char *pv_name;
    229 
    230 	struct pv_list *pvl;
    231 	struct dm_list *pvslist;
    232 	struct dm_list mdas;
    233 
    234 	if (arg_count(cmd, allocatable_ARG) + arg_count(cmd, addtag_ARG) +
    235 	    arg_count(cmd, deltag_ARG) + arg_count(cmd, uuid_ARG) != 1) {
    236 		log_error("Please give exactly one option of -x, -uuid, "
    237 			  "--addtag or --deltag");
    238 		return EINVALID_CMD_LINE;
    239 	}
    240 
    241 	if (!(arg_count(cmd, all_ARG)) && !argc) {
    242 		log_error("Please give a physical volume path");
    243 		return EINVALID_CMD_LINE;
    244 	}
    245 
    246 	if (arg_count(cmd, all_ARG) && argc) {
    247 		log_error("Option a and PhysicalVolumePath are exclusive");
    248 		return EINVALID_CMD_LINE;
    249 	}
    250 
    251 	if (argc) {
    252 		log_verbose("Using physical volume(s) on command line");
    253 		for (; opt < argc; opt++) {
    254 			pv_name = argv[opt];
    255 			dm_list_init(&mdas);
    256 			if (!(pv = pv_read(cmd, pv_name, &mdas, NULL, 1))) {
    257 				log_error("Failed to read physical volume %s",
    258 					  pv_name);
    259 				continue;
    260 			}
    261 			/*
    262 			 * If a PV has no MDAs it may appear to be an
    263 			 * orphan until the metadata is read off
    264 			 * another PV in the same VG.  Detecting this
    265 			 * means checking every VG by scanning every
    266 			 * PV on the system.
    267 			 */
    268 			if (is_orphan(pv) && !dm_list_size(&mdas)) {
    269 				if (!scan_vgs_for_pvs(cmd)) {
    270 					log_error("Rescan for PVs without "
    271 						  "metadata areas failed.");
    272 					continue;
    273 				}
    274 				if (!(pv = pv_read(cmd, pv_name,
    275 						   NULL, NULL, 1))) {
    276 					log_error("Failed to read "
    277 						  "physical volume %s",
    278 						  pv_name);
    279 					continue;
    280 				}
    281 			}
    282 
    283 			total++;
    284 			done += _pvchange_single(cmd, pv, NULL);
    285 		}
    286 	} else {
    287 		log_verbose("Scanning for physical volume names");
    288 		if (!(pvslist = get_pvs(cmd))) {
    289 			return ECMD_FAILED;
    290 		}
    291 
    292 		dm_list_iterate_items(pvl, pvslist) {
    293 			total++;
    294 			done += _pvchange_single(cmd, pvl->pv, NULL);
    295 		}
    296 	}
    297 
    298 	log_print("%d physical volume%s changed / %d physical volume%s "
    299 		  "not changed",
    300 		  done, done == 1 ? "" : "s",
    301 		  total - done, (total - done) == 1 ? "" : "s");
    302 
    303 	return (total == done) ? ECMD_PROCESSED : ECMD_FAILED;
    304 }
    305