vgcreate.c revision 1.1.1.2 1 /* $NetBSD: vgcreate.c,v 1.1.1.2 2009/02/18 11:17:50 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 if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE)) {
52 log_error("Can't get lock for orphan PVs");
53 return ECMD_FAILED;
54 }
55
56 if (!lock_vol(cmd, vp_new.vg_name, LCK_VG_WRITE | LCK_NONBLOCK)) {
57 log_error("Can't get lock for %s", vp_new.vg_name);
58 unlock_vg(cmd, VG_ORPHANS);
59 return ECMD_FAILED;
60 }
61
62 /* Create the new VG */
63 if (!(vg = vg_create(cmd, vp_new.vg_name, vp_new.extent_size,
64 vp_new.max_pv, vp_new.max_lv, vp_new.alloc,
65 argc - 1, argv + 1)))
66 goto bad;
67
68 if (vp_new.max_lv != vg->max_lv)
69 log_warn("WARNING: Setting maxlogicalvolumes to %d "
70 "(0 means unlimited)", vg->max_lv);
71
72 if (vp_new.max_pv != vg->max_pv)
73 log_warn("WARNING: Setting maxphysicalvolumes to %d "
74 "(0 means unlimited)", vg->max_pv);
75
76 if (arg_count(cmd, addtag_ARG)) {
77 if (!(tag = arg_str_value(cmd, addtag_ARG, NULL))) {
78 log_error("Failed to get tag");
79 goto bad;
80 }
81
82 if (!(vg->fid->fmt->features & FMT_TAGS)) {
83 log_error("Volume group format does not support tags");
84 goto bad;
85 }
86
87 if (!str_list_add(cmd->mem, &vg->tags, tag)) {
88 log_error("Failed to add tag %s to volume group %s",
89 tag, vp_new.vg_name);
90 goto bad;
91 }
92 }
93
94 /* FIXME: move this inside vg_create? */
95 if (vp_new.clustered) {
96 vg->status |= CLUSTERED;
97 clustered_message = "Clustered ";
98 } else {
99 vg->status &= ~CLUSTERED;
100 if (locking_is_clustered())
101 clustered_message = "Non-clustered ";
102 }
103
104 if (!archive(vg)) {
105 goto bad;
106 }
107
108 /* Store VG on disk(s) */
109 if (!vg_write(vg) || !vg_commit(vg)) {
110 goto bad;
111 }
112
113 unlock_vg(cmd, vp_new.vg_name);
114 unlock_vg(cmd, VG_ORPHANS);
115
116 backup(vg);
117
118 log_print("%s%colume group \"%s\" successfully created",
119 clustered_message, *clustered_message ? 'v' : 'V', vg->name);
120
121 return ECMD_PROCESSED;
122
123 bad:
124 unlock_vg(cmd, vp_new.vg_name);
125 unlock_vg(cmd, VG_ORPHANS);
126 return ECMD_FAILED;
127 }
128