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

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