source: MondoRescue/trunk/mondo/src/common/libmondo-raid.c@ 1081

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

merge -r1078:1080 $SVN_M/branches/stable

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