source: MondoRescue/branches/stable/mondo/src/common/libmondo-raid.c@ 1172

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

Suppress res variable and returns from mr_getline which is void

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