source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-raid.c@ 2323

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

r3334@localhost: bruno | 2009-08-08 12:17:37 +0200

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