source: MondoRescue/branches/3.0/mondo/src/common/libmondo-raid.c@ 3043

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