source: MondoRescue/branches/stable/mondo/src/common/libmondo-raid.c@ 1075

Last change on this file since 1075 was 1075, checked in by Bruno Cornec, 17 years ago

Improvements for USB support in mindi when called from mondo
Changelogs in C files removed from common

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