source: MondoRescue/branches/3.2/mondo/src/common/libmondo-raid.c@ 3610

Last change on this file since 3610 was 3610, checked in by Bruno Cornec, 7 years ago
  • Change interfaces for the call_program_and_get_last_line_of_output and where_is_root_mounted(now return dynamically allocated string)
  • removes useless function store_netfs_config
  • Property svn:keywords set to Id
File size: 33.8 KB
Line 
1/* libmondo-raid.c subroutines for handling RAID
2 $Id: libmondo-raid.c 3610 2016-11-05 17:12:23Z bruno $
3*/
4
5
6/**
7 * @file
8 * Functions for handling RAID (especially during restore).
9 */
10
11#include "my-stuff.h"
12#include "mondostructures.h"
13#include "libmondo-gui-EXT.h"
14#include "libmondo-files-EXT.h"
15#include "libmondo-tools-EXT.h"
16#include "libmondo-string-EXT.h"
17#include "libmondo-fork-EXT.h"
18#include "lib-common-externs.h"
19#include "libmondo-raid.h"
20#include "mr_mem.h"
21#include "mr_str.h"
22
23#ifdef __FreeBSD__
24/* Nonstandard library functions: */
25extern void errx(int exitval, const char *fmt, ...);
26extern char *strsep(char **stringp, const char *delim);
27#endif
28
29/*@unused@*/
30//static char cvsid[] = "$Id: libmondo-raid.c 3610 2016-11-05 17:12:23Z bruno $";
31
32
33/**
34 * @addtogroup raidGroup
35 * @{
36 */
37/**
38 * See if a particular RAID level is supported by the kernel.
39 * @param raidno The RAID level (-1 through 5) to check. -1 means "linear" under Linux and
40 * "concatenated" under FreeBSD. It's really the same thing, just different wording.
41 * @return TRUE if it's supported, FALSE if not.
42 */
43bool is_this_raid_personality_registered(int raidno)
44{
45#ifdef __FreeBSD__
46 return ((raidno == -1) || (raidno == 0) || (raidno == 1)
47 || (raidno == 5)) ? TRUE : FALSE;
48#else
49 /*@ buffer ********************************************************** */
50 char *command = NULL;
51 int res;
52
53 mr_asprintf(command, "grep \"");
54 if (raidno == -1) {
55 mr_strcat(command, "linear");
56 } else {
57 mr_strcat(command, "raid%d", raidno);
58 }
59 mr_strcat(command, "\" /proc/mdstat > /dev/null 2> /dev/null");
60 log_it("Is raid %d registered? Command = '%s'", raidno, command);
61 res = system(command);
62 paranoid_free(command);
63
64 if (res) {
65 return (FALSE);
66 } else {
67 return (TRUE);
68 }
69#endif
70}
71
72
73
74
75
76
77/**
78 * Search for @p device in @p disklist.
79 * @param disklist The disklist to search in.
80 * @param device The device to search for.
81 * @return The index number of @p device, or -1 if it does not exist.
82 */
83int
84where_in_drivelist_is_drive(struct list_of_disks *disklist, char *device)
85{
86
87 /*@ int ************************************************************* */
88 int i = 0;
89
90 assert(disklist != NULL);
91 assert_string_is_neither_NULL_nor_zerolength(device);
92
93 for (i = 0; i < disklist->entries; i++) {
94 if (!strcmp(disklist->el[i].device, device)) {
95 break;
96 }
97 }
98 if (i == disklist->entries) {
99 return (-1);
100 } else {
101 return (i);
102 }
103}
104
105
106
107
108
109
110
111
112/**
113 * Determine which RAID device is using a particular partition.
114 * @param raidlist The RAID information structure.
115 * @param device The partition to find out about.
116 * @return The index number of the RAID device using @p device, or -1 if there is none.
117 */
118int
119which_raid_device_is_using_this_partition(struct raidlist_itself *raidlist,
120 char *device)
121{
122#ifdef __FreeBSD__
123// FreeBSD-specific version of which_raid_device_is_using_this_partition()
124 /*@ int ********************************************************* */
125 int i = 0;
126
127 for (i = 0; i < raidlist->entries; i++) {
128 bool thisone = FALSE;
129 int j, k, l;
130
131 for (j = 0; j < raidlist->el[i].plexes; ++j) {
132 for (k = 0; k < raidlist->el[i].plex[j].subdisks; ++k) {
133 for (l = 0; l < raidlist->disks.entries; ++l) {
134 if (!strcmp(raidlist->disks.el[l].device,
135 device) &&
136 !strcmp(raidlist->el[i].plex[j].sd[k].which_device,
137 raidlist->disks.el[l].name))
138 thisone = TRUE;
139 }
140 }
141 }
142
143 if (thisone) {
144 break;
145 }
146 }
147 if (i == raidlist->entries) {
148 return (-1);
149 } else {
150 return (i);
151 }
152}
153
154#else
155// Linux-specific version of which_raid_device_is_using_this_partition()
156// and one other function which FreeBSD doesn't use
157
158 int current_raiddev = 0;
159
160 assert_string_is_neither_NULL_nor_zerolength(device);
161 assert(raidlist != NULL);
162
163 for (current_raiddev = 0; current_raiddev < raidlist->entries;
164 current_raiddev++) {
165 if (where_in_drivelist_is_drive
166 (&raidlist->el[current_raiddev].data_disks, device) >= 0
167 || where_in_drivelist_is_drive(&raidlist->el[current_raiddev].
168 spare_disks, device) >= 0
169 || where_in_drivelist_is_drive(&raidlist->el[current_raiddev].
170 parity_disks, device) >= 0
171 || where_in_drivelist_is_drive(&raidlist->el[current_raiddev].
172 failed_disks, device) >= 0) {
173 break;
174 }
175 }
176 if (current_raiddev == raidlist->entries) {
177 return (-1);
178 } else {
179 return (current_raiddev);
180 }
181}
182
183/**
184 * Write an @c int variable to a list of RAID variables.
185 * @param raidrec The RAID device record to write to.
186 * @param lino The variable index number to modify/create.
187 * @param label The label to write.
188 * @param value The value to write.
189 */
190void
191write_variableINT_to_raid_var_line(struct raid_device_record *raidrec,
192 int lino, char *label, int value)
193{
194 /*@ buffers ***************************************************** */
195 char *sz_value = NULL;
196
197 assert(raidrec != NULL);
198 assert(label != NULL);
199
200 mr_asprintf(sz_value, "%d", value);
201 strcpy(raidrec->additional_vars.el[lino].label, label);
202 strcpy(raidrec->additional_vars.el[lino].value, sz_value);
203 mr_free(sz_value);
204}
205#endif
206
207
208
209
210
211
212
213
214#ifdef __FreeBSD__
215/**
216 * Add a disk to a RAID plex.
217 * @param p The plex to add the device to.
218 * @param device_to_add The device to add to @p p.
219 */
220void add_disk_to_raid_device(struct vinum_plex *p, char *device_to_add)
221{
222 strcpy(p->sd[p->subdisks].which_device, device_to_add);
223 ++p->subdisks;
224
225}
226#else
227/**
228 * Add a disk to a RAID device.
229 * @param disklist The disklist to add the device to.
230 * @param device_to_add The device to add to @p disklist.
231 * @param index The index number of the disklist entry we're creating.
232 */
233void add_disk_to_raid_device(struct list_of_disks *disklist,
234 char *device_to_add, int index)
235{
236 int items;
237
238 assert(disklist != NULL);
239 assert_string_is_neither_NULL_nor_zerolength(device_to_add);
240 items = disklist->entries;
241 strcpy(disklist->el[items].device, device_to_add);
242 disklist->el[items].index = index;
243 items++;
244 disklist->entries = items;
245 log_msg(3, "Adding device %s (%d)", device_to_add, index);
246}
247#endif
248
249
250/**
251 * Save the additional RAID variables to a stream.
252 * @param vars The RAID variable list to save.
253 * @param fout The FILE pointer to save them to.
254 */
255void
256save_additional_vars_to_file(struct additional_raid_variables *vars,
257 FILE * fout)
258{
259 int i;
260
261 assert(vars != NULL);
262 assert(fout != NULL);
263
264 for (i = 0; i < vars->entries; i++) {
265 fprintf(fout, " %-21s %s\n", vars->el[i].label,
266 vars->el[i].value);
267 }
268}
269
270
271/**
272 * Save a raidlist structure to disk in raidtab format.
273 * @param raidlist The raidlist to save.
274 * @param fname The file to save it to.
275 * @return 0, always.
276 * @bug Return value is redundant.
277 */
278int save_raidlist_to_raidtab(struct raidlist_itself *raidlist, char *fname)
279{
280 FILE *fout;
281 int current_raid_device;
282#ifdef __FreeBSD__
283 int i;
284#else
285// Linux
286#endif
287
288 assert(raidlist != NULL);
289 assert_string_is_neither_NULL_nor_zerolength(fname);
290
291 if (raidlist->entries <= 0) {
292 unlink(fname);
293 log_it("Deleting raidtab (no RAID devs anyway)");
294 return (0);
295 }
296 if (!(fout = fopen(fname, "w"))) {
297 log_OS_error("Failed to save raidlist");
298 return (1);
299 }
300 fprintf(fout, "# Generated by Mondo Rescue\n");
301
302#ifdef __FreeBSD__
303 for (i = 0; i < raidlist->disks.entries; ++i) {
304 fprintf(fout, "drive %s device %s\n", raidlist->disks.el[i].name,
305 raidlist->disks.el[i].device);
306 }
307 for (i = 0; i < (raidlist->spares.entries); ++i) {
308 fprintf(fout, "drive %s device %s hotspare\n",
309 raidlist->spares.el[i].name,
310 raidlist->spares.el[i].device);
311 }
312#endif
313
314 for (current_raid_device = 0; current_raid_device < raidlist->entries;
315 current_raid_device++) {
316 save_raidrec_to_file(&raidlist->el[current_raid_device], fout);
317 }
318 paranoid_fclose(fout);
319 return (0);
320}
321
322
323/**
324 * Save an individual RAID device record to a stream.
325 * @param raidrec The RAID device record to save.
326 * @param fout The stream to save it to.
327 */
328void save_raidrec_to_file(struct
329#ifdef __FreeBSD__
330 vinum_volume
331#else
332 raid_device_record
333#endif
334 * raidrec, FILE * fout)
335{
336#ifdef __FreeBSD__
337 int i, j;
338 char *org = NULL;
339
340 fprintf(fout, "\nvolume %s\n", raidrec->volname);
341 for (i = 0; i < raidrec->plexes; ++i) {
342 switch (raidrec->plex[i].raidlevel) {
343 case -1:
344 mr_asprintf(org, "concat");
345 break;
346 case 0:
347 mr_asprintf(org, "striped");
348 break;
349 case 5:
350 mr_asprintf(org, "raid5");
351 break;
352 }
353 fprintf(fout, " plex org %s", org);
354 mr_free(org);
355
356 if (raidrec->plex[i].raidlevel != -1) {
357 fprintf(fout, " %ik", raidrec->plex[i].stripesize);
358 }
359 fprintf(fout, "\n");
360
361 for (j = 0; j < raidrec->plex[i].subdisks; ++j) {
362 fprintf(fout, " sd drive %s size 0\n", raidrec->plex[i].sd[j].which_device);
363 }
364 }
365#else
366 assert(raidrec != NULL);
367 assert(fout != NULL);
368
369 fprintf(fout, "raiddev %s\n", raidrec->raid_device);
370 if (raidrec->raid_level == -2) {
371 fprintf(fout, " raid-level multipath\n");
372 } else if (raidrec->raid_level == -1) {
373 fprintf(fout, " raid-level linear\n");
374 } else {
375 fprintf(fout, " raid-level %d\n", raidrec->raid_level);
376 }
377 fprintf(fout, " nr-raid-disks %d\n", raidrec->data_disks.entries);
378 if (raidrec->spare_disks.entries > 0) {
379 fprintf(fout, " nr-spare-disks %d\n", raidrec->spare_disks.entries);
380 }
381 if (raidrec->parity_disks.entries > 0) {
382 fprintf(fout, " nr-parity-disks %d\n", raidrec->parity_disks.entries);
383 }
384 fprintf(fout, " persistent-superblock %d\n", raidrec->persistent_superblock);
385 if (raidrec->chunk_size > -1) {
386 fprintf(fout, " chunk-size %d\n", raidrec->chunk_size);
387 }
388 if (raidrec->parity > -1) {
389 switch(raidrec->parity) {
390 case 0:
391 fprintf(fout, " parity-algorithm left-asymmetric\n");
392 break;
393 case 1:
394 fprintf(fout, " parity-algorithm right-asymmetric\n");
395 break;
396 case 2:
397 fprintf(fout, " parity-algorithm left-symmetric\n");
398 break;
399 case 3:
400 fprintf(fout, " parity-algorithm right-symmetric\n");
401 break;
402 default:
403 fatal_error("Unknown RAID parity algorithm.");
404 break;
405 }
406 }
407 save_additional_vars_to_file(&raidrec->additional_vars, fout);
408 fprintf(fout, "\n");
409 save_disklist_to_file("raid-disk", &raidrec->data_disks, fout);
410 save_disklist_to_file("spare-disk", &raidrec->spare_disks, fout);
411 save_disklist_to_file("parity-disk", &raidrec->parity_disks, fout);
412 save_disklist_to_file("failed-disk", &raidrec->failed_disks, fout);
413 fprintf(fout, "\n");
414#endif
415}
416
417/**
418 * Retrieve the next line from a raidtab stream.
419 * @param fin The file to read the input from.
420 * @param label Where to put the line's label.
421 * @param value Where to put the line's value.
422 * @return 0 if the line was read and stored successfully, 1 if we're at end of file.
423 */
424static int get_next_raidtab_line(FILE * fin, char **label, char **value)
425{
426 char *incoming = NULL;
427 char *p = NULL;
428
429 assert(fin != NULL);
430
431 if (feof(fin)) {
432 return (1);
433 }
434
435 for (mr_getline(incoming, fin); !feof(fin); mr_getline(incoming, fin)) {
436 log_msg(10,"Found in raidtab line: %s",incoming);
437 mr_strip_spaces(incoming);
438 p = strchr(incoming, ' ');
439 if (strlen(incoming) < 3 || incoming[0] == '#' || !p) {
440 mr_free(incoming);
441 continue;
442 }
443 *(p++) = '\0';
444 while (*p == ' ') {
445 p++;
446 }
447 *label = incoming;
448 *value = p;
449 log_msg(10,"Found in raidtab pair: %s|%s",*label,*value);
450 return (0);
451 }
452 return (1);
453}
454
455
456
457/**
458 * Load a raidtab file into a raidlist structure.
459 * @param raidlist The raidlist to fill.
460 * @param fname The file to read from.
461 * @return 0 for success, 1 for failure.
462 */
463#ifdef __FreeBSD__
464int load_raidtab_into_raidlist(struct raidlist_itself *raidlist, char *fname)
465{
466 FILE *fin;
467 int items;
468
469 raidlist->spares.entries = 0;
470 raidlist->disks.entries = 0;
471 if (length_of_file(fname) < 5) {
472 log_it("Raidtab is very small or non-existent. Ignoring it.");
473 raidlist->entries = 0;
474 return (0);
475 }
476 if (!(fin = fopen(fname, "r"))) {
477 log_it("Cannot open raidtab");
478 return (1);
479 }
480 items = 0;
481 log_it("Loading raidtab...");
482 while (!feof(fin)) {
483 int argc;
484 char **argv = get_next_vinum_conf_line(fin, &argc);
485 if (!argv)
486 break;
487 if (!strcmp(argv[0], "drive")) {
488 char *drivename, *devname;
489 if (argc < 4)
490 continue;
491 drivename = argv[1];
492 devname = get_option_val(argc, argv, "device");
493 if (!devname)
494 continue;
495
496 if (get_option_state(argc, argv, "hotspare")) {
497 strcpy(raidlist->spares.el[raidlist->spares.entries].name, drivename);
498 strcpy(raidlist->spares.el[raidlist->spares.entries].device, devname);
499 raidlist->spares.el[raidlist->spares.entries].index = raidlist->disks.entries;
500 raidlist->spares.entries++;
501 } else {
502 strcpy(raidlist->disks.el[raidlist->disks.entries].name, drivename);
503 strcpy(raidlist->disks.el[raidlist->disks.entries].device, devname);
504 raidlist->disks.el[raidlist->disks.entries].index = raidlist->disks.entries;
505 raidlist->disks.entries++;
506 }
507 } else if (!strcmp(argv[0], "volume")) {
508 char *volname;
509 if (argc < 2)
510 continue;
511 volname = argv[1];
512 strcpy(raidlist->el[raidlist->entries].volname, volname);
513 raidlist->el[raidlist->entries].plexes = 0;
514 raidlist->entries++;
515 } else if (!strcmp(argv[0], "plex")) {
516 int raidlevel, stripesize;
517 char *org = 0;
518 char **tmp = 0;
519 if (argc < 3)
520 continue;
521 org = get_option_val(argc, argv, "org");
522 if (!org)
523 continue;
524 if (strcmp(org, "concat")) {
525 tmp = get_option_vals(argc, argv, "org", 2);
526 if (tmp && tmp[1]) {
527 stripesize = (int) (size_spec(tmp[1]) / 1024);
528 } else
529 stripesize = 279;
530 } else
531 stripesize = 0;
532
533 if (!strcmp(org, "concat")) {
534 raidlevel = -1;
535 } else if (!strcmp(org, "striped")) {
536 raidlevel = 0;
537 } else if (!strcmp(org, "raid5")) {
538 raidlevel = 5;
539 } else
540 continue;
541
542 raidlist->el[raidlist->entries - 1].plex
543 [raidlist->el[raidlist->entries - 1].plexes].raidlevel =
544 raidlevel;
545 raidlist->el[raidlist->entries -
546 1].plex[raidlist->el[raidlist->entries -
547 1].plexes].stripesize =
548 stripesize;
549 raidlist->el[raidlist->entries -
550 1].plex[raidlist->el[raidlist->entries -
551 1].plexes].subdisks = 0;
552 raidlist->el[raidlist->entries - 1].plexes++;
553 } else if ((!strcmp(argv[0], "sd"))
554 || (!strcmp(argv[0], "subdisk"))) {
555 char *drive = 0;
556 if (argc < 3)
557 continue;
558 drive = get_option_val(argc, argv, "drive");
559 if (!drive)
560 continue;
561
562 strcpy(raidlist->el[raidlist->entries - 1].plex
563 [raidlist->el[raidlist->entries - 1].plexes - 1].sd
564 [raidlist->el[raidlist->entries - 1].plex
565 [raidlist->el[raidlist->entries - 1].plexes -
566 1].subdisks].which_device, drive);
567 raidlist->el[raidlist->entries -
568 1].plex[raidlist->el[raidlist->entries -
569 1].plexes - 1].subdisks++;
570 }
571 }
572 fclose(fin);
573 log_it("Raidtab loaded successfully.");
574 log_it("%d RAID devices in raidtab", raidlist->entries);
575 return (0);
576}
577
578
579#else
580
581int load_raidtab_into_raidlist(struct raidlist_itself *raidlist, char *fname) {
582
583 FILE *fin = NULL;
584 char *label = NULL;
585 char *value = NULL;
586 int items = 0;
587 int v = 0;
588
589 assert(raidlist != NULL);
590 assert_string_is_neither_NULL_nor_zerolength(fname);
591
592 if (length_of_file(fname) < 5) {
593 log_it("Raidtab is very small or non-existent. Ignoring it.");
594 raidlist->entries = 0;
595 return (0);
596 }
597 if (!(fin = fopen(fname, "r"))) {
598 log_it("Cannot open raidtab");
599 return (1);
600 }
601 log_it("Loading raidtab...");
602 get_next_raidtab_line(fin, &label, &value);
603 while (!feof(fin)) {
604 log_msg(1, "Looking for raid record #%d", items);
605 initialize_raidrec(&raidlist->el[items]);
606 v = 0;
607 /* find the 'raiddev' entry, indicating the start of the block of info */
608 while (!feof(fin) && strcmp(label, "raiddev")) {
609 strcpy(raidlist->el[items].additional_vars.el[v].label, label);
610 strcpy(raidlist->el[items].additional_vars.el[v].value, value);
611 mr_free(label);
612 log_msg(2,"Found raidtab entry Label: %s Value: %s",raidlist->el[items].additional_vars.el[v].label,raidlist->el[items].additional_vars.el[v].value);
613 v++;
614 get_next_raidtab_line(fin, &label, &value);
615 }
616
617 raidlist->el[items].additional_vars.entries = v;
618 if (feof(fin)) {
619 log_msg(1, "No more records.");
620 continue;
621 }
622 log_msg(2, "Record #%d (%s) found", items, value);
623 strcpy(raidlist->el[items].raid_device, value);
624 mr_free(label);
625 for (get_next_raidtab_line(fin, &label, &value); !feof(fin) && strcmp(label, "raiddev"); get_next_raidtab_line(fin, &label, &value)) {
626 process_raidtab_line(fin, &raidlist->el[items], label, value);
627 mr_free(label);
628 }
629 items++;
630 }
631 mr_free(label);
632 paranoid_fclose(fin);
633 raidlist->entries = items;
634 log_msg(1, "Raidtab loaded successfully.");
635 log_msg(1, "%d RAID devices in raidtab", items);
636 return (0);
637}
638#endif
639
640
641
642
643
644
645
646
647#ifndef __FreeBSD__
648/**
649 * Process a single line from the raidtab and store the results into @p raidrec.
650 * @param fin The stream to read the line from.
651 * @param raidrec The RAID device record to update.
652 * @param label Where to put the label processed.
653 * @param value Where to put the value processed.
654 */
655void
656process_raidtab_line(FILE * fin, struct raid_device_record *raidrec, char *label, char *value)
657{
658
659 /*@ add mallocs * */
660 char *labelB = NULL;
661 char *valueB = NULL;
662
663 struct list_of_disks *disklist;
664 int index;
665 int v;
666
667 assert(fin != NULL);
668 assert(raidrec != NULL);
669 assert_string_is_neither_NULL_nor_zerolength(label);
670 assert(value != NULL);
671
672 if (!strcmp(label, "raid-level")) {
673 if (!strcmp(value, "multipath")) {
674 raidrec->raid_level = -2;
675 } else if (!strcmp(value, "linear")) {
676 raidrec->raid_level = -1;
677 } else {
678 raidrec->raid_level = atoi(value);
679 }
680 log_msg(4,"Found raid level %d",raidrec->raid_level);
681 } else if (!strcmp(label, "nr-raid-disks")) { /* ignore it */
682 } else if (!strcmp(label, "nr-spare-disks")) { /* ignore it */
683 } else if (!strcmp(label, "nr-parity-disks")) { /* ignore it */
684 } else if (!strcmp(label, "nr-failed-disks")) { /* ignore it */
685 } else if (!strcmp(label, "persistent-superblock")) {
686 raidrec->persistent_superblock = atoi(value);
687 } else if (!strcmp(label, "chunk-size")) {
688 raidrec->chunk_size = atoi(value);
689 } else if (!strcmp(label, "parity-algorithm")) {
690 if (!strcmp(value, "left-asymmetric")) {
691 raidrec->parity = 0;
692 } else if (!strcmp(value, "right-asymmetric")) {
693 raidrec->parity = 1;
694 } else if (!strcmp(value, "left-symmetric")) {
695 raidrec->parity = 2;
696 } else if (!strcmp(value, "right-symmetric")) {
697 raidrec->parity = 3;
698 } else {
699 log_msg(1, "Unknown RAID parity algorithm '%s'", value);
700 }
701 log_msg(4,"Found raid parity %d",raidrec->parity);
702 } else if (!strcmp(label, "device")) {
703 get_next_raidtab_line(fin, &labelB, &valueB);
704 if (!strcmp(labelB, "raid-disk")) {
705 disklist = &raidrec->data_disks;
706 } else if (!strcmp(labelB, "spare-disk")) {
707 disklist = &raidrec->spare_disks;
708 } else if (!strcmp(labelB, "parity-disk")) {
709 disklist = &raidrec->parity_disks;
710 } else if (!strcmp(labelB, "failed-disk")) {
711 disklist = &raidrec->failed_disks;
712 } else {
713 disklist = NULL;
714 }
715 if (!disklist) {
716 log_it("Ignoring '%s %s' pair of disk %s", labelB, valueB, label);
717 } else {
718 index = atoi(valueB);
719 add_disk_to_raid_device(disklist, value, index);
720 }
721 mr_free(labelB);
722 } else {
723 v = raidrec->additional_vars.entries;
724 strcpy(raidrec->additional_vars.el[v].label, label);
725 strcpy(raidrec->additional_vars.el[v].value, value);
726 log_msg(4,"Found additional raid pair #%d: %s / %s",v,raidrec->additional_vars.el[v].label,raidrec->additional_vars.el[v].value);
727 v++;
728 raidrec->additional_vars.entries = v;
729 }
730}
731#endif
732
733
734/**
735 * Save a disklist to a stream in raidtab format.
736 * @param listname One of "raid-disk", "spare-disk", "parity-disk", or "failed-disk".
737 * @param disklist The disklist to save to @p fout.
738 * @param fout The stream to write to.
739 */
740void
741save_disklist_to_file(char *listname, struct list_of_disks *disklist, FILE * fout)
742{
743 int i;
744
745 assert_string_is_neither_NULL_nor_zerolength(listname);
746 assert(disklist != NULL);
747 assert(fout != NULL);
748
749 for (i = 0; i < disklist->entries; i++) {
750 fprintf(fout, " device %s\n", disklist->el[i].device);
751 fprintf(fout, " %-21s %d\n", listname, disklist->el[i].index);
752 }
753}
754
755
756
757
758
759#ifdef __FreeBSD__
760/**
761 * Add a new plex to a volume. The index of the plex will be <tt>v-\>plexes - 1</tt>.
762 * @param v The volume to operate on.
763 * @param raidlevel The RAID level of the new plex.
764 * @param stripesize The stripe size (chunk size) of the new plex.
765 */
766void add_plex_to_volume(struct vinum_volume *v, int raidlevel,
767 int stripesize)
768{
769 v->plex[v->plexes].raidlevel = raidlevel;
770 v->plex[v->plexes].stripesize = stripesize;
771 v->plex[v->plexes].subdisks = 0;
772 ++v->plexes;
773}
774
775/**
776 * For internal use only.
777 */
778char **get_next_vinum_conf_line(FILE * f, int *argc)
779{
780 int cnt = 0;
781 static char *argv[64];
782 char **ap;
783 char *line = NULL;
784
785 mr_getline(line, f);
786 if (feof(f)) {
787 log_it("[GNVCL] Uh... I reached the EOF.");
788 return 0;
789 }
790
791 for (ap = argv; (*ap = strsep(&line, " \t")) != NULL;)
792 if (**ap != '\0') {
793 if (++ap >= &argv[64])
794 break;
795 cnt++;
796 }
797 mr_free(line);
798
799 if (strchr(argv[cnt - 1], '\n')) {
800 *(strchr(argv[cnt - 1], '\n')) = '\0';
801 }
802
803 if (argc)
804 *argc = cnt;
805 return argv;
806}
807
808/**
809 * For internal use only.
810 */
811char *get_option_val(int argc, char **argv, char *option)
812{
813 int i;
814 for (i = 0; i < (argc - 1); ++i) {
815 if (!strcmp(argv[i], option)) {
816 return argv[i + 1];
817 }
818 }
819 return 0;
820}
821
822/**
823 * For internal use only.
824 */
825char **get_option_vals(int argc, char **argv, char *option, int nval)
826{
827 int i, j;
828 static char **ret;
829 ret = (char **) malloc(nval * sizeof(char *));
830 for (i = 0; i < (argc - nval); ++i) {
831 if (!strcmp(argv[i], option)) {
832 for (j = 0; j < nval; ++j) {
833 ret[j] = (char *) malloc(strlen(argv[i + j + 1]) + 1);
834 strcpy(ret[j], argv[i + j + 1]);
835 }
836 return ret;
837 }
838 }
839 return 0;
840}
841
842/**
843 * For internal use only.
844 */
845bool get_option_state(int argc, char **argv, char *option)
846{
847 int i;
848 for (i = 0; i < argc; ++i)
849 if (!strcmp(argv[i], option))
850 return TRUE;
851
852 return FALSE;
853}
854
855/**
856 * Taken from Vinum source -- for internal use only.
857 */
858long long size_spec(char *spec)
859{
860 u_int64_t size;
861 char *s;
862 int sign = 1; /* -1 if negative */
863
864 size = 0;
865 if (spec != NULL) { /* we have a parameter */
866 s = spec;
867 if (*s == '-') { /* negative, */
868 sign = -1;
869 s++; /* skip */
870 }
871 if ((*s >= '0') && (*s <= '9')) { /* it's numeric */
872 while ((*s >= '0') && (*s <= '9')) /* it's numeric */
873 size = size * 10 + *s++ - '0'; /* convert it */
874 switch (*s) {
875 case '\0':
876 return size * sign;
877
878 case 'B':
879 case 'b':
880 case 'S':
881 case 's':
882 return size * sign * 512;
883
884 case 'K':
885 case 'k':
886 return size * sign * 1024;
887
888 case 'M':
889 case 'm':
890 return size * sign * 1024 * 1024;
891
892 case 'G':
893 case 'g':
894 return size * sign * 1024 * 1024 * 1024;
895
896 case 'T':
897 case 't':
898 log_it
899 ("Ok, I'm scared... Someone did a TERABYTE+ size-spec");
900 return size * sign * 1024 * 1024 * 1024 * 1024;
901
902 case 'P':
903 case 'p':
904 log_it
905 ("If I was scared last time, I'm freaked out now. Someone actually has a PETABYTE?!?!?!?!");
906 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024;
907
908 case 'E':
909 case 'e':
910 log_it
911 ("Okay, I'm REALLY freaked out. Who could devote a whole EXABYTE to their data?!?!");
912 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
913 1024;
914
915 case 'Z':
916 case 'z':
917 log_it
918 ("WHAT!?!? A ZETABYTE!?!? You've GOT to be kidding me!!!");
919 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
920 1024 * 1024;
921
922 case 'Y':
923 case 'y':
924 log_it
925 ("Oh my gosh. You actually think a YOTTABYTE will get you anywhere? What're you going to do with 1,208,925,819,614,629,174,706,176 bytes?!?!");
926 popup_and_OK
927 ("That sizespec is more than 1,208,925,819,614,629,174,706,176 bytes. You have a shocking amount of data. Please send a screenshot to the list :-)");
928 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
929 1024 * 1024 * 1024;
930 }
931 }
932 }
933 return size * sign;
934}
935
936#endif
937
938
939
940
941int parse_mdstat(char *mdstat_fname, struct raidlist_itself *raidlist, char *device_prefix) {
942
943const char delims[] = " ";
944
945FILE *fin = NULL;
946int res = 0, row, i, index_min;
947int v = 0;
948int lastpos = 0;
949size_t len = 0;
950char *token = NULL;
951char *string = NULL;
952char *cmd = NULL;
953char *pos = NULL;
954char type;
955char *strtmp = NULL;
956char *strtmp2 = NULL;
957
958// open file
959if (!(fin = fopen(mdstat_fname, "r"))) {
960 log_msg(1, "Could not open %s.", mdstat_fname);
961 return 1;
962}
963// initialise record, build progress and row counters
964raidlist->entries = 0;
965raidlist->el[raidlist->entries].progress = 999;
966row = 1;
967// skip first output row - contains registered RAID levels
968res = getline(&strtmp2, &len, fin);
969mr_free(strtmp2);
970// parse the rest
971while ( !feof_unlocked(fin) ) {
972 res = getline(&string, &len, fin);
973 log_msg(8, "mdstat line '%s' read.", string);
974 if (res <= 0) break;
975 // trim spaces
976 strip_spaces(string);
977 log_msg(8, "mdstat line 2 '%s' read.", string);
978 // if we have newline after only spaces, this is a blank line, update
979 // counters, otherwise do normal parsing
980 if (!strcmp(string,"")) {
981 row = 1;
982 raidlist->entries++;
983 raidlist->el[raidlist->entries].progress = 999;
984 } else {
985 switch (row) {
986 case 1: // device information
987 // check whether last line of record and if so skip
988 log_msg(8, "This is the device line");
989 pos = strcasestr(string, "unused devices: ");
990 if (pos != NULL) {
991 break;
992 }
993 // tokenise string
994 token = mr_strtok(string, delims, &lastpos);
995 if (token == NULL) {
996 // should not happen !
997 break;
998 }
999 // get RAID device name
1000 mr_asprintf(strtmp,"%s%s", device_prefix, token);
1001 strncpy(raidlist->el[raidlist->entries].raid_device, strtmp, 63);
1002 raidlist->el[raidlist->entries].raid_device[63] = '\0';
1003 mr_free(strtmp);
1004 mr_free(token);
1005 // store the UUID value in the additional_vars structure
1006 v = raidlist->el[raidlist->entries].additional_vars.entries;
1007 strcpy(raidlist->el[raidlist->entries].additional_vars.el[v].label, "UUID");
1008 mr_asprintf(cmd,"mdadm --detail %s | grep UUID | cut -d: -f2- | sed 's/^ *//'", raidlist->el[raidlist->entries].raid_device);
1009 strtmp = call_program_and_get_last_line_of_output(cmd);
1010 strcpy(raidlist->el[raidlist->entries].additional_vars.el[v].value, strtmp);
1011 mr_free(strtmp);
1012 v++;
1013 // store the Version value in the additional_vars structure
1014 strcpy(raidlist->el[raidlist->entries].additional_vars.el[v].label, "Version");
1015 mr_asprintf(cmd,"mdadm --detail %s | grep Version | cut -d: -f2- | sed 's/^ *//'", raidlist->el[raidlist->entries].raid_device);
1016 strtmp = call_program_and_get_last_line_of_output(cmd);
1017 strcpy(raidlist->el[raidlist->entries].additional_vars.el[v].value, strtmp);
1018 mr_free(strtmp);
1019 v++;
1020 raidlist->el[raidlist->entries].additional_vars.entries = v;
1021 // skip ':' and status
1022 token = mr_strtok(string, delims, &lastpos);
1023 if (token == NULL) {
1024 // should not happen !
1025 break;
1026 }
1027 mr_free(token);
1028 token = mr_strtok(string, delims, &lastpos);
1029 if (token == NULL) {
1030 // should not happen !
1031 break;
1032 }
1033 if (!strcmp(token, "inactive")) {
1034 log_msg(1, "RAID device '%s' inactive.", raidlist->el[raidlist->entries].raid_device);
1035 mr_free(string);
1036 mr_free(token);
1037 return 1;
1038 }
1039 mr_free(token);
1040
1041 // get RAID level
1042 token = mr_strtok(string, delims, &lastpos);
1043 if (token == NULL) {
1044 // should not happen !
1045 break;
1046 }
1047 // skip potential auto-read-only entry
1048 if (!strcmp(token, "(auto-read-only)")) {
1049 mr_free(token);
1050 token = mr_strtok (string, delims, &lastpos);
1051 if (token == NULL) {
1052 // should not happen !
1053 break;
1054 }
1055 }
1056 if (!strcmp(token, "multipath")) {
1057 raidlist->el[raidlist->entries].raid_level = -2;
1058 } else if (!strcmp(token, "linear")) {
1059 raidlist->el[raidlist->entries].raid_level = -1;
1060 } else if (!strcmp(token, "raid0")) {
1061 raidlist->el[raidlist->entries].raid_level = 0;
1062 } else if (!strcmp(token, "raid1")) {
1063 raidlist->el[raidlist->entries].raid_level = 1;
1064 } else if (!strcmp(token, "raid4")) {
1065 raidlist->el[raidlist->entries].raid_level = 4;
1066 } else if (!strcmp(token, "raid5")) {
1067 raidlist->el[raidlist->entries].raid_level = 5;
1068 } else if (!strcmp(token, "raid6")) {
1069 raidlist->el[raidlist->entries].raid_level = 6;
1070 } else if (!strcmp(token, "raid10")) {
1071 raidlist->el[raidlist->entries].raid_level = 10;
1072 } else {
1073 log_msg(1, "Unknown RAID level '%s'", token);
1074 mr_free(string);
1075 mr_free(token);
1076 return 1;
1077 }
1078 mr_free(token);
1079
1080 // get RAID devices (type, index, device)
1081 // Note: parity disk for RAID4 is last normal disk, there is no '(P)'
1082 raidlist->el[raidlist->entries].data_disks.entries = 0;
1083 raidlist->el[raidlist->entries].spare_disks.entries = 0;
1084 raidlist->el[raidlist->entries].failed_disks.entries = 0;
1085 while((token = mr_strtok (string, delims, &lastpos))) {
1086 if ((pos = strstr(token, "("))) {
1087 type = *(pos+1);
1088 } else {
1089 type = ' ';
1090 }
1091 pos = strstr(token, "[");
1092 *pos = '\0';
1093 switch(type) {
1094 case ' ': // normal data disks
1095 raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].index = atoi(pos + 1);
1096 mr_asprintf(strtmp,"%s%s", device_prefix, token);
1097 strcpy(raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].device, strtmp);
1098 mr_free(strtmp);
1099 raidlist->el[raidlist->entries].data_disks.entries++;
1100 break;
1101 case 'S': // spare disks
1102 raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].index = atoi(pos + 1);
1103 mr_asprintf(strtmp,"%s%s", device_prefix, token);
1104 strcpy(raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].device, strtmp);
1105 mr_free(strtmp);
1106 raidlist->el[raidlist->entries].spare_disks.entries++;
1107 break;
1108 case 'F': // failed disks
1109 raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].index = atoi(pos + 1);
1110 mr_asprintf(strtmp,"%s%s", device_prefix, token);
1111 strcpy(raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].device, strtmp);
1112 mr_free(strtmp);
1113 raidlist->el[raidlist->entries].failed_disks.entries++;
1114 log_it("At least one failed disk found in RAID array.\n");
1115 break;
1116 default: // error
1117 log_msg(1, "Unknown device type '%c'", type);
1118 mr_free(string);
1119 mr_free(token);
1120 return 1;
1121 break;
1122 }
1123 mr_free(token);
1124 }
1125
1126 // adjust index for each device so that it starts with 0 for every type
1127 index_min = 99;
1128 for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
1129 if (raidlist->el[raidlist->entries].data_disks.el[i].index < index_min) {
1130 index_min = raidlist->el[raidlist->entries].data_disks.el[i].index;
1131 }
1132 }
1133 if (index_min > 0) {
1134 for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
1135 raidlist->el[raidlist->entries].data_disks.el[i].index = raidlist->el[raidlist->entries].data_disks.el[i].index - index_min;
1136 }
1137 }
1138 index_min = 99;
1139 for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
1140 if (raidlist->el[raidlist->entries].spare_disks.el[i].index < index_min) {
1141 index_min = raidlist->el[raidlist->entries].spare_disks.el[i].index;
1142 }
1143 }
1144 if (index_min > 0) {
1145 for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
1146 raidlist->el[raidlist->entries].spare_disks.el[i].index = raidlist->el[raidlist->entries].spare_disks.el[i].index - index_min;
1147 }
1148 }
1149 index_min = 99;
1150 for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
1151 if (raidlist->el[raidlist->entries].failed_disks.el[i].index < index_min) {
1152 index_min = raidlist->el[raidlist->entries].failed_disks.el[i].index;
1153 }
1154 }
1155 if (index_min > 0) {
1156 for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
1157 raidlist->el[raidlist->entries].failed_disks.el[i].index = raidlist->el[raidlist->entries].failed_disks.el[i].index - index_min;
1158 }
1159 }
1160 break;
1161 case 2: // config information
1162 // check for persistent super block
1163 if (strcasestr(string, "super non-persistent")) {
1164 raidlist->el[raidlist->entries].persistent_superblock = 0;
1165 } else {
1166 raidlist->el[raidlist->entries].persistent_superblock = 1;
1167 }
1168 // extract chunk size
1169 if (!(pos = strcasestr(string, "k chunk"))) {
1170 raidlist->el[raidlist->entries].chunk_size = -1;
1171 } else {
1172 while (*pos != ' ') {
1173 pos -= 1;
1174 if (pos < string) {
1175 log_it("String underflow!\n");
1176 mr_free(string);
1177 return 1;
1178 }
1179 }
1180 raidlist->el[raidlist->entries].chunk_size = atoi(pos + 1);
1181 }
1182 // extract parity if present
1183 if ((pos = strcasestr(string, "algorithm"))) {
1184 raidlist->el[raidlist->entries].parity = atoi(pos + 9);
1185 } else {
1186 raidlist->el[raidlist->entries].parity = -1;
1187 }
1188 break;
1189 case 3: // optional build status information
1190 if (!(pos = strchr(string, '\%'))) {
1191 if (strcasestr(string, "delayed")) {
1192 raidlist->el[raidlist->entries].progress = -1; // delayed (therefore, stuck at 0%)
1193 } else {
1194 raidlist->el[raidlist->entries].progress = 999; // not found
1195 }
1196 } else {
1197 while (*pos != ' ') {
1198 pos -= 1;
1199 if (pos < string) {
1200 printf("ERROR: String underflow!\n");
1201 mr_free(string);
1202 return 1;
1203 }
1204 }
1205 raidlist->el[raidlist->entries].progress = atoi(pos);
1206 }
1207 break;
1208 default: // error or IN PROGRESS
1209 if (raidlist->el[raidlist->entries].progress != -1 &&
1210 raidlist->el[raidlist->entries].progress != 999) {
1211 log_msg(1, "Row %d should not occur in record!", row);
1212 }
1213 break;
1214 }
1215 row++;
1216 }
1217 // free string
1218 mr_free(string);
1219}
1220// close file
1221fclose(fin);
1222// return success
1223return 0;
1224
1225}
1226
1227
1228
1229
1230int create_raidtab_from_mdstat(char *mdstat_fname,char *raidtab_fname)
1231{
1232 struct raidlist_itself *raidlist;
1233 int retval = 0;
1234
1235 raidlist = malloc(sizeof(struct raidlist_itself));
1236
1237 // FIXME: Prefix '/dev/' should really be dynamic!
1238 if (parse_mdstat(mdstat_fname,raidlist, "/dev/")) {
1239 log_to_screen("Sorry, cannot read %s", mdstat_fname);
1240 return (1);
1241 }
1242
1243 retval += save_raidlist_to_raidtab(raidlist, raidtab_fname);
1244 return (retval);
1245}
1246
1247
1248
1249/* @} - end of raidGroup */
Note: See TracBrowser for help on using the repository browser.