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 .
2016-02-08 15:14:43 +03:00
* Copyright ( C ) 2004 - 2016 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 ,
2016-01-21 13:49:46 +03:00
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 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
{
2016-06-15 12:35:16 +03:00
const char * cmd_name = command_name ( cmd ) ;
2016-06-17 14:25:41 +03:00
const char * type_str = arg_str_value ( cmd , type_ARG , NULL ) ;
2021-03-18 15:19:45 +03:00
int only_linear = 0 ;
2016-06-17 14:25:41 +03:00
2021-03-18 15:19:45 +03:00
if ( type_str ) {
if ( ! strcmp ( type_str , " linear " ) ) {
type_str = " striped " ;
only_linear = 1 ; /* User requested linear only target */
}
if ( ! ( lp - > segtype = get_segtype_from_string ( cmd , type_str ) ) )
return_0 ;
}
2004-05-11 22:47:40 +04:00
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 ;
2016-06-15 12:35:16 +03:00
else if ( ! strcmp ( cmd_name , " lvextend " ) )
2004-05-11 22:47:40 +04:00
lp - > resize = LV_EXTEND ;
2016-06-15 12:35:16 +03:00
else
lp - > resize = LV_ANY ;
2016-06-17 14:25:41 +03:00
lp - > sign = lp - > poolmetadata_sign = SIGN_NONE ;
2001-11-13 17:17:50 +03:00
2016-06-17 14:25:41 +03:00
if ( ( lp - > use_policies = arg_is_set ( cmd , usepolicies_ARG ) ) ) {
2016-06-24 01:24:26 +03:00
/* do nothing; lv_resize will handle --use-policies itself */
2016-06-14 14:40:57 +03:00
if ( arg_from_list_is_set ( cmd , NULL ,
chunksize_ARG , extents_ARG ,
poolmetadatasize_ARG ,
regionsize_ARG ,
size_ARG ,
stripes_ARG , stripesize_ARG ,
- 1 ) )
log_print_unless_silent ( " Ignoring size parameters with --use-policies. " ) ;
2010-10-15 20:28:14 +04:00
} 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
*/
2016-06-17 14:25:41 +03:00
if ( ( 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 ) ;
}
2015-12-16 13:23:45 +03:00
2016-02-26 00:44:31 +03:00
if ( arg_from_list_is_zero ( cmd , " may not be zero " ,
chunksize_ARG , extents_ARG ,
poolmetadatasize_ARG ,
regionsize_ARG ,
size_ARG ,
stripes_ARG , stripesize_ARG ,
virtualsize_ARG ,
- 1 ) )
return_0 ;
2016-06-17 14:25:41 +03:00
if ( ( lp - > poolmetadata_size = arg_uint64_value ( cmd , poolmetadatasize_ARG , 0 ) ) ) {
lp - > poolmetadata_sign = arg_sign_value ( cmd , poolmetadatasize_ARG , SIGN_NONE ) ;
if ( lp - > poolmetadata_sign = = SIGN_MINUS ) {
2015-12-16 13:23:45 +03:00
log_error ( " Can't reduce pool metadata size. " ) ;
return 0 ;
}
}
2016-06-17 14:25:41 +03:00
if ( ( lp - > size = arg_uint64_value ( cmd , size_ARG , 0 ) ) ) {
lp - > sign = arg_sign_value ( cmd , size_ARG , SIGN_NONE ) ;
lp - > percent = PERCENT_NONE ;
2010-10-15 20:28:14 +04:00
}
2001-11-13 17:17:50 +03:00
2016-06-17 14:25:41 +03:00
if ( lp - > size & & lp - > extents ) {
log_error ( " Please specify either size or extents but not both. " ) ;
return 0 ;
2010-10-15 20:28:14 +04:00
}
2001-11-13 17:17:50 +03:00
2016-06-17 14:25:41 +03:00
if ( ! lp - > extents & &
! lp - > size & &
! lp - > poolmetadata_size & &
( argc > = 2 ) ) {
lp - > extents = 100 ;
lp - > percent = PERCENT_PVS ;
lp - > sign = SIGN_PLUS ;
2010-10-15 20:28:14 +04:00
}
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 ) {
2016-06-14 15:56:17 +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 & &
2016-06-17 14:25:41 +03:00
( ( lp - > sign = = SIGN_PLUS ) | |
( lp - > poolmetadata_sign = = SIGN_PLUS ) ) ) {
2016-06-14 15:56:17 +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
}
if ( ! argc ) {
2016-06-14 15:56:17 +03:00
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
2016-06-24 01:33:40 +03:00
if ( ! validate_lvname_param ( cmd , & lp - > vg_name , & lp - > lv_name ) )
2016-06-17 14:25:41 +03:00
return_0 ;
2008-02-06 15:45:32 +03:00
2016-06-17 14:25:41 +03:00
/* Check for $LVM_VG_NAME */
if ( ! lp - > vg_name & & ! ( lp - > vg_name = extract_vgname ( cmd , NULL ) ) ) {
log_error ( " Please specify a logical volume path. " ) ;
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
2017-02-19 21:08:48 +03:00
if ( arg_is_set ( cmd , mirrors_ARG ) ) {
if ( arg_sign_value ( cmd , mirrors_ARG , SIGN_NONE ) ! = SIGN_NONE ) {
log_error ( " Mirrors argument may not be signed. " ) ;
return 0 ;
}
if ( ( lp - > mirrors = arg_uint_value ( cmd , mirrors_ARG , 0 ) ) )
lp - > mirrors + + ;
2013-01-09 02:39:00 +04:00
}
2016-06-17 14:25:41 +03:00
if ( ( lp - > stripes = arg_uint_value ( cmd , stripes_ARG , 0 ) ) & &
( arg_sign_value ( cmd , stripes_ARG , SIGN_NONE ) = = SIGN_MINUS ) ) {
log_error ( " Stripes argument may not be negative. " ) ;
return 0 ;
2013-01-09 02:39:00 +04:00
}
2021-03-18 15:19:45 +03:00
if ( only_linear & & lp - > stripes > 1 ) {
log_error ( " Cannot use stripes with linear type. " ) ;
return 0 ;
}
2016-06-17 14:25:41 +03:00
if ( ( lp - > stripe_size = arg_uint64_value ( cmd , stripesize_ARG , 0 ) ) & &
( arg_sign_value ( cmd , stripesize_ARG , SIGN_NONE ) = = SIGN_MINUS ) ) {
log_error ( " Stripesize may not be negative. " ) ;
return 0 ;
2013-01-09 02:39:00 +04:00
}
2016-06-17 14:25:41 +03:00
lp - > argc = - - argc ;
lp - > argv = + + argv ;
2013-01-09 02:39:00 +04:00
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 ;
2016-02-08 15:14:43 +03:00
struct dm_list * pvh ;
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 ) ) ) {
2016-06-14 15:56:17 +03:00
log_error ( " Logical volume %s not found in volume group %s. " ,
lp - > lv_name , 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 ;
2016-06-14 16:32:21 +03:00
if ( ! lv_resize ( 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 )
{
2016-02-08 15:14:43 +03:00
struct processing_handle * handle ;
2021-02-21 00:11:30 +03:00
struct lvresize_params lp = {
. alloc = ( alloc_policy_t ) arg_uint_value ( cmd , alloc_ARG , 0 ) ,
. yes = arg_is_set ( cmd , yes_ARG ) ,
. force = arg_is_set ( cmd , force_ARG ) ,
. nofsck = arg_is_set ( cmd , nofsck_ARG ) ,
. nosync = arg_is_set ( cmd , nosync_ARG ) ,
. resizefs = arg_is_set ( cmd , resizefs_ARG ) ,
. lockopt = arg_str_value ( cmd , lockopt_ARG , NULL ) ,
} ;
2016-02-23 21:57:56 +03:00
int ret ;
2015-12-01 20:01:26 +03:00
2016-02-23 21:57:56 +03:00
if ( ! _lvresize_params ( cmd , argc , argv , & lp ) ) {
stack ;
2015-12-01 20:01:26 +03:00
return EINVALID_CMD_LINE ;
2016-02-23 21:57:56 +03:00
}
2015-12-01 20:01:26 +03:00
2016-05-31 13:24:05 +03:00
if ( ! ( handle = init_processing_handle ( cmd , NULL ) ) ) {
2015-12-01 20:01:26 +03:00
log_error ( " Failed to initialize processing handle. " ) ;
return ECMD_FAILED ;
}
handle - > custom_handle = & lp ;
2016-05-03 12:46:28 +03:00
ret = process_each_vg ( cmd , 0 , NULL , lp . vg_name , NULL , READ_FOR_UPDATE , 0 , handle ,
2015-12-01 20:01:26 +03:00
& _lvresize_single ) ;
2004-05-11 22:47:40 +04:00
2015-12-01 20:01:26 +03:00
destroy_processing_handle ( cmd , handle ) ;
2016-02-08 15:14:43 +03:00
2019-03-20 21:20:26 +03:00
if ( lp . lockd_lv_refresh_path & & ! lockd_lv_refresh ( cmd , & lp ) )
ret = ECMD_FAILED ;
2015-12-01 20:01:26 +03:00
return ret ;
2004-05-11 22:47:40 +04:00
}