2009-02-24 13:03:45 +00: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 18:38:27 +00:00
# include "lvm-version.h"
2010-05-19 11:53:12 +00:00
# include "metadata-exported.h"
2010-12-14 23:20:58 +00:00
# include "lvm2app.h"
2013-12-17 16:51:11 -06:00
# include "lvm_misc.h"
2009-02-24 13:03:45 +00:00
2009-07-28 11:03:28 +00:00
const char * lvm_library_get_version ( void )
{
return LVM_VERSION ;
}
2013-12-17 16:51:11 -06:00
static lvm_t _lvm_init ( const char * system_dir )
2009-02-24 13:03:45 +00:00
{
struct cmd_context * cmd ;
/* FIXME: logging bound to handle
*/
2011-06-15 13:29:48 +00:00
if ( ! udev_init_library_context ( ) )
stack ;
2014-05-22 09:56:44 +02: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 12:47:11 +02:00
* - when we check other devices ( e . g . device_is_usable fn ) , we use major : minor , not dm names
2014-05-22 09:56:44 +02:00
*/
dm_set_name_mangling_mode ( DM_STRING_MANGLING_NONE ) ;
2009-02-24 13:03:45 +00:00
/* create context */
/* FIXME: split create_toolcontext */
2010-03-17 14:45:28 +00:00
/* FIXME: make all globals configurable */
2012-08-26 00:15:45 +01:00
cmd = create_toolcontext ( 0 , system_dir , 0 , 0 ) ;
2009-02-24 13:03:45 +00:00
if ( ! cmd )
return NULL ;
2009-07-16 00:36:59 +00:00
if ( stored_errno ( ) )
return ( lvm_t ) cmd ;
2009-02-24 13:03:45 +00: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 11:15:55 +00:00
if ( ! init_locking ( - 1 , cmd , 0 ) ) {
2009-02-24 13:03:45 +00:00
/* FIXME: use EAGAIN as error code here */
2009-07-28 09:16:18 +00:00
lvm_quit ( ( lvm_t ) cmd ) ;
2009-02-24 13:03:45 +00:00
return NULL ;
}
2009-07-14 03:01:18 +00: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 13:28:04 +00:00
cmd - > cmd_line = " liblvm " ;
2009-02-24 13:03:45 +00:00
2013-04-02 17:10:02 -04:00
/*
2013-07-09 12:34:48 +01:00
* Turn off writing to stdout / stderr .
* FIXME Fix lib / to support a non - interactive mode instead .
2013-04-02 17:10:02 -04:00
*/
log_suppress ( 1 ) ;
2009-02-24 13:03:45 +00:00
return ( lvm_t ) cmd ;
}
2013-12-17 16:51:11 -06: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 09:16:18 +00:00
void lvm_quit ( lvm_t libh )
2009-02-24 13:03:45 +00:00
{
2013-12-17 16:51:11 -06:00
struct saved_env e = store_user_env ( ( struct cmd_context * ) libh ) ;
2015-05-02 21:13:15 +02:00
fin_locking ( ) ;
2009-02-24 13:03:45 +00:00
destroy_toolcontext ( ( struct cmd_context * ) libh ) ;
2011-06-15 13:29:48 +00:00
udev_fin_library_context ( ) ;
2013-12-17 16:51:11 -06:00
restore_user_env ( & e ) ;
2009-02-24 13:03:45 +00:00
}
2009-07-27 10:18:51 +00:00
int lvm_config_reload ( lvm_t libh )
2009-02-24 13:03:45 +00:00
{
2013-12-17 16:51:11 -06:00
int rc = 0 ;
2009-02-24 13:03:45 +00:00
/* FIXME: re-init locking needed here? */
2013-12-17 16:51:11 -06:00
struct saved_env e = store_user_env ( ( struct cmd_context * ) libh ) ;
2009-07-27 21:02:35 +00:00
if ( ! refresh_toolcontext ( ( struct cmd_context * ) libh ) )
2013-12-17 16:51:11 -06:00
rc = - 1 ;
restore_user_env ( & e ) ;
return rc ;
2009-02-24 13:03:45 +00:00
}
2009-07-16 00:36:59 +00:00
2009-07-28 13:16:40 +00:00
/*
* FIXME : submit a patch to document the - - config option
*/
2009-07-27 21:02:17 +00:00
int lvm_config_override ( lvm_t libh , const char * config_settings )
{
2013-12-17 16:51:11 -06:00
int rc = 0 ;
2009-07-27 21:02:17 +00:00
struct cmd_context * cmd = ( struct cmd_context * ) libh ;
2013-12-17 16:51:11 -06:00
struct saved_env e = store_user_env ( ( struct cmd_context * ) libh ) ;
2012-10-16 10:07:27 +02:00
if ( ! override_config_tree_from_string ( cmd , config_settings ) )
2013-12-17 16:51:11 -06:00
rc = - 1 ;
restore_user_env ( & e ) ;
return rc ;
2009-07-27 21:02:17 +00:00
}
2012-07-31 16:18:01 +02:00
int lvm_config_find_bool ( lvm_t libh , const char * config_path , int fail )
{
2013-12-17 16:51:11 -06:00
int rc = 0 ;
2013-03-05 17:00:43 +01:00
struct cmd_context * cmd = ( struct cmd_context * ) libh ;
2013-12-17 16:51:11 -06:00
struct saved_env e = store_user_env ( ( struct cmd_context * ) libh ) ;
2013-03-05 17:00:43 +01:00
2013-12-17 16:51:11 -06:00
rc = dm_config_tree_find_bool ( cmd - > cft , config_path , fail ) ;
restore_user_env ( & e ) ;
return rc ;
2012-07-31 16:18:01 +02:00
}
2009-07-16 00:36:59 +00:00
int lvm_errno ( lvm_t libh )
{
2013-12-17 16:51:11 -06: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 00:36:59 +00:00
}
const char * lvm_errmsg ( lvm_t libh )
{
2013-09-26 12:19:18 -05:00
const char * rc = NULL ;
struct cmd_context * cmd = ( struct cmd_context * ) libh ;
2013-12-17 16:51:11 -06:00
struct saved_env e = store_user_env ( ( struct cmd_context * ) libh ) ;
2013-09-26 12:19:18 -05:00
const char * msg = stored_errmsg_with_clear ( ) ;
if ( msg ) {
rc = dm_pool_strdup ( cmd - > mem , msg ) ;
free ( ( void * ) msg ) ;
}
2013-12-17 16:51:11 -06:00
restore_user_env ( & e ) ;
2013-09-26 12:19:18 -05:00
return rc ;
2009-07-16 00:36:59 +00:00
}
2010-05-19 11:53:12 +00:00
const char * lvm_vgname_from_pvid ( lvm_t libh , const char * pvid )
{
2013-12-17 16:51:11 -06:00
const char * rc = NULL ;
2010-05-19 11:53:12 +00:00
struct cmd_context * cmd = ( struct cmd_context * ) libh ;
2010-05-19 12:12:47 +00:00
struct id id ;
2013-12-17 16:51:11 -06:00
struct saved_env e = store_user_env ( ( struct cmd_context * ) libh ) ;
2010-05-19 11:53:12 +00:00
2013-12-17 16:51:11 -06:00
if ( id_read_format ( & id , pvid ) ) {
rc = find_vgname_from_pvid ( cmd , ( char * ) id . uuid ) ;
} else {
2010-05-19 11:53:12 +00:00
log_error ( INTERNAL_ERROR " Unable to convert uuid " ) ;
}
2013-12-17 16:51:11 -06:00
restore_user_env ( & e ) ;
return rc ;
2010-05-19 11:53:12 +00:00
}
const char * lvm_vgname_from_device ( lvm_t libh , const char * device )
{
2013-12-17 16:51:11 -06:00
const char * rc = NULL ;
2010-05-19 11:53:12 +00:00
struct cmd_context * cmd = ( struct cmd_context * ) libh ;
2013-12-17 16:51:11 -06: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 11:53:12 +00:00
}
2012-10-11 17:25:14 +02:00
2013-12-17 16:51:11 -06: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 17:25:14 +02:00
float lvm_percent_to_float ( percent_t v )
{
2014-06-09 12:08:27 +02:00
return dm_percent_to_float ( v ) ;
2012-10-11 17:25:14 +02:00
}