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

Last change on this file since 783 was 783, checked in by Bruno Cornec, 18 years ago
  • Massive rewrite continues for memory management.
  • main structure should now have all parameters allocated dynamically
  • new lib libmr.a + dir + build process reviewed to support it.
  • new include subdir to host external definitions of the new lib
  • code now compiles. Still one remaining link issues for mondorestore. This should allow for some tests soon.

(goal is to separate completely reviewed code and functions and provide clean interfaces)

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