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

Last change on this file since 764 was 689, checked in by bcornec, 18 years ago

Still other memory management improvements ( I hope :-)

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