source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-raid.c@ 2517

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