source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-raid.c@ 2357

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