Debian Codegraph @

src/plugins/lvm/lvm.c

raw
1 /*
2 * Copyright (C) 2014 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 <libdevmapper.h>
26 #include <json-glib/json-glib.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 /**
35 * SECTION: lvm
36 * @short_description: plugin for operations with LVM
37 * @title: LVM
38 * @include: lvm.h
39 *
40 * A plugin for operations with LVM. All sizes passed in/out to/from
41 * the functions are in bytes.
42 */
43
44 /**
45 * bd_lvm_error_quark: (skip)
46 */
47 GQuark bd_lvm_error_quark (void)
48 {
49 return g_quark_from_static_string ("g-bd-lvm-error-quark");
50 }
51
52 static volatile guint avail_deps = 0;
53 static volatile guint avail_features = 0;
54 static volatile guint avail_module_deps = 0;
55 static GMutex deps_check_lock;
56
57 #define DEPS_LVM 0
58 #define DEPS_LVM_MASK (1 << DEPS_LVM)
59 #define DEPS_LVMDEVICES 1
60 #define DEPS_LVMDEVICES_MASK (1 << DEPS_LVMDEVICES)
61 #define DEPS_LVMCONFIG 2
62 #define DEPS_LVMCONFIG_MASK (1 << DEPS_LVMCONFIG)
63 #define DEPS_LAST 3
64
65 static const UtilDep deps[DEPS_LAST] = {
66 {"lvm", LVM_MIN_VERSION, "version", "LVM version:\\s+([\\d\\.]+)"},
67 {"lvmdevices", NULL, NULL, NULL},
68 {"lvmconfig", "2.03.17", "--version", "LVM version:\\s+([\\d\\.]+)"},
69 };
70
71 #define FEATURES_VDO 0
72 #define FEATURES_VDO_MASK (1 << FEATURES_VDO)
73 #define FEATURES_WRITECACHE 1
74 #define FEATURES_WRITECACHE_MASK (1 << FEATURES_WRITECACHE)
75 #define FEATURES_LAST 2
76
77 static const UtilFeatureDep features[FEATURES_LAST] = {
78 {"lvm", "vdo", "segtypes", NULL},
79 {"lvm", "writecache", "segtypes", NULL},
80 };
81
82 #define MODULE_DEPS_VDO 0
83 #define MODULE_DEPS_VDO_MASK (1 << MODULE_DEPS_VDO)
84 #define MODULE_DEPS_LAST 1
85
86 static const gchar*const module_deps[MODULE_DEPS_LAST] = { "dm-vdo" };
87
88
89 /**
90 * bd_lvm_init:
91 *
92 * Initializes the plugin. **This function is called automatically by the
93 * library's initialization functions.**
94 *
95 */
96 gboolean bd_lvm_init (void) {
97 dm_log_with_errno_init ((dm_log_with_errno_fn) redirect_dm_log);
98 #ifdef DEBUG
99 dm_log_init_verbose (LOG_DEBUG);
100 #else
101 dm_log_init_verbose (LOG_INFO);
102 #endif
103
104 return TRUE;
105 };
106
107 /**
108 * bd_lvm_close:
109 *
110 * Cleans up after the plugin. **This function is called automatically by the
111 * library's functions that unload it.**
112 *
113 */
114 void bd_lvm_close (void) {
115 dm_log_with_errno_init (NULL);
116 dm_log_init_verbose (0);
117
118 /* reset the cached availability checks so they are re-evaluated
119 next time the plugin is loaded */
120 g_atomic_int_set (&avail_deps, 0);
121 g_atomic_int_set (&avail_features, 0);
122 g_atomic_int_set (&avail_module_deps, 0);
123 }
124
125 /**
126 * bd_lvm_is_tech_avail:
127 * @tech: the queried tech
128 * @mode: a bit mask of queried modes of operation (#BDLVMTechMode) for @tech
129 * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
130 *
131 * Returns: whether the @tech-@mode combination is available -- supported by the
132 * plugin implementation and having all the runtime dependencies available
133 */
134 gboolean bd_lvm_is_tech_avail (BDLVMTech tech, guint64 mode, GError **error) {
135 switch (tech) {
136 case BD_LVM_TECH_THIN_CALCS:
137 if (mode & ~BD_LVM_TECH_MODE_QUERY) {
138 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_TECH_UNAVAIL,
139 "Only 'query' supported for thin calculations");
140 return FALSE;
141 } else
142 return TRUE;
143 case BD_LVM_TECH_CALCS:
144 if (mode & ~BD_LVM_TECH_MODE_QUERY) {
145 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_TECH_UNAVAIL,
146 "Only 'query' supported for calculations");
147 return FALSE;
148 } else
149 return TRUE;
150 case BD_LVM_TECH_VDO:
151 return check_features (&avail_features, FEATURES_VDO_MASK, features, FEATURES_LAST, &deps_check_lock, error) &&
152 check_module_deps (&avail_module_deps, MODULE_DEPS_VDO_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error) &&
153 check_deps (&avail_deps, DEPS_LVM_MASK, deps, DEPS_LAST, &deps_check_lock, error);
154 case BD_LVM_TECH_WRITECACHE:
155 return check_features (&avail_features, FEATURES_WRITECACHE_MASK, features, FEATURES_LAST, &deps_check_lock, error) &&
156 check_deps (&avail_deps, DEPS_LVM_MASK, deps, DEPS_LAST, &deps_check_lock, error);
157 case BD_LVM_TECH_DEVICES:
158 return check_deps (&avail_deps, DEPS_LVMDEVICES_MASK, deps, DEPS_LAST, &deps_check_lock, error);
159 case BD_LVM_TECH_CONFIG:
160 return check_deps (&avail_deps, DEPS_LVMCONFIG_MASK, deps, DEPS_LAST, &deps_check_lock, error);
161 default:
162 /* everything is supported by this implementation of the plugin */
163 return check_deps (&avail_deps, DEPS_LVM_MASK, deps, DEPS_LAST, &deps_check_lock, error);
164 }
165 }
166
167 static gboolean call_lvm_and_report_error (const gchar **args, const BDExtraArg **extra, gboolean lock_config, GError **error) {
168 gboolean success = FALSE;
169 guint i = 0;
170 guint args_length = g_strv_length ((gchar **) args);
171 g_autofree gchar *config_arg = NULL;
172 g_autofree gchar *devices_arg = NULL;
173
174 if (!check_deps (&avail_deps, DEPS_LVM_MASK, deps, DEPS_LAST, &deps_check_lock, error))
175 return FALSE;
176
177 /* don't allow global config string changes during the run */
178 if (lock_config)
179 g_mutex_lock (&global_config_lock);
180
181 /* allocate enough space for the args plus "lvm", "--config", "--devices" and NULL */
182 const gchar **argv = g_new0 (const gchar*, args_length + 4);
183
184 /* construct argv from args with "lvm" prepended */
185 argv[0] = "lvm";
186 for (i=0; i < args_length; i++)
187 argv[i+1] = args[i];
188 if (global_config_str) {
189 config_arg = g_strdup_printf ("--config=%s", global_config_str);
190 argv[++args_length] = config_arg;
191 }
192 if (global_devices_str) {
193 devices_arg = g_strdup_printf ("--devices=%s", global_devices_str);
194 argv[++args_length] = devices_arg;
195 }
196 argv[++args_length] = NULL;
197
198 success = bd_utils_exec_and_report_error (argv, extra, error);
199 if (lock_config)
200 g_mutex_unlock (&global_config_lock);
201 g_free (argv);
202
203 return success;
204 }
205
206 static gboolean call_lvm_and_capture_output (const gchar **args, const BDExtraArg **extra, gchar **output, GError **error) {
207 gboolean success = FALSE;
208 guint i = 0;
209 guint args_length = g_strv_length ((gchar **) args);
210 g_autofree gchar *config_arg = NULL;
211 g_autofree gchar *devices_arg = NULL;
212
213 if (!check_deps (&avail_deps, DEPS_LVM_MASK, deps, DEPS_LAST, &deps_check_lock, error))
214 return FALSE;
215
216 /* don't allow global config string changes during the run */
217 g_mutex_lock (&global_config_lock);
218
219 /* allocate enough space for the args plus "lvm", "--config", "--devices" and NULL */
220 const gchar **argv = g_new0 (const gchar*, args_length + 4);
221
222 /* construct argv from args with "lvm" prepended */
223 argv[0] = "lvm";
224 for (i=0; i < args_length; i++)
225 argv[i+1] = args[i];
226 if (global_config_str) {
227 config_arg = g_strdup_printf ("--config=%s", global_config_str);
228 argv[++args_length] = config_arg;
229 }
230 if (global_devices_str) {
231 devices_arg = g_strdup_printf ("--devices=%s", global_devices_str);
232 argv[++args_length] = devices_arg;
233 }
234 argv[++args_length] = NULL;
235
236 success = bd_utils_exec_and_capture_output (argv, extra, output, error);
237 g_mutex_unlock (&global_config_lock);
238 g_free (argv);
239
240 return success;
241 }
242
243 static gboolean call_lvm_and_report_progress (const gchar **args, const BDExtraArg **extra, BDUtilsProgExtract prog_extract, gint *proc_status, GError **error) {
244 gboolean success = FALSE;
245 guint i = 0;
246 guint args_length = g_strv_length ((gchar **) args);
247 g_autofree gchar *config_arg = NULL;
248 g_autofree gchar *devices_arg = NULL;
249
250 if (!check_deps (&avail_deps, DEPS_LVM_MASK, deps, DEPS_LAST, &deps_check_lock, error))
251 return FALSE;
252
253 /* don't allow global config string changes during the run */
254 g_mutex_lock (&global_config_lock);
255
256 /* allocate enough space for the args plus "lvm", "--config", "--devices" and NULL */
257 const gchar **argv = g_new0 (const gchar*, args_length + 4);
258
259 /* construct argv from args with "lvm" prepended */
260 argv[0] = "lvm";
261 for (i=0; i < args_length; i++)
262 argv[i+1] = args[i];
263 if (global_config_str) {
264 config_arg = g_strdup_printf ("--config=%s", global_config_str);
265 argv[++args_length] = config_arg;
266 }
267 if (global_devices_str) {
268 devices_arg = g_strdup_printf ("--devices=%s", global_devices_str);
269 argv[++args_length] = devices_arg;
270 }
271 argv[++args_length] = NULL;
272
273 success = bd_utils_exec_and_report_progress (argv, extra, prog_extract, proc_status, error);
274 g_mutex_unlock (&global_config_lock);
275 g_free (argv);
276
277 return success;
278 }
279
280 /**
281 * call_lvm_and_parse_json_report:
282 * @args: LVM command arguments (must include --reportformat json_std)
283 * @report_key: the key in the report object to extract (e.g., "pv", "vg", "lv")
284 * @out_parser: (out): the JsonParser that owns the returned array; caller must unref
285 * @allow_no_output: if %TRUE, return %NULL without error when there is no output
286 * @error: (out) (optional): place to store error
287 *
288 * Runs an LVM command, parses its JSON output, and returns the array of objects
289 * from the report section identified by @report_key.
290 *
291 * Returns: (transfer none): the JsonArray from the report, owned by @out_parser,
292 * or %NULL on error (or empty output if @allow_no_output is %TRUE)
293 */
294 static JsonArray* call_lvm_and_parse_json_report (const gchar **args,
295 const gchar *report_key,
296 JsonParser **out_parser,
297 gboolean allow_no_output,
298 GError **error) {
299 gboolean success = FALSE;
300 gchar *output = NULL;
301 JsonParser *parser = NULL;
302 JsonArray *report_array = NULL;
303 JsonObject *report_obj = NULL;
304 JsonArray *data_array = NULL;
305 GError *l_error = NULL;
306
307 *out_parser = NULL;
308
309 success = call_lvm_and_capture_output (args, NULL, &output, &l_error);
310 if (!success) {
311 if (allow_no_output && g_error_matches (l_error, BD_UTILS_EXEC_ERROR, BD_UTILS_EXEC_ERROR_NOOUT)) {
312 g_clear_error (&l_error);
313 return NULL;
314 }
315 g_propagate_error (error, l_error);
316 return NULL;
317 }
318
319 parser = json_parser_new ();
320 if (!json_parser_load_from_data (parser, output, -1, &l_error)) {
321 g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_PARSE,
322 "Failed to parse JSON output from LVM: %s", l_error->message);
323 g_error_free (l_error);
324 g_object_unref (parser);
325 g_free (output);
326 return NULL;
327 }
328 g_free (output);
329
330 report_array = json_object_get_array_member (
331 json_node_get_object (json_parser_get_root (parser)), "report");
332
333 if (!report_array || json_array_get_length (report_array) == 0) {
334 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_PARSE,
335 "Failed to parse LVM report");
336 g_object_unref (parser);
337 return NULL;
338 }
339
340 report_obj = json_array_get_object_element (report_array, 0);
341 data_array = json_object_get_array_member (report_obj, report_key);
342
343 if (!data_array) {
344 g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_PARSE,
345 "Failed to parse LVM report: missing '%s' key", report_key);
346 g_object_unref (parser);
347 return NULL;
348 }
349
350 *out_parser = parser;
351 return data_array;
352 }
353
354
355 /* LVM json_std outputs "" for absent string values (e.g. vg_name for a PV
356 not in any VG). Convert these to NULL for consistency with the dbus plugin. */
357 static gchar* _lvm_json_get_string (JsonObject *obj, const gchar *key) {
358 const gchar *value = json_object_get_string_member_with_default (obj, key, NULL);
359 if (!value || !*value)
360 return NULL;
361 return g_strdup (value);
362 }
363
364 static BDLVMPVdata* get_pv_data_from_json (JsonObject *pv_obj) {
365 BDLVMPVdata *data = g_new0 (BDLVMPVdata, 1);
366 JsonArray *tags_array = NULL;
367 guint tags_len = 0;
368
369 data->pv_name = _lvm_json_get_string (pv_obj, "pv_name");
370 data->pv_uuid = _lvm_json_get_string (pv_obj, "pv_uuid");
371
372 data->pv_free = g_ascii_strtoull (json_object_get_string_member_with_default (pv_obj, "pv_free", "0"), NULL, 0);
373 data->pv_size = g_ascii_strtoull (json_object_get_string_member_with_default (pv_obj, "pv_size", "0"), NULL, 0);
374 data->pe_start = g_ascii_strtoull (json_object_get_string_member_with_default (pv_obj, "pe_start", "0"), NULL, 0);
375
376 data->vg_name = _lvm_json_get_string (pv_obj, "vg_name");
377 data->vg_uuid = _lvm_json_get_string (pv_obj, "vg_uuid");
378
379 data->vg_size = g_ascii_strtoull (json_object_get_string_member_with_default (pv_obj, "vg_size", "0"), NULL, 0);
380 data->vg_free = g_ascii_strtoull (json_object_get_string_member_with_default (pv_obj, "vg_free", "0"), NULL, 0);
381 data->vg_extent_size = g_ascii_strtoull (json_object_get_string_member_with_default (pv_obj, "vg_extent_size", "0"), NULL, 0);
382
383 data->vg_extent_count = (guint64) json_object_get_int_member_with_default (pv_obj, "vg_extent_count", 0);
384 data->vg_free_count = (guint64) json_object_get_int_member_with_default (pv_obj, "vg_free_count", 0);
385 data->vg_pv_count = (guint64) json_object_get_int_member_with_default (pv_obj, "pv_count", 0);
386
387 if (json_object_has_member (pv_obj, "pv_tags")) {
388 tags_array = json_object_get_array_member (pv_obj, "pv_tags");
389 tags_len = json_array_get_length (tags_array);
390 data->pv_tags = g_new0 (gchar *, tags_len + 1);
391 for (guint i = 0; i < tags_len; i++)
392 data->pv_tags[i] = g_strdup (json_array_get_string_element (tags_array, i));
393 data->pv_tags[tags_len] = NULL;
394 }
395
396 data->missing = (gboolean) json_object_get_int_member_with_default (pv_obj, "pv_missing", 0);
397
398 return data;
399 }
400
401 static BDLVMVGdata* get_vg_data_from_json (JsonObject *vg_obj) {
402 BDLVMVGdata *data = g_new0 (BDLVMVGdata, 1);
403 JsonArray *tags_array = NULL;
404 guint tags_len = 0;
405
406 data->name = _lvm_json_get_string (vg_obj, "vg_name");
407 data->uuid = _lvm_json_get_string (vg_obj, "vg_uuid");
408
409 data->size = g_ascii_strtoull (json_object_get_string_member_with_default (vg_obj, "vg_size", "0"), NULL, 0);
410 data->free = g_ascii_strtoull (json_object_get_string_member_with_default (vg_obj, "vg_free", "0"), NULL, 0);
411 data->extent_size = g_ascii_strtoull (json_object_get_string_member_with_default (vg_obj, "vg_extent_size", "0"), NULL, 0);
412
413 data->extent_count = (guint64) json_object_get_int_member_with_default (vg_obj, "vg_extent_count", 0);
414 data->free_count = (guint64) json_object_get_int_member_with_default (vg_obj, "vg_free_count", 0);
415 data->pv_count = (guint64) json_object_get_int_member_with_default (vg_obj, "pv_count", 0);
416
417 data->exported = (gboolean) json_object_get_int_member_with_default (vg_obj, "vg_exported", 0);
418
419 if (json_object_has_member (vg_obj, "vg_tags")) {
420 tags_array = json_object_get_array_member (vg_obj, "vg_tags");
421 tags_len = json_array_get_length (tags_array);
422 data->vg_tags = g_new0 (gchar *, tags_len + 1);
423 for (guint i = 0; i < tags_len; i++)
424 data->vg_tags[i] = g_strdup (json_array_get_string_element (tags_array, i));
425 data->vg_tags[tags_len] = NULL;
426 }
427
428 return data;
429 }
430
431 static gchar **prepare_sublvs (gchar **values, gchar *extra_value) {
432 /* LVM2 guarantees: No "/dev/" prefixes or "[unknown]" in a list of sub-lvs. */
433 gboolean found_extra = FALSE;
434 for (int i = 0; values[i]; i++) {
435 gchar *paren = strrchr (values[i], '(');
436 if (paren) {
437 /* LVM2 guarantees: start offsets of sub-lvs are always zero. */
438 *paren = '\0';
439 }
440 if (g_strcmp0 (extra_value, values[i]) == 0)
441 found_extra = TRUE;
442 }
443 if (extra_value && *extra_value && !found_extra) {
444 int len = g_strv_length (values);
445 gchar **new_values = g_new0 (gchar *, len+2);
446 for (int j = 0; j < len; j++)
447 new_values[j] = values[j];
448 new_values[len] = g_strdup (extra_value);
449 g_free (values);
450 values = new_values;
451 }
452 return values;
453 }
454
455 static BDLVMLVdata* get_lv_data_from_json (JsonObject *lv_obj) {
456 BDLVMLVdata *data = g_new0 (BDLVMLVdata, 1);
457 JsonArray *array = NULL;
458 guint array_len = 0;
459 const gchar *value = NULL;
460
461 data->lv_name = _lvm_json_get_string (lv_obj, "lv_name");
462 data->vg_name = _lvm_json_get_string (lv_obj, "vg_name");
463 data->uuid = _lvm_json_get_string (lv_obj, "lv_uuid");
464
465 data->size = g_ascii_strtoull (json_object_get_string_member_with_default (lv_obj, "lv_size", "0"), NULL, 0);
466
467 data->attr = _lvm_json_get_string (lv_obj, "lv_attr");
468
469 value = json_object_get_string_member_with_default (lv_obj, "segtype", NULL);
470 if (g_strcmp0 (value, "error") == 0) {
471 /* A segment type "error" appears when "vgreduce
472 * --removemissing" replaces a missing PV with a device mapper
473 * "error" target. It very likely was a "linear" segment before that
474 * and will again be "linear" after repair. Let's not expose
475 * this implementation detail.
476 */
477 data->segtype = g_strdup ("linear");
478 } else
479 data->segtype = (value && *value) ? g_strdup (value) : NULL;
480
481 data->origin = _lvm_json_get_string (lv_obj, "origin");
482 data->pool_lv = _lvm_json_get_string (lv_obj, "pool_lv");
483 data->data_lv = _lvm_json_get_string (lv_obj, "data_lv");
484 data->metadata_lv = _lvm_json_get_string (lv_obj, "metadata_lv");
485
486 /* lv_role is an array in JSON, join with "," */
487 if (json_object_has_member (lv_obj, "lv_role")) {
488 array = json_object_get_array_member (lv_obj, "lv_role");
489 array_len = json_array_get_length (array);
490 if (array_len > 0) {
491 gchar **role_strs = g_new0 (gchar *, array_len + 1);
492 for (guint i = 0; i < array_len; i++)
493 role_strs[i] = (gchar *) json_array_get_string_element (array, i);
494 data->roles = g_strjoinv (",", role_strs);
495 g_free (role_strs);
496 }
497 }
498
499 data->move_pv = _lvm_json_get_string (lv_obj, "move_pv");
500
501 if (json_object_has_member (lv_obj, "data_percent") && !json_object_get_null_member (lv_obj, "data_percent"))
502 data->data_percent = (guint64) json_object_get_double_member (lv_obj, "data_percent");
503 else
504 data->data_percent = 0;
505
506 if (json_object_has_member (lv_obj, "metadata_percent") && !json_object_get_null_member (lv_obj, "metadata_percent"))
507 data->metadata_percent = (guint64) json_object_get_double_member (lv_obj, "metadata_percent");
508 else
509 data->metadata_percent = 0;
510
511 if (json_object_has_member (lv_obj, "copy_percent") && !json_object_get_null_member (lv_obj, "copy_percent"))
512 data->copy_percent = (guint64) json_object_get_double_member (lv_obj, "copy_percent");
513 else
514 data->copy_percent = 0;
515
516 if (json_object_has_member (lv_obj, "lv_tags")) {
517 array = json_object_get_array_member (lv_obj, "lv_tags");
518 array_len = json_array_get_length (array);
519 data->lv_tags = g_new0 (gchar *, array_len + 1);
520 for (guint i = 0; i < array_len; i++)
521 data->lv_tags[i] = g_strdup (json_array_get_string_element (array, i));
522 data->lv_tags[array_len] = NULL;
523 }
524
525 /* replace '[' and ']' (marking LVs as internal) with spaces and then
526 remove all the leading and trailing whitespace */
527 if (data->pool_lv)
528 g_strstrip (g_strdelimit (data->pool_lv, "[]", ' '));
529 if (data->data_lv)
530 g_strstrip (g_strdelimit (data->data_lv, "[]", ' '));
531 if (data->metadata_lv)
532 g_strstrip (g_strdelimit (data->metadata_lv, "[]", ' '));
533
534 /* devices (present only in tree variant) */
535 if (json_object_has_member (lv_obj, "devices")) {
536 array = json_object_get_array_member (lv_obj, "devices");
537 array_len = json_array_get_length (array);
538
539 /* If the list is empty, this is probably an "error" segment
540 resulting from a "vgreduce --removemissing" operation.
541
542 If the first value starts with "[unknown]", it is a segment with a
543 missing PV that hasn't been converted to an "error" segment yet.
544
545 If it starts with "/dev/", we have a single PV.
546 Additional segments are added in merge_lv_data below.
547
548 Otherwise it is a list of sub-lvs.
549 */
550 if (array_len == 0 || g_str_has_prefix (json_array_get_string_element (array, 0), "[unknown]")) {
551 data->segs = g_new0 (BDLVMSEGdata *, 1);
552 data->segs[0] = NULL;
553 } else if (g_str_has_prefix (json_array_get_string_element (array, 0), "/dev/")) {
554 data->segs = g_new0 (BDLVMSEGdata *, 2);
555 data->segs[0] = g_new0 (BDLVMSEGdata, 1);
556 data->segs[1] = NULL;
557
558 gchar *dev = g_strdup (json_array_get_string_element (array, 0));
559 gchar *paren = strrchr (dev, '(');
560 if (paren) {
561 data->segs[0]->pv_start_pe = g_ascii_strtoull (paren + 1, NULL, 10);
562 *paren = '\0';
563 }
564 data->segs[0]->pvdev = dev;
565
566 value = json_object_get_string_member_with_default (lv_obj, "seg_size_pe", NULL);
567 if (value)
568 data->segs[0]->size_pe = g_ascii_strtoull (value, NULL, 0);
569 } else {
570 /* sub-LVs */
571 gchar **values = g_new0 (gchar *, array_len + 1);
572 for (guint i = 0; i < array_len; i++)
573 values[i] = g_strdup (json_array_get_string_element (array, i));
574 values[array_len] = NULL;
575
576 data->data_lvs = prepare_sublvs (values, data->data_lv);
577
578 if (json_object_has_member (lv_obj, "metadata_devices")) {
579 JsonArray *md_array = json_object_get_array_member (lv_obj, "metadata_devices");
580 guint md_len = json_array_get_length (md_array);
581 gchar **md_values = g_new0 (gchar *, md_len + 1);
582 for (guint i = 0; i < md_len; i++)
583 md_values[i] = g_strdup (json_array_get_string_element (md_array, i));
584 md_values[md_len] = NULL;
585 data->metadata_lvs = prepare_sublvs (md_values, data->metadata_lv);
586 }
587 }
588 }
589
590 return data;
591 }
592
593 static void merge_lv_data (BDLVMLVdata *data, BDLVMLVdata *more_data) {
594 /* LVM2 guarantees:
595 - more_data->data_lvs is NULL
596 - more_data->metadata_lvs is NULL
597 - more_data->segs has zero or one entry
598 - more_data->seg_type is the same as data->seg_type (after mapping "error" to "linear")
599 */
600
601 if (more_data->segs && more_data->segs[0]) {
602 int i;
603 for (i = 0; data->segs && data->segs[i]; i++)
604 ;
605
606 BDLVMSEGdata **new_segs = g_new0 (BDLVMSEGdata *, i+2);
607 for (i = 0; data->segs && data->segs[i]; i++)
608 new_segs[i] = data->segs[i];
609 new_segs[i] = more_data->segs[0];
610 g_free (data->segs);
611 data->segs = new_segs;
612 more_data->segs[0] = NULL;
613 }
614 }
615
616 static BDLVMVDOPooldata* get_vdo_data_from_json (JsonObject *vdo_obj) {
617 BDLVMVDOPooldata *data = g_new0 (BDLVMVDOPooldata, 1);
618 const gchar *value = NULL;
619
620 value = json_object_get_string_member_with_default (vdo_obj, "vdo_operating_mode", NULL);
621 if (g_strcmp0 (value, "recovering") == 0)
622 data->operating_mode = BD_LVM_VDO_MODE_RECOVERING;
623 else if (g_strcmp0 (value, "read-only") == 0)
624 data->operating_mode = BD_LVM_VDO_MODE_READ_ONLY;
625 else if (g_strcmp0 (value, "normal") == 0)
626 data->operating_mode = BD_LVM_VDO_MODE_NORMAL;
627 else {
628 bd_utils_log_format (BD_UTILS_LOG_DEBUG, "Unknown VDO operating mode: %s", value);
629 data->operating_mode = BD_LVM_VDO_MODE_UNKNOWN;
630 }
631
632 value = json_object_get_string_member_with_default (vdo_obj, "vdo_compression_state", NULL);
633 if (g_strcmp0 (value, "online") == 0)
634 data->compression_state = BD_LVM_VDO_COMPRESSION_ONLINE;
635 else if (g_strcmp0 (value, "offline") == 0)
636 data->compression_state = BD_LVM_VDO_COMPRESSION_OFFLINE;
637 else {
638 bd_utils_log_format (BD_UTILS_LOG_DEBUG, "Unknown VDO compression state: %s", value);
639 data->compression_state = BD_LVM_VDO_COMPRESSION_UNKNOWN;
640 }
641
642 value = json_object_get_string_member_with_default (vdo_obj, "vdo_index_state", NULL);
643 if (g_strcmp0 (value, "error") == 0)
644 data->index_state = BD_LVM_VDO_INDEX_ERROR;
645 else if (g_strcmp0 (value, "closed") == 0)
646 data->index_state = BD_LVM_VDO_INDEX_CLOSED;
647 else if (g_strcmp0 (value, "opening") == 0)
648 data->index_state = BD_LVM_VDO_INDEX_OPENING;
649 else if (g_strcmp0 (value, "closing") == 0)
650 data->index_state = BD_LVM_VDO_INDEX_CLOSING;
651 else if (g_strcmp0 (value, "offline") == 0)
652 data->index_state = BD_LVM_VDO_INDEX_OFFLINE;
653 else if (g_strcmp0 (value, "online") == 0)
654 data->index_state = BD_LVM_VDO_INDEX_ONLINE;
655 else {
656 bd_utils_log_format (BD_UTILS_LOG_DEBUG, "Unknown VDO index state: %s", value);
657 data->index_state = BD_LVM_VDO_INDEX_UNKNOWN;
658 }
659
660 value = json_object_get_string_member_with_default (vdo_obj, "vdo_write_policy", NULL);
661 if (g_strcmp0 (value, "auto") == 0)
662 data->write_policy = BD_LVM_VDO_WRITE_POLICY_AUTO;
663 else if (g_strcmp0 (value, "sync") == 0)
664 data->write_policy = BD_LVM_VDO_WRITE_POLICY_SYNC;
665 else if (g_strcmp0 (value, "async") == 0)
666 data->write_policy = BD_LVM_VDO_WRITE_POLICY_ASYNC;
667 else {
668 bd_utils_log_format (BD_UTILS_LOG_DEBUG, "Unknown VDO write policy: %s", value);
669 data->write_policy = BD_LVM_VDO_WRITE_POLICY_UNKNOWN;
670 }
671
672 value = json_object_get_string_member_with_default (vdo_obj, "vdo_index_memory_size", "0");
673 data->index_memory_size = g_ascii_strtoull (value, NULL, 0);
674
675 if (json_object_has_member (vdo_obj, "vdo_used_size") && !json_object_get_null_member (vdo_obj, "vdo_used_size"))
676 data->used_size = (guint64) json_object_get_int_member (vdo_obj, "vdo_used_size");
677 else
678 data->used_size = 0;
679
680 if (json_object_has_member (vdo_obj, "vdo_saving_percent") && !json_object_get_null_member (vdo_obj, "vdo_saving_percent"))
681 data->saving_percent = (gint32) json_object_get_double_member (vdo_obj, "vdo_saving_percent");
682 else
683 data->saving_percent = 0;
684
685 data->compression = (gboolean) json_object_get_int_member_with_default (vdo_obj, "vdo_compression", 0);
686 data->deduplication = (gboolean) json_object_get_int_member_with_default (vdo_obj, "vdo_deduplication", 0);
687
688 return data;
689 }
690
691 /**
692 * bd_lvm_pvcreate:
693 * @device: the device to make PV from
694 * @data_alignment: data (first PE) alignment or 0 to use the default
695 * @metadata_size: size of the area reserved for metadata or 0 to use the default
696 * @extra: (nullable) (array zero-terminated=1): extra options for the PV creation
697 * (just passed to LVM as is)
698 * @error: (out) (optional): place to store error (if any)
699 *
700 * Returns: whether the PV was successfully created or not
701 *
702 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_CREATE
703 */
704 gboolean bd_lvm_pvcreate (const gchar *device, guint64 data_alignment, guint64 metadata_size, const BDExtraArg **extra, GError **error) {
705 const gchar *args[5] = {"pvcreate", device, NULL, NULL, NULL};
706 guint next_arg = 2;
707 gchar *dataalign_str = NULL;
708 gchar *metadata_str = NULL;
709 gboolean ret = FALSE;
710
711 if (data_alignment != 0) {
712 dataalign_str = g_strdup_printf ("--dataalignment=%"G_GUINT64_FORMAT"K", data_alignment / 1024);
713 args[next_arg++] = dataalign_str;
714 }
715
716 if (metadata_size != 0) {
717 metadata_str = g_strdup_printf ("--metadatasize=%"G_GUINT64_FORMAT"K", metadata_size / 1024);
718 args[next_arg++] = metadata_str;
719 }
720
721 ret = call_lvm_and_report_error (args, extra, TRUE, error);
722 g_free (dataalign_str);
723 g_free (metadata_str);
724
725 return ret;
726 }
727
728 /**
729 * bd_lvm_pvresize:
730 * @device: the device to resize
731 * @size: the new requested size of the PV or 0 if it should be adjusted to device's size
732 * @extra: (nullable) (array zero-terminated=1): extra options for the PV resize
733 * (just passed to LVM as is)
734 * @error: (out) (optional): place to store error (if any)
735 *
736 * Returns: whether the PV's size was successfully changed or not
737 *
738 * If given @size different from 0, sets the PV's size to the given value (see
739 * pvresize(8)). If given @size 0, adjusts the PV's size to the underlying
740 * block device's size.
741 *
742 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
743 */
744 gboolean bd_lvm_pvresize (const gchar *device, guint64 size, const BDExtraArg **extra, GError **error) {
745 gchar *size_str = NULL;
746 const gchar *args[6] = {"pvresize", "-y", NULL, NULL, NULL, NULL};
747 guint8 next_pos = 2;
748 guint8 to_free_pos = 0;
749 gboolean ret = FALSE;
750
751 if (size != 0) {
752 size_str = g_strdup_printf ("%"G_GUINT64_FORMAT"K", size / 1024);
753 args[next_pos] = "--setphysicalvolumesize";
754 next_pos++;
755 args[next_pos] = size_str;
756 to_free_pos = next_pos;
757 next_pos++;
758 }
759
760 args[next_pos] = device;
761
762 ret = call_lvm_and_report_error (args, extra, TRUE, error);
763 if (to_free_pos > 0)
764 g_free ((gchar *) args[to_free_pos]);
765
766 return ret;
767 }
768
769 /**
770 * bd_lvm_pvremove:
771 * @device: the PV device to be removed/destroyed
772 * @extra: (nullable) (array zero-terminated=1): extra options for the PV removal
773 * (just passed to LVM as is)
774 * @error: (out) (optional): place to store error (if any)
775 *
776 * Returns: whether the PV was successfully removed/destroyed or not
777 *
778 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_REMOVE
779 */
780 gboolean bd_lvm_pvremove (const gchar *device, const BDExtraArg **extra, GError **error) {
781 /* one has to be really persuasive to remove a PV (the double --force is not
782 bug, at least not in this code) */
783 const gchar *args[6] = {"pvremove", "--force", "--force", "--yes", device, NULL};
784
785 return call_lvm_and_report_error (args, extra, TRUE, error);
786 }
787
788 static gboolean extract_pvmove_progress (const gchar *line, guint8 *completion) {
789 gchar *num_start = NULL;
790 gint n_scanned = 0;
791 guint8 try_completion = 0;
792
793 num_start = strrchr (line, ' ');
794 if (!num_start)
795 return FALSE;
796 num_start++;
797 n_scanned = sscanf (num_start, "%hhu", &try_completion);
798 if (n_scanned == 1) {
799 *completion = try_completion;
800 return TRUE;
801 }
802 return FALSE;
803 }
804
805 /**
806 * bd_lvm_pvmove:
807 * @src: the PV device to move extents off of
808 * @dest: (nullable): the PV device to move extents onto or %NULL
809 * @extra: (nullable) (array zero-terminated=1): extra options for the PV move
810 * (just passed to LVM as is)
811 * @error: (out) (optional): place to store error (if any)
812 *
813 * Returns: whether the extents from the @src PV where successfully moved or not
814 *
815 * If @dest is %NULL, VG allocation rules are used for the extents from the @src
816 * PV (see pvmove(8)).
817 *
818 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
819 */
820 gboolean bd_lvm_pvmove (const gchar *src, const gchar *dest, const BDExtraArg **extra, GError **error) {
821 const gchar *args[6] = {"pvmove", "-i", "1", src, NULL, NULL};
822 gint status = 0;
823 if (dest)
824 args[4] = dest;
825
826 return call_lvm_and_report_progress (args, extra, extract_pvmove_progress, &status, error);
827 }
828
829 /**
830 * bd_lvm_pvscan:
831 * @device: (nullable): the device to scan for PVs or %NULL
832 * @update_cache: whether to update the lvmetad cache or not
833 * @extra: (nullable) (array zero-terminated=1): extra options for the PV scan
834 * (just passed to LVM as is)
835 * @error: (out) (optional): place to store error (if any)
836 *
837 * Returns: whether the system or @device was successfully scanned for PVs or not
838 *
839 * The @device argument is used only if @update_cache is %TRUE. Otherwise the
840 * whole system is scanned for PVs.
841 *
842 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
843 */
844 gboolean bd_lvm_pvscan (const gchar *device, gboolean update_cache, const BDExtraArg **extra, GError **error) {
845 const gchar *args[4] = {"pvscan", NULL, NULL, NULL};
846 if (update_cache) {
847 args[1] = "--cache";
848 args[2] = device;
849 }
850 else
851 if (device)
852 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Ignoring the device argument in pvscan (cache update not requested)");
853
854 return call_lvm_and_report_error (args, extra, TRUE, error);
855 }
856
857 static gboolean _manage_lvm_tags (const gchar *devspec, const gchar **tags, const gchar *action, const gchar *cmd, GError **error) {
858 guint tags_len = g_strv_length ((gchar **) tags);
859 const gchar **argv = g_new0 (const gchar*, 2 * tags_len + 3);
860 guint next_arg = 0;
861 gboolean success = FALSE;
862
863 argv[next_arg++] = cmd;
864 for (guint i = 0; i < tags_len; i++) {
865 argv[next_arg++] = action;
866 argv[next_arg++] = tags[i];
867 }
868 argv[next_arg++] = devspec;
869 argv[next_arg] = NULL;
870
871 success = call_lvm_and_report_error (argv, NULL, TRUE, error);
872 g_free (argv);
873 return success;
874 }
875
876 /**
877 * bd_lvm_add_pv_tags:
878 * @device: the device to set PV tags for
879 * @tags: (array zero-terminated=1): list of tags to add
880 * @error: (out) (optional): place to store error (if any)
881 *
882 * Returns: whether the tags were successfully added to @device or not
883 *
884 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
885 */
886 gboolean bd_lvm_add_pv_tags (const gchar *device, const gchar **tags, GError **error) {
887 return _manage_lvm_tags (device, tags, "--addtag", "pvchange", error);
888 }
889
890 /**
891 * bd_lvm_delete_pv_tags:
892 * @device: the device to set PV tags for
893 * @tags: (array zero-terminated=1): list of tags to remove
894 * @error: (out) (optional): place to store error (if any)
895 *
896 * Returns: whether the tags were successfully removed from @device or not
897 *
898 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
899 */
900 gboolean bd_lvm_delete_pv_tags (const gchar *device, const gchar **tags, GError **error) {
901 return _manage_lvm_tags (device, tags, "--deltag", "pvchange", error);
902 }
903
904 /**
905 * bd_lvm_pvinfo:
906 * @device: a PV to get information about or %NULL
907 * @error: (out) (optional): place to store error (if any)
908 *
909 * Returns: (transfer full): information about the PV on the given @device or
910 * %NULL in case of error (the @error) gets populated in those cases)
911 *
912 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
913 */
914 BDLVMPVdata* bd_lvm_pvinfo (const gchar *device, GError **error) {
915 const gchar *args[10] = {"pvs", "--units=b", "--nosuffix",
916 "--reportformat", "json_std",
917 "-o", "pv_name,pv_uuid,pv_free,pv_size,pe_start,vg_name,vg_uuid,vg_size," \
918 "vg_free,vg_extent_size,vg_extent_count,vg_free_count,pv_count,pv_tags,pv_missing",
919 device, NULL};
920 JsonParser *parser = NULL;
921 JsonArray *pv_array = NULL;
922 BDLVMPVdata *ret = NULL;
923
924 pv_array = call_lvm_and_parse_json_report (args, "pv", &parser, FALSE, error);
925 if (!pv_array)
926 return NULL;
927
928 if (json_array_get_length (pv_array) == 0) {
929 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_PARSE,
930 "Failed to parse information about the PV");
931 g_object_unref (parser);
932 return NULL;
933 }
934
935 ret = get_pv_data_from_json (json_array_get_object_element (pv_array, 0));
936
937 g_object_unref (parser);
938 return ret;
939 }
940
941 /**
942 * bd_lvm_pvs:
943 * @error: (out) (optional): place to store error (if any)
944 *
945 * Returns: (array zero-terminated=1): information about PVs found in the system
946 *
947 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
948 */
949 BDLVMPVdata** bd_lvm_pvs (GError **error) {
950 const gchar *args[9] = {"pvs", "--units=b", "--nosuffix",
951 "--reportformat", "json_std",
952 "-o", "pv_name,pv_uuid,pv_free,pv_size,pe_start,vg_name,vg_uuid,vg_size," \
953 "vg_free,vg_extent_size,vg_extent_count,vg_free_count,pv_count,pv_tags,pv_missing",
954 NULL};
955 JsonParser *parser = NULL;
956 JsonArray *pv_array = NULL;
957 GPtrArray *pvs;
958 GError *l_error = NULL;
959
960 pvs = g_ptr_array_new ();
961
962 pv_array = call_lvm_and_parse_json_report (args, "pv", &parser, TRUE, &l_error);
963 if (!pv_array) {
964 if (l_error) {
965 g_propagate_error (error, l_error);
966 g_ptr_array_free (pvs, TRUE);
967 return NULL;
968 }
969 /* no output => no PVs, not an error */
970 g_ptr_array_add (pvs, NULL);
971 return (BDLVMPVdata **) g_ptr_array_free (pvs, FALSE);
972 }
973
974 for (guint i = 0; i < json_array_get_length (pv_array); i++) {
975 JsonObject *pv_obj = json_array_get_object_element (pv_array, i);
976 BDLVMPVdata *pvdata = get_pv_data_from_json (pv_obj);
977 if (pvdata)
978 g_ptr_array_add (pvs, pvdata);
979 }
980
981 g_object_unref (parser);
982
983 /* returning NULL-terminated array of BDLVMPVdata */
984 g_ptr_array_add (pvs, NULL);
985 return (BDLVMPVdata **) g_ptr_array_free (pvs, FALSE);
986 }
987
988 /**
989 * bd_lvm_vgcreate:
990 * @name: name of the newly created VG
991 * @pv_list: (array zero-terminated=1): list of PVs the newly created VG should use
992 * @pe_size: PE size or 0 if the default value should be used
993 * @extra: (nullable) (array zero-terminated=1): extra options for the VG creation
994 * (just passed to LVM as is)
995 * @error: (out) (optional): place to store error (if any)
996 *
997 * Returns: whether the VG @name was successfully created or not
998 *
999 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_CREATE
1000 */
1001 gboolean bd_lvm_vgcreate (const gchar *name, const gchar **pv_list, guint64 pe_size, const BDExtraArg **extra, GError **error) {
1002 guint i = 0;
1003 guint pv_list_len = pv_list ? g_strv_length ((gchar **) pv_list) : 0;
1004 const gchar **argv = g_new0 (const gchar*, pv_list_len + 5);
1005 pe_size = RESOLVE_PE_SIZE (pe_size);
1006 gboolean success = FALSE;
1007
1008 argv[0] = "vgcreate";
1009 argv[1] = "-s";
1010 argv[2] = g_strdup_printf ("%"G_GUINT64_FORMAT"K", pe_size / 1024);
1011 argv[3] = name;
1012 for (i=4; i < (pv_list_len + 4); i++) {
1013 argv[i] = pv_list[i-4];
1014 }
1015 argv[i] = NULL;
1016
1017 success = call_lvm_and_report_error (argv, extra, TRUE, error);
1018 g_free ((gchar *) argv[2]);
1019 g_free (argv);
1020
1021 return success;
1022 }
1023
1024 /**
1025 * bd_lvm_vgremove:
1026 * @vg_name: name of the to be removed VG
1027 * @extra: (nullable) (array zero-terminated=1): extra options for the VG removal
1028 * (just passed to LVM as is)
1029 * @error: (out) (optional): place to store error (if any)
1030 *
1031 * Returns: whether the VG was successfully removed or not
1032 *
1033 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_REMOVE
1034 */
1035 gboolean bd_lvm_vgremove (const gchar *vg_name, const BDExtraArg **extra, GError **error) {
1036 const gchar *args[4] = {"vgremove", "--force", vg_name, NULL};
1037
1038 return call_lvm_and_report_error (args, extra, TRUE, error);
1039 }
1040
1041 /**
1042 * bd_lvm_vgrename:
1043 * @old_vg_name: old name of the VG to rename
1044 * @new_vg_name: new name for the @old_vg_name VG
1045 * @extra: (nullable) (array zero-terminated=1): extra options for the VG rename
1046 * (just passed to LVM as is)
1047 * @error: (out) (optional): place to store error (if any)
1048 *
1049 * Returns: whether the VG was successfully renamed or not
1050 *
1051 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1052 */
1053 gboolean bd_lvm_vgrename (const gchar *old_vg_name, const gchar *new_vg_name, const BDExtraArg **extra, GError **error) {
1054 const gchar *args[4] = {"vgrename", old_vg_name, new_vg_name, NULL};
1055
1056 return call_lvm_and_report_error (args, extra, TRUE, error);
1057 }
1058
1059 /**
1060 * bd_lvm_vgactivate:
1061 * @vg_name: name of the to be activated VG
1062 * @extra: (nullable) (array zero-terminated=1): extra options for the VG activation
1063 * (just passed to LVM as is)
1064 * @error: (out) (optional): place to store error (if any)
1065 *
1066 * Returns: whether the VG was successfully activated or not
1067 *
1068 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1069 */
1070 gboolean bd_lvm_vgactivate (const gchar *vg_name, const BDExtraArg **extra, GError **error) {
1071 const gchar *args[4] = {"vgchange", "-ay", vg_name, NULL};
1072
1073 return call_lvm_and_report_error (args, extra, TRUE, error);
1074 }
1075
1076 /**
1077 * bd_lvm_vgdeactivate:
1078 * @vg_name: name of the to be deactivated VG
1079 * @extra: (nullable) (array zero-terminated=1): extra options for the VG deactivation
1080 * (just passed to LVM as is)
1081 * @error: (out) (optional): place to store error (if any)
1082 *
1083 * Returns: whether the VG was successfully deactivated or not
1084 *
1085 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1086 */
1087 gboolean bd_lvm_vgdeactivate (const gchar *vg_name, const BDExtraArg **extra, GError **error) {
1088 const gchar *args[4] = {"vgchange", "-an", vg_name, NULL};
1089
1090 return call_lvm_and_report_error (args, extra, TRUE, error);
1091 }
1092
1093 /**
1094 * bd_lvm_vgextend:
1095 * @vg_name: name of the to be extended VG
1096 * @device: PV device to extend the @vg_name VG with
1097 * @extra: (nullable) (array zero-terminated=1): extra options for the VG extension
1098 * (just passed to LVM as is)
1099 * @error: (out) (optional): place to store error (if any)
1100 *
1101 * Returns: whether the VG @vg_name was successfully extended with the given @device or not.
1102 *
1103 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1104 */
1105 gboolean bd_lvm_vgextend (const gchar *vg_name, const gchar *device, const BDExtraArg **extra, GError **error) {
1106 const gchar *args[4] = {"vgextend", vg_name, device, NULL};
1107
1108 return call_lvm_and_report_error (args, extra, TRUE, error);
1109 }
1110
1111 /**
1112 * bd_lvm_vgreduce:
1113 * @vg_name: name of the to be reduced VG
1114 * @device: (nullable): PV device the @vg_name VG should be reduced of or %NULL
1115 * if the VG should be reduced of the missing PVs
1116 * @extra: (nullable) (array zero-terminated=1): extra options for the VG reduction
1117 * (just passed to LVM as is)
1118 * @error: (out) (optional): place to store error (if any)
1119 *
1120 * Returns: whether the VG @vg_name was successfully reduced of the given @device or not
1121 *
1122 * Note: This function does not move extents off of the PV before removing
1123 * it from the VG. You must do that first by calling #bd_lvm_pvmove.
1124 *
1125 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1126 */
1127 gboolean bd_lvm_vgreduce (const gchar *vg_name, const gchar *device, const BDExtraArg **extra, GError **error) {
1128 const gchar *args[5] = {"vgreduce", NULL, NULL, NULL, NULL};
1129
1130 if (!device) {
1131 args[1] = "--removemissing";
1132 args[2] = "--force";
1133 args[3] = vg_name;
1134 } else {
1135 args[1] = vg_name;
1136 args[2] = device;
1137 }
1138
1139 return call_lvm_and_report_error (args, extra, TRUE, error);
1140 }
1141
1142 /**
1143 * bd_lvm_add_vg_tags:
1144 * @vg_name: the VG to set tags on
1145 * @tags: (array zero-terminated=1): list of tags to add
1146 * @error: (out) (optional): place to store error (if any)
1147 *
1148 * Returns: whether the tags were successfully added to @vg_name or not
1149 *
1150 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
1151 */
1152 gboolean bd_lvm_add_vg_tags (const gchar *vg_name, const gchar **tags, GError **error) {
1153 return _manage_lvm_tags (vg_name, tags, "--addtag", "vgchange", error);
1154 }
1155
1156 /**
1157 * bd_lvm_delete_vg_tags:
1158 * @vg_name: the VG to set tags on
1159 * @tags: (array zero-terminated=1): list of tags to remove
1160 * @error: (out) (optional): place to store error (if any)
1161 *
1162 * Returns: whether the tags were successfully removed from @vg_name or not
1163 *
1164 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
1165 */
1166 gboolean bd_lvm_delete_vg_tags (const gchar *vg_name, const gchar **tags, GError **error) {
1167 return _manage_lvm_tags (vg_name, tags, "--deltag", "vgchange", error);
1168 }
1169
1170 static gboolean _vglock_start_stop (const gchar *vg_name, gboolean start, const BDExtraArg **extra, GError **error) {
1171 const gchar *args[4] = {"vgchange", NULL, vg_name, NULL};
1172
1173 if (start)
1174 args[1] = "--lockstart";
1175 else
1176 args[1] = "--lockstop";
1177
1178 return call_lvm_and_report_error (args, extra, TRUE, error);
1179 }
1180
1181 /**
1182 * bd_lvm_vglock_start:
1183 * @vg_name: a shared VG to start the lockspace in lvmlockd
1184 * @extra: (nullable) (array zero-terminated=1): extra options for the vgchange command
1185 * (just passed to LVM as is)
1186 * @error: (out) (optional): place to store error (if any)
1187 *
1188 * Returns: whether the lock was successfully started for @vg_name or not
1189 *
1190 * Tech category: %BD_LVM_TECH_SHARED-%BD_LVM_TECH_MODE_MODIFY
1191 */
1192 gboolean bd_lvm_vglock_start (const gchar *vg_name, const BDExtraArg **extra, GError **error) {
1193 return _vglock_start_stop (vg_name, TRUE, extra, error);
1194 }
1195
1196 /**
1197 * bd_lvm_vglock_stop:
1198 * @vg_name: a shared VG to stop the lockspace in lvmlockd
1199 * @extra: (nullable) (array zero-terminated=1): extra options for the vgchange command
1200 * (just passed to LVM as is)
1201 * @error: (out) (optional): place to store error (if any)
1202 *
1203 * Returns: whether the lock was successfully stopped for @vg_name or not
1204 *
1205 * Tech category: %BD_LVM_TECH_SHARED-%BD_LVM_TECH_MODE_MODIFY
1206 */
1207 gboolean bd_lvm_vglock_stop (const gchar *vg_name, const BDExtraArg **extra, GError **error) {
1208 return _vglock_start_stop (vg_name, FALSE, extra, error);
1209 }
1210
1211 /**
1212 * bd_lvm_vginfo:
1213 * @vg_name: a VG to get information about
1214 * @error: (out) (optional): place to store error (if any)
1215 *
1216 * Returns: (transfer full): information about the @vg_name VG or %NULL in case
1217 * of error (the @error) gets populated in those cases)
1218 *
1219 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
1220 */
1221 BDLVMVGdata* bd_lvm_vginfo (const gchar *vg_name, GError **error) {
1222 const gchar *args[10] = {"vgs", "--nosuffix", "--units=b",
1223 "--reportformat", "json_std",
1224 "-o", "name,uuid,size,free,extent_size,extent_count,free_count,pv_count,vg_exported,vg_tags",
1225 vg_name, NULL};
1226 JsonParser *parser = NULL;
1227 JsonArray *vg_array = NULL;
1228 BDLVMVGdata *ret = NULL;
1229
1230 vg_array = call_lvm_and_parse_json_report (args, "vg", &parser, FALSE, error);
1231 if (!vg_array)
1232 return NULL;
1233
1234 if (json_array_get_length (vg_array) == 0) {
1235 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_PARSE,
1236 "Failed to parse information about the VG");
1237 g_object_unref (parser);
1238 return NULL;
1239 }
1240
1241 ret = get_vg_data_from_json (json_array_get_object_element (vg_array, 0));
1242
1243 g_object_unref (parser);
1244 return ret;
1245 }
1246
1247 /**
1248 * bd_lvm_vgs:
1249 * @error: (out) (optional): place to store error (if any)
1250 *
1251 * Returns: (array zero-terminated=1): information about VGs found in the system
1252 *
1253 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
1254 */
1255 BDLVMVGdata** bd_lvm_vgs (GError **error) {
1256 const gchar *args[9] = {"vgs", "--nosuffix", "--units=b",
1257 "--reportformat", "json_std",
1258 "-o", "name,uuid,size,free,extent_size,extent_count,free_count,pv_count,vg_exported,vg_tags",
1259 NULL};
1260 JsonParser *parser = NULL;
1261 JsonArray *vg_array = NULL;
1262 GPtrArray *vgs;
1263 GError *l_error = NULL;
1264
1265 vgs = g_ptr_array_new ();
1266
1267 vg_array = call_lvm_and_parse_json_report (args, "vg", &parser, TRUE, &l_error);
1268 if (!vg_array) {
1269 if (l_error) {
1270 g_propagate_error (error, l_error);
1271 g_ptr_array_free (vgs, TRUE);
1272 return NULL;
1273 }
1274 /* no output => no VGs, not an error */
1275 g_ptr_array_add (vgs, NULL);
1276 return (BDLVMVGdata **) g_ptr_array_free (vgs, FALSE);
1277 }
1278
1279 for (guint i = 0; i < json_array_get_length (vg_array); i++) {
1280 JsonObject *vg_obj = json_array_get_object_element (vg_array, i);
1281 BDLVMVGdata *vgdata = get_vg_data_from_json (vg_obj);
1282 if (vgdata)
1283 g_ptr_array_add (vgs, vgdata);
1284 }
1285
1286 g_object_unref (parser);
1287
1288 /* returning NULL-terminated array of BDLVMVGdata */
1289 g_ptr_array_add (vgs, NULL);
1290 return (BDLVMVGdata **) g_ptr_array_free (vgs, FALSE);
1291 }
1292
1293 /**
1294 * bd_lvm_lvorigin:
1295 * @vg_name: name of the VG containing the queried LV
1296 * @lv_name: name of the queried LV
1297 * @error: (out) (optional): place to store error (if any)
1298 *
1299 * Returns: (transfer full): the origin volume for the @vg_name/@lv_name LV or
1300 * %NULL if failed to determine (@error) is set in those cases)
1301 *
1302 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
1303 */
1304 gchar* bd_lvm_lvorigin (const gchar *vg_name, const gchar *lv_name, GError **error) {
1305 gboolean success = FALSE;
1306 gchar *output = NULL;
1307 const gchar *args[6] = {"lvs", "--noheadings", "-o", "origin", NULL, NULL};
1308 args[4] = g_strdup_printf ("%s/%s", vg_name, lv_name);
1309
1310 success = call_lvm_and_capture_output (args, NULL, &output, error);
1311 g_free ((gchar *) args[4]);
1312
1313 if (!success)
1314 /* the error is already populated from the call */
1315 return NULL;
1316
1317 return g_strstrip (output);
1318 }
1319
1320 /**
1321 * bd_lvm_lvcreate:
1322 * @vg_name: name of the VG to create a new LV in
1323 * @lv_name: name of the to-be-created LV
1324 * @size: requested size of the new LV
1325 * @type: (nullable): type of the new LV ("striped", "raid1",..., see lvcreate (8))
1326 * @pv_list: (nullable) (array zero-terminated=1): list of PVs the newly created LV should use or %NULL
1327 * if not specified
1328 * @extra: (nullable) (array zero-terminated=1): extra options for the LV creation
1329 * (just passed to LVM as is)
1330 * @error: (out) (optional): place to store error (if any)
1331 *
1332 * Returns: whether the given @vg_name/@lv_name LV was successfully created or not
1333 *
1334 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_CREATE
1335 */
1336 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) {
1337 guint pv_list_len = pv_list ? g_strv_length ((gchar **) pv_list) : 0;
1338 const gchar **args = g_new0 (const gchar*, pv_list_len + 10);
1339 gboolean success = FALSE;
1340 guint64 i = 0;
1341 guint64 j = 0;
1342 gchar *size_str = NULL;
1343 gchar *type_str = NULL;
1344
1345 args[i++] = "lvcreate";
1346 args[i++] = "-n";
1347 args[i++] = lv_name;
1348 args[i++] = "-L";
1349 size_str = g_strdup_printf ("%"G_GUINT64_FORMAT"K", size/1024);
1350 args[i++] = size_str;
1351 args[i++] = "-y";
1352 if (type) {
1353 if (g_strcmp0 (type, "striped") == 0) {
1354 args[i++] = "--stripes";
1355 type_str = g_strdup_printf ("%d", pv_list_len);
1356 args[i++] = type_str;
1357 } else {
1358 args[i++] = "--type";
1359 args[i++] = type;
1360 }
1361 }
1362 args[i++] = vg_name;
1363
1364 for (j=0; j < pv_list_len; j++)
1365 args[i++] = pv_list[j];
1366
1367 args[i] = NULL;
1368
1369 success = call_lvm_and_report_error (args, extra, TRUE, error);
1370 g_free (size_str);
1371 g_free (type_str);
1372 g_free (args);
1373
1374 return success;
1375 }
1376
1377 /**
1378 * bd_lvm_lvremove:
1379 * @vg_name: name of the VG containing the to-be-removed LV
1380 * @lv_name: name of the to-be-removed LV
1381 * @force: whether to force removal or not
1382 * @extra: (nullable) (array zero-terminated=1): extra options for the LV removal
1383 * (just passed to LVM as is)
1384 * @error: (out) (optional): place to store error (if any)
1385 *
1386 * Returns: whether the @vg_name/@lv_name LV was successfully removed or not
1387 *
1388 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_REMOVE
1389 */
1390 gboolean bd_lvm_lvremove (const gchar *vg_name, const gchar *lv_name, gboolean force, const BDExtraArg **extra, GError **error) {
1391 /* '--yes' is needed if DISCARD is enabled */
1392 const gchar *args[5] = {"lvremove", "--yes", NULL, NULL, NULL};
1393 guint8 next_arg = 2;
1394 gboolean success = FALSE;
1395
1396 if (force) {
1397 args[next_arg] = "--force";
1398 next_arg++;
1399 }
1400
1401 args[next_arg] = g_strdup_printf ("%s/%s", vg_name, lv_name);
1402
1403 success = call_lvm_and_report_error (args, extra, TRUE, error);
1404 g_free ((gchar *) args[next_arg]);
1405
1406 return success;
1407 }
1408
1409 /**
1410 * bd_lvm_lvrename:
1411 * @vg_name: name of the VG containing the to-be-renamed LV
1412 * @lv_name: name of the to-be-renamed LV
1413 * @new_name: new name for the @vg_name/@lv_name LV
1414 * @extra: (nullable) (array zero-terminated=1): extra options for the LV rename
1415 * (just passed to LVM as is)
1416 * @error: (out) (optional): place to store error (if any)
1417 *
1418 * Returns: whether the @vg_name/@lv_name LV was successfully renamed to
1419 * @vg_name/@new_name or not
1420 *
1421 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1422 */
1423 gboolean bd_lvm_lvrename (const gchar *vg_name, const gchar *lv_name, const gchar *new_name, const BDExtraArg **extra, GError **error) {
1424 const gchar *args[5] = {"lvrename", vg_name, lv_name, new_name, NULL};
1425 return call_lvm_and_report_error (args, extra, TRUE, error);
1426 }
1427
1428
1429 /**
1430 * bd_lvm_lvresize:
1431 * @vg_name: name of the VG containing the to-be-resized LV
1432 * @lv_name: name of the to-be-resized LV
1433 * @size: the requested new size of the LV
1434 * @extra: (nullable) (array zero-terminated=1): extra options for the LV resize
1435 * (just passed to LVM as is)
1436 * @error: (out) (optional): place to store error (if any)
1437 *
1438 * Returns: whether the @vg_name/@lv_name LV was successfully resized or not
1439 *
1440 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1441 */
1442 gboolean bd_lvm_lvresize (const gchar *vg_name, const gchar *lv_name, guint64 size, const BDExtraArg **extra, GError **error) {
1443 const gchar *args[8] = {"lvresize", "--force", "-L", NULL, NULL, NULL, NULL, NULL};
1444 gboolean success = FALSE;
1445 guint8 next_arg = 4;
1446 g_autofree gchar *lvspec = NULL;
1447
1448 args[3] = g_strdup_printf ("%"G_GUINT64_FORMAT"K", size/1024);
1449
1450 /* Starting with 2.03.19 we need to add an extra option to avoid
1451 any filesystem related checks by lvresize.
1452 */
1453 success = bd_utils_check_util_version (deps[DEPS_LVM].name, LVM_VERSION_FSRESIZE,
1454 deps[DEPS_LVM].ver_arg, deps[DEPS_LVM].ver_regexp, NULL);
1455 if (success) {
1456 args[next_arg++] = "--fs";
1457 args[next_arg++] = "ignore";
1458 }
1459
1460 lvspec = g_strdup_printf ("%s/%s", vg_name, lv_name);
1461 args[next_arg++] = lvspec;
1462
1463 success = call_lvm_and_report_error (args, extra, TRUE, error);
1464 g_free ((gchar *) args[3]);
1465
1466 return success;
1467 }
1468
1469 /**
1470 * bd_lvm_lvrepair:
1471 * @vg_name: name of the VG containing the to-be-repaired LV
1472 * @lv_name: name of the to-be-repaired LV
1473 * @pv_list: (array zero-terminated=1): list of PVs to be used for the repair
1474 * @extra: (nullable) (array zero-terminated=1): extra options for the LV repair
1475 * (just passed to LVM as is)
1476 * @error: (out) (optional): place to store error (if any)
1477 *
1478 * Returns: whether the @vg_name/@lv_name LV was successfully repaired or not
1479 *
1480 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1481 */
1482 gboolean bd_lvm_lvrepair (const gchar *vg_name, const gchar *lv_name, const gchar **pv_list, const BDExtraArg **extra, GError **error) {
1483 guint i = 0;
1484 guint pv_list_len = pv_list ? g_strv_length ((gchar **) pv_list) : 0;
1485 const gchar **argv = g_new0 (const gchar*, pv_list_len + 5);
1486 gboolean success = FALSE;
1487
1488 argv[0] = "lvconvert";
1489 argv[1] = "--repair";
1490 argv[2] = "--yes";
1491 argv[3] = g_strdup_printf ("%s/%s", vg_name, lv_name);
1492 for (i=4; i < (pv_list_len + 4); i++) {
1493 argv[i] = pv_list[i-4];
1494 }
1495 argv[i] = NULL;
1496
1497 success = call_lvm_and_report_error (argv, extra, TRUE, error);
1498 g_free ((gchar *) argv[3]);
1499 g_free (argv);
1500
1501 return success;
1502 }
1503
1504 /**
1505 * bd_lvm_lvactivate:
1506 * @vg_name: name of the VG containing the to-be-activated LV
1507 * @lv_name: name of the to-be-activated LV
1508 * @ignore_skip: whether to ignore the skip flag or not
1509 * @shared: whether to activate the LV in shared mode (used for shared LVM setups with lvmlockd,
1510 * use %FALSE if not sure)
1511 * @extra: (nullable) (array zero-terminated=1): extra options for the LV activation
1512 * (just passed to LVM as is)
1513 * @error: (out) (optional): place to store error (if any)
1514 *
1515 * Returns: whether the @vg_name/@lv_name LV was successfully activated or not
1516 *
1517 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1518 */
1519 gboolean bd_lvm_lvactivate (const gchar *vg_name, const gchar *lv_name, gboolean ignore_skip, gboolean shared, const BDExtraArg **extra, GError **error) {
1520 const gchar *args[5] = {"lvchange", NULL, NULL, NULL, NULL};
1521 guint8 next_arg = 2;
1522 gboolean success = FALSE;
1523
1524 if (shared)
1525 args[1] = "-asy";
1526 else
1527 args[1] = "-ay";
1528
1529 if (ignore_skip) {
1530 args[next_arg] = "-K";
1531 next_arg++;
1532 }
1533 args[next_arg] = g_strdup_printf ("%s/%s", vg_name, lv_name);
1534
1535 success = call_lvm_and_report_error (args, extra, TRUE, error);
1536 g_free ((gchar *) args[next_arg]);
1537
1538 return success;
1539 }
1540
1541 /**
1542 * bd_lvm_lvdeactivate:
1543 * @vg_name: name of the VG containing the to-be-deactivated LV
1544 * @lv_name: name of the to-be-deactivated LV
1545 * @extra: (nullable) (array zero-terminated=1): extra options for the LV deactivation
1546 * (just passed to LVM as is)
1547 * @error: (out) (optional): place to store error (if any)
1548 *
1549 * Returns: whether the @vg_name/@lv_name LV was successfully deactivated or not
1550 *
1551 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_MODIFY
1552 */
1553 gboolean bd_lvm_lvdeactivate (const gchar *vg_name, const gchar *lv_name, const BDExtraArg **extra, GError **error) {
1554 const gchar *args[4] = {"lvchange", "-an", NULL, NULL};
1555 gboolean success = FALSE;
1556
1557 args[2] = g_strdup_printf ("%s/%s", vg_name, lv_name);
1558
1559 success = call_lvm_and_report_error (args, extra, TRUE, error);
1560 g_free ((gchar *) args[2]);
1561
1562 return success;
1563 }
1564
1565 /**
1566 * bd_lvm_lvsnapshotcreate:
1567 * @vg_name: name of the VG containing the LV a new snapshot should be created of
1568 * @origin_name: name of the LV a new snapshot should be created of
1569 * @snapshot_name: name of the to-be-created snapshot
1570 * @size: requested size for the snapshot
1571 * @extra: (nullable) (array zero-terminated=1): extra options for the LV snapshot creation
1572 * (just passed to LVM as is)
1573 * @error: (out) (optional): place to store error (if any)
1574 *
1575 * Returns: whether the @snapshot_name snapshot of the @vg_name/@origin_name LV
1576 * was successfully created or not.
1577 *
1578 * Tech category: %BD_LVM_TECH_BASIC_SNAP-%BD_LVM_TECH_MODE_CREATE
1579 */
1580 gboolean bd_lvm_lvsnapshotcreate (const gchar *vg_name, const gchar *origin_name, const gchar *snapshot_name, guint64 size, const BDExtraArg **extra, GError **error) {
1581 const gchar *args[8] = {"lvcreate", "-s", "-L", NULL, "-n", snapshot_name, NULL, NULL};
1582 gboolean success = FALSE;
1583
1584 args[3] = g_strdup_printf ("%"G_GUINT64_FORMAT"K", size / 1024);
1585 args[6] = g_strdup_printf ("%s/%s", vg_name, origin_name);
1586
1587 success = call_lvm_and_report_error (args, extra, TRUE, error);
1588 g_free ((gchar *) args[3]);
1589 g_free ((gchar *) args[6]);
1590
1591 return success;
1592 }
1593
1594 /**
1595 * bd_lvm_lvsnapshotmerge:
1596 * @vg_name: name of the VG containing the to-be-merged LV snapshot
1597 * @snapshot_name: name of the to-be-merged LV snapshot
1598 * @extra: (nullable) (array zero-terminated=1): extra options for the LV snapshot merge
1599 * (just passed to LVM as is)
1600 * @error: (out) (optional): place to store error (if any)
1601 *
1602 * Returns: whether the @vg_name/@snapshot_name LV snapshot was successfully merged or not
1603 *
1604 * Tech category: %BD_LVM_TECH_BASIC_SNAP-%BD_LVM_TECH_MODE_MODIFY
1605 */
1606 gboolean bd_lvm_lvsnapshotmerge (const gchar *vg_name, const gchar *snapshot_name, const BDExtraArg **extra, GError **error) {
1607 const gchar *args[4] = {"lvconvert", "--merge", NULL, NULL};
1608 gboolean success = FALSE;
1609
1610 args[2] = g_strdup_printf ("%s/%s", vg_name, snapshot_name);
1611
1612 success = call_lvm_and_report_error (args, extra, TRUE, error);
1613 g_free ((gchar *) args[2]);
1614
1615 return success;
1616 }
1617
1618 /**
1619 * bd_lvm_add_lv_tags:
1620 * @vg_name: name of the VG that contains the LV to set tags on
1621 * @lv_name: name of the LV to set tags on
1622 * @tags: (array zero-terminated=1): list of tags to add
1623 * @error: (out) (optional): place to store error (if any)
1624 *
1625 * Returns: whether the tags were successfully added to @device or not
1626 *
1627 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
1628 */
1629 gboolean bd_lvm_add_lv_tags (const gchar *vg_name, const gchar *lv_name, const gchar **tags, GError **error) {
1630 g_autofree gchar *lvspec = g_strdup_printf ("%s/%s", vg_name, lv_name);
1631 return _manage_lvm_tags (lvspec, tags, "--addtag", "lvchange", error);
1632 }
1633
1634 /**
1635 * bd_lvm_delete_lv_tags:
1636 * @vg_name: name of the VG that contains the LV to set tags on
1637 * @lv_name: name of the LV to set tags on
1638 * @tags: (array zero-terminated=1): list of tags to remove
1639 * @error: (out) (optional): place to store error (if any)
1640 *
1641 * Returns: whether the tags were successfully removed from @device or not
1642 *
1643 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
1644 */
1645 gboolean bd_lvm_delete_lv_tags (const gchar *vg_name, const gchar *lv_name, const gchar **tags, GError **error) {
1646 g_autofree gchar *lvspec = g_strdup_printf ("%s/%s", vg_name, lv_name);
1647 return _manage_lvm_tags (lvspec, tags, "--deltag", "lvchange", error);
1648 }
1649
1650 /**
1651 * bd_lvm_lvinfo:
1652 * @vg_name: name of the VG that contains the LV to get information about
1653 * @lv_name: name of the LV to get information about
1654 * @error: (out) (optional): place to store error (if any)
1655 *
1656 * Returns: (transfer full): information about the @vg_name/@lv_name LV or %NULL in case
1657 * of error (the @error) gets populated in those cases)
1658 *
1659 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
1660 */
1661 BDLVMLVdata* bd_lvm_lvinfo (const gchar *vg_name, const gchar *lv_name, GError **error) {
1662 const gchar *args[11] = {"lvs", "--nosuffix", "--units=b",
1663 "--reportformat", "json_std", "-a",
1664 "-o", "vg_name,lv_name,lv_uuid,lv_size,lv_attr,segtype,origin,pool_lv,data_lv,metadata_lv,lv_role,move_pv,data_percent,metadata_percent,copy_percent,lv_tags",
1665 NULL, NULL};
1666 JsonParser *parser = NULL;
1667 JsonArray *lv_array = NULL;
1668 BDLVMLVdata *ret = NULL;
1669
1670 args[8] = g_strdup_printf ("%s/%s", vg_name, lv_name);
1671
1672 lv_array = call_lvm_and_parse_json_report (args, "lv", &parser, FALSE, error);
1673 g_free ((gchar *) args[8]);
1674
1675 if (!lv_array)
1676 return NULL;
1677
1678 if (json_array_get_length (lv_array) == 0) {
1679 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_PARSE,
1680 "Failed to parse information about the LV");
1681 g_object_unref (parser);
1682 return NULL;
1683 }
1684
1685 ret = get_lv_data_from_json (json_array_get_object_element (lv_array, 0));
1686
1687 g_object_unref (parser);
1688 return ret;
1689 }
1690
1691 /**
1692 * bd_lvm_lvinfo_tree:
1693 * @vg_name: name of the VG that contains the LV to get information about
1694 * @lv_name: name of the LV to get information about
1695 * @error: (out) (optional): place to store error (if any)
1696 *
1697 * This function will fill out the data_lvs, metadata_lvs, and segs fields as well.
1698 *
1699 * Returns: (transfer full): information about the @vg_name/@lv_name LV or %NULL in case
1700 * of error (the @error) gets populated in those cases)
1701 *
1702 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
1703 */
1704 BDLVMLVdata* bd_lvm_lvinfo_tree (const gchar *vg_name, const gchar *lv_name, GError **error) {
1705 const gchar *args[11] = {"lvs", "--nosuffix", "--units=b",
1706 "--reportformat", "json_std", "-a",
1707 "-o", "vg_name,lv_name,lv_uuid,lv_size,lv_attr,segtype,origin,pool_lv,data_lv,metadata_lv,lv_role,move_pv,data_percent,metadata_percent,copy_percent,lv_tags,devices,metadata_devices,seg_size_pe",
1708 NULL, NULL};
1709 JsonParser *parser = NULL;
1710 JsonArray *lv_array = NULL;
1711 BDLVMLVdata *result = NULL;
1712
1713 args[8] = g_strdup_printf ("%s/%s", vg_name, lv_name);
1714
1715 lv_array = call_lvm_and_parse_json_report (args, "lv", &parser, FALSE, error);
1716 g_free ((gchar *) args[8]);
1717
1718 if (!lv_array)
1719 return NULL;
1720
1721 if (json_array_get_length (lv_array) == 0) {
1722 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_PARSE,
1723 "Failed to parse information about the LV");
1724 g_object_unref (parser);
1725 return NULL;
1726 }
1727
1728 for (guint i = 0; i < json_array_get_length (lv_array); i++) {
1729 JsonObject *lv_obj = json_array_get_object_element (lv_array, i);
1730 BDLVMLVdata *lvdata = get_lv_data_from_json (lv_obj);
1731 if (result) {
1732 merge_lv_data (result, lvdata);
1733 bd_lvm_lvdata_free (lvdata);
1734 } else
1735 result = lvdata;
1736 }
1737
1738 g_object_unref (parser);
1739
1740 if (result == NULL)
1741 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_PARSE,
1742 "Failed to parse information about the LV");
1743 return result;
1744 }
1745
1746 /**
1747 * bd_lvm_lvs:
1748 * @vg_name: (nullable): name of the VG to get information about LVs from
1749 * @error: (out) (optional): place to store error (if any)
1750 *
1751 * Returns: (array zero-terminated=1): information about LVs found in the given
1752 * @vg_name VG or in system if @vg_name is %NULL
1753 *
1754 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
1755 */
1756 BDLVMLVdata** bd_lvm_lvs (const gchar *vg_name, GError **error) {
1757 const gchar *args[11] = {"lvs", "--nosuffix", "--units=b",
1758 "--reportformat", "json_std", "-a",
1759 "-o", "vg_name,lv_name,lv_uuid,lv_size,lv_attr,segtype,origin,pool_lv,data_lv,metadata_lv,lv_role,move_pv,data_percent,metadata_percent,copy_percent,lv_tags",
1760 NULL, NULL};
1761 JsonParser *parser = NULL;
1762 JsonArray *lv_array = NULL;
1763 GPtrArray *lvs;
1764 GError *l_error = NULL;
1765
1766 lvs = g_ptr_array_new ();
1767
1768 if (vg_name)
1769 args[8] = vg_name;
1770
1771 lv_array = call_lvm_and_parse_json_report (args, "lv", &parser, TRUE, &l_error);
1772 if (!lv_array) {
1773 if (l_error) {
1774 g_propagate_error (error, l_error);
1775 g_ptr_array_free (lvs, TRUE);
1776 return NULL;
1777 }
1778 /* no output => no LVs, not an error */
1779 g_ptr_array_add (lvs, NULL);
1780 return (BDLVMLVdata **) g_ptr_array_free (lvs, FALSE);
1781 }
1782
1783 for (guint i = 0; i < json_array_get_length (lv_array); i++) {
1784 JsonObject *lv_obj = json_array_get_object_element (lv_array, i);
1785 BDLVMLVdata *lvdata = get_lv_data_from_json (lv_obj);
1786 if (lvdata) {
1787 /* ignore duplicate entries in lvs output, these are caused by multi segments LVs */
1788 gboolean duplicate = FALSE;
1789 for (gsize j = 0; j < lvs->len; j++) {
1790 if (g_strcmp0 (((BDLVMLVdata *) g_ptr_array_index (lvs, j))->lv_name, lvdata->lv_name) == 0) {
1791 bd_utils_log_format (BD_UTILS_LOG_DEBUG,
1792 "Duplicate LV entry for '%s' found in lvs output",
1793 lvdata->lv_name);
1794 bd_lvm_lvdata_free (lvdata);
1795 duplicate = TRUE;
1796 break;
1797 }
1798 }
1799
1800 if (!duplicate)
1801 g_ptr_array_add (lvs, lvdata);
1802 }
1803 }
1804
1805 g_object_unref (parser);
1806
1807 /* returning NULL-terminated array of BDLVMLVdata */
1808 g_ptr_array_add (lvs, NULL);
1809 return (BDLVMLVdata **) g_ptr_array_free (lvs, FALSE);
1810 }
1811
1812 /**
1813 * bd_lvm_lvs_tree:
1814 * @vg_name: (nullable): name of the VG to get information about LVs from
1815 * @error: (out) (optional): place to store error (if any)
1816 *
1817 * This function will fill out the data_lvs, metadata_lvs, and segs fields as well.
1818 *
1819 * Returns: (array zero-terminated=1): information about LVs found in the given
1820 * @vg_name VG or in system if @vg_name is %NULL.
1821 *
1822 * Tech category: %BD_LVM_TECH_BASIC-%BD_LVM_TECH_MODE_QUERY
1823 */
1824 BDLVMLVdata** bd_lvm_lvs_tree (const gchar *vg_name, GError **error) {
1825 const gchar *args[11] = {"lvs", "--nosuffix", "--units=b",
1826 "--reportformat", "json_std", "-a",
1827 "-o", "vg_name,lv_name,lv_uuid,lv_size,lv_attr,segtype,origin,pool_lv,data_lv,metadata_lv,lv_role,move_pv,data_percent,metadata_percent,copy_percent,lv_tags,devices,metadata_devices,seg_size_pe",
1828 NULL, NULL};
1829 JsonParser *parser = NULL;
1830 JsonArray *lv_array = NULL;
1831 GPtrArray *lvs;
1832 GError *l_error = NULL;
1833
1834 lvs = g_ptr_array_new ();
1835
1836 if (vg_name)
1837 args[8] = vg_name;
1838
1839 lv_array = call_lvm_and_parse_json_report (args, "lv", &parser, TRUE, &l_error);
1840 if (!lv_array) {
1841 if (l_error) {
1842 g_propagate_error (error, l_error);
1843 g_ptr_array_free (lvs, TRUE);
1844 return NULL;
1845 }
1846 /* no output => no LVs, not an error */
1847 g_ptr_array_add (lvs, NULL);
1848 return (BDLVMLVdata **) g_ptr_array_free (lvs, FALSE);
1849 }
1850
1851 for (guint i = 0; i < json_array_get_length (lv_array); i++) {
1852 JsonObject *lv_obj = json_array_get_object_element (lv_array, i);
1853 BDLVMLVdata *lvdata = get_lv_data_from_json (lv_obj);
1854 if (lvdata) {
1855 gboolean merged = FALSE;
1856 for (gsize j = 0; j < lvs->len; j++) {
1857 BDLVMLVdata *other = (BDLVMLVdata *) g_ptr_array_index (lvs, j);
1858 if (g_strcmp0 (other->lv_name, lvdata->lv_name) == 0) {
1859 merge_lv_data (other, lvdata);
1860 bd_lvm_lvdata_free (lvdata);
1861 merged = TRUE;
1862 break;
1863 }
1864 }
1865
1866 if (!merged)
1867 g_ptr_array_add (lvs, lvdata);
1868 }
1869 }
1870
1871 g_object_unref (parser);
1872
1873 /* returning NULL-terminated array of BDLVMLVdata */
1874 g_ptr_array_add (lvs, NULL);
1875 return (BDLVMLVdata **) g_ptr_array_free (lvs, FALSE);
1876 }
1877
1878 /**
1879 * bd_lvm_thpoolcreate:
1880 * @vg_name: name of the VG to create a thin pool in
1881 * @lv_name: name of the to-be-created pool LV
1882 * @size: requested size of the to-be-created pool
1883 * @md_size: requested metadata size or 0 to use the default
1884 * @chunk_size: requested chunk size or 0 to use the default
1885 * @profile: (nullable): profile to use (see lvm(8) for more information) or %NULL to use
1886 * the default
1887 * @extra: (nullable) (array zero-terminated=1): extra options for the thin pool creation
1888 * (just passed to LVM as is)
1889 * @error: (out) (optional): place to store error (if any)
1890 *
1891 * Returns: whether the @vg_name/@lv_name thin pool was successfully created or not
1892 *
1893 * Tech category: %BD_LVM_TECH_THIN-%BD_LVM_TECH_MODE_CREATE
1894 */
1895 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) {
1896 const gchar *args[9] = {"lvcreate", "-T", "-L", NULL, NULL, NULL, NULL, NULL, NULL};
1897 guint8 next_arg = 4;
1898 gboolean success = FALSE;
1899
1900 args[3] = g_strdup_printf ("%"G_GUINT64_FORMAT"K", size/1024);
1901
1902 if (md_size != 0) {
1903 args[next_arg] = g_strdup_printf ("--poolmetadatasize=%"G_GUINT64_FORMAT"K", md_size / 1024);
1904 next_arg++;
1905 }
1906
1907 if (chunk_size != 0) {
1908 args[next_arg] = g_strdup_printf ("--chunksize=%"G_GUINT64_FORMAT"K", chunk_size / 1024);
1909 next_arg++;
1910 }
1911
1912 if (profile) {
1913 args[next_arg] = g_strdup_printf ("--profile=%s", profile);
1914 next_arg++;
1915 }
1916
1917 args[next_arg] = g_strdup_printf ("%s/%s", vg_name, lv_name);
1918
1919 success = call_lvm_and_report_error (args, extra, TRUE, error);
1920 g_free ((gchar *) args[3]);
1921 g_free ((gchar *) args[4]);
1922 g_free ((gchar *) args[5]);
1923 g_free ((gchar *) args[6]);
1924 g_free ((gchar *) args[7]);
1925
1926 return success;
1927 }
1928
1929 /**
1930 * bd_lvm_thlvcreate:
1931 * @vg_name: name of the VG containing the thin pool providing extents for the to-be-created thin LV
1932 * @pool_name: name of the pool LV providing extents for the to-be-created thin LV
1933 * @lv_name: name of the to-be-created thin LV
1934 * @size: requested virtual size of the to-be-created thin LV
1935 * @extra: (nullable) (array zero-terminated=1): extra options for the thin LV creation
1936 * (just passed to LVM as is)
1937 * @error: (out) (optional): place to store error (if any)
1938 *
1939 * Returns: whether the @vg_name/@lv_name thin LV was successfully created or not
1940 *
1941 * Tech category: %BD_LVM_TECH_THIN-%BD_LVM_TECH_MODE_CREATE
1942 */
1943 gboolean bd_lvm_thlvcreate (const gchar *vg_name, const gchar *pool_name, const gchar *lv_name, guint64 size, const BDExtraArg **extra, GError **error) {
1944 const gchar *args[8] = {"lvcreate", "-T", NULL, "-V", NULL, "-n", lv_name, NULL};
1945 gboolean success;
1946
1947 args[2] = g_strdup_printf ("%s/%s", vg_name, pool_name);
1948 args[4] = g_strdup_printf ("%"G_GUINT64_FORMAT"K", size / 1024);
1949
1950 success = call_lvm_and_report_error (args, extra, TRUE, error);
1951 g_free ((gchar *) args[2]);
1952 g_free ((gchar *) args[4]);
1953
1954 return success;
1955 }
1956
1957 /**
1958 * bd_lvm_thlvpoolname:
1959 * @vg_name: name of the VG containing the queried thin LV
1960 * @lv_name: name of the queried thin LV
1961 * @error: (out) (optional): place to store error (if any)
1962 *
1963 * Returns: (transfer full): the name of the pool volume for the @vg_name/@lv_name
1964 * thin LV or %NULL if failed to determine (@error) is set in those cases)
1965 *
1966 * Tech category: %BD_LVM_TECH_THIN-%BD_LVM_TECH_MODE_QUERY
1967 */
1968 gchar* bd_lvm_thlvpoolname (const gchar *vg_name, const gchar *lv_name, GError **error) {
1969 gboolean success = FALSE;
1970 gchar *output = NULL;
1971 const gchar *args[6] = {"lvs", "--noheadings", "-o", "pool_lv", NULL, NULL};
1972 args[4] = g_strdup_printf ("%s/%s", vg_name, lv_name);
1973
1974 success = call_lvm_and_capture_output (args, NULL, &output, error);
1975 g_free ((gchar *) args[4]);
1976
1977 if (!success)
1978 /* the error is already populated from the call */
1979 return NULL;
1980
1981 return g_strstrip (output);
1982 }
1983
1984 /**
1985 * bd_lvm_thsnapshotcreate:
1986 * @vg_name: name of the VG containing the thin LV a new snapshot should be created of
1987 * @origin_name: name of the thin LV a new snapshot should be created of
1988 * @snapshot_name: name of the to-be-created snapshot
1989 * @pool_name: (nullable): name of the thin pool to create the snapshot in or %NULL if not specified
1990 * @extra: (nullable) (array zero-terminated=1): extra options for the thin LV snapshot creation
1991 * (just passed to LVM as is)
1992 * @error: (out) (optional): place to store error (if any)
1993 *
1994 * Returns: whether the @snapshot_name snapshot of the @vg_name/@origin_name
1995 * thin LV was successfully created or not.
1996 *
1997 * Tech category: %BD_LVM_TECH_THIN-%BD_LVM_TECH_MODE_CREATE
1998 */
1999 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) {
2000 const gchar *args[8] = {"lvcreate", "-s", "-n", snapshot_name, NULL, NULL, NULL, NULL};
2001 guint next_arg = 4;
2002 gboolean success = FALSE;
2003
2004 if (pool_name) {
2005 args[next_arg] = "--thinpool";
2006 next_arg++;
2007 args[next_arg] = pool_name;
2008 next_arg++;
2009 }
2010
2011 args[next_arg] = g_strdup_printf ("%s/%s", vg_name, origin_name);
2012
2013 success = call_lvm_and_report_error (args, extra, TRUE, error);
2014 g_free ((gchar *) args[next_arg]);
2015
2016 return success;
2017 }
2018
2019 /**
2020 * get_lv_type_from_flags: (skip)
2021 * @meta: getting type for a (future) metadata LV
2022 *
2023 * Get LV type string from flags.
2024 */
2025 static const gchar* get_lv_type_from_flags (BDLVMCachePoolFlags flags, gboolean meta, GError **error G_GNUC_UNUSED) {
2026 if (!meta) {
2027 if (flags & BD_LVM_CACHE_POOL_STRIPED)
2028 return "striped";
2029 else if (flags & BD_LVM_CACHE_POOL_RAID1)
2030 return "raid1";
2031 else if (flags & BD_LVM_CACHE_POOL_RAID5)
2032 return "raid5";
2033 else if (flags & BD_LVM_CACHE_POOL_RAID6)
2034 return "raid6";
2035 else if (flags & BD_LVM_CACHE_POOL_RAID10)
2036 return "raid10";
2037 else
2038 return NULL;
2039 } else {
2040 if (flags & BD_LVM_CACHE_POOL_META_STRIPED)
2041 return "striped";
2042 else if (flags & BD_LVM_CACHE_POOL_META_RAID1)
2043 return "raid1";
2044 else if (flags & BD_LVM_CACHE_POOL_META_RAID5)
2045 return "raid5";
2046 else if (flags & BD_LVM_CACHE_POOL_META_RAID6)
2047 return "raid6";
2048 else if (flags & BD_LVM_CACHE_POOL_META_RAID10)
2049 return "raid10";
2050 else
2051 return NULL;
2052 }
2053 }
2054
2055 /**
2056 * bd_lvm_cache_create_pool:
2057 * @vg_name: name of the VG to create @pool_name in
2058 * @pool_name: name of the cache pool LV to create
2059 * @pool_size: desired size of the cache pool @pool_name
2060 * @md_size: desired size of the @pool_name cache pool's metadata LV or 0 to
2061 * use the default
2062 * @mode: cache mode of the @pool_name cache pool
2063 * @flags: a combination of (ORed) #BDLVMCachePoolFlags
2064 * @fast_pvs: (array zero-terminated=1): list of (fast) PVs to create the @pool_name
2065 * cache pool (and the metadata LV)
2066 * @error: (out) (optional): place to store error (if any)
2067 *
2068 * Returns: whether the cache pool @vg_name/@pool_name was successfully created or not
2069 *
2070 * Tech category: %BD_LVM_TECH_CACHE-%BD_LVM_TECH_MODE_CREATE
2071 */
2072 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) {
2073 gboolean success = FALSE;
2074 const gchar *type = NULL;
2075 gchar *name = NULL;
2076 gchar *msg = NULL;
2077 guint64 progress_id = 0;
2078 const gchar *args[10] = {"lvconvert", "-y", "--type", "cache-pool", "--poolmetadata", NULL, "--cachemode", NULL, NULL, NULL};
2079 GError *l_error = NULL;
2080
2081 msg = g_strdup_printf ("Started 'create cache pool %s/%s'", vg_name, pool_name);
2082 progress_id = bd_utils_report_started (msg);
2083 g_free (msg);
2084
2085 /* create an LV for the pool */
2086 type = get_lv_type_from_flags (flags, FALSE, NULL);
2087 success = bd_lvm_lvcreate (vg_name, pool_name, pool_size, type, fast_pvs, NULL, &l_error);
2088 if (!success) {
2089 g_prefix_error (&l_error, "Failed to create the pool LV: ");
2090 bd_utils_report_finished (progress_id, l_error->message);
2091 g_propagate_error (error, l_error);
2092 return FALSE;
2093 }
2094
2095 /* 1/3 steps done */
2096 bd_utils_report_progress (progress_id, 33, "Created the data LV");
2097
2098 /* determine the size of the metadata LV */
2099 type = get_lv_type_from_flags (flags, TRUE, NULL);
2100 if (md_size == 0)
2101 md_size = bd_lvm_cache_get_default_md_size (pool_size, &l_error);
2102 if (l_error) {
2103 g_prefix_error (&l_error, "Failed to determine size for the pool metadata LV: ");
2104 bd_utils_report_finished (progress_id, l_error->message);
2105 g_propagate_error (error, l_error);
2106 return FALSE;
2107 }
2108 name = g_strdup_printf ("%s_meta", pool_name);
2109
2110 /* create the metadata LV */
2111 success = bd_lvm_lvcreate (vg_name, name, md_size, type, fast_pvs, NULL, &l_error);
2112 if (!success) {
2113 g_free (name);
2114 g_prefix_error (&l_error, "Failed to create the pool metadata LV: ");
2115 bd_utils_report_finished (progress_id, l_error->message);
2116 g_propagate_error (error, l_error);
2117 return FALSE;
2118 }
2119
2120 /* 2/3 steps done */
2121 bd_utils_report_progress (progress_id, 66, "Created the metadata LV");
2122
2123 /* create the cache pool from the two LVs */
2124 args[5] = name;
2125 args[7] = (const gchar *) bd_lvm_cache_get_mode_str (mode, &l_error);
2126 if (!args[7]) {
2127 g_free ((gchar *) args[5]);
2128 bd_utils_report_finished (progress_id, l_error->message);
2129 g_propagate_error (error, l_error);
2130 return FALSE;
2131 }
2132 name = g_strdup_printf ("%s/%s", vg_name, pool_name);
2133 args[8] = name;
2134 success = call_lvm_and_report_error (args, NULL, TRUE, &l_error);
2135 g_free ((gchar *) args[5]);
2136 g_free ((gchar *) args[8]);
2137
2138 if (!success) {
2139 if (l_error)
2140 bd_utils_report_finished (progress_id, l_error->message);
2141 else
2142 bd_utils_report_finished (progress_id, "Completed");
2143 g_propagate_error (error, l_error);
2144 } else
2145 bd_utils_report_finished (progress_id, "Completed");
2146
2147 /* just return the result of the last step (it sets error on fail) */
2148 return success;
2149 }
2150
2151 /**
2152 * bd_lvm_cache_attach:
2153 * @vg_name: name of the VG containing the @data_lv and the @cache_pool_lv LVs
2154 * @data_lv: data LV to attach the @cache_pool_lv to
2155 * @cache_pool_lv: cache pool LV to attach to the @data_lv
2156 * @extra: (nullable) (array zero-terminated=1): extra options for the cache attachment
2157 * (just passed to LVM as is)
2158 * @error: (out) (optional): place to store error (if any)
2159 *
2160 * Returns: whether the @cache_pool_lv was successfully attached to the @data_lv or not
2161 *
2162 * Note: Both @data_lv and @cache_lv will be deactivated before the operation.
2163 *
2164 * Tech category: %BD_LVM_TECH_CACHE-%BD_LVM_TECH_MODE_MODIFY
2165 */
2166 gboolean bd_lvm_cache_attach (const gchar *vg_name, const gchar *data_lv, const gchar *cache_pool_lv, const BDExtraArg **extra, GError **error) {
2167 const gchar *args[8] = {"lvconvert", "-y", "--type", "cache", "--cachepool", NULL, NULL, NULL};
2168 gboolean success = FALSE;
2169
2170 args[5] = g_strdup_printf ("%s/%s", vg_name, cache_pool_lv);
2171 args[6] = g_strdup_printf ("%s/%s", vg_name, data_lv);
2172 success = call_lvm_and_report_error (args, extra, TRUE, error);
2173
2174 g_free ((gchar *) args[5]);
2175 g_free ((gchar *) args[6]);
2176 return success;
2177 }
2178
2179 /**
2180 * bd_lvm_cache_detach:
2181 * @vg_name: name of the VG containing the @cached_lv
2182 * @cached_lv: name of the cached LV to detach its cache from
2183 * @destroy: whether to destroy the cache after detach or not
2184 * @extra: (nullable) (array zero-terminated=1): extra options for the cache detachment
2185 * (just passed to LVM as is)
2186 * @error: (out) (optional): place to store error (if any)
2187 *
2188 * Returns: whether the cache was successfully detached from the @cached_lv or not
2189 *
2190 * Note: synces the cache first
2191 *
2192 * Tech category: %BD_LVM_TECH_CACHE-%BD_LVM_TECH_MODE_MODIFY
2193 */
2194 gboolean bd_lvm_cache_detach (const gchar *vg_name, const gchar *cached_lv, gboolean destroy, const BDExtraArg **extra, GError **error) {
2195 /* need to both "assume yes" and "force" to get rid of the interactive
2196 questions in case of "--uncache" */
2197 const gchar *args[6] = {"lvconvert", "-y", "-f", NULL, NULL, NULL};
2198 gboolean success = FALSE;
2199
2200 args[3] = destroy ? "--uncache" : "--splitcache";
2201 args[4] = g_strdup_printf ("%s/%s", vg_name, cached_lv);
2202 success = call_lvm_and_report_error (args, extra, TRUE, error);
2203
2204 g_free ((gchar *) args[4]);
2205 return success;
2206 }
2207
2208 /**
2209 * bd_lvm_cache_create_cached_lv:
2210 * @vg_name: name of the VG to create a cached LV in
2211 * @lv_name: name of the cached LV to create
2212 * @data_size: size of the data LV
2213 * @cache_size: size of the cache (or cached LV more precisely)
2214 * @md_size: size of the cache metadata LV or 0 to use the default
2215 * @mode: cache mode for the cached LV
2216 * @flags: a combination of (ORed) #BDLVMCachePoolFlags
2217 * @slow_pvs: (array zero-terminated=1): list of slow PVs (used for the data LV)
2218 * @fast_pvs: (array zero-terminated=1): list of fast PVs (used for the cache LV)
2219 * @error: (out) (optional): place to store error (if any)
2220 *
2221 * Returns: whether the cached LV @lv_name was successfully created or not
2222 *
2223 * Tech category: %BD_LVM_TECH_CACHE-%BD_LVM_TECH_MODE_CREATE
2224 */
2225 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,
2226 const gchar **slow_pvs, const gchar **fast_pvs, GError **error) {
2227 gboolean success = FALSE;
2228 gchar *name = NULL;
2229 gchar *msg = NULL;
2230 guint64 progress_id = 0;
2231 GError *l_error = NULL;
2232
2233 msg = g_strdup_printf ("Started 'create cached LV %s/%s'", vg_name, lv_name);
2234 progress_id = bd_utils_report_started (msg);
2235 g_free (msg);
2236
2237 name = g_strdup_printf ("%s_cache", lv_name);
2238 success = bd_lvm_cache_create_pool (vg_name, name, cache_size, md_size, mode, flags, fast_pvs, &l_error);
2239 if (!success) {
2240 g_prefix_error (&l_error, "Failed to create the cache pool '%s': ", name);
2241 g_free (name);
2242 bd_utils_report_finished (progress_id, l_error->message);
2243 g_propagate_error (error, l_error);
2244 return FALSE;
2245 }
2246
2247 /* 3/5 steps (cache pool creation has 3 steps) done */
2248 bd_utils_report_progress (progress_id, 60, "Cache pool created");
2249
2250 success = bd_lvm_lvcreate (vg_name, lv_name, data_size, NULL, slow_pvs, NULL, &l_error);
2251 if (!success) {
2252 g_free (name);
2253 g_prefix_error (&l_error, "Failed to create the data LV: ");
2254 bd_utils_report_finished (progress_id, l_error->message);
2255 g_propagate_error (error, l_error);
2256 return FALSE;
2257 }
2258
2259 /* 4/5 steps (cache pool creation has 3 steps) done */
2260 bd_utils_report_progress (progress_id, 80, "Data LV created");
2261
2262 success = bd_lvm_cache_attach (vg_name, lv_name, name, NULL, &l_error);
2263 if (!success) {
2264 g_prefix_error (&l_error, "Failed to attach the cache pool '%s' to the data LV: ", name);
2265 g_free (name);
2266 bd_utils_report_finished (progress_id, l_error->message);
2267 g_propagate_error (error, l_error);
2268 return FALSE;
2269 }
2270
2271 bd_utils_report_finished (progress_id, "Completed");
2272 g_free (name);
2273 return TRUE;
2274 }
2275
2276 /**
2277 * bd_lvm_writecache_attach:
2278 * @vg_name: name of the VG containing the @data_lv and the @cache_pool_lv LVs
2279 * @data_lv: data LV to attach the @cache_lv to
2280 * @cache_lv: cache (fast) LV to attach to the @data_lv
2281 * @extra: (nullable) (array zero-terminated=1): extra options for the cache attachment
2282 * (just passed to LVM as is)
2283 * @error: (out) (optional): place to store error (if any)
2284 *
2285 * Returns: whether the @cache_lv was successfully attached to the @data_lv or not
2286 *
2287 * Tech category: %BD_LVM_TECH_WRITECACHE-%BD_LVM_TECH_MODE_MODIFY
2288 */
2289 gboolean bd_lvm_writecache_attach (const gchar *vg_name, const gchar *data_lv, const gchar *cache_lv, const BDExtraArg **extra, GError **error) {
2290 const gchar *args[8] = {"lvconvert", "-y", "--type", "writecache", "--cachevol", NULL, NULL, NULL};
2291 gboolean success = FALSE;
2292
2293 /* both LVs need to be inactive for the writecache convert to work */
2294 success = bd_lvm_lvdeactivate (vg_name, data_lv, NULL, error);
2295 if (!success)
2296 return FALSE;
2297
2298 success = bd_lvm_lvdeactivate (vg_name, cache_lv, NULL, error);
2299 if (!success)
2300 return FALSE;
2301
2302 args[5] = g_strdup_printf ("%s/%s", vg_name, cache_lv);
2303 args[6] = g_strdup_printf ("%s/%s", vg_name, data_lv);
2304 success = call_lvm_and_report_error (args, extra, TRUE, error);
2305
2306 g_free ((gchar *) args[5]);
2307 g_free ((gchar *) args[6]);
2308 return success;
2309 }
2310
2311 /**
2312 * bd_lvm_writecache_detach:
2313 * @vg_name: name of the VG containing the @cached_lv
2314 * @cached_lv: name of the cached LV to detach its cache from
2315 * @destroy: whether to destroy the cache after detach or not
2316 * @extra: (nullable) (array zero-terminated=1): extra options for the cache detachment
2317 * (just passed to LVM as is)
2318 * @error: (out) (optional): place to store error (if any)
2319 *
2320 * Returns: whether the cache was successfully detached from the @cached_lv or not
2321 *
2322 * Note: synces the cache first
2323 *
2324 * Tech category: %BD_LVM_TECH_WRITECACHE-%BD_LVM_TECH_MODE_MODIFY
2325 */
2326 gboolean bd_lvm_writecache_detach (const gchar *vg_name, const gchar *cached_lv, gboolean destroy, const BDExtraArg **extra, GError **error) {
2327 return bd_lvm_cache_detach (vg_name, cached_lv, destroy, extra, error);
2328 }
2329
2330 /**
2331 * bd_lvm_writecache_create_cached_lv:
2332 * @vg_name: name of the VG to create a cached LV in
2333 * @lv_name: name of the cached LV to create
2334 * @data_size: size of the data LV
2335 * @cache_size: size of the cache (or cached LV more precisely)
2336 * @slow_pvs: (array zero-terminated=1): list of slow PVs (used for the data LV)
2337 * @fast_pvs: (array zero-terminated=1): list of fast PVs (used for the cache LV)
2338 * @error: (out) (optional): place to store error (if any)
2339 *
2340 * Returns: whether the cached LV @lv_name was successfully created or not
2341 *
2342 * Tech category: %BD_LVM_TECH_WRITECACHE-%BD_LVM_TECH_MODE_CREATE
2343 */
2344 gboolean bd_lvm_writecache_create_cached_lv (const gchar *vg_name, const gchar *lv_name, guint64 data_size, guint64 cache_size,
2345 const gchar **slow_pvs, const gchar **fast_pvs, GError **error) {
2346 gboolean success = FALSE;
2347 gchar *name = NULL;
2348 gchar *msg = NULL;
2349 guint64 progress_id = 0;
2350 GError *l_error = NULL;
2351
2352 msg = g_strdup_printf ("Started 'create cached LV %s/%s'", vg_name, lv_name);
2353 progress_id = bd_utils_report_started (msg);
2354 g_free (msg);
2355
2356 name = g_strdup_printf ("%s_writecache", lv_name);
2357 success = bd_lvm_lvcreate (vg_name, name, cache_size, NULL, fast_pvs, NULL, &l_error);
2358 if (!success) {
2359 g_prefix_error (&l_error, "Failed to create the cache LV '%s': ", name);
2360 g_free (name);
2361 bd_utils_report_finished (progress_id, l_error->message);
2362 g_propagate_error (error, l_error);
2363 return FALSE;
2364 }
2365
2366 /* 1/3 steps done */
2367 bd_utils_report_progress (progress_id, 33, "Cache LV created");
2368
2369 success = bd_lvm_lvcreate (vg_name, lv_name, data_size, NULL, slow_pvs, NULL, &l_error);
2370 if (!success) {
2371 g_free (name);
2372 g_prefix_error (&l_error, "Failed to create the data LV: ");
2373 bd_utils_report_finished (progress_id, l_error->message);
2374 g_propagate_error (error, l_error);
2375 return FALSE;
2376 }
2377
2378 /* 2/3 steps done */
2379 bd_utils_report_progress (progress_id, 66, "Data LV created");
2380
2381 success = bd_lvm_writecache_attach (vg_name, lv_name, name, NULL, &l_error);
2382 if (!success) {
2383 g_prefix_error (&l_error, "Failed to attach the cache LV '%s' to the data LV: ", name);
2384 g_free (name);
2385 bd_utils_report_finished (progress_id, l_error->message);
2386 g_propagate_error (error, l_error);
2387 return FALSE;
2388 }
2389
2390 bd_utils_report_finished (progress_id, "Completed");
2391 g_free (name);
2392 return TRUE;
2393 }
2394
2395 /**
2396 * bd_lvm_cache_pool_name:
2397 * @vg_name: name of the VG containing the @cached_lv
2398 * @cached_lv: cached LV to get the name of the its pool LV for
2399 * @error: (out) (optional): place to store error (if any)
2400 *
2401 * Returns: name of the cache pool LV used by the @cached_lv or %NULL in case of error
2402 *
2403 * Tech category: %BD_LVM_TECH_CACHE-%BD_LVM_TECH_MODE_QUERY
2404 */
2405 gchar* bd_lvm_cache_pool_name (const gchar *vg_name, const gchar *cached_lv, GError **error) {
2406 gchar *ret = NULL;
2407 gchar *name_start = NULL;
2408 gchar *name_end = NULL;
2409 gchar *pool_name = NULL;
2410
2411 /* same as for a thin LV, but with square brackets */
2412 ret = bd_lvm_thlvpoolname (vg_name, cached_lv, error);
2413 if (!ret)
2414 return NULL;
2415
2416 name_start = strchr (ret, '[');
2417 if (!name_start) {
2418 g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_CACHE_INVAL,
2419 "Failed to determine cache pool name from: '%s'", ret);
2420 g_free (ret);
2421 return NULL;
2422 }
2423 name_start++;
2424
2425 name_end = strchr (ret, ']');
2426 if (!name_end) {
2427 g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_CACHE_INVAL,
2428 "Failed to determine cache pool name from: '%s'", ret);
2429 g_free (ret);
2430 return NULL;
2431 }
2432
2433 pool_name = g_strndup (name_start, name_end - name_start);
2434 g_free (ret);
2435
2436 return pool_name;
2437 }
2438
2439 /**
2440 * bd_lvm_thpool_convert:
2441 * @vg_name: name of the VG to create the new thin pool in
2442 * @data_lv: name of the LV that should become the data part of the new pool
2443 * @metadata_lv: name of the LV that should become the metadata part of the new pool
2444 * @name: (nullable): name for the thin pool (if %NULL, the name @data_lv is inherited)
2445 * @extra: (nullable) (array zero-terminated=1): extra options for the thin pool creation
2446 * (just passed to LVM as is)
2447 * @error: (out) (optional): place to store error (if any)
2448 *
2449 * Converts the @data_lv and @metadata_lv into a new thin pool in the @vg_name
2450 * VG.
2451 *
2452 * Returns: whether the new thin pool was successfully created from @data_lv and
2453 * @metadata_lv or not
2454 *
2455 * Tech category: %BD_LVM_TECH_THIN-%BD_LVM_TECH_MODE_CREATE
2456 */
2457 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) {
2458 const gchar *args[8] = {"lvconvert", "--yes", "--type", "thin-pool", "--poolmetadata", metadata_lv, NULL, NULL};
2459 gboolean success = FALSE;
2460
2461 args[6] = g_strdup_printf ("%s/%s", vg_name, data_lv);
2462
2463 success = call_lvm_and_report_error (args, extra, TRUE, error);
2464 g_free ((gchar *) args[6]);
2465
2466 if (success && name)
2467 success = bd_lvm_lvrename (vg_name, data_lv, name, NULL, error);
2468
2469 return success;
2470 }
2471
2472 /**
2473 * bd_lvm_cache_pool_convert:
2474 * @vg_name: name of the VG to create the new thin pool in
2475 * @data_lv: name of the LV that should become the data part of the new pool
2476 * @metadata_lv: name of the LV that should become the metadata part of the new pool
2477 * @name: (nullable): name for the thin pool (if %NULL, the name @data_lv is inherited)
2478 * @extra: (nullable) (array zero-terminated=1): extra options for the thin pool creation
2479 * (just passed to LVM as is)
2480 * @error: (out) (optional): place to store error (if any)
2481 *
2482 * Converts the @data_lv and @metadata_lv into a new cache pool in the @vg_name
2483 * VG.
2484 *
2485 * Returns: whether the new cache pool was successfully created from @data_lv and
2486 * @metadata_lv or not
2487 *
2488 * Tech category: %BD_LVM_TECH_CACHE-%BD_LVM_TECH_MODE_CREATE
2489 */
2490 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) {
2491 const gchar *args[8] = {"lvconvert", "--yes", "--type", "cache-pool", "--poolmetadata", metadata_lv, NULL, NULL};
2492 gboolean success = FALSE;
2493
2494 args[6] = g_strdup_printf ("%s/%s", vg_name, data_lv);
2495
2496 success = call_lvm_and_report_error (args, extra, TRUE, error);
2497 g_free ((gchar *) args[6]);
2498
2499 if (success && name)
2500 success = bd_lvm_lvrename (vg_name, data_lv, name, NULL, error);
2501
2502 return success;
2503 }
2504
2505 /**
2506 * bd_lvm_vdo_pool_create:
2507 * @vg_name: name of the VG to create a new LV in
2508 * @lv_name: name of the to-be-created VDO LV
2509 * @pool_name: (nullable): name of the to-be-created VDO pool LV or %NULL for default name
2510 * @data_size: requested size of the data VDO LV (physical size of the @pool_name VDO pool LV)
2511 * @virtual_size: requested virtual_size of the @lv_name VDO LV
2512 * @index_memory: amount of index memory (in bytes) or 0 for default
2513 * @compression: whether to enable compression or not
2514 * @deduplication: whether to enable deduplication or not
2515 * @write_policy: write policy for the volume
2516 * @extra: (nullable) (array zero-terminated=1): extra options for the VDO LV creation
2517 * (just passed to LVM as is)
2518 * @error: (out) (optional): place to store error (if any)
2519 *
2520 * Returns: whether the given @vg_name/@lv_name VDO LV was successfully created or not
2521 *
2522 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_CREATE
2523 */
2524 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) {
2525 const gchar *args[16] = {"lvcreate", "--type", "vdo", "-n", lv_name, "-L", NULL, "-V", NULL,
2526 "--compression", compression ? "y" : "n",
2527 "--deduplication", deduplication ? "y" : "n",
2528 "-y", NULL, NULL};
2529 gboolean success = FALSE;
2530 gchar *old_config = NULL;
2531 const gchar *write_policy_str = NULL;
2532
2533 write_policy_str = bd_lvm_get_vdo_write_policy_str (write_policy, error);
2534 if (!write_policy_str)
2535 return FALSE;
2536
2537 args[6] = g_strdup_printf ("%"G_GUINT64_FORMAT"K", data_size / 1024);
2538 args[8] = g_strdup_printf ("%"G_GUINT64_FORMAT"K", virtual_size / 1024);
2539
2540 if (pool_name) {
2541 args[14] = g_strdup_printf ("%s/%s", vg_name, pool_name);
2542 } else
2543 args[14] = vg_name;
2544
2545 /* index_memory and write_policy can be specified only using the config */
2546 g_mutex_lock (&global_config_lock);
2547 old_config = global_config_str;
2548 if (index_memory != 0)
2549 global_config_str = g_strdup_printf ("%s allocation {vdo_index_memory_size_mb=%"G_GUINT64_FORMAT" vdo_write_policy=\"%s\"}", old_config ? old_config : "",
2550 index_memory / (1024 * 1024),
2551 write_policy_str);
2552 else
2553 global_config_str = g_strdup_printf ("%s allocation {vdo_write_policy=\"%s\"}", old_config ? old_config : "",
2554 write_policy_str);
2555
2556 success = call_lvm_and_report_error (args, extra, FALSE, error);
2557
2558 g_free (global_config_str);
2559 global_config_str = old_config;
2560 g_mutex_unlock (&global_config_lock);
2561
2562 g_free ((gchar *) args[6]);
2563 g_free ((gchar *) args[8]);
2564
2565 if (pool_name)
2566 g_free ((gchar *) args[14]);
2567
2568 return success;
2569 }
2570
2571 static gboolean _vdo_set_compression_deduplication (const gchar *vg_name, const gchar *pool_name, const gchar *op, gboolean enable, const BDExtraArg **extra, GError **error) {
2572 const gchar *args[5] = {"lvchange", op, enable ? "y" : "n", NULL, NULL};
2573 gboolean success = FALSE;
2574
2575 args[3] = g_strdup_printf ("%s/%s", vg_name, pool_name);
2576
2577 success = call_lvm_and_report_error (args, extra, TRUE, error);
2578 g_free ((gchar *) args[3]);
2579
2580 return success;
2581 }
2582
2583 /**
2584 * bd_lvm_vdo_enable_compression:
2585 * @vg_name: name of the VG containing the to-be-changed VDO pool LV
2586 * @pool_name: name of the VDO pool LV to enable compression on
2587 * @extra: (nullable) (array zero-terminated=1): extra options for the VDO change
2588 * (just passed to LVM as is)
2589 * @error: (out) (optional): place to store error (if any)
2590 *
2591 * Returns: whether compression was successfully enabled on @vg_name/@pool_name LV or not
2592 *
2593 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_MODIFY
2594 */
2595 gboolean bd_lvm_vdo_enable_compression (const gchar *vg_name, const gchar *pool_name, const BDExtraArg **extra, GError **error) {
2596 return _vdo_set_compression_deduplication (vg_name, pool_name, "--compression", TRUE, extra, error);
2597 }
2598
2599 /**
2600 * bd_lvm_vdo_disable_compression:
2601 * @vg_name: name of the VG containing the to-be-changed VDO pool LV
2602 * @pool_name: name of the VDO pool LV to disable compression on
2603 * @extra: (nullable) (array zero-terminated=1): extra options for the VDO change
2604 * (just passed to LVM as is)
2605 * @error: (out) (optional): place to store error (if any)
2606 *
2607 * Returns: whether compression was successfully disabled on @vg_name/@pool_name LV or not
2608 *
2609 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_MODIFY
2610 */
2611 gboolean bd_lvm_vdo_disable_compression (const gchar *vg_name, const gchar *pool_name, const BDExtraArg **extra, GError **error) {
2612 return _vdo_set_compression_deduplication (vg_name, pool_name, "--compression", FALSE, extra, error);
2613 }
2614
2615 /**
2616 * bd_lvm_vdo_enable_deduplication:
2617 * @vg_name: name of the VG containing the to-be-changed VDO pool LV
2618 * @pool_name: name of the VDO pool LV to enable deduplication on
2619 * @extra: (nullable) (array zero-terminated=1): extra options for the VDO change
2620 * (just passed to LVM as is)
2621 * @error: (out) (optional): place to store error (if any)
2622 *
2623 * Returns: whether deduplication was successfully enabled on @vg_name/@pool_name LV or not
2624 *
2625 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_MODIFY
2626 */
2627 gboolean bd_lvm_vdo_enable_deduplication (const gchar *vg_name, const gchar *pool_name, const BDExtraArg **extra, GError **error) {
2628 return _vdo_set_compression_deduplication (vg_name, pool_name, "--deduplication", TRUE, extra, error);
2629 }
2630
2631 /**
2632 * bd_lvm_vdo_disable_deduplication:
2633 * @vg_name: name of the VG containing the to-be-changed VDO pool LV
2634 * @pool_name: name of the VDO pool LV to disable deduplication on
2635 * @extra: (nullable) (array zero-terminated=1): extra options for the VDO change
2636 * (just passed to LVM as is)
2637 * @error: (out) (optional): place to store error (if any)
2638 *
2639 * Returns: whether deduplication was successfully disabled on @vg_name/@pool_name LV or not
2640 *
2641 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_MODIFY
2642 */
2643 gboolean bd_lvm_vdo_disable_deduplication (const gchar *vg_name, const gchar *pool_name, const BDExtraArg **extra, GError **error) {
2644 return _vdo_set_compression_deduplication (vg_name, pool_name, "--deduplication", FALSE, extra, error);
2645 }
2646
2647 /**
2648 * bd_lvm_vdo_info:
2649 * @vg_name: name of the VG that contains the LV to get information about
2650 * @lv_name: name of the LV to get information about
2651 * @error: (out) (optional): place to store error (if any)
2652 *
2653 * Returns: (transfer full): information about the @vg_name/@lv_name LV or %NULL in case
2654 * of error (the @error) gets populated in those cases)
2655 *
2656 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_QUERY
2657 */
2658 BDLVMVDOPooldata* bd_lvm_vdo_info (const gchar *vg_name, const gchar *lv_name, GError **error) {
2659 const gchar *args[10] = {"lvs", "--nosuffix", "--units=b",
2660 "--reportformat", "json_std", "-a",
2661 "-o", "vdo_operating_mode,vdo_compression_state,vdo_index_state,vdo_write_policy,vdo_index_memory_size,vdo_used_size,vdo_saving_percent,vdo_compression,vdo_deduplication",
2662 NULL, NULL};
2663 JsonParser *parser = NULL;
2664 JsonArray *lv_array = NULL;
2665 BDLVMVDOPooldata *ret = NULL;
2666
2667 args[8] = g_strdup_printf ("%s/%s", vg_name, lv_name);
2668
2669 lv_array = call_lvm_and_parse_json_report (args, "lv", &parser, FALSE, error);
2670 g_free ((gchar *) args[8]);
2671
2672 if (!lv_array)
2673 return NULL;
2674
2675 if (json_array_get_length (lv_array) == 0) {
2676 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_PARSE,
2677 "Failed to parse information about the VDO LV");
2678 g_object_unref (parser);
2679 return NULL;
2680 }
2681
2682 ret = get_vdo_data_from_json (json_array_get_object_element (lv_array, 0));
2683
2684 g_object_unref (parser);
2685 return ret;
2686 }
2687
2688 /**
2689 * bd_lvm_vdo_resize:
2690 * @vg_name: name of the VG containing the to-be-resized VDO LV
2691 * @lv_name: name of the to-be-resized VDO LV
2692 * @size: the requested new size of the VDO LV
2693 * @extra: (nullable) (array zero-terminated=1): extra options for the VDO LV resize
2694 * (just passed to LVM as is)
2695 * @error: (out) (optional): place to store error (if any)
2696 *
2697 * Returns: whether the @vg_name/@lv_name VDO LV was successfully resized or not
2698 *
2699 * Note: Reduction needs to process TRIM for reduced disk area to unmap used data blocks
2700 * from the VDO pool LV and it may take a long time.
2701 *
2702 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_MODIFY
2703 */
2704 gboolean bd_lvm_vdo_resize (const gchar *vg_name, const gchar *lv_name, guint64 size, const BDExtraArg **extra, GError **error) {
2705 return bd_lvm_lvresize (vg_name, lv_name, size, extra, error);
2706 }
2707
2708 /**
2709 * bd_lvm_vdo_pool_resize:
2710 * @vg_name: name of the VG containing the to-be-resized VDO pool LV
2711 * @pool_name: name of the to-be-resized VDO pool LV
2712 * @size: the requested new size of the VDO pool LV
2713 * @extra: (nullable) (array zero-terminated=1): extra options for the VDO pool LV resize
2714 * (just passed to LVM as is)
2715 * @error: (out) (optional): place to store error (if any)
2716 *
2717 * Returns: whether the @vg_name/@pool_name VDO pool LV was successfully resized or not
2718 *
2719 * Note: Size of the VDO pool LV can be only extended, not reduced.
2720 *
2721 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_MODIFY
2722 */
2723 gboolean bd_lvm_vdo_pool_resize (const gchar *vg_name, const gchar *pool_name, guint64 size, const BDExtraArg **extra, GError **error) {
2724 BDLVMLVdata *info = NULL;
2725
2726 info = bd_lvm_lvinfo (vg_name, pool_name, error);
2727 if (!info)
2728 return FALSE;
2729
2730 if (info->size >= size) {
2731 g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_NOT_SUPPORTED,
2732 "Reducing physical size of the VDO pool LV is not supported.");
2733 bd_lvm_lvdata_free (info);
2734 return FALSE;
2735 }
2736
2737 bd_lvm_lvdata_free (info);
2738
2739 return bd_lvm_lvresize (vg_name, pool_name, size, extra, error);
2740 }
2741
2742 /**
2743 * bd_lvm_vdo_pool_convert:
2744 * @vg_name: name of the VG that contains @pool_lv
2745 * @pool_lv: name of the LV that should become the new VDO pool LV
2746 * @name: (nullable): name for the VDO LV or %NULL for default name
2747 * @virtual_size: virtual size for the new VDO LV
2748 * @index_memory: amount of index memory (in bytes) or 0 for default
2749 * @compression: whether to enable compression or not
2750 * @deduplication: whether to enable deduplication or not
2751 * @write_policy: write policy for the volume
2752 * @extra: (nullable) (array zero-terminated=1): extra options for the VDO pool creation
2753 * (just passed to LVM as is)
2754 * @error: (out) (optional): place to store error (if any)
2755 *
2756 * Converts the @pool_lv into a new VDO pool LV in the @vg_name VG and creates a new
2757 * @name VDO LV with size @virtual_size.
2758 *
2759 * Note: All data on @pool_lv will be irreversibly destroyed.
2760 *
2761 * Returns: whether the new VDO pool LV was successfully created from @pool_lv and or not
2762 *
2763 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_CREATE&%BD_LVM_TECH_MODE_MODIFY
2764 */
2765 gboolean bd_lvm_vdo_pool_convert (const gchar *vg_name, const gchar *pool_lv, const gchar *name, guint64 virtual_size, guint64 index_memory, gboolean compression, gboolean deduplication, BDLVMVDOWritePolicy write_policy, const BDExtraArg **extra, GError **error) {
2766 const gchar *args[14] = {"lvconvert", "--yes", "--type", "vdo-pool",
2767 "--compression", compression ? "y" : "n",
2768 "--deduplication", deduplication ? "y" : "n",
2769 NULL, NULL, NULL, NULL, NULL, NULL};
2770 gboolean success = FALSE;
2771 guint next_arg = 8;
2772 gchar *size_str = NULL;
2773 gchar *lv_spec = NULL;
2774 gchar *old_config = NULL;
2775 const gchar *write_policy_str = NULL;
2776
2777 write_policy_str = bd_lvm_get_vdo_write_policy_str (write_policy, error);
2778 if (!write_policy_str)
2779 return FALSE;
2780
2781 if (name) {
2782 args[next_arg++] = "-n";
2783 args[next_arg++] = name;
2784 }
2785
2786 args[next_arg++] = "-V";
2787 size_str = g_strdup_printf ("%"G_GUINT64_FORMAT"K", virtual_size / 1024);
2788 args[next_arg++] = size_str;
2789 lv_spec = g_strdup_printf ("%s/%s", vg_name, pool_lv);
2790 args[next_arg++] = lv_spec;
2791
2792 /* index_memory and write_policy can be specified only using the config */
2793 g_mutex_lock (&global_config_lock);
2794 old_config = global_config_str;
2795 if (index_memory != 0)
2796 global_config_str = g_strdup_printf ("%s allocation {vdo_index_memory_size_mb=%"G_GUINT64_FORMAT" vdo_write_policy=\"%s\"}", old_config ? old_config : "",
2797 index_memory / (1024 * 1024),
2798 write_policy_str);
2799 else
2800 global_config_str = g_strdup_printf ("%s allocation {vdo_write_policy=\"%s\"}", old_config ? old_config : "",
2801 write_policy_str);
2802
2803 success = call_lvm_and_report_error (args, extra, FALSE, error);
2804
2805 g_free (global_config_str);
2806 global_config_str = old_config;
2807 g_mutex_unlock (&global_config_lock);
2808
2809 g_free (size_str);
2810 g_free (lv_spec);
2811
2812 return success;
2813 }
2814
2815 /**
2816 * bd_lvm_vdolvpoolname:
2817 * @vg_name: name of the VG containing the queried VDO LV
2818 * @lv_name: name of the queried VDO LV
2819 * @error: (out) (optional): place to store error (if any)
2820 *
2821 * Returns: (transfer full): the name of the pool volume for the @vg_name/@lv_name
2822 * VDO LV or %NULL if failed to determine (@error) is set in those cases)
2823 *
2824 * Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_QUERY
2825 */
2826 gchar* bd_lvm_vdolvpoolname (const gchar *vg_name, const gchar *lv_name, GError **error) {
2827 gboolean success = FALSE;
2828 gchar *output = NULL;
2829 const gchar *args[6] = {"lvs", "--noheadings", "-o", "pool_lv", NULL, NULL};
2830 args[4] = g_strdup_printf ("%s/%s", vg_name, lv_name);
2831
2832 success = call_lvm_and_capture_output (args, NULL, &output, error);
2833 g_free ((gchar *) args[4]);
2834
2835 if (!success)
2836 /* the error is already populated from the call */
2837 return NULL;
2838
2839 return g_strstrip (output);
2840 }