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

Last change on this file since 729 was 689, checked in by bcornec, 18 years ago

Still other memory management improvements ( I hope :-)

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