source: MondoRescue/branches/3.3/mondo/src/common/libmondo-raid.c@ 3855

Last change on this file since 3855 was 3855, checked in by Bruno Cornec, 4 months ago

Fix save_disklist_to_file and resolve_naff_tokens protos

  • Property svn:keywords set to Id
File size: 33.9 KB
Line 
1/* libmondo-raid.c subroutines for handling RAID
2 $Id: libmondo-raid.c 3855 2024-03-06 02:43:29Z 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 3855 2024-03-06 02:43:29Z 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 a disklist to a stream in raidtab format.
325 * @param listname One of "raid-disk", "spare-disk", "parity-disk", or "failed-disk".
326 * @param disklist The disklist to save to @p fout.
327 * @param fout The stream to write to.
328 */
329void save_disklist_to_file(const char *listname, struct list_of_disks *disklist, FILE * fout)
330{
331 int i;
332
333 assert_string_is_neither_NULL_nor_zerolength(listname);
334 assert(disklist != NULL);
335 assert(fout != NULL);
336
337 for (i = 0; i < disklist->entries; i++) {
338 fprintf(fout, " device %s\n", disklist->el[i].device);
339 fprintf(fout, " %-21s %d\n", listname, disklist->el[i].index);
340 }
341}
342
343/**
344 * Save an individual RAID device record to a stream.
345 * @param raidrec The RAID device record to save.
346 * @param fout The stream to save it to.
347 */
348void save_raidrec_to_file(struct
349#ifdef __FreeBSD__
350 vinum_volume
351#else
352 raid_device_record
353#endif
354 * raidrec, FILE * fout)
355{
356#ifdef __FreeBSD__
357 int i, j;
358 char *org = NULL;
359
360 fprintf(fout, "\nvolume %s\n", raidrec->volname);
361 for (i = 0; i < raidrec->plexes; ++i) {
362 switch (raidrec->plex[i].raidlevel) {
363 case -1:
364 mr_asprintf(org, "concat");
365 break;
366 case 0:
367 mr_asprintf(org, "striped");
368 break;
369 case 5:
370 mr_asprintf(org, "raid5");
371 break;
372 }
373 fprintf(fout, " plex org %s", org);
374 mr_free(org);
375
376 if (raidrec->plex[i].raidlevel != -1) {
377 fprintf(fout, " %ik", raidrec->plex[i].stripesize);
378 }
379 fprintf(fout, "\n");
380
381 for (j = 0; j < raidrec->plex[i].subdisks; ++j) {
382 fprintf(fout, " sd drive %s size 0\n", raidrec->plex[i].sd[j].which_device);
383 }
384 }
385#else
386 assert(raidrec != NULL);
387 assert(fout != NULL);
388
389 fprintf(fout, "raiddev %s\n", raidrec->raid_device);
390 if (raidrec->raid_level == -2) {
391 fprintf(fout, " raid-level multipath\n");
392 } else if (raidrec->raid_level == -1) {
393 fprintf(fout, " raid-level linear\n");
394 } else {
395 fprintf(fout, " raid-level %d\n", raidrec->raid_level);
396 }
397 fprintf(fout, " nr-raid-disks %d\n", raidrec->data_disks.entries);
398 if (raidrec->spare_disks.entries > 0) {
399 fprintf(fout, " nr-spare-disks %d\n", raidrec->spare_disks.entries);
400 }
401 if (raidrec->parity_disks.entries > 0) {
402 fprintf(fout, " nr-parity-disks %d\n", raidrec->parity_disks.entries);
403 }
404 fprintf(fout, " persistent-superblock %d\n", raidrec->persistent_superblock);
405 if (raidrec->chunk_size > -1) {
406 fprintf(fout, " chunk-size %d\n", raidrec->chunk_size);
407 }
408 if (raidrec->parity > -1) {
409 switch(raidrec->parity) {
410 case 0:
411 fprintf(fout, " parity-algorithm left-asymmetric\n");
412 break;
413 case 1:
414 fprintf(fout, " parity-algorithm right-asymmetric\n");
415 break;
416 case 2:
417 fprintf(fout, " parity-algorithm left-symmetric\n");
418 break;
419 case 3:
420 fprintf(fout, " parity-algorithm right-symmetric\n");
421 break;
422 default:
423 fatal_error("Unknown RAID parity algorithm.");
424 break;
425 }
426 }
427 save_additional_vars_to_file(&raidrec->additional_vars, fout);
428 fprintf(fout, "\n");
429 save_disklist_to_file("raid-disk", &raidrec->data_disks, fout);
430 save_disklist_to_file("spare-disk", &raidrec->spare_disks, fout);
431 save_disklist_to_file("parity-disk", &raidrec->parity_disks, fout);
432 save_disklist_to_file("failed-disk", &raidrec->failed_disks, fout);
433 fprintf(fout, "\n");
434#endif
435}
436
437/**
438 * Retrieve the next line from a raidtab stream.
439 * @param fin The file to read the input from.
440 * @param label Where to put the line's label.
441 * @param value Where to put the line's value.
442 * @return 0 if the line was read and stored successfully, 1 if we're at end of file.
443 */
444static int get_next_raidtab_line(FILE * fin, char **label, char **value)
445{
446 char *incoming = NULL;
447 char *p = NULL;
448 char *res = NULL;
449
450 assert(fin != NULL);
451
452 if (feof(fin)) {
453 return (1);
454 }
455
456 for (mr_getline(res, fin); !feof(fin); mr_getline(res, fin)) {
457 log_msg(10,"Found in raidtab line: %s",res);
458 incoming = mr_strip_spaces(res);
459 mr_free(res);
460 p = strchr(incoming, ' ');
461 if (strlen(incoming) < 3 || incoming[0] == '#' || !p) {
462 mr_free(incoming);
463 continue;
464 }
465 *(p++) = '\0';
466 while (*p == ' ') {
467 p++;
468 }
469 *label = incoming;
470 *value = p;
471 log_msg(10,"Found in raidtab pair: %s|%s",*label,*value);
472 return (0);
473 }
474 return (1);
475}
476
477
478
479/**
480 * Load a raidtab file into a raidlist structure.
481 * @param raidlist The raidlist to fill.
482 * @param fname The file to read from.
483 * @return 0 for success, 1 for failure.
484 */
485#ifdef __FreeBSD__
486int load_raidtab_into_raidlist(struct raidlist_itself *raidlist, char *fname)
487{
488 FILE *fin;
489 int items;
490
491 raidlist->spares.entries = 0;
492 raidlist->disks.entries = 0;
493 if (length_of_file(fname) < 5) {
494 log_it("Raidtab is very small or non-existent. Ignoring it.");
495 raidlist->entries = 0;
496 return (0);
497 }
498 if (!(fin = fopen(fname, "r"))) {
499 log_it("Cannot open raidtab");
500 return (1);
501 }
502 items = 0;
503 log_it("Loading raidtab...");
504 while (!feof(fin)) {
505 int argc;
506 char **argv = get_next_vinum_conf_line(fin, &argc);
507 if (!argv)
508 break;
509 if (!strcmp(argv[0], "drive")) {
510 char *drivename, *devname;
511 if (argc < 4)
512 continue;
513 drivename = argv[1];
514 devname = get_option_val(argc, argv, "device");
515 if (!devname)
516 continue;
517
518 if (get_option_state(argc, argv, "hotspare")) {
519 strcpy(raidlist->spares.el[raidlist->spares.entries].name, drivename);
520 strcpy(raidlist->spares.el[raidlist->spares.entries].device, devname);
521 raidlist->spares.el[raidlist->spares.entries].index = raidlist->disks.entries;
522 raidlist->spares.entries++;
523 } else {
524 strcpy(raidlist->disks.el[raidlist->disks.entries].name, drivename);
525 strcpy(raidlist->disks.el[raidlist->disks.entries].device, devname);
526 raidlist->disks.el[raidlist->disks.entries].index = raidlist->disks.entries;
527 raidlist->disks.entries++;
528 }
529 } else if (!strcmp(argv[0], "volume")) {
530 char *volname;
531 if (argc < 2)
532 continue;
533 volname = argv[1];
534 strcpy(raidlist->el[raidlist->entries].volname, volname);
535 raidlist->el[raidlist->entries].plexes = 0;
536 raidlist->entries++;
537 } else if (!strcmp(argv[0], "plex")) {
538 int raidlevel, stripesize;
539 char *org = 0;
540 char **tmp = 0;
541 if (argc < 3)
542 continue;
543 org = get_option_val(argc, argv, "org");
544 if (!org)
545 continue;
546 if (strcmp(org, "concat")) {
547 tmp = get_option_vals(argc, argv, "org", 2);
548 if (tmp && tmp[1]) {
549 stripesize = (int) (size_spec(tmp[1]) / 1024);
550 } else
551 stripesize = 279;
552 } else
553 stripesize = 0;
554
555 if (!strcmp(org, "concat")) {
556 raidlevel = -1;
557 } else if (!strcmp(org, "striped")) {
558 raidlevel = 0;
559 } else if (!strcmp(org, "raid5")) {
560 raidlevel = 5;
561 } else
562 continue;
563
564 raidlist->el[raidlist->entries - 1].plex
565 [raidlist->el[raidlist->entries - 1].plexes].raidlevel =
566 raidlevel;
567 raidlist->el[raidlist->entries -
568 1].plex[raidlist->el[raidlist->entries -
569 1].plexes].stripesize =
570 stripesize;
571 raidlist->el[raidlist->entries -
572 1].plex[raidlist->el[raidlist->entries -
573 1].plexes].subdisks = 0;
574 raidlist->el[raidlist->entries - 1].plexes++;
575 } else if ((!strcmp(argv[0], "sd"))
576 || (!strcmp(argv[0], "subdisk"))) {
577 char *drive = 0;
578 if (argc < 3)
579 continue;
580 drive = get_option_val(argc, argv, "drive");
581 if (!drive)
582 continue;
583
584 strcpy(raidlist->el[raidlist->entries - 1].plex
585 [raidlist->el[raidlist->entries - 1].plexes - 1].sd
586 [raidlist->el[raidlist->entries - 1].plex
587 [raidlist->el[raidlist->entries - 1].plexes -
588 1].subdisks].which_device, drive);
589 raidlist->el[raidlist->entries -
590 1].plex[raidlist->el[raidlist->entries -
591 1].plexes - 1].subdisks++;
592 }
593 }
594 fclose(fin);
595 log_it("Raidtab loaded successfully.");
596 log_it("%d RAID devices in raidtab", raidlist->entries);
597 return (0);
598}
599
600
601#else
602
603int load_raidtab_into_raidlist(struct raidlist_itself *raidlist, char *fname) {
604
605 FILE *fin = NULL;
606 char *label = NULL;
607 char *value = NULL;
608 int items = 0;
609 int v = 0;
610
611 assert(raidlist != NULL);
612 assert_string_is_neither_NULL_nor_zerolength(fname);
613
614 if (length_of_file(fname) < 5) {
615 log_it("Raidtab is very small or non-existent. Ignoring it.");
616 raidlist->entries = 0;
617 return (0);
618 }
619 if (!(fin = fopen(fname, "r"))) {
620 log_it("Cannot open raidtab");
621 return (1);
622 }
623 log_it("Loading raidtab...");
624 get_next_raidtab_line(fin, &label, &value);
625 while (!feof(fin)) {
626 log_msg(1, "Looking for raid record #%d", items);
627 initialize_raidrec(&raidlist->el[items]);
628 v = 0;
629 /* find the 'raiddev' entry, indicating the start of the block of info */
630 while (!feof(fin) && strcmp(label, "raiddev")) {
631 strcpy(raidlist->el[items].additional_vars.el[v].label, label);
632 strcpy(raidlist->el[items].additional_vars.el[v].value, value);
633 mr_free(label);
634 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);
635 v++;
636 get_next_raidtab_line(fin, &label, &value);
637 }
638
639 raidlist->el[items].additional_vars.entries = v;
640 if (feof(fin)) {
641 log_msg(1, "No more records.");
642 continue;
643 }
644 log_msg(2, "Record #%d (%s) found", items, value);
645 strcpy(raidlist->el[items].raid_device, value);
646 mr_free(label);
647 for (get_next_raidtab_line(fin, &label, &value); !feof(fin) && strcmp(label, "raiddev"); get_next_raidtab_line(fin, &label, &value)) {
648 process_raidtab_line(fin, &raidlist->el[items], label, value);
649 mr_free(label);
650 }
651 items++;
652 }
653 mr_free(label);
654 paranoid_fclose(fin);
655 raidlist->entries = items;
656 log_msg(1, "Raidtab loaded successfully.");
657 log_msg(1, "%d RAID devices in raidtab", items);
658 return (0);
659}
660#endif
661
662
663
664
665
666
667
668
669#ifndef __FreeBSD__
670/**
671 * Process a single line from the raidtab and store the results into @p raidrec.
672 * @param fin The stream to read the line from.
673 * @param raidrec The RAID device record to update.
674 * @param label Where to put the label processed.
675 * @param value Where to put the value processed.
676 */
677void
678process_raidtab_line(FILE * fin, struct raid_device_record *raidrec, char *label, char *value)
679{
680
681 /*@ add mallocs * */
682 char *labelB = NULL;
683 char *valueB = NULL;
684
685 struct list_of_disks *disklist;
686 int index;
687 int v;
688
689 assert(fin != NULL);
690 assert(raidrec != NULL);
691 assert_string_is_neither_NULL_nor_zerolength(label);
692 assert(value != NULL);
693
694 if (!strcmp(label, "raid-level")) {
695 if (!strcmp(value, "multipath")) {
696 raidrec->raid_level = -2;
697 } else if (!strcmp(value, "linear")) {
698 raidrec->raid_level = -1;
699 } else {
700 raidrec->raid_level = atoi(value);
701 }
702 log_msg(4,"Found raid level %d",raidrec->raid_level);
703 } else if (!strcmp(label, "nr-raid-disks")) { /* ignore it */
704 } else if (!strcmp(label, "nr-spare-disks")) { /* ignore it */
705 } else if (!strcmp(label, "nr-parity-disks")) { /* ignore it */
706 } else if (!strcmp(label, "nr-failed-disks")) { /* ignore it */
707 } else if (!strcmp(label, "persistent-superblock")) {
708 raidrec->persistent_superblock = atoi(value);
709 } else if (!strcmp(label, "chunk-size")) {
710 raidrec->chunk_size = atoi(value);
711 } else if (!strcmp(label, "parity-algorithm")) {
712 if (!strcmp(value, "left-asymmetric")) {
713 raidrec->parity = 0;
714 } else if (!strcmp(value, "right-asymmetric")) {
715 raidrec->parity = 1;
716 } else if (!strcmp(value, "left-symmetric")) {
717 raidrec->parity = 2;
718 } else if (!strcmp(value, "right-symmetric")) {
719 raidrec->parity = 3;
720 } else {
721 log_msg(1, "Unknown RAID parity algorithm '%s'", value);
722 }
723 log_msg(4,"Found raid parity %d",raidrec->parity);
724 } else if (!strcmp(label, "device")) {
725 get_next_raidtab_line(fin, &labelB, &valueB);
726 if (!strcmp(labelB, "raid-disk")) {
727 disklist = &raidrec->data_disks;
728 } else if (!strcmp(labelB, "spare-disk")) {
729 disklist = &raidrec->spare_disks;
730 } else if (!strcmp(labelB, "parity-disk")) {
731 disklist = &raidrec->parity_disks;
732 } else if (!strcmp(labelB, "failed-disk")) {
733 disklist = &raidrec->failed_disks;
734 } else {
735 disklist = NULL;
736 }
737 if (!disklist) {
738 log_it("Ignoring '%s %s' pair of disk %s", labelB, valueB, label);
739 } else {
740 index = atoi(valueB);
741 add_disk_to_raid_device(disklist, value, index);
742 }
743 mr_free(labelB);
744 } else {
745 v = raidrec->additional_vars.entries;
746 strcpy(raidrec->additional_vars.el[v].label, label);
747 strcpy(raidrec->additional_vars.el[v].value, value);
748 log_msg(4,"Found additional raid pair #%d: %s / %s",v,raidrec->additional_vars.el[v].label,raidrec->additional_vars.el[v].value);
749 v++;
750 raidrec->additional_vars.entries = v;
751 }
752}
753#endif
754
755
756#ifdef __FreeBSD__
757/**
758 * Add a new plex to a volume. The index of the plex will be <tt>v-\>plexes - 1</tt>.
759 * @param v The volume to operate on.
760 * @param raidlevel The RAID level of the new plex.
761 * @param stripesize The stripe size (chunk size) of the new plex.
762 */
763void add_plex_to_volume(struct vinum_volume *v, int raidlevel,
764 int stripesize)
765{
766 v->plex[v->plexes].raidlevel = raidlevel;
767 v->plex[v->plexes].stripesize = stripesize;
768 v->plex[v->plexes].subdisks = 0;
769 ++v->plexes;
770}
771
772/**
773 * For internal use only.
774 */
775char **get_next_vinum_conf_line(FILE * f, int *argc)
776{
777 int cnt = 0;
778 static char *argv[64];
779 char **ap;
780 char *line = NULL;
781
782 mr_getline(line, f);
783 if (feof(f)) {
784 log_it("[GNVCL] Uh... I reached the EOF.");
785 return 0;
786 }
787
788 for (ap = argv; (*ap = strsep(&line, " \t")) != NULL;)
789 if (**ap != '\0') {
790 if (++ap >= &argv[64])
791 break;
792 cnt++;
793 }
794 mr_free(line);
795
796 if (strchr(argv[cnt - 1], '\n')) {
797 *(strchr(argv[cnt - 1], '\n')) = '\0';
798 }
799
800 if (argc)
801 *argc = cnt;
802 return argv;
803}
804
805/**
806 * For internal use only.
807 */
808char *get_option_val(int argc, char **argv, char *option)
809{
810 int i;
811 for (i = 0; i < (argc - 1); ++i) {
812 if (!strcmp(argv[i], option)) {
813 return argv[i + 1];
814 }
815 }
816 return 0;
817}
818
819/**
820 * For internal use only.
821 */
822char **get_option_vals(int argc, char **argv, char *option, int nval)
823{
824 int i, j;
825 static char **ret;
826 ret = (char **) malloc(nval * sizeof(char *));
827 for (i = 0; i < (argc - nval); ++i) {
828 if (!strcmp(argv[i], option)) {
829 for (j = 0; j < nval; ++j) {
830 ret[j] = (char *) malloc(strlen(argv[i + j + 1]) + 1);
831 strcpy(ret[j], argv[i + j + 1]);
832 }
833 return ret;
834 }
835 }
836 return 0;
837}
838
839/**
840 * For internal use only.
841 */
842bool get_option_state(int argc, char **argv, char *option)
843{
844 int i;
845 for (i = 0; i < argc; ++i)
846 if (!strcmp(argv[i], option))
847 return TRUE;
848
849 return FALSE;
850}
851
852/**
853 * Taken from Vinum source -- for internal use only.
854 */
855long long size_spec(char *spec)
856{
857 u_int64_t size;
858 char *s;
859 int sign = 1; /* -1 if negative */
860
861 size = 0;
862 if (spec != NULL) { /* we have a parameter */
863 s = spec;
864 if (*s == '-') { /* negative, */
865 sign = -1;
866 s++; /* skip */
867 }
868 if ((*s >= '0') && (*s <= '9')) { /* it's numeric */
869 while ((*s >= '0') && (*s <= '9')) /* it's numeric */
870 size = size * 10 + *s++ - '0'; /* convert it */
871 switch (*s) {
872 case '\0':
873 return size * sign;
874
875 case 'B':
876 case 'b':
877 case 'S':
878 case 's':
879 return size * sign * 512;
880
881 case 'K':
882 case 'k':
883 return size * sign * 1024;
884
885 case 'M':
886 case 'm':
887 return size * sign * 1024 * 1024;
888
889 case 'G':
890 case 'g':
891 return size * sign * 1024 * 1024 * 1024;
892
893 case 'T':
894 case 't':
895 log_it
896 ("Ok, I'm scared... Someone did a TERABYTE+ size-spec");
897 return size * sign * 1024 * 1024 * 1024 * 1024;
898
899 case 'P':
900 case 'p':
901 log_it
902 ("If I was scared last time, I'm freaked out now. Someone actually has a PETABYTE?!?!?!?!");
903 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024;
904
905 case 'E':
906 case 'e':
907 log_it
908 ("Okay, I'm REALLY freaked out. Who could devote a whole EXABYTE to their data?!?!");
909 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
910 1024;
911
912 case 'Z':
913 case 'z':
914 log_it
915 ("WHAT!?!? A ZETABYTE!?!? You've GOT to be kidding me!!!");
916 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
917 1024 * 1024;
918
919 case 'Y':
920 case 'y':
921 log_it
922 ("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?!?!");
923 popup_and_OK
924 ("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 :-)");
925 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
926 1024 * 1024 * 1024;
927 }
928 }
929 }
930 return size * sign;
931}
932
933#endif
934
935
936
937
938int parse_mdstat(char *mdstat_fname, struct raidlist_itself *raidlist, char *device_prefix) {
939
940const char delims[] = " ";
941
942FILE *fin = NULL;
943int res = 0, row, i, index_min;
944int v = 0;
945int lastpos = 0;
946size_t len = 0;
947char *token = NULL;
948char *string = NULL;
949char *cmd = NULL;
950char *pos = NULL;
951char type;
952char *strtmp = NULL;
953char *strtmp2 = NULL;
954
955// open file
956if (!(fin = fopen(mdstat_fname, "r"))) {
957 log_msg(1, "Could not open %s.", mdstat_fname);
958 return 1;
959}
960// initialise record, build progress and row counters
961raidlist->entries = 0;
962raidlist->el[raidlist->entries].progress = 999;
963row = 1;
964// skip first output row - contains registered RAID levels
965res = getline(&strtmp2, &len, fin);
966mr_free(strtmp2);
967// parse the rest
968while ( !feof_unlocked(fin) ) {
969 res = getline(&string, &len, fin);
970 log_msg(8, "mdstat line '%s' read.", string);
971 if (res <= 0) break;
972 // trim spaces
973 strip_spaces(string);
974 log_msg(8, "mdstat line 2 '%s' read.", string);
975 // if we have newline after only spaces, this is a blank line, update
976 // counters, otherwise do normal parsing
977 if (!strcmp(string,"")) {
978 row = 1;
979 raidlist->entries++;
980 raidlist->el[raidlist->entries].progress = 999;
981 } else {
982 switch (row) {
983 case 1: // device information
984 // check whether last line of record and if so skip
985 log_msg(8, "This is the device line");
986 pos = strcasestr(string, "unused devices: ");
987 if (pos != NULL) {
988 break;
989 }
990 // tokenise string
991 token = mr_strtok(string, delims, &lastpos);
992 if (token == NULL) {
993 // should not happen !
994 break;
995 }
996 // get RAID device name
997 mr_asprintf(strtmp,"%s%s", device_prefix, token);
998 strncpy(raidlist->el[raidlist->entries].raid_device, strtmp, 63);
999 raidlist->el[raidlist->entries].raid_device[63] = '\0';
1000 mr_free(strtmp);
1001 mr_free(token);
1002 // store the UUID value in the additional_vars structure
1003 v = raidlist->el[raidlist->entries].additional_vars.entries;
1004 strcpy(raidlist->el[raidlist->entries].additional_vars.el[v].label, "UUID");
1005 mr_asprintf(cmd,"mdadm --detail %s | grep UUID | cut -d: -f2- | sed 's/^ *//'", raidlist->el[raidlist->entries].raid_device);
1006 strtmp = call_program_and_get_last_line_of_output(cmd);
1007 strcpy(raidlist->el[raidlist->entries].additional_vars.el[v].value, strtmp);
1008 mr_free(strtmp);
1009 v++;
1010 // store the Version value in the additional_vars structure
1011 strcpy(raidlist->el[raidlist->entries].additional_vars.el[v].label, "Version");
1012 mr_asprintf(cmd,"mdadm --detail %s | grep Version | cut -d: -f2- | sed 's/^ *//'", raidlist->el[raidlist->entries].raid_device);
1013 strtmp = call_program_and_get_last_line_of_output(cmd);
1014 strcpy(raidlist->el[raidlist->entries].additional_vars.el[v].value, strtmp);
1015 mr_free(strtmp);
1016 v++;
1017 raidlist->el[raidlist->entries].additional_vars.entries = v;
1018 // skip ':' and status
1019 token = mr_strtok(string, delims, &lastpos);
1020 if (token == NULL) {
1021 // should not happen !
1022 break;
1023 }
1024 mr_free(token);
1025 token = mr_strtok(string, delims, &lastpos);
1026 if (token == NULL) {
1027 // should not happen !
1028 break;
1029 }
1030 if (!strcmp(token, "inactive")) {
1031 log_msg(1, "RAID device '%s' inactive.", raidlist->el[raidlist->entries].raid_device);
1032 mr_free(string);
1033 mr_free(token);
1034 return 1;
1035 }
1036 mr_free(token);
1037
1038 // get RAID level
1039 token = mr_strtok(string, delims, &lastpos);
1040 if (token == NULL) {
1041 // should not happen !
1042 break;
1043 }
1044 // skip potential auto-read-only entry
1045 if (!strcmp(token, "(auto-read-only)")) {
1046 mr_free(token);
1047 token = mr_strtok (string, delims, &lastpos);
1048 if (token == NULL) {
1049 // should not happen !
1050 break;
1051 }
1052 }
1053 if (!strcmp(token, "multipath")) {
1054 raidlist->el[raidlist->entries].raid_level = -2;
1055 } else if (!strcmp(token, "linear")) {
1056 raidlist->el[raidlist->entries].raid_level = -1;
1057 } else if (!strcmp(token, "raid0")) {
1058 raidlist->el[raidlist->entries].raid_level = 0;
1059 } else if (!strcmp(token, "raid1")) {
1060 raidlist->el[raidlist->entries].raid_level = 1;
1061 } else if (!strcmp(token, "raid4")) {
1062 raidlist->el[raidlist->entries].raid_level = 4;
1063 } else if (!strcmp(token, "raid5")) {
1064 raidlist->el[raidlist->entries].raid_level = 5;
1065 } else if (!strcmp(token, "raid6")) {
1066 raidlist->el[raidlist->entries].raid_level = 6;
1067 } else if (!strcmp(token, "raid10")) {
1068 raidlist->el[raidlist->entries].raid_level = 10;
1069 } else {
1070 log_msg(1, "Unknown RAID level '%s'", token);
1071 mr_free(string);
1072 mr_free(token);
1073 return 1;
1074 }
1075 mr_free(token);
1076
1077 // get RAID devices (type, index, device)
1078 // Note: parity disk for RAID4 is last normal disk, there is no '(P)'
1079 raidlist->el[raidlist->entries].data_disks.entries = 0;
1080 raidlist->el[raidlist->entries].spare_disks.entries = 0;
1081 raidlist->el[raidlist->entries].failed_disks.entries = 0;
1082 while((token = mr_strtok (string, delims, &lastpos))) {
1083 if ((pos = strstr(token, "("))) {
1084 type = *(pos+1);
1085 } else {
1086 type = ' ';
1087 }
1088 pos = strstr(token, "[");
1089 *pos = '\0';
1090 switch(type) {
1091 case ' ': // normal data disks
1092 raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].index = atoi(pos + 1);
1093 mr_asprintf(strtmp,"%s%s", device_prefix, token);
1094 strcpy(raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].device, strtmp);
1095 mr_free(strtmp);
1096 raidlist->el[raidlist->entries].data_disks.entries++;
1097 break;
1098 case 'S': // spare disks
1099 raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].index = atoi(pos + 1);
1100 mr_asprintf(strtmp,"%s%s", device_prefix, token);
1101 strcpy(raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].device, strtmp);
1102 mr_free(strtmp);
1103 raidlist->el[raidlist->entries].spare_disks.entries++;
1104 break;
1105 case 'F': // failed disks
1106 raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].index = atoi(pos + 1);
1107 mr_asprintf(strtmp,"%s%s", device_prefix, token);
1108 strcpy(raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].device, strtmp);
1109 mr_free(strtmp);
1110 raidlist->el[raidlist->entries].failed_disks.entries++;
1111 log_it("At least one failed disk found in RAID array.\n");
1112 break;
1113 default: // error
1114 log_msg(1, "Unknown device type '%c'", type);
1115 mr_free(string);
1116 mr_free(token);
1117 return 1;
1118 break;
1119 }
1120 mr_free(token);
1121 }
1122
1123 // adjust index for each device so that it starts with 0 for every type
1124 index_min = 99;
1125 for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
1126 if (raidlist->el[raidlist->entries].data_disks.el[i].index < index_min) {
1127 index_min = raidlist->el[raidlist->entries].data_disks.el[i].index;
1128 }
1129 }
1130 if (index_min > 0) {
1131 for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
1132 raidlist->el[raidlist->entries].data_disks.el[i].index = raidlist->el[raidlist->entries].data_disks.el[i].index - index_min;
1133 }
1134 }
1135 index_min = 99;
1136 for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
1137 if (raidlist->el[raidlist->entries].spare_disks.el[i].index < index_min) {
1138 index_min = raidlist->el[raidlist->entries].spare_disks.el[i].index;
1139 }
1140 }
1141 if (index_min > 0) {
1142 for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
1143 raidlist->el[raidlist->entries].spare_disks.el[i].index = raidlist->el[raidlist->entries].spare_disks.el[i].index - index_min;
1144 }
1145 }
1146 index_min = 99;
1147 for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
1148 if (raidlist->el[raidlist->entries].failed_disks.el[i].index < index_min) {
1149 index_min = raidlist->el[raidlist->entries].failed_disks.el[i].index;
1150 }
1151 }
1152 if (index_min > 0) {
1153 for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
1154 raidlist->el[raidlist->entries].failed_disks.el[i].index = raidlist->el[raidlist->entries].failed_disks.el[i].index - index_min;
1155 }
1156 }
1157 break;
1158 case 2: // config information
1159 // check for persistent super block
1160 if (strcasestr(string, "super non-persistent")) {
1161 raidlist->el[raidlist->entries].persistent_superblock = 0;
1162 } else {
1163 raidlist->el[raidlist->entries].persistent_superblock = 1;
1164 }
1165 // extract chunk size
1166 if (!(pos = strcasestr(string, "k chunk"))) {
1167 raidlist->el[raidlist->entries].chunk_size = -1;
1168 } else {
1169 while (*pos != ' ') {
1170 pos -= 1;
1171 if (pos < string) {
1172 log_it("String underflow!\n");
1173 mr_free(string);
1174 return 1;
1175 }
1176 }
1177 raidlist->el[raidlist->entries].chunk_size = atoi(pos + 1);
1178 }
1179 // extract parity if present
1180 if ((pos = strcasestr(string, "algorithm"))) {
1181 raidlist->el[raidlist->entries].parity = atoi(pos + 9);
1182 } else {
1183 raidlist->el[raidlist->entries].parity = -1;
1184 }
1185 break;
1186 case 3: // optional build status information
1187 if (!(pos = strchr(string, '\%'))) {
1188 if (strcasestr(string, "delayed")) {
1189 raidlist->el[raidlist->entries].progress = -1; // delayed (therefore, stuck at 0%)
1190 } else {
1191 raidlist->el[raidlist->entries].progress = 999; // not found
1192 }
1193 } else {
1194 while (*pos != ' ') {
1195 pos -= 1;
1196 if (pos < string) {
1197 printf("ERROR: String underflow!\n");
1198 mr_free(string);
1199 return 1;
1200 }
1201 }
1202 raidlist->el[raidlist->entries].progress = atoi(pos);
1203 }
1204 break;
1205 default: // error or IN PROGRESS
1206 if (raidlist->el[raidlist->entries].progress != -1 &&
1207 raidlist->el[raidlist->entries].progress != 999) {
1208 log_msg(1, "Row %d should not occur in record!", row);
1209 }
1210 break;
1211 }
1212 row++;
1213 }
1214 // free string
1215 mr_free(string);
1216}
1217// close file
1218fclose(fin);
1219// return success
1220return 0;
1221
1222}
1223
1224
1225
1226
1227int create_raidtab_from_mdstat(char *mdstat_fname,char *raidtab_fname)
1228{
1229 struct raidlist_itself *raidlist;
1230 int retval = 0;
1231
1232 raidlist = malloc(sizeof(struct raidlist_itself));
1233
1234 // FIXME: Prefix '/dev/' should really be dynamic!
1235 if (parse_mdstat(mdstat_fname,raidlist, "/dev/")) {
1236 log_to_screen("Sorry, cannot read %s", mdstat_fname);
1237 return (1);
1238 }
1239
1240 retval += save_raidlist_to_raidtab(raidlist, raidtab_fname);
1241 return (retval);
1242}
1243
1244
1245
1246/* @} - end of raidGroup */
Note: See TracBrowser for help on using the repository browser.