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 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 General Public License v .2 .
2001-11-07 11:50:07 +03:00
*
* You should have received a copy of the GNU 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-12 00:00:35 +03:00
# define DEFAULT_EXTENT 4096 /* In KB */
2001-10-12 16:21:43 +04:00
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
{
2002-12-20 02:25:55 +03:00
size_t max_lv , max_pv ;
2001-11-06 22:02:26 +03:00
uint32_t extent_size ;
2001-10-15 16:49:58 +04:00
char * vg_name ;
2001-10-04 00:38:07 +04:00
struct volume_group * vg ;
2004-03-08 20:19:15 +03:00
const char * tag ;
2004-05-19 02:12:53 +04:00
alloc_policy_t alloc ;
2005-03-22 01:55:12 +03:00
int clustered ;
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
}
2007-03-09 23:47:41 +03:00
vg_name = skip_dev_dir ( cmd , argv [ 0 ] , NULL ) ;
2003-11-06 23:33:34 +03:00
max_lv = arg_uint_value ( cmd , maxlogicalvolumes_ARG , 0 ) ;
max_pv = arg_uint_value ( cmd , maxphysicalvolumes_ARG , 0 ) ;
2006-05-10 01:23:51 +04:00
alloc = arg_uint_value ( cmd , alloc_ARG , ALLOC_NORMAL ) ;
2004-05-19 02:12:53 +04:00
if ( alloc = = ALLOC_INHERIT ) {
log_error ( " Volume Group allocation policy cannot inherit "
" from anything " ) ;
return EINVALID_CMD_LINE ;
}
2003-11-06 23:33:34 +03:00
2004-03-26 18:46:37 +03:00
if ( ! ( cmd - > fmt - > features & FMT_UNLIMITED_VOLS ) ) {
if ( ! max_lv )
max_lv = 255 ;
if ( ! max_pv )
max_pv = 255 ;
if ( max_lv > 255 | | max_pv > 255 ) {
log_error ( " Number of volumes may not exceed 255 " ) ;
return EINVALID_CMD_LINE ;
}
}
if ( arg_sign_value ( cmd , physicalextentsize_ARG , 0 ) = = SIGN_MINUS ) {
log_error ( " Physical extent size may not be negative " ) ;
return EINVALID_CMD_LINE ;
}
if ( arg_sign_value ( cmd , maxlogicalvolumes_ARG , 0 ) = = SIGN_MINUS ) {
log_error ( " Max Logical Volumes may not be negative " ) ;
return EINVALID_CMD_LINE ;
}
if ( arg_sign_value ( cmd , maxphysicalvolumes_ARG , 0 ) = = SIGN_MINUS ) {
log_error ( " Max Physical Volumes may not be negative " ) ;
return EINVALID_CMD_LINE ;
}
/* Units of 512-byte sectors */
extent_size =
arg_uint_value ( cmd , physicalextentsize_ARG , DEFAULT_EXTENT ) * 2 ;
2002-01-10 17:27:47 +03:00
2003-11-06 20:07:19 +03:00
if ( ! extent_size ) {
log_error ( " Physical extent size may not be zero " ) ;
return EINVALID_CMD_LINE ;
}
2005-06-06 22:16:33 +04:00
if ( ! validate_vg_name ( cmd , vg_name ) ) {
2003-09-15 19:01:00 +04:00
log_error ( " New volume group name \" %s \" is invalid " , vg_name ) ;
2002-02-12 00:00:35 +03:00
return ECMD_FAILED ;
}
2001-10-15 22:39:40 +04:00
2001-11-21 22:32:35 +03:00
/* Create the new VG */
2004-05-19 02:12:53 +04:00
if ( ! ( vg = vg_create ( cmd , vg_name , extent_size , max_pv , max_lv , alloc ,
2002-02-12 00:00:35 +03:00
argc - 1 , argv + 1 ) ) )
2001-10-05 02:53:37 +04:00
return ECMD_FAILED ;
2001-10-04 00:38:07 +04:00
2002-01-10 17:27:47 +03:00
if ( 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
2002-01-10 17:27:47 +03:00
if ( 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 " ) ;
return ECMD_FAILED ;
}
if ( ! ( vg - > fid - > fmt - > features & FMT_TAGS ) ) {
log_error ( " Volume group format does not support tags " ) ;
return ECMD_FAILED ;
}
if ( ! str_list_add ( cmd - > mem , & vg - > tags , tag ) ) {
log_error ( " Failed to add tag %s to volume group %s " ,
tag , vg_name ) ;
return ECMD_FAILED ;
}
}
2005-03-22 01:55:12 +03:00
if ( arg_count ( cmd , clustered_ARG ) )
clustered = ! strcmp ( arg_str_value ( cmd , clustered_ARG , " n " ) , " y " ) ;
else
/* Default depends on current locking type */
clustered = locking_is_clustered ( ) ;
if ( clustered )
vg - > status | = CLUSTERED ;
else
vg - > status & = ~ CLUSTERED ;
2005-10-03 22:16:44 +04:00
if ( ! lock_vol ( cmd , ORPHAN , LCK_VG_WRITE ) ) {
2002-02-12 00:00:35 +03:00
log_error ( " Can't get lock for orphan PVs " ) ;
return ECMD_FAILED ;
}
2002-02-11 18:42:34 +03:00
2002-02-27 15:26:41 +03:00
if ( ! lock_vol ( cmd , vg_name , LCK_VG_WRITE | LCK_NONBLOCK ) ) {
2002-02-12 00:00:35 +03:00
log_error ( " Can't get lock for %s " , vg_name ) ;
2005-10-03 22:16:44 +04:00
unlock_vg ( cmd , ORPHAN ) ;
2002-02-12 00:00:35 +03:00
return ECMD_FAILED ;
}
2002-02-11 18:42:34 +03:00
if ( ! archive ( vg ) ) {
2002-03-11 18:08:39 +03:00
unlock_vg ( cmd , vg_name ) ;
2005-10-03 22:16:44 +04:00
unlock_vg ( cmd , ORPHAN ) ;
2002-01-09 16:17:14 +03:00
return ECMD_FAILED ;
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 ) ) {
2002-03-11 18:08:39 +03:00
unlock_vg ( cmd , vg_name ) ;
2005-10-03 22:16:44 +04:00
unlock_vg ( cmd , ORPHAN ) ;
2001-10-05 02:53:37 +04:00
return ECMD_FAILED ;
2002-02-11 18:42:34 +03:00
}
2002-03-11 18:08:39 +03:00
unlock_vg ( cmd , vg_name ) ;
2005-10-03 22:16:44 +04:00
unlock_vg ( cmd , ORPHAN ) ;
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
2002-01-30 18:04:48 +03:00
log_print ( " Volume group \" %s \" successfully created " , vg - > name ) ;
2001-10-12 18:25:53 +04:00
2003-10-22 02:06:07 +04:00
return ECMD_PROCESSED ;
2001-10-04 00:38:07 +04:00
}