xref: /netbsd-src/external/gpl2/lvm2/dist/tools/vgcreate.c (revision 7c604eea85b4f330dc75ffe65e947f4d73758aa0)
1*7c604eeaShaad /*	$NetBSD: vgcreate.c,v 1.1.1.3 2009/12/02 00:25:57 haad Exp $	*/
256a34939Shaad 
356a34939Shaad /*
456a34939Shaad  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5*7c604eeaShaad  * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
656a34939Shaad  *
756a34939Shaad  * This file is part of LVM2.
856a34939Shaad  *
956a34939Shaad  * This copyrighted material is made available to anyone wishing to use,
1056a34939Shaad  * modify, copy, or redistribute it subject to the terms and conditions
1156a34939Shaad  * of the GNU Lesser General Public License v.2.1.
1256a34939Shaad  *
1356a34939Shaad  * You should have received a copy of the GNU Lesser General Public License
1456a34939Shaad  * along with this program; if not, write to the Free Software Foundation,
1556a34939Shaad  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1656a34939Shaad  */
1756a34939Shaad 
1856a34939Shaad #include "tools.h"
1956a34939Shaad 
vgcreate(struct cmd_context * cmd,int argc,char ** argv)2056a34939Shaad int vgcreate(struct cmd_context *cmd, int argc, char **argv)
2156a34939Shaad {
2256a34939Shaad 	struct vgcreate_params vp_new;
2356a34939Shaad 	struct vgcreate_params vp_def;
2456a34939Shaad 	struct volume_group *vg;
2556a34939Shaad 	const char *tag;
2656a34939Shaad 	const char *clustered_message = "";
27*7c604eeaShaad 	char *vg_name;
28*7c604eeaShaad 	struct pvcreate_params pp;
2956a34939Shaad 
3056a34939Shaad 	if (!argc) {
3156a34939Shaad 		log_error("Please provide volume group name and "
3256a34939Shaad 			  "physical volumes");
3356a34939Shaad 		return EINVALID_CMD_LINE;
3456a34939Shaad 	}
3556a34939Shaad 
36*7c604eeaShaad 	vg_name = argv[0];
37*7c604eeaShaad 	argc--;
38*7c604eeaShaad 	argv++;
39*7c604eeaShaad 
40*7c604eeaShaad 	if (arg_count(cmd, metadatacopies_ARG)) {
41*7c604eeaShaad 		log_error("Invalid option --metadatacopies, "
42*7c604eeaShaad 			  "use --pvmetadatacopies instead.");
43*7c604eeaShaad 		return EINVALID_CMD_LINE;
44*7c604eeaShaad 	}
45*7c604eeaShaad 	pvcreate_params_set_defaults(&pp);
46*7c604eeaShaad 	if (!pvcreate_params_validate(cmd, argc, argv, &pp)) {
4756a34939Shaad 		return EINVALID_CMD_LINE;
4856a34939Shaad 	}
4956a34939Shaad 
50*7c604eeaShaad 	vgcreate_params_set_defaults(&vp_def, NULL);
51*7c604eeaShaad 	vp_def.vg_name = vg_name;
52*7c604eeaShaad 	if (vgcreate_params_set_from_args(cmd, &vp_new, &vp_def))
5356a34939Shaad 		return EINVALID_CMD_LINE;
5456a34939Shaad 
55*7c604eeaShaad 	if (vgcreate_params_validate(cmd, &vp_new))
5656a34939Shaad 	    return EINVALID_CMD_LINE;
5756a34939Shaad 
58*7c604eeaShaad 	/* Create the new VG */
59*7c604eeaShaad 	vg = vg_create(cmd, vp_new.vg_name);
60*7c604eeaShaad 	if (vg_read_error(vg))
61*7c604eeaShaad 		goto_bad;
62*7c604eeaShaad 
63*7c604eeaShaad 	if (!vg_set_extent_size(vg, vp_new.extent_size) ||
64*7c604eeaShaad 	    !vg_set_max_lv(vg, vp_new.max_lv) ||
65*7c604eeaShaad 	    !vg_set_max_pv(vg, vp_new.max_pv) ||
66*7c604eeaShaad 	    !vg_set_alloc_policy(vg, vp_new.alloc) ||
67*7c604eeaShaad 	    !vg_set_clustered(vg, vp_new.clustered))
68*7c604eeaShaad 		goto_bad;
69*7c604eeaShaad 
70bec4d750Shaad 	if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE)) {
71bec4d750Shaad 		log_error("Can't get lock for orphan PVs");
72*7c604eeaShaad 		goto bad_orphan;
73bec4d750Shaad 	}
74bec4d750Shaad 
75*7c604eeaShaad 	/* attach the pv's */
76*7c604eeaShaad 	if (!vg_extend(vg, argc, argv, &pp))
77*7c604eeaShaad 		goto_bad;
7856a34939Shaad 
7956a34939Shaad 	if (vp_new.max_lv != vg->max_lv)
8056a34939Shaad 		log_warn("WARNING: Setting maxlogicalvolumes to %d "
8156a34939Shaad 			 "(0 means unlimited)", vg->max_lv);
8256a34939Shaad 
8356a34939Shaad 	if (vp_new.max_pv != vg->max_pv)
8456a34939Shaad 		log_warn("WARNING: Setting maxphysicalvolumes to %d "
8556a34939Shaad 			 "(0 means unlimited)", vg->max_pv);
8656a34939Shaad 
8756a34939Shaad 	if (arg_count(cmd, addtag_ARG)) {
8856a34939Shaad 		if (!(tag = arg_str_value(cmd, addtag_ARG, NULL))) {
8956a34939Shaad 			log_error("Failed to get tag");
90bec4d750Shaad 			goto bad;
9156a34939Shaad 		}
9256a34939Shaad 
9356a34939Shaad 		if (!(vg->fid->fmt->features & FMT_TAGS)) {
9456a34939Shaad 			log_error("Volume group format does not support tags");
95bec4d750Shaad 			goto bad;
9656a34939Shaad 		}
9756a34939Shaad 
9856a34939Shaad 		if (!str_list_add(cmd->mem, &vg->tags, tag)) {
9956a34939Shaad 			log_error("Failed to add tag %s to volume group %s",
10056a34939Shaad 				  tag, vp_new.vg_name);
101bec4d750Shaad 			goto bad;
10256a34939Shaad 		}
10356a34939Shaad 	}
10456a34939Shaad 
105*7c604eeaShaad 	if (vg_is_clustered(vg)) {
10656a34939Shaad 		clustered_message = "Clustered ";
10756a34939Shaad 	} else {
10856a34939Shaad 		if (locking_is_clustered())
10956a34939Shaad 			clustered_message = "Non-clustered ";
11056a34939Shaad 	}
11156a34939Shaad 
112*7c604eeaShaad 	if (!archive(vg))
113*7c604eeaShaad 		goto_bad;
11456a34939Shaad 
11556a34939Shaad 	/* Store VG on disk(s) */
116*7c604eeaShaad 	if (!vg_write(vg) || !vg_commit(vg))
117*7c604eeaShaad 		goto_bad;
11856a34939Shaad 
11956a34939Shaad 	unlock_vg(cmd, VG_ORPHANS);
120*7c604eeaShaad 	unlock_vg(cmd, vp_new.vg_name);
12156a34939Shaad 
12256a34939Shaad 	backup(vg);
12356a34939Shaad 
12456a34939Shaad 	log_print("%s%colume group \"%s\" successfully created",
12556a34939Shaad 		  clustered_message, *clustered_message ? 'v' : 'V', vg->name);
12656a34939Shaad 
127*7c604eeaShaad 	vg_release(vg);
12856a34939Shaad 	return ECMD_PROCESSED;
129bec4d750Shaad 
130bec4d750Shaad bad:
131bec4d750Shaad 	unlock_vg(cmd, VG_ORPHANS);
132*7c604eeaShaad bad_orphan:
133*7c604eeaShaad 	vg_release(vg);
134*7c604eeaShaad 	unlock_vg(cmd, vp_new.vg_name);
135bec4d750Shaad 	return ECMD_FAILED;
13656a34939Shaad }
137