2001-10-03 20:38:07 +00:00
/*
2001-11-07 08:50:07 +00:00
* Copyright ( C ) 2001 Sistina Software
*
* vgcreate is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 , or ( at your option )
* any later version .
*
* vgcreate is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with LVM ; see the file COPYING . If not , write to
* the Free Software Foundation , 59 Temple Place - Suite 330 ,
* Boston , MA 02111 - 1307 , USA .
2001-10-03 20:38:07 +00:00
*
*/
# include "tools.h"
2001-10-15 18:39:40 +00:00
/* FIXME From config file? */
2002-04-25 10:53:58 +00:00
# define DEFAULT_PV 256
# define DEFAULT_LV 256
2001-10-15 20:29:15 +00:00
2002-02-11 21:00:35 +00:00
# define DEFAULT_EXTENT 4096 /* In KB */
2001-10-12 12:21:43 +00:00
2002-02-11 20:50:53 +00:00
int vgcreate ( struct cmd_context * cmd , int argc , char * * argv )
2001-10-03 20:38:07 +00:00
{
2002-12-19 23:25:55 +00:00
size_t max_lv , max_pv ;
2001-11-06 19:02:26 +00:00
uint32_t extent_size ;
2001-10-15 12:49:58 +00:00
char * vg_name ;
2002-01-28 16:30:42 +00:00
char vg_path [ PATH_MAX ] ;
2001-10-03 20:38:07 +00:00
struct volume_group * vg ;
2001-10-12 12:21:43 +00:00
if ( ! argc ) {
log_error ( " Please provide volume group name and "
" physical volumes " ) ;
2001-10-04 22:53:37 +00:00
return EINVALID_CMD_LINE ;
2001-10-03 20:38:07 +00:00
}
if ( argc = = 1 ) {
2001-10-12 12:21:43 +00:00
log_error ( " Please enter physical volume name(s) " ) ;
2001-10-04 22:53:37 +00:00
return EINVALID_CMD_LINE ;
2001-10-03 20:38:07 +00:00
}
2001-10-15 12:49:58 +00:00
vg_name = argv [ 0 ] ;
2002-12-19 23:25:55 +00:00
max_lv = arg_uint_value ( cmd , maxlogicalvolumes_ARG , DEFAULT_LV ) ;
max_pv = arg_uint_value ( cmd , maxphysicalvolumes_ARG , DEFAULT_PV ) ;
2001-10-15 20:29:15 +00:00
/* Units of 512-byte sectors */
2002-02-11 21:00:35 +00:00
extent_size =
2002-12-19 23:25:55 +00:00
arg_uint_value ( cmd , physicalextentsize_ARG , DEFAULT_EXTENT ) * 2 ;
2001-10-12 12:21:43 +00:00
2001-10-15 18:39:40 +00:00
if ( max_lv < 1 ) {
log_error ( " maxlogicalvolumes too low " ) ;
return EINVALID_CMD_LINE ;
}
2002-01-10 14:27:47 +00:00
2001-10-15 18:39:40 +00:00
if ( max_pv < 1 ) {
log_error ( " maxphysicalvolumes too low " ) ;
return EINVALID_CMD_LINE ;
}
2002-01-10 14:27:47 +00:00
2002-02-11 21:00:35 +00:00
/* Strip dev_dir if present */
if ( ! strncmp ( vg_name , cmd - > dev_dir , strlen ( cmd - > dev_dir ) ) )
vg_name + = strlen ( cmd - > dev_dir ) ;
2001-10-15 18:39:40 +00:00
2002-02-11 21:00:35 +00:00
snprintf ( vg_path , PATH_MAX , " %s%s " , cmd - > dev_dir , vg_name ) ;
if ( path_exists ( vg_path ) ) {
2002-02-11 15:42:34 +00:00
log_error ( " %s: already exists in filesystem " , vg_path ) ;
return ECMD_FAILED ;
}
2002-01-28 16:30:42 +00:00
2003-02-03 20:09:58 +00:00
if ( ! validate_name ( vg_name ) ) {
2002-02-11 21:00:35 +00:00
log_error ( " New volume group name \" %s \" has invalid characters " ,
vg_name ) ;
return ECMD_FAILED ;
}
2001-10-15 18:39:40 +00:00
2001-11-21 19:32:35 +00:00
/* Create the new VG */
2002-04-24 18:20:51 +00:00
if ( ! ( vg = vg_create ( cmd , vg_name , extent_size , max_pv , max_lv ,
2002-02-11 21:00:35 +00:00
argc - 1 , argv + 1 ) ) )
2001-10-04 22:53:37 +00:00
return ECMD_FAILED ;
2001-10-03 20:38:07 +00:00
2002-01-10 14:27:47 +00:00
if ( max_lv ! = vg - > max_lv )
log_error ( " Warning: Setting maxlogicalvolumes to %d " ,
2001-10-15 18:39:40 +00:00
vg - > max_lv ) ;
2002-01-10 14:27:47 +00:00
if ( max_pv ! = vg - > max_pv )
log_error ( " Warning: Setting maxphysicalvolumes to %d " ,
2001-10-15 18:39:40 +00:00
vg - > max_pv ) ;
2002-02-27 12:26:41 +00:00
if ( ! lock_vol ( cmd , " " , LCK_VG_WRITE ) ) {
2002-02-11 21:00:35 +00:00
log_error ( " Can't get lock for orphan PVs " ) ;
return ECMD_FAILED ;
}
2002-02-11 15:42:34 +00:00
2002-02-27 12:26:41 +00:00
if ( ! lock_vol ( cmd , vg_name , LCK_VG_WRITE | LCK_NONBLOCK ) ) {
2002-02-11 21:00:35 +00:00
log_error ( " Can't get lock for %s " , vg_name ) ;
2002-03-11 15:08:39 +00:00
unlock_vg ( cmd , " " ) ;
2002-02-11 21:00:35 +00:00
return ECMD_FAILED ;
}
2002-02-11 15:42:34 +00:00
if ( ! archive ( vg ) ) {
2002-03-11 15:08:39 +00:00
unlock_vg ( cmd , vg_name ) ;
unlock_vg ( cmd , " " ) ;
2002-01-09 13:17:14 +00:00
return ECMD_FAILED ;
2002-02-11 15:42:34 +00:00
}
2002-01-09 13:17:14 +00:00
2001-11-21 19:32:35 +00:00
/* Store VG on disk(s) */
2003-07-04 22:34:56 +00:00
if ( ! vg_write ( vg ) | | ! vg_commit ( vg ) ) {
2002-03-11 15:08:39 +00:00
unlock_vg ( cmd , vg_name ) ;
unlock_vg ( cmd , " " ) ;
2001-10-04 22:53:37 +00:00
return ECMD_FAILED ;
2002-02-11 15:42:34 +00:00
}
2002-03-11 15:08:39 +00:00
unlock_vg ( cmd , vg_name ) ;
unlock_vg ( cmd , " " ) ;
2001-10-03 20:38:07 +00:00
2002-01-07 11:12:11 +00:00
backup ( vg ) ;
2001-12-31 21:27:39 +00:00
2002-01-30 15:04:48 +00:00
log_print ( " Volume group \" %s \" successfully created " , vg - > name ) ;
2001-10-12 14:25:53 +00:00
2001-10-03 20:38:07 +00:00
return 0 ;
}