| 1 |
/* |
| 2 |
* Copyright (C) 2014-2025 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 |
*/ |
| 18 |
|
| 19 |
#include <glib.h> |
| 20 |
#include <math.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <libdevmapper.h> |
| 23 |
|
| 24 |
#include "lvm.h" |
| 25 |
#include "lvm-private.h" |
| 26 |
#include "check_deps.h" |
| 27 |
#include "dm_logging.h" |
| 28 |
#include "vdo_stats.h" |
| 29 |
|
| 30 |
|
| 31 |
#define INT_FLOAT_EPS 1e-5 |
| 32 |
|
| 33 |
#define MIN_PE_SIZE (1 KiB) |
| 34 |
#define MAX_PE_SIZE (16 GiB) |
| 35 |
|
| 36 |
#define VDO_POOL_SUFFIX "vpool" |
| 37 |
|
| 38 |
#define THPOOL_MD_FACTOR_NEW (0.2) |
| 39 |
#define THPOOL_MD_FACTOR_EXISTS (1 / 6.0) |
| 40 |
|
| 41 |
#define MIN_THPOOL_MD_SIZE (4 MiB) |
| 42 |
/* DM_THIN_MAX_METADATA_SIZE is in 512 sectors */ |
| 43 |
#define MAX_THPOOL_MD_SIZE (DM_THIN_MAX_METADATA_SIZE * 512) |
| 44 |
|
| 45 |
#define MIN_THPOOL_CHUNK_SIZE (64 KiB) |
| 46 |
#define MAX_THPOOL_CHUNK_SIZE (1 GiB) |
| 47 |
#define DEFAULT_CHUNK_SIZE (64 KiB) |
| 48 |
|
| 49 |
/* according to lvmcache (7) */ |
| 50 |
#define MIN_CACHE_MD_SIZE (8 MiB) |
| 51 |
|
| 52 |
#ifdef __LP64__ |
| 53 |
/* 64bit system */ |
| 54 |
#define MAX_LV_SIZE (8 EiB) |
| 55 |
#else |
| 56 |
/* 32bit system */ |
| 57 |
#define MAX_LV_SIZE (16 TiB) |
| 58 |
#endif |
| 59 |
|
| 60 |
GMutex global_config_lock; |
| 61 |
gchar *global_config_str = NULL; |
| 62 |
gchar *global_devices_str = NULL; |
| 63 |
|
| 64 |
BDLVMPVdata* bd_lvm_pvdata_copy (BDLVMPVdata *data) { |
| 65 |
if (data == NULL) |
| 66 |
return NULL; |
| 67 |
|
| 68 |
BDLVMPVdata *new_data = g_new0 (BDLVMPVdata, 1); |
| 69 |
|
| 70 |
new_data->pv_name = g_strdup (data->pv_name); |
| 71 |
new_data->pv_uuid = g_strdup (data->pv_uuid); |
| 72 |
new_data->pv_free = data->pv_free; |
| 73 |
new_data->pv_size = data->pv_size; |
| 74 |
new_data->pe_start = data->pe_start; |
| 75 |
new_data->vg_name = g_strdup (data->vg_name); |
| 76 |
new_data->vg_uuid = g_strdup (data->vg_uuid); |
| 77 |
new_data->vg_size = data->vg_size; |
| 78 |
new_data->vg_free = data->vg_free; |
| 79 |
new_data->vg_extent_size = data->vg_extent_size; |
| 80 |
new_data->vg_extent_count = data->vg_extent_count; |
| 81 |
new_data->vg_free_count = data->vg_free_count; |
| 82 |
new_data->vg_pv_count = data->vg_pv_count; |
| 83 |
new_data->pv_tags = g_strdupv (data->pv_tags); |
| 84 |
new_data->missing = data->missing; |
| 85 |
|
| 86 |
return new_data; |
| 87 |
} |
| 88 |
|
| 89 |
void bd_lvm_pvdata_free (BDLVMPVdata *data) { |
| 90 |
if (data == NULL) |
| 91 |
return; |
| 92 |
|
| 93 |
g_free (data->pv_name); |
| 94 |
g_free (data->pv_uuid); |
| 95 |
g_free (data->vg_name); |
| 96 |
g_free (data->vg_uuid); |
| 97 |
g_strfreev (data->pv_tags); |
| 98 |
g_free (data); |
| 99 |
} |
| 100 |
|
| 101 |
BDLVMVGdata* bd_lvm_vgdata_copy (BDLVMVGdata *data) { |
| 102 |
if (data == NULL) |
| 103 |
return NULL; |
| 104 |
|
| 105 |
BDLVMVGdata *new_data = g_new0 (BDLVMVGdata, 1); |
| 106 |
|
| 107 |
new_data->name = g_strdup (data->name); |
| 108 |
new_data->uuid = g_strdup (data->uuid); |
| 109 |
new_data->size = data->size; |
| 110 |
new_data->free = data->free; |
| 111 |
new_data->extent_size = data->extent_size; |
| 112 |
new_data->extent_count = data->extent_count; |
| 113 |
new_data->free_count = data->free_count; |
| 114 |
new_data->pv_count = data->pv_count; |
| 115 |
new_data->vg_tags = g_strdupv (data->vg_tags); |
| 116 |
new_data->exported = data->exported; |
| 117 |
return new_data; |
| 118 |
} |
| 119 |
|
| 120 |
void bd_lvm_vgdata_free (BDLVMVGdata *data) { |
| 121 |
if (data == NULL) |
| 122 |
return; |
| 123 |
|
| 124 |
g_free (data->name); |
| 125 |
g_free (data->uuid); |
| 126 |
g_strfreev (data->vg_tags); |
| 127 |
g_free (data); |
| 128 |
} |
| 129 |
|
| 130 |
BDLVMSEGdata* bd_lvm_segdata_copy (BDLVMSEGdata *data) { |
| 131 |
if (data == NULL) |
| 132 |
return NULL; |
| 133 |
|
| 134 |
BDLVMSEGdata *new_data = g_new0 (BDLVMSEGdata, 1); |
| 135 |
|
| 136 |
new_data->size_pe = data->size_pe; |
| 137 |
new_data->pv_start_pe = data->pv_start_pe; |
| 138 |
new_data->pvdev = g_strdup (data->pvdev); |
| 139 |
return new_data; |
| 140 |
} |
| 141 |
|
| 142 |
void bd_lvm_segdata_free (BDLVMSEGdata *data) { |
| 143 |
if (data == NULL) |
| 144 |
return; |
| 145 |
|
| 146 |
g_free (data->pvdev); |
| 147 |
g_free (data); |
| 148 |
} |
| 149 |
|
| 150 |
static BDLVMSEGdata **copy_segs (BDLVMSEGdata **segs) { |
| 151 |
int len; |
| 152 |
BDLVMSEGdata **new_segs; |
| 153 |
|
| 154 |
if (segs == NULL) |
| 155 |
return NULL; |
| 156 |
|
| 157 |
for (len = 0; segs[len]; len++) |
| 158 |
; |
| 159 |
|
| 160 |
new_segs = g_new0 (BDLVMSEGdata *, len+1); |
| 161 |
for (int i = 0; i < len; i++) |
| 162 |
new_segs[i] = bd_lvm_segdata_copy (segs[i]); |
| 163 |
|
| 164 |
return new_segs; |
| 165 |
} |
| 166 |
|
| 167 |
static void free_segs (BDLVMSEGdata **segs) { |
| 168 |
if (segs == NULL) |
| 169 |
return; |
| 170 |
|
| 171 |
for (int i = 0; segs[i]; i++) |
| 172 |
bd_lvm_segdata_free (segs[i]); |
| 173 |
(g_free) (segs); |
| 174 |
} |
| 175 |
|
| 176 |
BDLVMLVdata* bd_lvm_lvdata_copy (BDLVMLVdata *data) { |
| 177 |
if (data == NULL) |
| 178 |
return NULL; |
| 179 |
|
| 180 |
BDLVMLVdata *new_data = g_new0 (BDLVMLVdata, 1); |
| 181 |
|
| 182 |
new_data->lv_name = g_strdup (data->lv_name); |
| 183 |
new_data->vg_name = g_strdup (data->vg_name); |
| 184 |
new_data->uuid = g_strdup (data->uuid); |
| 185 |
new_data->size = data->size; |
| 186 |
new_data->attr = g_strdup (data->attr); |
| 187 |
new_data->segtype = g_strdup (data->segtype); |
| 188 |
new_data->origin = g_strdup (data->origin); |
| 189 |
new_data->pool_lv = g_strdup (data->pool_lv); |
| 190 |
new_data->data_lv = g_strdup (data->data_lv); |
| 191 |
new_data->metadata_lv = g_strdup (data->metadata_lv); |
| 192 |
new_data->roles = g_strdup (data->roles); |
| 193 |
new_data->move_pv = g_strdup (data->move_pv); |
| 194 |
new_data->data_percent = data->data_percent; |
| 195 |
new_data->metadata_percent = data->metadata_percent; |
| 196 |
new_data->copy_percent = data->copy_percent; |
| 197 |
new_data->lv_tags = g_strdupv (data->lv_tags); |
| 198 |
new_data->data_lvs = g_strdupv (data->data_lvs); |
| 199 |
new_data->metadata_lvs = g_strdupv (data->metadata_lvs); |
| 200 |
new_data->segs = copy_segs (data->segs); |
| 201 |
return new_data; |
| 202 |
} |
| 203 |
|
| 204 |
void bd_lvm_lvdata_free (BDLVMLVdata *data) { |
| 205 |
if (data == NULL) |
| 206 |
return; |
| 207 |
|
| 208 |
g_free (data->lv_name); |
| 209 |
g_free (data->vg_name); |
| 210 |
g_free (data->uuid); |
| 211 |
g_free (data->attr); |
| 212 |
g_free (data->segtype); |
| 213 |
g_free (data->origin); |
| 214 |
g_free (data->pool_lv); |
| 215 |
g_free (data->data_lv); |
| 216 |
g_free (data->metadata_lv); |
| 217 |
g_free (data->roles); |
| 218 |
g_free (data->move_pv); |
| 219 |
g_strfreev (data->lv_tags); |
| 220 |
g_strfreev (data->data_lvs); |
| 221 |
g_strfreev (data->metadata_lvs); |
| 222 |
free_segs (data->segs); |
| 223 |
g_free (data); |
| 224 |
} |
| 225 |
|
| 226 |
BDLVMVDOPooldata* bd_lvm_vdopooldata_copy (BDLVMVDOPooldata *data) { |
| 227 |
if (data == NULL) |
| 228 |
return NULL; |
| 229 |
|
| 230 |
BDLVMVDOPooldata *new_data = g_new0 (BDLVMVDOPooldata, 1); |
| 231 |
|
| 232 |
new_data->operating_mode = data->operating_mode; |
| 233 |
new_data->compression_state = data->compression_state; |
| 234 |
new_data->index_state = data->index_state; |
| 235 |
new_data->write_policy = data->write_policy; |
| 236 |
new_data->used_size = data->used_size; |
| 237 |
new_data->saving_percent = data->saving_percent; |
| 238 |
new_data->index_memory_size = data->index_memory_size; |
| 239 |
new_data->deduplication = data->deduplication; |
| 240 |
new_data->compression = data->compression; |
| 241 |
return new_data; |
| 242 |
} |
| 243 |
|
| 244 |
void bd_lvm_vdopooldata_free (BDLVMVDOPooldata *data) { |
| 245 |
if (data == NULL) |
| 246 |
return; |
| 247 |
|
| 248 |
g_free (data); |
| 249 |
} |
| 250 |
|
| 251 |
BDLVMCacheStats* bd_lvm_cache_stats_copy (BDLVMCacheStats *data) { |
| 252 |
if (data == NULL) |
| 253 |
return NULL; |
| 254 |
|
| 255 |
BDLVMCacheStats *new = g_new0 (BDLVMCacheStats, 1); |
| 256 |
|
| 257 |
new->block_size = data->block_size; |
| 258 |
new->cache_size = data->cache_size; |
| 259 |
new->cache_used = data->cache_used; |
| 260 |
new->md_block_size = data->md_block_size; |
| 261 |
new->md_size = data->md_size; |
| 262 |
new->md_used = data->md_used; |
| 263 |
new->read_hits = data->read_hits; |
| 264 |
new->read_misses = data->read_misses; |
| 265 |
new->write_hits = data->write_hits; |
| 266 |
new->write_misses = data->write_misses; |
| 267 |
new->mode = data->mode; |
| 268 |
|
| 269 |
return new; |
| 270 |
} |
| 271 |
|
| 272 |
void bd_lvm_cache_stats_free (BDLVMCacheStats *data) { |
| 273 |
g_free (data); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* bd_lvm_is_supported_pe_size: |
| 278 |
* @size: size (in bytes) to test |
| 279 |
* @error: (out) (optional): place to store error (if any) |
| 280 |
* |
| 281 |
* Returns: whether the given size is supported physical extent size or not |
| 282 |
* |
| 283 |
* Tech category: %BD_LVM_TECH_CALCS no mode (it is ignored) |
| 284 |
*/ |
| 285 |
gboolean bd_lvm_is_supported_pe_size (guint64 size, GError **error G_GNUC_UNUSED) { |
| 286 |
return (((size % 2) == 0) && (size >= (MIN_PE_SIZE)) && (size <= (MAX_PE_SIZE))); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* bd_lvm_get_supported_pe_sizes: |
| 291 |
* @error: (out) (optional): place to store error (if any) |
| 292 |
* |
| 293 |
* Returns: (transfer full) (array fixed-size=25): list of supported PE sizes |
| 294 |
* |
| 295 |
* Tech category: %BD_LVM_TECH_CALCS no mode (it is ignored) |
| 296 |
*/ |
| 297 |
guint64 *bd_lvm_get_supported_pe_sizes (GError **error G_GNUC_UNUSED) { |
| 298 |
guint8 i; |
| 299 |
guint64 val = MIN_PE_SIZE; |
| 300 |
guint8 num_items = ((guint8) round (log2 ((double) MAX_PE_SIZE))) - ((guint8) round (log2 ((double) MIN_PE_SIZE))) + 2; |
| 301 |
guint64 *ret = g_new0 (guint64, num_items); |
| 302 |
|
| 303 |
for (i=0; (val <= MAX_PE_SIZE); i++, val = val * 2) |
| 304 |
ret[i] = val; |
| 305 |
|
| 306 |
ret[num_items-1] = 0; |
| 307 |
|
| 308 |
return ret; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* bd_lvm_get_max_lv_size: |
| 313 |
* @error: (out) (optional): place to store error (if any) |
| 314 |
* |
| 315 |
* Returns: maximum LV size in bytes |
| 316 |
* |
| 317 |
* Tech category: %BD_LVM_TECH_CALCS no mode (it is ignored) |
| 318 |
*/ |
| 319 |
guint64 bd_lvm_get_max_lv_size (GError **error G_GNUC_UNUSED) { |
| 320 |
return MAX_LV_SIZE; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* bd_lvm_round_size_to_pe: |
| 325 |
* @size: size to be rounded |
| 326 |
* @pe_size: physical extent (PE) size or 0 to use the default |
| 327 |
* @roundup: whether to round up or down (ceil or floor) |
| 328 |
* @error: (out) (optional): place to store error (if any) |
| 329 |
* |
| 330 |
* Returns: @size rounded to @pe_size according to the @roundup |
| 331 |
* |
| 332 |
* Rounds given @size up/down to a multiple of @pe_size according to the value |
| 333 |
* of the @roundup parameter. If the rounded value is too big to fit in the |
| 334 |
* return type, the result is rounded down (floored) regardless of the @roundup |
| 335 |
* parameter. |
| 336 |
* |
| 337 |
* Tech category: %BD_LVM_TECH_CALCS no mode (it is ignored) |
| 338 |
*/ |
| 339 |
guint64 bd_lvm_round_size_to_pe (guint64 size, guint64 pe_size, gboolean roundup, GError **error G_GNUC_UNUSED) { |
| 340 |
pe_size = RESOLVE_PE_SIZE (pe_size); |
| 341 |
guint64 delta = size % pe_size; |
| 342 |
if (delta == 0) |
| 343 |
return size; |
| 344 |
|
| 345 |
if (roundup && (((G_MAXUINT64 - (pe_size - delta)) >= size))) |
| 346 |
return size + (pe_size - delta); |
| 347 |
else |
| 348 |
return size - delta; |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* bd_lvm_get_lv_physical_size: |
| 353 |
* @lv_size: LV size |
| 354 |
* @pe_size: PE size |
| 355 |
* @error: (out) (optional): place to store error (if any) |
| 356 |
* |
| 357 |
* Returns: space taken on disk(s) by the LV with given @size |
| 358 |
* |
| 359 |
* Gives number of bytes needed for an LV with the size @lv_size on an LVM stack |
| 360 |
* using given @pe_size. |
| 361 |
* |
| 362 |
* Tech category: %BD_LVM_TECH_CALCS no mode (it is ignored) |
| 363 |
*/ |
| 364 |
guint64 bd_lvm_get_lv_physical_size (guint64 lv_size, guint64 pe_size, GError **error) { |
| 365 |
pe_size = RESOLVE_PE_SIZE (pe_size); |
| 366 |
|
| 367 |
/* the LV just takes space rounded up to the multiple of extent size */ |
| 368 |
return bd_lvm_round_size_to_pe (lv_size, pe_size, TRUE, error); |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* bd_lvm_get_thpool_padding: |
| 373 |
* @size: size of the thin pool |
| 374 |
* @pe_size: PE size or 0 if the default value should be used |
| 375 |
* @included: if padding is already included in the size |
| 376 |
* @error: (out) (optional): place to store error (if any) |
| 377 |
* |
| 378 |
* Returns: size of the padding needed for a thin pool with the given @size |
| 379 |
* according to the @pe_size and @included |
| 380 |
* |
| 381 |
* Tech category: %BD_LVM_TECH_THIN_CALCS no mode (it is ignored) |
| 382 |
*/ |
| 383 |
guint64 bd_lvm_get_thpool_padding (guint64 size, guint64 pe_size, gboolean included, GError **error) { |
| 384 |
guint64 raw_md_size; |
| 385 |
pe_size = RESOLVE_PE_SIZE (pe_size); |
| 386 |
|
| 387 |
if (included) |
| 388 |
raw_md_size = (guint64) ceil (size * THPOOL_MD_FACTOR_EXISTS); |
| 389 |
else |
| 390 |
raw_md_size = (guint64) ceil (size * THPOOL_MD_FACTOR_NEW); |
| 391 |
|
| 392 |
return MIN (bd_lvm_round_size_to_pe (raw_md_size, pe_size, TRUE, error), |
| 393 |
bd_lvm_round_size_to_pe (MAX_THPOOL_MD_SIZE, pe_size, TRUE, error)); |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* bd_lvm_get_thpool_meta_size: |
| 398 |
* @size: size of the thin pool |
| 399 |
* @chunk_size: chunk size of the thin pool or 0 to use the default |
| 400 |
* @n_snapshots: ignored |
| 401 |
* @error: (out) (optional): place to store error (if any) |
| 402 |
* |
| 403 |
* Note: This function will be changed in 3.0: the @n_snapshots parameter |
| 404 |
* is currently not used and will be removed. |
| 405 |
* |
| 406 |
* Returns: recommended size of the metadata space for the specified pool |
| 407 |
* |
| 408 |
* Tech category: %BD_LVM_TECH_THIN_CALCS no mode (it is ignored) |
| 409 |
*/ |
| 410 |
guint64 bd_lvm_get_thpool_meta_size (guint64 size, guint64 chunk_size, guint64 n_snapshots G_GNUC_UNUSED, GError **error G_GNUC_UNUSED) { |
| 411 |
guint64 md_size = 0; |
| 412 |
|
| 413 |
/* based on lvcreate metadata size calculation */ |
| 414 |
md_size = UINT64_C (64) * size / (chunk_size ? chunk_size : DEFAULT_CHUNK_SIZE); |
| 415 |
|
| 416 |
if (md_size > MAX_THPOOL_MD_SIZE) |
| 417 |
md_size = MAX_THPOOL_MD_SIZE; |
| 418 |
else if (md_size < MIN_THPOOL_MD_SIZE) |
| 419 |
md_size = MIN_THPOOL_MD_SIZE; |
| 420 |
|
| 421 |
return md_size; |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* bd_lvm_is_valid_thpool_md_size: |
| 426 |
* @size: the size to be tested |
| 427 |
* @error: (out) (optional): place to store error (if any) |
| 428 |
* |
| 429 |
* Returns: whether the given size is a valid thin pool metadata size or not |
| 430 |
* |
| 431 |
* Tech category: %BD_LVM_TECH_THIN_CALCS no mode (it is ignored) |
| 432 |
*/ |
| 433 |
gboolean bd_lvm_is_valid_thpool_md_size (guint64 size, GError **error G_GNUC_UNUSED) { |
| 434 |
return ((MIN_THPOOL_MD_SIZE <= size) && (size <= MAX_THPOOL_MD_SIZE)); |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* bd_lvm_is_valid_thpool_chunk_size: |
| 439 |
* @size: the size to be tested |
| 440 |
* @discard: whether discard/TRIM is required to be supported or not |
| 441 |
* @error: (out) (optional): place to store error (if any) |
| 442 |
* |
| 443 |
* Returns: whether the given size is a valid thin pool chunk size or not |
| 444 |
* |
| 445 |
* Tech category: %BD_LVM_TECH_THIN_CALCS no mode (it is ignored) |
| 446 |
*/ |
| 447 |
gboolean bd_lvm_is_valid_thpool_chunk_size (guint64 size, gboolean discard, GError **error G_GNUC_UNUSED) { |
| 448 |
gdouble size_log2 = 0.0; |
| 449 |
|
| 450 |
if ((size < MIN_THPOOL_CHUNK_SIZE) || (size > MAX_THPOOL_CHUNK_SIZE)) |
| 451 |
return FALSE; |
| 452 |
|
| 453 |
/* To support discard, chunk size must be a power of two. Otherwise it must be a |
| 454 |
multiple of 64 KiB. */ |
| 455 |
if (discard) { |
| 456 |
size_log2 = log2 ((double) size); |
| 457 |
return ABS (((int) round (size_log2)) - size_log2) <= INT_FLOAT_EPS; |
| 458 |
} else |
| 459 |
return (size % (64 KiB)) == 0; |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
* bd_lvm_cache_get_default_md_size: |
| 464 |
* @cache_size: size of the cache to determine MD size for |
| 465 |
* @error: (out) (optional): place to store error (if any) |
| 466 |
* |
| 467 |
* Returns: recommended default size of the cache metadata LV or 0 in case of error |
| 468 |
* |
| 469 |
* Tech category: %BD_LVM_TECH_CACHE_CALCS no mode (it is ignored) |
| 470 |
*/ |
| 471 |
guint64 bd_lvm_cache_get_default_md_size (guint64 cache_size, GError **error G_GNUC_UNUSED) { |
| 472 |
return MAX ((guint64) cache_size / 1000, MIN_CACHE_MD_SIZE); |
| 473 |
} |
| 474 |
|
| 475 |
/** |
| 476 |
* bd_lvm_set_global_config: |
| 477 |
* @new_config: (nullable): string representation of the new global libblockdev LVM |
| 478 |
* configuration to set or %NULL to reset to default |
| 479 |
* @error: (out) (optional): place to store error (if any) |
| 480 |
* |
| 481 |
* |
| 482 |
* Note: This function sets configuration options for LVM calls internally |
| 483 |
* in libblockdev, it doesn't change the global lvm.conf config file. |
| 484 |
* Calling this function with `backup {backup=0 archive=0}` for example |
| 485 |
* means `--config=backup {backup=0 archive=0}"` will be added to all |
| 486 |
* calls libblockdev makes. |
| 487 |
* |
| 488 |
* Returns: whether the new requested global config @new_config was successfully |
| 489 |
* set or not |
| 490 |
* |
| 491 |
* Tech category: %BD_LVM_TECH_GLOB_CONF no mode (it is ignored) |
| 492 |
*/ |
| 493 |
gboolean bd_lvm_set_global_config (const gchar *new_config, GError **error G_GNUC_UNUSED) { |
| 494 |
/* XXX: the error attribute will likely be used in the future when |
| 495 |
some validation comes into the game */ |
| 496 |
|
| 497 |
g_mutex_lock (&global_config_lock); |
| 498 |
|
| 499 |
/* first free the old value */ |
| 500 |
g_free (global_config_str); |
| 501 |
|
| 502 |
/* now store the new one */ |
| 503 |
if (!new_config || g_strcmp0 (new_config, "") == 0) |
| 504 |
global_config_str = NULL; |
| 505 |
else |
| 506 |
global_config_str = g_strdup (new_config); |
| 507 |
|
| 508 |
g_mutex_unlock (&global_config_lock); |
| 509 |
return TRUE; |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* bd_lvm_get_global_config: |
| 514 |
* @error: (out) (optional): place to store error (if any) |
| 515 |
* |
| 516 |
* Returns: (transfer full): a copy of a string representation of the currently |
| 517 |
* set libblockdev LVM global configuration |
| 518 |
* |
| 519 |
* Note: This function does not change the global `lvm.conf` config |
| 520 |
* file, see %bd_lvm_set_global_config for details. |
| 521 |
* |
| 522 |
* Tech category: %BD_LVM_TECH_GLOB_CONF no mode (it is ignored) |
| 523 |
*/ |
| 524 |
gchar* bd_lvm_get_global_config (GError **error G_GNUC_UNUSED) { |
| 525 |
gchar *ret = NULL; |
| 526 |
|
| 527 |
g_mutex_lock (&global_config_lock); |
| 528 |
ret = g_strdup (global_config_str ? global_config_str : ""); |
| 529 |
g_mutex_unlock (&global_config_lock); |
| 530 |
|
| 531 |
return ret; |
| 532 |
} |
| 533 |
|
| 534 |
/** |
| 535 |
* bd_lvm_set_devices_filter: |
| 536 |
* @devices: (nullable) (array zero-terminated=1): list of devices for lvm commands to work on |
| 537 |
* @error: (out) (optional): place to store error (if any) |
| 538 |
* |
| 539 |
* Returns: whether the devices filter was successfully set or not |
| 540 |
* |
| 541 |
* Tech category: %BD_LVM_TECH_DEVICES no mode (it is ignored) |
| 542 |
*/ |
| 543 |
gboolean bd_lvm_set_devices_filter (const gchar **devices, GError **error) { |
| 544 |
if (!bd_lvm_is_tech_avail (BD_LVM_TECH_DEVICES, 0, error)) |
| 545 |
return FALSE; |
| 546 |
|
| 547 |
g_mutex_lock (&global_config_lock); |
| 548 |
|
| 549 |
/* first free the old value */ |
| 550 |
g_free (global_devices_str); |
| 551 |
|
| 552 |
/* now store the new one */ |
| 553 |
if (!devices || !(*devices)) |
| 554 |
global_devices_str = NULL; |
| 555 |
else |
| 556 |
global_devices_str = g_strjoinv (",", (gchar **) devices); |
| 557 |
|
| 558 |
g_mutex_unlock (&global_config_lock); |
| 559 |
return TRUE; |
| 560 |
} |
| 561 |
|
| 562 |
/** |
| 563 |
* bd_lvm_get_devices_filter: |
| 564 |
* @error: (out) (optional): place to store error (if any) |
| 565 |
* |
| 566 |
* Returns: (transfer full) (array zero-terminated=1): a copy of a string representation of |
| 567 |
* the currently set LVM devices filter |
| 568 |
* |
| 569 |
* Tech category: %BD_LVM_TECH_DEVICES no mode (it is ignored) |
| 570 |
*/ |
| 571 |
gchar** bd_lvm_get_devices_filter (GError **error G_GNUC_UNUSED) { |
| 572 |
gchar **ret = NULL; |
| 573 |
|
| 574 |
g_mutex_lock (&global_config_lock); |
| 575 |
|
| 576 |
if (global_devices_str) |
| 577 |
ret = g_strsplit (global_devices_str, ",", -1); |
| 578 |
else |
| 579 |
ret = NULL; |
| 580 |
|
| 581 |
g_mutex_unlock (&global_config_lock); |
| 582 |
|
| 583 |
return ret; |
| 584 |
} |
| 585 |
|
| 586 |
/** |
| 587 |
* bd_lvm_cache_get_mode_str: |
| 588 |
* @mode: mode to get the string representation for |
| 589 |
* @error: (out) (optional): place to store error (if any) |
| 590 |
* |
| 591 |
* Returns: string representation of @mode or %NULL in case of error |
| 592 |
* |
| 593 |
* Tech category: always provided/supported |
| 594 |
*/ |
| 595 |
const gchar* bd_lvm_cache_get_mode_str (BDLVMCacheMode mode, GError **error) { |
| 596 |
if (mode == BD_LVM_CACHE_MODE_WRITETHROUGH) |
| 597 |
return "writethrough"; |
| 598 |
else if (mode == BD_LVM_CACHE_MODE_WRITEBACK) |
| 599 |
return "writeback"; |
| 600 |
else if (mode == BD_LVM_CACHE_MODE_UNKNOWN) |
| 601 |
return "unknown"; |
| 602 |
else { |
| 603 |
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_CACHE_INVAL, |
| 604 |
"Invalid mode given: %d", mode); |
| 605 |
return NULL; |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
/** |
| 610 |
* bd_lvm_cache_get_mode_from_str: |
| 611 |
* @mode_str: string representation of a cache mode |
| 612 |
* @error: (out) (optional): place to store error (if any) |
| 613 |
* |
| 614 |
* Returns: cache mode for the @mode_str or %BD_LVM_CACHE_MODE_UNKNOWN if |
| 615 |
* failed to determine |
| 616 |
* |
| 617 |
* Tech category: always provided/supported |
| 618 |
*/ |
| 619 |
BDLVMCacheMode bd_lvm_cache_get_mode_from_str (const gchar *mode_str, GError **error) { |
| 620 |
if (g_strcmp0 (mode_str, "writethrough") == 0) |
| 621 |
return BD_LVM_CACHE_MODE_WRITETHROUGH; |
| 622 |
else if (g_strcmp0 (mode_str, "writeback") == 0) |
| 623 |
return BD_LVM_CACHE_MODE_WRITEBACK; |
| 624 |
else if (g_strcmp0 (mode_str, "unknown") == 0) |
| 625 |
return BD_LVM_CACHE_MODE_UNKNOWN; |
| 626 |
else { |
| 627 |
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_CACHE_INVAL, |
| 628 |
"Invalid mode given: %s", mode_str); |
| 629 |
return BD_LVM_CACHE_MODE_UNKNOWN; |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
/** |
| 634 |
* bd_lvm_get_vdo_operating_mode_str: |
| 635 |
* @mode: mode to get the string representation for |
| 636 |
* @error: (out) (optional): place to store error (if any) |
| 637 |
* |
| 638 |
* Returns: string representation of @mode or %NULL in case of error |
| 639 |
* |
| 640 |
* Tech category: always provided/supported |
| 641 |
*/ |
| 642 |
const gchar* bd_lvm_get_vdo_operating_mode_str (BDLVMVDOOperatingMode mode, GError **error) { |
| 643 |
switch (mode) { |
| 644 |
case BD_LVM_VDO_MODE_RECOVERING: |
| 645 |
return "recovering"; |
| 646 |
case BD_LVM_VDO_MODE_READ_ONLY: |
| 647 |
return "read-only"; |
| 648 |
case BD_LVM_VDO_MODE_NORMAL: |
| 649 |
return "normal"; |
| 650 |
case BD_LVM_VDO_MODE_UNKNOWN: |
| 651 |
return "unknown"; |
| 652 |
default: |
| 653 |
g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_FAIL, |
| 654 |
"Invalid LVM VDO operating mode."); |
| 655 |
return NULL; |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
/** |
| 660 |
* bd_lvm_get_vdo_compression_state_str: |
| 661 |
* @state: state to get the string representation for |
| 662 |
* @error: (out) (optional): place to store error (if any) |
| 663 |
* |
| 664 |
* Returns: string representation of @state or %NULL in case of error |
| 665 |
* |
| 666 |
* Tech category: always provided/supported |
| 667 |
*/ |
| 668 |
const gchar* bd_lvm_get_vdo_compression_state_str (BDLVMVDOCompressionState state, GError **error) { |
| 669 |
switch (state) { |
| 670 |
case BD_LVM_VDO_COMPRESSION_ONLINE: |
| 671 |
return "online"; |
| 672 |
case BD_LVM_VDO_COMPRESSION_OFFLINE: |
| 673 |
return "offline"; |
| 674 |
case BD_LVM_VDO_COMPRESSION_UNKNOWN: |
| 675 |
return "unknown"; |
| 676 |
default: |
| 677 |
g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_FAIL, |
| 678 |
"Invalid LVM VDO compression state."); |
| 679 |
return NULL; |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
/** |
| 684 |
* bd_lvm_get_vdo_index_state_str: |
| 685 |
* @state: state to get the string representation for |
| 686 |
* @error: (out) (optional): place to store error (if any) |
| 687 |
* |
| 688 |
* Returns: string representation of @state or %NULL in case of error |
| 689 |
* |
| 690 |
* Tech category: always provided/supported |
| 691 |
*/ |
| 692 |
const gchar* bd_lvm_get_vdo_index_state_str (BDLVMVDOIndexState state, GError **error) { |
| 693 |
switch (state) { |
| 694 |
case BD_LVM_VDO_INDEX_ERROR: |
| 695 |
return "error"; |
| 696 |
case BD_LVM_VDO_INDEX_CLOSED: |
| 697 |
return "closed"; |
| 698 |
case BD_LVM_VDO_INDEX_OPENING: |
| 699 |
return "opening"; |
| 700 |
case BD_LVM_VDO_INDEX_CLOSING: |
| 701 |
return "closing"; |
| 702 |
case BD_LVM_VDO_INDEX_OFFLINE: |
| 703 |
return "offline"; |
| 704 |
case BD_LVM_VDO_INDEX_ONLINE: |
| 705 |
return "online"; |
| 706 |
case BD_LVM_VDO_INDEX_UNKNOWN: |
| 707 |
return "unknown"; |
| 708 |
default: |
| 709 |
g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_FAIL, |
| 710 |
"Invalid LVM VDO index state."); |
| 711 |
return NULL; |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
/** |
| 716 |
* bd_lvm_get_vdo_write_policy_str: |
| 717 |
* @policy: policy to get the string representation for |
| 718 |
* @error: (out) (optional): place to store error (if any) |
| 719 |
* |
| 720 |
* Returns: string representation of @policy or %NULL in case of error |
| 721 |
* |
| 722 |
* Tech category: always provided/supported |
| 723 |
*/ |
| 724 |
const gchar* bd_lvm_get_vdo_write_policy_str (BDLVMVDOWritePolicy policy, GError **error) { |
| 725 |
switch (policy) { |
| 726 |
case BD_LVM_VDO_WRITE_POLICY_AUTO: |
| 727 |
return "auto"; |
| 728 |
case BD_LVM_VDO_WRITE_POLICY_SYNC: |
| 729 |
return "sync"; |
| 730 |
case BD_LVM_VDO_WRITE_POLICY_ASYNC: |
| 731 |
return "async"; |
| 732 |
case BD_LVM_VDO_WRITE_POLICY_UNKNOWN: |
| 733 |
return "unknown"; |
| 734 |
default: |
| 735 |
g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_FAIL, |
| 736 |
"Invalid LVM VDO write policy."); |
| 737 |
return NULL; |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
/** |
| 742 |
* bd_lvm_get_vdo_write_policy_from_str: |
| 743 |
* @policy_str: string representation of a policy |
| 744 |
* @error: (out) (optional): place to store error (if any) |
| 745 |
* |
| 746 |
* Returns: write policy for the @policy_str or %BD_LVM_VDO_WRITE_POLICY_UNKNOWN if |
| 747 |
* failed to determine |
| 748 |
* |
| 749 |
* Tech category: always provided/supported |
| 750 |
*/ |
| 751 |
BDLVMVDOWritePolicy bd_lvm_get_vdo_write_policy_from_str (const gchar *policy_str, GError **error) { |
| 752 |
if (g_strcmp0 (policy_str, "auto") == 0) |
| 753 |
return BD_LVM_VDO_WRITE_POLICY_AUTO; |
| 754 |
else if (g_strcmp0 (policy_str, "sync") == 0) |
| 755 |
return BD_LVM_VDO_WRITE_POLICY_SYNC; |
| 756 |
else if (g_strcmp0 (policy_str, "async") == 0) |
| 757 |
return BD_LVM_VDO_WRITE_POLICY_ASYNC; |
| 758 |
else { |
| 759 |
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_VDO_POLICY_INVAL, |
| 760 |
"Invalid policy given: %s", policy_str); |
| 761 |
return BD_LVM_VDO_WRITE_POLICY_UNKNOWN; |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
/** |
| 766 |
* bd_lvm_vdo_get_stats_full: |
| 767 |
* @vg_name: name of the VG that contains @pool_name VDO pool |
| 768 |
* @pool_name: name of the VDO pool to get statistics for |
| 769 |
* @error: (out) (optional): place to store error (if any) |
| 770 |
* |
| 771 |
* Returns: (transfer full) (element-type utf8 utf8): hashtable of type string - string of available |
| 772 |
* statistics or %NULL in case of error |
| 773 |
* (@error gets populated in those cases) |
| 774 |
* |
| 775 |
* Statistics are collected from the values exposed by the kernel `dm-vdo` module. |
| 776 |
* |
| 777 |
* Some of the keys are computed to mimic the information produced by the vdo tools. |
| 778 |
* Please note the contents of the hashtable may vary depending on the actual dm-vdo module version. |
| 779 |
* |
| 780 |
* Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_QUERY |
| 781 |
*/ |
| 782 |
GHashTable* bd_lvm_vdo_get_stats_full (const gchar *vg_name, const gchar *pool_name, GError **error) { |
| 783 |
g_autofree gchar *kvdo_name = g_strdup_printf ("%s-%s-%s", vg_name, pool_name, VDO_POOL_SUFFIX); |
| 784 |
return vdo_get_stats_full (kvdo_name, error); |
| 785 |
} |
| 786 |
|
| 787 |
/** |
| 788 |
* bd_lvm_vdo_get_stats: |
| 789 |
* @vg_name: name of the VG that contains @pool_name VDO pool |
| 790 |
* @pool_name: name of the VDO pool to get statistics for |
| 791 |
* @error: (out) (optional): place to store error (if any) |
| 792 |
* |
| 793 |
* Returns: (transfer full): a structure containing selected statistics or %NULL in case of error |
| 794 |
* (@error gets populated in those cases) |
| 795 |
* |
| 796 |
* In contrast to @bd_lvm_vdo_get_stats_full this function will only return selected statistics |
| 797 |
* in a fixed structure. In case a value is not available, -1 would be returned. |
| 798 |
* |
| 799 |
* Tech category: %BD_LVM_TECH_VDO-%BD_LVM_TECH_MODE_QUERY |
| 800 |
*/ |
| 801 |
BDLVMVDOStats* bd_lvm_vdo_get_stats (const gchar *vg_name, const gchar *pool_name, GError **error) { |
| 802 |
GHashTable *full_stats = NULL; |
| 803 |
BDLVMVDOStats *stats = NULL; |
| 804 |
|
| 805 |
full_stats = bd_lvm_vdo_get_stats_full (vg_name, pool_name, error); |
| 806 |
if (!full_stats) |
| 807 |
return NULL; |
| 808 |
|
| 809 |
stats = g_new0 (BDLVMVDOStats, 1); |
| 810 |
get_stat_val64_default (full_stats, "blockSize", &stats->block_size, -1); |
| 811 |
get_stat_val64_default (full_stats, "logicalBlockSize", &stats->logical_block_size, -1); |
| 812 |
get_stat_val64_default (full_stats, "physicalBlocks", &stats->physical_blocks, -1); |
| 813 |
get_stat_val64_default (full_stats, "dataBlocksUsed", &stats->data_blocks_used, -1); |
| 814 |
get_stat_val64_default (full_stats, "overheadBlocksUsed", &stats->overhead_blocks_used, -1); |
| 815 |
get_stat_val64_default (full_stats, "logicalBlocksUsed", &stats->logical_blocks_used, -1); |
| 816 |
get_stat_val64_default (full_stats, "usedPercent", &stats->used_percent, -1); |
| 817 |
get_stat_val64_default (full_stats, "savingPercent", &stats->saving_percent, -1); |
| 818 |
if (!get_stat_val_double (full_stats, "writeAmplificationRatio", &stats->write_amplification_ratio)) |
| 819 |
stats->write_amplification_ratio = -1; |
| 820 |
|
| 821 |
g_hash_table_destroy (full_stats); |
| 822 |
|
| 823 |
return stats; |
| 824 |
} |
| 825 |
|
| 826 |
/* check whether the LVM devices file is enabled by LVM |
| 827 |
* we use the existence of the "lvmdevices" command to check whether the feature is available |
| 828 |
* or not, but this can still be disabled either in LVM or in lvm.conf |
| 829 |
*/ |
| 830 |
static gboolean _lvm_devices_enabled () { |
| 831 |
const gchar *args[6] = {"lvmconfig", "--typeconfig", NULL, "devices/use_devicesfile", NULL, NULL}; |
| 832 |
gboolean ret = FALSE; |
| 833 |
GError *loc_error = NULL; |
| 834 |
gchar *output = NULL; |
| 835 |
guint enabled = 0; |
| 836 |
gint scanned = 0; |
| 837 |
g_autofree gchar *config_arg = NULL; |
| 838 |
|
| 839 |
/* try full config first -- if we get something from this it means the feature is |
| 840 |
explicitly enabled or disabled by system lvm.conf or using the --config option */ |
| 841 |
args[2] = "full"; |
| 842 |
|
| 843 |
/* make sure to include the global config from us when getting the current config value */ |
| 844 |
g_mutex_lock (&global_config_lock); |
| 845 |
if (global_config_str) { |
| 846 |
config_arg = g_strdup_printf ("--config=%s", global_config_str); |
| 847 |
args[4] = config_arg; |
| 848 |
} |
| 849 |
|
| 850 |
ret = bd_utils_exec_and_capture_output (args, NULL, &output, &loc_error); |
| 851 |
g_mutex_unlock (&global_config_lock); |
| 852 |
if (ret) { |
| 853 |
scanned = sscanf (output, "use_devicesfile=%u", &enabled); |
| 854 |
g_free (output); |
| 855 |
if (scanned != 1) |
| 856 |
return FALSE; |
| 857 |
|
| 858 |
return enabled != 0; |
| 859 |
} else { |
| 860 |
g_clear_error (&loc_error); |
| 861 |
g_free (output); |
| 862 |
} |
| 863 |
|
| 864 |
output = NULL; |
| 865 |
|
| 866 |
/* now try default */ |
| 867 |
args[2] = "default"; |
| 868 |
ret = bd_utils_exec_and_capture_output (args, NULL, &output, &loc_error); |
| 869 |
if (ret) { |
| 870 |
scanned = sscanf (output, "# use_devicesfile=%u", &enabled); |
| 871 |
g_free (output); |
| 872 |
if (scanned != 1) |
| 873 |
return FALSE; |
| 874 |
|
| 875 |
return enabled != 0; |
| 876 |
} else { |
| 877 |
g_clear_error (&loc_error); |
| 878 |
g_free (output); |
| 879 |
} |
| 880 |
|
| 881 |
return FALSE; |
| 882 |
} |
| 883 |
|
| 884 |
/** |
| 885 |
* bd_lvm_devices_add: |
| 886 |
* @device: device (PV) to add to the devices file |
| 887 |
* @devices_file: (nullable): LVM devices file or %NULL for default |
| 888 |
* @extra: (nullable) (array zero-terminated=1): extra options for the lvmdevices command |
| 889 |
* @error: (out) (optional): place to store error (if any) |
| 890 |
* |
| 891 |
* Returns: whether the @device was successfully added to @devices_file or not |
| 892 |
* |
| 893 |
* Tech category: %BD_LVM_TECH_DEVICES no mode (it is ignored) |
| 894 |
*/ |
| 895 |
gboolean bd_lvm_devices_add (const gchar *device, const gchar *devices_file, const BDExtraArg **extra, GError **error) { |
| 896 |
const gchar *args[5] = {"lvmdevices", "--adddev", device, NULL, NULL}; |
| 897 |
g_autofree gchar *devfile = NULL; |
| 898 |
|
| 899 |
if (!bd_lvm_is_tech_avail (BD_LVM_TECH_DEVICES, 0, error)) |
| 900 |
return FALSE; |
| 901 |
|
| 902 |
if (!_lvm_devices_enabled ()) { |
| 903 |
g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_DEVICES_DISABLED, |
| 904 |
"LVM devices file not enabled."); |
| 905 |
return FALSE; |
| 906 |
} |
| 907 |
|
| 908 |
if (devices_file) { |
| 909 |
devfile = g_strdup_printf ("--devicesfile=%s", devices_file); |
| 910 |
args[3] = devfile; |
| 911 |
} |
| 912 |
|
| 913 |
return bd_utils_exec_and_report_error (args, extra, error); |
| 914 |
} |
| 915 |
|
| 916 |
/** |
| 917 |
* bd_lvm_devices_delete: |
| 918 |
* @device: device (PV) to delete from the devices file |
| 919 |
* @devices_file: (nullable): LVM devices file or %NULL for default |
| 920 |
* @extra: (nullable) (array zero-terminated=1): extra options for the lvmdevices command |
| 921 |
* @error: (out) (optional): place to store error (if any) |
| 922 |
* |
| 923 |
* Returns: whether the @device was successfully removed from @devices_file or not |
| 924 |
* |
| 925 |
* Tech category: %BD_LVM_TECH_DEVICES no mode (it is ignored) |
| 926 |
*/ |
| 927 |
gboolean bd_lvm_devices_delete (const gchar *device, const gchar *devices_file, const BDExtraArg **extra, GError **error) { |
| 928 |
const gchar *args[5] = {"lvmdevices", "--deldev", device, NULL, NULL}; |
| 929 |
g_autofree gchar *devfile = NULL; |
| 930 |
|
| 931 |
if (!bd_lvm_is_tech_avail (BD_LVM_TECH_DEVICES, 0, error)) |
| 932 |
return FALSE; |
| 933 |
|
| 934 |
if (!_lvm_devices_enabled ()) { |
| 935 |
g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_DEVICES_DISABLED, |
| 936 |
"LVM devices file not enabled."); |
| 937 |
return FALSE; |
| 938 |
} |
| 939 |
|
| 940 |
if (devices_file) { |
| 941 |
devfile = g_strdup_printf ("--devicesfile=%s", devices_file); |
| 942 |
args[3] = devfile; |
| 943 |
} |
| 944 |
|
| 945 |
return bd_utils_exec_and_report_error (args, extra, error); |
| 946 |
} |
| 947 |
|
| 948 |
/** |
| 949 |
* bd_lvm_config_get: |
| 950 |
* @section: (nullable): LVM config section, e.g. 'global' or %NULL to print the entire config |
| 951 |
* @setting: (nullable): name of the specific setting, e.g. 'umask' or %NULL to print the entire @section |
| 952 |
* @type: type of the config, e.g. 'full' or 'current' |
| 953 |
* @values_only: whether to include only values without keys in the output |
| 954 |
* @global_config: whether to include our internal global config in the call or not |
| 955 |
* @extra: (nullable) (array zero-terminated=1): extra options for the lvmconfig command |
| 956 |
* (just passed to LVM as is) |
| 957 |
* @error: (out) (optional): place to store error (if any) |
| 958 |
* |
| 959 |
* Returns: (transfer full): Requested LVM config @section and @setting configuration or %NULL in case of error. |
| 960 |
* |
| 961 |
* Tech category: %BD_LVM_TECH_CONFIG no mode (it is ignored) |
| 962 |
*/ |
| 963 |
gchar* bd_lvm_config_get (const gchar *section, const gchar *setting, const gchar *type, gboolean values_only, gboolean global_config, const BDExtraArg **extra, GError **error) { |
| 964 |
g_autofree gchar *conf_spec = NULL; |
| 965 |
g_autofree gchar *config_arg = NULL; |
| 966 |
const gchar *args[7] = {"lvmconfig", "--typeconfig", NULL, NULL, NULL, NULL, NULL}; |
| 967 |
guint next_arg = 2; |
| 968 |
gchar *output = NULL; |
| 969 |
gboolean success = FALSE; |
| 970 |
|
| 971 |
if (!section && setting) { |
| 972 |
g_set_error_literal (error, BD_LVM_ERROR, BD_LVM_ERROR_FAIL, |
| 973 |
"Specifying setting without section is not supported."); |
| 974 |
return NULL; |
| 975 |
} |
| 976 |
|
| 977 |
if (section) |
| 978 |
if (setting) |
| 979 |
conf_spec = g_strdup_printf ("%s/%s", section, setting); |
| 980 |
else |
| 981 |
conf_spec = g_strdup (section); |
| 982 |
else |
| 983 |
conf_spec = NULL; |
| 984 |
|
| 985 |
args[next_arg++] = type; |
| 986 |
args[next_arg++] = conf_spec; |
| 987 |
if (values_only) |
| 988 |
args[next_arg++] = "--valuesonly"; |
| 989 |
|
| 990 |
g_mutex_lock (&global_config_lock); |
| 991 |
if (global_config && global_config_str) { |
| 992 |
config_arg = g_strdup_printf ("--config=%s", global_config_str); |
| 993 |
args[next_arg++] = config_arg; |
| 994 |
} |
| 995 |
g_mutex_unlock (&global_config_lock); |
| 996 |
|
| 997 |
success = bd_utils_exec_and_capture_output (args, extra, &output, error); |
| 998 |
if (!success) |
| 999 |
return NULL; |
| 1000 |
return g_strchomp (output); |
| 1001 |
} |
| 1002 |
|
| 1003 |
gboolean _vgcfgbackup_restore (const gchar *command, const gchar *vg_name, const gchar *file, const BDExtraArg **extra, GError **error) { |
| 1004 |
const gchar *args[7] = {"lvm", NULL, NULL, NULL, NULL, NULL, NULL}; |
| 1005 |
guint next_arg = 1; |
| 1006 |
g_autofree gchar *config_arg = NULL; |
| 1007 |
|
| 1008 |
args[next_arg++] = command; |
| 1009 |
if (file) { |
| 1010 |
args[next_arg++] = "-f"; |
| 1011 |
args[next_arg++] = file; |
| 1012 |
} |
| 1013 |
args[next_arg++] = vg_name; |
| 1014 |
|
| 1015 |
g_mutex_lock (&global_config_lock); |
| 1016 |
if (global_config_str) { |
| 1017 |
config_arg = g_strdup_printf ("--config=%s", global_config_str); |
| 1018 |
args[next_arg++] = config_arg; |
| 1019 |
} |
| 1020 |
g_mutex_unlock (&global_config_lock); |
| 1021 |
|
| 1022 |
return bd_utils_exec_and_report_error (args, extra, error); |
| 1023 |
} |
| 1024 |
|
| 1025 |
/** |
| 1026 |
* bd_lvm_vgcfgbackup: |
| 1027 |
* @vg_name: name of the VG to backup configuration |
| 1028 |
* @backup_file: (nullable): file to save the backup to or %NULL for using the default backup file |
| 1029 |
* in /etc/lvm/backup |
| 1030 |
* @extra: (nullable) (array zero-terminated=1): extra options for the vgcfgbackup command |
| 1031 |
* (just passed to LVM as is) |
| 1032 |
* @error: (out) (optional): place to store error (if any) |
| 1033 |
* |
| 1034 |
* Note: This function does not back up the data content of LVs. See `vgcfbackup(8)` man page |
| 1035 |
* for more information. |
| 1036 |
* |
| 1037 |
* Returns: Whether the backup was successfully created or not. |
| 1038 |
* |
| 1039 |
* Tech category: %BD_LVM_TECH_VG_CFG_BACKUP_RESTORE no mode (it is ignored) |
| 1040 |
*/ |
| 1041 |
gboolean bd_lvm_vgcfgbackup (const gchar *vg_name, const gchar *backup_file, const BDExtraArg **extra, GError **error) { |
| 1042 |
return _vgcfgbackup_restore ("vgcfgbackup", vg_name, backup_file, extra, error); |
| 1043 |
} |
| 1044 |
|
| 1045 |
/** |
| 1046 |
* bd_lvm_vgcfgrestore: |
| 1047 |
* @vg_name: name of the VG to restore configuration |
| 1048 |
* @backup_file: (nullable): file to restore VG configuration from to or %NULL for using the |
| 1049 |
* latest backup in /etc/lvm/backup |
| 1050 |
* @extra: (nullable) (array zero-terminated=1): extra options for the vgcfgrestore command |
| 1051 |
* (just passed to LVM as is) |
| 1052 |
* @error: (out) (optional): place to store error (if any) |
| 1053 |
* |
| 1054 |
* Note: This function restores VG configuration created by %bd_lvm_vgcfgbackup from given |
| 1055 |
* @backup_file or from the latest backup in /etc/lvm/backup. |
| 1056 |
* |
| 1057 |
* Returns: Whether the configuration was successfully restored or not. |
| 1058 |
* |
| 1059 |
* Tech category: %BD_LVM_TECH_VG_CFG_BACKUP_RESTORE no mode (it is ignored) |
| 1060 |
*/ |
| 1061 |
gboolean bd_lvm_vgcfgrestore (const gchar *vg_name, const gchar *backup_file, const BDExtraArg **extra, GError **error) { |
| 1062 |
return _vgcfgbackup_restore ("vgcfgrestore", vg_name, backup_file, extra, error); |
| 1063 |
} |
| 1064 |
|
| 1065 |
/** |
| 1066 |
* bd_lvm_cache_stats: |
| 1067 |
* @vg_name: name of the VG containing the @cached_lv |
| 1068 |
* @cached_lv: cached LV to get stats for |
| 1069 |
* @error: (out) (optional): place to store error (if any) |
| 1070 |
* |
| 1071 |
* Returns: stats for the @cached_lv or %NULL in case of error |
| 1072 |
* |
| 1073 |
* Tech category: %BD_LVM_TECH_CACHE-%BD_LVM_TECH_MODE_QUERY |
| 1074 |
*/ |
| 1075 |
BDLVMCacheStats* bd_lvm_cache_stats (const gchar *vg_name, const gchar *cached_lv, GError **error) { |
| 1076 |
struct dm_pool *pool = NULL; |
| 1077 |
struct dm_task *task = NULL; |
| 1078 |
struct dm_info info; |
| 1079 |
struct dm_status_cache *status = NULL; |
| 1080 |
gchar *map_name = NULL; |
| 1081 |
guint64 start = 0; |
| 1082 |
guint64 length = 0; |
| 1083 |
gchar *type = NULL; |
| 1084 |
gchar *params = NULL; |
| 1085 |
BDLVMCacheStats *ret = NULL; |
| 1086 |
BDLVMLVdata *lvdata = NULL; |
| 1087 |
|
| 1088 |
lvdata = bd_lvm_lvinfo (vg_name, cached_lv, error); |
| 1089 |
if (!lvdata) |
| 1090 |
return NULL; |
| 1091 |
|
| 1092 |
pool = dm_pool_create ("bd-pool", 20); |
| 1093 |
|
| 1094 |
if (g_strcmp0 (lvdata->segtype, "thin-pool") == 0) |
| 1095 |
map_name = dm_build_dm_name (pool, vg_name, lvdata->data_lv, NULL); |
| 1096 |
else |
| 1097 |
/* translate the VG+LV name into the DM map name */ |
| 1098 |
map_name = dm_build_dm_name (pool, vg_name, cached_lv, NULL); |
| 1099 |
|
| 1100 |
bd_lvm_lvdata_free (lvdata); |
| 1101 |
|
| 1102 |
task = dm_task_create (DM_DEVICE_STATUS); |
| 1103 |
if (!task) { |
| 1104 |
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_DM_ERROR, |
| 1105 |
"Failed to create DM task for the cache map '%s': ", map_name); |
| 1106 |
dm_pool_destroy (pool); |
| 1107 |
return NULL; |
| 1108 |
} |
| 1109 |
|
| 1110 |
if (dm_task_set_name (task, map_name) == 0) { |
| 1111 |
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_DM_ERROR, |
| 1112 |
"Failed to create DM task for the cache map '%s': ", map_name); |
| 1113 |
dm_task_destroy (task); |
| 1114 |
dm_pool_destroy (pool); |
| 1115 |
return NULL; |
| 1116 |
} |
| 1117 |
|
| 1118 |
if (dm_task_run (task) == 0) { |
| 1119 |
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_DM_ERROR, |
| 1120 |
"Failed to run the DM task for the cache map '%s': ", map_name); |
| 1121 |
dm_task_destroy (task); |
| 1122 |
dm_pool_destroy (pool); |
| 1123 |
return NULL; |
| 1124 |
} |
| 1125 |
|
| 1126 |
if (dm_task_get_info (task, &info) == 0) { |
| 1127 |
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_DM_ERROR, |
| 1128 |
"Failed to get task info for the cache map '%s': ", map_name); |
| 1129 |
dm_task_destroy (task); |
| 1130 |
dm_pool_destroy (pool); |
| 1131 |
return NULL; |
| 1132 |
} |
| 1133 |
|
| 1134 |
if (!info.exists) { |
| 1135 |
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_CACHE_NOCACHE, |
| 1136 |
"The cache map '%s' doesn't exist: ", map_name); |
| 1137 |
dm_task_destroy (task); |
| 1138 |
dm_pool_destroy (pool); |
| 1139 |
return NULL; |
| 1140 |
} |
| 1141 |
|
| 1142 |
dm_get_next_target (task, NULL, &start, &length, &type, ¶ms); |
| 1143 |
|
| 1144 |
if (dm_get_status_cache (pool, params, &status) == 0) { |
| 1145 |
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_CACHE_INVAL, |
| 1146 |
"Failed to get status of the cache map '%s': ", map_name); |
| 1147 |
dm_task_destroy (task); |
| 1148 |
dm_pool_destroy (pool); |
| 1149 |
return NULL; |
| 1150 |
} |
| 1151 |
|
| 1152 |
ret = g_new0 (BDLVMCacheStats, 1); |
| 1153 |
ret->block_size = status->block_size * SECTOR_SIZE; |
| 1154 |
ret->cache_size = status->total_blocks * ret->block_size; |
| 1155 |
ret->cache_used = status->used_blocks * ret->block_size; |
| 1156 |
|
| 1157 |
ret->md_block_size = status->metadata_block_size * SECTOR_SIZE; |
| 1158 |
ret->md_size = status->metadata_total_blocks * ret->md_block_size; |
| 1159 |
ret->md_used = status->metadata_used_blocks * ret->md_block_size; |
| 1160 |
|
| 1161 |
ret->read_hits = status->read_hits; |
| 1162 |
ret->read_misses = status->read_misses; |
| 1163 |
ret->write_hits = status->write_hits; |
| 1164 |
ret->write_misses = status->write_misses; |
| 1165 |
|
| 1166 |
if (status->feature_flags & DM_CACHE_FEATURE_WRITETHROUGH) |
| 1167 |
ret->mode = BD_LVM_CACHE_MODE_WRITETHROUGH; |
| 1168 |
else if (status->feature_flags & DM_CACHE_FEATURE_WRITEBACK) |
| 1169 |
ret->mode = BD_LVM_CACHE_MODE_WRITEBACK; |
| 1170 |
else { |
| 1171 |
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_CACHE_INVAL, |
| 1172 |
"Failed to determine status of the cache from '%"G_GUINT64_FORMAT"': ", |
| 1173 |
status->feature_flags); |
| 1174 |
dm_task_destroy (task); |
| 1175 |
dm_pool_destroy (pool); |
| 1176 |
bd_lvm_cache_stats_free (ret); |
| 1177 |
return NULL; |
| 1178 |
} |
| 1179 |
|
| 1180 |
dm_task_destroy (task); |
| 1181 |
dm_pool_destroy (pool); |
| 1182 |
|
| 1183 |
return ret; |
| 1184 |
} |