2009-02-24 16:03:45 +03:00
/*
* Copyright ( C ) 2008 , 2009 Red Hat , Inc . All rights reserved .
*
* This file is part of LVM2 .
*
* 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 Lesser General Public License v .2 .1 .
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program ; if not , write to the Free Software Foundation ,
* Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
*/
# include "lib.h"
# include "toolcontext.h"
# include "locking.h"
2009-07-29 22:38:27 +04:00
# include "lvm-version.h"
2010-05-19 15:53:12 +04:00
# include "metadata-exported.h"
2010-12-15 02:20:58 +03:00
# include "lvm2app.h"
2013-12-18 02:51:11 +04:00
# include "lvm_misc.h"
2009-02-24 16:03:45 +03:00
2009-07-28 15:03:28 +04:00
const char * lvm_library_get_version ( void )
{
return LVM_VERSION ;
}
2013-12-18 02:51:11 +04:00
static lvm_t _lvm_init ( const char * system_dir )
2009-02-24 16:03:45 +03:00
{
struct cmd_context * cmd ;
/* FIXME: logging bound to handle
*/
2011-06-15 17:29:48 +04:00
if ( ! udev_init_library_context ( ) )
stack ;
2014-05-22 11:56:44 +04:00
/*
* It ' s not necessary to use name mangling for LVM :
* - the character set used for VG - LV names is subset of udev character set
2014-09-23 14:47:11 +04:00
* - when we check other devices ( e . g . device_is_usable fn ) , we use major : minor , not dm names
2014-05-22 11:56:44 +04:00
*/
dm_set_name_mangling_mode ( DM_STRING_MANGLING_NONE ) ;
2009-02-24 16:03:45 +03:00
/* create context */
/* FIXME: split create_toolcontext */
2010-03-17 17:45:28 +03:00
/* FIXME: make all globals configurable */
2012-08-26 03:15:45 +04:00
cmd = create_toolcontext ( 0 , system_dir , 0 , 0 ) ;
2009-02-24 16:03:45 +03:00
if ( ! cmd )
return NULL ;
2009-07-16 04:36:59 +04:00
if ( stored_errno ( ) )
return ( lvm_t ) cmd ;
2009-02-24 16:03:45 +03:00
/*
* FIXME : if an non memory error occured , return the cmd ( maybe some
* cleanup needed ) .
*/
/* initialization from lvm_run_command */
init_error_message_produced ( 0 ) ;
/* FIXME: locking_type config option needed? */
/* initialize locking */
2010-05-06 15:15:55 +04:00
if ( ! init_locking ( - 1 , cmd , 0 ) ) {
2009-02-24 16:03:45 +03:00
/* FIXME: use EAGAIN as error code here */
2009-07-28 13:16:18 +04:00
lvm_quit ( ( lvm_t ) cmd ) ;
2009-02-24 16:03:45 +03:00
return NULL ;
}
2009-07-14 07:01:18 +04:00
/*
* FIXME : Use cmd - > cmd_line as audit trail for liblvm calls . Used in
* archive ( ) call . Possible example :
* cmd_line = " lvm_vg_create: vg1 \n lvm_vg_extend vg1 /dev/sda1 \n "
*/
2010-12-20 16:28:04 +03:00
cmd - > cmd_line = " liblvm " ;
2009-02-24 16:03:45 +03:00
2013-04-03 01:10:02 +04:00
/*
2013-07-09 15:34:48 +04:00
* Turn off writing to stdout / stderr .
* FIXME Fix lib / to support a non - interactive mode instead .
2013-04-03 01:10:02 +04:00
*/
log_suppress ( 1 ) ;
2009-02-24 16:03:45 +03:00
return ( lvm_t ) cmd ;
}
2013-12-18 02:51:11 +04:00
lvm_t lvm_init ( const char * system_dir )
{
lvm_t h = NULL ;
struct saved_env e = store_user_env ( NULL ) ;
h = _lvm_init ( system_dir ) ;
restore_user_env ( & e ) ;
return h ;
}
2009-07-28 13:16:18 +04:00
void lvm_quit ( lvm_t libh )
2009-02-24 16:03:45 +03:00
{
2013-12-18 02:51:11 +04:00
struct saved_env e = store_user_env ( ( struct cmd_context * ) libh ) ;
2015-05-02 22:13:15 +03:00
fin_locking ( ) ;
2009-02-24 16:03:45 +03:00
destroy_toolcontext ( ( struct cmd_context * ) libh ) ;
2011-06-15 17:29:48 +04:00
udev_fin_library_context ( ) ;
2013-12-18 02:51:11 +04:00
restore_user_env ( & e ) ;
2009-02-24 16:03:45 +03:00
}
2009-07-27 14:18:51 +04:00
int lvm_config_reload ( lvm_t libh )
2009-02-24 16:03:45 +03:00
{
2013-12-18 02:51:11 +04:00
int rc = 0 ;
2009-02-24 16:03:45 +03:00
/* FIXME: re-init locking needed here? */
2013-12-18 02:51:11 +04:00
struct saved_env e = store_user_env ( ( struct cmd_context * ) libh ) ;
2009-07-28 01:02:35 +04:00
if ( ! refresh_toolcontext ( ( struct cmd_context * ) libh ) )
2013-12-18 02:51:11 +04:00
rc = - 1 ;
restore_user_env ( & e ) ;
return rc ;
2009-02-24 16:03:45 +03:00
}
2009-07-16 04:36:59 +04:00
2009-07-28 17:16:40 +04:00
/*
* FIXME : submit a patch to document the - - config option
*/
2009-07-28 01:02:17 +04:00
int lvm_config_override ( lvm_t libh , const char * config_settings )
{
2013-12-18 02:51:11 +04:00
int rc = 0 ;
2009-07-28 01:02:17 +04:00
struct cmd_context * cmd = ( struct cmd_context * ) libh ;
2013-12-18 02:51:11 +04:00
struct saved_env e = store_user_env ( ( struct cmd_context * ) libh ) ;
2012-10-16 12:07:27 +04:00
if ( ! override_config_tree_from_string ( cmd , config_settings ) )
2013-12-18 02:51:11 +04:00
rc = - 1 ;
restore_user_env ( & e ) ;
return rc ;
2009-07-28 01:02:17 +04:00
}
2012-07-31 18:18:01 +04:00
int lvm_config_find_bool ( lvm_t libh , const char * config_path , int fail )
{
2013-12-18 02:51:11 +04:00
int rc = 0 ;
2013-03-05 20:00:43 +04:00
struct cmd_context * cmd = ( struct cmd_context * ) libh ;
2013-12-18 02:51:11 +04:00
struct saved_env e = store_user_env ( ( struct cmd_context * ) libh ) ;
2013-03-05 20:00:43 +04:00
2013-12-18 02:51:11 +04:00
rc = dm_config_tree_find_bool ( cmd - > cft , config_path , fail ) ;
restore_user_env ( & e ) ;
return rc ;
2012-07-31 18:18:01 +04:00
}
2009-07-16 04:36:59 +04:00
int lvm_errno ( lvm_t libh )
{
2013-12-18 02:51:11 +04:00
int rc ;
struct saved_env e = store_user_env ( ( struct cmd_context * ) libh ) ;
rc = stored_errno ( ) ;
restore_user_env ( & e ) ;
return rc ;
2009-07-16 04:36:59 +04:00
}
const char * lvm_errmsg ( lvm_t libh )
{
2013-09-26 21:19:18 +04:00
const char * rc = NULL ;
struct cmd_context * cmd = ( struct cmd_context * ) libh ;
2013-12-18 02:51:11 +04:00
struct saved_env e = store_user_env ( ( struct cmd_context * ) libh ) ;
2013-09-26 21:19:18 +04:00
const char * msg = stored_errmsg_with_clear ( ) ;
if ( msg ) {
rc = dm_pool_strdup ( cmd - > mem , msg ) ;
free ( ( void * ) msg ) ;
}
2013-12-18 02:51:11 +04:00
restore_user_env ( & e ) ;
2013-09-26 21:19:18 +04:00
return rc ;
2009-07-16 04:36:59 +04:00
}
2010-05-19 15:53:12 +04:00
const char * lvm_vgname_from_pvid ( lvm_t libh , const char * pvid )
{
2013-12-18 02:51:11 +04:00
const char * rc = NULL ;
2010-05-19 15:53:12 +04:00
struct cmd_context * cmd = ( struct cmd_context * ) libh ;
2010-05-19 16:12:47 +04:00
struct id id ;
2013-12-18 02:51:11 +04:00
struct saved_env e = store_user_env ( ( struct cmd_context * ) libh ) ;
2010-05-19 15:53:12 +04:00
2013-12-18 02:51:11 +04:00
if ( id_read_format ( & id , pvid ) ) {
rc = find_vgname_from_pvid ( cmd , ( char * ) id . uuid ) ;
} else {
2010-05-19 15:53:12 +04:00
log_error ( INTERNAL_ERROR " Unable to convert uuid " ) ;
}
2013-12-18 02:51:11 +04:00
restore_user_env ( & e ) ;
return rc ;
2010-05-19 15:53:12 +04:00
}
const char * lvm_vgname_from_device ( lvm_t libh , const char * device )
{
2013-12-18 02:51:11 +04:00
const char * rc = NULL ;
2010-05-19 15:53:12 +04:00
struct cmd_context * cmd = ( struct cmd_context * ) libh ;
2013-12-18 02:51:11 +04:00
struct saved_env e = store_user_env ( cmd ) ;
rc = find_vgname_from_pvname ( cmd , device ) ;
restore_user_env ( & e ) ;
return rc ;
2010-05-19 15:53:12 +04:00
}
2012-10-11 19:25:14 +04:00
2013-12-18 02:51:11 +04:00
/*
* No context to work with , so no ability to save off and restore env is not
* available and is not needed .
*/
2012-10-11 19:25:14 +04:00
float lvm_percent_to_float ( percent_t v )
{
2014-06-09 14:08:27 +04:00
return dm_percent_to_float ( v ) ;
2012-10-11 19:25:14 +04:00
}