2001-11-15 17:27:45 +00:00
/*
2004-03-30 19:35:44 +00:00
* Copyright ( C ) 2001 - 2004 Sistina Software , Inc . All rights reserved .
* Copyright ( C ) 2004 Red Hat , Inc . All rights reserved .
2001-11-15 17:27:45 +00:00
*
2004-03-30 19:35:44 +00:00
* This file is part of LVM2 .
2001-11-15 17:27:45 +00:00
*
2004-03-30 19:35:44 +00:00
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
* of the GNU General Public License v .2 .
2001-11-15 17:27:45 +00:00
*
* You should have received a copy of the GNU General Public License
2004-03-30 19:35:44 +00: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-15 17:27:45 +00:00
*/
# include "tools.h"
2003-01-08 16:41:22 +00:00
# include "lvm-types.h"
2001-11-15 17:27:45 +00:00
2002-02-11 20:50:53 +00:00
int lvrename ( struct cmd_context * cmd , int argc , char * * argv )
2001-11-15 17:27:45 +00:00
{
2002-12-19 23:25:55 +00:00
size_t maxlen ;
2001-11-15 17:27:45 +00:00
char * lv_name_old , * lv_name_new ;
2002-12-19 23:25:55 +00:00
const char * vg_name , * vg_name_new , * vg_name_old ;
2001-11-15 17:27:45 +00:00
char * st ;
2002-11-18 14:04:08 +00:00
int consistent = 1 ;
2001-11-15 17:27:45 +00:00
struct volume_group * vg ;
struct logical_volume * lv ;
2002-01-21 14:28:12 +00:00
struct lv_list * lvl ;
2001-11-15 17:27:45 +00:00
2002-09-05 12:49:23 +00:00
if ( argc = = 3 ) {
vg_name = argv [ 0 ] ;
if ( ! strncmp ( vg_name , cmd - > dev_dir , strlen ( cmd - > dev_dir ) ) )
vg_name + = strlen ( cmd - > dev_dir ) ;
lv_name_old = argv [ 1 ] ;
lv_name_new = argv [ 2 ] ;
if ( strchr ( lv_name_old , ' / ' ) & &
( vg_name_old = extract_vgname ( cmd , lv_name_old ) ) & &
strcmp ( vg_name_old , vg_name ) ) {
log_error ( " Please use a single volume group name "
" ( \" %s \" or \" %s \" ) " , vg_name , vg_name_old ) ;
return EINVALID_CMD_LINE ;
}
} else if ( argc = = 2 ) {
lv_name_old = argv [ 0 ] ;
lv_name_new = argv [ 1 ] ;
vg_name = extract_vgname ( cmd , lv_name_old ) ;
} else {
log_error ( " Old and new logical volume names required " ) ;
2001-11-15 17:27:45 +00:00
return EINVALID_CMD_LINE ;
}
2003-02-03 20:09:58 +00:00
if ( ! validate_name ( vg_name ) ) {
2002-09-05 12:49:23 +00:00
log_error ( " Please provide a valid volume group name " ) ;
2001-11-15 17:27:45 +00:00
return EINVALID_CMD_LINE ;
}
if ( strchr ( lv_name_new , ' / ' ) & &
2002-04-24 18:20:51 +00:00
( vg_name_new = extract_vgname ( cmd , lv_name_new ) ) & &
2001-11-15 17:27:45 +00:00
strcmp ( vg_name , vg_name_new ) ) {
log_error ( " Logical volume names must "
2002-01-30 15:04:48 +00:00
" have the same volume group ( \" %s \" or \" %s \" ) " ,
2001-11-15 17:27:45 +00:00
vg_name , vg_name_new ) ;
return EINVALID_CMD_LINE ;
}
if ( ( st = strrchr ( lv_name_old , ' / ' ) ) )
lv_name_old = st + 1 ;
if ( ( st = strrchr ( lv_name_new , ' / ' ) ) )
lv_name_new = st + 1 ;
/* Check sanity of new name */
2002-02-11 20:50:53 +00:00
maxlen = NAME_LEN - strlen ( vg_name ) - strlen ( cmd - > dev_dir ) - 3 ;
2001-11-15 17:27:45 +00:00
if ( strlen ( lv_name_new ) > maxlen ) {
log_error ( " New logical volume path exceeds maximum length "
2003-01-08 16:41:22 +00:00
" of % " PRIsize_t " ! " , maxlen ) ;
2001-11-15 17:27:45 +00:00
return ECMD_FAILED ;
}
if ( ! * lv_name_new ) {
log_error ( " New logical volume name may not be blank " ) ;
return ECMD_FAILED ;
}
2002-11-18 14:04:08 +00:00
/* FIXME Remove this restriction eventually */
if ( ! strncmp ( lv_name_new , " snapshot " , 8 ) ) {
log_error ( " Names starting \" snapshot \" are reserved. "
" Please choose a different LV name. " ) ;
return ECMD_FAILED ;
}
2003-02-03 20:09:58 +00:00
if ( ! validate_name ( lv_name_new ) ) {
2003-09-15 15:01:00 +00:00
log_error ( " New logical volume name \" %s \" is invalid " ,
2002-02-11 21:00:35 +00:00
lv_name_new ) ;
2001-11-15 17:27:45 +00:00
return EINVALID_CMD_LINE ;
}
if ( ! strcmp ( lv_name_old , lv_name_new ) ) {
log_error ( " Old and new logical volume names must differ " ) ;
return EINVALID_CMD_LINE ;
}
2002-01-30 15:04:48 +00:00
log_verbose ( " Checking for existing volume group \" %s \" " , vg_name ) ;
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 ) ) {
2002-02-11 15:42:34 +00:00
log_error ( " Can't get lock for %s " , vg_name ) ;
return ECMD_FAILED ;
}
2002-11-18 14:04:08 +00:00
if ( ! ( vg = vg_read ( cmd , vg_name , & consistent ) ) ) {
2002-01-30 15:04:48 +00:00
log_error ( " Volume group \" %s \" doesn't exist " , vg_name ) ;
2002-02-11 15:42:34 +00:00
goto error ;
2001-11-15 17:27:45 +00:00
}
2002-02-11 21:00:35 +00:00
if ( vg - > status & EXPORTED_VG ) {
log_error ( " Volume group \" %s \" is exported " , vg - > name ) ;
2002-02-11 15:42:34 +00:00
goto error ;
2002-02-11 21:00:35 +00:00
}
2002-01-29 17:23:33 +00:00
2002-02-11 21:00:35 +00:00
if ( ! ( vg - > status & LVM_WRITE ) ) {
log_error ( " Volume group \" %s \" is read-only " , vg_name ) ;
2002-02-11 15:42:34 +00:00
goto error ;
2002-02-11 21:00:35 +00:00
}
2002-01-29 17:23:33 +00:00
2002-01-21 14:28:12 +00:00
if ( find_lv_in_vg ( vg , lv_name_new ) ) {
2002-01-30 15:04:48 +00:00
log_error ( " Logical volume \" %s \" already exists in "
" volume group \" %s \" " , lv_name_new , vg_name ) ;
2002-02-11 15:42:34 +00:00
goto error ;
2001-11-15 17:27:45 +00:00
}
2002-01-21 14:28:12 +00:00
if ( ! ( lvl = find_lv_in_vg ( vg , lv_name_old ) ) ) {
2002-01-30 15:04:48 +00:00
log_error ( " Existing logical volume \" %s \" not found in "
" volume group \" %s \" " , lv_name_old , vg_name ) ;
2002-02-11 15:42:34 +00:00
goto error ;
2001-11-15 17:27:45 +00:00
}
2002-01-21 16:49:32 +00:00
lv = lvl - > lv ;
2001-11-15 17:27:45 +00:00
2003-05-06 12:10:18 +00:00
if ( lv - > status & LOCKED ) {
log_error ( " Cannot rename locked LV %s " , lv - > name ) ;
goto error ;
}
2003-07-04 22:34:56 +00:00
if ( ! archive ( lv - > vg ) ) {
stack ;
2002-02-11 15:42:34 +00:00
goto error ;
2003-07-04 22:34:56 +00:00
}
2002-01-10 23:21:07 +00:00
2002-02-11 20:50:53 +00:00
if ( ! ( lv - > name = pool_strdup ( cmd - > mem , lv_name_new ) ) ) {
2001-11-15 17:27:45 +00:00
log_error ( " Failed to allocate space for new name " ) ;
2003-07-04 22:34:56 +00:00
goto error ;
2001-11-15 17:27:45 +00:00
}
log_verbose ( " Writing out updated volume group " ) ;
2003-07-04 22:34:56 +00:00
if ( ! vg_write ( vg ) ) {
stack ;
goto error ;
}
2001-12-31 21:27:39 +00:00
2002-01-10 23:21:07 +00:00
backup ( lv - > vg ) ;
2001-11-15 17:27:45 +00:00
2004-05-05 12:03:07 +00:00
if ( ! suspend_lv ( cmd , lv - > lvid . s ) ) {
2003-07-04 22:34:56 +00:00
stack ;
goto error ;
}
if ( ! vg_commit ( vg ) ) {
stack ;
2004-05-05 12:03:07 +00:00
resume_lv ( cmd , lv - > lvid . s ) ;
2003-07-04 22:34:56 +00:00
goto error ;
}
2004-05-05 12:03:07 +00:00
resume_lv ( cmd , lv - > lvid . s ) ;
2003-07-04 22:34:56 +00:00
2002-03-11 15:08:39 +00:00
unlock_vg ( cmd , vg_name ) ;
2002-02-11 15:42:34 +00:00
2002-01-30 15:04:48 +00:00
log_print ( " Renamed \" %s \" to \" %s \" in volume group \" %s \" " ,
lv_name_old , lv_name_new , vg_name ) ;
2001-11-15 17:27:45 +00:00
2003-10-21 22:06:07 +00:00
return ECMD_PROCESSED ;
2002-02-11 15:42:34 +00:00
error :
2002-03-11 15:08:39 +00:00
unlock_vg ( cmd , vg_name ) ;
2002-02-11 15:42:34 +00:00
return ECMD_FAILED ;
2001-11-15 17:27:45 +00:00
}