4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
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.
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
20 int vgcreate(struct cmd_context
*cmd
, int argc
, char **argv
)
22 struct vgcreate_params vp_new
;
23 struct vgcreate_params vp_def
;
24 struct volume_group
*vg
;
26 const char *clustered_message
= "";
28 struct pvcreate_params pp
;
31 log_error("Please provide volume group name and "
33 return EINVALID_CMD_LINE
;
40 if (arg_count(cmd
, metadatacopies_ARG
)) {
41 log_error("Invalid option --metadatacopies, "
42 "use --pvmetadatacopies instead.");
43 return EINVALID_CMD_LINE
;
45 pvcreate_params_set_defaults(&pp
);
46 if (!pvcreate_params_validate(cmd
, argc
, argv
, &pp
)) {
47 return EINVALID_CMD_LINE
;
50 vgcreate_params_set_defaults(&vp_def
, NULL
);
51 vp_def
.vg_name
= vg_name
;
52 if (vgcreate_params_set_from_args(cmd
, &vp_new
, &vp_def
))
53 return EINVALID_CMD_LINE
;
55 if (vgcreate_params_validate(cmd
, &vp_new
))
56 return EINVALID_CMD_LINE
;
58 /* Create the new VG */
59 vg
= vg_create(cmd
, vp_new
.vg_name
);
60 if (vg_read_error(vg
))
63 if (!vg_set_extent_size(vg
, vp_new
.extent_size
) ||
64 !vg_set_max_lv(vg
, vp_new
.max_lv
) ||
65 !vg_set_max_pv(vg
, vp_new
.max_pv
) ||
66 !vg_set_alloc_policy(vg
, vp_new
.alloc
) ||
67 !vg_set_clustered(vg
, vp_new
.clustered
))
70 if (!lock_vol(cmd
, VG_ORPHANS
, LCK_VG_WRITE
)) {
71 log_error("Can't get lock for orphan PVs");
76 if (!vg_extend(vg
, argc
, argv
, &pp
))
79 if (vp_new
.max_lv
!= vg
->max_lv
)
80 log_warn("WARNING: Setting maxlogicalvolumes to %d "
81 "(0 means unlimited)", vg
->max_lv
);
83 if (vp_new
.max_pv
!= vg
->max_pv
)
84 log_warn("WARNING: Setting maxphysicalvolumes to %d "
85 "(0 means unlimited)", vg
->max_pv
);
87 if (arg_count(cmd
, addtag_ARG
)) {
88 if (!(tag
= arg_str_value(cmd
, addtag_ARG
, NULL
))) {
89 log_error("Failed to get tag");
93 if (!(vg
->fid
->fmt
->features
& FMT_TAGS
)) {
94 log_error("Volume group format does not support tags");
98 if (!str_list_add(cmd
->mem
, &vg
->tags
, tag
)) {
99 log_error("Failed to add tag %s to volume group %s",
100 tag
, vp_new
.vg_name
);
105 if (vg_is_clustered(vg
)) {
106 clustered_message
= "Clustered ";
108 if (locking_is_clustered())
109 clustered_message
= "Non-clustered ";
115 /* Store VG on disk(s) */
116 if (!vg_write(vg
) || !vg_commit(vg
))
119 unlock_vg(cmd
, VG_ORPHANS
);
120 unlock_vg(cmd
, vp_new
.vg_name
);
124 log_print("%s%colume group \"%s\" successfully created",
125 clustered_message
, *clustered_message
? 'v' : 'V', vg
->name
);
128 return ECMD_PROCESSED
;
131 unlock_vg(cmd
, VG_ORPHANS
);
134 unlock_vg(cmd
, vp_new
.vg_name
);