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

Last change on this file since 2405 was 2405, checked in by Bruno Cornec, 15 years ago

Removes some malloc_string static allocation

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