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

Last change on this file since 672 was 672, checked in by bcornec, 18 years ago

merge -r651:671 $SVN_M/branches/stable

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