1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-22 17:35:59 +03:00
lvm2/tools/vgcreate.c

132 lines
3.2 KiB
C
Raw Normal View History

2001-10-04 00:38:07 +04:00
/*
2004-03-30 23:35:44 +04:00
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
* Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
2001-11-07 11:50:07 +03:00
*
2004-03-30 23:35:44 +04:00
* This file is part of LVM2.
2001-11-07 11:50:07 +03:00
*
2004-03-30 23:35:44 +04:00
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v.2.1.
2001-11-07 11:50:07 +03:00
*
* You should have received a copy of the GNU Lesser General Public License
2004-03-30 23:35:44 +04:00
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2001-10-04 00:38:07 +04:00
*/
#include "tools.h"
int vgcreate(struct cmd_context *cmd, int argc, char **argv)
2001-10-04 00:38:07 +04:00
{
struct vgcreate_params vp_new;
struct vgcreate_params vp_def;
2001-10-04 00:38:07 +04:00
struct volume_group *vg;
2004-03-08 20:19:15 +03:00
const char *tag;
const char *clustered_message = "";
2001-10-04 00:38:07 +04:00
2001-10-12 16:21:43 +04:00
if (!argc) {
log_error("Please provide volume group name and "
"physical volumes");
return EINVALID_CMD_LINE;
2001-10-04 00:38:07 +04:00
}
if (argc == 1) {
2001-10-12 16:21:43 +04:00
log_error("Please enter physical volume name(s)");
return EINVALID_CMD_LINE;
2001-10-04 00:38:07 +04:00
}
vp_def.vg_name = NULL;
2008-01-17 18:31:18 +03:00
vp_def.extent_size = DEFAULT_EXTENT_SIZE * 2;
vp_def.max_pv = DEFAULT_MAX_PV;
vp_def.max_lv = DEFAULT_MAX_LV;
vp_def.alloc = DEFAULT_ALLOC_POLICY;
vp_def.clustered = DEFAULT_CLUSTERED;
if (fill_vg_create_params(cmd, argv[0], &vp_new, &vp_def))
2004-03-26 18:46:37 +03:00
return EINVALID_CMD_LINE;
if (validate_vg_create_params(cmd, &vp_new))
return EINVALID_CMD_LINE;
2001-10-15 22:39:40 +04:00
/* Create the new VG */
Change vg_create() to take only minimal parameters and obtain a lock. vg_t *vg_create(struct cmd_context *cmd, const char *vg_name); This is the first step towards the API called to create a VG. Call vg_lock_newname() inside this function. Use _vg_make_handle() where possible. Now we have 2 ways to construct a volume group: 1) vg_read: Used when constructing an existing VG from disks 2) vg_create: Used when constructing a new VG Both of these interfaces obtain a lock, and return a vg_t *. The usage of _vg_make_handle() inside vg_create() doesn't fit perfectly but it's ok for now. Needs some cleanup though and I've noted "FIXME" in the code. Add the new vg_create() plus vg 'set' functions for non-default VG parameters in the following tools: - vgcreate: Fairly straightforward refactoring. We just moved vg_lock_newname inside vg_create so we check the return via vg_read_error. - vgsplit: The refactoring here is a bit more tricky. Originally we called vg_lock_newname and depending on the error code, we either read the existing vg or created the new one. Now vg_create() calls vg_lock_newname, so we first try to create the VG. If this fails with FAILED_EXIST, we can then do the vg_read. If the create succeeds, we check the input parameters and set any new values on the VG. TODO in future patches: 1. The VG_ORPHAN lock needs some thought. We may want to treat this as any other VG, and require the application to obtain a handle and pass it to other API calls (for example, vg_extend). Or, we may find that hiding the VG_ORPHAN lock inside other APIs is the way to go. I thought of placing the VG_ORPHAN lock inside vg_create() and tying it to the vg handle, but was not certain this was the right approach. 2. Cleanup error paths. Integrate vg_read_error() with vg_create and vg_read* error codes and/or the new error APIs. Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
2009-07-09 14:09:33 +04:00
vg = vg_create(cmd, vp_new.vg_name);
if (vg_read_error(vg))
goto_bad;
if (!vg_set_extent_size(vg, vp_new.extent_size) ||
!vg_set_max_lv(vg, vp_new.max_lv) ||
!vg_set_max_pv(vg, vp_new.max_pv) ||
!vg_set_alloc_policy(vg, vp_new.alloc))
goto_bad;
if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE)) {
log_error("Can't get lock for orphan PVs");
goto bad_orphan;
}
Change vg_create() to take only minimal parameters and obtain a lock. vg_t *vg_create(struct cmd_context *cmd, const char *vg_name); This is the first step towards the API called to create a VG. Call vg_lock_newname() inside this function. Use _vg_make_handle() where possible. Now we have 2 ways to construct a volume group: 1) vg_read: Used when constructing an existing VG from disks 2) vg_create: Used when constructing a new VG Both of these interfaces obtain a lock, and return a vg_t *. The usage of _vg_make_handle() inside vg_create() doesn't fit perfectly but it's ok for now. Needs some cleanup though and I've noted "FIXME" in the code. Add the new vg_create() plus vg 'set' functions for non-default VG parameters in the following tools: - vgcreate: Fairly straightforward refactoring. We just moved vg_lock_newname inside vg_create so we check the return via vg_read_error. - vgsplit: The refactoring here is a bit more tricky. Originally we called vg_lock_newname and depending on the error code, we either read the existing vg or created the new one. Now vg_create() calls vg_lock_newname, so we first try to create the VG. If this fails with FAILED_EXIST, we can then do the vg_read. If the create succeeds, we check the input parameters and set any new values on the VG. TODO in future patches: 1. The VG_ORPHAN lock needs some thought. We may want to treat this as any other VG, and require the application to obtain a handle and pass it to other API calls (for example, vg_extend). Or, we may find that hiding the VG_ORPHAN lock inside other APIs is the way to go. I thought of placing the VG_ORPHAN lock inside vg_create() and tying it to the vg handle, but was not certain this was the right approach. 2. Cleanup error paths. Integrate vg_read_error() with vg_create and vg_read* error codes and/or the new error APIs. Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
2009-07-09 14:09:33 +04:00
/* attach the pv's */
if (!vg_extend(vg, argc - 1, argv + 1))
goto_bad;
2001-10-04 00:38:07 +04:00
if (vp_new.max_lv != vg->max_lv)
log_warn("WARNING: Setting maxlogicalvolumes to %d "
"(0 means unlimited)", vg->max_lv);
2001-10-15 22:39:40 +04:00
if (vp_new.max_pv != vg->max_pv)
log_warn("WARNING: Setting maxphysicalvolumes to %d "
"(0 means unlimited)", vg->max_pv);
2001-10-15 22:39:40 +04:00
2004-03-26 18:46:37 +03:00
if (arg_count(cmd, addtag_ARG)) {
if (!(tag = arg_str_value(cmd, addtag_ARG, NULL))) {
log_error("Failed to get tag");
goto bad;
2004-03-26 18:46:37 +03:00
}
if (!(vg->fid->fmt->features & FMT_TAGS)) {
log_error("Volume group format does not support tags");
goto bad;
2004-03-26 18:46:37 +03:00
}
if (!str_list_add(cmd->mem, &vg->tags, tag)) {
log_error("Failed to add tag %s to volume group %s",
tag, vp_new.vg_name);
goto bad;
2004-03-26 18:46:37 +03:00
}
}
/* FIXME: move this inside vg_create? */
if (vp_new.clustered) {
vg->status |= CLUSTERED;
clustered_message = "Clustered ";
} else {
vg->status &= ~CLUSTERED;
if (locking_is_clustered())
clustered_message = "Non-clustered ";
}
if (!archive(vg)) {
goto bad;
}
2002-01-09 16:17:14 +03:00
/* Store VG on disk(s) */
if (!vg_write(vg) || !vg_commit(vg)) {
goto bad;
}
unlock_vg(cmd, VG_ORPHANS);
unlock_vg(cmd, vp_new.vg_name);
2001-10-04 00:38:07 +04:00
2002-01-07 14:12:11 +03:00
backup(vg);
log_print("%s%colume group \"%s\" successfully created",
clustered_message, *clustered_message ? 'v' : 'V', vg->name);
2001-10-12 18:25:53 +04:00
vg_release(vg);
2003-10-22 02:06:07 +04:00
return ECMD_PROCESSED;
bad:
unlock_vg(cmd, VG_ORPHANS);
bad_orphan:
vg_release(vg);
unlock_vg(cmd, vp_new.vg_name);
return ECMD_FAILED;
2001-10-04 00:38:07 +04:00
}