source: MondoRescue/branches/2.2.9/mondo/src/common/libmondo-raid.c@ 2230

Last change on this file since 2230 was 2211, checked in by Bruno Cornec, 15 years ago

r3089@localhost: bruno | 2009-05-18 06:41:05 +0200

  • move call to asprintf to call to mr_asprintf (suppress a compiler warning)
  • remove all the most obvious bad call to strcat and replace by mr_strcat as appropriate
  • Property svn:keywords set to Id
File size: 32.5 KB
Line 
1/* libmondo-raid.c subroutines for handling RAID
2 $Id: libmondo-raid.c 2211 2009-06-03 17:10:19Z 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_string.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 2211 2009-06-03 17:10:19Z bruno $";
30
31
32/**
33 * @addtogroup raidGroup
34 * @{
35 */
36/**
37 * See if a particular RAID level is supported by the kernel.
38 * @param raidno The RAID level (-1 through 5) to check. -1 means "linear" under Linux and
39 * "concatenated" under FreeBSD. It's really the same thing, just different wording.
40 * @return TRUE if it's supported, FALSE if not.
41 */
42bool is_this_raid_personality_registered(int raidno)
43{
44#ifdef __FreeBSD__
45 return ((raidno == -1) || (raidno == 0) || (raidno == 1)
46 || (raidno == 5)) ? TRUE : FALSE;
47#else
48 /*@ buffer ********************************************************** */
49 char *command = NULL;
50 int res;
51
52 mr_asprintf(&command, "grep \"");
53 if (raidno == -1) {
54 mr_strcat(command, "linear");
55 } else {
56 mr_strcat(command, "raid%d", raidno);
57 }
58 mr_strcat(command, "\" /proc/mdstat > /dev/null 2> /dev/null");
59 log_it("Is raid %d registered? Command = '%s'", raidno, command);
60 res = system(command);
61 paranoid_free(command);
62
63 if (res) {
64 return (FALSE);
65 } else {
66 return (TRUE);
67 }
68#endif
69}
70
71
72
73
74
75
76/**
77 * Search for @p device in @p disklist.
78 * @param disklist The disklist to search in.
79 * @param device The device to search for.
80 * @return The index number of @p device, or -1 if it does not exist.
81 */
82int
83where_in_drivelist_is_drive(struct list_of_disks *disklist, char *device)
84{
85
86 /*@ int ************************************************************* */
87 int i = 0;
88
89 assert(disklist != NULL);
90 assert_string_is_neither_NULL_nor_zerolength(device);
91
92 for (i = 0; i < disklist->entries; i++) {
93 if (!strcmp(disklist->el[i].device, device)) {
94 break;
95 }
96 }
97 if (i == disklist->entries) {
98 return (-1);
99 } else {
100 return (i);
101 }
102}
103
104
105
106
107
108
109
110
111/**
112 * Determine which RAID device is using a particular partition.
113 * @param raidlist The RAID information structure.
114 * @param device The partition to find out about.
115 * @return The index number of the RAID device using @p device, or -1 if there is none.
116 */
117int
118which_raid_device_is_using_this_partition(struct raidlist_itself *raidlist,
119 char *device)
120{
121#ifdef __FreeBSD__
122// FreeBSD-specific version of which_raid_device_is_using_this_partition()
123 /*@ int ********************************************************* */
124 int i = 0;
125
126 for (i = 0; i < raidlist->entries; i++) {
127 bool thisone = FALSE;
128 int j, k, l;
129
130 for (j = 0; j < raidlist->el[i].plexes; ++j) {
131 for (k = 0; k < raidlist->el[i].plex[j].subdisks; ++k) {
132 for (l = 0; l < raidlist->disks.entries; ++l) {
133 if (!strcmp(raidlist->disks.el[l].device,
134 device) &&
135 !strcmp(raidlist->el[i].plex[j].sd[k].which_device,
136 raidlist->disks.el[l].name))
137 thisone = TRUE;
138 }
139 }
140 }
141
142 if (thisone) {
143 break;
144 }
145 }
146 if (i == raidlist->entries) {
147 return (-1);
148 } else {
149 return (i);
150 }
151}
152
153#else
154// Linux-specific version of which_raid_device_is_using_this_partition()
155// and one other function which FreeBSD doesn't use
156
157 int current_raiddev = 0;
158
159 assert_string_is_neither_NULL_nor_zerolength(device);
160 assert(raidlist != NULL);
161
162 for (current_raiddev = 0; current_raiddev < raidlist->entries;
163 current_raiddev++) {
164 if (where_in_drivelist_is_drive
165 (&raidlist->el[current_raiddev].data_disks, device) >= 0
166 || where_in_drivelist_is_drive(&raidlist->el[current_raiddev].
167 spare_disks, device) >= 0
168 || where_in_drivelist_is_drive(&raidlist->el[current_raiddev].
169 parity_disks, device) >= 0
170 || where_in_drivelist_is_drive(&raidlist->el[current_raiddev].
171 failed_disks, device) >= 0) {
172 break;
173 }
174 }
175 if (current_raiddev == raidlist->entries) {
176 return (-1);
177 } else {
178 return (current_raiddev);
179 }
180}
181
182/**
183 * Write an @c int variable to a list of RAID variables.
184 * @param raidrec The RAID device record to write to.
185 * @param lino The variable index number to modify/create.
186 * @param label The label to write.
187 * @param value The value to write.
188 */
189void
190write_variableINT_to_raid_var_line(struct raid_device_record *raidrec,
191 int lino, char *label, int value)
192{
193 /*@ buffers ***************************************************** */
194 char *sz_value;
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 paranoid_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 paranoid_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 paranoid_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 paranoid_free(tmp);
484 return (0);
485 }
486 if (!(fin = fopen(fname, "r"))) {
487 log_it("Cannot open raidtab");
488 paranoid_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 paranoid_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 paranoid_free(tmp);
620 paranoid_free(label);
621 paranoid_free(value);
622 return (0);
623 }
624 if (!(fin = fopen(fname, "r"))) {
625 log_it("Cannot open raidtab");
626 paranoid_free(tmp);
627 paranoid_free(label);
628 paranoid_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 paranoid_free(tmp);
665 paranoid_free(label);
666 paranoid_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 paranoid_free(tmp);
766 paranoid_free(labelB);
767 paranoid_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 *) malloc(MAX_STR_LEN);
824 if (!line)
825 errx(1,
826 "unable to allocate %i bytes of memory for `char *line' at %s:%i",
827 MAX_STR_LEN, __FILE__, __LINE__);
828 (void) fgets(line, MAX_STR_LEN, f);
829 if (feof(f)) {
830 log_it("[GNVCL] Uh... I reached the EOF.");
831 return 0;
832 }
833
834 for (ap = argv; (*ap = strsep(&line, " \t")) != NULL;)
835 if (**ap != '\0') {
836 if (++ap >= &argv[64])
837 break;
838 cnt++;
839 }
840
841 if (strchr(argv[cnt - 1], '\n')) {
842 *(strchr(argv[cnt - 1], '\n')) = '\0';
843 }
844
845 if (argc)
846 *argc = cnt;
847 return argv;
848}
849
850/**
851 * For internal use only.
852 */
853char *get_option_val(int argc, char **argv, char *option)
854{
855 int i;
856 for (i = 0; i < (argc - 1); ++i) {
857 if (!strcmp(argv[i], option)) {
858 return argv[i + 1];
859 }
860 }
861 return 0;
862}
863
864/**
865 * For internal use only.
866 */
867char **get_option_vals(int argc, char **argv, char *option, int nval)
868{
869 int i, j;
870 static char **ret;
871 ret = (char **) malloc(nval * sizeof(char *));
872 for (i = 0; i < (argc - nval); ++i) {
873 if (!strcmp(argv[i], option)) {
874 for (j = 0; j < nval; ++j) {
875 ret[j] = (char *) malloc(strlen(argv[i + j + 1]) + 1);
876 strcpy(ret[j], argv[i + j + 1]);
877 }
878 return ret;
879 }
880 }
881 return 0;
882}
883
884/**
885 * For internal use only.
886 */
887bool get_option_state(int argc, char **argv, char *option)
888{
889 int i;
890 for (i = 0; i < argc; ++i)
891 if (!strcmp(argv[i], option))
892 return TRUE;
893
894 return FALSE;
895}
896
897/**
898 * Taken from Vinum source -- for internal use only.
899 */
900long long size_spec(char *spec)
901{
902 u_int64_t size;
903 char *s;
904 int sign = 1; /* -1 if negative */
905
906 size = 0;
907 if (spec != NULL) { /* we have a parameter */
908 s = spec;
909 if (*s == '-') { /* negative, */
910 sign = -1;
911 s++; /* skip */
912 }
913 if ((*s >= '0') && (*s <= '9')) { /* it's numeric */
914 while ((*s >= '0') && (*s <= '9')) /* it's numeric */
915 size = size * 10 + *s++ - '0'; /* convert it */
916 switch (*s) {
917 case '\0':
918 return size * sign;
919
920 case 'B':
921 case 'b':
922 case 'S':
923 case 's':
924 return size * sign * 512;
925
926 case 'K':
927 case 'k':
928 return size * sign * 1024;
929
930 case 'M':
931 case 'm':
932 return size * sign * 1024 * 1024;
933
934 case 'G':
935 case 'g':
936 return size * sign * 1024 * 1024 * 1024;
937
938 case 'T':
939 case 't':
940 log_it
941 ("Ok, I'm scared... Someone did a TERABYTE+ size-spec");
942 return size * sign * 1024 * 1024 * 1024 * 1024;
943
944 case 'P':
945 case 'p':
946 log_it
947 ("If I was scared last time, I'm freaked out now. Someone actually has a PETABYTE?!?!?!?!");
948 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024;
949
950 case 'E':
951 case 'e':
952 log_it
953 ("Okay, I'm REALLY freaked out. Who could devote a whole EXABYTE to their data?!?!");
954 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
955 1024;
956
957 case 'Z':
958 case 'z':
959 log_it
960 ("WHAT!?!? A ZETABYTE!?!? You've GOT to be kidding me!!!");
961 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
962 1024 * 1024;
963
964 case 'Y':
965 case 'y':
966 log_it
967 ("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?!?!");
968 popup_and_OK
969 ("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 :-)");
970 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
971 1024 * 1024 * 1024;
972 }
973 }
974 }
975 return size * sign;
976}
977
978#endif
979
980
981
982
983int parse_mdstat(struct raidlist_itself *raidlist, char *device_prefix) {
984
985 const char delims[] = " ";
986
987 FILE *fin;
988 int res = 0, row, i, index_min;
989 int lastpos = 0;
990 size_t len = 0;
991 char *token;
992 char *string = NULL;
993 char *pos;
994 char type;
995 char *strtmp;
996
997 // open file
998 if (!(fin = fopen(MDSTAT_FILE, "r"))) {
999 log_msg(1, "Could not open %s.\n", MDSTAT_FILE);
1000 return 1;
1001 }
1002 // initialise record, build progress and row counters
1003 raidlist->entries = 0;
1004 raidlist->el[raidlist->entries].progress = 999;
1005 row = 1;
1006 // skip first output row - contains registered RAID levels
1007 res = getline(&string, &len, fin);
1008 // parse the rest
1009 while ( !feof_unlocked(fin) ) {
1010 res = getline(&string, &len, fin);
1011 if (res <= 0) break;
1012 // trim leading spaces
1013 pos = string;
1014 while (*pos == ' ') pos += 1;
1015 mr_asprintf(&strtmp, "%s", pos);
1016 strcpy(string, strtmp);
1017 paranoid_free(strtmp);
1018 // if we have newline after only spaces, this is a blank line, update
1019 // counters, otherwise do normal parsing
1020 if (*string == '\n') {
1021 row = 1;
1022 raidlist->entries++;
1023 raidlist->el[raidlist->entries].progress = 999;
1024 } else {
1025 switch (row) {
1026 case 1: // device information
1027 // check whether last line of record and if so skip
1028 pos = strcasestr(string, "unused devices: ");
1029 if (pos == string) {
1030 //raidlist->entries--;
1031 break;
1032 }
1033 // tokenise string
1034 token = mr_strtok (string, delims, &lastpos);
1035 // get RAID device name
1036 mr_asprintf(&strtmp,"%s%s", device_prefix, token);
1037 strcpy(raidlist->el[raidlist->entries].raid_device, strtmp);
1038 paranoid_free(strtmp);
1039 paranoid_free(token);
1040 // skip ':' and status
1041 token = mr_strtok (string, delims, &lastpos);
1042 paranoid_free(token);
1043 token = mr_strtok (string, delims, &lastpos);
1044 if (!strcmp(token, "inactive")) {
1045 log_msg(1, "RAID device '%s' inactive.\n",
1046 raidlist->el[raidlist->entries].raid_device);
1047 paranoid_free(string);
1048 paranoid_free(token);
1049 return 1;
1050 }
1051 paranoid_free(token);
1052
1053 // get RAID level
1054 token = mr_strtok (string, delims, &lastpos);
1055 if (!strcmp(token, "multipath")) {
1056 raidlist->el[raidlist->entries].raid_level = -2;
1057 } else if (!strcmp(token, "linear")) {
1058 raidlist->el[raidlist->entries].raid_level = -1;
1059 } else if (!strcmp(token, "raid0")) {
1060 raidlist->el[raidlist->entries].raid_level = 0;
1061 } else if (!strcmp(token, "raid1")) {
1062 raidlist->el[raidlist->entries].raid_level = 1;
1063 } else if (!strcmp(token, "raid4")) {
1064 raidlist->el[raidlist->entries].raid_level = 4;
1065 } else if (!strcmp(token, "raid5")) {
1066 raidlist->el[raidlist->entries].raid_level = 5;
1067 } else if (!strcmp(token, "raid6")) {
1068 raidlist->el[raidlist->entries].raid_level = 6;
1069 } else if (!strcmp(token, "raid10")) {
1070 raidlist->el[raidlist->entries].raid_level = 10;
1071 } else {
1072 log_msg(1, "Unknown RAID level '%s'.\n", token);
1073 paranoid_free(string);
1074 paranoid_free(token);
1075 return 1;
1076 }
1077 paranoid_free(token);
1078
1079 // get RAID devices (type, index, device)
1080 // Note: parity disk for RAID4 is last normal disk, there is no '(P)'
1081 raidlist->el[raidlist->entries].data_disks.entries = 0;
1082 raidlist->el[raidlist->entries].spare_disks.entries = 0;
1083 raidlist->el[raidlist->entries].failed_disks.entries = 0;
1084 while((token = mr_strtok (string, delims, &lastpos))) {
1085 if ((pos = strstr(token, "("))) {
1086 type = *(pos+1);
1087 } else {
1088 type = ' ';
1089 }
1090 pos = strstr(token, "[");
1091 *pos = '\0';
1092 switch(type) {
1093 case ' ': // normal data disks
1094 raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].index = atoi(pos + 1);
1095 mr_asprintf(&strtmp,"%s%s", device_prefix, token);
1096 strcpy(raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].device, strtmp);
1097 paranoid_free(strtmp);
1098 raidlist->el[raidlist->entries].data_disks.entries++;
1099 break;
1100 case 'S': // spare disks
1101 raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].index = atoi(pos + 1);
1102 mr_asprintf(&strtmp,"%s%s", device_prefix, token);
1103 strcpy(raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].device, strtmp);
1104 paranoid_free(strtmp);
1105 raidlist->el[raidlist->entries].spare_disks.entries++;
1106 break;
1107 case 'F': // failed disks
1108 raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].index = atoi(pos + 1);
1109 mr_asprintf(&strtmp,"%s%s", device_prefix, token);
1110 strcpy(raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].device, strtmp);
1111 paranoid_free(strtmp);
1112 raidlist->el[raidlist->entries].failed_disks.entries++;
1113 log_it("At least one failed disk found in RAID array.\n");
1114 break;
1115 default: // error
1116 log_msg(1, "Unknown device type '%c'\n", type);
1117 paranoid_free(string);
1118 paranoid_free(token);
1119 return 1;
1120 break;
1121 }
1122 paranoid_free(token);
1123 }
1124
1125 // adjust index for each device so that it starts with 0 for every type
1126 index_min = 99;
1127 for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
1128 if (raidlist->el[raidlist->entries].data_disks.el[i].index < index_min) {
1129 index_min = raidlist->el[raidlist->entries].data_disks.el[i].index;
1130 }
1131 }
1132 if (index_min > 0) {
1133 for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
1134 raidlist->el[raidlist->entries].data_disks.el[i].index = raidlist->el[raidlist->entries].data_disks.el[i].index - index_min;
1135 }
1136 }
1137 index_min = 99;
1138 for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
1139 if (raidlist->el[raidlist->entries].spare_disks.el[i].index < index_min) {
1140 index_min = raidlist->el[raidlist->entries].spare_disks.el[i].index;
1141 }
1142 }
1143 if (index_min > 0) {
1144 for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
1145 raidlist->el[raidlist->entries].spare_disks.el[i].index = raidlist->el[raidlist->entries].spare_disks.el[i].index - index_min;
1146 }
1147 }
1148 index_min = 99;
1149 for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
1150 if (raidlist->el[raidlist->entries].failed_disks.el[i].index < index_min) {
1151 index_min = raidlist->el[raidlist->entries].failed_disks.el[i].index;
1152 }
1153 }
1154 if (index_min > 0) {
1155 for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
1156 raidlist->el[raidlist->entries].failed_disks.el[i].index = raidlist->el[raidlist->entries].failed_disks.el[i].index - index_min;
1157 }
1158 }
1159 break;
1160 case 2: // config information
1161 // check for persistent super block
1162 if (strcasestr(string, "super non-persistent")) {
1163 raidlist->el[raidlist->entries].persistent_superblock = 0;
1164 } else {
1165 raidlist->el[raidlist->entries].persistent_superblock = 1;
1166 }
1167 // extract chunk size
1168 if (!(pos = strcasestr(string, "k chunk"))) {
1169 raidlist->el[raidlist->entries].chunk_size = -1;
1170 } else {
1171 while (*pos != ' ') {
1172 pos -= 1;
1173 if (pos < string) {
1174 log_it("String underflow!\n");
1175 paranoid_free(string);
1176 return 1;
1177 }
1178 }
1179 raidlist->el[raidlist->entries].chunk_size = atoi(pos + 1);
1180 }
1181 // extract parity if present
1182 if ((pos = strcasestr(string, "algorithm"))) {
1183 raidlist->el[raidlist->entries].parity = atoi(pos + 9);
1184 } else {
1185 raidlist->el[raidlist->entries].parity = -1;
1186 }
1187 break;
1188 case 3: // optional build status information
1189 if (!(pos = strchr(string, '\%'))) {
1190 if (strcasestr(string, "delayed")) {
1191 raidlist->el[raidlist->entries].progress = -1; // delayed (therefore, stuck at 0%)
1192 } else {
1193 raidlist->el[raidlist->entries].progress = 999; // not found
1194 }
1195 } else {
1196 while (*pos != ' ') {
1197 pos -= 1;
1198 if (pos < string) {
1199 printf("ERROR: String underflow!\n");
1200 paranoid_free(string);
1201 return 1;
1202 }
1203 }
1204 raidlist->el[raidlist->entries].progress = atoi(pos);
1205 }
1206 break;
1207 default: // error or IN PROGRESS
1208 if (raidlist->el[raidlist->entries].progress != -1 &&
1209 raidlist->el[raidlist->entries].progress != 999) {
1210 log_msg(1, "Row %d should not occur in record!\n", row);
1211 }
1212 break;
1213 }
1214 row++;
1215 }
1216 }
1217 // close file
1218 fclose(fin);
1219 // free string
1220 paranoid_free(string);
1221 // return success
1222 return 0;
1223
1224}
1225
1226
1227
1228
1229int create_raidtab_from_mdstat(char *raidtab_fname)
1230{
1231 struct raidlist_itself *raidlist;
1232 int retval = 0;
1233
1234 raidlist = malloc(sizeof(struct raidlist_itself));
1235
1236 // FIXME: Prefix '/dev/' should really be dynamic!
1237 if (parse_mdstat(raidlist, "/dev/")) {
1238 log_to_screen("Sorry, cannot read %s", MDSTAT_FILE);
1239 return (1);
1240 }
1241
1242 retval += save_raidlist_to_raidtab(raidlist, raidtab_fname);
1243 return (retval);
1244}
1245
1246
1247
1248/* @} - end of raidGroup */
Note: See TracBrowser for help on using the repository browser.