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

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

merge -r1082:1105 $SVN_M/branches/stable

  • Property svn:keywords set to Id
File size: 31.4 KB
RevLine 
[45]1/* $Id: libmondo-raid.c 1106 2007-02-07 22:55:11Z 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 1106 2007-02-07 22:55:11Z 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,
[1106]216 char *device_to_add, int idx)
[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);
[1106]224 disklist->el[items].index = idx;
[43]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)) {
[1086]601 mr_msg(1, "Looking for raid record #%d", items);
[43]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)) {
[1086]613 mr_msg(1, "No more records.");
[43]614 continue;
615 }
[1086]616 mr_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;
[1086]627 mr_msg(1, "Raidtab loaded successfully.");
628 mr_msg(1, "%d RAID devices in raidtab", items);
[43]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 v = 0;
[1]655
[43]656 assert(fin != NULL);
657 assert(raidrec != NULL);
658 assert_string_is_neither_NULL_nor_zerolength(label);
659 assert(value != NULL);
[1]660
[43]661 if (!strcmp(label, "raid-level")) {
[561]662 if (!strcmp(value, "multipath")) {
663 raidrec->raid_level = -2;
664 } else if (!strcmp(value, "linear")) {
[43]665 raidrec->raid_level = -1;
666 } else {
667 raidrec->raid_level = atoi(value);
668 }
669 } else if (!strcmp(label, "nr-raid-disks")) { /* ignore it */
670 } else if (!strcmp(label, "nr-spare-disks")) { /* ignore it */
671 } else if (!strcmp(label, "nr-parity-disks")) { /* ignore it */
672 } else if (!strcmp(label, "nr-failed-disks")) { /* ignore it */
673 } else if (!strcmp(label, "persistent-superblock")) {
674 raidrec->persistent_superblock = atoi(value);
675 } else if (!strcmp(label, "chunk-size")) {
676 raidrec->chunk_size = atoi(value);
[561]677 } else if (!strcmp(label, "parity-algorithm")) {
678 if (!strcmp(value, "left-asymmetric")) {
679 raidrec->parity = 0;
680 } else if (!strcmp(value, "right-asymmetric")) {
681 raidrec->parity = 1;
682 } else if (!strcmp(value, "left-symmetric")) {
683 raidrec->parity = 2;
684 } else if (!strcmp(value, "right-symmetric")) {
685 raidrec->parity = 3;
686 } else {
[1086]687 mr_msg(1, "Unknown RAID parity algorithm '%s'\n.", value);
[561]688 }
[43]689 } else if (!strcmp(label, "device")) {
690 get_next_raidtab_line(fin, labelB, valueB);
691 if (!strcmp(labelB, "raid-disk")) {
692 disklist = &raidrec->data_disks;
693 } else if (!strcmp(labelB, "spare-disk")) {
694 disklist = &raidrec->spare_disks;
695 } else if (!strcmp(labelB, "parity-disk")) {
696 disklist = &raidrec->parity_disks;
697 } else if (!strcmp(labelB, "failed-disk")) {
698 disklist = &raidrec->failed_disks;
699 } else {
700 disklist = NULL;
701 }
702 if (!disklist) {
[900]703 mr_asprintf(&tmp,
[45]704 "Ignoring '%s %s' pair of disk %s", labelB, valueB,
705 label);
[43]706 log_it(tmp);
[900]707 mr_free(tmp);
[43]708 } else {
[1106]709 add_disk_to_raid_device(disklist, value, atoi(valueB));
[43]710 }
[900]711 mr_free(labelB);
712 mr_free(valueB);
[43]713 } else {
714 v = raidrec->additional_vars.entries;
[689]715 raidrec->additional_vars.el[v].label = label;
716 raidrec->additional_vars.el[v].value = value;
[43]717 raidrec->additional_vars.entries = ++v;
[1]718 }
719}
720#endif
721
722
723/**
724 * Save a disklist to a stream in raidtab format.
725 * @param listname One of "raid-disk", "spare-disk", "parity-disk", or "failed-disk".
726 * @param disklist The disklist to save to @p fout.
727 * @param fout The stream to write to.
728 */
[43]729void
730save_disklist_to_file(char *listname,
731 struct list_of_disks *disklist, FILE * fout)
[1]732{
[43]733 int i;
[1]734
[43]735 assert_string_is_neither_NULL_nor_zerolength(listname);
736 assert(disklist != NULL);
737 assert(fout != NULL);
[1]738
[43]739 for (i = 0; i < disklist->entries; i++) {
740 fprintf(fout, " device %s\n",
741 disklist->el[i].device);
742 fprintf(fout, " %-21s %d\n", listname, disklist->el[i].index);
743 }
[1]744}
745
746
747#ifdef __FreeBSD__
748/**
749 * Add a new plex to a volume. The index of the plex will be <tt>v-\>plexes - 1</tt>.
750 * @param v The volume to operate on.
751 * @param raidlevel The RAID level of the new plex.
752 * @param stripesize The stripe size (chunk size) of the new plex.
753 */
[43]754void add_plex_to_volume(struct vinum_volume *v, int raidlevel,
755 int stripesize)
[1]756{
[43]757 v->plex[v->plexes].raidlevel = raidlevel;
758 v->plex[v->plexes].stripesize = stripesize;
759 v->plex[v->plexes].subdisks = 0;
760 ++v->plexes;
[1]761}
762
763/**
764 * For internal use only.
765 */
[43]766char **get_next_vinum_conf_line(FILE * f, int *argc)
[1]767{
[43]768 int cnt = 0;
[688]769 char *argv[64];
[43]770 char **ap;
[688]771 char *line = NULL;
772 size_t = 0;
773 int lastpos = 0;
774
[900]775 mr_getline(&line, &n, f);
[43]776 if (feof(f)) {
777 log_it("[GNVCL] Uh... I reached the EOF.");
[688]778 return NULL;
[1]779 }
780
[688]781 for (ap = argv; (*ap = mr_strtok(line, " \t", &lastpos)) != NULL;)
[43]782 if (**ap != '\0') {
783 if (++ap >= &argv[64])
784 break;
785 cnt++;
786 }
[1]787
[43]788 if (strchr(argv[cnt - 1], '\n')) {
789 *(strchr(argv[cnt - 1], '\n')) = '\0';
790 }
791
792 if (argc)
793 *argc = cnt;
794 return argv;
[1]795}
796
797/**
798 * For internal use only.
799 */
[43]800char *get_option_val(int argc, char **argv, char *option)
[1]801{
[43]802 int i;
803 for (i = 0; i < (argc - 1); ++i) {
804 if (!strcmp(argv[i], option)) {
805 return argv[i + 1];
806 }
[1]807 }
[43]808 return 0;
[1]809}
810
811/**
812 * For internal use only.
813 */
[43]814char **get_option_vals(int argc, char **argv, char *option, int nval)
[1]815{
[43]816 int i, j;
817 static char **ret;
[1081]818 ret = (char **) mr_malloc(nval * sizeof(char *));
[43]819 for (i = 0; i < (argc - nval); ++i) {
820 if (!strcmp(argv[i], option)) {
821 for (j = 0; j < nval; ++j) {
[1081]822 ret[j] = (char *) mr_malloc(strlen(argv[i + j + 1]) + 1);
[43]823 strcpy(ret[j], argv[i + j + 1]);
824 }
825 return ret;
826 }
[1]827 }
[43]828 return 0;
[1]829}
830
831/**
832 * For internal use only.
833 */
[43]834bool get_option_state(int argc, char **argv, char *option)
[1]835{
[43]836 int i;
837 for (i = 0; i < argc; ++i)
838 if (!strcmp(argv[i], option))
839 return TRUE;
[1]840
[43]841 return FALSE;
[1]842}
843
844/**
845 * Taken from Vinum source -- for internal use only.
846 */
847long long size_spec(char *spec)
848{
[43]849 u_int64_t size;
850 char *s;
851 int sign = 1; /* -1 if negative */
[1]852
[43]853 size = 0;
854 if (spec != NULL) { /* we have a parameter */
855 s = spec;
856 if (*s == '-') { /* negative, */
857 sign = -1;
858 s++; /* skip */
859 }
860 if ((*s >= '0') && (*s <= '9')) { /* it's numeric */
861 while ((*s >= '0') && (*s <= '9')) /* it's numeric */
862 size = size * 10 + *s++ - '0'; /* convert it */
863 switch (*s) {
864 case '\0':
865 return size * sign;
[1]866
[43]867 case 'B':
868 case 'b':
869 case 'S':
870 case 's':
871 return size * sign * 512;
[1]872
[43]873 case 'K':
874 case 'k':
875 return size * sign * 1024;
[1]876
[43]877 case 'M':
878 case 'm':
879 return size * sign * 1024 * 1024;
[1]880
[43]881 case 'G':
882 case 'g':
883 return size * sign * 1024 * 1024 * 1024;
[1]884
[43]885 case 'T':
886 case 't':
887 log_it
888 ("Ok, I'm scared... Someone did a TERABYTE+ size-spec");
889 return size * sign * 1024 * 1024 * 1024 * 1024;
[1]890
[43]891 case 'P':
892 case 'p':
893 log_it
894 ("If I was scared last time, I'm freaked out now. Someone actually has a PETABYTE?!?!?!?!");
895 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024;
[1]896
[43]897 case 'E':
898 case 'e':
899 log_it
900 ("Okay, I'm REALLY freaked out. Who could devote a whole EXABYTE to their data?!?!");
901 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
902 1024;
[1]903
[43]904 case 'Z':
905 case 'z':
906 log_it
907 ("WHAT!?!? A ZETABYTE!?!? You've GOT to be kidding me!!!");
908 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
909 1024 * 1024;
910
911 case 'Y':
912 case 'y':
913 log_it
914 ("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?!?!");
915 popup_and_OK
[507]916 (_("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]917 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
918 1024 * 1024 * 1024;
919 }
920 }
[1]921 }
[43]922 return size * sign;
[1]923}
924
925#endif
926
927
928
929
[561]930int parse_mdstat(struct raidlist_itself *raidlist, char *device_prefix) {
[43]931
[561]932 const char delims[] = " ";
933
934 FILE *fin;
[900]935 int row, i, index_min;
[561]936 int lastpos = 0;
937 size_t len = 0;
938 char *token;
939 char *string = NULL;
940 char *pos;
941 char type;
942 char *strtmp;
943
944 // open file
945 if (!(fin = fopen(MDSTAT_FILE, "r"))) {
[1086]946 mr_msg(1, "Could not open %s.\n", MDSTAT_FILE);
[561]947 return 1;
948 }
949 // initialise record, build progress and row counters
950 raidlist->entries = 0;
951 raidlist->el[raidlist->entries].progress = 999;
952 row = 1;
953 // skip first output row - contains registered RAID levels
[900]954 mr_getline(&string, &len, fin);
[561]955 // parse the rest
956 while ( !feof_unlocked(fin) ) {
[900]957 mr_getline(&string, &len, fin);
[561]958 // trim leading spaces
959 pos = string;
[783]960 while (*pos == ' ') pos++;
[900]961 mr_asprintf(&strtmp, pos);
962 mr_allocstr(string,strtmp);
963 mr_free(strtmp);
[561]964 // if we have newline after only spaces, this is a blank line, update
965 // counters, otherwise do normal parsing
966 if (*string == '\n') {
967 row = 1;
968 raidlist->entries++;
969 raidlist->el[raidlist->entries].progress = 999;
970 } else {
971 switch (row) {
972 case 1: // device information
973 // check whether last line of record and if so skip
974 pos = strcasestr(string, "unused devices: ");
975 if (pos == string) {
976 //raidlist->entries--;
977 break;
[43]978 }
[561]979 // tokenise string
980 token = mr_strtok (string, delims, &lastpos);
981 // get RAID device name
[900]982 mr_asprintf(&strtmp,"%s%s", device_prefix, token);
[783]983 raidlist->el[raidlist->entries].raid_device = strtmp;
[900]984 mr_free(token);
[561]985 // skip ':' and status
[588]986 token = mr_strtok (string, delims, &lastpos);
[900]987 mr_free(token);
[588]988 token = mr_strtok (string, delims, &lastpos);
[561]989 if (!strcmp(token, "inactive")) {
[1086]990 mr_msg(1, "RAID device '%s' inactive.\n",
[561]991 raidlist->el[raidlist->entries].raid_device);
[900]992 mr_free(string);
993 mr_free(token);
[561]994 return 1;
995 }
[900]996 mr_free(token);
[45]997
[561]998 // get RAID level
[588]999 token = mr_strtok (string, delims, &lastpos);
[561]1000 if (!strcmp(token, "multipath")) {
1001 raidlist->el[raidlist->entries].raid_level = -2;
1002 } else if (!strcmp(token, "linear")) {
1003 raidlist->el[raidlist->entries].raid_level = -1;
1004 } else if (!strcmp(token, "raid0")) {
1005 raidlist->el[raidlist->entries].raid_level = 0;
1006 } else if (!strcmp(token, "raid1")) {
1007 raidlist->el[raidlist->entries].raid_level = 1;
1008 } else if (!strcmp(token, "raid4")) {
1009 raidlist->el[raidlist->entries].raid_level = 4;
1010 } else if (!strcmp(token, "raid5")) {
1011 raidlist->el[raidlist->entries].raid_level = 5;
1012 } else if (!strcmp(token, "raid6")) {
1013 raidlist->el[raidlist->entries].raid_level = 6;
1014 } else if (!strcmp(token, "raid10")) {
1015 raidlist->el[raidlist->entries].raid_level = 10;
1016 } else {
[1086]1017 mr_msg(1, "Unknown RAID level '%s'.\n", token);
[900]1018 mr_free(string);
1019 mr_free(token);
[561]1020 return 1;
1021 }
[900]1022 mr_free(token);
[45]1023
[561]1024 // get RAID devices (type, index, device)
1025 // Note: parity disk for RAID4 is last normal disk, there is no '(P)'
1026 raidlist->el[raidlist->entries].data_disks.entries = 0;
1027 raidlist->el[raidlist->entries].spare_disks.entries = 0;
1028 raidlist->el[raidlist->entries].failed_disks.entries = 0;
[588]1029 while((token = mr_strtok (string, delims, &lastpos))) {
[561]1030 if ((pos = strstr(token, "("))) {
1031 type = *(pos+1);
1032 } else {
1033 type = ' ';
1034 }
1035 pos = strstr(token, "[");
1036 *pos = '\0';
1037 switch(type) {
1038 case ' ': // normal data disks
1039 raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].index = atoi(pos + 1);
[900]1040 mr_asprintf(&strtmp,"%s%s", device_prefix, token);
[561]1041 strcpy(raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].device, strtmp);
[900]1042 mr_free(strtmp);
[561]1043 raidlist->el[raidlist->entries].data_disks.entries++;
1044 break;
1045 case 'S': // spare disks
1046 raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].index = atoi(pos + 1);
[900]1047 mr_asprintf(&strtmp,"%s%s", device_prefix, token);
[561]1048 strcpy(raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].device, strtmp);
[900]1049 mr_free(strtmp);
[561]1050 raidlist->el[raidlist->entries].spare_disks.entries++;
1051 break;
1052 case 'F': // failed disks
1053 raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].index = atoi(pos + 1);
[900]1054 mr_asprintf(&strtmp,"%s%s", device_prefix, token);
[561]1055 strcpy(raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].device, strtmp);
[900]1056 mr_free(strtmp);
[561]1057 raidlist->el[raidlist->entries].failed_disks.entries++;
1058 log_it("At least one failed disk found in RAID array.\n");
1059 break;
1060 default: // error
[1086]1061 mr_msg(1, "Unknown device type '%c'\n", type);
[900]1062 mr_free(string);
1063 mr_free(token);
[561]1064 return 1;
1065 break;
1066 }
[900]1067 mr_free(token);
[43]1068 }
[561]1069
1070 // adjust index for each device so that it starts with 0 for every type
1071 index_min = 99;
1072 for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
1073 if (raidlist->el[raidlist->entries].data_disks.el[i].index < index_min) {
1074 index_min = raidlist->el[raidlist->entries].data_disks.el[i].index;
1075 }
1076 }
1077 if (index_min > 0) {
1078 for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
1079 raidlist->el[raidlist->entries].data_disks.el[i].index = raidlist->el[raidlist->entries].data_disks.el[i].index - index_min;
1080 }
1081 }
1082 index_min = 99;
1083 for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
1084 if (raidlist->el[raidlist->entries].spare_disks.el[i].index < index_min) {
1085 index_min = raidlist->el[raidlist->entries].spare_disks.el[i].index;
1086 }
1087 }
1088 if (index_min > 0) {
1089 for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
1090 raidlist->el[raidlist->entries].spare_disks.el[i].index = raidlist->el[raidlist->entries].spare_disks.el[i].index - index_min;
1091 }
1092 }
1093 index_min = 99;
1094 for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
1095 if (raidlist->el[raidlist->entries].failed_disks.el[i].index < index_min) {
1096 index_min = raidlist->el[raidlist->entries].failed_disks.el[i].index;
1097 }
1098 }
1099 if (index_min > 0) {
1100 for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
1101 raidlist->el[raidlist->entries].failed_disks.el[i].index = raidlist->el[raidlist->entries].failed_disks.el[i].index - index_min;
1102 }
1103 }
1104 break;
1105 case 2: // config information
1106 // check for persistent super block
1107 if (strcasestr(string, "super non-persistent")) {
1108 raidlist->el[raidlist->entries].persistent_superblock = 0;
1109 } else {
1110 raidlist->el[raidlist->entries].persistent_superblock = 1;
1111 }
1112 // extract chunk size
1113 if (!(pos = strcasestr(string, "k chunk"))) {
1114 raidlist->el[raidlist->entries].chunk_size = -1;
1115 } else {
1116 while (*pos != ' ') {
[672]1117 pos -= 1;
[561]1118 if (pos < string) {
1119 log_it("String underflow!\n");
[900]1120 mr_free(string);
[561]1121 return 1;
1122 }
1123 }
1124 raidlist->el[raidlist->entries].chunk_size = atoi(pos + 1);
1125 }
1126 // extract parity if present
1127 if ((pos = strcasestr(string, "algorithm"))) {
1128 raidlist->el[raidlist->entries].parity = atoi(pos + 9);
1129 } else {
1130 raidlist->el[raidlist->entries].parity = -1;
1131 }
1132 break;
1133 case 3: // optional build status information
1134 if (!(pos = strchr(string, '\%'))) {
1135 if (strcasestr(string, "delayed")) {
1136 raidlist->el[raidlist->entries].progress = -1; // delayed (therefore, stuck at 0%)
1137 } else {
1138 raidlist->el[raidlist->entries].progress = 999; // not found
1139 }
1140 } else {
1141 while (*pos != ' ') {
[672]1142 pos -= 1;
[561]1143 if (pos < string) {
1144 printf("ERROR: String underflow!\n");
[900]1145 mr_free(string);
[561]1146 return 1;
1147 }
1148 }
1149 raidlist->el[raidlist->entries].progress = atoi(pos);
1150 }
1151 break;
1152 default: // error
[1086]1153 mr_msg(1, "Row %d should not occur in record!\n", row);
[561]1154 break;
1155 }
1156 row++;
1157 }
1158 }
1159 // close file
1160 fclose(fin);
1161 // free string
[900]1162 mr_free(string);
[561]1163 // return success
1164 return 0;
1165
[1]1166}
1167
1168
1169
[561]1170
1171int create_raidtab_from_mdstat(char *raidtab_fname)
[1]1172{
[43]1173 struct raidlist_itself *raidlist;
1174 int retval = 0;
[1]1175
[1081]1176 raidlist = mr_malloc(sizeof(struct raidlist_itself));
[1]1177
[561]1178 // FIXME: Prefix '/dev/' should really be dynamic!
1179 if (parse_mdstat(raidlist, "/dev/")) {
1180 log_to_screen("Sorry, cannot read %s", MDSTAT_FILE);
[43]1181 return (1);
1182 }
1183
1184 retval += save_raidlist_to_raidtab(raidlist, raidtab_fname);
1185 return (retval);
[1]1186}
1187
1188
1189/* @} - end of raidGroup */
Note: See TracBrowser for help on using the repository browser.