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

Last change on this file was 1176, checked in by Bruno Cornec, 17 years ago

Some merges from stable (synchro for mem. mngt)

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