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

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

r3335@localhost: bruno | 2009-08-08 23:04:12 +0200

  • Change mr_asprintf to avoid the need of pointer and be consistent with the other mr_mem functions
  • Property svn:keywords set to Id
File size: 32.2 KB
RevLine 
[1]1/* libmondo-raid.c subroutines for handling RAID
[128]2 $Id: libmondo-raid.c 2324 2009-08-18 13:13:54Z 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"
[541]17#include "lib-common-externs.h"
[1]18#include "libmondo-raid.h"
[2211]19#include "mr_mem.h"
[2241]20#include "mr_str.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 2324 2009-08-18 13:13:54Z 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 ********************************************************** */
[2211]49 char *command = NULL;
[128]50 int res;
51
[2323]52 mr_asprintf(command, "grep \"");
[128]53 if (raidno == -1) {
[2211]54 mr_strcat(command, "linear");
[128]55 } else {
[2211]56 mr_strcat(command, "raid%d", raidno);
[128]57 }
[2211]58 mr_strcat(command, "\" /proc/mdstat > /dev/null 2> /dev/null");
[128]59 log_it("Is raid %d registered? Command = '%s'", raidno, command);
60 res = system(command);
61 paranoid_free(command);
[2211]62
[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 ***************************************************** */
[2260]194 char *sz_value = NULL;
[1]195
[128]196 assert(raidrec != NULL);
197 assert(label != NULL);
[1]198
[2323]199 mr_asprintf(sz_value, "%d", value);
[128]200 strcpy(raidrec->additional_vars.el[lino].label, label);
201 strcpy(raidrec->additional_vars.el[lino].value, sz_value);
[2260]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,
233 char *device_to_add, int index)
[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);
241 disklist->el[items].index = index;
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)) {
438 paranoid_free(incoming);
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);
454 paranoid_free(incoming);
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 int items;
[1]474
[128]475 raidlist->spares.entries = 0;
476 raidlist->disks.entries = 0;
477 if (length_of_file(fname) < 5) {
478 log_it("Raidtab is very small or non-existent. Ignoring it.");
479 raidlist->entries = 0;
480 return (0);
[1]481 }
[128]482 if (!(fin = fopen(fname, "r"))) {
483 log_it("Cannot open raidtab");
484 return (1);
[1]485 }
[128]486 items = 0;
487 log_it("Loading raidtab...");
488 while (!feof(fin)) {
489 int argc;
490 char **argv = get_next_vinum_conf_line(fin, &argc);
491 if (!argv)
492 break;
493 if (!strcmp(argv[0], "drive")) {
494 char *drivename, *devname;
495 if (argc < 4)
496 continue;
497 drivename = argv[1];
498 devname = get_option_val(argc, argv, "device");
499 if (!devname)
500 continue;
[1]501
[128]502 if (get_option_state(argc, argv, "hotspare")) {
503 strcpy(raidlist->spares.el[raidlist->spares.entries].name,
504 drivename);
505 strcpy(raidlist->spares.el[raidlist->spares.entries].
506 device, devname);
507 raidlist->spares.el[raidlist->spares.entries].index =
508 raidlist->disks.entries;
509 raidlist->spares.entries++;
510 } else {
511 strcpy(raidlist->disks.el[raidlist->disks.entries].name,
512 drivename);
513 strcpy(raidlist->disks.el[raidlist->disks.entries].device,
514 devname);
515 raidlist->disks.el[raidlist->disks.entries].index =
516 raidlist->disks.entries;
517 raidlist->disks.entries++;
518 }
519 } else if (!strcmp(argv[0], "volume")) {
520 char *volname;
521 if (argc < 2)
522 continue;
523 volname = argv[1];
524 strcpy(raidlist->el[raidlist->entries].volname, volname);
525 raidlist->el[raidlist->entries].plexes = 0;
526 raidlist->entries++;
527 } else if (!strcmp(argv[0], "plex")) {
528 int raidlevel, stripesize;
529 char *org = 0;
530 char **tmp = 0;
531 if (argc < 3)
532 continue;
533 org = get_option_val(argc, argv, "org");
534 if (!org)
535 continue;
536 if (strcmp(org, "concat")) {
537 tmp = get_option_vals(argc, argv, "org", 2);
538 if (tmp && tmp[1]) {
539 stripesize = (int) (size_spec(tmp[1]) / 1024);
540 } else
541 stripesize = 279;
542 } else
543 stripesize = 0;
[1]544
[128]545 if (!strcmp(org, "concat")) {
546 raidlevel = -1;
547 } else if (!strcmp(org, "striped")) {
548 raidlevel = 0;
549 } else if (!strcmp(org, "raid5")) {
550 raidlevel = 5;
551 } else
552 continue;
553
554 raidlist->el[raidlist->entries - 1].plex
555 [raidlist->el[raidlist->entries - 1].plexes].raidlevel =
556 raidlevel;
557 raidlist->el[raidlist->entries -
558 1].plex[raidlist->el[raidlist->entries -
559 1].plexes].stripesize =
560 stripesize;
561 raidlist->el[raidlist->entries -
562 1].plex[raidlist->el[raidlist->entries -
563 1].plexes].subdisks = 0;
564 raidlist->el[raidlist->entries - 1].plexes++;
565 } else if ((!strcmp(argv[0], "sd"))
566 || (!strcmp(argv[0], "subdisk"))) {
567 char *drive = 0;
568 if (argc < 3)
569 continue;
570 drive = get_option_val(argc, argv, "drive");
571 if (!drive)
572 continue;
573
574 strcpy(raidlist->el[raidlist->entries - 1].plex
575 [raidlist->el[raidlist->entries - 1].plexes - 1].sd
576 [raidlist->el[raidlist->entries - 1].plex
577 [raidlist->el[raidlist->entries - 1].plexes -
578 1].subdisks].which_device, drive);
579 raidlist->el[raidlist->entries -
580 1].plex[raidlist->el[raidlist->entries -
581 1].plexes - 1].subdisks++;
582 }
[1]583 }
[128]584 fclose(fin);
585 log_it("Raidtab loaded successfully.");
[2324]586 log_it("%d RAID devices in raidtab", raidlist->entries);
[128]587 return (0);
[1]588}
589
590
591#else
592
[128]593int load_raidtab_into_raidlist(struct raidlist_itself *raidlist,
594 char *fname)
[1]595{
[128]596 FILE *fin;
597 char *tmp;
598 char *label;
599 char *value;
600 int items;
601 int v;
[1]602
[128]603 malloc_string(tmp);
604 malloc_string(label);
605 malloc_string(value);
606 assert(raidlist != NULL);
607 assert_string_is_neither_NULL_nor_zerolength(fname);
[1]608
[128]609 if (length_of_file(fname) < 5) {
610 log_it("Raidtab is very small or non-existent. Ignoring it.");
611 raidlist->entries = 0;
612 paranoid_free(tmp);
613 paranoid_free(label);
614 paranoid_free(value);
615 return (0);
[1]616 }
[128]617 if (!(fin = fopen(fname, "r"))) {
618 log_it("Cannot open raidtab");
619 paranoid_free(tmp);
620 paranoid_free(label);
621 paranoid_free(value);
622 return (1);
[1]623 }
[128]624 items = 0;
625 log_it("Loading raidtab...");
626 get_next_raidtab_line(fin, label, value);
627 while (!feof(fin)) {
628 log_msg(1, "Looking for raid record #%d", items);
629 initialize_raidrec(&raidlist->el[items]);
630 v = 0;
631 /* find the 'raiddev' entry, indicating the start of the block of info */
632 while (!feof(fin) && strcmp(label, "raiddev")) {
633 strcpy(raidlist->el[items].additional_vars.el[v].label, label);
634 strcpy(raidlist->el[items].additional_vars.el[v].value, value);
635 v++;
636 get_next_raidtab_line(fin, label, value);
637 log_it(tmp);
638 }
639 raidlist->el[items].additional_vars.entries = v;
640 if (feof(fin)) {
641 log_msg(1, "No more records.");
642 continue;
643 }
644 log_msg(2, "Record #%d (%s) found", items, value);
645 strcpy(raidlist->el[items].raid_device, value);
646 for (get_next_raidtab_line(fin, label, value);
647 !feof(fin) && strcmp(label, "raiddev");
648 get_next_raidtab_line(fin, label, value)) {
649 process_raidtab_line(fin, &raidlist->el[items], label, value);
650 }
651 items++;
[1]652 }
[128]653 paranoid_fclose(fin);
654 raidlist->entries = items;
655 log_msg(1, "Raidtab loaded successfully.");
656 log_msg(1, "%d RAID devices in raidtab", items);
657 paranoid_free(tmp);
658 paranoid_free(label);
659 paranoid_free(value);
660 return (0);
[1]661}
662#endif
663
664
665
666
667
668
669
670
671#ifndef __FreeBSD__
672/**
673 * Process a single line from the raidtab and store the results into @p raidrec.
674 * @param fin The stream to read the line from.
675 * @param raidrec The RAID device record to update.
676 * @param label Where to put the label processed.
677 * @param value Where to put the value processed.
678 */
679void
[128]680process_raidtab_line(FILE * fin,
681 struct raid_device_record *raidrec,
682 char *label, char *value)
[1]683{
684
[128]685 /*@ add mallocs * */
686 char *labelB;
687 char *valueB;
[1]688
[128]689 struct list_of_disks *disklist;
690 int index;
691 int v;
[1]692
[128]693 malloc_string(labelB);
694 malloc_string(valueB);
695 assert(fin != NULL);
696 assert(raidrec != NULL);
697 assert_string_is_neither_NULL_nor_zerolength(label);
698 assert(value != NULL);
[1]699
[128]700 if (!strcmp(label, "raid-level")) {
[558]701 if (!strcmp(value, "multipath")) {
702 raidrec->raid_level = -2;
703 } else if (!strcmp(value, "linear")) {
[128]704 raidrec->raid_level = -1;
705 } else {
706 raidrec->raid_level = atoi(value);
707 }
708 } else if (!strcmp(label, "nr-raid-disks")) { /* ignore it */
709 } else if (!strcmp(label, "nr-spare-disks")) { /* ignore it */
710 } else if (!strcmp(label, "nr-parity-disks")) { /* ignore it */
711 } else if (!strcmp(label, "nr-failed-disks")) { /* ignore it */
712 } else if (!strcmp(label, "persistent-superblock")) {
713 raidrec->persistent_superblock = atoi(value);
714 } else if (!strcmp(label, "chunk-size")) {
715 raidrec->chunk_size = atoi(value);
[558]716 } else if (!strcmp(label, "parity-algorithm")) {
717 if (!strcmp(value, "left-asymmetric")) {
718 raidrec->parity = 0;
719 } else if (!strcmp(value, "right-asymmetric")) {
720 raidrec->parity = 1;
721 } else if (!strcmp(value, "left-symmetric")) {
722 raidrec->parity = 2;
723 } else if (!strcmp(value, "right-symmetric")) {
724 raidrec->parity = 3;
725 } else {
726 log_msg(1, "Unknown RAID parity algorithm '%s'\n.", value);
727 }
[128]728 } else if (!strcmp(label, "device")) {
729 get_next_raidtab_line(fin, labelB, valueB);
730 if (!strcmp(labelB, "raid-disk")) {
731 disklist = &raidrec->data_disks;
732 } else if (!strcmp(labelB, "spare-disk")) {
733 disklist = &raidrec->spare_disks;
734 } else if (!strcmp(labelB, "parity-disk")) {
735 disklist = &raidrec->parity_disks;
736 } else if (!strcmp(labelB, "failed-disk")) {
737 disklist = &raidrec->failed_disks;
738 } else {
739 disklist = NULL;
740 }
741 if (!disklist) {
[2324]742 log_it("Ignoring '%s %s' pair of disk %s", labelB, valueB, label);
[128]743 } else {
744 index = atoi(valueB);
745 add_disk_to_raid_device(disklist, value, index);
746 }
747 } else {
748 v = raidrec->additional_vars.entries;
749 strcpy(raidrec->additional_vars.el[v].label, label);
750 strcpy(raidrec->additional_vars.el[v].value, value);
751 raidrec->additional_vars.entries = ++v;
[1]752 }
[128]753 paranoid_free(labelB);
754 paranoid_free(valueB);
[1]755}
756#endif
757
758
759/**
760 * Save a disklist to a stream in raidtab format.
761 * @param listname One of "raid-disk", "spare-disk", "parity-disk", or "failed-disk".
762 * @param disklist The disklist to save to @p fout.
763 * @param fout The stream to write to.
764 */
[128]765void
766save_disklist_to_file(char *listname,
767 struct list_of_disks *disklist, FILE * fout)
[1]768{
[128]769 int i;
[1]770
[128]771 assert_string_is_neither_NULL_nor_zerolength(listname);
772 assert(disklist != NULL);
773 assert(fout != NULL);
[1]774
[128]775 for (i = 0; i < disklist->entries; i++) {
776 fprintf(fout, " device %s\n",
777 disklist->el[i].device);
778 fprintf(fout, " %-21s %d\n", listname, disklist->el[i].index);
779 }
[1]780}
781
782
783
784
785
786#ifdef __FreeBSD__
787/**
788 * Add a new plex to a volume. The index of the plex will be <tt>v-\>plexes - 1</tt>.
789 * @param v The volume to operate on.
790 * @param raidlevel The RAID level of the new plex.
791 * @param stripesize The stripe size (chunk size) of the new plex.
792 */
[128]793void add_plex_to_volume(struct vinum_volume *v, int raidlevel,
794 int stripesize)
[1]795{
[128]796 v->plex[v->plexes].raidlevel = raidlevel;
797 v->plex[v->plexes].stripesize = stripesize;
798 v->plex[v->plexes].subdisks = 0;
799 ++v->plexes;
[1]800}
801
802/**
803 * For internal use only.
804 */
[128]805char **get_next_vinum_conf_line(FILE * f, int *argc)
[1]806{
[128]807 int cnt = 0;
808 static char *argv[64];
809 char **ap;
810 char *line = (char *) malloc(MAX_STR_LEN);
811 if (!line)
812 errx(1,
813 "unable to allocate %i bytes of memory for `char *line' at %s:%i",
814 MAX_STR_LEN, __FILE__, __LINE__);
815 (void) fgets(line, MAX_STR_LEN, f);
816 if (feof(f)) {
817 log_it("[GNVCL] Uh... I reached the EOF.");
818 return 0;
[1]819 }
820
[128]821 for (ap = argv; (*ap = strsep(&line, " \t")) != NULL;)
822 if (**ap != '\0') {
823 if (++ap >= &argv[64])
824 break;
825 cnt++;
826 }
[1]827
[128]828 if (strchr(argv[cnt - 1], '\n')) {
829 *(strchr(argv[cnt - 1], '\n')) = '\0';
830 }
831
832 if (argc)
833 *argc = cnt;
834 return argv;
[1]835}
836
837/**
838 * For internal use only.
839 */
[128]840char *get_option_val(int argc, char **argv, char *option)
[1]841{
[128]842 int i;
843 for (i = 0; i < (argc - 1); ++i) {
844 if (!strcmp(argv[i], option)) {
845 return argv[i + 1];
846 }
[1]847 }
[128]848 return 0;
[1]849}
850
851/**
852 * For internal use only.
853 */
[128]854char **get_option_vals(int argc, char **argv, char *option, int nval)
[1]855{
[128]856 int i, j;
857 static char **ret;
858 ret = (char **) malloc(nval * sizeof(char *));
859 for (i = 0; i < (argc - nval); ++i) {
860 if (!strcmp(argv[i], option)) {
861 for (j = 0; j < nval; ++j) {
862 ret[j] = (char *) malloc(strlen(argv[i + j + 1]) + 1);
863 strcpy(ret[j], argv[i + j + 1]);
864 }
865 return ret;
866 }
[1]867 }
[128]868 return 0;
[1]869}
870
871/**
872 * For internal use only.
873 */
[128]874bool get_option_state(int argc, char **argv, char *option)
[1]875{
[128]876 int i;
877 for (i = 0; i < argc; ++i)
878 if (!strcmp(argv[i], option))
879 return TRUE;
[1]880
[128]881 return FALSE;
[1]882}
883
884/**
885 * Taken from Vinum source -- for internal use only.
886 */
887long long size_spec(char *spec)
888{
[128]889 u_int64_t size;
890 char *s;
891 int sign = 1; /* -1 if negative */
[1]892
[128]893 size = 0;
894 if (spec != NULL) { /* we have a parameter */
895 s = spec;
896 if (*s == '-') { /* negative, */
897 sign = -1;
898 s++; /* skip */
899 }
900 if ((*s >= '0') && (*s <= '9')) { /* it's numeric */
901 while ((*s >= '0') && (*s <= '9')) /* it's numeric */
902 size = size * 10 + *s++ - '0'; /* convert it */
903 switch (*s) {
904 case '\0':
905 return size * sign;
[1]906
[128]907 case 'B':
908 case 'b':
909 case 'S':
910 case 's':
911 return size * sign * 512;
[1]912
[128]913 case 'K':
914 case 'k':
915 return size * sign * 1024;
[1]916
[128]917 case 'M':
918 case 'm':
919 return size * sign * 1024 * 1024;
[1]920
[128]921 case 'G':
922 case 'g':
923 return size * sign * 1024 * 1024 * 1024;
[1]924
[128]925 case 'T':
926 case 't':
927 log_it
928 ("Ok, I'm scared... Someone did a TERABYTE+ size-spec");
929 return size * sign * 1024 * 1024 * 1024 * 1024;
[1]930
[128]931 case 'P':
932 case 'p':
933 log_it
934 ("If I was scared last time, I'm freaked out now. Someone actually has a PETABYTE?!?!?!?!");
935 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024;
[1]936
[128]937 case 'E':
938 case 'e':
939 log_it
940 ("Okay, I'm REALLY freaked out. Who could devote a whole EXABYTE to their data?!?!");
941 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
942 1024;
[1]943
[128]944 case 'Z':
945 case 'z':
946 log_it
947 ("WHAT!?!? A ZETABYTE!?!? You've GOT to be kidding me!!!");
948 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
949 1024 * 1024;
950
951 case 'Y':
952 case 'y':
953 log_it
954 ("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?!?!");
955 popup_and_OK
[541]956 ("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]957 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
958 1024 * 1024 * 1024;
959 }
960 }
[1]961 }
[128]962 return size * sign;
[1]963}
964
965#endif
966
967
968
969
[558]970int parse_mdstat(struct raidlist_itself *raidlist, char *device_prefix) {
[128]971
[558]972 const char delims[] = " ";
973
974 FILE *fin;
975 int res = 0, row, i, index_min;
[559]976 int lastpos = 0;
[558]977 size_t len = 0;
[559]978 char *token;
979 char *string = NULL;
980 char *pos;
981 char type;
982 char *strtmp;
[558]983
984 // open file
985 if (!(fin = fopen(MDSTAT_FILE, "r"))) {
986 log_msg(1, "Could not open %s.\n", MDSTAT_FILE);
987 return 1;
988 }
989 // initialise record, build progress and row counters
990 raidlist->entries = 0;
991 raidlist->el[raidlist->entries].progress = 999;
992 row = 1;
993 // skip first output row - contains registered RAID levels
994 res = getline(&string, &len, fin);
995 // parse the rest
996 while ( !feof_unlocked(fin) ) {
997 res = getline(&string, &len, fin);
998 if (res <= 0) break;
999 // trim leading spaces
1000 pos = string;
[671]1001 while (*pos == ' ') pos += 1;
[2323]1002 mr_asprintf(strtmp, "%s", pos);
[670]1003 strcpy(string, strtmp);
[2324]1004 mr_free(strtmp);
[558]1005 // if we have newline after only spaces, this is a blank line, update
1006 // counters, otherwise do normal parsing
1007 if (*string == '\n') {
1008 row = 1;
1009 raidlist->entries++;
1010 raidlist->el[raidlist->entries].progress = 999;
1011 } else {
1012 switch (row) {
1013 case 1: // device information
1014 // check whether last line of record and if so skip
1015 pos = strcasestr(string, "unused devices: ");
1016 if (pos == string) {
1017 //raidlist->entries--;
1018 break;
[128]1019 }
[558]1020 // tokenise string
[2321]1021 token = mr_strtok(string, delims, &lastpos);
[558]1022 // get RAID device name
[2323]1023 mr_asprintf(strtmp,"%s%s", device_prefix, token);
[558]1024 strcpy(raidlist->el[raidlist->entries].raid_device, strtmp);
[2324]1025 mr_free(strtmp);
[2241]1026 mr_free(token);
[558]1027 // skip ':' and status
[2321]1028 token = mr_strtok(string, delims, &lastpos);
[2241]1029 mr_free(token);
[2321]1030 token = mr_strtok(string, delims, &lastpos);
[558]1031 if (!strcmp(token, "inactive")) {
1032 log_msg(1, "RAID device '%s' inactive.\n",
1033 raidlist->el[raidlist->entries].raid_device);
1034 paranoid_free(string);
[2241]1035 mr_free(token);
[558]1036 return 1;
[128]1037 }
[2241]1038 mr_free(token);
[559]1039
[558]1040 // get RAID level
[2321]1041 token = mr_strtok(string, delims, &lastpos);
[558]1042 if (!strcmp(token, "multipath")) {
1043 raidlist->el[raidlist->entries].raid_level = -2;
1044 } else if (!strcmp(token, "linear")) {
1045 raidlist->el[raidlist->entries].raid_level = -1;
1046 } else if (!strcmp(token, "raid0")) {
1047 raidlist->el[raidlist->entries].raid_level = 0;
1048 } else if (!strcmp(token, "raid1")) {
1049 raidlist->el[raidlist->entries].raid_level = 1;
1050 } else if (!strcmp(token, "raid4")) {
1051 raidlist->el[raidlist->entries].raid_level = 4;
1052 } else if (!strcmp(token, "raid5")) {
1053 raidlist->el[raidlist->entries].raid_level = 5;
1054 } else if (!strcmp(token, "raid6")) {
1055 raidlist->el[raidlist->entries].raid_level = 6;
1056 } else if (!strcmp(token, "raid10")) {
1057 raidlist->el[raidlist->entries].raid_level = 10;
1058 } else {
1059 log_msg(1, "Unknown RAID level '%s'.\n", token);
1060 paranoid_free(string);
[559]1061 paranoid_free(token);
[558]1062 return 1;
1063 }
[2241]1064 mr_free(token);
[559]1065
[558]1066 // get RAID devices (type, index, device)
1067 // Note: parity disk for RAID4 is last normal disk, there is no '(P)'
1068 raidlist->el[raidlist->entries].data_disks.entries = 0;
1069 raidlist->el[raidlist->entries].spare_disks.entries = 0;
1070 raidlist->el[raidlist->entries].failed_disks.entries = 0;
[565]1071 while((token = mr_strtok (string, delims, &lastpos))) {
[558]1072 if ((pos = strstr(token, "("))) {
1073 type = *(pos+1);
1074 } else {
1075 type = ' ';
1076 }
1077 pos = strstr(token, "[");
1078 *pos = '\0';
1079 switch(type) {
1080 case ' ': // normal data disks
1081 raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].index = atoi(pos + 1);
[2323]1082 mr_asprintf(strtmp,"%s%s", device_prefix, token);
[558]1083 strcpy(raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].device, strtmp);
[2324]1084 mr_free(strtmp);
[558]1085 raidlist->el[raidlist->entries].data_disks.entries++;
1086 break;
1087 case 'S': // spare disks
1088 raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].index = atoi(pos + 1);
[2323]1089 mr_asprintf(strtmp,"%s%s", device_prefix, token);
[558]1090 strcpy(raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].device, strtmp);
[2324]1091 mr_free(strtmp);
[558]1092 raidlist->el[raidlist->entries].spare_disks.entries++;
1093 break;
1094 case 'F': // failed disks
1095 raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].index = atoi(pos + 1);
[2323]1096 mr_asprintf(strtmp,"%s%s", device_prefix, token);
[558]1097 strcpy(raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].device, strtmp);
[2324]1098 mr_free(strtmp);
[558]1099 raidlist->el[raidlist->entries].failed_disks.entries++;
1100 log_it("At least one failed disk found in RAID array.\n");
1101 break;
1102 default: // error
1103 log_msg(1, "Unknown device type '%c'\n", type);
1104 paranoid_free(string);
[559]1105 paranoid_free(token);
[558]1106 return 1;
1107 break;
1108 }
[2241]1109 mr_free(token);
[558]1110 }
[559]1111
[558]1112 // adjust index for each device so that it starts with 0 for every type
1113 index_min = 99;
1114 for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
1115 if (raidlist->el[raidlist->entries].data_disks.el[i].index < index_min) {
1116 index_min = raidlist->el[raidlist->entries].data_disks.el[i].index;
1117 }
1118 }
1119 if (index_min > 0) {
1120 for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
1121 raidlist->el[raidlist->entries].data_disks.el[i].index = raidlist->el[raidlist->entries].data_disks.el[i].index - index_min;
1122 }
1123 }
1124 index_min = 99;
1125 for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
1126 if (raidlist->el[raidlist->entries].spare_disks.el[i].index < index_min) {
1127 index_min = raidlist->el[raidlist->entries].spare_disks.el[i].index;
1128 }
1129 }
1130 if (index_min > 0) {
1131 for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
1132 raidlist->el[raidlist->entries].spare_disks.el[i].index = raidlist->el[raidlist->entries].spare_disks.el[i].index - index_min;
1133 }
1134 }
1135 index_min = 99;
1136 for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
1137 if (raidlist->el[raidlist->entries].failed_disks.el[i].index < index_min) {
1138 index_min = raidlist->el[raidlist->entries].failed_disks.el[i].index;
1139 }
1140 }
1141 if (index_min > 0) {
1142 for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
1143 raidlist->el[raidlist->entries].failed_disks.el[i].index = raidlist->el[raidlist->entries].failed_disks.el[i].index - index_min;
1144 }
1145 }
1146 break;
1147 case 2: // config information
1148 // check for persistent super block
1149 if (strcasestr(string, "super non-persistent")) {
1150 raidlist->el[raidlist->entries].persistent_superblock = 0;
1151 } else {
1152 raidlist->el[raidlist->entries].persistent_superblock = 1;
1153 }
1154 // extract chunk size
1155 if (!(pos = strcasestr(string, "k chunk"))) {
1156 raidlist->el[raidlist->entries].chunk_size = -1;
1157 } else {
1158 while (*pos != ' ') {
[671]1159 pos -= 1;
[558]1160 if (pos < string) {
1161 log_it("String underflow!\n");
1162 paranoid_free(string);
1163 return 1;
1164 }
1165 }
1166 raidlist->el[raidlist->entries].chunk_size = atoi(pos + 1);
1167 }
1168 // extract parity if present
1169 if ((pos = strcasestr(string, "algorithm"))) {
1170 raidlist->el[raidlist->entries].parity = atoi(pos + 9);
1171 } else {
1172 raidlist->el[raidlist->entries].parity = -1;
1173 }
1174 break;
1175 case 3: // optional build status information
1176 if (!(pos = strchr(string, '\%'))) {
1177 if (strcasestr(string, "delayed")) {
1178 raidlist->el[raidlist->entries].progress = -1; // delayed (therefore, stuck at 0%)
1179 } else {
1180 raidlist->el[raidlist->entries].progress = 999; // not found
1181 }
1182 } else {
1183 while (*pos != ' ') {
[671]1184 pos -= 1;
[558]1185 if (pos < string) {
1186 printf("ERROR: String underflow!\n");
1187 paranoid_free(string);
1188 return 1;
1189 }
1190 }
1191 raidlist->el[raidlist->entries].progress = atoi(pos);
1192 }
1193 break;
[1250]1194 default: // error or IN PROGRESS
1195 if (raidlist->el[raidlist->entries].progress != -1 &&
1196 raidlist->el[raidlist->entries].progress != 999) {
[1261]1197 log_msg(1, "Row %d should not occur in record!\n", row);
[1250]1198 }
[558]1199 break;
1200 }
1201 row++;
1202 }
1203 }
1204 // close file
1205 fclose(fin);
1206 // free string
1207 paranoid_free(string);
1208 // return success
1209 return 0;
1210
[1]1211}
1212
1213
1214
[558]1215
1216int create_raidtab_from_mdstat(char *raidtab_fname)
[1]1217{
[128]1218 struct raidlist_itself *raidlist;
1219 int retval = 0;
[1]1220
[128]1221 raidlist = malloc(sizeof(struct raidlist_itself));
[1]1222
[558]1223 // FIXME: Prefix '/dev/' should really be dynamic!
1224 if (parse_mdstat(raidlist, "/dev/")) {
1225 log_to_screen("Sorry, cannot read %s", MDSTAT_FILE);
[128]1226 return (1);
1227 }
1228
1229 retval += save_raidlist_to_raidtab(raidlist, raidtab_fname);
1230 return (retval);
[1]1231}
1232
1233
1234
1235/* @} - end of raidGroup */
Note: See TracBrowser for help on using the repository browser.