1 /*        $NetBSD: vgcreate.c,v 1.1.1.3 2009/12/02 00:25:57 haad Exp $          */
2 
3 /*
4  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5  * Copyright (C) 2004-2009 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 
vgcreate(struct cmd_context * cmd,int argc,char ** argv)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           char *vg_name;
28           struct pvcreate_params pp;
29 
30           if (!argc) {
31                     log_error("Please provide volume group name and "
32                                 "physical volumes");
33                     return EINVALID_CMD_LINE;
34           }
35 
36           vg_name = argv[0];
37           argc--;
38           argv++;
39 
40           if (arg_count(cmd, metadatacopies_ARG)) {
41                     log_error("Invalid option --metadatacopies, "
42                                 "use --pvmetadatacopies instead.");
43                     return EINVALID_CMD_LINE;
44           }
45           pvcreate_params_set_defaults(&pp);
46           if (!pvcreate_params_validate(cmd, argc, argv, &pp)) {
47                     return EINVALID_CMD_LINE;
48           }
49 
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;
54 
55           if (vgcreate_params_validate(cmd, &vp_new))
56               return EINVALID_CMD_LINE;
57 
58           /* Create the new VG */
59           vg = vg_create(cmd, vp_new.vg_name);
60           if (vg_read_error(vg))
61                     goto_bad;
62 
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))
68                     goto_bad;
69 
70           if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE)) {
71                     log_error("Can't get lock for orphan PVs");
72                     goto bad_orphan;
73           }
74 
75           /* attach the pv's */
76           if (!vg_extend(vg, argc, argv, &pp))
77                     goto_bad;
78 
79           if (vp_new.max_lv != vg->max_lv)
80                     log_warn("WARNING: Setting maxlogicalvolumes to %d "
81                                "(0 means unlimited)", vg->max_lv);
82 
83           if (vp_new.max_pv != vg->max_pv)
84                     log_warn("WARNING: Setting maxphysicalvolumes to %d "
85                                "(0 means unlimited)", vg->max_pv);
86 
87           if (arg_count(cmd, addtag_ARG)) {
88                     if (!(tag = arg_str_value(cmd, addtag_ARG, NULL))) {
89                               log_error("Failed to get tag");
90                               goto bad;
91                     }
92 
93                     if (!(vg->fid->fmt->features & FMT_TAGS)) {
94                               log_error("Volume group format does not support tags");
95                               goto bad;
96                     }
97 
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);
101                               goto bad;
102                     }
103           }
104 
105           if (vg_is_clustered(vg)) {
106                     clustered_message = "Clustered ";
107           } else {
108                     if (locking_is_clustered())
109                               clustered_message = "Non-clustered ";
110           }
111 
112           if (!archive(vg))
113                     goto_bad;
114 
115           /* Store VG on disk(s) */
116           if (!vg_write(vg) || !vg_commit(vg))
117                     goto_bad;
118 
119           unlock_vg(cmd, VG_ORPHANS);
120           unlock_vg(cmd, vp_new.vg_name);
121 
122           backup(vg);
123 
124           log_print("%s%colume group \"%s\" successfully created",
125                       clustered_message, *clustered_message ? 'v' : 'V', vg->name);
126 
127           vg_release(vg);
128           return ECMD_PROCESSED;
129 
130 bad:
131           unlock_vg(cmd, VG_ORPHANS);
132 bad_orphan:
133           vg_release(vg);
134           unlock_vg(cmd, vp_new.vg_name);
135           return ECMD_FAILED;
136 }
137