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 .
2007-08-21 00:55:30 +04:00
* Copyright ( C ) 2004 - 2007 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
2007-08-21 00:55:30 +04:00
* of the GNU Lesser General Public License v .2 .1 .
2001-11-07 11:50:07 +03:00
*
2007-08-21 00:55:30 +04: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"
2002-02-11 23:50:53 +03:00
int vgcreate ( struct cmd_context * cmd , int argc , char * * argv )
2001-10-04 00:38:07 +04:00
{
2008-01-16 01:56:30 +03: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 ;
2008-04-08 18:22:13 +04:00
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 " ) ;
2001-10-05 02:53:37 +04:00
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) " ) ;
2001-10-05 02:53:37 +04:00
return EINVALID_CMD_LINE ;
2001-10-04 00:38:07 +04:00
}
2008-01-16 01:56:30 +03:00
vp_def . vg_name = NULL ;
2008-01-17 18:31:18 +03:00
vp_def . extent_size = DEFAULT_EXTENT_SIZE * 2 ;
2008-01-16 01:56:30 +03:00
vp_def . max_pv = 0 ;
vp_def . max_lv = 0 ;
vp_def . alloc = ALLOC_NORMAL ;
vp_def . clustered = 0 ;
if ( fill_vg_create_params ( cmd , argv [ 0 ] , & vp_new , & vp_def ) )
2004-03-26 18:46:37 +03:00
return EINVALID_CMD_LINE ;
2008-01-16 01:56:30 +03:00
if ( validate_vg_create_params ( cmd , & vp_new ) )
2008-01-11 10:02:35 +03:00
return EINVALID_CMD_LINE ;
2001-10-15 22:39:40 +04:00
2008-12-01 23:14:33 +03:00
if ( ! lock_vol ( cmd , VG_ORPHANS , LCK_VG_WRITE ) ) {
log_error ( " Can't get lock for orphan PVs " ) ;
return ECMD_FAILED ;
}
if ( ! lock_vol ( cmd , vp_new . vg_name , LCK_VG_WRITE | LCK_NONBLOCK ) ) {
log_error ( " Can't get lock for %s " , vp_new . vg_name ) ;
unlock_vg ( cmd , VG_ORPHANS ) ;
return ECMD_FAILED ;
}
2001-11-21 22:32:35 +03:00
/* Create the new VG */
2008-01-16 01:56:30 +03:00
if ( ! ( vg = vg_create ( cmd , vp_new . vg_name , vp_new . extent_size ,
vp_new . max_pv , vp_new . max_lv , vp_new . alloc ,
2002-02-12 00:00:35 +03:00
argc - 1 , argv + 1 ) ) )
2008-12-01 23:14:33 +03:00
goto bad ;
2001-10-04 00:38:07 +04:00
2008-01-16 01:56:30 +03:00
if ( vp_new . max_lv ! = vg - > max_lv )
2007-06-28 21:33:44 +04:00
log_warn ( " WARNING: Setting maxlogicalvolumes to %d "
" (0 means unlimited) " , vg - > max_lv ) ;
2001-10-15 22:39:40 +04:00
2008-01-16 01:56:30 +03:00
if ( vp_new . max_pv ! = vg - > max_pv )
2007-06-28 21:33:44 +04:00
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 " ) ;
2008-12-01 23:14:33 +03:00
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 " ) ;
2008-12-01 23:14:33 +03:00
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 " ,
2008-01-16 01:56:30 +03:00
tag , vp_new . vg_name ) ;
2008-12-01 23:14:33 +03:00
goto bad ;
2004-03-26 18:46:37 +03:00
}
}
2008-01-15 00:07:58 +03:00
/* FIXME: move this inside vg_create? */
2008-04-08 18:22:13 +04:00
if ( vp_new . clustered ) {
2005-03-22 01:55:12 +03:00
vg - > status | = CLUSTERED ;
2008-04-08 18:22:13 +04:00
clustered_message = " Clustered " ;
} else {
2005-03-22 01:55:12 +03:00
vg - > status & = ~ CLUSTERED ;
2008-04-08 18:22:13 +04:00
if ( locking_is_clustered ( ) )
clustered_message = " Non-clustered " ;
}
2005-03-22 01:55:12 +03:00
2002-02-11 18:42:34 +03:00
if ( ! archive ( vg ) ) {
2008-12-01 23:14:33 +03:00
goto bad ;
2002-02-11 18:42:34 +03:00
}
2002-01-09 16:17:14 +03:00
2001-11-21 22:32:35 +03:00
/* Store VG on disk(s) */
2003-07-05 02:34:56 +04:00
if ( ! vg_write ( vg ) | | ! vg_commit ( vg ) ) {
2008-12-01 23:14:33 +03:00
goto bad ;
2002-02-11 18:42:34 +03:00
}
2008-01-16 01:56:30 +03:00
unlock_vg ( cmd , vp_new . vg_name ) ;
2007-11-02 23:40:05 +03:00
unlock_vg ( cmd , VG_ORPHANS ) ;
2001-10-04 00:38:07 +04:00
2002-01-07 14:12:11 +03:00
backup ( vg ) ;
2002-01-01 00:27:39 +03:00
2008-04-08 18:22:13 +04:00
log_print ( " %s%colume group \" %s \" successfully created " ,
clustered_message , * clustered_message ? ' v ' : ' V ' , vg - > name ) ;
2001-10-12 18:25:53 +04:00
2003-10-22 02:06:07 +04:00
return ECMD_PROCESSED ;
2008-12-01 23:14:33 +03:00
bad :
unlock_vg ( cmd , vp_new . vg_name ) ;
unlock_vg ( cmd , VG_ORPHANS ) ;
return ECMD_FAILED ;
2001-10-04 00:38:07 +04:00
}