Debian Codegraph @

src/plugins/mdraid.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 #define _XOPEN_SOURCE /* needed for time.h */
21
22 #include <glib.h>
23 #include <unistd.h>
24 #include <blockdev/utils.h>
25 #include <string.h>
26 #include <glob.h>
27 #include <time.h>
28 #include <bs_size.h>
29
30 #include "mdraid.h"
31 #include "check_deps.h"
32
33 #define MDADM_MIN_VERSION "3.3.2"
34
35 /**
36 * SECTION: mdraid
37 * @short_description: plugin for basic operations with MD RAID
38 * @title: MD RAID
39 * @include: mdraid.h
40 *
41 * A plugin for basic operations with MD RAID. Also sizes are in
42 * bytes unless specified otherwise.
43 */
44
45 /**
46 * bd_md_error_quark: (skip)
47 */
48 GQuark bd_md_error_quark (void)
49 {
50 return g_quark_from_static_string ("g-bd-md-error-quark");
51 }
52
53 /**
54 * bd_md_examine_data_copy: (skip)
55 * @data: (nullable): %BDMDExamineData to copy
56 *
57 * Creates a new copy of @data.
58 */
59 BDMDExamineData* bd_md_examine_data_copy (BDMDExamineData *data) {
60 if (data == NULL)
61 return NULL;
62
63 BDMDExamineData *new_data = g_new0 (BDMDExamineData, 1);
64
65 new_data->device = g_strdup (data->device);
66 new_data->level = g_strdup (data->level);
67 new_data->num_devices = data->num_devices;
68 new_data->name = g_strdup (data->name);
69 new_data->size = data->size;
70 new_data->uuid = g_strdup (data->uuid);
71 new_data->update_time = data->update_time;
72 new_data->dev_uuid = g_strdup (data->dev_uuid);
73 new_data->events = data->events;
74 new_data->metadata = g_strdup (data->metadata);
75 new_data->chunk_size = data->chunk_size;
76 return new_data;
77 }
78
79 /**
80 * bd_md_examine_data_free: (skip)
81 * @data: (nullable): %BDMDExamineData to free
82 *
83 * Frees @data.
84 */
85 void bd_md_examine_data_free (BDMDExamineData *data) {
86 if (data == NULL)
87 return;
88
89 g_free (data->device);
90 g_free (data->level);
91 g_free (data->name);
92 g_free (data->uuid);
93 g_free (data->dev_uuid);
94 g_free (data->metadata);
95 g_free (data);
96 }
97
98 /**
99 * bd_md_detail_data_copy: (skip)
100 * @data: (nullable): %BDMDDetailData to copy
101 *
102 * Creates a new copy of @data.
103 */
104 BDMDDetailData* bd_md_detail_data_copy (BDMDDetailData *data) {
105 if (data == NULL)
106 return NULL;
107
108 BDMDDetailData *new_data = g_new0 (BDMDDetailData, 1);
109
110 new_data->device = g_strdup (data->device);
111 new_data->name = g_strdup (data->name);
112 new_data->metadata = g_strdup (data->metadata);
113 new_data->creation_time = g_strdup (data->creation_time);
114 new_data->level = g_strdup (data->level);
115 new_data->array_size = data->array_size;
116 new_data->use_dev_size = data->use_dev_size;
117 new_data->raid_devices = data->raid_devices;
118 new_data->active_devices = data->active_devices;
119 new_data->working_devices = data->working_devices;
120 new_data->failed_devices = data->failed_devices;
121 new_data->spare_devices = data->spare_devices;
122 new_data->clean = data->clean;
123 new_data->uuid = g_strdup (data->uuid);
124 new_data->container = g_strdup (data->container);
125
126 return new_data;
127 }
128
129 /**
130 * bd_md_detail_data_free: (skip)
131 * @data: (nullable): %BDMDDetailData to free
132 *
133 * Frees @data.
134 */
135 void bd_md_detail_data_free (BDMDDetailData *data) {
136 if (data == NULL)
137 return;
138
139 g_free (data->device);
140 g_free (data->name);
141 g_free (data->metadata);
142 g_free (data->creation_time);
143 g_free (data->level);
144 g_free (data->uuid);
145 g_free (data->container);
146
147 g_free (data);
148 }
149
150
151 static volatile guint avail_deps = 0;
152 static GMutex deps_check_lock;
153
154 #define DEPS_MDADM 0
155 #define DEPS_MDADM_MASK (1 << DEPS_MDADM)
156 #define DEPS_LAST 1
157
158 static const UtilDep deps[DEPS_LAST] = {
159 {"mdadm", MDADM_MIN_VERSION, NULL, "mdadm - v([\\d\\.]+)"},
160 };
161
162
163 /**
164 * bd_md_init:
165 *
166 * Initializes the plugin. **This function is called automatically by the
167 * library's initialization functions.**
168 *
169 */
170 gboolean bd_md_init (void) {
171 /* nothing to do here */
172 return TRUE;
173 };
174
175 /**
176 * bd_md_close:
177 *
178 * Cleans up after the plugin. **This function is called automatically by the
179 * library's functions that unload it.**
180 *
181 */
182 void bd_md_close (void) {
183 g_atomic_int_set (&avail_deps, 0);
184 }
185
186
187 /**
188 * bd_md_is_tech_avail:
189 * @tech: the queried tech
190 * @mode: a bit mask of queried modes of operation for @tech
191 * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
192 *
193 * Returns: whether the @tech-@mode combination is available -- supported by the
194 * plugin implementation and having all the runtime dependencies available
195 */
196 gboolean bd_md_is_tech_avail (BDMDTech tech G_GNUC_UNUSED, guint64 mode G_GNUC_UNUSED, GError **error) {
197 /* all tech-mode combinations are supported by this implementation of the
198 plugin, but it requires the 'mdadm' utility */
199 return check_deps (&avail_deps, DEPS_MDADM_MASK, deps, DEPS_LAST, &deps_check_lock, error);
200 }
201
202 /**
203 * parse_mdadm_vars: (skip)
204 * @str: string to parse
205 * @item_sep: item separator(s) (key-value pairs separator)
206 * @key_val_sep: key-value separator(s) (typically ":" or "=")
207 * @num_items: (out): number of parsed items (key-value pairs)
208 *
209 * Returns: (transfer full): GHashTable containing the key-value pairs parsed
210 * from the @str.
211 */
212 static GHashTable* parse_mdadm_vars (const gchar *str, const gchar *item_sep, const gchar *key_val_sep, guint *num_items) {
213 GHashTable *table = NULL;
214 gchar **items = NULL;
215 gchar **item_p = NULL;
216 gchar **key_val = NULL;
217 gchar **vals = NULL;
218
219 table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
220 *num_items = 0;
221
222 items = g_strsplit_set (str, item_sep, 0);
223 for (item_p=items; *item_p; item_p++) {
224 key_val = g_strsplit (*item_p, key_val_sep, 2);
225 if (g_strv_length ((gchar **) key_val) == 2) {
226 /* we only want to process valid lines (with the separator) */
227 /* only use the first value for the given key */
228 if (!g_hash_table_contains (table, g_strstrip (key_val[0]))) {
229 if (strstr (key_val[1], "<--")) {
230 /* mdadm --examine output for a set being migrated */
231 vals = g_strsplit (key_val[1], "<--", 2);
232 g_hash_table_insert (table, g_strstrip (key_val[0]), g_strstrip (vals[0]));
233 g_free (key_val[1]);
234 g_free (vals[1]);
235 g_free (vals);
236 } else {
237 g_hash_table_insert (table, g_strstrip (key_val[0]), g_strstrip (key_val[1]));
238 }
239 g_free (key_val);
240 } else {
241 g_strfreev (key_val);
242 }
243 (*num_items)++;
244 } else
245 /* invalid line, just free key_val */
246 g_strfreev (key_val);
247 }
248
249 g_strfreev (items);
250 return table;
251 }
252
253 static BDMDExamineData* get_examine_data_from_table (GHashTable *table, gboolean free_table) {
254 BDMDExamineData *data = g_new0 (BDMDExamineData, 1);
255 gchar *value = NULL;
256 gchar *first_space = NULL;
257 BSSize size = NULL;
258 BSError *bs_error = NULL;
259 struct tm tm;
260 char time_str[20];
261 gchar *name_str = NULL;
262
263 data->level = g_strdup ((gchar*) g_hash_table_lookup (table, "Raid Level"));
264 if (!(data->level))
265 /* BUG: mdadm outputs "RAID Level" for some metadata formats (rhbz#1380034) */
266 data->level = g_strdup ((gchar*) g_hash_table_lookup (table, "RAID Level"));
267
268 value = (gchar*) g_hash_table_lookup (table, "Raid Devices");
269 if (!value)
270 /* BUG: mdadm outputs "RAID Devices" for some metadata formats (rhbz#1380034) */
271 value = (gchar*) g_hash_table_lookup (table, "RAID Devices");
272 if (value)
273 data->num_devices = g_ascii_strtoull (value, NULL, 0);
274 else
275 data->num_devices = 0;
276
277 name_str = ((gchar*) g_hash_table_lookup (table, "Name"));
278 if (name_str) {
279 g_strstrip (name_str);
280 first_space = strchr (name_str, ' ');
281 if (first_space)
282 *first_space = '\0';
283 data->name = g_strdup (name_str);
284 }
285
286 value = (gchar*) g_hash_table_lookup (table, "Array Size");
287 if (value) {
288 first_space = strchr (value, ' ');
289 if (first_space)
290 *first_space = '\0';
291 if (value && first_space)
292 /* Array Size is in KiB */
293 data->size = g_ascii_strtoull (value, NULL, 0) * 1024;
294 } else
295 data->size = 0;
296
297 data->uuid = g_strdup ((gchar*) g_hash_table_lookup (table, "Array UUID"));
298 if (!data->uuid)
299 /* also try just "UUID" which may be reported e.g for IMSM FW RAID */
300 data->uuid = g_strdup ((gchar*) g_hash_table_lookup (table, "UUID"));
301
302 value = (gchar*) g_hash_table_lookup (table, "Update Time");
303 if (value) {
304 memset (&tm, 0, sizeof (struct tm));
305 strptime (value, "%a %b %e %H:%M:%S %Y", &tm);
306 strftime (time_str, sizeof (time_str), "%s" , &tm);
307
308 data->update_time = g_ascii_strtoull (time_str, NULL, 0);
309 } else
310 data->update_time = 0;
311
312 data->dev_uuid = g_strdup ((gchar*) g_hash_table_lookup (table, "Device UUID"));
313
314 value = (gchar*) g_hash_table_lookup (table, "Events");
315 if (value)
316 data->events = g_ascii_strtoull (value, NULL, 0);
317 else
318 data->events = 0;
319
320 value = (gchar*) g_hash_table_lookup (table, "Version");
321 if (value)
322 data->metadata = g_strdup (value);
323 else
324 data->metadata = NULL;
325
326 value = (gchar*) g_hash_table_lookup (table, "Chunk Size");
327 if (value) {
328 size = bs_size_new_from_str (value, &bs_error);
329 if (size) {
330 data->chunk_size = bs_size_get_bytes (size, NULL, &bs_error);
331 bs_size_free (size);
332 }
333
334 if (bs_error) {
335 bd_utils_log_format (BD_UTILS_LOG_WARNING, "get_examine_data_from_table(): Failed to parse chunk size "
336 "from mdexamine data: %s", bs_error->msg);
337 bs_clear_error (&bs_error);
338 }
339 } else
340 data->chunk_size = 0;
341
342 if (free_table)
343 g_hash_table_destroy (table);
344
345 return data;
346 }
347
348 static BDMDDetailData* get_detail_data_from_table (GHashTable *table, gboolean free_table) {
349 BDMDDetailData *data = g_new0 (BDMDDetailData, 1);
350 gchar *value = NULL;
351 gchar *name_str = NULL;
352 gchar *first_space = NULL;
353
354 data->metadata = g_strdup ((gchar*) g_hash_table_lookup (table, "Version"));
355 data->creation_time = g_strdup ((gchar*) g_hash_table_lookup (table, "Creation Time"));
356 data->level = g_strdup ((gchar*) g_hash_table_lookup (table, "Raid Level"));
357 data->uuid = g_strdup ((gchar*) g_hash_table_lookup (table, "UUID"));
358
359 name_str = ((gchar*) g_hash_table_lookup (table, "Name"));
360 if (name_str) {
361 g_strstrip (name_str);
362 first_space = strchr (name_str, ' ');
363 if (first_space)
364 *first_space = '\0';
365 data->name = g_strdup (name_str);
366 }
367
368 value = (gchar*) g_hash_table_lookup (table, "Array Size");
369 if (value) {
370 first_space = strchr (value, ' ');
371 if (first_space)
372 *first_space = '\0';
373 if (value && first_space)
374 data->array_size = g_ascii_strtoull (value, NULL, 0);
375 }
376 else
377 data->array_size = 0;
378
379 value = (gchar*) g_hash_table_lookup (table, "Used Dev Size");
380 if (value) {
381 first_space = strchr (value, ' ');
382 if (first_space)
383 *first_space = '\0';
384 if (value && first_space)
385 data->use_dev_size = g_ascii_strtoull (value, NULL, 0);
386 }
387 else
388 data->use_dev_size = 0;
389
390 value = (gchar*) g_hash_table_lookup (table, "Raid Devices");
391 if (value)
392 data->raid_devices = g_ascii_strtoull (value, NULL, 0);
393 else
394 data->raid_devices = 0;
395
396 value = (gchar*) g_hash_table_lookup (table, "Total Devices");
397 if (value)
398 data->total_devices = g_ascii_strtoull (value, NULL, 0);
399 else
400 data->total_devices = 0;
401
402 value = (gchar*) g_hash_table_lookup (table, "Active Devices");
403 if (value)
404 data->active_devices = g_ascii_strtoull (value, NULL, 0);
405 else
406 data->active_devices = 0;
407
408 value = (gchar*) g_hash_table_lookup (table, "Working Devices");
409 if (value)
410 data->working_devices = g_ascii_strtoull (value, NULL, 0);
411 else
412 data->working_devices = 0;
413
414 value = (gchar*) g_hash_table_lookup (table, "Failed Devices");
415 if (value)
416 data->failed_devices = g_ascii_strtoull (value, NULL, 0);
417 else
418 data->failed_devices = 0;
419
420 value = (gchar*) g_hash_table_lookup (table, "Spare Devices");
421 if (value)
422 data->spare_devices = g_ascii_strtoull (value, NULL, 0);
423 else
424 data->spare_devices = 0;
425
426 value = (gchar*) g_hash_table_lookup (table, "State");
427 if (value)
428 data->clean = (g_strcmp0 (value, "clean") == 0);
429 else
430 data->clean = FALSE;
431
432 if (free_table)
433 g_hash_table_destroy (table);
434
435 return data;
436 }
437
438 /**
439 * get_sysfs_name_from_input: (skip)
440 * @input: either RAID name or node name
441 * @error: (out) (optional): place to store error (if any)
442 *
443 * Returns: (transfer full): RAID node name
444 */
445 static gchar* get_sysfs_name_from_input (const gchar *input, GError **error) {
446 gchar* sysfs_name = NULL;
447 gchar* path = NULL;
448
449 /* get rid of the "/dev/" or "/dev/md/" prefix (if any) */
450 if (g_str_has_prefix (input, "/dev/md/"))
451 input = input + 8;
452 else if (g_str_has_prefix (input, "/dev/"))
453 input = input + 5;
454
455 path = g_strdup_printf ("/sys/class/block/%s/md", input);
456 if (access (path, F_OK) == 0)
457 sysfs_name = g_strdup (input);
458 else
459 sysfs_name = bd_md_node_from_name (input, error);
460
461 g_free (path);
462
463 return sysfs_name;
464 }
465
466 /**
467 * get_mdadm_spec_from_input: (skip)
468 * @input: RAID specification from user
469 * @error: (out) (optional): place to store error (if any)
470 *
471 * Returns: (transfer full): RAID specification for mdadm
472 *
473 * Takes some RAID specification (raid name, node name, path or name symlink)
474 * and returns a new specification suitable for mdadm command.
475 */
476 static gchar* get_mdadm_spec_from_input (const gchar *input, GError **error) {
477 gchar* md_path_str = NULL;
478 gchar* name_path_str = NULL;
479 gchar* mdadm_spec = NULL;
480
481 if (g_str_has_prefix (input, "/dev/")) {
482 if (access (input, F_OK) == 0)
483 mdadm_spec = g_strdup (input);
484 else {
485 g_set_error (error, BD_MD_ERROR, BD_MD_ERROR_INVAL,
486 "Device %s doesn't exist.", input);
487 mdadm_spec = NULL;
488 }
489 } else {
490 md_path_str = g_strdup_printf ("/dev/%s", input);
491 name_path_str = g_strdup_printf ("/dev/md/%s", input);
492 if (access (name_path_str, F_OK) == 0)
493 mdadm_spec = g_strdup (name_path_str);
494 else if (access (md_path_str, F_OK) == 0)
495 mdadm_spec = g_strdup (md_path_str);
496 else
497 mdadm_spec = g_strdup (input);
498 }
499
500 g_free (md_path_str);
501 g_free (name_path_str);
502
503 return mdadm_spec;
504 }
505
506 /**
507 * bd_md_get_superblock_size:
508 * @member_size: size of an array member
509 * @version: (nullable): metadata version or %NULL to use the current default version
510 * @error: (out) (optional): place to store error (if any)
511 *
512 * Returns: Calculated superblock size for an array with a given @member_size
513 * and metadata @version or default if unsupported @version is used.
514 *
515 * Tech category: always available
516 */
517 guint64 bd_md_get_superblock_size (guint64 member_size, const gchar *version, GError **error G_GNUC_UNUSED) {
518 guint64 headroom = BD_MD_SUPERBLOCK_SIZE;
519 guint64 min_headroom = (1 MiB);
520
521 /* mdadm 3.2.4 made a major change in the amount of space used for 1.1 and
522 * 1.2 in order to reserve space for reshaping. See commit 508a7f16 in the
523 * upstream mdadm repository. */
524 if (!version || (g_strcmp0 (version, "1.1") == 0) ||
525 (g_strcmp0 (version, "1.2") == 0) || (g_strcmp0 (version, "default") == 0)) {
526 /* MDADM: We try to leave 0.1% at the start for reshape
527 * MDADM: operations, but limit this to 128Meg (0.1% of 10Gig)
528 * MDADM: which is plenty for efficient reshapes
529 * NOTE: In the mdadm code this is in 512b sectors. Converted to use MiB */
530 headroom = (128 MiB);
531 while (((headroom << 10) > member_size) && (headroom > min_headroom))
532 headroom >>= 1;
533 }
534
535 return headroom;
536 }
537
538 /**
539 * bd_md_create:
540 * @device_name: name of the device to create
541 * @level: RAID level (as understood by mdadm, see mdadm(8))
542 * @disks: (array zero-terminated=1): disks to use for the new RAID (including spares)
543 * @spares: number of spare devices
544 * @version: (nullable): metadata version
545 * @bitmap: (nullable): write-intent bitmap location ('none', 'internal') or %NULL to let mdadm decide (i.e. internal > 100GB)
546 * @chunk_size: chunk size of the device to create
547 * @extra: (nullable) (array zero-terminated=1): extra options for the creation (right now
548 * passed to the 'mdadm' utility)
549 * @error: (out) (optional): place to store error (if any)
550 *
551 * Returns: whether the new MD RAID device @device_name was successfully created or not
552 *
553 * Tech category: %BD_MD_TECH_MDRAID-%BD_MD_TECH_MODE_CREATE
554 */
555 gboolean bd_md_create (const gchar *device_name, const gchar *level, const gchar **disks, guint64 spares, const gchar *version, const gchar *bitmap, guint64 chunk_size, const BDExtraArg **extra, GError **error) {
556 const gchar **argv = NULL;
557 /* {"mdadm", "create", device, "--run", "level", "raid-devices",...} */
558 guint argv_len = 6;
559 guint argv_top = 0;
560 guint i = 0;
561 guint num_disks = 0;
562 gchar *level_str = NULL;
563 gchar *rdevices_str = NULL;
564 gchar *spares_str = NULL;
565 gchar *version_str = NULL;
566 gchar *chunk_str = NULL;
567 gchar *bitmap_str = NULL;
568 gboolean ret = FALSE;
569
570 if (!check_deps (&avail_deps, DEPS_MDADM_MASK, deps, DEPS_LAST, &deps_check_lock, error))
571 return FALSE;
572
573 if (spares != 0)
574 argv_len++;
575 if (version)
576 argv_len++;
577 if (bitmap)
578 argv_len++;
579 if (chunk_size != 0)
580 argv_len++;
581
582 num_disks = g_strv_length ((gchar **) disks);
583 argv_len += num_disks;
584
585 argv = g_new0 (const gchar*, argv_len + 1);
586
587 level_str = g_strdup_printf ("--level=%s", level);
588 rdevices_str = g_strdup_printf ("--raid-devices=%"G_GUINT64_FORMAT, (num_disks - spares));
589
590 argv[argv_top++] = "mdadm";
591 argv[argv_top++] = "--create";
592 argv[argv_top++] = device_name;
593 argv[argv_top++] = "--run";
594 argv[argv_top++] = level_str;
595 argv[argv_top++] = rdevices_str;
596
597 if (spares != 0) {
598 spares_str = g_strdup_printf ("--spare-devices=%"G_GUINT64_FORMAT, spares);
599 argv[argv_top++] = spares_str;
600 }
601 if (version) {
602 version_str = g_strdup_printf ("--metadata=%s", version);
603 argv[argv_top++] = version_str;
604 }
605 if (bitmap) {
606 bitmap_str = g_strdup_printf ("--bitmap=%s", bitmap);
607 argv[argv_top++] = bitmap_str;
608 }
609 if (chunk_size != 0) {
610 chunk_str = g_strdup_printf ("--chunk=%"G_GUINT64_FORMAT, chunk_size/1024);
611 argv[argv_top++] = chunk_str;
612 }
613
614 for (i=0; i < num_disks; i++)
615 argv[argv_top++] = disks[i];
616 argv[argv_top] = NULL;
617
618 ret = bd_utils_exec_and_report_error (argv, extra, error);
619
620 g_free (level_str);
621 g_free (rdevices_str);
622 g_free (spares_str);
623 g_free (version_str);
624 g_free (chunk_str);
625 g_free (bitmap_str);
626 g_free (argv);
627
628 return ret;
629 }
630
631 /**
632 * bd_md_destroy:
633 * @device: device to destroy MD RAID metadata on
634 * @error: (out) (optional): place to store error (if any)
635 *
636 * Returns: whether the MD RAID metadata was successfully destroyed on @device or not
637 *
638 * Tech category: %BD_MD_TECH_MDRAID-%BD_MD_TECH_MODE_DELETE
639 */
640 gboolean bd_md_destroy (const gchar *device, GError **error) {
641 const gchar *argv[] = {"mdadm", "--zero-superblock", device, NULL};
642
643 if (!check_deps (&avail_deps, DEPS_MDADM_MASK, deps, DEPS_LAST, &deps_check_lock, error))
644 return FALSE;
645
646 return bd_utils_exec_and_report_error (argv, NULL, error);
647 }
648
649 /**
650 * bd_md_deactivate:
651 * @raid_spec: specification of the RAID device (name, node or path)
652 * @error: (out) (optional): place to store error (if any)
653 *
654 * Returns: whether the RAID device @raid_spec was successfully deactivated or not
655 *
656 * Tech category: %BD_MD_TECH_MDRAID-%BD_MD_TECH_MODE_MODIFY
657 */
658 gboolean bd_md_deactivate (const gchar *raid_spec, GError **error) {
659 const gchar *argv[] = {"mdadm", "--stop", NULL, NULL};
660 gchar *mdadm_spec = NULL;
661 gboolean ret = FALSE;
662
663 if (!check_deps (&avail_deps, DEPS_MDADM_MASK, deps, DEPS_LAST, &deps_check_lock, error))
664 return FALSE;
665
666 mdadm_spec = get_mdadm_spec_from_input (raid_spec, error);
667 if (!mdadm_spec)
668 /* error is already populated */
669 return FALSE;
670
671 argv[2] = mdadm_spec;
672
673 ret = bd_utils_exec_and_report_error (argv, NULL, error);
674 g_free (mdadm_spec);
675
676 return ret;
677 }
678
679 /**
680 * bd_md_activate:
681 * @raid_spec: (nullable): specification of the RAID device (name, node or path) to activate (if not given "--scan" is implied and @members is ignored)
682 * @members: (nullable) (array zero-terminated=1): member devices to be considered for @device activation
683 * @uuid: (nullable): UUID (in the MD RAID format!) of the MD RAID to activate
684 * @start_degraded: whether to start the array even if it's degraded
685 * @extra: (nullable) (array zero-terminated=1): extra options for the activation (right now
686 * passed to the 'mdadm' utility)
687 * @error: (out) (optional): place to store error (if any)
688 *
689 * Returns: whether the MD RAID @device was successfully activated or not
690 *
691 * Note: either @members or @uuid (or both) have to be specified.
692 *
693 * Tech category: %BD_MD_TECH_MDRAID-%BD_MD_TECH_MODE_MODIFY
694 */
695 gboolean bd_md_activate (const gchar *raid_spec, const gchar **members, const gchar *uuid, gboolean start_degraded, const BDExtraArg **extra, GError **error) {
696 guint64 num_members = (raid_spec && members) ? g_strv_length ((gchar **) members) : 0;
697 const gchar **argv = NULL;
698 gchar *uuid_str = NULL;
699 guint argv_top = 0;
700 guint i = 0;
701 gboolean ret = FALSE;
702 BDMDDetailData *data = NULL;
703
704 if (!check_deps (&avail_deps, DEPS_MDADM_MASK, deps, DEPS_LAST, &deps_check_lock, error))
705 return FALSE;
706
707 if (raid_spec) {
708 data = bd_md_detail (raid_spec, NULL);
709 if (data) {
710 bd_utils_log_format (BD_UTILS_LOG_INFO,
711 "RAID array '%s' is already active with %"G_GUINT64_FORMAT" devices"
712 " (%"G_GUINT64_FORMAT" active, %"G_GUINT64_FORMAT" spare)",
713 raid_spec, data->total_devices,
714 data->active_devices, data->spare_devices);
715 bd_md_detail_data_free (data);
716 return TRUE;
717 }
718 }
719
720 /* mdadm, --assemble, raid_spec/--scan, --run, --uuid=uuid, member1, member2,..., NULL*/
721 argv = g_new0 (const gchar*, num_members + 6);
722
723 argv[argv_top++] = "mdadm";
724 argv[argv_top++] = "--assemble";
725 if (raid_spec)
726 argv[argv_top++] = raid_spec;
727 else
728 argv[argv_top++] = "--scan";
729 if (start_degraded)
730 argv[argv_top++] = "--run";
731 if (uuid) {
732 uuid_str = g_strdup_printf ("--uuid=%s", uuid);
733 argv[argv_top++] = uuid_str;
734 }
735 /* only add member device if device_name given (a combination of --scan with
736 a list of members doesn't work) */
737 if (raid_spec && members)
738 for (i=0; i < num_members; i++)
739 argv[argv_top++] = members[i];
740 argv[argv_top] = NULL;
741
742 ret = bd_utils_exec_and_report_error (argv, extra, error);
743
744 g_free (uuid_str);
745 g_free (argv);
746
747 return ret;
748 }
749
750 /**
751 * bd_md_run:
752 * @raid_spec: specification of the (possibly degraded) RAID device (name, node or path) to be started
753 * @error: (out) (optional): place to store error (if any)
754 *
755 * Returns: whether the @raid_spec was successfully started or not
756 *
757 * Tech category: %BD_MD_TECH_MDRAID-%BD_MD_TECH_MODE_MODIFY
758 */
759 gboolean bd_md_run (const gchar *raid_spec, GError **error) {
760 const gchar *argv[] = {"mdadm", "--run", NULL, NULL};
761 gchar *mdadm_spec = NULL;
762 gboolean ret = FALSE;
763
764 if (!check_deps (&avail_deps, DEPS_MDADM_MASK, deps, DEPS_LAST, &deps_check_lock, error))
765 return FALSE;
766
767 mdadm_spec = get_mdadm_spec_from_input (raid_spec, error);
768 if (!mdadm_spec)
769 /* error is already populated */
770 return FALSE;
771
772 argv[2] = mdadm_spec;
773
774 ret = bd_utils_exec_and_report_error (argv, NULL, error);
775 g_free (mdadm_spec);
776
777 return ret;
778 }
779
780 /**
781 * bd_md_nominate:
782 * @device: device to nominate (add to its appropriate RAID) as a MD RAID device
783 * @error: (out) (optional): place to store error (if any)
784 *
785 * Returns: whether the @device was successfully nominated (added to its
786 * appropriate RAID) or not
787 *
788 * Note: may start the MD RAID if it becomes ready by adding @device.
789 *
790 * Tech category: %BD_MD_TECH_MDRAID-%BD_MD_TECH_MODE_MODIFY
791 */
792 gboolean bd_md_nominate (const gchar *device, GError **error) {
793 const gchar *argv[] = {"mdadm", "--incremental", "--quiet", "--run", device, NULL};
794
795 if (!check_deps (&avail_deps, DEPS_MDADM_MASK, deps, DEPS_LAST, &deps_check_lock, error))
796 return FALSE;
797
798 return bd_utils_exec_and_report_error (argv, NULL, error);
799 }
800
801 /**
802 * bd_md_denominate:
803 * @device: device to denominate (remove from its appropriate RAID) as a MD RAID device
804 * @error: (out) (optional): place to store error (if any)
805 *
806 * Returns: whether the @device was successfully denominated (added to its
807 * appropriate RAID) or not
808 *
809 * Note: may start the MD RAID if it becomes ready by adding @device.
810 *
811 * Tech category: %BD_MD_TECH_MDRAID-%BD_MD_TECH_MODE_MODIFY
812 */
813 gboolean bd_md_denominate (const gchar *device, GError **error) {
814 const gchar *argv[] = {"mdadm", "--incremental", "--fail", device, NULL};
815
816 /* XXX: stupid mdadm! --incremental --fail requires "sda1" instead of "/dev/sda1" */
817 if (g_str_has_prefix (device, "/dev/"))
818 argv[3] = (device + 5);
819
820 return bd_utils_exec_and_report_error (argv, NULL, error);
821 }
822
823 /**
824 * bd_md_add:
825 * @raid_spec: specification of the RAID device (name, node or path) to add @device into
826 * @device: name of the device to add to the @raid_spec RAID device
827 * @raid_devs: number of devices the @raid_spec RAID should actively use or 0
828 * to leave unspecified (see below)
829 * @extra: (nullable) (array zero-terminated=1): extra options for the addition (right now
830 * passed to the 'mdadm' utility)
831 * @error: (out) (optional): place to store error (if any)
832 *
833 * Returns: whether the @device was successfully added to the @raid_spec RAID or
834 * not
835 *
836 * The @raid_devs parameter is used when adding devices to a raid array that has
837 * no actual redundancy. In this case it is necessary to explicitly grow the
838 * array all at once rather than manage it in the sense of adding spares.
839 *
840 * Whether the new device will be added as a spare or an active member is
841 * decided by mdadm.
842 *
843 * Tech category: %BD_MD_TECH_MDRAID-%BD_MD_TECH_MODE_MODIFY
844 */
845 gboolean bd_md_add (const gchar *raid_spec, const gchar *device, guint64 raid_devs, const BDExtraArg **extra, GError **error) {
846 const gchar *argv[7] = {"mdadm", NULL, NULL, NULL, NULL, NULL, NULL};
847 guint argv_top = 1;
848 gchar *mdadm_spec = NULL;
849 gchar *raid_devs_str = NULL;
850 gboolean ret = FALSE;
851
852 if (!check_deps (&avail_deps, DEPS_MDADM_MASK, deps, DEPS_LAST, &deps_check_lock, error))
853 return FALSE;
854
855 mdadm_spec = get_mdadm_spec_from_input (raid_spec, error);
856 if (!mdadm_spec)
857 /* error is already populated */
858 return FALSE;
859
860 if (raid_devs != 0) {
861 raid_devs_str = g_strdup_printf ("--raid-devices=%"G_GUINT64_FORMAT, raid_devs);
862 argv[argv_top++] = "--grow";
863 argv[argv_top++] = mdadm_spec;
864 argv[argv_top++] = raid_devs_str;
865 } else
866 argv[argv_top++] = mdadm_spec;
867
868 argv[argv_top++] = "--add";
869 argv[argv_top] = device;
870
871 ret = bd_utils_exec_and_report_error (argv, extra, error);
872 g_free (mdadm_spec);
873 g_free (raid_devs_str);
874
875 return ret;
876 }
877
878 /**
879 * bd_md_remove:
880 * @raid_spec: specification of the RAID device (name, node or path) to remove @device from
881 * @device: device to remove from the @raid_spec RAID
882 * @fail: whether to mark the @device as failed before removing
883 * @extra: (nullable) (array zero-terminated=1): extra options for the removal (right now
884 * passed to the 'mdadm' utility)
885 * @error: (out) (optional): place to store error (if any)
886 *
887 * Returns: whether the @device was successfully removed from the @raid_spec
888 * RAID or not.
889 *
890 * Tech category: %BD_MD_TECH_MDRAID-%BD_MD_TECH_MODE_MODIFY
891 */
892 gboolean bd_md_remove (const gchar *raid_spec, const gchar *device, gboolean fail, const BDExtraArg **extra, GError **error) {
893 const gchar *argv[] = {"mdadm", NULL, NULL, NULL, NULL, NULL, NULL};
894 guint argv_top = 2;
895 gchar *mdadm_spec = NULL;
896 gboolean ret = FALSE;
897 gchar *dev_path = NULL;
898
899 if (!check_deps (&avail_deps, DEPS_MDADM_MASK, deps, DEPS_LAST, &deps_check_lock, error))
900 return FALSE;
901
902 mdadm_spec = get_mdadm_spec_from_input (raid_spec, error);
903 if (!mdadm_spec)
904 /* error is already populated */
905 return FALSE;
906
907 argv[1] = mdadm_spec;
908
909 dev_path = bd_utils_resolve_device (device, error);
910 if (!dev_path) {
911 /* error is populated */
912 g_free (mdadm_spec);
913 return FALSE;
914 }
915
916 if (fail) {
917 argv[argv_top++] = "--fail";
918 argv[argv_top++] = dev_path;
919 }
920
921 argv[argv_top++] = "--remove";
922 argv[argv_top++] = dev_path;
923
924 ret = bd_utils_exec_and_report_error (argv, extra, error);
925 g_free (dev_path);
926 g_free (mdadm_spec);
927
928 return ret;
929 }
930
931 /**
932 * bd_md_examine:
933 * @device: name of the device (a member of an MD RAID) to examine
934 * @error: (out) (optional): place to store error (if any)
935 *
936 * Returns: information about the MD RAID extracted from the @device
937 *
938 * Tech category: %BD_MD_TECH_MDRAID-%BD_MD_TECH_MODE_QUERY
939 */
940 BDMDExamineData* bd_md_examine (const gchar *device, GError **error) {
941 const gchar *argv[] = {"mdadm", "--examine", "-E", device, NULL};
942 gchar *output = NULL;
943 gboolean success = FALSE;
944 GHashTable *table = NULL;
945 guint num_items = 0;
946 BDMDExamineData *ret = NULL;
947 gchar *value = NULL;
948 gchar **output_fields = NULL;
949 gchar *orig_data = NULL;
950 guint i = 0;
951 gboolean found_array_line = FALSE;
952
953 if (!check_deps (&avail_deps, DEPS_MDADM_MASK, deps, DEPS_LAST, &deps_check_lock, error))
954 return NULL;
955
956 success = bd_utils_exec_and_capture_output (argv, NULL, &output, error);
957 if (!success)
958 /* error is already populated */
959 return NULL;
960
961 table = parse_mdadm_vars (output, "\n", ":", &num_items);
962 g_free (output);
963 if (!table || (num_items == 0)) {
964 /* something bad happened */
965 g_set_error_literal (error, BD_MD_ERROR, BD_MD_ERROR_PARSE, "Failed to parse mdexamine data");
966 if (table)
967 g_hash_table_destroy (table);
968 return NULL;
969 }
970
971 ret = get_examine_data_from_table (table, TRUE);
972 if (!ret) {
973 g_set_error_literal (error, BD_MD_ERROR, BD_MD_ERROR_PARSE, "Failed to get mdexamine data");
974 if (table)
975 g_hash_table_destroy (table);
976 return NULL;
977 }
978
979 /* canonicalize UUIDs (as long as we got them) */
980 orig_data = ret->uuid;
981 if (orig_data) {
982 ret->uuid = bd_md_canonicalize_uuid (orig_data, error);
983 if (!ret->uuid) {
984 g_prefix_error (error, "Failed to canonicalize MD UUID '%s': ", orig_data);
985 g_free (orig_data);
986 bd_md_examine_data_free (ret);
987 return NULL;
988 }
989 g_free (orig_data);
990 }
991
992 orig_data = ret->dev_uuid;
993 if (orig_data) {
994 ret->dev_uuid = bd_md_canonicalize_uuid (orig_data, error);
995 if (!ret->dev_uuid) {
996 g_prefix_error (error, "Failed to canonicalize MD UUID '%s': ", orig_data);
997 g_free (orig_data);
998 bd_md_examine_data_free (ret);
999 return NULL;
1000 }
1001 g_free (orig_data);
1002 }
1003
1004 argv[2] = "--export";
1005 success = bd_utils_exec_and_capture_output (argv, NULL, &output, error);
1006 if (!success) {
1007 /* error is already populated */
1008 bd_md_examine_data_free (ret);
1009 return NULL;
1010 }
1011
1012 /* try to get a better information about RAID level because it may be
1013 misleading in the output without --export */
1014 output_fields = g_strsplit (output, "\n", 0);
1015 g_free (output);
1016 output = NULL;
1017 for (i=0; (i < g_strv_length (output_fields) - 1); i++)
1018 if (g_str_has_prefix (output_fields[i], "MD_LEVEL=")) {
1019 value = strchr (output_fields[i], '=');
1020 value++;
1021 g_free (ret->level);
1022 ret->level = g_strdup (value);
1023 } else if (!ret->uuid && g_str_has_prefix (output_fields[i], "MD_UUID=")) {
1024 value = strchr (output_fields[i], '=');
1025 value++;
1026 ret->uuid = bd_md_canonicalize_uuid (value, error);
1027 if (!ret->uuid) {
1028 g_prefix_error (error, "Failed to canonicalize MD UUID '%s': ", value);
1029 bd_md_examine_data_free (ret);
1030 g_strfreev (output_fields);
1031 return NULL;
1032 }
1033 }
1034 g_strfreev (output_fields);
1035
1036 argv[2] = "--brief";
1037 success = bd_utils_exec_and_capture_output (argv, NULL, &output, error);
1038 if (!success) {
1039 /* error is already populated */
1040 bd_md_examine_data_free (ret);
1041 return NULL;
1042 }
1043
1044 /* try to find the "ARRAY /dev/md/something" pair in the output */
1045 output_fields = g_strsplit_set (output, " \n", 0);
1046 for (i=0; !found_array_line && (i < g_strv_length (output_fields) - 1); i++)
1047 if (g_strcmp0 (output_fields[i], "ARRAY") == 0) {
1048 found_array_line = TRUE;
1049 if (g_str_has_prefix (output_fields[i+1], "/dev/md/")) {
1050 ret->device = g_strdup (output_fields[i+1]);
1051 } else {
1052 ret->device = NULL;
1053 }
1054 }
1055 if (!found_array_line)
1056 ret->device = NULL;
1057 g_strfreev (output_fields);
1058
1059 table = parse_mdadm_vars (output, " ", "=", &num_items);
1060 g_free (output);
1061 if (!table) {
1062 /* something bad happened or some expected items were missing */
1063 g_set_error_literal (error, BD_MD_ERROR, BD_MD_ERROR_PARSE,
1064 "Failed to parse mdexamine metadata");
1065 bd_md_examine_data_free (ret);
1066 return NULL;
1067 }
1068
1069 /* try to get metadata version from the output (may be missing) */
1070 g_free (ret->metadata);
1071 value = (gchar*) g_hash_table_lookup (table, "metadata");
1072 if (value)
1073 ret->metadata = g_strdup (value);
1074 else
1075 ret->metadata = NULL;
1076 g_hash_table_destroy (table);
1077
1078 return ret;
1079 }
1080
1081 /**
1082 * bd_md_detail:
1083 * @raid_spec: specification of the RAID device (name, node or path) to examine
1084 * @error: (out) (optional): place to store error (if any)
1085 *
1086 * Returns: information about the MD RAID @raid_spec
1087 *
1088 * Tech category: %BD_MD_TECH_MDRAID-%BD_MD_TECH_MODE_QUERY
1089 */
1090 BDMDDetailData* bd_md_detail (const gchar *raid_spec, GError **error) {
1091 const gchar *argv[] = {"mdadm", "--detail", NULL, NULL, NULL};
1092 gchar *output = NULL;
1093 gboolean success = FALSE;
1094 GHashTable *table = NULL;
1095 guint num_items = 0;
1096 gchar *orig_uuid = NULL;
1097 g_autofree gchar *mdadm_spec = NULL;
1098 gchar *value = NULL;
1099 gchar **output_fields = NULL;
1100 guint i = 0;
1101 BDMDDetailData *ret = NULL;
1102
1103 if (!check_deps (&avail_deps, DEPS_MDADM_MASK, deps, DEPS_LAST, &deps_check_lock, error))
1104 return NULL;
1105
1106 mdadm_spec = get_mdadm_spec_from_input (raid_spec, error);
1107 if (!mdadm_spec)
1108 /* error is already populated */
1109 return NULL;
1110
1111 argv[2] = mdadm_spec;
1112
1113 success = bd_utils_exec_and_capture_output (argv, NULL, &output, error);
1114 if (!success)
1115 /* error is already populated */
1116 return NULL;
1117
1118 table = parse_mdadm_vars (output, "\n", ":", &num_items);
1119 g_free (output);
1120 if (!table || (num_items == 0)) {
1121 /* something bad happened or some expected items were missing */
1122 g_set_error_literal (error, BD_MD_ERROR, BD_MD_ERROR_PARSE, "Failed to parse mddetail data");
1123 if (table)
1124 g_hash_table_destroy (table);
1125 return NULL;
1126 }
1127
1128 ret = get_detail_data_from_table (table, TRUE);
1129 if (!ret) {
1130 g_set_error_literal (error, BD_MD_ERROR, BD_MD_ERROR_PARSE, "Failed to get mddetail data");
1131 return NULL;
1132 }
1133
1134 ret->device = g_strdup (argv[2]);
1135
1136 orig_uuid = ret->uuid;
1137 if (orig_uuid) {
1138 ret->uuid = bd_md_canonicalize_uuid (orig_uuid, error);
1139 if (!ret->uuid) {
1140 g_prefix_error (error, "Failed to canonicalize MD UUID '%s': ", orig_uuid);
1141 g_free (orig_uuid);
1142 bd_md_detail_data_free (ret);
1143 return NULL;
1144 }
1145 g_free (orig_uuid);
1146 }
1147
1148 argv[2] = "--export";
1149 argv[3] = mdadm_spec;
1150 success = bd_utils_exec_and_capture_output (argv, NULL, &output, error);
1151 if (!success) {
1152 /* error is already populated */
1153 bd_md_detail_data_free (ret);
1154 return NULL;
1155 }
1156
1157 /* try to get a better information about RAID level because it may be
1158 missing in the output without --export */
1159 output_fields = g_strsplit (output, "\n", 0);
1160 g_free (output);
1161 output = NULL;
1162 for (i = 0; (i < g_strv_length (output_fields) - 1); i++) {
1163 if (!ret->uuid && g_str_has_prefix (output_fields[i], "MD_UUID=")) {
1164 value = strchr (output_fields[i], '=');
1165 value++;
1166 ret->uuid = bd_md_canonicalize_uuid (value, error);
1167 if (!ret->uuid) {
1168 g_prefix_error (error, "Failed to canonicalize MD UUID '%s': ", value);
1169 bd_md_detail_data_free (ret);
1170 g_strfreev (output_fields);
1171 return NULL;
1172 }
1173 } else if (g_str_has_prefix (output_fields[i], "MD_CONTAINER=")) {
1174 value = strchr (output_fields[i], '=');
1175 value++;
1176 ret->container = g_strdup (value);
1177 }
1178 }
1179 g_strfreev (output_fields);
1180
1181 return ret;
1182 }
1183
1184 /**
1185 * bd_md_canonicalize_uuid:
1186 * @uuid: UUID to canonicalize
1187 * @error: (out) (optional): place to store error (if any)
1188 *
1189 * Returns: (transfer full): canonicalized form of @uuid or %NULL in case of error
1190 *
1191 * This function expects a UUID in the form that mdadm returns. The change is as
1192 * follows: 3386ff85:f5012621:4a435f06:1eb47236 -> 3386ff85-f501-2621-4a43-5f061eb47236
1193 *
1194 * Tech category: always available
1195 */
1196 gchar* bd_md_canonicalize_uuid (const gchar *uuid, GError **error) {
1197 const gchar *next_set = uuid;
1198 gchar *ret = g_new0 (gchar, 37);
1199 gchar *dest = ret;
1200 GRegex *regex = NULL;
1201
1202 regex = g_regex_new ("[0-9a-f]{8}:[0-9a-f]{8}:[0-9a-f]{8}:[0-9a-f]{8}", 0, 0, error);
1203 if (!regex) {
1204 /* error is already populated */
1205 g_free (ret);
1206 return NULL;
1207 }
1208
1209 if (!g_regex_match (regex, uuid, 0, NULL)) {
1210 g_set_error (error, BD_MD_ERROR, BD_MD_ERROR_BAD_FORMAT,
1211 "malformed or invalid UUID: %s", uuid);
1212 g_regex_unref (regex);
1213 g_free (ret);
1214 return NULL;
1215 }
1216 g_regex_unref (regex);
1217
1218 /* first 8 symbols */
1219 memcpy (dest, next_set, 8);
1220 next_set += 9;
1221 dest += 8;
1222 *dest = '-';
1223 dest++;
1224
1225 /* 4 symbols from the second 8 */
1226 memcpy (dest, next_set, 4);
1227 next_set += 4;
1228 dest += 4;
1229 *dest = '-';
1230 dest++;
1231
1232 /* 4 symbols from the second 8 */
1233 memcpy (dest, next_set, 4);
1234 next_set += 5;
1235 dest += 4;
1236 *dest = '-';
1237 dest++;
1238
1239 /* 4 symbols from the third 8 */
1240 memcpy (dest, next_set, 4);
1241 next_set += 4;
1242 dest += 4;
1243 *dest = '-';
1244 dest++;
1245
1246 /* 4 symbols from the third 8 with no trailing - */
1247 memcpy (dest, next_set, 4);
1248 next_set += 5;
1249 dest += 4;
1250
1251 /* 9 symbols (8 + \0) from the fourth 8 */
1252 memcpy (dest, next_set, 9);
1253
1254 return ret;
1255 }
1256
1257 /**
1258 * bd_md_get_md_uuid:
1259 * @uuid: UUID to transform into format used by MD RAID
1260 * @error: (out) (optional): place to store error (if any)
1261 *
1262 * Returns: (transfer full): transformed form of @uuid or %NULL in case of error
1263 *
1264 * This function expects a UUID in the canonical (traditional format) and
1265 * returns a UUID in the format used by MD RAID and is thus reverse to
1266 * bd_md_canonicalize_uuid(). The change is as follows:
1267 * 3386ff85-f501-2621-4a43-5f061eb47236 -> 3386ff85:f5012621:4a435f06:1eb47236
1268 *
1269 * Tech category: always available
1270 */
1271 gchar* bd_md_get_md_uuid (const gchar *uuid, GError **error) {
1272 const gchar *next_set = uuid;
1273 gchar *ret = g_new0 (gchar, 37);
1274 gchar *dest = ret;
1275 GRegex *regex = NULL;
1276
1277 regex = g_regex_new ("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", 0, 0, error);
1278 if (!regex) {
1279 /* error is already populated */
1280 g_free (ret);
1281 return NULL;
1282 }
1283
1284 if (!g_regex_match (regex, uuid, 0, NULL)) {
1285 g_set_error (error, BD_MD_ERROR, BD_MD_ERROR_BAD_FORMAT,
1286 "malformed or invalid UUID: %s", uuid);
1287 g_regex_unref (regex);
1288 g_free (ret);
1289 return NULL;
1290 }
1291 g_regex_unref (regex);
1292
1293 /* first 8 symbols */
1294 memcpy (dest, next_set, 8);
1295 next_set += 9;
1296 dest += 8;
1297 *dest = ':';
1298 dest++;
1299
1300 /* 4 symbols from the 4 */
1301 memcpy (dest, next_set, 4);
1302 next_set += 5;
1303 dest += 4;
1304
1305 /* 4 symbols from the second 4 plus :*/
1306 memcpy (dest, next_set, 4);
1307 next_set += 5;
1308 dest += 4;
1309 *dest = ':';
1310 dest++;
1311
1312 /* 4 symbols from the third 4 */
1313 memcpy (dest, next_set, 4);
1314 next_set += 5;
1315 dest += 4;
1316
1317 /* 4 symbols from the 12 */
1318 memcpy (dest, next_set, 4);
1319 next_set += 4;
1320 dest += 4;
1321 *dest = ':';
1322 dest++;
1323
1324 /* 9 symbols (8 + \0) from the 12 */
1325 memcpy (dest, next_set, 9);
1326
1327 return ret;
1328 }
1329
1330 /**
1331 * bd_md_node_from_name:
1332 * @name: name of the MD RAID
1333 * @error: (out) (optional): place to store error (if any)
1334 *
1335 * Returns: device node of the @name MD RAID or %NULL in case of error
1336 *
1337 * Tech category: always available
1338 */
1339 gchar* bd_md_node_from_name (const gchar *name, GError **error) {
1340 gchar *dev_path = NULL;
1341 gchar *ret = NULL;
1342 gchar *md_path = g_strdup_printf ("/dev/md/%s", name);
1343
1344 dev_path = bd_utils_resolve_device (md_path, error);
1345 g_free (md_path);
1346 if (!dev_path)
1347 /* error is already populated */
1348 return NULL;
1349
1350 ret = g_path_get_basename (dev_path);
1351 g_free (dev_path);
1352
1353 return ret;
1354 }
1355
1356 /**
1357 * bd_md_name_from_node:
1358 * @node: path of the MD RAID's device node
1359 * @error: (out) (optional): place to store error (if any)
1360 *
1361 * Returns: @name of the MD RAID the device node belongs to or %NULL in case of error
1362 *
1363 * Tech category: always available
1364 */
1365 gchar* bd_md_name_from_node (const gchar *node, GError **error) {
1366 glob_t glob_buf;
1367 gchar **path_p;
1368 gboolean found = FALSE;
1369 gchar *dev_path = NULL;
1370 gchar *name = NULL;
1371 gchar *node_name = NULL;
1372
1373 /* get rid of the "/dev/" prefix (if any) */
1374 if (g_str_has_prefix (node, "/dev/"))
1375 node = node + 5;
1376
1377 if (glob ("/dev/md/*", GLOB_NOSORT, NULL, &glob_buf) != 0) {
1378 g_set_error (error, BD_MD_ERROR, BD_MD_ERROR_NO_MATCH,
1379 "No name found for the node '%s'", node);
1380 return NULL;
1381 }
1382 for (path_p = glob_buf.gl_pathv; *path_p && !found; path_p++) {
1383 dev_path = bd_utils_resolve_device (*path_p, NULL);
1384 if (!dev_path)
1385 continue;
1386 node_name = g_path_get_basename (dev_path);
1387 g_free (dev_path);
1388 if (g_strcmp0 (node_name, node) == 0) {
1389 found = TRUE;
1390 name = g_path_get_basename (*path_p);
1391 }
1392 g_free (node_name);
1393 }
1394 globfree (&glob_buf);
1395
1396 if (!found)
1397 g_set_error (error, BD_MD_ERROR, BD_MD_ERROR_NO_MATCH,
1398 "No name found for the node '%s'", node);
1399 return name;
1400 }
1401
1402 /**
1403 * bd_md_get_status:
1404 * @raid_spec: specification of the RAID device (name, node or path) to get status
1405 * @error: (out) (optional): place to store error (if any)
1406 *
1407 * Returns: (transfer full): status of the @raid_spec RAID.
1408 *
1409 * Tech category: %BD_MD_TECH_MDRAID-%BD_MD_TECH_MODE_QUERY
1410 */
1411 gchar* bd_md_get_status (const gchar *raid_spec, GError **error) {
1412 gboolean success = FALSE;
1413 gchar *ret = NULL;
1414 gchar *raid_node = NULL;
1415 gchar *sys_path = NULL;
1416
1417 raid_node = get_sysfs_name_from_input (raid_spec, error);
1418 if (!raid_node)
1419 /* error is already populated */
1420 return NULL;
1421
1422 sys_path = g_strdup_printf ("/sys/class/block/%s/md/array_state", raid_node);
1423 g_free (raid_node);
1424
1425 success = g_file_get_contents (sys_path, &ret, NULL, error);
1426 if (!success) {
1427 /* error is already populated */
1428 g_free (sys_path);
1429 return NULL;
1430 }
1431
1432 g_free (sys_path);
1433
1434 return g_strstrip (ret);
1435 }
1436
1437 /**
1438 * bd_md_set_bitmap_location:
1439 * @raid_spec: specification of the RAID device (name, node or path) to set the bitmap location
1440 * @location: bitmap location (none, internal or path)
1441 * @error: (out) (optional): place to store error (if any)
1442 *
1443 * Returns: whether @location was successfully set for @raid_spec
1444 *
1445 * Tech category: %BD_MD_TECH_MDRAID-%BD_MD_TECH_MODE_MODIFY
1446 */
1447 gboolean bd_md_set_bitmap_location (const gchar *raid_spec, const gchar *location, GError **error) {
1448 const gchar *argv[] = {"mdadm", "--grow", NULL, "--bitmap", location, NULL};
1449 gchar* mdadm_spec = NULL;
1450 gboolean ret = FALSE;
1451
1452 if (!check_deps (&avail_deps, DEPS_MDADM_MASK, deps, DEPS_LAST, &deps_check_lock, error))
1453 return FALSE;
1454
1455 mdadm_spec = get_mdadm_spec_from_input (raid_spec, error);
1456 if (!mdadm_spec)
1457 /* error is already populated */
1458 return FALSE;
1459
1460 argv[2] = mdadm_spec;
1461
1462 if ((g_strcmp0 (location, "none") != 0) && (g_strcmp0 (location, "internal") != 0) &&
1463 !g_str_has_prefix (location , "/")) {
1464
1465 g_set_error_literal (error, BD_MD_ERROR, BD_MD_ERROR_INVAL,
1466 "Bitmap location must start with '/' or be 'internal' or 'none'.");
1467 g_free (mdadm_spec);
1468 return FALSE;
1469 }
1470
1471 ret = bd_utils_exec_and_report_error (argv, NULL, error);
1472
1473 g_free (mdadm_spec);
1474
1475 return ret;
1476 }
1477
1478 /**
1479 * bd_md_get_bitmap_location:
1480 * @raid_spec: specification of the RAID device (name, node or path) to get the bitmap location
1481 * @error: (out) (optional): place to store error (if any)
1482 *
1483 * Returns: (transfer full): bitmap location for @raid_spec
1484 *
1485 * Tech category: %BD_MD_TECH_MDRAID-%BD_MD_TECH_MODE_QUERY
1486 */
1487 gchar* bd_md_get_bitmap_location (const gchar *raid_spec, GError **error) {
1488 g_autofree gchar *raid_node = NULL;
1489 g_autofree gchar *sys_path = NULL;
1490 gchar *ret = NULL;
1491 gboolean success = FALSE;
1492 GError *l_error = NULL;
1493
1494 raid_node = get_sysfs_name_from_input (raid_spec, error);
1495 if (!raid_node)
1496 /* error is already populated */
1497 return NULL;
1498
1499 sys_path = g_strdup_printf ("/sys/class/block/%s/md/bitmap/location", raid_node);
1500
1501 success = g_file_get_contents (sys_path, &ret, NULL, &l_error);
1502 if (!success) {
1503 if (g_error_matches (l_error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) {
1504 g_clear_error (&l_error);
1505 return g_strdup ("none");
1506 } else {
1507 g_propagate_prefixed_error (error, l_error, "Failed to get bitmap location: ");
1508 return NULL;
1509 }
1510 }
1511
1512 return g_strstrip (ret);
1513 }
1514
1515 /**
1516 * bd_md_request_sync_action:
1517 * @raid_spec: specification of the RAID device (name, node or path) to request sync action on
1518 * @action: requested sync action (resync, recovery, check, repair or idle)
1519 * @error: (out) (optional): place to store error (if any)
1520 *
1521 * Returns: whether the @action was successfully requested for the @raid_spec
1522 * RAID or not.
1523 *
1524 * Tech category: %BD_MD_TECH_MDRAID-%BD_MD_TECH_MODE_MODIFY
1525 */
1526 gboolean bd_md_request_sync_action (const gchar *raid_spec, const gchar *action, GError **error) {
1527 gchar *sys_path = NULL;
1528 gchar *raid_node = NULL;
1529 gboolean success = FALSE;
1530
1531 if ((g_strcmp0 (action, "resync") != 0) && (g_strcmp0 (action, "recovery") != 0) &&
1532 (g_strcmp0 (action, "check") != 0) && (g_strcmp0 (action, "repair") != 0) &&
1533 (g_strcmp0 (action, "idle") != 0)) {
1534
1535 g_set_error_literal (error, BD_MD_ERROR, BD_MD_ERROR_INVAL,
1536 "Action must be one of resync, recovery, check, repair or idle.");
1537 return FALSE;
1538 }
1539
1540 raid_node = get_sysfs_name_from_input (raid_spec, error);
1541 if (!raid_node)
1542 /* error is already populated */
1543 return FALSE;
1544
1545 sys_path = g_strdup_printf ("/sys/class/block/%s/md/sync_action", raid_node);
1546 g_free (raid_node);
1547
1548 success = bd_utils_echo_str_to_file (action, sys_path, error);
1549 g_free (sys_path);
1550 if (!success) {
1551 g_prefix_error (error, "Failed to set requested sync action.");
1552 return FALSE;
1553 }
1554
1555 return TRUE;
1556 }