Imported Upstream version 2.02.79
[platform/upstream/device-mapper.git] / liblvm / lvm_base.c
1 /*
2  * Copyright (C) 2008,2009 Red Hat, Inc. All rights reserved.
3  *
4  * This file is part of LVM2.
5  *
6  * This copyrighted material is made available to anyone wishing to use,
7  * modify, copy, or redistribute it subject to the terms and conditions
8  * of the GNU Lesser General Public License v.2.1.
9  *
10  * You should have received a copy of the GNU Lesser General Public License
11  * along with this program; if not, write to the Free Software Foundation,
12  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
13  */
14
15 #include "lib.h"
16 #include "toolcontext.h"
17 #include "locking.h"
18 #include "lvm-version.h"
19 #include "metadata-exported.h"
20 #include "lvm2app.h"
21
22 const char *lvm_library_get_version(void)
23 {
24         return LVM_VERSION;
25 }
26
27 lvm_t lvm_init(const char *system_dir)
28 {
29         struct cmd_context *cmd;
30
31         /* FIXME: logging bound to handle
32          */
33
34         /* create context */
35         /* FIXME: split create_toolcontext */
36         /* FIXME: make all globals configurable */
37         cmd = create_toolcontext(0, system_dir);
38         if (!cmd)
39                 return NULL;
40
41         if (stored_errno())
42                 return (lvm_t) cmd;
43
44         /*
45          * FIXME: if an non memory error occured, return the cmd (maybe some
46          * cleanup needed).
47          */
48
49         /* initialization from lvm_run_command */
50         init_error_message_produced(0);
51
52         /* FIXME: locking_type config option needed? */
53         /* initialize locking */
54         if (!init_locking(-1, cmd, 0)) {
55                 /* FIXME: use EAGAIN as error code here */
56                 lvm_quit((lvm_t) cmd);
57                 return NULL;
58         }
59         /*
60          * FIXME: Use cmd->cmd_line as audit trail for liblvm calls.  Used in
61          * archive() call.  Possible example:
62          * cmd_line = "lvm_vg_create: vg1\nlvm_vg_extend vg1 /dev/sda1\n"
63          */
64         cmd->cmd_line = "liblvm";
65
66         return (lvm_t) cmd;
67 }
68
69 void lvm_quit(lvm_t libh)
70 {
71         destroy_toolcontext((struct cmd_context *)libh);
72 }
73
74 int lvm_config_reload(lvm_t libh)
75 {
76         /* FIXME: re-init locking needed here? */
77         if (!refresh_toolcontext((struct cmd_context *)libh))
78                 return -1;
79         return 0;
80 }
81
82 /*
83  * FIXME: submit a patch to document the --config option
84  */
85 int lvm_config_override(lvm_t libh, const char *config_settings)
86 {
87         struct cmd_context *cmd = (struct cmd_context *)libh;
88         if (override_config_tree_from_string(cmd, config_settings))
89                 return -1;
90         return 0;
91 }
92
93 int lvm_errno(lvm_t libh)
94 {
95         return stored_errno();
96 }
97
98 const char *lvm_errmsg(lvm_t libh)
99 {
100         return stored_errmsg();
101 }
102
103 const char *lvm_vgname_from_pvid(lvm_t libh, const char *pvid)
104 {
105         struct cmd_context *cmd = (struct cmd_context *)libh;
106         struct id id;
107
108         if (!id_read_format(&id, pvid)) {
109                 log_error(INTERNAL_ERROR "Unable to convert uuid");
110                 return NULL;
111         }
112         return find_vgname_from_pvid(cmd, (char *)id.uuid);
113 }
114
115 const char *lvm_vgname_from_device(lvm_t libh, const char *device)
116 {
117         struct cmd_context *cmd = (struct cmd_context *)libh;
118         return find_vgname_from_pvname(cmd, device);
119 }