2001-11-13 17:17:50 +03:00
/*
2004-05-11 20:01:58 +04:00
* Copyright ( C ) 2001 - 2004 Sistina Software , Inc . All rights reserved .
2014-10-31 12:15:00 +03:00
* Copyright ( C ) 2004 - 2014 Red Hat , Inc . All rights reserved .
2001-11-13 17:17:50 +03:00
*
2004-03-30 23:35:44 +04:00
* This file is part of LVM2 .
2001-11-13 17:17:50 +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-13 17:17:50 +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-11-13 17:17:50 +03:00
*/
# include "tools.h"
2006-04-19 19:33:07 +04:00
static int _lvresize_params ( struct cmd_context * cmd , int argc , char * * argv ,
struct lvresize_params * lp )
2004-05-11 22:47:40 +04:00
{
const char * cmd_name ;
char * st ;
2010-10-15 20:28:14 +04:00
int use_policy = arg_count ( cmd , use_policies_ARG ) ;
2004-05-11 22:47:40 +04:00
lp - > sign = SIGN_NONE ;
2013-06-11 15:45:49 +04:00
lp - > poolmetadatasign = SIGN_NONE ;
2004-05-11 22:47:40 +04:00
lp - > resize = LV_ANY ;
2001-11-13 17:17:50 +03:00
2002-02-11 23:50:53 +03:00
cmd_name = command_name ( cmd ) ;
2001-11-13 17:17:50 +03:00
if ( ! strcmp ( cmd_name , " lvreduce " ) )
2004-05-11 22:47:40 +04:00
lp - > resize = LV_REDUCE ;
2001-11-13 17:17:50 +03:00
if ( ! strcmp ( cmd_name , " lvextend " ) )
2004-05-11 22:47:40 +04:00
lp - > resize = LV_EXTEND ;
2001-11-13 17:17:50 +03:00
2010-10-15 20:28:14 +04:00
if ( use_policy ) {
/* do nothing; _lvresize will handle --use-policies itself */
lp - > extents = 0 ;
2007-09-21 01:39:08 +04:00
lp - > sign = SIGN_PLUS ;
2010-10-15 20:28:14 +04:00
lp - > percent = PERCENT_LV ;
} else {
/*
* Allow omission of extents and size if the user has given us
* one or more PVs . Most likely , the intent was " resize this
* LV the best you can with these PVs "
2013-06-11 21:46:42 +04:00
* If only - - poolmetadatasize is specified with list of PVs ,
* then metadata will be extended there .
2010-10-15 20:28:14 +04:00
*/
2013-06-11 15:45:49 +04:00
lp - > sizeargs = arg_count ( cmd , extents_ARG ) + arg_count ( cmd , size_ARG ) ;
if ( ( lp - > sizeargs = = 0 ) & & ( argc > = 2 ) ) {
2010-10-15 20:28:14 +04:00
lp - > extents = 100 ;
lp - > percent = PERCENT_PVS ;
lp - > sign = SIGN_PLUS ;
2013-06-11 21:46:42 +04:00
lp - > sizeargs = ! lp - > poolmetadatasize ? 1 : 0 ;
2013-06-11 15:45:49 +04:00
} else if ( ( lp - > sizeargs ! = 1 ) & &
( ( lp - > sizeargs = = 2 ) | |
! arg_count ( cmd , poolmetadatasize_ARG ) ) ) {
2010-10-15 20:28:14 +04:00
log_error ( " Please specify either size or extents but not "
" both. " ) ;
return 0 ;
}
2001-11-13 17:17:50 +03:00
2010-10-15 20:28:14 +04:00
if ( arg_count ( cmd , extents_ARG ) ) {
lp - > extents = arg_uint_value ( cmd , extents_ARG , 0 ) ;
lp - > sign = arg_sign_value ( cmd , extents_ARG , SIGN_NONE ) ;
lp - > percent = arg_percent_value ( cmd , extents_ARG , PERCENT_NONE ) ;
}
2001-11-13 17:17:50 +03:00
2010-10-15 20:28:14 +04:00
/* Size returned in kilobyte units; held in sectors */
if ( arg_count ( cmd , size_ARG ) ) {
lp - > size = arg_uint64_value ( cmd , size_ARG , 0 ) ;
lp - > sign = arg_sign_value ( cmd , size_ARG , SIGN_NONE ) ;
lp - > percent = PERCENT_NONE ;
}
2013-06-11 15:45:49 +04:00
if ( arg_count ( cmd , poolmetadatasize_ARG ) ) {
lp - > poolmetadatasize = arg_uint64_value ( cmd , poolmetadatasize_ARG , 0 ) ;
lp - > poolmetadatasign = arg_sign_value ( cmd , poolmetadatasize_ARG , SIGN_NONE ) ;
if ( lp - > poolmetadatasign = = SIGN_MINUS ) {
log_error ( " Can't reduce pool metadata size. " ) ;
return 0 ;
}
}
2001-11-13 17:17:50 +03:00
}
2004-05-11 22:47:40 +04:00
if ( lp - > resize = = LV_EXTEND & & lp - > sign = = SIGN_MINUS ) {
2001-11-13 17:17:50 +03:00
log_error ( " Negative argument not permitted - use lvreduce " ) ;
2004-05-11 22:47:40 +04:00
return 0 ;
2001-11-13 17:17:50 +03:00
}
2013-06-11 15:45:49 +04:00
if ( lp - > resize = = LV_REDUCE & &
( ( lp - > sign = = SIGN_PLUS ) | | ( lp - > poolmetadatasign = = SIGN_PLUS ) ) ) {
2001-11-13 17:17:50 +03:00
log_error ( " Positive sign not permitted - use lvextend " ) ;
2004-05-11 22:47:40 +04:00
return 0 ;
2001-11-13 17:17:50 +03:00
}
2009-11-03 18:50:42 +03:00
lp - > resizefs = arg_is_set ( cmd , resizefs_ARG ) ;
lp - > nofsck = arg_is_set ( cmd , nofsck_ARG ) ;
2004-06-15 21:23:49 +04:00
2001-11-13 17:17:50 +03:00
if ( ! argc ) {
log_error ( " Please provide the logical volume name " ) ;
2004-05-11 22:47:40 +04:00
return 0 ;
2001-11-13 17:17:50 +03:00
}
2004-05-11 22:47:40 +04:00
lp - > lv_name = argv [ 0 ] ;
2001-11-13 17:17:50 +03:00
argv + + ;
argc - - ;
2014-09-27 22:43:32 +04:00
if ( ! ( lp - > lv_name = skip_dev_dir ( cmd , lp - > lv_name , NULL ) ) | |
2008-04-10 23:59:43 +04:00
! ( lp - > vg_name = extract_vgname ( cmd , lp - > lv_name ) ) ) {
2001-11-13 17:17:50 +03:00
log_error ( " Please provide a volume group name " ) ;
2004-05-11 22:47:40 +04:00
return 0 ;
2001-11-13 17:17:50 +03:00
}
2008-02-06 15:45:32 +03:00
2007-11-02 23:40:05 +03:00
if ( ! validate_name ( lp - > vg_name ) ) {
log_error ( " Volume group name %s has invalid characters " ,
lp - > vg_name ) ;
2007-11-12 23:02:55 +03:00
return 0 ;
2007-11-02 23:40:05 +03:00
}
2001-11-13 17:17:50 +03:00
2004-05-11 22:47:40 +04:00
if ( ( st = strrchr ( lp - > lv_name , ' / ' ) ) )
lp - > lv_name = st + 1 ;
2001-11-13 17:17:50 +03:00
2004-05-11 22:47:40 +04:00
lp - > argc = argc ;
lp - > argv = argv ;
2013-01-09 02:39:00 +04:00
lp - > ac_policy = arg_count ( cmd , use_policies_ARG ) ;
lp - > ac_stripes = arg_count ( cmd , stripes_ARG ) ;
if ( lp - > ac_stripes ) {
lp - > ac_stripes_value = arg_uint_value ( cmd , stripes_ARG , 1 ) ;
} else {
lp - > ac_stripes_value = 0 ;
}
lp - > ac_mirrors = arg_count ( cmd , mirrors_ARG ) ;
if ( lp - > ac_mirrors ) {
if ( arg_sign_value ( cmd , mirrors_ARG , SIGN_NONE ) = = SIGN_MINUS ) {
log_error ( " Mirrors argument may not be negative " ) ;
return 0 ;
}
lp - > ac_mirrors_value = arg_uint_value ( cmd , mirrors_ARG , 1 ) + 1 ;
} else {
lp - > ac_mirrors_value = 0 ;
}
lp - > ac_stripesize = arg_count ( cmd , stripesize_ARG ) ;
if ( lp - > ac_stripesize ) {
if ( arg_sign_value ( cmd , stripesize_ARG , SIGN_NONE ) = = SIGN_MINUS ) {
log_error ( " Stripesize may not be negative. " ) ;
return 0 ;
}
lp - > ac_stripesize_value = arg_uint64_value ( cmd , stripesize_ARG , 0 ) ;
}
lp - > ac_no_sync = arg_count ( cmd , nosync_ARG ) ;
2013-07-06 06:28:21 +04:00
lp - > ac_alloc = ( alloc_policy_t ) arg_uint_value ( cmd , alloc_ARG , 0 ) ;
2013-01-09 02:39:00 +04:00
lp - > ac_type = arg_str_value ( cmd , type_ARG , NULL ) ;
lp - > ac_force = arg_count ( cmd , force_ARG ) ;
2004-05-11 22:47:40 +04:00
return 1 ;
}
2002-02-11 18:42:34 +03:00
2015-12-01 20:01:26 +03:00
static int _lvresize_single ( struct cmd_context * cmd , const char * vg_name ,
struct volume_group * vg , struct processing_handle * handle )
2004-05-11 22:47:40 +04:00
{
2015-12-01 20:01:26 +03:00
struct lvresize_params * lp = ( struct lvresize_params * ) handle - > custom_handle ;
2013-01-09 02:39:00 +04:00
struct dm_list * pvh = NULL ;
2014-10-31 12:15:00 +03:00
struct logical_volume * lv ;
2015-12-01 20:01:26 +03:00
int ret = ECMD_FAILED ;
2004-05-11 22:47:40 +04:00
2014-10-31 12:15:00 +03:00
/* Does LV exist? */
2015-12-01 20:01:26 +03:00
if ( ! ( lv = find_lv ( vg , lp - > lv_name ) ) ) {
2014-10-31 12:15:00 +03:00
log_error ( " Logical volume %s not found in volume group %s " ,
2015-12-01 20:01:26 +03:00
lp - > lv_name , lp - > vg_name ) ;
2013-07-06 06:28:21 +04:00
goto out ;
2014-10-31 12:15:00 +03:00
}
2013-07-06 06:28:21 +04:00
2015-12-01 20:01:26 +03:00
if ( ! ( pvh = lp - > argc ? create_pv_list ( cmd - > mem , vg , lp - > argc , lp - > argv , 1 ) : & vg - > pvs ) )
2013-07-06 06:28:21 +04:00
goto_out ;
2015-12-01 20:01:26 +03:00
if ( ! lv_resize_prepare ( cmd , lv , lp , pvh ) ) {
ret = EINVALID_CMD_LINE ;
2013-07-06 06:28:21 +04:00
goto_out ;
2013-01-09 02:39:00 +04:00
}
2015-12-01 20:01:26 +03:00
if ( ! lv_resize ( cmd , lv , lp , pvh ) )
2013-07-06 06:28:21 +04:00
goto_out ;
2015-12-01 20:01:26 +03:00
ret = ECMD_PROCESSED ;
2013-07-06 06:28:21 +04:00
out :
2015-12-01 20:01:26 +03:00
return ret ;
}
int lvresize ( struct cmd_context * cmd , int argc , char * * argv )
{
struct processing_handle * handle = NULL ;
struct lvresize_params lp = { 0 } ;
int ret = ECMD_FAILED ;
if ( ! _lvresize_params ( cmd , argc , argv , & lp ) )
return EINVALID_CMD_LINE ;
if ( ! ( handle = init_processing_handle ( cmd ) ) ) {
log_error ( " Failed to initialize processing handle. " ) ;
return ECMD_FAILED ;
}
handle - > custom_handle = & lp ;
ret = process_each_vg ( cmd , 0 , NULL , lp . vg_name , READ_FOR_UPDATE , handle ,
& _lvresize_single ) ;
2004-05-11 22:47:40 +04:00
2015-12-01 20:01:26 +03:00
destroy_processing_handle ( cmd , handle ) ;
return ret ;
2004-05-11 22:47:40 +04:00
}