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

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

r3369@localhost: bruno | 2009-08-18 16:57:27 +0200

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