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

Last change on this file was 3614, checked in by Bruno Cornec, 7 years ago

mr_strip_spaces now returns a dynamically allocated string

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