2002-11-18 17:04:08 +03: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 .
2002-11-18 17:04:08 +03:00
*
2004-03-30 23:35:44 +04:00
* This file is part of LVM2 .
2002-11-18 17:04:08 +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 .
2002-11-18 17:04:08 +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
2002-11-18 17:04:08 +03:00
*/
# include "tools.h"
static int vgconvert_single ( struct cmd_context * cmd , const char * vg_name ,
2009-07-01 21:00:50 +04:00
struct volume_group * vg ,
2014-11-27 17:02:13 +03:00
struct processing_handle * handle __attribute__ ( ( unused ) ) )
2002-11-18 17:04:08 +03:00
{
2016-02-19 00:31:27 +03:00
struct pv_create_args pva = { 0 } ;
2003-11-06 19:58:38 +03:00
struct logical_volume * lv ;
struct lv_list * lvl ;
struct lvinfo info ;
int active = 0 ;
2002-11-18 17:04:08 +03:00
2013-07-01 13:27:22 +04:00
if ( ! vg_check_status ( vg , LVM_WRITE | EXPORTED_VG ) )
return_ECMD_FAILED ;
2002-11-18 17:04:08 +03:00
2011-02-25 17:02:53 +03:00
if ( vg - > fid - > fmt = = cmd - > fmt ) {
2002-11-18 17:04:08 +03:00
log_error ( " Volume group \" %s \" already uses format %s " ,
vg_name , cmd - > fmt - > name ) ;
return ECMD_FAILED ;
}
2011-02-25 17:02:53 +03:00
if ( cmd - > fmt - > features & FMT_MDAS ) {
2012-02-28 18:24:57 +04:00
if ( arg_sign_value ( cmd , metadatasize_ARG , SIGN_NONE ) = = SIGN_MINUS ) {
2003-09-15 19:04:39 +04:00
log_error ( " Metadata size may not be negative " ) ;
return EINVALID_CMD_LINE ;
}
2016-02-19 00:31:27 +03:00
pva . pvmetadatasize = arg_uint64_value ( cmd , metadatasize_ARG , UINT64_C ( 0 ) ) ;
if ( ! pva . pvmetadatasize )
pva . pvmetadatasize = find_config_tree_int ( cmd , metadata_pvmetadatasize_CFG , NULL ) ;
2002-11-18 17:04:08 +03:00
2016-02-19 00:31:27 +03:00
pva . pvmetadatacopies = arg_int_value ( cmd , pvmetadatacopies_ARG , - 1 ) ;
if ( pva . pvmetadatacopies < 0 )
pva . pvmetadatacopies = find_config_tree_int ( cmd , metadata_pvmetadatacopies_CFG , NULL ) ;
2002-11-18 17:04:08 +03:00
}
2013-05-28 14:37:22 +04:00
if ( cmd - > fmt - > features & FMT_BAS ) {
if ( arg_sign_value ( cmd , bootloaderareasize_ARG , SIGN_NONE ) = = SIGN_MINUS ) {
log_error ( " Bootloader area size may not be negative " ) ;
2013-02-15 14:14:26 +04:00
return EINVALID_CMD_LINE ;
}
2016-02-19 00:31:27 +03:00
pva . ba_size = arg_uint64_value ( cmd , bootloaderareasize_ARG , UINT64_C ( 0 ) ) ;
2013-02-15 14:14:26 +04:00
}
2014-10-14 21:12:15 +04:00
if ( ! vg_check_new_extent_size ( cmd - > fmt , vg - > extent_size ) )
return_ECMD_FAILED ;
2002-11-18 17:04:08 +03:00
if ( ! archive ( vg ) ) {
log_error ( " Archive of \" %s \" metadata failed. " , vg_name ) ;
return ECMD_FAILED ;
}
2008-08-29 17:41:21 +04:00
/* Set PV/LV limit if converting from unlimited metadata format */
if ( vg - > fid - > fmt - > features & FMT_UNLIMITED_VOLS & &
2011-02-25 17:02:53 +03:00
! ( cmd - > fmt - > features & FMT_UNLIMITED_VOLS ) ) {
2008-08-29 17:41:21 +04:00
if ( ! vg - > max_lv )
vg - > max_lv = 255 ;
if ( ! vg - > max_pv )
vg - > max_pv = 255 ;
}
2008-08-28 22:41:51 +04:00
/* If converting to restricted lvid, check if lvid is compatible */
if ( ! ( vg - > fid - > fmt - > features & FMT_RESTRICTED_LVIDS ) & &
2011-02-25 17:02:53 +03:00
cmd - > fmt - > features & FMT_RESTRICTED_LVIDS )
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( lvl , & vg - > lvs )
2008-08-28 22:41:51 +04:00
if ( ! lvid_in_restricted_range ( & lvl - > lv - > lvid ) ) {
log_error ( " Logical volume %s lvid format is "
" incompatible with requested "
" metadata format. " , lvl - > lv - > name ) ;
return ECMD_FAILED ;
}
2015-03-09 22:01:12 +03:00
/* New-style system ID supported? */
if ( vg - > system_id & & * vg - > system_id & & ( cmd - > fmt - > features & FMT_SYSTEMID_ON_PVS ) ) {
log_error ( " Unable to convert VG %s while it has a system ID set (%s). " , vg - > name ,
vg - > system_id ) ;
return ECMD_FAILED ;
}
2003-11-06 19:58:38 +03:00
/* Attempt to change any LVIDs that are too big */
2011-02-25 17:02:53 +03:00
if ( cmd - > fmt - > features & FMT_RESTRICTED_LVIDS ) {
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( lvl , & vg - > lvs ) {
2003-11-06 19:58:38 +03:00
lv = lvl - > lv ;
2016-12-13 02:09:15 +03:00
if ( lv_is_snapshot ( lv ) )
2005-04-07 16:39:44 +04:00
continue ;
2003-11-06 19:58:38 +03:00
if ( lvnum_from_lvid ( & lv - > lvid ) < MAX_RESTRICTED_LVS )
continue ;
2010-08-17 20:25:32 +04:00
if ( lv_info ( cmd , lv , 0 , & info , 0 , 0 ) & & info . exists ) {
2003-11-06 19:58:38 +03:00
log_error ( " Logical volume %s must be "
" deactivated before conversion. " ,
lv - > name ) ;
active + + ;
continue ;
}
lvid_from_lvnum ( & lv - > lvid , & lv - > vg - > id , find_free_lvnum ( lv ) ) ;
}
}
2013-07-01 13:27:22 +04:00
if ( active )
return_ECMD_FAILED ;
2003-11-06 19:58:38 +03:00
2003-04-25 02:13:48 +04:00
/* FIXME Cache the label format change so we don't have to skip this */
if ( test_mode ( ) ) {
log_verbose ( " Test mode: Skipping metadata writing for VG %s in "
" format %s " , vg_name , cmd - > fmt - > name ) ;
2003-10-22 02:06:07 +04:00
return ECMD_PROCESSED ;
2003-04-25 02:13:48 +04:00
}
2002-11-18 17:04:08 +03:00
log_verbose ( " Writing metadata for VG %s using format %s " , vg_name ,
2011-02-25 17:02:53 +03:00
cmd - > fmt - > name ) ;
2016-02-16 22:22:50 +03:00
2016-06-18 00:37:30 +03:00
if ( ! backup_restore_vg ( cmd , vg , 1 , & pva ) ) {
2002-11-18 17:04:08 +03:00
log_error ( " Conversion failed for volume group %s. " , vg_name ) ;
2011-02-25 17:02:53 +03:00
log_error ( " Use pvcreate and vgcfgrestore to repair from "
" archived metadata. " ) ;
return ECMD_FAILED ;
2002-11-18 17:04:08 +03:00
}
config: add silent mode
Accept -q as the short form of --quiet.
Suppress non-essential standard output if -q is given twice.
Treat log/silent in lvm.conf as equivalent to -qq.
Review all log_print messages and change some to
log_print_unless_silent.
When silent, the following commands still produce output:
dumpconfig, lvdisplay, lvmdiskscan, lvs, pvck, pvdisplay,
pvs, version, vgcfgrestore -l, vgdisplay, vgs.
[Needs checking.]
Non-essential messages are shifted from log level 4 to log level 5
for syslog and lvm2_log_fn purposes.
2012-08-25 23:35:48 +04:00
log_print_unless_silent ( " Volume group %s successfully converted " , vg_name ) ;
2002-11-18 17:04:08 +03:00
backup ( vg ) ;
2003-10-22 02:06:07 +04:00
return ECMD_PROCESSED ;
2002-11-18 17:04:08 +03:00
}
int vgconvert ( struct cmd_context * cmd , int argc , char * * argv )
{
if ( ! argc ) {
log_error ( " Please enter volume group(s) " ) ;
return EINVALID_CMD_LINE ;
}
2015-03-10 17:31:54 +03:00
if ( arg_is_set ( cmd , metadatatype_ARG ) & & lvmetad_used ( ) ) {
log_error ( " lvmetad must be disabled to change metadata types. " ) ;
return EINVALID_CMD_LINE ;
}
2002-11-18 17:04:08 +03:00
if ( arg_int_value ( cmd , labelsector_ARG , 0 ) > = LABEL_SCAN_SECTORS ) {
log_error ( " labelsector must be less than %lu " ,
LABEL_SCAN_SECTORS ) ;
return EINVALID_CMD_LINE ;
}
if ( ! ( cmd - > fmt - > features & FMT_MDAS ) & &
commands: new method for defining commands
. Define a prototype for every lvm command.
. Match every user command with one definition.
. Generate help text and man pages from them.
The new file command-lines.in defines a prototype for every
unique lvm command. A unique lvm command is a unique
combination of: command name + required option args +
required positional args. Each of these prototypes also
includes the optional option args and optional positional
args that the command will accept, a description, and a
unique string ID for the definition. Any valid command
will match one of the prototypes.
Here's an example of the lvresize command definitions from
command-lines.in, there are three unique lvresize commands:
lvresize --size SizeMB LV
OO: --alloc Alloc, --autobackup Bool, --force,
--nofsck, --nosync, --noudevsync, --reportformat String, --resizefs,
--stripes Number, --stripesize SizeKB, --poolmetadatasize SizeMB
OP: PV ...
ID: lvresize_by_size
DESC: Resize an LV by a specified size.
lvresize LV PV ...
OO: --alloc Alloc, --autobackup Bool, --force,
--nofsck, --nosync, --noudevsync,
--reportformat String, --resizefs, --stripes Number, --stripesize SizeKB
ID: lvresize_by_pv
DESC: Resize an LV by specified PV extents.
FLAGS: SECONDARY_SYNTAX
lvresize --poolmetadatasize SizeMB LV_thinpool
OO: --alloc Alloc, --autobackup Bool, --force,
--nofsck, --nosync, --noudevsync,
--reportformat String, --stripes Number, --stripesize SizeKB
OP: PV ...
ID: lvresize_pool_metadata_by_size
DESC: Resize a pool metadata SubLV by a specified size.
The three commands have separate definitions because they have
different required parameters. Required parameters are specified
on the first line of the definition. Optional options are
listed after OO, and optional positional args are listed after OP.
This data is used to generate corresponding command definition
structures for lvm in command-lines.h. usage/help output is also
auto generated, so it is always in sync with the definitions.
Every user-entered command is compared against the set of
command structures, and matched with one. An error is
reported if an entered command does not have the required
parameters for any definition. The closest match is printed
as a suggestion, and running lvresize --help will display
the usage for each possible lvresize command.
The prototype syntax used for help/man output includes
required --option and positional args on the first line,
and optional --option and positional args enclosed in [ ]
on subsequent lines.
command_name <required_opt_args> <required_pos_args>
[ <optional_opt_args> ]
[ <optional_pos_args> ]
Command definitions that are not to be advertised/suggested
have the flag SECONDARY_SYNTAX. These commands will not be
printed in the normal help output.
Man page prototypes are also generated from the same original
command definitions, and are always in sync with the code
and help text.
Very early in command execution, a matching command definition
is found. lvm then knows the operation being done, and that
the provided args conform to the definition. This will allow
lots of ad hoc checking/validation to be removed throughout
the code.
Each command definition can also be routed to a specific
function to implement it. The function is associated with
an enum value for the command definition (generated from
the ID string.) These per-command-definition implementation
functions have not yet been created, so all commands
currently fall back to the existing per-command-name
implementation functions.
Using per-command-definition functions will allow lots of
code to be removed which tries to figure out what the
command is meant to do. This is currently based on ad hoc
and complicated option analysis. When using the new
functions, what the command is doing is already known
from the associated command definition.
2016-08-12 23:52:18 +03:00
arg_is_set ( cmd , pvmetadatacopies_ARG ) ) {
2002-11-18 17:04:08 +03:00
log_error ( " Metadata parameters only apply to text format " ) ;
return EINVALID_CMD_LINE ;
}
2013-05-28 14:37:22 +04:00
if ( ! ( cmd - > fmt - > features & FMT_BAS ) & &
2016-06-22 00:24:52 +03:00
arg_is_set ( cmd , bootloaderareasize_ARG ) ) {
2013-05-28 14:37:22 +04:00
log_error ( " Bootloader area parameters only apply to text format " ) ;
2013-02-15 14:14:26 +04:00
return EINVALID_CMD_LINE ;
}
2016-05-03 12:46:28 +03:00
return process_each_vg ( cmd , argc , argv , NULL , NULL , READ_FOR_UPDATE , 0 , NULL ,
2002-11-18 17:04:08 +03:00
& vgconvert_single ) ;
}