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

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

Compiler warning fixes

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