source: MondoRescue/trunk/mondo/src/common/libmondo-raid.c@ 1106

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

merge -r1082:1105 $SVN_M/branches/stable

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