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 ,
* Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 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 ,
2006-05-10 01:23:51 +04:00
void * handle __attribute ( ( unused ) ) )
2002-11-18 17:04:08 +03:00
{
struct physical_volume * pv , * existing_pv ;
2003-11-06 19:58:38 +03:00
struct logical_volume * lv ;
struct lv_list * lvl ;
2002-11-18 17:04:08 +03:00
uint64_t size = 0 ;
2008-11-04 01:14:30 +03:00
struct dm_list mdas ;
2002-11-18 17:04:08 +03:00
int pvmetadatacopies = 0 ;
uint64_t pvmetadatasize = 0 ;
uint64_t pe_end = 0 , pe_start = 0 ;
2003-10-16 00:02:46 +04:00
struct pv_list * pvl ;
2002-12-03 16:23:50 +03:00
int change_made = 0 ;
2003-11-06 19:58:38 +03:00
struct lvinfo info ;
int active = 0 ;
2002-11-18 17:04:08 +03:00
2009-09-15 02:47:49 +04:00
if ( ! vg_check_status ( vg , LVM_WRITE | EXPORTED_VG ) ) {
stack ;
2002-11-18 17:04:08 +03:00
return ECMD_FAILED ;
2009-09-15 02:47:49 +04:00
}
2002-11-18 17:04:08 +03:00
if ( vg - > fid - > fmt = = cmd - > fmt ) {
log_error ( " Volume group \" %s \" already uses format %s " ,
vg_name , cmd - > fmt - > name ) ;
return ECMD_FAILED ;
}
if ( cmd - > fmt - > features & FMT_MDAS ) {
2003-09-15 19:04:39 +04:00
if ( arg_sign_value ( cmd , metadatasize_ARG , 0 ) = = SIGN_MINUS ) {
log_error ( " Metadata size may not be negative " ) ;
return EINVALID_CMD_LINE ;
}
2002-12-20 02:25:55 +03:00
pvmetadatasize = arg_uint64_value ( cmd , metadatasize_ARG ,
2007-11-14 03:08:25 +03:00
UINT64_C ( 0 ) ) ;
2002-11-18 17:04:08 +03:00
if ( ! pvmetadatasize )
pvmetadatasize =
2006-05-16 20:48:31 +04:00
find_config_tree_int ( cmd ,
2002-11-18 17:04:08 +03:00
" metadata/pvmetadatasize " ,
2004-03-08 21:28:45 +03:00
DEFAULT_PVMETADATASIZE ) ;
2002-11-18 17:04:08 +03:00
2009-10-06 00:55:56 +04:00
pvmetadatacopies = arg_int_value ( cmd , pvmetadatacopies_ARG , - 1 ) ;
2002-11-18 17:04:08 +03:00
if ( pvmetadatacopies < 0 )
pvmetadatacopies =
2006-05-16 20:48:31 +04:00
find_config_tree_int ( cmd ,
2002-11-18 17:04:08 +03:00
" metadata/pvmetadatacopies " ,
2004-03-08 21:28:45 +03:00
DEFAULT_PVMETADATACOPIES ) ;
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 & &
! ( cmd - > fmt - > features & FMT_UNLIMITED_VOLS ) ) {
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 ) & &
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 ;
}
2003-11-06 19:58:38 +03:00
/* Attempt to change any LVIDs that are too big */
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 ;
2005-04-07 16:39:44 +04:00
if ( lv - > status & SNAPSHOT )
continue ;
2003-11-06 19:58:38 +03:00
if ( lvnum_from_lvid ( & lv - > lvid ) < MAX_RESTRICTED_LVS )
continue ;
2007-11-12 23:51:54 +03:00
if ( lv_info ( cmd , lv , & 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 ) ) ;
}
}
2009-09-15 02:47:49 +04:00
if ( active ) {
stack ;
2003-11-06 19:58:38 +03:00
return ECMD_FAILED ;
2009-09-15 02:47:49 +04:00
}
2003-11-06 19:58:38 +03:00
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( pvl , & vg - > pvs ) {
2003-10-16 00:02:46 +04:00
existing_pv = pvl - > pv ;
2002-11-18 17:04:08 +03:00
2007-06-16 02:16:55 +04:00
pe_start = pv_pe_start ( existing_pv ) ;
pe_end = pv_pe_count ( existing_pv ) * pv_pe_size ( existing_pv )
2002-11-18 17:04:08 +03:00
+ pe_start - 1 ;
2008-11-04 01:14:30 +03:00
dm_list_init ( & mdas ) ;
2008-06-25 00:10:32 +04:00
if ( ! ( pv = pv_create ( cmd , pv_dev ( existing_pv ) ,
2009-07-30 21:45:28 +04:00
& existing_pv - > id , size , 0 , 0 ,
2007-06-16 02:16:55 +04:00
pe_start , pv_pe_count ( existing_pv ) ,
pv_pe_size ( existing_pv ) , pvmetadatacopies ,
2002-11-18 17:04:08 +03:00
pvmetadatasize , & mdas ) ) ) {
log_error ( " Failed to setup physical volume \" %s \" " ,
2007-10-12 18:29:32 +04:00
pv_dev_name ( existing_pv ) ) ;
2002-12-03 16:23:50 +03:00
if ( change_made )
log_error ( " Use pvcreate and vgcfgrestore to "
" repair from archived metadata. " ) ;
2002-11-18 17:04:08 +03:00
return ECMD_FAILED ;
}
2002-12-03 16:23:50 +03:00
/* Need to revert manually if it fails after this point */
change_made = 1 ;
2002-11-18 17:04:08 +03:00
log_verbose ( " Set up physical volume for \" %s \" with % " PRIu64
2007-10-12 18:29:32 +04:00
" available sectors " , pv_dev_name ( pv ) , pv_size ( pv ) ) ;
2002-11-18 17:04:08 +03:00
/* Wipe existing label first */
2007-06-16 02:16:55 +04:00
if ( ! label_remove ( pv_dev ( pv ) ) ) {
2002-11-18 17:04:08 +03:00
log_error ( " Failed to wipe existing label on %s " ,
2007-10-12 18:29:32 +04:00
pv_dev_name ( pv ) ) ;
2002-11-18 17:04:08 +03:00
log_error ( " Use pvcreate and vgcfgrestore to repair "
" from archived metadata. " ) ;
return ECMD_FAILED ;
}
log_very_verbose ( " Writing physical volume data to disk \" %s \" " ,
2007-10-12 18:29:32 +04:00
pv_dev_name ( pv ) ) ;
2002-11-18 17:04:08 +03:00
if ( ! ( pv_write ( cmd , pv , & mdas ,
2002-12-20 02:25:55 +03:00
arg_int64_value ( cmd , labelsector_ARG ,
DEFAULT_LABELSECTOR ) ) ) ) {
2002-11-18 17:04:08 +03:00
log_error ( " Failed to write physical volume \" %s \" " ,
2007-10-12 18:29:32 +04:00
pv_dev_name ( pv ) ) ;
2002-11-18 17:04:08 +03:00
log_error ( " Use pvcreate and vgcfgrestore to repair "
" from archived metadata. " ) ;
return ECMD_FAILED ;
}
log_verbose ( " Physical volume \" %s \" successfully created " ,
2007-10-12 18:29:32 +04:00
pv_dev_name ( pv ) ) ;
2002-11-18 17:04:08 +03:00
}
log_verbose ( " Deleting existing metadata for VG %s " , vg_name ) ;
2009-09-03 01:39:07 +04:00
if ( ! vg_remove_mdas ( vg ) ) {
2002-11-18 17:04:08 +03:00
log_error ( " Removal of existing metadata for %s failed. " ,
vg_name ) ;
log_error ( " Use pvcreate and vgcfgrestore to repair "
" from archived metadata. " ) ;
return ECMD_FAILED ;
}
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 ,
cmd - > fmt - > name ) ;
if ( ! backup_restore_vg ( cmd , vg ) ) {
log_error ( " Conversion failed for volume group %s. " , vg_name ) ;
log_error ( " Use pvcreate and vgcfgrestore to repair from "
" archived metadata. " ) ;
return ECMD_FAILED ;
}
log_print ( " Volume group %s successfully converted " , vg_name ) ;
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 ;
}
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 ;
}
2009-10-06 00:55:56 +04:00
if ( arg_count ( cmd , metadatacopies_ARG ) ) {
log_error ( " Invalid option --metadatacopies, "
" use --pvmetadatacopies instead. " ) ;
return EINVALID_CMD_LINE ;
}
2002-11-18 17:04:08 +03:00
if ( ! ( cmd - > fmt - > features & FMT_MDAS ) & &
2009-10-06 00:55:56 +04:00
( arg_count ( cmd , pvmetadatacopies_ARG ) | |
2002-11-18 17:04:08 +03:00
arg_count ( cmd , metadatasize_ARG ) ) ) {
log_error ( " Metadata parameters only apply to text format " ) ;
return EINVALID_CMD_LINE ;
}
2009-10-06 00:55:56 +04:00
if ( arg_count ( cmd , pvmetadatacopies_ARG ) & &
arg_int_value ( cmd , pvmetadatacopies_ARG , - 1 ) > 2 ) {
2002-11-18 17:04:08 +03:00
log_error ( " Metadatacopies may only be 0, 1 or 2 " ) ;
return EINVALID_CMD_LINE ;
}
2009-07-01 21:00:50 +04:00
return process_each_vg ( cmd , argc , argv , READ_FOR_UPDATE , NULL ,
2002-11-18 17:04:08 +03:00
& vgconvert_single ) ;
}