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

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

Suppress lib-common-externs.h useless

  • 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 1116 2007-02-09 00:02:11Z 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"
[1]20
21#ifdef __FreeBSD__
22/* Nonstandard library functions: */
[128]23extern void errx(int exitval, const char *fmt, ...);
24extern char *strsep(char **stringp, const char *delim);
[1]25#endif
26
27/*@unused@*/
[128]28//static char cvsid[] = "$Id: libmondo-raid.c 1116 2007-02-09 00:02:11Z bruno $";
[1]29
30
31/**
32 * @addtogroup raidGroup
33 * @{
34 */
35/**
36 * See if a particular RAID level is supported by the kernel.
37 * @param raidno The RAID level (-1 through 5) to check. -1 means "linear" under Linux and
38 * "concatenated" under FreeBSD. It's really the same thing, just different wording.
39 * @return TRUE if it's supported, FALSE if not.
40 */
[128]41bool is_this_raid_personality_registered(int raidno)
[1]42{
43#ifdef __FreeBSD__
[128]44 return ((raidno == -1) || (raidno == 0) || (raidno == 1)
45 || (raidno == 5)) ? TRUE : FALSE;
[1]46#else
[128]47 /*@ buffer ********************************************************** */
48 char *command;
49 int res;
50
[1080]51 command = mr_malloc(MAX_STR_LEN * 2);
[273]52 strcpy(command, "grep \" /proc/mdstat");
[128]53 if (raidno == -1) {
54 strcat(command, "linear");
55 } else {
56 sprintf(command + strlen(command), "raid%d", raidno);
57 }
58 strcat(command, "\" > /dev/null 2> /dev/null");
59 log_it("Is raid %d registered? Command = '%s'", raidno, command);
60 res = system(command);
[1080]61 mr_free(command);
[128]62 if (res) {
63 return (FALSE);
64 } else {
65 return (TRUE);
66 }
[1]67#endif
68}
69
70
71
72
73
74
75/**
76 * Search for @p device in @p disklist.
77 * @param disklist The disklist to search in.
78 * @param device The device to search for.
79 * @return The index number of @p device, or -1 if it does not exist.
80 */
81int
[128]82where_in_drivelist_is_drive(struct list_of_disks *disklist, char *device)
[1]83{
84
[128]85 /*@ int ************************************************************* */
86 int i = 0;
[1]87
[128]88 assert(disklist != NULL);
89 assert_string_is_neither_NULL_nor_zerolength(device);
[1]90
[128]91 for (i = 0; i < disklist->entries; i++) {
92 if (!strcmp(disklist->el[i].device, device)) {
93 break;
94 }
[1]95 }
[128]96 if (i == disklist->entries) {
97 return (-1);
98 } else {
99 return (i);
100 }
[1]101}
102
103
104
105
106
107
108
109
110/**
111 * Determine which RAID device is using a particular partition.
112 * @param raidlist The RAID information structure.
113 * @param device The partition to find out about.
114 * @return The index number of the RAID device using @p device, or -1 if there is none.
115 */
116int
[128]117which_raid_device_is_using_this_partition(struct raidlist_itself *raidlist,
118 char *device)
[1]119{
120#ifdef __FreeBSD__
121// FreeBSD-specific version of which_raid_device_is_using_this_partition()
[128]122 /*@ int ********************************************************* */
123 int i = 0;
[1]124
[128]125 for (i = 0; i < raidlist->entries; i++) {
126 bool thisone = FALSE;
127 int j, k, l;
128
129 for (j = 0; j < raidlist->el[i].plexes; ++j) {
130 for (k = 0; k < raidlist->el[i].plex[j].subdisks; ++k) {
131 for (l = 0; l < raidlist->disks.entries; ++l) {
132 if (!strcmp(raidlist->disks.el[l].device,
133 device) &&
134 !strcmp(raidlist->el[i].plex[j].sd[k].which_device,
135 raidlist->disks.el[l].name))
136 thisone = TRUE;
137 }
138 }
[1]139 }
[128]140
141 if (thisone) {
142 break;
143 }
[1]144 }
[128]145 if (i == raidlist->entries) {
146 return (-1);
147 } else {
148 return (i);
[1]149 }
150}
151
152#else
153// Linux-specific version of which_raid_device_is_using_this_partition()
154// and one other function which FreeBSD doesn't use
155
[128]156 int current_raiddev = 0;
[1]157
[128]158 assert_string_is_neither_NULL_nor_zerolength(device);
159 assert(raidlist != NULL);
[1]160
[128]161 for (current_raiddev = 0; current_raiddev < raidlist->entries;
162 current_raiddev++) {
163 if (where_in_drivelist_is_drive
164 (&raidlist->el[current_raiddev].data_disks, device) >= 0
165 || where_in_drivelist_is_drive(&raidlist->el[current_raiddev].
166 spare_disks, device) >= 0
167 || where_in_drivelist_is_drive(&raidlist->el[current_raiddev].
168 parity_disks, device) >= 0
169 || where_in_drivelist_is_drive(&raidlist->el[current_raiddev].
170 failed_disks, device) >= 0) {
171 break;
172 }
[1]173 }
[128]174 if (current_raiddev == raidlist->entries) {
175 return (-1);
176 } else {
177 return (current_raiddev);
178 }
[1]179}
180
181/**
182 * Write an @c int variable to a list of RAID variables.
183 * @param raidrec The RAID device record to write to.
184 * @param lino The variable index number to modify/create.
185 * @param label The label to write.
186 * @param value The value to write.
187 */
188void
[128]189write_variableINT_to_raid_var_line(struct raid_device_record *raidrec,
190 int lino, char *label, int value)
[1]191{
[128]192 /*@ buffers ***************************************************** */
193 char *sz_value;
[1]194
[128]195 malloc_string(sz_value);
196 assert(raidrec != NULL);
197 assert(label != NULL);
[1]198
[128]199 sprintf(sz_value, "%d", value);
200 strcpy(raidrec->additional_vars.el[lino].label, label);
201 strcpy(raidrec->additional_vars.el[lino].value, sz_value);
[1080]202 mr_free(sz_value);
[1]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 */
[128]219void add_disk_to_raid_device(struct vinum_plex *p, char *device_to_add)
[1]220{
[128]221 strcpy(p->sd[p->subdisks].which_device, device_to_add);
222 ++p->subdisks;
[1]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 */
[128]232void add_disk_to_raid_device(struct list_of_disks *disklist,
[1103]233 char *device_to_add, int idx)
[1]234{
[128]235 int items;
[1]236
[128]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);
[1103]241 disklist->el[items].index = idx;
[128]242 items++;
243 disklist->entries = items;
[1]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 */
[128]253void
254save_additional_vars_to_file(struct additional_raid_variables *vars,
255 FILE * fout)
[1]256{
[128]257 int i;
[1]258
[128]259 assert(vars != NULL);
260 assert(fout != NULL);
[1]261
[128]262 for (i = 0; i < vars->entries; i++) {
263 fprintf(fout, " %-21s %s\n", vars->el[i].label,
264 vars->el[i].value);
265 }
[1]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 */
[128]276int save_raidlist_to_raidtab(struct raidlist_itself *raidlist, char *fname)
[1]277{
[128]278 FILE *fout;
279 int current_raid_device;
[1]280#ifdef __FreeBSD__
[128]281 int i;
[1]282#else
283// Linux
284#endif
285
[128]286 assert(raidlist != NULL);
287 assert_string_is_neither_NULL_nor_zerolength(fname);
[1]288
[128]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");
[1]299
300#ifdef __FreeBSD__
[128]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 }
[1]310#endif
311
[128]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);
[1]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 */
[128]326void save_raidrec_to_file(struct
[1]327#ifdef __FreeBSD__
[128]328 vinum_volume
[1]329#else
[128]330 raid_device_record
[1]331#endif
[128]332 * raidrec, FILE * fout)
[1]333{
334#ifdef __FreeBSD__
[128]335 int i, j;
[1]336
[128]337 fprintf(fout, "\nvolume %s\n", raidrec->volname);
338 for (i = 0; i < raidrec->plexes; ++i) {
339 char org[24];
340 switch (raidrec->plex[i].raidlevel) {
341 case -1:
342 strcpy(org, "concat");
343 break;
344 case 0:
345 strcpy(org, "striped");
346 break;
347 case 5:
348 strcpy(org, "raid5");
349 break;
350 }
351 fprintf(fout, " plex org %s", org);
352 if (raidrec->plex[i].raidlevel != -1) {
353 fprintf(fout, " %ik", raidrec->plex[i].stripesize);
354 }
355 fprintf(fout, "\n");
356
357 for (j = 0; j < raidrec->plex[i].subdisks; ++j) {
358 fprintf(fout, " sd drive %s size 0\n",
359 raidrec->plex[i].sd[j].which_device);
360 }
[1]361 }
[128]362#else
363 assert(raidrec != NULL);
364 assert(fout != NULL);
365
366 fprintf(fout, "raiddev %s\n", raidrec->raid_device);
[558]367 if (raidrec->raid_level == -2) {
368 fprintf(fout, " raid-level multipath\n");
369 } else if (raidrec->raid_level == -1) {
[128]370 fprintf(fout, " raid-level linear\n");
371 } else {
372 fprintf(fout, " raid-level %d\n",
373 raidrec->raid_level);
[1]374 }
[128]375 fprintf(fout, " nr-raid-disks %d\n",
376 raidrec->data_disks.entries);
[558]377 if (raidrec->spare_disks.entries > 0) {
378 fprintf(fout, " nr-spare-disks %d\n",
379 raidrec->spare_disks.entries);
380 }
[128]381 if (raidrec->parity_disks.entries > 0) {
382 fprintf(fout, " nr-parity-disks %d\n",
383 raidrec->parity_disks.entries);
[1]384 }
[128]385 fprintf(fout, " persistent-superblock %d\n",
386 raidrec->persistent_superblock);
[558]387 if (raidrec->chunk_size > -1) {
388 fprintf(fout, " chunk-size %d\n", raidrec->chunk_size);
389 }
390 if (raidrec->parity > -1) {
391 switch(raidrec->parity) {
392 case 0:
393 fprintf(fout, " parity-algorithm left-asymmetric\n");
394 break;
395 case 1:
396 fprintf(fout, " parity-algorithm right-asymmetric\n");
397 break;
398 case 2:
399 fprintf(fout, " parity-algorithm left-symmetric\n");
400 break;
401 case 3:
402 fprintf(fout, " parity-algorithm right-symmetric\n");
403 break;
404 default:
405 fatal_error("Unknown RAID parity algorithm.");
406 break;
407 }
408 }
[128]409 save_additional_vars_to_file(&raidrec->additional_vars, fout);
410 fprintf(fout, "\n");
411 save_disklist_to_file("raid-disk", &raidrec->data_disks, fout);
412 save_disklist_to_file("spare-disk", &raidrec->spare_disks, fout);
413 save_disklist_to_file("parity-disk", &raidrec->parity_disks, fout);
414 save_disklist_to_file("failed-disk", &raidrec->failed_disks, fout);
415 fprintf(fout, "\n");
[1]416#endif
417}
418
419/**
420 * Retrieve the next line from a raidtab stream.
421 * @param fin The file to read the input from.
422 * @param label Where to put the line's label.
423 * @param value Where to put the line's value.
424 * @return 0 if the line was read and stored successfully, 1 if we're at end of file.
425 */
[128]426int get_next_raidtab_line(FILE * fin, char *label, char *value)
[1]427{
[128]428 char *incoming;
429 char *p;
[1]430
[128]431 malloc_string(incoming);
432 assert(fin != NULL);
433 assert(label != NULL);
434 assert(value != NULL);
[1]435
[128]436 label[0] = value[0] = '\0';
437 if (feof(fin)) {
[1080]438 mr_free(incoming);
[128]439 return (1);
[1]440 }
[128]441 for (fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin);
442 fgets(incoming, MAX_STR_LEN - 1, fin)) {
443 strip_spaces(incoming);
444 p = strchr(incoming, ' ');
445 if (strlen(incoming) < 3 || incoming[0] == '#' || !p) {
446 continue;
447 }
448 *(p++) = '\0';
449 while (*p == ' ') {
450 p++;
451 }
452 strcpy(label, incoming);
453 strcpy(value, p);
[1080]454 mr_free(incoming);
[128]455 return (0);
456 }
457 return (1);
[1]458}
459
460
461
462/**
463 * Load a raidtab file into a raidlist structure.
464 * @param raidlist The raidlist to fill.
465 * @param fname The file to read from.
466 * @return 0 for success, 1 for failure.
467 */
468#ifdef __FreeBSD__
[128]469int load_raidtab_into_raidlist(struct raidlist_itself *raidlist,
470 char *fname)
[1]471{
[128]472 FILE *fin;
473 char *tmp;
474 int items;
[1]475
[128]476 malloc_string(tmp);
477 raidlist->spares.entries = 0;
478 raidlist->disks.entries = 0;
479 if (length_of_file(fname) < 5) {
480 log_it("Raidtab is very small or non-existent. Ignoring it.");
481 raidlist->entries = 0;
[1080]482 mr_free(tmp);
[128]483 return (0);
[1]484 }
[128]485 if (!(fin = fopen(fname, "r"))) {
486 log_it("Cannot open raidtab");
[1080]487 mr_free(tmp);
[128]488 return (1);
[1]489 }
[128]490 items = 0;
491 log_it("Loading raidtab...");
492 while (!feof(fin)) {
493 int argc;
494 char **argv = get_next_vinum_conf_line(fin, &argc);
495 if (!argv)
496 break;
497 if (!strcmp(argv[0], "drive")) {
498 char *drivename, *devname;
499 if (argc < 4)
500 continue;
501 drivename = argv[1];
502 devname = get_option_val(argc, argv, "device");
503 if (!devname)
504 continue;
[1]505
[128]506 if (get_option_state(argc, argv, "hotspare")) {
507 strcpy(raidlist->spares.el[raidlist->spares.entries].name,
508 drivename);
509 strcpy(raidlist->spares.el[raidlist->spares.entries].
510 device, devname);
511 raidlist->spares.el[raidlist->spares.entries].index =
512 raidlist->disks.entries;
513 raidlist->spares.entries++;
514 } else {
515 strcpy(raidlist->disks.el[raidlist->disks.entries].name,
516 drivename);
517 strcpy(raidlist->disks.el[raidlist->disks.entries].device,
518 devname);
519 raidlist->disks.el[raidlist->disks.entries].index =
520 raidlist->disks.entries;
521 raidlist->disks.entries++;
522 }
523 } else if (!strcmp(argv[0], "volume")) {
524 char *volname;
525 if (argc < 2)
526 continue;
527 volname = argv[1];
528 strcpy(raidlist->el[raidlist->entries].volname, volname);
529 raidlist->el[raidlist->entries].plexes = 0;
530 raidlist->entries++;
531 } else if (!strcmp(argv[0], "plex")) {
532 int raidlevel, stripesize;
533 char *org = 0;
534 char **tmp = 0;
535 if (argc < 3)
536 continue;
537 org = get_option_val(argc, argv, "org");
538 if (!org)
539 continue;
540 if (strcmp(org, "concat")) {
541 tmp = get_option_vals(argc, argv, "org", 2);
542 if (tmp && tmp[1]) {
543 stripesize = (int) (size_spec(tmp[1]) / 1024);
544 } else
545 stripesize = 279;
546 } else
547 stripesize = 0;
[1]548
[128]549 if (!strcmp(org, "concat")) {
550 raidlevel = -1;
551 } else if (!strcmp(org, "striped")) {
552 raidlevel = 0;
553 } else if (!strcmp(org, "raid5")) {
554 raidlevel = 5;
555 } else
556 continue;
557
558 raidlist->el[raidlist->entries - 1].plex
559 [raidlist->el[raidlist->entries - 1].plexes].raidlevel =
560 raidlevel;
561 raidlist->el[raidlist->entries -
562 1].plex[raidlist->el[raidlist->entries -
563 1].plexes].stripesize =
564 stripesize;
565 raidlist->el[raidlist->entries -
566 1].plex[raidlist->el[raidlist->entries -
567 1].plexes].subdisks = 0;
568 raidlist->el[raidlist->entries - 1].plexes++;
569 } else if ((!strcmp(argv[0], "sd"))
570 || (!strcmp(argv[0], "subdisk"))) {
571 char *drive = 0;
572 if (argc < 3)
573 continue;
574 drive = get_option_val(argc, argv, "drive");
575 if (!drive)
576 continue;
577
578 strcpy(raidlist->el[raidlist->entries - 1].plex
579 [raidlist->el[raidlist->entries - 1].plexes - 1].sd
580 [raidlist->el[raidlist->entries - 1].plex
581 [raidlist->el[raidlist->entries - 1].plexes -
582 1].subdisks].which_device, drive);
583 raidlist->el[raidlist->entries -
584 1].plex[raidlist->el[raidlist->entries -
585 1].plexes - 1].subdisks++;
586 }
[1]587 }
[128]588 fclose(fin);
589 log_it("Raidtab loaded successfully.");
590 sprintf(tmp, "%d RAID devices in raidtab", raidlist->entries);
591 log_it(tmp);
[1080]592 mr_free(tmp);
[128]593 return (0);
[1]594}
595
596
597#else
598
[128]599int load_raidtab_into_raidlist(struct raidlist_itself *raidlist,
600 char *fname)
[1]601{
[128]602 FILE *fin;
603 char *tmp;
604 char *label;
605 char *value;
606 int items;
607 int v;
[1]608
[128]609 malloc_string(tmp);
610 malloc_string(label);
611 malloc_string(value);
612 assert(raidlist != NULL);
613 assert_string_is_neither_NULL_nor_zerolength(fname);
[1]614
[128]615 if (length_of_file(fname) < 5) {
616 log_it("Raidtab is very small or non-existent. Ignoring it.");
617 raidlist->entries = 0;
[1080]618 mr_free(tmp);
619 mr_free(label);
620 mr_free(value);
[128]621 return (0);
[1]622 }
[128]623 if (!(fin = fopen(fname, "r"))) {
624 log_it("Cannot open raidtab");
[1080]625 mr_free(tmp);
626 mr_free(label);
627 mr_free(value);
[128]628 return (1);
[1]629 }
[128]630 items = 0;
631 log_it("Loading raidtab...");
632 get_next_raidtab_line(fin, label, value);
633 while (!feof(fin)) {
[1107]634 mr_msg(1, "Looking for raid record #%d", items);
[128]635 initialize_raidrec(&raidlist->el[items]);
636 v = 0;
637 /* find the 'raiddev' entry, indicating the start of the block of info */
638 while (!feof(fin) && strcmp(label, "raiddev")) {
639 strcpy(raidlist->el[items].additional_vars.el[v].label, label);
640 strcpy(raidlist->el[items].additional_vars.el[v].value, value);
641 v++;
642 get_next_raidtab_line(fin, label, value);
643 log_it(tmp);
644 }
645 raidlist->el[items].additional_vars.entries = v;
646 if (feof(fin)) {
[1107]647 mr_msg(1, "No more records.");
[128]648 continue;
649 }
[1107]650 mr_msg(2, "Record #%d (%s) found", items, value);
[128]651 strcpy(raidlist->el[items].raid_device, value);
652 for (get_next_raidtab_line(fin, label, value);
653 !feof(fin) && strcmp(label, "raiddev");
654 get_next_raidtab_line(fin, label, value)) {
655 process_raidtab_line(fin, &raidlist->el[items], label, value);
656 }
657 items++;
[1]658 }
[128]659 paranoid_fclose(fin);
660 raidlist->entries = items;
[1107]661 mr_msg(1, "Raidtab loaded successfully.");
662 mr_msg(1, "%d RAID devices in raidtab", items);
[1080]663 mr_free(tmp);
664 mr_free(label);
665 mr_free(value);
[128]666 return (0);
[1]667}
668#endif
669
670
671
672
673
674
675
676
677#ifndef __FreeBSD__
678/**
679 * Process a single line from the raidtab and store the results into @p raidrec.
680 * @param fin The stream to read the line from.
681 * @param raidrec The RAID device record to update.
682 * @param label Where to put the label processed.
683 * @param value Where to put the value processed.
684 */
685void
[128]686process_raidtab_line(FILE * fin,
687 struct raid_device_record *raidrec,
688 char *label, char *value)
[1]689{
690
[128]691 /*@ add mallocs * */
692 char *tmp;
693 char *labelB;
694 char *valueB;
[1]695
[128]696 struct list_of_disks *disklist;
697 int v;
[1]698
[128]699 malloc_string(tmp);
700 malloc_string(labelB);
701 malloc_string(valueB);
702 assert(fin != NULL);
703 assert(raidrec != NULL);
704 assert_string_is_neither_NULL_nor_zerolength(label);
705 assert(value != NULL);
[1]706
[128]707 if (!strcmp(label, "raid-level")) {
[558]708 if (!strcmp(value, "multipath")) {
709 raidrec->raid_level = -2;
710 } else if (!strcmp(value, "linear")) {
[128]711 raidrec->raid_level = -1;
712 } else {
713 raidrec->raid_level = atoi(value);
714 }
715 } else if (!strcmp(label, "nr-raid-disks")) { /* ignore it */
716 } else if (!strcmp(label, "nr-spare-disks")) { /* ignore it */
717 } else if (!strcmp(label, "nr-parity-disks")) { /* ignore it */
718 } else if (!strcmp(label, "nr-failed-disks")) { /* ignore it */
719 } else if (!strcmp(label, "persistent-superblock")) {
720 raidrec->persistent_superblock = atoi(value);
721 } else if (!strcmp(label, "chunk-size")) {
722 raidrec->chunk_size = atoi(value);
[558]723 } else if (!strcmp(label, "parity-algorithm")) {
724 if (!strcmp(value, "left-asymmetric")) {
725 raidrec->parity = 0;
726 } else if (!strcmp(value, "right-asymmetric")) {
727 raidrec->parity = 1;
728 } else if (!strcmp(value, "left-symmetric")) {
729 raidrec->parity = 2;
730 } else if (!strcmp(value, "right-symmetric")) {
731 raidrec->parity = 3;
732 } else {
[1107]733 mr_msg(1, "Unknown RAID parity algorithm '%s'\n.", value);
[558]734 }
[128]735 } else if (!strcmp(label, "device")) {
736 get_next_raidtab_line(fin, labelB, valueB);
737 if (!strcmp(labelB, "raid-disk")) {
738 disklist = &raidrec->data_disks;
739 } else if (!strcmp(labelB, "spare-disk")) {
740 disklist = &raidrec->spare_disks;
741 } else if (!strcmp(labelB, "parity-disk")) {
742 disklist = &raidrec->parity_disks;
743 } else if (!strcmp(labelB, "failed-disk")) {
744 disklist = &raidrec->failed_disks;
745 } else {
746 disklist = NULL;
747 }
748 if (!disklist) {
749 sprintf(tmp,
750 "Ignoring '%s %s' pair of disk %s", labelB, valueB,
751 label);
752 log_it(tmp);
753 } else {
[1103]754 add_disk_to_raid_device(disklist, value, atoi(valueB));
[128]755 }
756 } else {
757 v = raidrec->additional_vars.entries;
758 strcpy(raidrec->additional_vars.el[v].label, label);
759 strcpy(raidrec->additional_vars.el[v].value, value);
760 raidrec->additional_vars.entries = ++v;
[1]761 }
[1080]762 mr_free(tmp);
763 mr_free(labelB);
764 mr_free(valueB);
[1]765}
766#endif
767
768
769/**
770 * Save a disklist to a stream in raidtab format.
771 * @param listname One of "raid-disk", "spare-disk", "parity-disk", or "failed-disk".
772 * @param disklist The disklist to save to @p fout.
773 * @param fout The stream to write to.
774 */
[128]775void
776save_disklist_to_file(char *listname,
777 struct list_of_disks *disklist, FILE * fout)
[1]778{
[128]779 int i;
[1]780
[128]781 assert_string_is_neither_NULL_nor_zerolength(listname);
782 assert(disklist != NULL);
783 assert(fout != NULL);
[1]784
[128]785 for (i = 0; i < disklist->entries; i++) {
786 fprintf(fout, " device %s\n",
787 disklist->el[i].device);
788 fprintf(fout, " %-21s %d\n", listname, disklist->el[i].index);
789 }
[1]790}
791
792
793
794
795
796#ifdef __FreeBSD__
797/**
798 * Add a new plex to a volume. The index of the plex will be <tt>v-\>plexes - 1</tt>.
799 * @param v The volume to operate on.
800 * @param raidlevel The RAID level of the new plex.
801 * @param stripesize The stripe size (chunk size) of the new plex.
802 */
[128]803void add_plex_to_volume(struct vinum_volume *v, int raidlevel,
804 int stripesize)
[1]805{
[128]806 v->plex[v->plexes].raidlevel = raidlevel;
807 v->plex[v->plexes].stripesize = stripesize;
808 v->plex[v->plexes].subdisks = 0;
809 ++v->plexes;
[1]810}
811
812/**
813 * For internal use only.
814 */
[128]815char **get_next_vinum_conf_line(FILE * f, int *argc)
[1]816{
[128]817 int cnt = 0;
818 static char *argv[64];
819 char **ap;
[1080]820 char *line = (char *) mr_malloc(MAX_STR_LEN);
[128]821 (void) fgets(line, MAX_STR_LEN, f);
822 if (feof(f)) {
823 log_it("[GNVCL] Uh... I reached the EOF.");
824 return 0;
[1]825 }
826
[128]827 for (ap = argv; (*ap = strsep(&line, " \t")) != NULL;)
828 if (**ap != '\0') {
829 if (++ap >= &argv[64])
830 break;
831 cnt++;
832 }
[1]833
[128]834 if (strchr(argv[cnt - 1], '\n')) {
835 *(strchr(argv[cnt - 1], '\n')) = '\0';
836 }
837
838 if (argc)
839 *argc = cnt;
840 return argv;
[1]841}
842
843/**
844 * For internal use only.
845 */
[128]846char *get_option_val(int argc, char **argv, char *option)
[1]847{
[128]848 int i;
849 for (i = 0; i < (argc - 1); ++i) {
850 if (!strcmp(argv[i], option)) {
851 return argv[i + 1];
852 }
[1]853 }
[128]854 return 0;
[1]855}
856
857/**
858 * For internal use only.
859 */
[128]860char **get_option_vals(int argc, char **argv, char *option, int nval)
[1]861{
[128]862 int i, j;
863 static char **ret;
[1080]864 ret = (char **) mr_malloc(nval * sizeof(char *));
[128]865 for (i = 0; i < (argc - nval); ++i) {
866 if (!strcmp(argv[i], option)) {
867 for (j = 0; j < nval; ++j) {
[1080]868 ret[j] = (char *) mr_malloc(strlen(argv[i + j + 1]) + 1);
[128]869 strcpy(ret[j], argv[i + j + 1]);
870 }
871 return ret;
872 }
[1]873 }
[128]874 return 0;
[1]875}
876
877/**
878 * For internal use only.
879 */
[128]880bool get_option_state(int argc, char **argv, char *option)
[1]881{
[128]882 int i;
883 for (i = 0; i < argc; ++i)
884 if (!strcmp(argv[i], option))
885 return TRUE;
[1]886
[128]887 return FALSE;
[1]888}
889
890/**
891 * Taken from Vinum source -- for internal use only.
892 */
893long long size_spec(char *spec)
894{
[128]895 u_int64_t size;
896 char *s;
897 int sign = 1; /* -1 if negative */
[1]898
[128]899 size = 0;
900 if (spec != NULL) { /* we have a parameter */
901 s = spec;
902 if (*s == '-') { /* negative, */
903 sign = -1;
904 s++; /* skip */
905 }
906 if ((*s >= '0') && (*s <= '9')) { /* it's numeric */
907 while ((*s >= '0') && (*s <= '9')) /* it's numeric */
908 size = size * 10 + *s++ - '0'; /* convert it */
909 switch (*s) {
910 case '\0':
911 return size * sign;
[1]912
[128]913 case 'B':
914 case 'b':
915 case 'S':
916 case 's':
917 return size * sign * 512;
[1]918
[128]919 case 'K':
920 case 'k':
921 return size * sign * 1024;
[1]922
[128]923 case 'M':
924 case 'm':
925 return size * sign * 1024 * 1024;
[1]926
[128]927 case 'G':
928 case 'g':
929 return size * sign * 1024 * 1024 * 1024;
[1]930
[128]931 case 'T':
932 case 't':
933 log_it
934 ("Ok, I'm scared... Someone did a TERABYTE+ size-spec");
935 return size * sign * 1024 * 1024 * 1024 * 1024;
[1]936
[128]937 case 'P':
938 case 'p':
939 log_it
940 ("If I was scared last time, I'm freaked out now. Someone actually has a PETABYTE?!?!?!?!");
941 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024;
[1]942
[128]943 case 'E':
944 case 'e':
945 log_it
946 ("Okay, I'm REALLY freaked out. Who could devote a whole EXABYTE to their data?!?!");
947 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
948 1024;
[1]949
[128]950 case 'Z':
951 case 'z':
952 log_it
953 ("WHAT!?!? A ZETABYTE!?!? You've GOT to be kidding me!!!");
954 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
955 1024 * 1024;
956
957 case 'Y':
958 case 'y':
959 log_it
960 ("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?!?!");
961 popup_and_OK
[541]962 ("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]963 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
964 1024 * 1024 * 1024;
965 }
966 }
[1]967 }
[128]968 return size * sign;
[1]969}
970
971#endif
972
973
974
975
[558]976int parse_mdstat(struct raidlist_itself *raidlist, char *device_prefix) {
[128]977
[558]978 const char delims[] = " ";
979
980 FILE *fin;
981 int res = 0, row, i, index_min;
[559]982 int lastpos = 0;
[558]983 size_t len = 0;
[559]984 char *token;
985 char *string = NULL;
986 char *pos;
987 char type;
988 char *strtmp;
[558]989
990 // open file
991 if (!(fin = fopen(MDSTAT_FILE, "r"))) {
[1107]992 mr_msg(1, "Could not open %s.\n", MDSTAT_FILE);
[558]993 return 1;
994 }
995 // initialise record, build progress and row counters
996 raidlist->entries = 0;
997 raidlist->el[raidlist->entries].progress = 999;
998 row = 1;
999 // skip first output row - contains registered RAID levels
1000 res = getline(&string, &len, fin);
1001 // parse the rest
1002 while ( !feof_unlocked(fin) ) {
1003 res = getline(&string, &len, fin);
1004 if (res <= 0) break;
1005 // trim leading spaces
1006 pos = string;
[671]1007 while (*pos == ' ') pos += 1;
[670]1008 asprintf(&strtmp, pos);
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
1029 asprintf(&strtmp,"%s%s", device_prefix, token);
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);
1088 asprintf(&strtmp,"%s%s", device_prefix, token);
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);
1095 asprintf(&strtmp,"%s%s", device_prefix, token);
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);
1102 asprintf(&strtmp,"%s%s", device_prefix, token);
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.