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

Last change on this file since 43 was 43, checked in by bcornec, 19 years ago

asprintf + memory management on libmondo-raid.c

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