Debian Codegraph @

src/plugins/lvm/lvm-dbus.c

raw
1 /*
2 * Copyright (C) 2015-2016 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Author: Vratislav Podzimek <[email protected]>
18 */
19
20 #include <glib.h>
21 #include <math.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <blockdev/utils.h>
25 #include <gio/gio.h>
26 #include <libdevmapper.h>
27
28 #include "lvm.h"
29 #include "lvm-private.h"
30 #include "check_deps.h"
31 #include "dm_logging.h"
32 #include "vdo_stats.h"
33
34 #define LVM_BUS_NAME "com.redhat.lvmdbus1"
35 #define LVM_OBJ_PREFIX "/com/redhat/lvmdbus1"
36 #define MANAGER_OBJ "/com/redhat/lvmdbus1/Manager"
37 #define MANAGER_INTF "com.redhat.lvmdbus1.Manager"
38 #define JOB_OBJ_PREFIX "/com/redhat/lvmdbus1/Job/"
39 #define JOB_INTF "com.redhat.lvmdbus1.Job"
40 #define PV_OBJ_PREFIX LVM_OBJ_PREFIX"/Pv"
41 #define VG_OBJ_PREFIX LVM_OBJ_PREFIX"/Vg"
42 #define LV_OBJ_PREFIX LVM_OBJ_PREFIX"/Lv"
43 #define HIDDEN_LV_OBJ_PREFIX LVM_OBJ_PREFIX"/HiddenLv"
44 #define THIN_POOL_OBJ_PREFIX LVM_OBJ_PREFIX"/ThinPool"
45 #define CACHE_POOL_OBJ_PREFIX LVM_OBJ_PREFIX"/CachePool"
46 #define VDO_POOL_OBJ_PREFIX LVM_OBJ_PREFIX"/VdoPool"
47 #define PV_INTF LVM_BUS_NAME".Pv"
48 #define VG_INTF LVM_BUS_NAME".Vg"
49 #define VG_VDO_INTF LVM_BUS_NAME".VgVdo"
50 #define LV_CMN_INTF LVM_BUS_NAME".LvCommon"
51 #define LV_INTF LVM_BUS_NAME".Lv"
52 #define CACHED_LV_INTF LVM_BUS_NAME".CachedLv"
53 #define SNAP_INTF LVM_BUS_NAME".Snapshot"
54 #define THPOOL_INTF LVM_BUS_NAME".ThinPool"
55 #define CACHE_POOL_INTF LVM_BUS_NAME".CachePool"
56 #define VDO_POOL_INTF LVM_BUS_NAME".VdoPool"
57 #define DBUS_PROPS_IFACE "org.freedesktop.DBus.Properties"
58 #define DBUS_INTRO_IFACE "org.freedesktop.DBus.Introspectable"
59 #define METHOD_CALL_TIMEOUT 5000
60 #define PROGRESS_WAIT 500 * 1000 /* microseconds */
61
62
63 static GDBusConnection *bus = NULL;
64
65 /**
66 * SECTION: lvm
67 * @short_description: plugin for operations with LVM
68 * @title: LVM
69 * @include: lvm.h
70 *
71 * A plugin for operations with LVM. All sizes passed in/out to/from
72 * the functions are in bytes.
73 */
74
75 /**
76 * bd_lvm_error_quark: (skip)
77 */
78 GQuark bd_lvm_error_quark (void)
79 {
80 return g_quark_from_static_string ("g-bd-lvm-error-quark");
81 }
82
83 static gboolean setup_dbus_connection (GError **error) {
84 gchar *addr = NULL;
85
86 addr = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, NULL, error);
87 if (!addr) {
88 g_prefix_error (error, "Failed to get system bus address: ");
89 return FALSE;
90 }
91
92 bus = g_dbus_connection_new_for_address_sync (addr,
93 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT|
94 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
95 NULL, NULL, error);
96
97 g_free (addr);
98
99 if (!bus) {
100 g_prefix_error (error, "Failed to create a new connection for the system bus: ");
101 return FALSE;
102 }
103
104 if (g_dbus_connection_is_closed (bus)) {
105 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_FAIL,
106 "Connection is closed");
107 return FALSE;
108 }
109
110 g_dbus_connection_set_exit_on_close (bus, FALSE);
111
112 return TRUE;
113 }
114
115 static volatile guint avail_deps = 0;
116 static volatile guint avail_dbus_deps = 0;
117 static volatile guint avail_features = 0;
118 static volatile guint avail_module_deps = 0;
119 static GMutex deps_check_lock;
120
121 #define DEPS_LVM 0
122 #define DEPS_LVM_MASK (1 << DEPS_LVM)
123 #define DEPS_LVMDEVICES 1
124 #define DEPS_LVMDEVICES_MASK (1 << DEPS_LVMDEVICES)
125 #define DEPS_LVMCONFIG 2
126 #define DEPS_LVMCONFIG_MASK (1 << DEPS_LVMCONFIG)
127 #define DEPS_LAST 3
128
129 static const UtilDep deps[DEPS_LAST] = {
130 {"lvm", LVM_MIN_VERSION, "version", "LVM version:\\s+([\\d\\.]+)"},
131 {"lvmdevices", NULL, NULL, NULL},
132 {"lvmconfig", "2.03.17", "--version", "LVM version:\\s+([\\d\\.]+)"},
133 };
134
135 #define DBUS_DEPS_LVMDBUSD 0
136 #define DBUS_DEPS_LVMDBUSD_MASK (1 << DBUS_DEPS_LVMDBUSD)
137 #define DBUS_DEPS_LVMDBUSD_WRITECACHE 1
138 #define DBUS_DEPS_LVMDBUSD_WRITECACHE_MASK (1 << DBUS_DEPS_LVMDBUSD_WRITECACHE)
139 #define DBUS_DEPS_LAST 2
140
141 static const DBusDep dbus_deps[DBUS_DEPS_LAST] = {
142 {LVM_BUS_NAME, LVM_OBJ_PREFIX, G_BUS_TYPE_SYSTEM, NULL, NULL, NULL, NULL},
143 {LVM_BUS_NAME, LVM_OBJ_PREFIX, G_BUS_TYPE_SYSTEM, "1.1.0", "Version", MANAGER_INTF, MANAGER_OBJ},
144 };
145
146 #define FEATURES_VDO 0
147 #define FEATURES_VDO_MASK (1 << FEATURES_VDO)
148 #define FEATURES_WRITECACHE 1
149 #define FEATURES_WRITECACHE_MASK (1 << FEATURES_WRITECACHE)
150 #define FEATURES_LAST 2
151
152 static const UtilFeatureDep features[FEATURES_LAST] = {
153 {"lvm", "vdo", "segtypes", NULL},
154 {"lvm", "writecache", "segtypes", NULL},
155 };
156
157 #define MODULE_DEPS_VDO 0
158 #define MODULE_DEPS_VDO_MASK (1 << MODULE_DEPS_VDO)
159 #define MODULE_DEPS_LAST 1
160
161 static const gchar*const module_deps[MODULE_DEPS_LAST] = { "dm-vdo" };
162
163 /**
164 * bd_lvm_init:
165 *
166 * Initializes the plugin. **This function is called automatically by the
167 * library's initialization functions.**
168 *
169 */
170 gboolean bd_lvm_init (void) {
171 GError *error = NULL;
172
173 /* the check() call should create the DBus connection for us, but let's not
174 completely rely on it */
175 if (G_UNLIKELY (!bus) && !setup_dbus_connection (&error)) {
176 bd_utils_log_format (BD_UTILS_LOG_CRIT, "Failed to setup DBus connection: %s", error->message);
177 g_clear_error (&error);
178 return FALSE;
179 }
180
181 dm_log_with_errno_init ((dm_log_with_errno_fn) redirect_dm_log);
182 #ifdef DEBUG
183 dm_log_init_verbose (LOG_DEBUG);
184 #else
185 dm_log_init_verbose (LOG_INFO);
186 #endif
187
188 return TRUE;
189 }
190
191 /**
192 * bd_lvm_close:
193 *
194 * Cleans up after the plugin. **This function is called automatically by the
195 * library's functions that unload it.**
196 *
197 */
198 void bd_lvm_close (void) {
199 GError *error = NULL;
200
201 if (bus) {
202 if (!g_dbus_connection_flush_sync (bus, NULL, &error)) {
203 bd_utils_log_format (BD_UTILS_LOG_CRIT, "Failed to flush DBus connection: %s", error->message);
204 g_clear_error (&error);
205 }
206 if (!g_dbus_connection_close_sync (bus, NULL, &error)) {
207 bd_utils_log_format (BD_UTILS_LOG_CRIT, "Failed to close DBus connection: %s", error->message);
208 g_clear_error (&error);
209 }
210
211 g_clear_object (&bus);
212 }
213
214 dm_log_with_errno_init (NULL);
215 dm_log_init_verbose (0);
216
217 g_atomic_int_set (&avail_deps, 0);
218 g_atomic_int_set (&avail_dbus_deps, 0);
219 g_atomic_int_set (&avail_features, 0);
220 g_atomic_int_set (&avail_module_deps, 0);
221 }
222
223 /**
224 * bd_lvm_is_tech_avail:
225 * @tech: the queried tech
226 * @mode: a bit mask of queried modes of operation (#BDLVMTechMode) for @tech
227 * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
228 *
229 * Returns: whether the @tech-@mode combination is available -- supported by the
230 * plugin implementation and having all the runtime dependencies available
231 */
232 gboolean bd_lvm_is_tech_avail (BDLVMTech tech, guint64 mode, GError **error) {
233 switch (tech) {
234 case BD_LVM_TECH_THIN_CALCS:
235 if (mode & ~BD_LVM_TECH_MODE_QUERY) {
236 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_TECH_UNAVAIL,
237 "Only 'query' supported for thin calculations");
238 return FALSE;
239 } else
240 return TRUE;
241 case BD_LVM_TECH_CALCS:
242 if (mode & ~BD_LVM_TECH_MODE_QUERY) {
243 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_TECH_UNAVAIL,
244 "Only 'query' supported for calculations");
245 return FALSE;
246 } else
247 return TRUE;
248 case BD_LVM_TECH_VDO:
249 return check_dbus_deps (&avail_dbus_deps, DBUS_DEPS_LVMDBUSD_MASK, dbus_deps, DBUS_DEPS_LAST, &deps_check_lock, error) &&
250 check_features (&avail_features, FEATURES_VDO_MASK, features, FEATURES_LAST, &deps_check_lock, error) &&
251 check_module_deps (&avail_module_deps, MODULE_DEPS_VDO_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error);
252 case BD_LVM_TECH_WRITECACHE:
253 return check_dbus_deps (&avail_dbus_deps, DBUS_DEPS_LVMDBUSD_MASK|DBUS_DEPS_LVMDBUSD_WRITECACHE_MASK, dbus_deps, DBUS_DEPS_LAST, &deps_check_lock, error) &&
254 check_features (&avail_features, FEATURES_WRITECACHE_MASK, features, FEATURES_LAST, &deps_check_lock, error);
255 case BD_LVM_TECH_DEVICES:
256 return check_deps (&avail_deps, DEPS_LVMDEVICES_MASK, deps, DEPS_LAST, &deps_check_lock, error);
257 case BD_LVM_TECH_CONFIG:
258 return check_deps (&avail_deps, DEPS_LVMCONFIG_MASK, deps, DEPS_LAST, &deps_check_lock, error);
259 case BD_LVM_TECH_VG_CFG_BACKUP_RESTORE:
260 return check_deps (&avail_deps, DEPS_LVM_MASK, deps, DEPS_LAST, &deps_check_lock, error);
261 default:
262 /* everything is supported by this implementation of the plugin */
263 return check_dbus_deps (&avail_dbus_deps, DBUS_DEPS_LVMDBUSD_MASK, dbus_deps, DBUS_DEPS_LAST, &deps_check_lock, error);
264 }
265 }
266
267 static gchar** get_existing_objects (const gchar *obj_prefix, GError **error) {
268 GVariant *intro_v = NULL;
269 gchar *intro_data = NULL;
270 GDBusNodeInfo *info = NULL;
271 gchar **ret = NULL;
272 GDBusNodeInfo **nodes;
273 guint64 n_nodes = 0;
274 guint64 i = 0;
275
276 intro_v = g_dbus_connection_call_sync (bus, LVM_BUS_NAME, obj_prefix, DBUS_INTRO_IFACE,
277 "Introspect", NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
278 -1, NULL, error);
279 if (!intro_v)
280 /* no introspection data, something went wrong (error must be set) */
281 return NULL;
282
283 g_variant_get (intro_v, "(s)", &intro_data);
284 g_variant_unref (intro_v);
285 info = g_dbus_node_info_new_for_xml (intro_data, error);
286 g_free (intro_data);
287
288 for (nodes = info->nodes; (*nodes); nodes++)
289 n_nodes++;
290
291 ret = g_new0 (gchar*, n_nodes + 1);
292 for (nodes = info->nodes, i=0; (*nodes); nodes++, i++) {
293 ret[i] = g_strdup_printf ("%s/%s", obj_prefix, ((*nodes)->path));
294 }
295 ret[i] = NULL;
296
297 g_dbus_node_info_unref (info);
298
299 return ret;
300 }
301
302
303 /**
304 * get_object_path:
305 * @obj_id: get object path for an LVM object (vgname/lvname)
306 * @error: (out) (optional): place to store error (if any)
307 *
308 * Returns: (transfer full): object path
309 */
310 static gchar* get_object_path (const gchar *obj_id, GError **error) {
311 GVariant *args = NULL;
312 GVariant *ret = NULL;
313 gchar *obj_path = NULL;
314
315 if (!obj_id || g_strcmp0 (obj_id, "") == 0) {
316 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_NOEXIST,
317 "Invalid LVM ID specified");
318 return NULL;
319 }
320
321 args = g_variant_new ("(s)", obj_id);
322 /* consumes (frees) the 'args' parameter */
323 ret = g_dbus_connection_call_sync (bus, LVM_BUS_NAME, MANAGER_OBJ, MANAGER_INTF,
324 "LookUpByLvmId", args, NULL, G_DBUS_CALL_FLAGS_NONE,
325 -1, NULL, error);
326 if (!ret)
327 /* error is already set */
328 return NULL;
329
330 g_variant_get (ret, "(o)", &obj_path);
331 g_variant_unref (ret);
332
333 if (g_strcmp0 (obj_path, "/") == 0) {
334 /* not a valid path (at least for us) */
335 g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_NOEXIST,
336 "The object with LVM ID '%s' doesn't exist", obj_id);
337 g_free (obj_path);
338 return NULL;
339 }
340
341 return obj_path;
342 }
343
344 /**
345 * get_object_property:
346 * @obj_path: lvmdbusd object path
347 * @iface: interface on @obj_path object
348 * @property: property to get from @obj_path and @iface
349 * @error: (out) (optional): place to store error (if any)
350 *
351 * Returns: (transfer full): object path
352 */
353 static GVariant* get_object_property (const gchar *obj_path, const gchar *iface, const gchar *property, GError **error) {
354 GVariant *args = NULL;
355 GVariant *ret = NULL;
356 GVariant *real_ret = NULL;
357
358 args = g_variant_new ("(ss)", iface, property);
359
360 /* consumes (frees) the 'args' parameter */
361 ret = g_dbus_connection_call_sync (bus, LVM_BUS_NAME, obj_path, DBUS_PROPS_IFACE,
362 "Get", args, NULL, G_DBUS_CALL_FLAGS_NONE,
363 -1, NULL, error);
364 if (!ret) {
365 g_prefix_error (error, "Failed to get %s property of the %s object: ", property, obj_path);
366 return NULL;
367 }
368
369 g_variant_get (ret, "(v)", &real_ret);
370 g_variant_unref (ret);
371
372 return real_ret;
373 }
374
375 /**
376 * get_lvm_object_property:
377 * @obj_id: LVM object to get the property for (vgname/lvname)
378 * @iface: interface where @property is defined
379 * @property: property to get from @obj_id and @iface
380 * @error: (out) (optional): place to store error (if any)
381 *
382 * Returns: (transfer full): property variant
383 */
384 static GVariant* get_lvm_object_property (const gchar *obj_id, const gchar *iface, const gchar *property, GError **error) {
385 gchar *obj_path = NULL;
386 GVariant *ret = NULL;
387
388 obj_path = get_object_path (obj_id, error);
389 if (!obj_path)
390 /* error is already set */
391 return NULL;
392 else {
393 ret = get_object_property (obj_path, iface, property, error);
394 g_free (obj_path);
395 return ret;
396 }
397 }
398
399 static gboolean unbox_params_and_add (GVariant *params, GVariantBuilder *builder) {
400 GVariantIter iter;
401 GVariant *param = NULL;
402 gboolean ret = FALSE;
403
404 if (g_variant_is_of_type (params, G_VARIANT_TYPE_DICTIONARY)) {
405 g_variant_iter_init (&iter, params);
406 while ((param = g_variant_iter_next_value (&iter))) {
407 g_variant_builder_add_value (builder, param);
408 ret = TRUE;
409 }
410 return ret;
411 }
412
413 if (g_variant_is_of_type (params, G_VARIANT_TYPE_VARIANT)) {
414 param = g_variant_get_variant (params);
415 return unbox_params_and_add (param, builder);
416 }
417
418 if (g_variant_is_container (params)) {
419 g_variant_iter_init (&iter, params);
420 while ((param = g_variant_iter_next_value (&iter)))
421 ret = unbox_params_and_add (param, builder);
422 return ret;
423 }
424
425 return FALSE;
426 }
427
428 /**
429 * call_lvm_method
430 * @obj: lvmdbusd object path
431 * @intf: interface to call @method on
432 * @method: method to call
433 * @params: parameters for @method
434 * @extra_params: extra parameters for @method
435 * @extra_args: extra command line argument to be passed to the LVM command
436 * @task_id: (out): task ID to watch progress of the operation
437 * @progress_id: (out): progress ID to watch progress of the operation
438 * @lock_config: whether to lock %global_config_lock or not (if %FALSE is given, caller is responsible
439 * for holding the lock for this call)
440 * @error: (out) (optional): place to store error (if any)
441 *
442 * Returns: (transfer full): return value of @method (variant)
443 */
444 static GVariant* call_lvm_method (const gchar *obj, const gchar *intf, const gchar *method, GVariant *params, GVariant *extra_params, const BDExtraArg **extra_args, guint64 *task_id, guint64 *progress_id, gboolean lock_config, GError **error) {
445 GVariant *config = NULL;
446 GVariant *devices = NULL;
447 GVariant *param = NULL;
448 GVariantIter iter;
449 GVariantBuilder builder;
450 GVariantBuilder extra_builder;
451 GVariant *config_extra_params = NULL;
452 GVariant *tmo = NULL;
453 GVariant *all_params = NULL;
454 GVariant *ret = NULL;
455 gchar *params_str = NULL;
456 gchar *log_msg = NULL;
457 gchar *prog_msg = NULL;
458 const BDExtraArg **extra_p = NULL;
459 gboolean added_extra = FALSE;
460
461 if (!check_dbus_deps (&avail_dbus_deps, DBUS_DEPS_LVMDBUSD_MASK, dbus_deps, DBUS_DEPS_LAST, &deps_check_lock, error))
462 return NULL;
463
464 /* don't allow global config string changes during the run */
465 if (lock_config)
466 g_mutex_lock (&global_config_lock);
467
468 if (global_config_str || global_devices_str || extra_params || extra_args) {
469 if (global_config_str || global_devices_str || extra_args) {
470 /* add the global config to the extra_params */
471 g_variant_builder_init (&extra_builder, G_VARIANT_TYPE_DICTIONARY);
472
473 if (extra_params)
474 added_extra = unbox_params_and_add (extra_params, &extra_builder);
475
476 if (extra_args) {
477 for (extra_p=extra_args; *extra_p; extra_p++) {
478 g_variant_builder_add (&extra_builder, "{sv}",
479 (*extra_p)->opt ? (*extra_p)->opt : "",
480 g_variant_new ("s",
481 (*extra_p)->val ? (*extra_p)->val : ""));
482 added_extra = TRUE;
483 }
484 }
485 if (global_config_str) {
486 config = g_variant_new ("s", global_config_str);
487 g_variant_builder_add (&extra_builder, "{sv}", "--config", config);
488 added_extra = TRUE;
489 }
490 if (global_devices_str) {
491 devices = g_variant_new ("s", global_devices_str);
492 g_variant_builder_add (&extra_builder, "{sv}", "--devices", devices);
493 added_extra = TRUE;
494 }
495
496 if (added_extra)
497 config_extra_params = g_variant_builder_end (&extra_builder);
498 g_variant_builder_clear (&extra_builder);
499 } else
500 /* just use the extra_params */
501 config_extra_params = extra_params;
502 }
503
504 if (!config_extra_params)
505 /* create an empty dictionary with the extra arguments */
506 config_extra_params = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
507
508 /* create new GVariant holding the given parameters with the global
509 config and extra_params merged together appended */
510 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
511
512 if (params) {
513 /* add parameters */
514 g_variant_iter_init (&iter, params);
515 while ((param = g_variant_iter_next_value (&iter))) {
516 g_variant_builder_add_value (&builder, param);
517 g_variant_unref (param);
518 }
519 }
520
521 /* add the timeout spec (in seconds) */
522 tmo = g_variant_new ("i", 1);
523 g_variant_builder_add_value (&builder, tmo);
524
525 /* add extra parameters including config */
526 g_variant_builder_add_value (&builder, config_extra_params);
527
528 all_params = g_variant_builder_end (&builder);
529 g_variant_builder_clear (&builder);
530
531 params_str = g_variant_print (all_params, FALSE);
532
533 *task_id = bd_utils_get_next_task_id ();
534 log_msg = g_strdup_printf ("Calling the '%s.%s' method on the '%s' object with the following parameters: '%s'",
535 intf, method, obj, params_str);
536 bd_utils_log_task_status (*task_id, log_msg);
537 g_free (log_msg);
538
539 /* now do the call with all the parameters */
540 ret = g_dbus_connection_call_sync (bus, LVM_BUS_NAME, obj, intf, method, all_params,
541 NULL, G_DBUS_CALL_FLAGS_NONE, METHOD_CALL_TIMEOUT, NULL, error);
542
543 if (lock_config)
544 g_mutex_unlock (&global_config_lock);
545 prog_msg = g_strdup_printf ("Started the '%s.%s' method on the '%s' object with the following parameters: '%s'",
546 intf, method, obj, params_str);
547 g_free (params_str);
548 *progress_id = bd_utils_report_started (prog_msg);
549 g_free (prog_msg);
550
551 if (!ret) {
552 g_prefix_error (error, "Failed to call the '%s' method on the '%s' object: ", method, obj);
553 return NULL;
554 }
555
556 return ret;
557 }
558
559 /**
560 * call_lvm_method_sync
561 * @obj: lvmdbusd object path
562 * @intf: interface to call @method on
563 * @method: method to call
564 * @params: parameters for @method
565 * @extra_params: extra parameters for @method
566 * @extra_args: extra command line argument to be passed to the LVM command
567 * @lock_config: whether to lock %global_config_lock or not (if %FALSE is given, caller is responsible
568 * for holding the lock for this call)
569 * @error: (out) (optional): place to store error (if any)
570 *
571 * Returns: whether calling the method was successful or not
572 */
573 static gboolean call_lvm_method_sync (const gchar *obj, const gchar *intf, const gchar *method, GVariant *params, GVariant *extra_params, const BDExtraArg **extra_args, gboolean lock_config, GError **error) {
574 GVariant *ret = NULL;
575 gchar *obj_path = NULL;
576 g_autofree gchar *task_path = NULL;
577 guint64 log_task_id = 0;
578 guint64 prog_id = 0;
579 gdouble progress = 0.0;
580 gchar *log_msg = NULL;
581 gboolean completed = FALSE;
582 gint64 error_code = 0;
583 gchar *error_msg = NULL;
584 GError *l_error = NULL;
585
586 ret = call_lvm_method (obj, intf, method, params, extra_params, extra_args, &log_task_id, &prog_id, lock_config, &l_error);
587 bd_utils_log_task_status (log_task_id, "Done.");
588 if (!ret) {
589 if (l_error) {
590 log_msg = g_strdup_printf ("Got error: %s", l_error->message);
591 bd_utils_log_task_status (log_task_id, log_msg);
592 bd_utils_report_finished (prog_id, log_msg);
593 g_free (log_msg);
594 g_propagate_error (error, l_error);
595 } else {
596 bd_utils_log_task_status (log_task_id, "Got unknown error");
597 bd_utils_report_finished (prog_id, "Got unknown error");
598 }
599 return FALSE;
600 }
601 if (g_variant_check_format_string (ret, "((oo))", TRUE)) {
602 g_variant_get (ret, "((oo))", &obj_path, &task_path);
603 if (g_strcmp0 (obj_path, "/") != 0) {
604 log_msg = g_strdup_printf ("Got result: %s", obj_path);
605 bd_utils_log_task_status (log_task_id, log_msg);
606 g_free (log_msg);
607 /* got a valid result, just return */
608 g_variant_unref (ret);
609 g_free (obj_path);
610 bd_utils_report_finished (prog_id, "Completed");
611 return TRUE;
612 } else {
613 g_variant_unref (ret);
614 g_free (obj_path);
615 if (g_strcmp0 (task_path, "/") == 0) {
616 log_msg = g_strdup_printf ("Task finished without result and without job started");
617 g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_FAIL,
618 "Running '%s' method on the '%s' object failed: %s",
619 method, obj, log_msg);
620 bd_utils_log_task_status (log_task_id, log_msg);
621 bd_utils_report_finished (prog_id, log_msg);
622 g_free (log_msg);
623 return FALSE;
624 }
625 }
626 } else if (g_variant_check_format_string (ret, "(o)", TRUE)) {
627 g_variant_get (ret, "(o)", &task_path);
628 if (g_strcmp0 (task_path, "/") != 0) {
629 g_variant_unref (ret);
630 } else {
631 bd_utils_log_task_status (log_task_id, "No result, no job started");
632 bd_utils_report_finished (prog_id, "Completed");
633 g_variant_unref (ret);
634 return TRUE;
635 }
636 } else {
637 g_variant_unref (ret);
638 bd_utils_log_task_status (log_task_id, "Failed to parse the returned value!");
639 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_PARSE,
640 "Failed to parse the returned value!");
641 bd_utils_report_finished (prog_id, "Failed to parse the returned value!");
642 return FALSE;
643 }
644
645 log_msg = g_strdup_printf ("Waiting for job '%s' to finish", task_path);
646 bd_utils_log_task_status (log_task_id, log_msg);
647 g_free (log_msg);
648
649 ret = NULL;
650 while (!completed && !l_error) {
651 g_usleep (PROGRESS_WAIT);
652 ret = get_object_property (task_path, JOB_INTF, "Complete", &l_error);
653 if (ret) {
654 g_variant_get (ret, "b", &completed);
655 g_variant_unref (ret);
656 ret = NULL;
657 }
658 if (!completed && !l_error) {
659 /* let's report progress and wait longer */
660 ret = get_object_property (task_path, JOB_INTF, "Percent", &l_error);
661 if (ret) {
662 g_variant_get (ret, "d", &progress);
663 bd_utils_report_progress (prog_id, (gint) progress, NULL);
664 g_variant_unref (ret);
665 ret = NULL;
666 } else {
667 bd_utils_log_format (BD_UTILS_LOG_DEBUG, "Got error when getting progress: %s", l_error->message);
668 g_clear_error (&l_error);
669 }
670 log_msg = g_strdup_printf ("Still waiting for job '%s' to finish", task_path);
671 bd_utils_log_task_status (log_task_id, log_msg);
672 g_free (log_msg);
673 }
674
675 }
676 log_msg = g_strdup_printf ("Job '%s' finished", task_path);
677 bd_utils_log_task_status (log_task_id, log_msg);
678 g_free (log_msg);
679
680 obj_path = NULL;
681 if (!l_error) {
682 ret = get_object_property (task_path, JOB_INTF, "Result", &l_error);
683 if (!ret) {
684 g_prefix_error (&l_error, "Getting result after waiting for '%s' method of the '%s' object failed: ",
685 method, obj);
686 bd_utils_report_finished (prog_id, l_error->message);
687 g_propagate_error (error, l_error);
688 return FALSE;
689 } else {
690 g_variant_get (ret, "o", &obj_path);
691 g_variant_unref (ret);
692 if (g_strcmp0 (obj_path, "/") != 0) {
693 log_msg = g_strdup_printf ("Got result: %s", obj_path);
694 bd_utils_log_task_status (log_task_id, log_msg);
695 g_free (log_msg);
696 } else {
697 ret = get_object_property (task_path, JOB_INTF, "GetError", &l_error);
698 if (!ret) {
699 if (!l_error)
700 g_set_error (&l_error, BD_LVM_ERROR, BD_LVM_ERROR_FAIL,
701 "Failed to get error from '%s' method of the '%s' object",
702 method, obj);
703 bd_utils_report_finished (prog_id, l_error->message);
704 g_propagate_error (error, l_error);
705 return FALSE;
706 }
707 g_variant_get (ret, "(is)", &error_code, &error_msg);
708 if (error_code != 0) {
709 if (error_msg) {
710 log_msg = g_strdup_printf ("Got error: %s", error_msg);
711 bd_utils_log_task_status (log_task_id, log_msg);
712 bd_utils_report_finished (prog_id, log_msg);
713 g_set_error (&l_error, BD_LVM_ERROR, BD_LVM_ERROR_FAIL,
714 "Running '%s' method on the '%s' object failed: %s",
715 method, obj, error_msg);
716 g_free (log_msg);
717 g_free (error_msg);
718 } else {
719 bd_utils_log_task_status (log_task_id, "Got unknown error");
720 bd_utils_report_finished (prog_id, "Got unknown error");
721 g_set_error (&l_error, BD_LVM_ERROR, BD_LVM_ERROR_FAIL,
722 "Got unknown error when running '%s' method on the '%s' object.",
723 method, obj);
724 }
725 g_propagate_error (error, l_error);
726 return FALSE;
727 } else
728 bd_utils_log_task_status (log_task_id, "No result");
729 }
730 bd_utils_report_finished (prog_id, "Completed");
731 g_free (obj_path);
732
733 /* remove the job object and clean after ourselves */
734 ret = g_dbus_connection_call_sync (bus, LVM_BUS_NAME, task_path, JOB_INTF, "Remove", NULL,
735 NULL, G_DBUS_CALL_FLAGS_NONE, METHOD_CALL_TIMEOUT, NULL, NULL);
736 if (ret)
737 g_variant_unref (ret);
738
739 return TRUE;
740 }
741 } else {
742 /* some real error */
743 g_prefix_error (&l_error, "Waiting for '%s' method of the '%s' object to finish failed: ",
744 method, obj);
745 g_propagate_error (error, l_error);
746 bd_utils_report_finished (prog_id, "Completed");
747 return FALSE;
748 }
749 }
750
751 static gboolean call_lvm_obj_method_sync (const gchar *obj_id, const gchar *intf, const gchar *method, GVariant *params, GVariant *extra_params, const BDExtraArg **extra_args, gboolean lock_config, GError **error) {
752 g_autofree gchar *obj_path = get_object_path (obj_id, error);
753 if (!obj_path)
754 return FALSE;
755
756 return call_lvm_method_sync (obj_path, intf, method, params, extra_params, extra_args, lock_config, error);
757 }
758
759 static gboolean call_lv_method_sync (const gchar *vg_name, const gchar *lv_name, const gchar *method, GVariant *params, GVariant *extra_params, const BDExtraArg **extra_args, gboolean lock_config, GError **error) {
760 g_autofree gchar *obj_id = g_strdup_printf ("%s/%s", vg_name, lv_name);
761
762 return call_lvm_obj_method_sync (obj_id, LV_INTF, method, params, extra_params, extra_args, lock_config, error);
763 }
764
765 static gboolean call_thpool_method_sync (const gchar *vg_name, const gchar *pool_name, const gchar *method, GVariant *params, GVariant *extra_params, const BDExtraArg **extra_args, gboolean lock_config, GError **error) {
766 g_autofree gchar *obj_id = g_strdup_printf ("%s/%s", vg_name, pool_name);
767
768 return call_lvm_obj_method_sync (obj_id, THPOOL_INTF, method, params, extra_params, extra_args, lock_config, error);
769 }
770
771 static gboolean call_vdopool_method_sync (const gchar *vg_name, const gchar *pool_name, const gchar *method, GVariant *params, GVariant *extra_params, const BDExtraArg **extra_args, gboolean lock_config, GError **error) {
772 g_autofree gchar *obj_id = g_strdup_printf ("%s/%s", vg_name, pool_name);
773
774 return call_lvm_obj_method_sync (obj_id, VDO_POOL_INTF, method, params, extra_params, extra_args, lock_config, error);
775 }
776
777 static GVariant* get_lv_property (const gchar *vg_name, const gchar *lv_name, const gchar *property, GError **error) {
778 gchar *lv_spec = NULL;
779 GVariant *ret = NULL;
780
781 lv_spec = g_strdup_printf ("%s/%s", vg_name, lv_name);
782
783 ret = get_lvm_object_property (lv_spec, LV_CMN_INTF, property, error);
784 g_free (lv_spec);
785
786 return ret;
787 }
788
789 static GVariant* get_object_properties (const gchar *obj_path, const gchar *iface, GError **error) {
790 GVariant *args = NULL;
791 GVariant *ret = NULL;
792 GVariant *real_ret = NULL;
793
794 args = g_variant_new ("(s)", iface);
795
796 /* consumes (frees) the 'args' parameter */
797 ret = g_dbus_connection_call_sync (bus, LVM_BUS_NAME, obj_path, DBUS_PROPS_IFACE,
798 "GetAll", args, NULL, G_DBUS_CALL_FLAGS_NONE,
799 -1, NULL, error);
800 if (!ret) {
801 g_prefix_error (error, "Failed to get properties of the %s object: ", obj_path);
802 return NULL;
803 }
804
805 real_ret = g_variant_get_child_value (ret, 0);
806 g_variant_unref (ret);
807
808 return real_ret;
809 }
810
811 static GVariant* get_lvm_object_properties (const gchar *obj_id, const gchar *iface, GError **error) {
812 GVariant *args = NULL;
813 GVariant *ret = NULL;
814 gchar *obj_path = NULL;
815
816 if (!obj_id || g_strcmp0 (obj_id, "") == 0) {
817 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_NOEXIST,
818 "Invalid LVM ID specified");
819 return NULL;
820 }
821
822 args = g_variant_new ("(s)", obj_id);
823 /* consumes (frees) the 'args' parameter */
824 ret = g_dbus_connection_call_sync (bus, LVM_BUS_NAME, MANAGER_OBJ, MANAGER_INTF,
825 "LookUpByLvmId", args, NULL, G_DBUS_CALL_FLAGS_NONE,
826 -1, NULL, error);
827 if (!ret)
828 return NULL;
829 g_variant_get (ret, "(o)", &obj_path);
830 g_variant_unref (ret);
831
832 if (g_strcmp0 (obj_path, "/") == 0) {
833 g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_NOEXIST,
834 "The object with LVM ID '%s' doesn't exist", obj_id);
835 g_free (obj_path);
836 return NULL;
837 }
838
839 ret = get_object_properties (obj_path, iface, error);
840 g_free (obj_path);
841 return ret;
842 }
843
844
845 static GVariant* get_pv_properties (const gchar *pv_name, GError **error) {
846 gchar *obj_id = NULL;
847 GVariant *ret = NULL;
848
849 if (!pv_name || g_strcmp0 (pv_name, "") == 0) {
850 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_NOEXIST,
851 "Invalid LVM ID specified");
852 return NULL;
853 }
854
855 if (!g_str_has_prefix (pv_name, "/dev/")) {
856 obj_id = g_strdup_printf ("/dev/%s", pv_name);
857 ret = get_lvm_object_properties (obj_id, PV_INTF, error);
858 g_free (obj_id);
859 } else
860 ret = get_lvm_object_properties (pv_name, PV_INTF, error);
861
862 return ret;
863 }
864
865 static GVariant* get_vg_properties (const gchar *vg_name, GError **error) {
866 GVariant *ret = NULL;
867
868 ret = get_lvm_object_properties (vg_name, VG_INTF, error);
869
870 return ret;
871 }
872
873 static GVariant* get_lv_properties (const gchar *vg_name, const gchar *lv_name, GError **error) {
874 gchar *lvm_spec = NULL;
875 GVariant *ret = NULL;
876
877 lvm_spec = g_strdup_printf ("%s/%s", vg_name, lv_name);
878
879 ret = get_lvm_object_properties (lvm_spec, LV_CMN_INTF, error);
880 g_free (lvm_spec);
881
882 return ret;
883 }
884
885 static GVariant* get_vdo_properties (const gchar *vg_name, const gchar *pool_name, GError **error) {
886 gchar *lvm_spec = NULL;
887 GVariant *ret = NULL;
888
889 lvm_spec = g_strdup_printf ("%s/%s", vg_name, pool_name);
890
891 ret = get_lvm_object_properties (lvm_spec, VDO_POOL_INTF, error);
892 g_free (lvm_spec);
893
894 return ret;
895 }
896
897 static BDLVMPVdata* get_pv_data_from_props (GVariant *props, GError **error G_GNUC_UNUSED) {
898 BDLVMPVdata *data = g_new0 (BDLVMPVdata, 1);
899 GVariantDict dict;
900 gchar *path = NULL;
901 GVariant *vg_props = NULL;
902 GVariant *value = NULL;
903 gsize n_children = 0;
904 gsize i = 0;
905 gchar **tags = NULL;
906 GError *l_error = NULL;
907
908 g_variant_dict_init (&dict, props);
909
910 g_variant_dict_lookup (&dict, "Name", "s", &(data->pv_name));
911 g_variant_dict_lookup (&dict, "Uuid", "s", &(data->pv_uuid));
912 g_variant_dict_lookup (&dict, "FreeBytes", "t", &(data->pv_free));
913 g_variant_dict_lookup (&dict, "SizeBytes", "t", &(data->pv_size));
914 g_variant_dict_lookup (&dict, "PeStart", "t", &(data->pe_start));
915 g_variant_dict_lookup (&dict, "Missing", "b", &(data->missing));
916
917 value = g_variant_dict_lookup_value (&dict, "Tags", (GVariantType*) "as");
918 if (value) {
919 n_children = g_variant_n_children (value);
920 tags = g_new0 (gchar*, n_children + 1);
921 for (i=0; i < n_children; i++) {
922 g_variant_get_child (value, i, "s", tags+i);
923 }
924 data->pv_tags = tags;
925 g_variant_unref (value);
926 }
927
928 /* returns an object path for the VG */
929 g_variant_dict_lookup (&dict, "Vg", "&o", &path);
930 if (g_strcmp0 (path, "/") == 0) {
931 /* no VG, the PV is not part of any VG */
932 g_variant_dict_clear (&dict);
933 return data;
934 }
935
936 vg_props = get_object_properties (path, VG_INTF, &l_error);
937 g_variant_dict_clear (&dict);
938 if (!vg_props) {
939 if (l_error) {
940 bd_utils_log_format (BD_UTILS_LOG_DEBUG, "Failed to get VG properties for PV %s: %s",
941 data->pv_name, l_error->message);
942 g_clear_error (&l_error);
943 }
944 return data;
945 }
946
947 g_variant_dict_init (&dict, vg_props);
948 g_variant_dict_lookup (&dict, "Name", "s", &(data->vg_name));
949 g_variant_dict_lookup (&dict, "Uuid", "s", &(data->vg_uuid));
950 g_variant_dict_lookup (&dict, "SizeBytes", "t", &(data->vg_size));
951 g_variant_dict_lookup (&dict, "FreeBytes", "t", &(data->vg_free));
952 g_variant_dict_lookup (&dict, "ExtentSizeBytes", "t", &(data->vg_extent_size));
953 g_variant_dict_lookup (&dict, "ExtentCount", "t", &(data->vg_extent_count));
954 g_variant_dict_lookup (&dict, "FreeCount", "t", &(data->vg_free_count));
955 g_variant_dict_lookup (&dict, "PvCount", "t", &(data->vg_pv_count));
956
957 g_variant_dict_clear (&dict);
958 g_variant_unref (vg_props);
959
960 return data;
961 }
962
963 static BDLVMVGdata* get_vg_data_from_props (GVariant *props, GError **error G_GNUC_UNUSED) {
964 BDLVMVGdata *data = g_new0 (BDLVMVGdata, 1);
965 GVariantDict dict;
966 GVariant *value = NULL;
967 gsize n_children = 0;
968 gsize i = 0;
969 gchar **tags = NULL;
970
971 g_variant_dict_init (&dict, props);
972
973 g_variant_dict_lookup (&dict, "Name", "s", &(data->name));
974 g_variant_dict_lookup (&dict, "Uuid", "s", &(data->uuid));
975
976 g_variant_dict_lookup (&dict, "SizeBytes", "t", &(data->size));
977 g_variant_dict_lookup (&dict, "FreeBytes", "t", &(data->free));
978 g_variant_dict_lookup (&dict, "ExtentSizeBytes", "t", &(data->extent_size));
979 g_variant_dict_lookup (&dict, "ExtentCount", "t", &(data->extent_count));
980 g_variant_dict_lookup (&dict, "FreeCount", "t", &(data->free_count));
981 g_variant_dict_lookup (&dict, "PvCount", "t", &(data->pv_count));
982 g_variant_dict_lookup (&dict, "Exportable", "b", &(data->exported));
983
984 value = g_variant_dict_lookup_value (&dict, "Tags", (GVariantType*) "as");
985 if (value) {
986 n_children = g_variant_n_children (value);
987 tags = g_new0 (gchar*, n_children + 1);
988 for (i=0; i < n_children; i++) {
989 g_variant_get_child (value, i, "s", tags+i);
990 }
991 data->vg_tags = tags;
992 g_variant_unref (value);
993 }
994
995 g_variant_dict_clear (&dict);
996
997 return data;
998 }
999
1000 static gchar* _lvm_data_lv_name (const gchar *vg_name, const gchar *lv_name, GError **error) {
1001 GVariant *prop = NULL;
1002 gchar *obj_id = NULL;
1003 gchar *obj_path = NULL;
1004 gchar *ret = NULL;
1005 gchar *segtype = NULL;
1006
1007 obj_id = g_strdup_printf ("%s/%s", vg_name, lv_name);
1008 obj_path = get_object_path (obj_id, error);
1009 g_free (obj_id);
1010 if (!obj_path)
1011 return NULL;
1012
1013 prop = get_lv_property (vg_name, lv_name, "SegType", error);
1014 if (!prop)
1015 return NULL;
1016 g_variant_get_child (prop, 0, "s", &segtype);
1017 g_variant_unref (prop);
1018
1019 if (g_strcmp0 (segtype, "thin-pool") == 0)
1020 prop = get_object_property (obj_path, THPOOL_INTF, "DataLv", NULL);
1021 else if (g_strcmp0 (segtype, "cache-pool") == 0)
1022 prop = get_object_property (obj_path, CACHE_POOL_INTF, "DataLv", NULL);
1023 else if (g_strcmp0 (segtype, "vdo-pool") == 0)
1024 prop = get_object_property (obj_path, VDO_POOL_INTF, "DataLv", NULL);
1025
1026 g_free (segtype);
1027 g_free (obj_path);
1028 if (!prop)
1029 return NULL;
1030 g_variant_get (prop, "o", &obj_path);
1031 g_variant_unref (prop);
1032
1033 if (g_strcmp0 (obj_path, "/") == 0) {
1034 /* no origin LV */
1035 g_free (obj_path);
1036 return NULL;
1037 }
1038 prop = get_object_property (obj_path, LV_CMN_INTF, "Name", error);
1039 if (!prop) {
1040 g_free (obj_path);
1041 return NULL;
1042 }
1043
1044 g_variant_get (prop, "s", &ret);
1045 g_variant_unref (prop);
1046
1047 return g_strstrip (g_strdelimit (ret, "[]", ' '));
1048 }
1049
1050 static gchar* _lvm_metadata_lv_name (const gchar *vg_name, const gchar *lv_name, GError **error) {
1051 GVariant *prop = NULL;
1052 gchar *obj_id = NULL;
1053 gchar *obj_path = NULL;
1054 gchar *ret = NULL;
1055
1056 obj_id = g_strdup_printf ("%s/%s", vg_name, lv_name);
1057 obj_path = get_object_path (obj_id, error);
1058 g_free (obj_id);
1059 if (!obj_path)
1060 return NULL;
1061
1062 prop = get_object_property (obj_path, THPOOL_INTF, "MetaDataLv", NULL);
1063 if (!prop)
1064 prop = get_object_property (obj_path, CACHE_POOL_INTF, "MetaDataLv", NULL);
1065 g_free (obj_path);
1066 if (!prop)
1067 return NULL;
1068 g_variant_get (prop, "o", &obj_path);
1069 g_variant_unref (prop);
1070
1071 if (g_strcmp0 (obj_path, "/") == 0) {
1072 /* no origin LV */
1073 g_free (obj_path);
1074 return NULL;
1075 }
1076 prop = get_object_property (obj_path, LV_CMN_INTF, "Name", error);
1077 if (!prop) {
1078 g_free (obj_path);
1079 return NULL;
1080 }
1081
1082 g_variant_get (prop, "s", &ret);
1083 g_variant_unref (prop);
1084
1085 return g_strstrip (g_strdelimit (ret, "[]", ' '));
1086 }
1087
1088 static BDLVMSEGdata** _lvm_segs (const gchar *vg_name, const gchar *lv_name, GError **error) {
1089 GVariant *prop = NULL;
1090 BDLVMSEGdata **segs;
1091 gsize n_segs;
1092 GVariantIter iter, iter2;
1093 GVariant *pv_segs, *pv_name_prop;
1094 const gchar *pv;
1095 gchar *pv_name;
1096 guint64 pv_first_pe, pv_last_pe;
1097 int i;
1098
1099 prop = get_lv_property (vg_name, lv_name, "Devices", error);
1100 if (!prop)
1101 return NULL;
1102
1103 /* Count number of segments */
1104 n_segs = 0;
1105 g_variant_iter_init (&iter, prop);
1106 while (g_variant_iter_next (&iter, "(&o@a(tts))", NULL, &pv_segs)) {
1107 n_segs += g_variant_n_children (pv_segs);
1108 g_variant_unref (pv_segs);
1109 }
1110
1111 if (n_segs == 0) {
1112 g_variant_unref (prop);
1113 return NULL;
1114 }
1115
1116 /* Build segments */
1117 segs = g_new0 (BDLVMSEGdata *, n_segs+1);
1118 i = 0;
1119 g_variant_iter_init (&iter, prop);
1120 while (g_variant_iter_next (&iter, "(&o@a(tts))", &pv, &pv_segs)) {
1121 pv_name_prop = get_object_property (pv, PV_INTF, "Name", NULL);
1122 if (pv_name_prop) {
1123 g_variant_get (pv_name_prop, "&s", &pv_name);
1124 g_variant_iter_init (&iter2, pv_segs);
1125 while (g_variant_iter_next (&iter2, "(tt&s)", &pv_first_pe, &pv_last_pe, NULL)) {
1126 BDLVMSEGdata *seg = g_new0(BDLVMSEGdata, 1);
1127 seg->pv_start_pe = pv_first_pe;
1128 seg->size_pe = pv_last_pe - pv_first_pe + 1;
1129 seg->pvdev = g_strdup (pv_name);
1130 segs[i++] = seg;
1131 }
1132 g_variant_unref (pv_name_prop);
1133 }
1134 g_variant_unref (pv_segs);
1135 }
1136
1137 g_variant_unref (prop);
1138 return segs;
1139 }
1140
1141 static void _lvm_data_and_metadata_lvs (const gchar *vg_name, const gchar *lv_name,
1142 gchar ***data_lvs_ret, gchar ***metadata_lvs_ret,
1143 GError **error) {
1144 GVariant *prop;
1145 gsize n_hidden_lvs;
1146 gchar **data_lvs;
1147 gchar **metadata_lvs;
1148 GVariantIter iter, iter2;
1149 int i_data;
1150 int i_metadata;
1151 const gchar *sublv;
1152 GVariant *sublv_roles_prop;
1153 GVariant *sublv_name_prop;
1154 gchar *sublv_name;
1155 const gchar *role;
1156
1157 prop = get_lv_property (vg_name, lv_name, "HiddenLvs", error);
1158 if (!prop) {
1159 *data_lvs_ret = NULL;
1160 *metadata_lvs_ret = NULL;
1161 return;
1162 }
1163
1164 n_hidden_lvs = g_variant_n_children (prop);
1165 data_lvs = g_new0(gchar *, n_hidden_lvs + 1);
1166 metadata_lvs = g_new0(gchar *, n_hidden_lvs + 1);
1167
1168 i_data = 0;
1169 i_metadata = 0;
1170 g_variant_iter_init (&iter, prop);
1171 while (g_variant_iter_next (&iter, "&o", &sublv)) {
1172 sublv_roles_prop = get_object_property (sublv, LV_INTF, "Roles", NULL);
1173 if (sublv_roles_prop) {
1174 sublv_name_prop = get_object_property (sublv, LV_INTF, "Name", NULL);
1175 if (sublv_name_prop) {
1176 g_variant_get (sublv_name_prop, "s", &sublv_name);
1177 if (sublv_name) {
1178 sublv_name = g_strstrip (g_strdelimit (sublv_name, "[]", ' '));
1179 g_variant_iter_init (&iter2, sublv_roles_prop);
1180 while (g_variant_iter_next (&iter2, "&s", &role)) {
1181 if (g_strcmp0 (role, "image") == 0) {
1182 data_lvs[i_data++] = sublv_name;
1183 sublv_name = NULL;
1184 break;
1185 } else if (g_strcmp0 (role, "metadata") == 0) {
1186 metadata_lvs[i_metadata++] = sublv_name;
1187 sublv_name = NULL;
1188 break;
1189 }
1190 }
1191 g_free (sublv_name);
1192 }
1193 g_variant_unref (sublv_name_prop);
1194 }
1195 g_variant_unref (sublv_roles_prop);
1196 }
1197 }
1198
1199 *data_lvs_ret = data_lvs;
1200 *metadata_lvs_ret = metadata_lvs;
1201
1202 g_variant_unref (prop);
1203 return;
1204 }
1205
1206 static BDLVMLVdata* get_lv_data_from_props (GVariant *props, GError **error G_GNUC_UNUSED) {
1207 BDLVMLVdata *data = g_new0 (BDLVMLVdata, 1);
1208 GVariantDict dict;
1209 GVariant *value = NULL;
1210 gchar *path = NULL;
1211 GVariant *name = NULL;
1212 gsize n_children = 0;
1213 gsize i = 0;
1214 gchar **roles = NULL;
1215 gchar **tags = NULL;
1216
1217 g_variant_dict_init (&dict, props);
1218
1219 g_variant_dict_lookup (&dict, "Name", "s", &(data->lv_name));
1220 g_variant_dict_lookup (&dict, "Uuid", "s", &(data->uuid));
1221 g_variant_dict_lookup (&dict, "Attr", "s", &(data->attr));
1222 g_variant_dict_lookup (&dict, "SizeBytes", "t", &(data->size));
1223 g_variant_dict_lookup (&dict, "DataPercent", "u", &(data->data_percent));
1224 g_variant_dict_lookup (&dict, "MetaDataPercent", "u", &(data->metadata_percent));
1225 g_variant_dict_lookup (&dict, "CopyPercent", "u", &(data->copy_percent));
1226
1227 /* XXX: how to deal with LVs with multiple segment types? We are just taking
1228 the first one now. */
1229 value = g_variant_dict_lookup_value (&dict, "SegType", (GVariantType*) "as");
1230 if (value) {
1231 const gchar *st;
1232 g_variant_get_child (value, 0, "&s", &st);
1233 if (g_strcmp0 (st, "error") == 0)
1234 st = "linear";
1235 data->segtype = g_strdup (st);
1236 g_variant_unref (value);
1237 }
1238
1239 value = g_variant_dict_lookup_value (&dict, "Roles", (GVariantType*) "as");
1240 if (value) {
1241 n_children = g_variant_n_children (value);
1242 roles = g_new0 (gchar*, n_children + 1);
1243 for (i=0; i < n_children; i++)
1244 g_variant_get_child (value, i, "&s", roles+i);
1245 data->roles = g_strjoinv (",", roles);
1246 g_free (roles);
1247 g_variant_unref (value);
1248 }
1249
1250 /* returns an object path for the VG */
1251 g_variant_dict_lookup (&dict, "Vg", "o", &path);
1252 name = get_object_property (path, VG_INTF, "Name", NULL);
1253 g_free (path);
1254 if (name) {
1255 g_variant_get (name, "s", &(data->vg_name));
1256 g_variant_unref (name);
1257 }
1258
1259 g_variant_dict_lookup (&dict, "OriginLv", "o", &path);
1260 if (g_strcmp0 (path, "/") != 0) {
1261 name = get_object_property (path, LV_CMN_INTF, "Name", NULL);
1262 if (name) {
1263 g_variant_get (name, "s", &(data->origin));
1264 g_variant_unref (name);
1265 }
1266 }
1267 g_free (path);
1268 path = NULL;
1269
1270 g_variant_dict_lookup (&dict, "PoolLv", "o", &path);
1271 if (g_strcmp0 (path, "/") != 0) {
1272 name = get_object_property (path, LV_CMN_INTF, "Name", NULL);
1273 if (name) {
1274 g_variant_get (name, "s", &(data->pool_lv));
1275 g_variant_unref (name);
1276 }
1277 }
1278 g_free (path);
1279 path = NULL;
1280
1281 g_variant_dict_lookup (&dict, "MovePv", "o", &path);
1282 if (path && g_strcmp0 (path, "/") != 0) {
1283 name = get_object_property (path, PV_INTF, "Name", NULL);
1284 if (name) {
1285 g_variant_get (name, "s", &(data->move_pv));
1286 g_variant_unref (name);
1287 }
1288 }
1289 g_free (path);
1290 path = NULL;
1291
1292 value = g_variant_dict_lookup_value (&dict, "Tags", (GVariantType*) "as");
1293 if (value) {
1294 n_children = g_variant_n_children (value);
1295 tags = g_new0 (gchar*, n_children + 1);
1296 for (i=0; i < n_children; i++) {
1297 g_variant_get_child (value, i, "s", tags+i);
1298 }
1299 data->lv_tags = tags;
1300 g_variant_unref (value);
1301 }
1302
1303 g_variant_dict_clear (&dict);
1304 g_variant_unref (props);
1305
1306 return data;
1307 }
1308
1309 static BDLVMVDOPooldata* get_vdo_data_from_props (GVariant *props, GError **error G_GNUC_UNUSED) {
1310 BDLVMVDOPooldata *data = g_new0 (BDLVMVDOPooldata, 1);
1311 GVariantDict dict;
1312 gchar *value = NULL;
1313 gdouble percent = 0;
1314
1315 g_variant_dict_init (&dict, props);
1316
1317 g_variant_dict_lookup (&dict, "OperatingMode", "s", &value);
1318 if (g_strcmp0 (value, "recovering") == 0)
1319 data->operating_mode = BD_LVM_VDO_MODE_RECOVERING;
1320 else if (g_strcmp0 (value, "read-only") == 0)
1321 data->operating_mode = BD_LVM_VDO_MODE_READ_ONLY;
1322 else if (g_strcmp0 (value, "normal") == 0)
1323 data->operating_mode = BD_LVM_VDO_MODE_NORMAL;
1324 else {
1325 bd_utils_log_format (BD_UTILS_LOG_DEBUG, "Unknown VDO operating mode: %s", value);
1326 data->operating_mode = BD_LVM_VDO_MODE_UNKNOWN;
1327 }
1328 g_free (value);
1329 value = NULL;
1330
1331 g_variant_dict_lookup (&dict, "CompressionState", "s", &value);
1332 if (g_strcmp0 (value, "online") == 0)
1333 data->compression_state = BD_LVM_VDO_COMPRESSION_ONLINE;
1334 else if (g_strcmp0 (value, "offline") == 0)
1335 data->compression_state = BD_LVM_VDO_COMPRESSION_OFFLINE;
1336 else {
1337 bd_utils_log_format (BD_UTILS_LOG_DEBUG, "Unknown VDO compression state: %s", value);
1338 data->compression_state = BD_LVM_VDO_COMPRESSION_UNKNOWN;
1339 }
1340 g_free (value);
1341 value = NULL;
1342
1343 g_variant_dict_lookup (&dict, "IndexState", "s", &value);
1344 if (g_strcmp0 (value, "error") == 0)
1345 data->index_state = BD_LVM_VDO_INDEX_ERROR;
1346 else if (g_strcmp0 (value, "closed") == 0)
1347 data->index_state = BD_LVM_VDO_INDEX_CLOSED;
1348 else if (g_strcmp0 (value, "opening") == 0)
1349 data->index_state = BD_LVM_VDO_INDEX_OPENING;
1350 else if (g_strcmp0 (value, "closing") == 0)
1351 data->index_state = BD_LVM_VDO_INDEX_CLOSING;
1352 else if (g_strcmp0 (value, "offline") == 0)
1353 data->index_state = BD_LVM_VDO_INDEX_OFFLINE;
1354 else if (g_strcmp0 (value, "online") == 0)
1355 data->index_state = BD_LVM_VDO_INDEX_ONLINE;
1356 else {
1357 bd_utils_log_format (BD_UTILS_LOG_DEBUG, "Unknown VDO index state: %s", value);
1358 data->index_state = BD_LVM_VDO_INDEX_UNKNOWN;
1359 }
1360 g_free (value);
1361 value = NULL;
1362
1363 g_variant_dict_lookup (&dict, "WritePolicy", "s", &value);
1364 if (g_strcmp0 (value, "auto") == 0)
1365 data->write_policy = BD_LVM_VDO_WRITE_POLICY_AUTO;
1366 else if (g_strcmp0 (value, "sync") == 0)
1367 data->write_policy = BD_LVM_VDO_WRITE_POLICY_SYNC;
1368 else if (g_strcmp0 (value, "async") == 0)
1369 data->write_policy = BD_LVM_VDO_WRITE_POLICY_ASYNC;
1370 else {
1371 bd_utils_log_format (BD_UTILS_LOG_DEBUG, "Unknown VDO write policy: %s", value);
1372 data->write_policy = BD_LVM_VDO_WRITE_POLICY_UNKNOWN;
1373 }
1374 g_free (value);
1375 value = NULL;
1376
1377 g_variant_dict_lookup (&dict, "UsedSize", "t", &(data->used_size));
1378 g_variant_dict_lookup (&dict, "SavingPercent", "d", &percent);
1379 data->saving_percent = (int) percent;
1380
1381 g_variant_dict_lookup (&dict, "IndexMemorySize", "t", &(data->index_memory_size));
1382
1383 g_variant_dict_lookup (&dict, "Compression", "s", &value);
1384 if (value && g_strcmp0 (value, "enabled") == 0)
1385 data->compression = TRUE;
1386 else
1387 data->compression = FALSE;
1388 g_free (value);
1389 value = NULL;
1390
1391 g_variant_dict_lookup (&dict, "Deduplication", "s", &value);
1392 if (value && g_strcmp0 (value, "enabled") == 0)
1393 data->deduplication = TRUE;
1394 else
1395 data->deduplication = FALSE;
1396 g_free (value);
1397 value = NULL;
1398
1399 g_variant_dict_clear (&dict);
1400 g_variant_unref (props);
1401
1402 return data;
1403 }
1404
1405 static GVariant* create_size_str_param (guint64 size, const gchar *unit) {
1406 gchar *str = NULL;
1407
1408 str = g_strdup_printf ("%"G_GUINT64_FORMAT"%s", size, unit ? unit : "");
1409 return g_variant_new_take_string (str);
1410 }
1411
1412 /**
1413 * bd_lvm_pvcreate:
1414 * @device: the device to make PV from
1415 * @data_alignment: data (first PE) alignment or 0 to use the default
1416 * @metadata_size: size of the area reserved for metadata or 0 to use the default
1417 * @extra: (nullable) (array zero-terminated=1): extra options for the PV creation
1418 * (just passed to LVM as is)
1419 * @error: (out) (optional): place to store error (if any)
1420 *
1421 * Returns: whether the PV was successfully created or not
1422 *
1423 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_CREATE
1424 */
1425 gboolean bd_lvm_pvcreate (const gchar *device, guint64 data_alignment, guint64 metadata_size, const BDExtraArg **extra, GError **error) {
1426 GVariantBuilder builder;
1427 GVariant *param = NULL;
1428 GVariant *params = NULL;
1429 GVariant *extra_params = NULL;
1430
1431 if (!device || g_strcmp0 (device, "") == 0) {
1432 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_NOEXIST,
1433 "Invalid LVM ID specified");
1434 return FALSE;
1435 }
1436
1437 if (data_alignment != 0 || metadata_size != 0) {
1438 g_variant_builder_init (&builder, G_VARIANT_TYPE_DICTIONARY);
1439 if (data_alignment != 0) {
1440 param = create_size_str_param (data_alignment, "b");
1441 g_variant_builder_add (&builder, "{sv}", "dataalignment", param);
1442 }
1443
1444 if (metadata_size != 0) {
1445 param = create_size_str_param (metadata_size, "b");
1446 g_variant_builder_add (&builder, "{sv}", "metadatasize", param);
1447 }
1448 extra_params = g_variant_builder_end (&builder);
1449 g_variant_builder_clear (&builder);
1450 }
1451
1452 params = g_variant_new ("(s)", device);
1453
1454 return call_lvm_method_sync (MANAGER_OBJ, MANAGER_INTF, "PvCreate", params, extra_params, extra, TRUE, error);
1455 }
1456
1457 /**
1458 * bd_lvm_pvresize:
1459 * @device: the device to resize
1460 * @size: the new requested size of the PV or 0 if it should be adjusted to device's size
1461 * @extra: (nullable) (array zero-terminated=1): extra options for the PV resize
1462 * (just passed to LVM as is)
1463 * @error: (out) (optional): place to store error (if any)
1464 *
1465 * Returns: whether the PV's size was successfully changed or not
1466 *
1467 * If given @size different from 0, sets the PV's size to the given value (see
1468 * pvresize(8)). If given @size 0, adjusts the PV's size to the underlying
1469 * block device's size.
1470 *
1471 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1472 */
1473 gboolean bd_lvm_pvresize (const gchar *device, guint64 size, const BDExtraArg **extra, GError **error) {
1474 GVariant *params = NULL;
1475 gchar *obj_path = get_object_path (device, error);
1476 if (!obj_path)
1477 return FALSE;
1478
1479 params = g_variant_new ("(t)", size);
1480 return call_lvm_method_sync (obj_path, PV_INTF, "ReSize", params, NULL, extra, TRUE, error);
1481 }
1482
1483 /**
1484 * bd_lvm_pvremove:
1485 * @device: the PV device to be removed/destroyed
1486 * @extra: (nullable) (array zero-terminated=1): extra options for the PV removal
1487 * (just passed to LVM as is)
1488 * @error: (out) (optional): place to store error (if any)
1489 *
1490 * Returns: whether the PV was successfully removed/destroyed or not
1491 *
1492 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_REMOVE
1493 */
1494 gboolean bd_lvm_pvremove (const gchar *device, const BDExtraArg **extra, GError **error) {
1495 GVariantBuilder builder;
1496 GVariant *params = NULL;
1497 GError *l_error = NULL;
1498 gboolean ret = FALSE;
1499
1500 if (!device || g_strcmp0 (device, "") == 0) {
1501 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_NOEXIST,
1502 "Invalid LVM ID specified");
1503 return FALSE;
1504 }
1505
1506 if (access (device, F_OK) != 0) {
1507 g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_NOEXIST,
1508 "The device '%s' doesn't exist", device);
1509 return FALSE;
1510 }
1511
1512 /* one has to be really persuasive to remove a PV (the double --force is not
1513 bug, at least not in this code) */
1514 g_variant_builder_init (&builder, G_VARIANT_TYPE_DICTIONARY);
1515 g_variant_builder_add (&builder, "{sv}", "-ff", g_variant_new ("s", ""));
1516 g_variant_builder_add (&builder, "{sv}", "--yes", g_variant_new ("s", ""));
1517
1518 params = g_variant_builder_end (&builder);
1519 g_variant_builder_clear (&builder);
1520 ret = call_lvm_obj_method_sync (device, PV_INTF, "Remove", NULL, params, extra, TRUE, &l_error);
1521 if (!ret && l_error && g_error_matches (l_error, BD_LVM_ERROR, BD_LVM_ERROR_NOEXIST)) {
1522 /* if the object doesn't exist, the given device is not a PV and thus
1523 this function should be a noop */
1524 g_clear_error (&l_error);
1525 ret = TRUE;
1526 }
1527
1528 if (l_error)
1529 g_propagate_error (error, l_error);
1530 return ret;
1531 }
1532
1533 /**
1534 * bd_lvm_pvmove:
1535 * @src: the PV device to move extents off of
1536 * @dest: (nullable): the PV device to move extents onto or %NULL
1537 * @extra: (nullable) (array zero-terminated=1): extra options for the PV move
1538 * (just passed to LVM as is)
1539 * @error: (out) (optional): place to store error (if any)
1540 *
1541 * Returns: whether the extents from the @src PV where successfully moved or not
1542 *
1543 * If @dest is %NULL, VG allocation rules are used for the extents from the @src
1544 * PV (see pvmove(8)).
1545 *
1546 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1547 */
1548 gboolean bd_lvm_pvmove (const gchar *src, const gchar *dest, const BDExtraArg **extra, GError **error) {
1549 GVariant *prop = NULL;
1550 gchar *src_path = NULL;
1551 gchar *dest_path = NULL;
1552 gchar *vg_obj_path = NULL;
1553 GVariantBuilder builder;
1554 GVariantType *type = NULL;
1555 GVariant *dest_var = NULL;
1556 GVariant *params = NULL;
1557 GError *l_error = NULL;
1558 gboolean ret = FALSE;
1559
1560 src_path = get_object_path (src, &l_error);
1561 if (!src_path || (g_strcmp0 (src_path, "/") == 0)) {
1562 if (!l_error)
1563 g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_NOEXIST,
1564 "The source PV '%s' doesn't exist", src);
1565 else
1566 g_propagate_error (error, l_error);
1567 return FALSE;
1568 }
1569 if (dest) {
1570 dest_path = get_object_path (dest, &l_error);
1571 if (!dest_path || (g_strcmp0 (dest_path, "/") == 0)) {
1572 if (!l_error)
1573 g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_NOEXIST,
1574 "The destination PV '%s' doesn't exist", dest);
1575 else
1576 g_propagate_error (error, l_error);
1577 return FALSE;
1578 }
1579 }
1580 prop = get_object_property (src_path, PV_INTF, "Vg", error);
1581 if (!prop) {
1582 g_free (src_path);
1583 return FALSE;
1584 }
1585 g_variant_get (prop, "o", &vg_obj_path);
1586
1587 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
1588 g_variant_builder_add_value (&builder, g_variant_new ("o", src_path));
1589 g_variant_builder_add_value (&builder, g_variant_new ("(tt)", (guint64) 0, (guint64) 0));
1590 if (dest) {
1591 dest_var = g_variant_new ("(ott)", dest_path, (guint64) 0, (guint64) 0);
1592 g_variant_builder_add_value (&builder, g_variant_new_array (NULL, &dest_var, 1));
1593 } else {
1594 type = g_variant_type_new ("a(ott)");
1595 g_variant_builder_add_value (&builder, g_variant_new_array (type, NULL, 0));
1596 g_variant_type_free (type);
1597 }
1598 params = g_variant_builder_end (&builder);
1599 g_variant_builder_clear (&builder);
1600
1601 ret = call_lvm_method_sync (vg_obj_path, VG_INTF, "Move", params, NULL, extra, TRUE, error);
1602
1603 g_free (src_path);
1604 g_free (dest_path);
1605 g_free (vg_obj_path);
1606 return ret;
1607 }
1608
1609 /**
1610 * bd_lvm_pvscan:
1611 * @device: (nullable): the device to scan for PVs or %NULL
1612 * @update_cache: whether to update the lvmetad cache or not
1613 * @extra: (nullable) (array zero-terminated=1): extra options for the PV scan
1614 * (just passed to LVM as is)
1615 * @error: (out) (optional): place to store error (if any)
1616 *
1617 * Returns: whether the system or @device was successfully scanned for PVs or not
1618 *
1619 * The @device argument is used only if @update_cache is %TRUE. Otherwise the
1620 * whole system is scanned for PVs.
1621 *
1622 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
1623 */
1624 gboolean bd_lvm_pvscan (const gchar *device, gboolean update_cache, const BDExtraArg **extra, GError **error) {
1625 GVariantBuilder builder;
1626 GVariantType *type = NULL;
1627 GVariant *params = NULL;
1628 GVariant *device_var = NULL;
1629
1630 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
1631 /* update the cache and specify the device (if any) */
1632 g_variant_builder_add_value (&builder, g_variant_new_boolean (FALSE));
1633 g_variant_builder_add_value (&builder, g_variant_new_boolean (update_cache));
1634 if (update_cache && device) {
1635 device_var = g_variant_new ("s", device);
1636 g_variant_builder_add_value (&builder, g_variant_new_array (NULL, &device_var, 1));
1637 } else {
1638 type = g_variant_type_new ("as");
1639 g_variant_builder_add_value (&builder, g_variant_new_array (type, NULL, 0));
1640 g_variant_type_free (type);
1641 }
1642 /* (major, minor)`s, we never specify them */
1643 type = g_variant_type_new ("a(ii)");
1644 g_variant_builder_add_value (&builder, g_variant_new_array (type, NULL, 0));
1645 g_variant_type_free (type);
1646
1647 params = g_variant_builder_end (&builder);
1648 g_variant_builder_clear (&builder);
1649
1650 return call_lvm_method_sync (MANAGER_OBJ, MANAGER_INTF, "PvScan", params, NULL, extra, TRUE, error);
1651 }
1652
1653
1654 static gboolean _manage_lvm_tags (const gchar *objpath, const gchar *pv_path, const gchar *intf, const gchar **tags, const gchar *func, GError **error) {
1655 guint num_tags = g_strv_length ((gchar **) tags);
1656 GVariant *params = NULL;
1657 GVariant **tags_array = NULL;
1658 GVariantBuilder builder;
1659 GVariant *pv_var = NULL;
1660 gboolean ret = FALSE;
1661
1662 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
1663
1664 if (pv_path) {
1665 /* PV tags are set from the VG interface so we need to add the PV as an argument here */
1666 pv_var = g_variant_new ("o", pv_path);
1667 g_variant_builder_add_value (&builder, g_variant_new_array (G_VARIANT_TYPE_OBJECT_PATH, &pv_var, 1));
1668 }
1669
1670 tags_array = g_new0 (GVariant *, num_tags + 1);
1671 for (guint i = 0; i < num_tags; i++)
1672 tags_array[i] = g_variant_new_string (tags[i]);
1673
1674 g_variant_builder_add_value (&builder, g_variant_new_array (G_VARIANT_TYPE_STRING, tags_array, num_tags));
1675
1676 params = g_variant_builder_end (&builder);
1677 g_variant_builder_clear (&builder);
1678
1679 ret = call_lvm_method_sync (objpath, intf, func, params, NULL, NULL, TRUE, error);
1680 g_free (tags_array);
1681 return ret;
1682 }
1683
1684 /**
1685 * bd_lvm_add_pv_tags:
1686 * @device: the device to set PV tags for
1687 * @tags: (array zero-terminated=1): list of tags to add
1688 * @error: (out) (optional): place to store error (if any)
1689 *
1690 * Returns: whether the tags were successfully added to @device or not
1691 *
1692 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
1693 */
1694 gboolean bd_lvm_add_pv_tags (const gchar *device, const gchar **tags, GError **error) {
1695 BDLVMPVdata *pvinfo = NULL;
1696 g_autofree gchar *vg_path = NULL;
1697 g_autofree gchar *pv_path = NULL;
1698
1699 pv_path = get_object_path (device, error);
1700 if (!pv_path)
1701 return FALSE;
1702
1703 pvinfo = bd_lvm_pvinfo (device, error);
1704 if (!pvinfo)
1705 return FALSE;
1706
1707 if (!pvinfo->vg_name) {
1708 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_FAIL,
1709 "Tags can't be added to PVs without a VG");
1710 bd_lvm_pvdata_free (pvinfo);
1711 return FALSE;
1712 }
1713
1714 vg_path = get_object_path (pvinfo->vg_name, error);
1715 bd_lvm_pvdata_free (pvinfo);
1716 if (!vg_path)
1717 return FALSE;
1718
1719 return _manage_lvm_tags (vg_path, pv_path, VG_INTF, tags, "PvTagsAdd", error);
1720 }
1721
1722 /**
1723 * bd_lvm_delete_pv_tags:
1724 * @device: the device to set PV tags for
1725 * @tags: (array zero-terminated=1): list of tags to remove
1726 * @error: (out) (optional): place to store error (if any)
1727 *
1728 * Returns: whether the tags were successfully removed from @device or not
1729 *
1730 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
1731 */
1732 gboolean bd_lvm_delete_pv_tags (const gchar *device, const gchar **tags, GError **error) {
1733 BDLVMPVdata *pvinfo = NULL;
1734 g_autofree gchar *vg_path = NULL;
1735 g_autofree gchar *pv_path = NULL;
1736
1737 pv_path = get_object_path (device, error);
1738 if (!pv_path)
1739 return FALSE;
1740
1741 pvinfo = bd_lvm_pvinfo (device, error);
1742 if (!pvinfo)
1743 return FALSE;
1744
1745 if (!pvinfo->vg_name) {
1746 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_FAIL,
1747 "Tags can't be removed from PVs without a VG");
1748 bd_lvm_pvdata_free (pvinfo);
1749 return FALSE;
1750 }
1751
1752 vg_path = get_object_path (pvinfo->vg_name, error);
1753 bd_lvm_pvdata_free (pvinfo);
1754 if (!vg_path)
1755 return FALSE;
1756
1757 return _manage_lvm_tags (vg_path, pv_path, VG_INTF, tags, "PvTagsDel", error);
1758 }
1759 /**
1760 * bd_lvm_pvinfo:
1761 * @device: a PV to get information about or %NULL
1762 * @error: (out) (optional): place to store error (if any)
1763 *
1764 * Returns: (transfer full): information about the PV on the given @device or
1765 * %NULL in case of error (the @error) gets populated in those cases)
1766 *
1767 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
1768 */
1769 BDLVMPVdata* bd_lvm_pvinfo (const gchar *device, GError **error) {
1770 GVariant *props = NULL;
1771 BDLVMPVdata *ret = NULL;
1772
1773 props = get_pv_properties (device, error);
1774 if (!props)
1775 /* the error is already populated */
1776 return NULL;
1777
1778 ret = get_pv_data_from_props (props, error);
1779 g_variant_unref (props);
1780
1781 return ret;
1782 }
1783
1784 /**
1785 * bd_lvm_pvs:
1786 * @error: (out) (optional): place to store error (if any)
1787 *
1788 * Returns: (array zero-terminated=1): information about PVs found in the system
1789 *
1790 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
1791 */
1792 BDLVMPVdata** bd_lvm_pvs (GError **error) {
1793 gchar **objects = NULL;
1794 guint64 n_pvs = 0;
1795 GVariant *props = NULL;
1796 BDLVMPVdata **ret = NULL;
1797 guint64 i = 0;
1798 GError *l_error = NULL;
1799
1800 objects = get_existing_objects (PV_OBJ_PREFIX, &l_error);
1801 if (!objects) {
1802 if (!l_error) {
1803 /* no PVs */
1804 ret = g_new0 (BDLVMPVdata*, 1);
1805 ret[0] = NULL;
1806 return ret;
1807 } else {
1808 g_propagate_error (error, l_error);
1809 return NULL;
1810 }
1811 }
1812
1813 n_pvs = g_strv_length ((gchar **) objects);
1814
1815 /* now create the return value -- NULL-terminated array of BDLVMPVdata */
1816 ret = g_new0 (BDLVMPVdata*, n_pvs + 1);
1817 for (i=0; i < n_pvs; i++) {
1818 props = get_object_properties (objects[i], PV_INTF, error);
1819 if (!props) {
1820 g_strfreev (objects);
1821 g_free (ret);
1822 return NULL;
1823 }
1824 ret[i] = get_pv_data_from_props (props, error);
1825 g_variant_unref (props);
1826 if (!(ret[i])) {
1827 g_strfreev (objects);
1828 g_free (ret);
1829 return NULL;
1830 }
1831 }
1832 ret[i] = NULL;
1833
1834 g_strfreev (objects);
1835 return ret;
1836 }
1837
1838 /**
1839 * bd_lvm_vgcreate:
1840 * @name: name of the newly created VG
1841 * @pv_list: (array zero-terminated=1): list of PVs the newly created VG should use
1842 * @pe_size: PE size or 0 if the default value should be used
1843 * @extra: (nullable) (array zero-terminated=1): extra options for the VG creation
1844 * (just passed to LVM as is)
1845 * @error: (out) (optional): place to store error (if any)
1846 *
1847 * Returns: whether the VG @name was successfully created or not
1848 *
1849 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_CREATE
1850 */
1851 gboolean bd_lvm_vgcreate (const gchar *name, const gchar **pv_list, guint64 pe_size, const BDExtraArg **extra, GError **error) {
1852 GVariantBuilder builder;
1853 gchar *path = NULL;
1854 const gchar **pv = NULL;
1855 GVariant *pvs = NULL;
1856 GVariant *params = NULL;
1857 GVariant *extra_params = NULL;
1858
1859 /* build the array of PVs (object paths) */
1860 g_variant_builder_init (&builder, G_VARIANT_TYPE_OBJECT_PATH_ARRAY);
1861 for (pv=pv_list; *pv; pv++) {
1862 path = get_object_path (*pv, error);
1863 if (!path) {
1864 g_variant_builder_clear (&builder);
1865 return FALSE;
1866 }
1867 g_variant_builder_add_value (&builder, g_variant_new ("o", path));
1868 g_free (path);
1869 }
1870 pvs = g_variant_builder_end (&builder);
1871 g_variant_builder_clear (&builder);
1872
1873 /* build the params tuple */
1874 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
1875 g_variant_builder_add_value (&builder, g_variant_new ("s", name));
1876 g_variant_builder_add_value (&builder, pvs);
1877 params = g_variant_builder_end (&builder);
1878 g_variant_builder_clear (&builder);
1879
1880 /* pe_size needs to go to extra_params params */
1881 pe_size = RESOLVE_PE_SIZE (pe_size);
1882 g_variant_builder_init (&builder, G_VARIANT_TYPE_DICTIONARY);
1883 g_variant_builder_add_value (&builder, g_variant_new ("{sv}", "--physicalextentsize", create_size_str_param (pe_size, "b")));
1884 extra_params = g_variant_builder_end (&builder);
1885 g_variant_builder_clear (&builder);
1886
1887 return call_lvm_method_sync (MANAGER_OBJ, MANAGER_INTF, "VgCreate", params, extra_params, extra, TRUE, error);
1888 }
1889
1890 /**
1891 * bd_lvm_vgremove:
1892 * @vg_name: name of the to be removed VG
1893 * @extra: (nullable) (array zero-terminated=1): extra options for the VG removal
1894 * (just passed to LVM as is)
1895 * @error: (out) (optional): place to store error (if any)
1896 *
1897 * Returns: whether the VG was successfully removed or not
1898 *
1899 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_REMOVE
1900 */
1901 gboolean bd_lvm_vgremove (const gchar *vg_name, const BDExtraArg **extra, GError **error) {
1902 return call_lvm_obj_method_sync (vg_name, VG_INTF, "Remove", NULL, NULL, extra, TRUE, error);
1903 }
1904
1905 /**
1906 * bd_lvm_vgrename:
1907 * @old_vg_name: old name of the VG to rename
1908 * @new_vg_name: new name for the @old_vg_name VG
1909 * @extra: (nullable) (array zero-terminated=1): extra options for the VG rename
1910 * (just passed to LVM as is)
1911 * @error: (out) (optional): place to store error (if any)
1912 *
1913 * Returns: whether the VG was successfully renamed or not
1914 *
1915 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1916 */
1917 gboolean bd_lvm_vgrename (const gchar *old_vg_name, const gchar *new_vg_name, const BDExtraArg **extra, GError **error) {
1918 GVariant *params = g_variant_new ("(s)", new_vg_name);
1919 return call_lvm_obj_method_sync (old_vg_name, VG_INTF, "Rename", params, NULL, extra, TRUE, error);
1920 }
1921
1922 /**
1923 * bd_lvm_vgactivate:
1924 * @vg_name: name of the to be activated VG
1925 * @extra: (nullable) (array zero-terminated=1): extra options for the VG activation
1926 * (just passed to LVM as is)
1927 * @error: (out) (optional): place to store error (if any)
1928 *
1929 * Returns: whether the VG was successfully activated or not
1930 *
1931 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1932 */
1933 gboolean bd_lvm_vgactivate (const gchar *vg_name, const BDExtraArg **extra, GError **error) {
1934 GVariant *params = g_variant_new ("(t)", (guint64) 0);
1935 return call_lvm_obj_method_sync (vg_name, VG_INTF, "Activate", params, NULL, extra, TRUE, error);
1936 }
1937
1938 /**
1939 * bd_lvm_vgdeactivate:
1940 * @vg_name: name of the to be deactivated VG
1941 * @extra: (nullable) (array zero-terminated=1): extra options for the VG deactivation
1942 * (just passed to LVM as is)
1943 * @error: (out) (optional): place to store error (if any)
1944 *
1945 * Returns: whether the VG was successfully deactivated or not
1946 *
1947 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1948 */
1949 gboolean bd_lvm_vgdeactivate (const gchar *vg_name, const BDExtraArg **extra, GError **error) {
1950 GVariant *params = g_variant_new ("(t)", (guint64) 0);
1951 return call_lvm_obj_method_sync (vg_name, VG_INTF, "Deactivate", params, NULL, extra, TRUE, error);
1952 }
1953
1954 /**
1955 * bd_lvm_vgextend:
1956 * @vg_name: name of the to be extended VG
1957 * @device: PV device to extend the @vg_name VG with
1958 * @extra: (nullable) (array zero-terminated=1): extra options for the VG extension
1959 * (just passed to LVM as is)
1960 * @error: (out) (optional): place to store error (if any)
1961 *
1962 * Returns: whether the VG @vg_name was successfully extended with the given @device or not.
1963 *
1964 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1965 */
1966 gboolean bd_lvm_vgextend (const gchar *vg_name, const gchar *device, const BDExtraArg **extra, GError **error) {
1967 g_autofree gchar *pv = NULL;
1968 GVariant *pv_var = NULL;
1969 GVariant *pvs = NULL;
1970 GVariant *params = NULL;
1971
1972 pv = get_object_path (device, error);
1973 if (!pv)
1974 return FALSE;
1975
1976 pv_var = g_variant_new ("o", pv);
1977 pvs = g_variant_new_array (NULL, &pv_var, 1);
1978 params = g_variant_new_tuple (&pvs, 1);
1979 return call_lvm_obj_method_sync (vg_name, VG_INTF, "Extend", params, NULL, extra, TRUE, error);
1980 }
1981
1982 /**
1983 * bd_lvm_vgreduce:
1984 * @vg_name: name of the to be reduced VG
1985 * @device: (nullable): PV device the @vg_name VG should be reduced of or %NULL
1986 * if the VG should be reduced of the missing PVs
1987 * @extra: (nullable) (array zero-terminated=1): extra options for the VG reduction
1988 * (just passed to LVM as is)
1989 * @error: (out) (optional): place to store error (if any)
1990 *
1991 * Returns: whether the VG @vg_name was successfully reduced of the given @device or not
1992 *
1993 * Note: This function does not move extents off of the PV before removing
1994 * it from the VG. You must do that first by calling #bd_lvm_pvmove.
1995 *
1996 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1997 */
1998 gboolean bd_lvm_vgreduce (const gchar *vg_name, const gchar *device, const BDExtraArg **extra, GError **error) {
1999 g_autofree gchar *pv = NULL;
2000 GVariantBuilder builder;
2001 GVariantType *type = NULL;
2002 GVariant *pv_var = NULL;
2003 GVariant *params = NULL;
2004 GVariant *extra_params = NULL;
2005
2006 if (device) {
2007 pv = get_object_path (device, error);
2008 if (!pv)
2009 return FALSE;
2010 }
2011
2012 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
2013 if (device) {
2014 /* do not remove missing */
2015 pv_var = g_variant_new ("o", pv);
2016 g_variant_builder_add_value (&builder, g_variant_new_boolean (FALSE));
2017 g_variant_builder_add_value (&builder, g_variant_new_array (NULL, &pv_var, 1));
2018 params = g_variant_builder_end (&builder);
2019 g_variant_builder_clear (&builder);
2020 } else {
2021 /* remove missing */
2022 g_variant_builder_add_value (&builder, g_variant_new_boolean (TRUE));
2023 type = g_variant_type_new ("ao");
2024 g_variant_builder_add_value (&builder, g_variant_new_array (type, NULL, 0));
2025 g_variant_type_free (type);
2026 params = g_variant_builder_end (&builder);
2027 g_variant_builder_clear (&builder);
2028
2029 g_variant_builder_init (&builder, G_VARIANT_TYPE_DICTIONARY);
2030 g_variant_builder_add_value (&builder, g_variant_new ("{sv}", "--force", g_variant_new ("s", "")));
2031 extra_params = g_variant_builder_end (&builder);
2032 g_variant_builder_clear (&builder);
2033 }
2034
2035 return call_lvm_obj_method_sync (vg_name, VG_INTF, "Reduce", params, extra_params, extra, TRUE, error);
2036 }
2037
2038 /**
2039 * bd_lvm_add_vg_tags:
2040 * @vg_name: the VG to set tags on
2041 * @tags: (array zero-terminated=1): list of tags to add
2042 * @error: (out) (optional): place to store error (if any)
2043 *
2044 * Returns: whether the tags were successfully added to @vg_name or not
2045 *
2046 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
2047 */
2048 gboolean bd_lvm_add_vg_tags (const gchar *vg_name, const gchar **tags, GError **error) {
2049 g_autofree gchar *obj_path = get_object_path (vg_name, error);
2050 if (!obj_path)
2051 return FALSE;
2052
2053 return _manage_lvm_tags (obj_path, NULL, VG_INTF, tags, "TagsAdd", error);
2054 }
2055
2056 /**
2057 * bd_lvm_delete_vg_tags:
2058 * @vg_name: the VG to set tags on
2059 * @tags: (array zero-terminated=1): list of tags to remove
2060 * @error: (out) (optional): place to store error (if any)
2061 *
2062 * Returns: whether the tags were successfully removed from @vg_name or not
2063 *
2064 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
2065 */
2066 gboolean bd_lvm_delete_vg_tags (const gchar *vg_name, const gchar **tags, GError **error) {
2067 g_autofree gchar *obj_path = get_object_path (vg_name, error);
2068 if (!obj_path)
2069 return FALSE;
2070
2071 return _manage_lvm_tags (obj_path, NULL, VG_INTF, tags, "TagsDel", error);
2072 }
2073
2074 static gboolean _vglock_start_stop (const gchar *vg_name, gboolean start, const BDExtraArg **extra, GError **error) {
2075 GVariantBuilder builder;
2076 GVariant *params = NULL;
2077
2078 g_variant_builder_init (&builder, G_VARIANT_TYPE_DICTIONARY);
2079 if (start)
2080 g_variant_builder_add (&builder, "{sv}", "--lockstart", g_variant_new ("s", ""));
2081 else
2082 g_variant_builder_add (&builder, "{sv}", "--lockstop", g_variant_new ("s", ""));
2083 params = g_variant_builder_end (&builder);
2084 g_variant_builder_clear (&builder);
2085
2086 return call_lvm_obj_method_sync (vg_name, VG_INTF, "Change", NULL, params, extra, TRUE, error);
2087 }
2088
2089 /**
2090 * bd_lvm_vglock_start:
2091 * @vg_name: a shared VG to start the lockspace in lvmlockd
2092 * @extra: (nullable) (array zero-terminated=1): extra options for the vgchange command
2093 * (just passed to LVM as is)
2094 * @error: (out) (optional): place to store error (if any)
2095 *
2096 * Returns: whether the lock was successfully started for @vg_name or not
2097 *
2098 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
2099 */
2100 gboolean bd_lvm_vglock_start (const gchar *vg_name, const BDExtraArg **extra, GError **error) {
2101 return _vglock_start_stop (vg_name, TRUE, extra, error);
2102 }
2103
2104 /**
2105 * bd_lvm_vglock_stop:
2106 * @vg_name: a shared VG to stop the lockspace in lvmlockd
2107 * @extra: (nullable) (array zero-terminated=1): extra options for the vgchange command
2108 * (just passed to LVM as is)
2109 * @error: (out) (optional): place to store error (if any)
2110 *
2111 * Returns: whether the lock was successfully stopped for @vg_name or not
2112 *
2113 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
2114 */
2115 gboolean bd_lvm_vglock_stop (const gchar *vg_name, const BDExtraArg **extra, GError **error) {
2116 return _vglock_start_stop (vg_name, FALSE, extra, error);
2117 }
2118
2119 /**
2120 * bd_lvm_vginfo:
2121 * @vg_name: a VG to get information about
2122 * @error: (out) (optional): place to store error (if any)
2123 *
2124 * Returns: (transfer full): information about the @vg_name VG or %NULL in case
2125 * of error (the @error) gets populated in those cases)
2126 *
2127 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
2128 */
2129 BDLVMVGdata* bd_lvm_vginfo (const gchar *vg_name, GError **error) {
2130 GVariant *props = NULL;
2131 BDLVMVGdata *ret = NULL;
2132
2133 props = get_vg_properties (vg_name, error);
2134 if (!props)
2135 /* the error is already populated */
2136 return NULL;
2137
2138 ret = get_vg_data_from_props (props, error);
2139 g_variant_unref (props);
2140
2141 return ret;
2142 }
2143
2144 /**
2145 * bd_lvm_vgs:
2146 * @error: (out) (optional): place to store error (if any)
2147 *
2148 * Returns: (array zero-terminated=1): information about VGs found in the system
2149 *
2150 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
2151 */
2152 BDLVMVGdata** bd_lvm_vgs (GError **error) {
2153 gchar **objects = NULL;
2154 guint64 n_vgs = 0;
2155 GVariant *props = NULL;
2156 BDLVMVGdata **ret = NULL;
2157 guint64 i = 0;
2158 GError *l_error = NULL;
2159
2160 objects = get_existing_objects (VG_OBJ_PREFIX, &l_error);
2161 if (!objects) {
2162 if (!l_error) {
2163 /* no VGs */
2164 ret = g_new0 (BDLVMVGdata*, 1);
2165 ret[0] = NULL;
2166 return ret;
2167 } else {
2168 g_propagate_error (error, l_error);
2169 return NULL;
2170 }
2171 }
2172
2173 n_vgs = g_strv_length ((gchar **) objects);
2174
2175 /* now create the return value -- NULL-terminated array of BDLVMVGdata */
2176 ret = g_new0 (BDLVMVGdata*, n_vgs + 1);
2177 for (i=0; i < n_vgs; i++) {
2178 props = get_object_properties (objects[i], VG_INTF, error);
2179 if (!props) {
2180 g_strfreev (objects);
2181 g_free (ret);
2182 return NULL;
2183 }
2184 ret[i] = get_vg_data_from_props (props, error);
2185 g_variant_unref (props);
2186 if (!(ret[i])) {
2187 g_strfreev (objects);
2188 g_free (ret);
2189 return NULL;
2190 }
2191 }
2192 ret[i] = NULL;
2193
2194 g_strfreev (objects);
2195 return ret;
2196 }
2197
2198 /**
2199 * bd_lvm_lvorigin:
2200 * @vg_name: name of the VG containing the queried LV
2201 * @lv_name: name of the queried LV
2202 * @error: (out) (optional): place to store error (if any)
2203 *
2204 * Returns: (transfer full): the origin volume for the @vg_name/@lv_name LV or
2205 * %NULL if failed to determine (@error) is set in those cases)
2206 *
2207 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
2208 */
2209 gchar* bd_lvm_lvorigin (const gchar *vg_name, const gchar *lv_name, GError **error) {
2210 GVariant *prop = NULL;
2211 gchar *obj_path = NULL;
2212 gchar *ret = NULL;
2213
2214 prop = get_lv_property (vg_name, lv_name, "OriginLv", error);
2215 if (!prop)
2216 return NULL;
2217 g_variant_get (prop, "o", &obj_path);
2218 g_variant_unref (prop);
2219
2220 if (g_strcmp0 (obj_path, "/") == 0) {
2221 /* no origin LV */
2222 g_free (obj_path);
2223 return NULL;
2224 }
2225 prop = get_object_property (obj_path, LV_CMN_INTF, "Name", error);
2226 if (!prop) {
2227 g_free (obj_path);
2228 return NULL;
2229 }
2230
2231 g_variant_get (prop, "s", &ret);
2232 g_variant_unref (prop);
2233
2234 return ret;
2235 }
2236
2237 /**
2238 * bd_lvm_lvcreate:
2239 * @vg_name: name of the VG to create a new LV in
2240 * @lv_name: name of the to-be-created LV
2241 * @size: requested size of the new LV
2242 * @type: (nullable): type of the new LV ("striped", "raid1",..., see lvcreate (8))
2243 * @pv_list: (nullable) (array zero-terminated=1): list of PVs the newly created LV should use or %NULL
2244 * if not specified
2245 * @extra: (nullable) (array zero-terminated=1): extra options for the LV creation
2246 * (just passed to LVM as is)
2247 * @error: (out) (optional): place to store error (if any)
2248 *
2249 * Returns: whether the given @vg_name/@lv_name LV was successfully created or not
2250 *
2251 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_CREATE
2252 */
2253 gboolean bd_lvm_lvcreate (const gchar *vg_name, const gchar *lv_name, guint64 size, const gchar *type, const gchar **pv_list, const BDExtraArg **extra, GError **error) {
2254 GVariantBuilder builder;
2255 gchar *path = NULL;
2256 const gchar **pv = NULL;
2257 GVariant *pvs = NULL;
2258 GVariantType *var_type = NULL;
2259 GVariant *params = NULL;
2260 GVariant *extra_params = NULL;
2261
2262 /* build the array of PVs (object paths) */
2263 if (pv_list && *pv_list) {
2264 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
2265 for (pv=pv_list; *pv; pv++) {
2266 path = get_object_path (*pv, error);
2267 if (!path) {
2268 g_variant_builder_clear (&builder);
2269 return FALSE;
2270 }
2271 g_variant_builder_add_value (&builder, g_variant_new ("(ott)", path, (guint64) 0, (guint64) 0));
2272 g_free (path);
2273 }
2274 pvs = g_variant_builder_end (&builder);
2275 g_variant_builder_clear (&builder);
2276 } else {
2277 var_type = g_variant_type_new ("a(ott)");
2278 pvs = g_variant_new_array (var_type, NULL, 0);
2279 g_variant_type_free (var_type);
2280 }
2281
2282 /* build the params tuple */
2283 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
2284 g_variant_builder_add_value (&builder, g_variant_new ("s", lv_name));
2285 g_variant_builder_add_value (&builder, g_variant_new ("t", size));
2286 g_variant_builder_add_value (&builder, pvs);
2287 params = g_variant_builder_end (&builder);
2288 g_variant_builder_clear (&builder);
2289
2290 if (type) {
2291 /* and now the extra_params params */
2292 g_variant_builder_init (&builder, G_VARIANT_TYPE_DICTIONARY);
2293 if (pv_list && g_strcmp0 (type, "striped") == 0)
2294 g_variant_builder_add_value (&builder, g_variant_new ("{sv}", "stripes", g_variant_new ("i", g_strv_length ((gchar **) pv_list))));
2295 else
2296 g_variant_builder_add_value (&builder, g_variant_new ("{sv}", "type", g_variant_new ("s", type)));
2297 extra_params = g_variant_builder_end (&builder);
2298 g_variant_builder_clear (&builder);
2299 }
2300
2301 return call_lvm_obj_method_sync (vg_name, VG_INTF, "LvCreate", params, extra_params, extra, TRUE, error);
2302 }
2303
2304 /**
2305 * bd_lvm_lvremove:
2306 * @vg_name: name of the VG containing the to-be-removed LV
2307 * @lv_name: name of the to-be-removed LV
2308 * @force: whether to force removal or not
2309 * @extra: (nullable) (array zero-terminated=1): extra options for the LV removal
2310 * (just passed to LVM as is)
2311 * @error: (out) (optional): place to store error (if any)
2312 *
2313 * Returns: whether the @vg_name/@lv_name LV was successfully removed or not
2314 *
2315 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_REMOVE
2316 */
2317 gboolean bd_lvm_lvremove (const gchar *vg_name, const gchar *lv_name, gboolean force, const BDExtraArg **extra, GError **error) {
2318 GVariantBuilder builder;
2319 GVariant *extra_params = NULL;
2320
2321 g_variant_builder_init (&builder, G_VARIANT_TYPE_DICTIONARY);
2322 /* '--yes' is needed if DISCARD is enabled */
2323 g_variant_builder_add (&builder, "{sv}", "--yes", g_variant_new ("s", ""));
2324 if (force) {
2325 g_variant_builder_add (&builder, "{sv}", "--force", g_variant_new ("s", ""));
2326 }
2327 extra_params = g_variant_builder_end (&builder);
2328 g_variant_builder_clear (&builder);
2329
2330 return call_lv_method_sync (vg_name, lv_name, "Remove", NULL, extra_params, extra, TRUE, error);
2331 }
2332
2333 /**
2334 * bd_lvm_lvrename:
2335 * @vg_name: name of the VG containing the to-be-renamed LV
2336 * @lv_name: name of the to-be-renamed LV
2337 * @new_name: new name for the @vg_name/@lv_name LV
2338 * @extra: (nullable) (array zero-terminated=1): extra options for the LV rename
2339 * (just passed to LVM as is)
2340 * @error: (out) (optional): place to store error (if any)
2341 *
2342 * Returns: whether the @vg_name/@lv_name LV was successfully renamed to
2343 * @vg_name/@new_name or not
2344 *
2345 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
2346 */
2347 gboolean bd_lvm_lvrename (const gchar *vg_name, const gchar *lv_name, const gchar *new_name, const BDExtraArg **extra, GError **error) {
2348 GVariant *params = NULL;
2349
2350 params = g_variant_new ("(s)", new_name);
2351 return call_lv_method_sync (vg_name, lv_name, "Rename", params, NULL, extra, TRUE, error);
2352 }
2353
2354 /**
2355 * bd_lvm_lvresize:
2356 * @vg_name: name of the VG containing the to-be-resized LV
2357 * @lv_name: name of the to-be-resized LV
2358 * @size: the requested new size of the LV
2359 * @extra: (nullable) (array zero-terminated=1): extra options for the LV resize
2360 * (just passed to LVM as is)
2361 * @error: (out) (optional): place to store error (if any)
2362 *
2363 * Returns: whether the @vg_name/@lv_name LV was successfully resized or not
2364 *
2365 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
2366 */
2367 gboolean bd_lvm_lvresize (const gchar *vg_name, const gchar *lv_name, guint64 size, const BDExtraArg **extra, GError **error) {
2368 GVariantBuilder builder;
2369 GVariantType *type = NULL;
2370 GVariant *params = NULL;
2371 GVariant *extra_params = NULL;
2372 gboolean success = FALSE;
2373
2374 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
2375 g_variant_builder_add_value (&builder, g_variant_new ("t", size));
2376 type = g_variant_type_new ("a(ott)");
2377 g_variant_builder_add_value (&builder, g_variant_new_array (type, NULL, 0));
2378 g_variant_type_free (type);
2379 params = g_variant_builder_end (&builder);
2380 g_variant_builder_clear (&builder);
2381
2382 /* Starting with 2.03.19 we need to add an extra option to avoid
2383 any filesystem related checks by lvresize.
2384 */
2385 success = bd_utils_check_util_version (deps[DEPS_LVM].name, LVM_VERSION_FSRESIZE,
2386 deps[DEPS_LVM].ver_arg, deps[DEPS_LVM].ver_regexp, NULL);
2387 if (success) {
2388 g_variant_builder_init (&builder, G_VARIANT_TYPE_DICTIONARY);
2389 g_variant_builder_add (&builder, "{sv}", "--fs", g_variant_new ("s", "ignore"));
2390 extra_params = g_variant_builder_end (&builder);
2391 g_variant_builder_clear (&builder);
2392 }
2393
2394 return call_lv_method_sync (vg_name, lv_name, "Resize", params, extra_params, extra, TRUE, error);
2395 }
2396
2397 /**
2398 * bd_lvm_lvrepair:
2399 * @vg_name: name of the VG containing the to-be-repaired LV
2400 * @lv_name: name of the to-be-repaired LV
2401 * @pv_list: (array zero-terminated=1): list of PVs to be used for the repair
2402 * @extra: (nullable) (array zero-terminated=1): extra options for the LV repair
2403 * (just passed to LVM as is)
2404 * @error: (out) (optional): place to store error (if any)
2405 *
2406 * Returns: whether the @vg_name/@lv_name LV was successfully repaired or not
2407 *
2408 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
2409 */
2410 gboolean bd_lvm_lvrepair (const gchar *vg_name, const gchar *lv_name, const gchar **pv_list,
2411 const BDExtraArg **extra, GError **error) {
2412 GVariantBuilder builder;
2413 GVariant *params = NULL;
2414 gchar *path = NULL;
2415 const gchar **pv = NULL;
2416 GVariant *pvs = NULL;
2417
2418 /* build the array of PVs (object paths) */
2419 g_variant_builder_init (&builder, G_VARIANT_TYPE_OBJECT_PATH_ARRAY);
2420 for (pv=pv_list; *pv; pv++) {
2421 path = get_object_path (*pv, error);
2422 if (!path) {
2423 g_variant_builder_clear (&builder);
2424 return FALSE;
2425 }
2426 g_variant_builder_add_value (&builder, g_variant_new ("o", path));
2427 g_free (path);
2428 }
2429 pvs = g_variant_builder_end (&builder);
2430 g_variant_builder_clear (&builder);
2431
2432 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
2433 g_variant_builder_add_value (&builder, pvs);
2434 params = g_variant_builder_end (&builder);
2435 g_variant_builder_clear (&builder);
2436
2437 return call_lv_method_sync (vg_name, lv_name, "RepairRaidLv", params, NULL, extra, TRUE, error);
2438 }
2439
2440 /**
2441 * bd_lvm_lvactivate:
2442 * @vg_name: name of the VG containing the to-be-activated LV
2443 * @lv_name: name of the to-be-activated LV
2444 * @ignore_skip: whether to ignore the skip flag or not
2445 * @shared: whether to activate the LV in shared mode (used for shared LVM setups with lvmlockd,
2446 * use %FALSE if not sure)
2447 * @extra: (nullable) (array zero-terminated=1): extra options for the LV activation
2448 * (just passed to LVM as is)
2449 * @error: (out) (optional): place to store error (if any)
2450 *
2451 * Returns: whether the @vg_name/@lv_name LV was successfully activated or not
2452 *
2453 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
2454 */
2455 gboolean bd_lvm_lvactivate (const gchar *vg_name, const gchar *lv_name, gboolean ignore_skip, gboolean shared, const BDExtraArg **extra, GError **error) {
2456 GVariant *params = NULL;
2457 GVariantBuilder builder;
2458 GVariant *extra_params = NULL;
2459
2460 if (shared)
2461 params = g_variant_new ("(t)", (guint64) 1 << 6);
2462 else
2463 params = g_variant_new ("(t)", (guint64) 0);
2464
2465 if (ignore_skip) {
2466 g_variant_builder_init (&builder, G_VARIANT_TYPE_DICTIONARY);
2467 g_variant_builder_add (&builder, "{sv}", "-K", g_variant_new ("s", ""));
2468 extra_params = g_variant_builder_end (&builder);
2469 g_variant_builder_clear (&builder);
2470 }
2471
2472 return call_lv_method_sync (vg_name, lv_name, "Activate", params, extra_params, extra, TRUE, error);
2473 }
2474
2475 /**
2476 * bd_lvm_lvdeactivate:
2477 * @vg_name: name of the VG containing the to-be-deactivated LV
2478 * @lv_name: name of the to-be-deactivated LV
2479 * @extra: (nullable) (array zero-terminated=1): extra options for the LV deactivation
2480 * (just passed to LVM as is)
2481 * @error: (out) (optional): place to store error (if any)
2482 *
2483 * Returns: whether the @vg_name/@lv_name LV was successfully deactivated or not
2484 *
2485 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
2486 */
2487 gboolean bd_lvm_lvdeactivate (const gchar *vg_name, const gchar *lv_name, const BDExtraArg **extra, GError **error) {
2488 GVariant *params = g_variant_new ("(t)", (guint64) 0);
2489 return call_lv_method_sync (vg_name, lv_name, "Deactivate", params, NULL, extra, TRUE, error);
2490 }
2491
2492 /**
2493 * bd_lvm_lvsnapshotcreate:
2494 * @vg_name: name of the VG containing the LV a new snapshot should be created of
2495 * @origin_name: name of the LV a new snapshot should be created of
2496 * @snapshot_name: name of the to-be-created snapshot
2497 * @size: requested size for the snapshot
2498 * @extra: (nullable) (array zero-terminated=1): extra options for the LV snapshot creation
2499 * (just passed to LVM as is)
2500 * @error: (out) (optional): place to store error (if any)
2501 *
2502 * Returns: whether the @snapshot_name snapshot of the @vg_name/@origin_name LV
2503 * was successfully created or not.
2504 *
2505 * Tech category: %BD_LVM_TECH_BASIC_SNAP-%BD_LVM_TECH_MODE_CREATE
2506 */
2507 gboolean bd_lvm_lvsnapshotcreate (const gchar *vg_name, const gchar *origin_name, const gchar *snapshot_name, guint64 size, const BDExtraArg **extra, GError **error) {
2508 GVariantBuilder builder;
2509 GVariant *params = NULL;
2510
2511 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
2512 g_variant_builder_add_value (&builder, g_variant_new ("s", snapshot_name));
2513 g_variant_builder_add_value (&builder, g_variant_new ("t", size));
2514 params = g_variant_builder_end (&builder);
2515 g_variant_builder_clear (&builder);
2516
2517 return call_lv_method_sync (vg_name, origin_name, "Snapshot", params, NULL, extra, TRUE, error);
2518 }
2519
2520 /**
2521 * bd_lvm_lvsnapshotmerge:
2522 * @vg_name: name of the VG containing the to-be-merged LV snapshot
2523 * @snapshot_name: name of the to-be-merged LV snapshot
2524 * @extra: (nullable) (array zero-terminated=1): extra options for the LV snapshot merge
2525 * (just passed to LVM as is)
2526 * @error: (out) (optional): place to store error (if any)
2527 *
2528 * Returns: whether the @vg_name/@snapshot_name LV snapshot was successfully merged or not
2529 *
2530 * Tech category: %BD_LVM_TECH_BASIC_SNAP-%BD_LVM_TECH_MODE_MODIFY
2531 */
2532 gboolean bd_lvm_lvsnapshotmerge (const gchar *vg_name, const gchar *snapshot_name, const BDExtraArg **extra, GError **error) {
2533 gchar *obj_id = NULL;
2534 g_autofree gchar *obj_path = NULL;
2535
2536 /* get object path for vg_name/snapshot_name and call SNAP_INTF, "Merge" */
2537 obj_id = g_strdup_printf ("%s/%s", vg_name, snapshot_name);
2538 obj_path = get_object_path (obj_id, error);
2539 g_free (obj_id);
2540 if (!obj_path)
2541 return FALSE;
2542
2543 return call_lvm_method_sync (obj_path, SNAP_INTF, "Merge", NULL, NULL, extra, TRUE, error);
2544 }
2545
2546 /**
2547 * bd_lvm_add_lv_tags:
2548 * @vg_name: name of the VG that contains the LV to set tags on
2549 * @lv_name: name of the LV to set tags on
2550 * @tags: (array zero-terminated=1): list of tags to add
2551 * @error: (out) (optional): place to store error (if any)
2552 *
2553 * Returns: whether the tags were successfully added to @device or not
2554 *
2555 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
2556 */
2557 gboolean bd_lvm_add_lv_tags (const gchar *vg_name, const gchar *lv_name, const gchar **tags, GError **error) {
2558 g_autofree gchar *obj_id = NULL;
2559 g_autofree gchar *obj_path = NULL;
2560
2561 /* get object path for vg_name/lv_name */
2562 obj_id = g_strdup_printf ("%s/%s", vg_name, lv_name);
2563 obj_path = get_object_path (obj_id, error);
2564 if (!obj_path)
2565 return FALSE;
2566
2567 return _manage_lvm_tags (obj_path, NULL, LV_INTF, tags, "TagsAdd", error);
2568 }
2569
2570 /**
2571 * bd_lvm_delete_lv_tags:
2572 * @vg_name: name of the VG that contains the LV to set tags on
2573 * @lv_name: name of the LV to set tags on
2574 * @tags: (array zero-terminated=1): list of tags to remove
2575 * @error: (out) (optional): place to store error (if any)
2576 *
2577 * Returns: whether the tags were successfully removed from @device or not
2578 *
2579 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
2580 */
2581 gboolean bd_lvm_delete_lv_tags (const gchar *vg_name, const gchar *lv_name, const gchar **tags, GError **error) {
2582 g_autofree gchar *obj_id = NULL;
2583 g_autofree gchar *obj_path = NULL;
2584
2585 /* get object path for vg_name/lv_name */
2586 obj_id = g_strdup_printf ("%s/%s", vg_name, lv_name);
2587 obj_path = get_object_path (obj_id, error);
2588 if (!obj_path)
2589 return FALSE;
2590
2591 return _manage_lvm_tags (obj_path, NULL, LV_INTF, tags, "TagsDel", error);
2592 }
2593
2594 /**
2595 * bd_lvm_lvinfo:
2596 * @vg_name: name of the VG that contains the LV to get information about
2597 * @lv_name: name of the LV to get information about
2598 * @error: (out) (optional): place to store error (if any)
2599 *
2600 * Returns: (transfer full): information about the @vg_name/@lv_name LV or %NULL in case
2601 * of error (the @error) gets populated in those cases)
2602 *
2603 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
2604 */
2605 BDLVMLVdata* bd_lvm_lvinfo (const gchar *vg_name, const gchar *lv_name, GError **error) {
2606 GVariant *props = NULL;
2607 BDLVMLVdata* ret = NULL;
2608
2609 props = get_lv_properties (vg_name, lv_name, error);
2610 if (!props)
2611 /* the error is already populated */
2612 return NULL;
2613
2614 ret = get_lv_data_from_props (props, error);
2615 if (!ret)
2616 return NULL;
2617
2618 if (g_strcmp0 (ret->segtype, "thin-pool") == 0 ||
2619 g_strcmp0 (ret->segtype, "cache-pool") == 0) {
2620 ret->data_lv = _lvm_data_lv_name (vg_name, lv_name, NULL);
2621 ret->metadata_lv = _lvm_metadata_lv_name (vg_name, lv_name, NULL);
2622 }
2623 if (g_strcmp0 (ret->segtype, "vdo-pool") == 0) {
2624 ret->data_lv = _lvm_data_lv_name (vg_name, lv_name, NULL);
2625 }
2626
2627 return ret;
2628 }
2629
2630 BDLVMLVdata* bd_lvm_lvinfo_tree (const gchar *vg_name, const gchar *lv_name, GError **error) {
2631 GVariant *props = NULL;
2632 BDLVMLVdata* ret = NULL;
2633
2634 props = get_lv_properties (vg_name, lv_name, error);
2635 if (!props)
2636 /* the error is already populated */
2637 return NULL;
2638
2639 ret = get_lv_data_from_props (props, error);
2640 if (!ret)
2641 return NULL;
2642
2643 if (g_strcmp0 (ret->segtype, "thin-pool") == 0 ||
2644 g_strcmp0 (ret->segtype, "cache-pool") == 0) {
2645 ret->data_lv = _lvm_data_lv_name (vg_name, lv_name, NULL);
2646 ret->metadata_lv = _lvm_metadata_lv_name (vg_name, lv_name, NULL);
2647 }
2648 if (g_strcmp0 (ret->segtype, "vdo-pool") == 0) {
2649 ret->data_lv = _lvm_data_lv_name (vg_name, lv_name, NULL);
2650 }
2651 ret->segs = _lvm_segs (vg_name, lv_name, NULL);
2652 _lvm_data_and_metadata_lvs (vg_name, lv_name, &ret->data_lvs, &ret->metadata_lvs, NULL);
2653
2654 return ret;
2655 }
2656
2657 static gchar* get_lv_vg_name (const gchar *lv_obj_path, GError **error) {
2658 GVariant *value = NULL;
2659 gchar *vg_obj_path = NULL;
2660 gchar *ret = NULL;
2661
2662 value = get_object_property (lv_obj_path, LV_CMN_INTF, "Vg", error);
2663 g_variant_get (value, "o", &vg_obj_path);
2664 g_variant_unref (value);
2665
2666 value = get_object_property (vg_obj_path, VG_INTF, "Name", error);
2667 g_variant_get (value, "s", &ret);
2668 g_free (vg_obj_path);
2669 g_variant_unref (value);
2670
2671 return ret;
2672 }
2673
2674 /**
2675 * filter_lvs_by_vg: (skip)
2676 *
2677 * Filter LVs by VG name and prepend the matching ones to the @out list.
2678 */
2679 static gboolean filter_lvs_by_vg (gchar **lvs, const gchar *vg_name, GSList **out, guint64 *n_lvs, GError **error) {
2680 gchar **lv_p = NULL;
2681 gchar *lv_vg_name = NULL;
2682 gboolean success = TRUE;
2683
2684 if (!lvs)
2685 /* nothing to do */
2686 return TRUE;
2687
2688 for (lv_p=lvs; *lv_p; lv_p++) {
2689 if (vg_name) {
2690 lv_vg_name = get_lv_vg_name (*lv_p, error);
2691 if (!lv_vg_name) {
2692 g_free (*lv_p);
2693 success = FALSE;
2694 continue;
2695 }
2696
2697 if (g_strcmp0 (lv_vg_name, vg_name) == 0) {
2698 *out = g_slist_prepend (*out, *lv_p);
2699 (*n_lvs)++;
2700 } else {
2701 g_free (*lv_p);
2702 }
2703
2704 g_free (lv_vg_name);
2705 } else {
2706 *out = g_slist_prepend (*out, *lv_p);
2707 (*n_lvs)++;
2708 }
2709 }
2710 return success;
2711 }
2712
2713 /**
2714 * bd_lvm_lvs:
2715 * @vg_name: (nullable): name of the VG to get information about LVs from
2716 * @error: (out) (optional): place to store error (if any)
2717 *
2718 * Returns: (array zero-terminated=1): information about LVs found in the given
2719 * @vg_name VG or in system if @vg_name is %NULL
2720 *
2721 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
2722 */
2723 BDLVMLVdata** bd_lvm_lvs (const gchar *vg_name, GError **error) {
2724 gchar **lvs = NULL;
2725 guint64 n_lvs = 0;
2726 GVariant *props = NULL;
2727 BDLVMLVdata **ret = NULL;
2728 guint64 j = 0;
2729 GSList *matched_lvs = NULL;
2730 GSList *lv = NULL;
2731 gboolean success = FALSE;
2732 GError *l_error = NULL;
2733
2734 lvs = get_existing_objects (LV_OBJ_PREFIX, &l_error);
2735 if (!lvs && l_error) {
2736 g_propagate_error (error, l_error);
2737 return NULL;
2738 }
2739 success = filter_lvs_by_vg (lvs, vg_name, &matched_lvs, &n_lvs, error);
2740 g_free (lvs);
2741 if (!success) {
2742 g_slist_free_full (matched_lvs, g_free);
2743 return NULL;
2744 }
2745
2746 lvs = get_existing_objects (THIN_POOL_OBJ_PREFIX, &l_error);
2747 if (!lvs && l_error) {
2748 g_propagate_error (error, l_error);
2749 g_slist_free_full (matched_lvs, g_free);
2750 return NULL;
2751 }
2752 success = filter_lvs_by_vg (lvs, vg_name, &matched_lvs, &n_lvs, error);
2753 g_free (lvs);
2754 if (!success) {
2755 g_slist_free_full (matched_lvs, g_free);
2756 return NULL;
2757 }
2758
2759 lvs = get_existing_objects (CACHE_POOL_OBJ_PREFIX, &l_error);
2760 if (!lvs && l_error) {
2761 g_propagate_error (error, l_error);
2762 g_slist_free_full (matched_lvs, g_free);
2763 return NULL;
2764 }
2765 success = filter_lvs_by_vg (lvs, vg_name, &matched_lvs, &n_lvs, error);
2766 g_free (lvs);
2767 if (!success) {
2768 g_slist_free_full (matched_lvs, g_free);
2769 return NULL;
2770 }
2771
2772 lvs = get_existing_objects (VDO_POOL_OBJ_PREFIX, &l_error);
2773 if (!lvs && l_error) {
2774 g_propagate_error (error, l_error);
2775 g_slist_free_full (matched_lvs, g_free);
2776 return NULL;
2777 }
2778 success = filter_lvs_by_vg (lvs, vg_name, &matched_lvs, &n_lvs, error);
2779 g_free (lvs);
2780 if (!success) {
2781 g_slist_free_full (matched_lvs, g_free);
2782 return NULL;
2783 }
2784
2785 lvs = get_existing_objects (HIDDEN_LV_OBJ_PREFIX, &l_error);
2786 if (!lvs && l_error) {
2787 g_propagate_error (error, l_error);
2788 g_slist_free_full (matched_lvs, g_free);
2789 return NULL;
2790 }
2791 success = filter_lvs_by_vg (lvs, vg_name, &matched_lvs, &n_lvs, error);
2792 g_free (lvs);
2793 if (!success) {
2794 g_slist_free_full (matched_lvs, g_free);
2795 return NULL;
2796 }
2797
2798 if (n_lvs == 0) {
2799 /* no LVs */
2800 ret = g_new0 (BDLVMLVdata*, 1);
2801 ret[0] = NULL;
2802 g_slist_free_full (matched_lvs, g_free);
2803 return ret;
2804 }
2805
2806 /* we have been prepending to the list so far, but it will be nicer if we
2807 reverse it (to get back the original order) */
2808 matched_lvs = g_slist_reverse (matched_lvs);
2809
2810 /* now create the return value -- NULL-terminated array of BDLVMLVdata */
2811 ret = g_new0 (BDLVMLVdata*, n_lvs + 1);
2812
2813 lv = matched_lvs;
2814 while (lv) {
2815 props = get_object_properties (lv->data, LV_CMN_INTF, &l_error);
2816 if (!props) {
2817 g_slist_free_full (matched_lvs, g_free);
2818 g_free (ret);
2819 g_propagate_error (error, l_error);
2820 return NULL;
2821 }
2822 ret[j] = get_lv_data_from_props (props, &l_error);
2823 if (!(ret[j])) {
2824 g_slist_free_full (matched_lvs, g_free);
2825 for (guint64 i = 0; i < j; i++)
2826 bd_lvm_lvdata_free (ret[i]);
2827 g_free (ret);
2828 g_propagate_error (error, l_error);
2829 return NULL;
2830 } else if ((g_strcmp0 (ret[j]->segtype, "thin-pool") == 0) ||
2831 (g_strcmp0 (ret[j]->segtype, "cache-pool") == 0)) {
2832 ret[j]->data_lv = _lvm_data_lv_name (ret[j]->vg_name, ret[j]->lv_name, &l_error);
2833 ret[j]->metadata_lv = _lvm_metadata_lv_name (ret[j]->vg_name, ret[j]->lv_name, &l_error);
2834 } else if (g_strcmp0 (ret[j]->segtype, "vdo-pool") == 0) {
2835 ret[j]->data_lv = _lvm_data_lv_name (ret[j]->vg_name, ret[j]->lv_name, &l_error);
2836 }
2837 if (l_error) {
2838 g_slist_free_full (matched_lvs, g_free);
2839 for (guint64 i = 0; i <= j; i++)
2840 bd_lvm_lvdata_free (ret[i]);
2841 g_free (ret);
2842 g_propagate_error (error, l_error);
2843 return NULL;
2844 }
2845 j++;
2846 lv = g_slist_next (lv);
2847 }
2848 g_slist_free_full (matched_lvs, g_free);
2849
2850 ret[j] = NULL;
2851 return ret;
2852 }
2853
2854 BDLVMLVdata** bd_lvm_lvs_tree (const gchar *vg_name, GError **error) {
2855 gchar **lvs = NULL;
2856 guint64 n_lvs = 0;
2857 GVariant *props = NULL;
2858 BDLVMLVdata **ret = NULL;
2859 guint64 j = 0;
2860 GSList *matched_lvs = NULL;
2861 GSList *lv = NULL;
2862 gboolean success = FALSE;
2863 GError *l_error = NULL;
2864
2865 lvs = get_existing_objects (LV_OBJ_PREFIX, &l_error);
2866 if (!lvs && l_error) {
2867 g_propagate_error (error, l_error);
2868 return NULL;
2869 }
2870 success = filter_lvs_by_vg (lvs, vg_name, &matched_lvs, &n_lvs, error);
2871 g_free (lvs);
2872 if (!success) {
2873 g_slist_free_full (matched_lvs, g_free);
2874 return NULL;
2875 }
2876
2877 lvs = get_existing_objects (THIN_POOL_OBJ_PREFIX, &l_error);
2878 if (!lvs && l_error) {
2879 g_propagate_error (error, l_error);
2880 g_slist_free_full (matched_lvs, g_free);
2881 return NULL;
2882 }
2883 success = filter_lvs_by_vg (lvs, vg_name, &matched_lvs, &n_lvs, error);
2884 g_free (lvs);
2885 if (!success) {
2886 g_slist_free_full (matched_lvs, g_free);
2887 return NULL;
2888 }
2889
2890 lvs = get_existing_objects (CACHE_POOL_OBJ_PREFIX, &l_error);
2891 if (!lvs && l_error) {
2892 g_propagate_error (error, l_error);
2893 g_slist_free_full (matched_lvs, g_free);
2894 return NULL;
2895 }
2896 success = filter_lvs_by_vg (lvs, vg_name, &matched_lvs, &n_lvs, error);
2897 g_free (lvs);
2898 if (!success) {
2899 g_slist_free_full (matched_lvs, g_free);
2900 return NULL;
2901 }
2902
2903 lvs = get_existing_objects (VDO_POOL_OBJ_PREFIX, &l_error);
2904 if (!lvs && l_error) {
2905 g_propagate_error (error, l_error);
2906 g_slist_free_full (matched_lvs, g_free);
2907 return NULL;
2908 }
2909 success = filter_lvs_by_vg (lvs, vg_name, &matched_lvs, &n_lvs, error);
2910 g_free (lvs);
2911 if (!success) {
2912 g_slist_free_full (matched_lvs, g_free);
2913 return NULL;
2914 }
2915
2916 lvs = get_existing_objects (HIDDEN_LV_OBJ_PREFIX, &l_error);
2917 if (!lvs && l_error) {
2918 g_propagate_error (error, l_error);
2919 g_slist_free_full (matched_lvs, g_free);
2920 return NULL;
2921 }
2922 success = filter_lvs_by_vg (lvs, vg_name, &matched_lvs, &n_lvs, error);
2923 g_free (lvs);
2924 if (!success) {
2925 g_slist_free_full (matched_lvs, g_free);
2926 return NULL;
2927 }
2928
2929 if (n_lvs == 0) {
2930 /* no LVs */
2931 ret = g_new0 (BDLVMLVdata*, 1);
2932 ret[0] = NULL;
2933 g_slist_free_full (matched_lvs, g_free);
2934 return ret;
2935 }
2936
2937 /* we have been prepending to the list so far, but it will be nicer if we
2938 reverse it (to get back the original order) */
2939 matched_lvs = g_slist_reverse (matched_lvs);
2940
2941 /* now create the return value -- NULL-terminated array of BDLVMLVdata */
2942 ret = g_new0 (BDLVMLVdata*, n_lvs + 1);
2943
2944 lv = matched_lvs;
2945 while (lv) {
2946 props = get_object_properties (lv->data, LV_CMN_INTF, &l_error);
2947 if (!props) {
2948 g_slist_free_full (matched_lvs, g_free);
2949 g_free (ret);
2950 g_propagate_error (error, l_error);
2951 return NULL;
2952 }
2953 ret[j] = get_lv_data_from_props (props, &l_error);
2954 if (!(ret[j])) {
2955 g_slist_free_full (matched_lvs, g_free);
2956 for (guint64 i = 0; i < j; i++)
2957 bd_lvm_lvdata_free (ret[i]);
2958 g_free (ret);
2959 g_propagate_error (error, l_error);
2960 return NULL;
2961 } else if ((g_strcmp0 (ret[j]->segtype, "thin-pool") == 0) ||
2962 (g_strcmp0 (ret[j]->segtype, "cache-pool") == 0)) {
2963 ret[j]->data_lv = _lvm_data_lv_name (ret[j]->vg_name, ret[j]->lv_name, &l_error);
2964 ret[j]->metadata_lv = _lvm_metadata_lv_name (ret[j]->vg_name, ret[j]->lv_name, &l_error);
2965 } else if (g_strcmp0 (ret[j]->segtype, "vdo-pool") == 0) {
2966 ret[j]->data_lv = _lvm_data_lv_name (ret[j]->vg_name, ret[j]->lv_name, &l_error);
2967 }
2968 ret[j]->segs = _lvm_segs (ret[j]->vg_name, ret[j]->lv_name, &l_error);
2969 _lvm_data_and_metadata_lvs (ret[j]->vg_name, ret[j]->lv_name, &ret[j]->data_lvs, &ret[j]->metadata_lvs,
2970 &l_error);
2971 if (l_error) {
2972 g_slist_free_full (matched_lvs, g_free);
2973 for (guint64 i = 0; i <= j; i++)
2974 bd_lvm_lvdata_free (ret[i]);
2975 g_free (ret);
2976 g_propagate_error (error, l_error);
2977 return NULL;
2978 }
2979 j++;
2980 lv = g_slist_next (lv);
2981 }
2982 g_slist_free_full (matched_lvs, g_free);
2983
2984 ret[j] = NULL;
2985 return ret;
2986 }
2987
2988 /**
2989 * bd_lvm_thpoolcreate:
2990 * @vg_name: name of the VG to create a thin pool in
2991 * @lv_name: name of the to-be-created pool LV
2992 * @size: requested size of the to-be-created pool
2993 * @md_size: requested metadata size or 0 to use the default
2994 * @chunk_size: requested chunk size or 0 to use the default
2995 * @profile: (nullable): profile to use (see lvm(8) for more information) or %NULL to use
2996 * the default
2997 * @extra: (nullable) (array zero-terminated=1): extra options for the thin pool creation
2998 * (just passed to LVM as is)
2999 * @error: (out) (optional): place to store error (if any)
3000 *
3001 * Returns: whether the @vg_name/@lv_name thin pool was successfully created or not
3002 *
3003 * Tech category: %BD_LVM_TECH_THIN-%BD_LVM_TECH_MODE_CREATE
3004 */
3005 gboolean bd_lvm_thpoolcreate (const gchar *vg_name, const gchar *lv_name, guint64 size, guint64 md_size, guint64 chunk_size, const gchar *profile, const BDExtraArg **extra, GError **error) {
3006 GVariantBuilder builder;
3007 GVariant *params = NULL;
3008 GVariant *extra_params = NULL;
3009 GVariant *param = NULL;
3010
3011 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
3012 g_variant_builder_add_value (&builder, g_variant_new ("s", lv_name));
3013 g_variant_builder_add_value (&builder, g_variant_new_uint64 (size));
3014 g_variant_builder_add_value (&builder, g_variant_new_boolean (TRUE));
3015 params = g_variant_builder_end (&builder);
3016 g_variant_builder_clear (&builder);
3017
3018 g_variant_builder_init (&builder, G_VARIANT_TYPE_DICTIONARY);
3019 if (md_size != 0) {
3020 param = create_size_str_param (md_size, "b");
3021 g_variant_builder_add (&builder, "{sv}", "poolmetadatasize", param);
3022 }
3023 if (chunk_size != 0) {
3024 param = create_size_str_param (chunk_size, "b");
3025 g_variant_builder_add (&builder, "{sv}", "chunksize", param);
3026 }
3027 if (profile) {
3028 g_variant_builder_add (&builder, "{sv}", "profile", g_variant_new ("s", profile));
3029 }
3030 extra_params = g_variant_builder_end (&builder);
3031 g_variant_builder_clear (&builder);
3032
3033 return call_lvm_obj_method_sync (vg_name, VG_INTF, "LvCreateLinear", params, extra_params, extra, TRUE, error);
3034 }
3035
3036 /**
3037 * bd_lvm_thlvcreate:
3038 * @vg_name: name of the VG containing the thin pool providing extents for the to-be-created thin LV
3039 * @pool_name: name of the pool LV providing extents for the to-be-created thin LV
3040 * @lv_name: name of the to-be-created thin LV
3041 * @size: requested virtual size of the to-be-created thin LV
3042 * @extra: (nullable) (array zero-terminated=1): extra options for the thin LV creation
3043 * (just passed to LVM as is)
3044 * @error: (out) (optional): place to store error (if any)
3045 *
3046 * Returns: whether the @vg_name/@lv_name thin LV was successfully created or not
3047 *
3048 * Tech category: %BD_LVM_TECH_THIN-%BD_LVM_TECH_MODE_CREATE
3049 */
3050 gboolean bd_lvm_thlvcreate (const gchar *vg_name, const gchar *pool_name, const gchar *lv_name, guint64 size, const BDExtraArg **extra, GError **error) {
3051 GVariantBuilder builder;
3052 GVariant *params = NULL;
3053
3054 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
3055 g_variant_builder_add_value (&builder, g_variant_new ("s", lv_name));
3056 g_variant_builder_add_value (&builder, g_variant_new ("t", size));
3057 params = g_variant_builder_end (&builder);
3058 g_variant_builder_clear (&builder);
3059
3060 return call_thpool_method_sync (vg_name, pool_name, "LvCreate", params, NULL, extra, TRUE, error);
3061 }
3062
3063 /**
3064 * bd_lvm_thlvpoolname:
3065 * @vg_name: name of the VG containing the queried thin LV
3066 * @lv_name: name of the queried thin LV
3067 * @error: (out) (optional): place to store error (if any)
3068 *
3069 * Returns: (transfer full): the name of the pool volume for the @vg_name/@lv_name
3070 * thin LV or %NULL if failed to determine (@error) is set in those cases)
3071 *
3072 * Tech category: %BD_LVM_TECH_THIN-%BD_LVM_TECH_MODE_QUERY
3073 */
3074 gchar* bd_lvm_thlvpoolname (const gchar *vg_name, const gchar *lv_name, GError **error) {
3075 GVariant *prop = NULL;
3076 gboolean is_thin = FALSE;
3077 gchar *pool_obj_path = NULL;
3078 gchar *ret = NULL;
3079
3080 prop = get_lv_property (vg_name, lv_name, "IsThinVolume", error);
3081 if (!prop)
3082 return NULL;
3083 is_thin = g_variant_get_boolean (prop);
3084 g_variant_unref (prop);
3085
3086 if (!is_thin) {
3087 g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_NOEXIST,
3088 "The LV '%s' is not a thin LV and thus have no thin pool", lv_name);
3089 return NULL;
3090 }
3091 prop = get_lv_property (vg_name, lv_name, "PoolLv", error);
3092 if (!prop)
3093 return NULL;
3094 g_variant_get (prop, "o", &pool_obj_path);
3095 g_variant_unref (prop);
3096
3097 prop = get_object_property (pool_obj_path, LV_CMN_INTF, "Name", error);
3098 g_free (pool_obj_path);
3099 if (!prop)
3100 return NULL;
3101 g_variant_get (prop, "s", &ret);
3102 g_variant_unref (prop);
3103
3104 return ret;
3105 }
3106
3107 /**
3108 * bd_lvm_thsnapshotcreate:
3109 * @vg_name: name of the VG containing the thin LV a new snapshot should be created of
3110 * @origin_name: name of the thin LV a new snapshot should be created of
3111 * @snapshot_name: name of the to-be-created snapshot
3112 * @pool_name: (nullable): name of the thin pool to create the snapshot in or %NULL if not specified
3113 * @extra: (nullable) (array zero-terminated=1): extra options for the thin LV snapshot creation
3114 * (just passed to LVM as is)
3115 * @error: (out) (optional): place to store error (if any)
3116 *
3117 * Returns: whether the @snapshot_name snapshot of the @vg_name/@origin_name
3118 * thin LV was successfully created or not.
3119 *
3120 * Tech category: %BD_LVM_TECH_THIN-%BD_LVM_TECH_MODE_CREATE
3121 */
3122 gboolean bd_lvm_thsnapshotcreate (const gchar *vg_name, const gchar *origin_name, const gchar *snapshot_name, const gchar *pool_name, const BDExtraArg **extra, GError **error) {
3123 GVariantBuilder builder;
3124 GVariant *params = NULL;
3125 GVariant *extra_params = NULL;
3126
3127 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
3128 g_variant_builder_add_value (&builder, g_variant_new ("s", snapshot_name));
3129 g_variant_builder_add_value (&builder, g_variant_new ("t", (guint64) 0));
3130 params = g_variant_builder_end (&builder);
3131 g_variant_builder_clear (&builder);
3132
3133 if (pool_name) {
3134 g_variant_builder_init (&builder, G_VARIANT_TYPE_DICTIONARY);
3135 g_variant_builder_add (&builder, "{sv}", "thinpool", g_variant_new ("s", pool_name));
3136 extra_params = g_variant_builder_end (&builder);
3137 g_variant_builder_clear (&builder);
3138 }
3139
3140 return call_lv_method_sync (vg_name, origin_name, "Snapshot", params, extra_params, extra, TRUE, error);
3141 }
3142
3143 /**
3144 * get_lv_type_from_flags: (skip)
3145 * @meta: getting type for a (future) metadata LV
3146 *
3147 * Get LV type string from flags.
3148 */
3149 static const gchar* get_lv_type_from_flags (BDLVMCachePoolFlags flags, gboolean meta, GError **error G_GNUC_UNUSED) {
3150 if (!meta) {
3151 if (flags & BD_LVM_CACHE_POOL_STRIPED)
3152 return "striped";
3153 else if (flags & BD_LVM_CACHE_POOL_RAID1)
3154 return "raid1";
3155 else if (flags & BD_LVM_CACHE_POOL_RAID5)
3156 return "raid5";
3157 else if (flags & BD_LVM_CACHE_POOL_RAID6)
3158 return "raid6";
3159 else if (flags & BD_LVM_CACHE_POOL_RAID10)
3160 return "raid10";
3161 else
3162 return NULL;
3163 } else {
3164 if (flags & BD_LVM_CACHE_POOL_META_STRIPED)
3165 return "striped";
3166 else if (flags & BD_LVM_CACHE_POOL_META_RAID1)
3167 return "raid1";
3168 else if (flags & BD_LVM_CACHE_POOL_META_RAID5)
3169 return "raid5";
3170 else if (flags & BD_LVM_CACHE_POOL_META_RAID6)
3171 return "raid6";
3172 else if (flags & BD_LVM_CACHE_POOL_META_RAID10)
3173 return "raid10";
3174 else
3175 return NULL;
3176 }
3177 }
3178
3179 /**
3180 * bd_lvm_cache_create_pool:
3181 * @vg_name: name of the VG to create @pool_name in
3182 * @pool_name: name of the cache pool LV to create
3183 * @pool_size: desired size of the cache pool @pool_name
3184 * @md_size: desired size of the @pool_name cache pool's metadata LV or 0 to
3185 * use the default
3186 * @mode: cache mode of the @pool_name cache pool
3187 * @flags: a combination of (ORed) #BDLVMCachePoolFlags
3188 * @fast_pvs: (array zero-terminated=1): list of (fast) PVs to create the @pool_name
3189 * cache pool (and the metadata LV)
3190 * @error: (out) (optional): place to store error (if any)
3191 *
3192 * Returns: whether the cache pool @vg_name/@pool_name was successfully created or not
3193 *
3194 * Tech category: %BD_LVM_TECH_CACHE-%BD_LVM_TECH_MODE_CREATE
3195 */
3196 gboolean bd_lvm_cache_create_pool (const gchar *vg_name, const gchar *pool_name, guint64 pool_size, guint64 md_size, BDLVMCacheMode mode, BDLVMCachePoolFlags flags, const gchar **fast_pvs, GError **error) {
3197 gboolean success = FALSE;
3198 const gchar *type = NULL;
3199 gchar *name = NULL;
3200 GVariantBuilder builder;
3201 GVariant *params = NULL;
3202 GVariant *extra = NULL;
3203 gchar *lv_id = NULL;
3204 gchar *lv_obj_path = NULL;
3205 const gchar *mode_str = NULL;
3206 gchar *msg = NULL;
3207 guint64 progress_id = 0;
3208 GError *l_error = NULL;
3209
3210 msg = g_strdup_printf ("Started 'create cache pool %s/%s'", vg_name, pool_name);
3211 progress_id = bd_utils_report_started (msg);
3212 g_free (msg);
3213
3214 /* create an LV for the pool */
3215 type = get_lv_type_from_flags (flags, FALSE, NULL);
3216 success = bd_lvm_lvcreate (vg_name, pool_name, pool_size, type, fast_pvs, NULL, &l_error);
3217 if (!success) {
3218 g_prefix_error (&l_error, "Failed to create the pool LV: ");
3219 bd_utils_report_finished (progress_id, l_error->message);
3220 g_propagate_error (error, l_error);
3221 return FALSE;
3222 }
3223
3224 /* 1/3 steps done */
3225 bd_utils_report_progress (progress_id, 33, "Created the data LV");
3226
3227 /* determine the size of the metadata LV */
3228 type = get_lv_type_from_flags (flags, TRUE, NULL);
3229 if (md_size == 0)
3230 md_size = bd_lvm_cache_get_default_md_size (pool_size, NULL);
3231 name = g_strdup_printf ("%s_meta", pool_name);
3232
3233 /* create the metadata LV */
3234 success = bd_lvm_lvcreate (vg_name, name, md_size, type, fast_pvs, NULL, &l_error);
3235 if (!success) {
3236 g_free (name);
3237 g_prefix_error (&l_error, "Failed to create the pool metadata LV: ");
3238 bd_utils_report_finished (progress_id, l_error->message);
3239 g_propagate_error (error, l_error);
3240 return FALSE;
3241 }
3242
3243 /* 2/3 steps done */
3244 bd_utils_report_progress (progress_id, 66, "Created the metadata LV");
3245
3246 /* create the cache pool from the two LVs */
3247 /* build the params tuple */
3248 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
3249 lv_id = g_strdup_printf ("%s/%s", vg_name, name);
3250 lv_obj_path = get_object_path (lv_id, &l_error);
3251 g_free (lv_id);
3252 if (!lv_obj_path) {
3253 g_variant_builder_clear (&builder);
3254 bd_utils_report_finished (progress_id, l_error->message);
3255 g_propagate_error (error, l_error);
3256 return FALSE;
3257 }
3258 g_variant_builder_add_value (&builder, g_variant_new ("o", lv_obj_path));
3259 lv_id = g_strdup_printf ("%s/%s", vg_name, pool_name);
3260 lv_obj_path = get_object_path (lv_id, &l_error);
3261 g_free (lv_id);
3262 if (!lv_obj_path) {
3263 g_variant_builder_clear (&builder);
3264 bd_utils_report_finished (progress_id, l_error->message);
3265 g_propagate_error (error, l_error);
3266 return FALSE;
3267 }
3268 g_variant_builder_add_value (&builder, g_variant_new ("o", lv_obj_path));
3269 params = g_variant_builder_end (&builder);
3270 g_variant_builder_clear (&builder);
3271
3272 /* build the dictionary with the extra params */
3273 g_variant_builder_init (&builder, G_VARIANT_TYPE_DICTIONARY);
3274 mode_str = bd_lvm_cache_get_mode_str (mode, &l_error);
3275 if (!mode_str) {
3276 g_variant_builder_clear (&builder);
3277 bd_utils_report_finished (progress_id, l_error->message);
3278 g_propagate_error (error, l_error);
3279 return FALSE;
3280 }
3281 g_variant_builder_add (&builder, "{sv}", "cachemode", g_variant_new ("s", mode_str));
3282 extra = g_variant_builder_end (&builder);
3283 g_variant_builder_clear (&builder);
3284
3285 success = call_lvm_obj_method_sync (vg_name, VG_INTF, "CreateCachePool", params, extra, NULL, TRUE, &l_error);
3286 if (!success) {
3287 bd_utils_report_finished (progress_id, l_error->message);
3288 g_propagate_error (error, l_error);
3289 } else
3290 bd_utils_report_finished (progress_id, "Completed");
3291
3292 return success;
3293 }
3294
3295 /**
3296 * bd_lvm_cache_attach:
3297 * @vg_name: name of the VG containing the @data_lv and the @cache_pool_lv LVs
3298 * @data_lv: data LV to attach the @cache_pool_lv to
3299 * @cache_pool_lv: cache pool LV to attach to the @data_lv
3300 * @extra: (nullable) (array zero-terminated=1): extra options for the cache attachment
3301 * (just passed to LVM as is)
3302 * @error: (out) (optional): place to store error (if any)
3303 *
3304 * Returns: whether the @cache_pool_lv was successfully attached to the @data_lv or not
3305 *
3306 * Tech category: %BD_LVM_TECH_CACHE-%BD_LVM_TECH_MODE_MODIFY
3307 */
3308 gboolean bd_lvm_cache_attach (const gchar *vg_name, const gchar *data_lv, const gchar *cache_pool_lv, const BDExtraArg **extra, GError **error) {
3309 GVariantBuilder builder;
3310 GVariant *params = NULL;
3311 gchar *lv_id = NULL;
3312 g_autofree gchar *lv_obj_path = NULL;
3313 gboolean ret = FALSE;
3314
3315 lv_id = g_strdup_printf ("%s/%s", vg_name, data_lv);
3316 lv_obj_path = get_object_path (lv_id, error);
3317 g_free (lv_id);
3318 if (!lv_obj_path)
3319 return FALSE;
3320 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
3321 g_variant_builder_add_value (&builder, g_variant_new ("o", lv_obj_path));
3322 params = g_variant_builder_end (&builder);
3323 g_variant_builder_clear (&builder);
3324
3325 lv_id = g_strdup_printf ("%s/%s", vg_name, cache_pool_lv);
3326
3327 ret = call_lvm_obj_method_sync (lv_id, CACHE_POOL_INTF, "CacheLv", params, NULL, extra, TRUE, error);
3328 g_free (lv_id);
3329 return ret;
3330 }
3331
3332 /**
3333 * bd_lvm_cache_detach:
3334 * @vg_name: name of the VG containing the @cached_lv
3335 * @cached_lv: name of the cached LV to detach its cache from
3336 * @destroy: whether to destroy the cache after detach or not
3337 * @extra: (nullable) (array zero-terminated=1): extra options for the cache detachment
3338 * (just passed to LVM as is)
3339 * @error: (out) (optional): place to store error (if any)
3340 *
3341 * Returns: whether the cache was successfully detached from the @cached_lv or not
3342 *
3343 * Note: synces the cache first
3344 *
3345 * Tech category: %BD_LVM_TECH_CACHE-%BD_LVM_TECH_MODE_MODIFY
3346 */
3347 gboolean bd_lvm_cache_detach (const gchar *vg_name, const gchar *cached_lv, gboolean destroy, const BDExtraArg **extra, GError **error) {
3348 g_autofree gchar *lv_id = NULL;
3349 g_autofree gchar *cache_pool_name = NULL;
3350 GVariantBuilder builder;
3351 GVariant *params = NULL;
3352
3353 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
3354 g_variant_builder_add_value (&builder, g_variant_new ("b", destroy));
3355 params = g_variant_builder_end (&builder);
3356 g_variant_builder_clear (&builder);
3357
3358 cache_pool_name = bd_lvm_cache_pool_name (vg_name, cached_lv, error);
3359 if (!cache_pool_name)
3360 return FALSE;
3361 lv_id = g_strdup_printf ("%s/%s", vg_name, cached_lv);
3362 return call_lvm_obj_method_sync (lv_id, CACHED_LV_INTF, "DetachCachePool", params, NULL, extra, TRUE, error);
3363 }
3364
3365 /**
3366 * bd_lvm_cache_create_cached_lv:
3367 * @vg_name: name of the VG to create a cached LV in
3368 * @lv_name: name of the cached LV to create
3369 * @data_size: size of the data LV
3370 * @cache_size: size of the cache (or cached LV more precisely)
3371 * @md_size: size of the cache metadata LV or 0 to use the default
3372 * @mode: cache mode for the cached LV
3373 * @flags: a combination of (ORed) #BDLVMCachePoolFlags
3374 * @slow_pvs: (array zero-terminated=1): list of slow PVs (used for the data LV)
3375 * @fast_pvs: (array zero-terminated=1): list of fast PVs (used for the cache LV)
3376 * @error: (out) (optional): place to store error (if any)
3377 *
3378 * Returns: whether the cached LV @lv_name was successfully created or not
3379 *
3380 * Tech category: %BD_LVM_TECH_CACHE-%BD_LVM_TECH_MODE_CREATE
3381 */
3382 gboolean bd_lvm_cache_create_cached_lv (const gchar *vg_name, const gchar *lv_name, guint64 data_size, guint64 cache_size, guint64 md_size, BDLVMCacheMode mode, BDLVMCachePoolFlags flags,
3383 const gchar **slow_pvs, const gchar **fast_pvs, GError **error) {
3384 gboolean success = FALSE;
3385 gchar *name = NULL;
3386 gchar *msg = NULL;
3387 guint64 progress_id = 0;
3388 GError *l_error = NULL;
3389
3390 msg = g_strdup_printf ("Started 'create cached LV %s/%s'", vg_name, lv_name);
3391 progress_id = bd_utils_report_started (msg);
3392 g_free (msg);
3393
3394 success = bd_lvm_lvcreate (vg_name, lv_name, data_size, NULL, slow_pvs, NULL, &l_error);
3395 if (!success) {
3396 g_prefix_error (&l_error, "Failed to create the data LV: ");
3397 bd_utils_report_finished (progress_id, l_error->message);
3398 g_propagate_error (error, l_error);
3399 return FALSE;
3400 }
3401
3402 /* 1/5 steps (cache pool creation has 3 steps) done */
3403 bd_utils_report_progress (progress_id, 20, "Data LV created");
3404
3405 name = g_strdup_printf ("%s_cache", lv_name);
3406 success = bd_lvm_cache_create_pool (vg_name, name, cache_size, md_size, mode, flags, fast_pvs, &l_error);
3407 if (!success) {
3408 g_prefix_error (&l_error, "Failed to create the cache pool '%s': ", name);
3409 g_free (name);
3410 bd_utils_report_finished (progress_id, l_error->message);
3411 g_propagate_error (error, l_error);
3412 return FALSE;
3413 }
3414
3415 /* 4/5 steps (cache pool creation has 3 steps) done */
3416 bd_utils_report_progress (progress_id, 80, "Cache pool created");
3417
3418 success = bd_lvm_cache_attach (vg_name, lv_name, name, NULL, &l_error);
3419 if (!success) {
3420 g_prefix_error (&l_error, "Failed to attach the cache pool '%s' to the data LV: ", name);
3421 g_free (name);
3422 bd_utils_report_finished (progress_id, l_error->message);
3423 g_propagate_error (error, l_error);
3424 return FALSE;
3425 }
3426
3427 bd_utils_report_finished (progress_id, "Completed");
3428 g_free (name);
3429 return TRUE;
3430 }
3431
3432 /**
3433 * bd_lvm_writecache_attach:
3434 * @vg_name: name of the VG containing the @data_lv and the @cache_pool_lv LVs
3435 * @data_lv: data LV to attach the @cache_lv to
3436 * @cache_lv: cache (fast) LV to attach to the @data_lv
3437 * @extra: (nullable) (array zero-terminated=1): extra options for the cache attachment
3438 * (just passed to LVM as is)
3439 * @error: (out) (optional): place to store error (if any)
3440 *
3441 * Returns: whether the @cache_lv was successfully attached to the @data_lv or not
3442 *
3443 * Note: Both @data_lv and @cache_lv will be deactivated before the operation.
3444 *
3445 * Tech category: %BD_LVM_TECH_WRITECACHE-%BD_LVM_TECH_MODE_MODIFY
3446 */
3447 gboolean bd_lvm_writecache_attach (const gchar *vg_name, const gchar *data_lv, const gchar *cache_lv, const BDExtraArg **extra, GError **error) {
3448 GVariantBuilder builder;
3449 GVariant *params = NULL;
3450 gchar *lv_id = NULL;
3451 g_autofree gchar *lv_obj_path = NULL;
3452 gboolean success = FALSE;
3453
3454 /* both LVs need to be inactive for the writecache convert to work */
3455 success = bd_lvm_lvdeactivate (vg_name, data_lv, NULL, error);
3456 if (!success)
3457 return FALSE;
3458
3459 success = bd_lvm_lvdeactivate (vg_name, cache_lv, NULL, error);
3460 if (!success)
3461 return FALSE;
3462
3463 lv_id = g_strdup_printf ("%s/%s", vg_name, data_lv);
3464 lv_obj_path = get_object_path (lv_id, error);
3465 g_free (lv_id);
3466 if (!lv_obj_path)
3467 return FALSE;
3468 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
3469 g_variant_builder_add_value (&builder, g_variant_new ("o", lv_obj_path));
3470 params = g_variant_builder_end (&builder);
3471 g_variant_builder_clear (&builder);
3472
3473 lv_id = g_strdup_printf ("%s/%s", vg_name, cache_lv);
3474
3475 success = call_lvm_obj_method_sync (lv_id, LV_INTF, "WriteCacheLv", params, NULL, extra, TRUE, error);
3476 g_free (lv_id);
3477 return success;
3478 }
3479
3480 /**
3481 * bd_lvm_writecache_detach:
3482 * @vg_name: name of the VG containing the @cached_lv
3483 * @cached_lv: name of the cached LV to detach its cache from
3484 * @destroy: whether to destroy the cache after detach or not
3485 * @extra: (nullable) (array zero-terminated=1): extra options for the cache detachment
3486 * (just passed to LVM as is)
3487 * @error: (out) (optional): place to store error (if any)
3488 *
3489 * Returns: whether the cache was successfully detached from the @cached_lv or not
3490 *
3491 * Note: synces the cache first
3492 *
3493 * Tech category: %BD_LVM_TECH_WRITECACHE-%BD_LVM_TECH_MODE_MODIFY
3494 */
3495 gboolean bd_lvm_writecache_detach (const gchar *vg_name, const gchar *cached_lv, gboolean destroy, const BDExtraArg **extra, GError **error) {
3496 return bd_lvm_cache_detach (vg_name, cached_lv, destroy, extra, error);
3497 }
3498
3499 /**
3500 * bd_lvm_writecache_create_cached_lv:
3501 * @vg_name: name of the VG to create a cached LV in
3502 * @lv_name: name of the cached LV to create
3503 * @data_size: size of the data LV
3504 * @cache_size: size of the cache (or cached LV more precisely)
3505 * @slow_pvs: (array zero-terminated=1): list of slow PVs (used for the data LV)
3506 * @fast_pvs: (array zero-terminated=1): list of fast PVs (used for the cache LV)
3507 * @error: (out) (optional): place to store error (if any)
3508 *
3509 * Returns: whether the cached LV @lv_name was successfully created or not
3510 *
3511 * Tech category: %BD_LVM_TECH_WRITECACHE-%BD_LVM_TECH_MODE_CREATE
3512 */
3513 gboolean bd_lvm_writecache_create_cached_lv (const gchar *vg_name, const gchar *lv_name, guint64 data_size, guint64 cache_size,
3514 const gchar **slow_pvs, const gchar **fast_pvs, GError **error) {
3515 gboolean success = FALSE;
3516 gchar *name = NULL;
3517 gchar *msg = NULL;
3518 guint64 progress_id = 0;
3519 GError *l_error = NULL;
3520
3521 msg = g_strdup_printf ("Started 'create cached LV %s/%s'", vg_name, lv_name);
3522 progress_id = bd_utils_report_started (msg);
3523 g_free (msg);
3524
3525 name = g_strdup_printf ("%s_writecache", lv_name);
3526 success = bd_lvm_lvcreate (vg_name, name, cache_size, NULL, fast_pvs, NULL, &l_error);
3527 if (!success) {
3528 g_prefix_error (&l_error, "Failed to create the cache LV '%s': ", name);
3529 g_free (name);
3530 bd_utils_report_finished (progress_id, l_error->message);
3531 g_propagate_error (error, l_error);
3532 return FALSE;
3533 }
3534
3535 /* 1/3 steps done */
3536 bd_utils_report_progress (progress_id, 33, "Cache LV created");
3537
3538 success = bd_lvm_lvcreate (vg_name, lv_name, data_size, NULL, slow_pvs, NULL, &l_error);
3539 if (!success) {
3540 g_prefix_error (&l_error, "Failed to create the data LV: ");
3541 g_free (name);
3542 bd_utils_report_finished (progress_id, l_error->message);
3543 g_propagate_error (error, l_error);
3544 return FALSE;
3545 }
3546
3547 /* 2/3 steps done */
3548 bd_utils_report_progress (progress_id, 66, "Data LV created");
3549
3550 success = bd_lvm_writecache_attach (vg_name, lv_name, name, NULL, &l_error);
3551 if (!success) {
3552 g_prefix_error (&l_error, "Failed to attach the cache LV '%s' to the data LV: ", name);
3553 g_free (name);
3554 bd_utils_report_finished (progress_id, l_error->message);
3555 g_propagate_error (error, l_error);
3556 return FALSE;
3557 }
3558
3559 bd_utils_report_finished (progress_id, "Completed");
3560 g_free (name);
3561 return TRUE;
3562 }
3563
3564 /**
3565 * bd_lvm_cache_pool_name:
3566 * @vg_name: name of the VG containing the @cached_lv
3567 * @cached_lv: cached LV to get the name of the its pool LV for
3568 * @error: (out) (optional): place to store error (if any)
3569 *
3570 * Returns: name of the cache pool LV used by the @cached_lv or %NULL in case of error
3571 *
3572 * Tech category: %BD_LVM_TECH_CACHE-%BD_LVM_TECH_MODE_QUERY
3573 */
3574 gchar* bd_lvm_cache_pool_name (const gchar *vg_name, const gchar *cached_lv, GError **error) {
3575 gchar *ret = NULL;
3576 gchar *name_start = NULL;
3577 gchar *name_end = NULL;
3578 gchar *pool_name = NULL;
3579 gchar *lv_spec = NULL;
3580 GVariant *prop = NULL;
3581 gchar *pool_obj_path = NULL;
3582
3583 /* same as for a thin LV, but with square brackets */
3584 lv_spec = g_strdup_printf ("%s/%s", vg_name, cached_lv);
3585 prop = get_lvm_object_property (lv_spec, CACHED_LV_INTF, "CachePool", error);
3586 g_free (lv_spec);
3587 if (!prop)
3588 return NULL;
3589 g_variant_get (prop, "o", &pool_obj_path);
3590 g_variant_unref (prop);
3591 prop = get_object_property (pool_obj_path, LV_CMN_INTF, "Name", error);
3592 g_free (pool_obj_path);
3593 if (!prop)
3594 return NULL;
3595 g_variant_get (prop, "s", &ret);
3596 g_variant_unref (prop);
3597
3598 name_start = strchr (ret, '[');
3599 if (!name_start) {
3600 g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_CACHE_INVAL,
3601 "Failed to determine cache pool name from: '%s'", ret);
3602 g_free (ret);
3603 return NULL;
3604 }
3605 name_start++;
3606
3607 name_end = strchr (ret, ']');
3608 if (!name_end) {
3609 g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_CACHE_INVAL,
3610 "Failed to determine cache pool name from: '%s'", ret);
3611 g_free (ret);
3612 return NULL;
3613 }
3614
3615 pool_name = g_strndup (name_start, name_end - name_start);
3616 g_free (ret);
3617
3618 return pool_name;
3619 }
3620
3621 /**
3622 * bd_lvm_thpool_convert:
3623 * @vg_name: name of the VG to create the new thin pool in
3624 * @data_lv: name of the LV that should become the data part of the new pool
3625 * @metadata_lv: name of the LV that should become the metadata part of the new pool
3626 * @name: (nullable): name for the thin pool (if %NULL, the name @data_lv is inherited)
3627 * @extra: (nullable) (array zero-terminated=1): extra options for the thin pool creation
3628 * (just passed to LVM as is)
3629 * @error: (out) (optional): place to store error (if any)
3630 *
3631 * Converts the @data_lv and @metadata_lv into a new thin pool in the @vg_name
3632 * VG.
3633 *
3634 * Returns: whether the new thin pool was successfully created from @data_lv and
3635 * @metadata_lv or not
3636 *
3637 * Tech category: %BD_LVM_TECH_THIN-%BD_LVM_TECH_MODE_CREATE
3638 */
3639 gboolean bd_lvm_thpool_convert (const gchar *vg_name, const gchar *data_lv, const gchar *metadata_lv, const gchar *name, const BDExtraArg **extra, GError **error) {
3640 GVariantBuilder builder;
3641 GVariant *params = NULL;
3642 gchar *obj_id = NULL;
3643 g_autofree gchar *data_lv_path = NULL;
3644 g_autofree gchar *metadata_lv_path = NULL;
3645 gboolean ret = FALSE;
3646
3647 obj_id = g_strdup_printf ("%s/%s", vg_name, data_lv);
3648 data_lv_path = get_object_path (obj_id, error);
3649 g_free (obj_id);
3650 if (!data_lv_path)
3651 return FALSE;
3652
3653 obj_id = g_strdup_printf ("%s/%s", vg_name, metadata_lv);
3654 metadata_lv_path = get_object_path (obj_id, error);
3655 g_free (obj_id);
3656 if (!metadata_lv_path)
3657 return FALSE;
3658
3659 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
3660 g_variant_builder_add_value (&builder, g_variant_new ("o", metadata_lv_path));
3661 g_variant_builder_add_value (&builder, g_variant_new ("o", data_lv_path));
3662 params = g_variant_builder_end (&builder);
3663 g_variant_builder_clear (&builder);
3664
3665 ret = call_lvm_obj_method_sync (vg_name, VG_INTF, "CreateThinPool", params, NULL, extra, TRUE, error);
3666 if (ret && name)
3667 ret = bd_lvm_lvrename (vg_name, data_lv, name, NULL, error);
3668 return ret;
3669 }
3670
3671 /**
3672 * bd_lvm_cache_pool_convert:
3673 * @vg_name: name of the VG to create the new thin pool in
3674 * @data_lv: name of the LV that should become the data part of the new pool
3675 * @metadata_lv: name of the LV that should become the metadata part of the new pool
3676 * @name: (nullable): name for the thin pool (if %NULL, the name @data_lv is inherited)
3677 * @extra: (nullable) (array zero-terminated=1): extra options for the thin pool creation
3678 * (just passed to LVM as is)
3679 * @error: (out) (optional): place to store error (if any)
3680 *
3681 * Converts the @data_lv and @metadata_lv into a new cache pool in the @vg_name
3682 * VG.
3683 *
3684 * Returns: whether the new cache pool was successfully created from @data_lv and
3685 * @metadata_lv or not
3686 *
3687 * Tech category: %BD_LVM_TECH_CACHE-%BD_LVM_TECH_MODE_CREATE
3688 */
3689 gboolean bd_lvm_cache_pool_convert (const gchar *vg_name, const gchar *data_lv, const gchar *metadata_lv, const gchar *name, const BDExtraArg **extra, GError **error) {
3690 GVariantBuilder builder;
3691 GVariant *params = NULL;
3692 gchar *obj_id = NULL;
3693 g_autofree gchar *data_lv_path = NULL;
3694 g_autofree gchar *metadata_lv_path = NULL;
3695 gboolean ret = FALSE;
3696
3697 obj_id = g_strdup_printf ("%s/%s", vg_name, data_lv);
3698 data_lv_path = get_object_path (obj_id, error);
3699 g_free (obj_id);
3700 if (!data_lv_path)
3701 return FALSE;
3702
3703 obj_id = g_strdup_printf ("%s/%s", vg_name, metadata_lv);
3704 metadata_lv_path = get_object_path (obj_id, error);
3705 g_free (obj_id);
3706 if (!metadata_lv_path)
3707 return FALSE;
3708
3709 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
3710 g_variant_builder_add_value (&builder, g_variant_new ("o", metadata_lv_path));
3711 g_variant_builder_add_value (&builder, g_variant_new ("o", data_lv_path));
3712 params = g_variant_builder_end (&builder);
3713 g_variant_builder_clear (&builder);
3714
3715 ret = call_lvm_obj_method_sync (vg_name, VG_INTF, "CreateCachePool", params, NULL, extra, TRUE, error);
3716
3717 if (ret && name)
3718 ret = bd_lvm_lvrename (vg_name, data_lv, name, NULL, error);
3719
3720 return ret;
3721 }
3722
3723 /**
3724 * bd_lvm_vdo_pool_create:
3725 * @vg_name: name of the VG to create a new LV in
3726 * @lv_name: name of the to-be-created VDO LV
3727 * @pool_name: (nullable): name of the to-be-created VDO pool LV or %NULL for default name
3728 * @data_size: requested size of the data VDO LV (physical size of the @pool_name VDO pool LV)
3729 * @virtual_size: requested virtual_size of the @lv_name VDO LV
3730 * @index_memory: amount of index memory (in bytes) or 0 for default
3731 * @compression: whether to enable compression or not
3732 * @deduplication: whether to enable deduplication or not
3733 * @write_policy: write policy for the volume
3734 * @extra: (nullable) (array zero-terminated=1): extra options for the VDO LV creation
3735 * (just passed to LVM as is)
3736 * @error: (out) (optional): place to store error (if any)
3737 *
3738 * Returns: whether the given @vg_name/@lv_name VDO LV was successfully created or not
3739 *
3740 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_CREATE
3741 */
3742 gboolean bd_lvm_vdo_pool_create (const gchar *vg_name, const gchar *lv_name, const gchar *pool_name, guint64 data_size, guint64 virtual_size, guint64 index_memory, gboolean compression, gboolean deduplication, BDLVMVDOWritePolicy write_policy, const BDExtraArg **extra, GError **error) {
3743 GVariantBuilder builder;
3744 GVariant *params = NULL;
3745 GVariant *extra_params = NULL;
3746 gchar *old_config = NULL;
3747 const gchar *write_policy_str = NULL;
3748 g_autofree gchar *name = NULL;
3749 gboolean ret = FALSE;
3750
3751 write_policy_str = bd_lvm_get_vdo_write_policy_str (write_policy, error);
3752 if (write_policy_str == NULL)
3753 return FALSE;
3754
3755 /* build the params tuple */
3756 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
3757
3758 if (!pool_name) {
3759 name = g_strdup_printf ("%s_vpool", lv_name);
3760 g_variant_builder_add_value (&builder, g_variant_new ("s", name));
3761 } else
3762 g_variant_builder_add_value (&builder, g_variant_new ("s", pool_name));
3763
3764 g_variant_builder_add_value (&builder, g_variant_new ("s", lv_name));
3765 g_variant_builder_add_value (&builder, g_variant_new ("t", data_size));
3766 g_variant_builder_add_value (&builder, g_variant_new ("t", virtual_size));
3767 params = g_variant_builder_end (&builder);
3768 g_variant_builder_clear (&builder);
3769
3770 /* and now the extra_params params */
3771 g_variant_builder_init (&builder, G_VARIANT_TYPE_DICTIONARY);
3772 g_variant_builder_add_value (&builder, g_variant_new ("{sv}", "--compression", g_variant_new ("s", compression ? "y" : "n")));
3773 g_variant_builder_add_value (&builder, g_variant_new ("{sv}", "--deduplication", g_variant_new ("s", deduplication ? "y" : "n")));
3774 extra_params = g_variant_builder_end (&builder);
3775 g_variant_builder_clear (&builder);
3776
3777 /* index_memory and write_policy can be specified only using the config */
3778 g_mutex_lock (&global_config_lock);
3779 old_config = global_config_str;
3780 if (index_memory != 0)
3781 global_config_str = g_strdup_printf ("%s allocation {vdo_index_memory_size_mb=%"G_GUINT64_FORMAT" vdo_write_policy=\"%s\"}", old_config ? old_config : "",
3782 index_memory / (1024 * 1024),
3783 write_policy_str);
3784 else
3785 global_config_str = g_strdup_printf ("%s allocation {vdo_write_policy=\"%s\"}", old_config ? old_config : "",
3786 write_policy_str);
3787
3788 ret = call_lvm_obj_method_sync (vg_name, VG_VDO_INTF, "CreateVdoPoolandLv", params, extra_params, extra, FALSE, error);
3789
3790 g_free (global_config_str);
3791 global_config_str = old_config;
3792 g_mutex_unlock (&global_config_lock);
3793
3794 return ret;
3795 }
3796
3797 /**
3798 * bd_lvm_vdo_enable_compression:
3799 * @vg_name: name of the VG containing the to-be-changed VDO pool LV
3800 * @pool_name: name of the VDO pool LV to enable compression on
3801 * @extra: (nullable) (array zero-terminated=1): extra options for the VDO change
3802 * (just passed to LVM as is)
3803 * @error: (out) (optional): place to store error (if any)
3804 *
3805 * Returns: whether compression was successfully enabled on @vg_name/@pool_name LV or not
3806 *
3807 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_MODIFY
3808 */
3809 gboolean bd_lvm_vdo_enable_compression (const gchar *vg_name, const gchar *pool_name, const BDExtraArg **extra, GError **error) {
3810 return call_vdopool_method_sync (vg_name, pool_name, "EnableCompression", NULL, NULL, extra, TRUE, error);
3811 }
3812
3813 /**
3814 * bd_lvm_vdo_disable_compression:
3815 * @vg_name: name of the VG containing the to-be-changed VDO pool LV
3816 * @pool_name: name of the VDO pool LV to disable compression on
3817 * @extra: (nullable) (array zero-terminated=1): extra options for the VDO change
3818 * (just passed to LVM as is)
3819 * @error: (out) (optional): place to store error (if any)
3820 *
3821 * Returns: whether compression was successfully disabled on @vg_name/@pool_name LV or not
3822 *
3823 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_MODIFY
3824 */
3825 gboolean bd_lvm_vdo_disable_compression (const gchar *vg_name, const gchar *pool_name, const BDExtraArg **extra, GError **error) {
3826 return call_vdopool_method_sync (vg_name, pool_name, "DisableCompression", NULL, NULL, extra, TRUE, error);
3827 }
3828
3829 /**
3830 * bd_lvm_vdo_enable_deduplication:
3831 * @vg_name: name of the VG containing the to-be-changed VDO pool LV
3832 * @pool_name: name of the VDO pool LV to enable deduplication on
3833 * @extra: (nullable) (array zero-terminated=1): extra options for the VDO change
3834 * (just passed to LVM as is)
3835 * @error: (out) (optional): place to store error (if any)
3836 *
3837 * Returns: whether deduplication was successfully enabled on @vg_name/@pool_name LV or not
3838 *
3839 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_MODIFY
3840 */
3841 gboolean bd_lvm_vdo_enable_deduplication (const gchar *vg_name, const gchar *pool_name, const BDExtraArg **extra, GError **error) {
3842 return call_vdopool_method_sync (vg_name, pool_name, "EnableDeduplication", NULL, NULL, extra, TRUE, error);
3843 }
3844
3845 /**
3846 * bd_lvm_vdo_disable_deduplication:
3847 * @vg_name: name of the VG containing the to-be-changed VDO pool LV
3848 * @pool_name: name of the VDO pool LV to disable deduplication on
3849 * @extra: (nullable) (array zero-terminated=1): extra options for the VDO change
3850 * (just passed to LVM as is)
3851 * @error: (out) (optional): place to store error (if any)
3852 *
3853 * Returns: whether deduplication was successfully disabled on @vg_name/@pool_name LV or not
3854 *
3855 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_MODIFY
3856 */
3857 gboolean bd_lvm_vdo_disable_deduplication (const gchar *vg_name, const gchar *pool_name, const BDExtraArg **extra, GError **error) {
3858 return call_vdopool_method_sync (vg_name, pool_name, "DisableDeduplication", NULL, NULL, extra, TRUE, error);
3859 }
3860
3861 /**
3862 * bd_lvm_vdo_info:
3863 * @vg_name: name of the VG that contains the LV to get information about
3864 * @lv_name: name of the LV to get information about
3865 * @error: (out) (optional): place to store error (if any)
3866 *
3867 * Returns: (transfer full): information about the @vg_name/@lv_name LV or %NULL in case
3868 * of error (the @error) gets populated in those cases)
3869 *
3870 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_QUERY
3871 */
3872 BDLVMVDOPooldata* bd_lvm_vdo_info (const gchar *vg_name, const gchar *pool_name, GError **error) {
3873 GVariant *props = NULL;
3874
3875 props = get_vdo_properties (vg_name, pool_name, error);
3876 if (!props)
3877 /* the error is already populated */
3878 return NULL;
3879
3880 return get_vdo_data_from_props (props, error);
3881 }
3882
3883 /**
3884 * bd_lvm_vdo_resize:
3885 * @vg_name: name of the VG containing the to-be-resized VDO LV
3886 * @lv_name: name of the to-be-resized VDO LV
3887 * @size: the requested new size of the VDO LV
3888 * @extra: (nullable) (array zero-terminated=1): extra options for the VDO LV resize
3889 * (just passed to LVM as is)
3890 * @error: (out) (optional): place to store error (if any)
3891 *
3892 * Returns: whether the @vg_name/@lv_name VDO LV was successfully resized or not
3893 *
3894 * Note: Reduction needs to process TRIM for reduced disk area to unmap used data blocks
3895 * from the VDO pool LV and it may take a long time.
3896 *
3897 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_MODIFY
3898 */
3899 gboolean bd_lvm_vdo_resize (const gchar *vg_name, const gchar *lv_name, guint64 size, const BDExtraArg **extra, GError **error) {
3900 return bd_lvm_lvresize (vg_name, lv_name, size, extra, error);
3901 }
3902
3903 /**
3904 * bd_lvm_vdo_pool_resize:
3905 * @vg_name: name of the VG containing the to-be-resized VDO pool LV
3906 * @pool_name: name of the to-be-resized VDO pool LV
3907 * @size: the requested new size of the VDO pool LV
3908 * @extra: (nullable) (array zero-terminated=1): extra options for the VDO pool LV resize
3909 * (just passed to LVM as is)
3910 * @error: (out) (optional): place to store error (if any)
3911 *
3912 * Returns: whether the @vg_name/@pool_name VDO pool LV was successfully resized or not
3913 *
3914 * Note: Size of the VDO pool LV can be only extended, not reduced.
3915 *
3916 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_MODIFY
3917 */
3918 gboolean bd_lvm_vdo_pool_resize (const gchar *vg_name, const gchar *pool_name, guint64 size, const BDExtraArg **extra, GError **error) {
3919 BDLVMLVdata *info = NULL;
3920
3921 info = bd_lvm_lvinfo (vg_name, pool_name, error);
3922 if (!info)
3923 return FALSE;
3924
3925 if (info->size >= size) {
3926 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_NOT_SUPPORTED,
3927 "Reducing physical size of the VDO pool LV is not supported.");
3928 bd_lvm_lvdata_free (info);
3929 return FALSE;
3930 }
3931
3932 bd_lvm_lvdata_free (info);
3933
3934 return bd_lvm_lvresize (vg_name, pool_name, size, extra, error);
3935 }
3936
3937 /**
3938 * bd_lvm_vdo_pool_convert:
3939 * @vg_name: name of the VG that contains @pool_lv
3940 * @pool_lv: name of the LV that should become the new VDO pool LV
3941 * @name: (nullable): name for the VDO LV or %NULL for default name
3942 * @virtual_size: virtual size for the new VDO LV
3943 * @extra: (nullable) (array zero-terminated=1): extra options for the VDO pool creation
3944 * (just passed to LVM as is)
3945 * @error: (out) (optional): place to store error (if any)
3946 *
3947 * Converts the @pool_lv into a new VDO pool LV in the @vg_name VG and creates a new
3948 * @name VDO LV with size @virtual_size.
3949 *
3950 * Note: All data on @pool_lv will be irreversibly destroyed.
3951 *
3952 * Returns: whether the new VDO pool LV was successfully created from @pool_lv and or not
3953 *
3954 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_CREATE&%BD_LVM_TECH_MODE_MODIFY
3955 */
3956 gboolean bd_lvm_vdo_pool_convert (const gchar *vg_name, const gchar *pool_lv, const gchar *name,
3957 guint64 virtual_size, guint64 index_memory, gboolean compression,
3958 gboolean deduplication, BDLVMVDOWritePolicy write_policy,
3959 const BDExtraArg **extra, GError **error) {
3960 GVariantBuilder builder;
3961 GVariant *params = NULL;
3962 GVariant *extra_params = NULL;
3963 g_autofree gchar *obj_id = NULL;
3964 g_autofree gchar *pool_lv_path = NULL;
3965 g_autofree gchar *lvname = NULL;
3966 gchar *old_config = NULL;
3967 const gchar *write_policy_str = NULL;
3968 gboolean ret = FALSE;
3969
3970 obj_id = g_strdup_printf ("%s/%s", vg_name, pool_lv);
3971 pool_lv_path = get_object_path (obj_id, error);
3972 if (!pool_lv_path)
3973 return FALSE;
3974
3975 write_policy_str = bd_lvm_get_vdo_write_policy_str (write_policy, error);
3976 if (write_policy_str == NULL)
3977 return FALSE;
3978
3979 /* build the params tuple */
3980 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
3981
3982 g_variant_builder_add_value (&builder, g_variant_new ("o", pool_lv_path));
3983
3984 if (!name) {
3985 lvname = g_strdup_printf ("%s_vdolv", pool_lv);
3986 g_variant_builder_add_value (&builder, g_variant_new ("s", lvname));
3987 } else
3988 g_variant_builder_add_value (&builder, g_variant_new ("s", name));
3989
3990 g_variant_builder_add_value (&builder, g_variant_new ("t", virtual_size));
3991 params = g_variant_builder_end (&builder);
3992 g_variant_builder_clear (&builder);
3993
3994 /* and now the extra_params params */
3995 g_variant_builder_init (&builder, G_VARIANT_TYPE_DICTIONARY);
3996 g_variant_builder_add_value (&builder, g_variant_new ("{sv}", "--compression", g_variant_new ("s", compression ? "y" : "n")));
3997 g_variant_builder_add_value (&builder, g_variant_new ("{sv}", "--deduplication", g_variant_new ("s", deduplication ? "y" : "n")));
3998 extra_params = g_variant_builder_end (&builder);
3999 g_variant_builder_clear (&builder);
4000
4001 /* index_memory and write_policy can be specified only using the config */
4002 g_mutex_lock (&global_config_lock);
4003 old_config = global_config_str;
4004 if (index_memory != 0)
4005 global_config_str = g_strdup_printf ("%s allocation {vdo_index_memory_size_mb=%"G_GUINT64_FORMAT" vdo_write_policy=\"%s\"}", old_config ? old_config : "",
4006 index_memory / (1024 * 1024),
4007 write_policy_str);
4008 else
4009 global_config_str = g_strdup_printf ("%s allocation {vdo_write_policy=\"%s\"}", old_config ? old_config : "",
4010 write_policy_str);
4011
4012 ret = call_lvm_obj_method_sync (vg_name, VG_VDO_INTF, "CreateVdoPool", params, extra_params, extra, FALSE, error);
4013
4014 g_free (global_config_str);
4015 global_config_str = old_config;
4016 g_mutex_unlock (&global_config_lock);
4017
4018 return ret;
4019 }
4020
4021 /**
4022 * bd_lvm_vdolvpoolname:
4023 * @vg_name: name of the VG containing the queried VDO LV
4024 * @lv_name: name of the queried VDO LV
4025 * @error: (out) (optional): place to store error (if any)
4026 *
4027 * Returns: (transfer full): the name of the pool volume for the @vg_name/@lv_name
4028 * VDO LV or %NULL if failed to determine (@error) is set in those cases
4029 *
4030 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_QUERY
4031 */
4032 gchar* bd_lvm_vdolvpoolname (const gchar *vg_name, const gchar *lv_name, GError **error) {
4033 GVariant *prop = NULL;
4034 const gchar *segtype = NULL;
4035 gchar *pool_obj_path = NULL;
4036 gchar *ret = NULL;
4037
4038 prop = get_lv_property (vg_name, lv_name, "SegType", error);
4039 if (!prop)
4040 return NULL;
4041
4042 g_variant_get_child (prop, 0, "&s", &segtype);
4043 if (g_strcmp0 (segtype, "vdo") != 0) {
4044 g_variant_unref (prop);
4045 g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_NOEXIST,
4046 "The LV '%s' is not a VDO LV and thus have no VDO pool", lv_name);
4047 return NULL;
4048 }
4049 g_variant_unref (prop);
4050
4051 prop = get_lv_property (vg_name, lv_name, "PoolLv", error);
4052 if (!prop)
4053 return NULL;
4054 g_variant_get (prop, "o", &pool_obj_path);
4055 g_variant_unref (prop);
4056
4057 prop = get_object_property (pool_obj_path, LV_CMN_INTF, "Name", error);
4058 g_free (pool_obj_path);
4059 if (!prop)
4060 return NULL;
4061 g_variant_get (prop, "s", &ret);
4062 g_variant_unref (prop);
4063
4064 return ret;
4065 }
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078