Home | History | Annotate | Line # | Download | only in tools
vgcreate.c revision 1.1
      1 /*	$NetBSD: vgcreate.c,v 1.1 2008/12/22 00:19:09 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 int vgcreate(struct cmd_context *cmd, int argc, char **argv)
     21 {
     22 	struct vgcreate_params vp_new;
     23 	struct vgcreate_params vp_def;
     24 	struct volume_group *vg;
     25 	const char *tag;
     26 	const char *clustered_message = "";
     27 
     28 	if (!argc) {
     29 		log_error("Please provide volume group name and "
     30 			  "physical volumes");
     31 		return EINVALID_CMD_LINE;
     32 	}
     33 
     34 	if (argc == 1) {
     35 		log_error("Please enter physical volume name(s)");
     36 		return EINVALID_CMD_LINE;
     37 	}
     38 
     39 	vp_def.vg_name = NULL;
     40 	vp_def.extent_size = DEFAULT_EXTENT_SIZE * 2;
     41 	vp_def.max_pv = 0;
     42 	vp_def.max_lv = 0;
     43 	vp_def.alloc = ALLOC_NORMAL;
     44 	vp_def.clustered = 0;
     45 	if (fill_vg_create_params(cmd, argv[0], &vp_new, &vp_def))
     46 		return EINVALID_CMD_LINE;
     47 
     48 	if (validate_vg_create_params(cmd, &vp_new))
     49 	    return EINVALID_CMD_LINE;
     50 
     51 	/* Create the new VG */
     52 	if (!(vg = vg_create(cmd, vp_new.vg_name, vp_new.extent_size,
     53 			     vp_new.max_pv, vp_new.max_lv, vp_new.alloc,
     54 			     argc - 1, argv + 1)))
     55 		return ECMD_FAILED;
     56 
     57 	if (vp_new.max_lv != vg->max_lv)
     58 		log_warn("WARNING: Setting maxlogicalvolumes to %d "
     59 			 "(0 means unlimited)", vg->max_lv);
     60 
     61 	if (vp_new.max_pv != vg->max_pv)
     62 		log_warn("WARNING: Setting maxphysicalvolumes to %d "
     63 			 "(0 means unlimited)", vg->max_pv);
     64 
     65 	if (arg_count(cmd, addtag_ARG)) {
     66 		if (!(tag = arg_str_value(cmd, addtag_ARG, NULL))) {
     67 			log_error("Failed to get tag");
     68 			return ECMD_FAILED;
     69 		}
     70 
     71 		if (!(vg->fid->fmt->features & FMT_TAGS)) {
     72 			log_error("Volume group format does not support tags");
     73 			return ECMD_FAILED;
     74 		}
     75 
     76 		if (!str_list_add(cmd->mem, &vg->tags, tag)) {
     77 			log_error("Failed to add tag %s to volume group %s",
     78 				  tag, vp_new.vg_name);
     79 			return ECMD_FAILED;
     80 		}
     81 	}
     82 
     83 	/* FIXME: move this inside vg_create? */
     84 	if (vp_new.clustered) {
     85 		vg->status |= CLUSTERED;
     86 		clustered_message = "Clustered ";
     87 	} else {
     88 		vg->status &= ~CLUSTERED;
     89 		if (locking_is_clustered())
     90 			clustered_message = "Non-clustered ";
     91 	}
     92 
     93 	if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE)) {
     94 		log_error("Can't get lock for orphan PVs");
     95 		return ECMD_FAILED;
     96 	}
     97 
     98 	if (!lock_vol(cmd, vp_new.vg_name, LCK_VG_WRITE | LCK_NONBLOCK)) {
     99 		log_error("Can't get lock for %s", vp_new.vg_name);
    100 		unlock_vg(cmd, VG_ORPHANS);
    101 		return ECMD_FAILED;
    102 	}
    103 
    104 	if (!archive(vg)) {
    105 		unlock_vg(cmd, vp_new.vg_name);
    106 		unlock_vg(cmd, VG_ORPHANS);
    107 		return ECMD_FAILED;
    108 	}
    109 
    110 	/* Store VG on disk(s) */
    111 	if (!vg_write(vg) || !vg_commit(vg)) {
    112 		unlock_vg(cmd, vp_new.vg_name);
    113 		unlock_vg(cmd, VG_ORPHANS);
    114 		return ECMD_FAILED;
    115 	}
    116 
    117 	unlock_vg(cmd, vp_new.vg_name);
    118 	unlock_vg(cmd, VG_ORPHANS);
    119 
    120 	backup(vg);
    121 
    122 	log_print("%s%colume group \"%s\" successfully created",
    123 		  clustered_message, *clustered_message ? 'v' : 'V', vg->name);
    124 
    125 	return ECMD_PROCESSED;
    126 }
    127