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

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

MDSTAT_FILE is used everywhere (replacing /proc/mdstat strings)

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