source: MondoRescue/branches/3.2/mondo/src/common/libmondo-raid.c@ 3380

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