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

Last change on this file since 147 was 87, checked in by bcornec, 18 years ago
  • Now use -Wall to compile
  • asprintf for newt-specific.c
  • Bug in libmondo-string.c line 1120: if we use %% in this format, then the percentage is printeed wrongly (after the real percentage we have a huge number)
  • Property svn:keywords set to Id
File size: 25.9 KB
Line 
1/* $Id: libmondo-raid.c 87 2005-10-27 20:03:00Z bcornec $
2 subroutines for handling RAID
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 87 2005-10-27 20:03:00Z 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,
51 "cat /proc/mdstat | grep \"linear\" > /dev/null 2> /dev/null");
52 } else {
53 asprintf(&command,
54 "cat /proc/mdstat | grep \"raid%d\" > /dev/null 2> /dev/null",
55 raidno);
56 }
57 log_it("Is raid %d registered? Command = '%s'", raidno, command);
58 res = system(command);
59 paranoid_free(command);
60 if (res) {
61 return (FALSE);
62 } else {
63 return (TRUE);
64 }
65#endif
66}
67
68
69/**
70 * Search for @p device in @p disklist.
71 * @param disklist The disklist to search in.
72 * @param device The device to search for.
73 * @return The index number of @p device, or -1 if it does not exist.
74 */
75int
76where_in_drivelist_is_drive(struct list_of_disks *disklist, char *device)
77{
78
79 /*@ int ************************************************************* */
80 int i = 0;
81
82 assert(disklist != NULL);
83 assert_string_is_neither_NULL_nor_zerolength(device);
84
85 for (i = 0; i < disklist->entries; i++) {
86 if (!strcmp(disklist->el[i].device, device)) {
87 break;
88 }
89 }
90 if (i == disklist->entries) {
91 return (-1);
92 } else {
93 return (i);
94 }
95}
96
97/**
98 * Determine which RAID device is using a particular partition.
99 * @param raidlist The RAID information structure.
100 * @param device The partition to find out about.
101 * @return The index number of the RAID device using @p device, or -1 if there is none.
102 */
103int
104which_raid_device_is_using_this_partition(struct raidlist_itself *raidlist,
105 char *device)
106{
107#ifdef __FreeBSD__
108// FreeBSD-specific version of which_raid_device_is_using_this_partition()
109 /*@ int ********************************************************* */
110 int i = 0;
111
112 for (i = 0; i < raidlist->entries; i++) {
113 bool thisone = FALSE;
114 int j, k, l;
115
116 for (j = 0; j < raidlist->el[i].plexes; ++j) {
117 for (k = 0; k < raidlist->el[i].plex[j].subdisks; ++k) {
118 for (l = 0; l < raidlist->disks.entries; ++l) {
119 if (!strcmp(raidlist->disks.el[l].device,
120 device) &&
121 !strcmp(raidlist->el[i].plex[j].sd[k].which_device,
122 raidlist->disks.el[l].name))
123 thisone = TRUE;
124 }
125 }
126 }
127
128 if (thisone) {
129 break;
130 }
131 }
132 if (i == raidlist->entries) {
133 return (-1);
134 } else {
135 return (i);
136 }
137}
138
139#else
140// Linux-specific version of which_raid_device_is_using_this_partition()
141// and one other function which FreeBSD doesn't use
142
143 int current_raiddev = 0;
144
145 assert_string_is_neither_NULL_nor_zerolength(device);
146 assert(raidlist != NULL);
147
148 for (current_raiddev = 0; current_raiddev < raidlist->entries;
149 current_raiddev++) {
150 if (where_in_drivelist_is_drive
151 (&raidlist->el[current_raiddev].data_disks, device) >= 0
152 || where_in_drivelist_is_drive(&raidlist->el[current_raiddev].
153 spare_disks, device) >= 0
154 || where_in_drivelist_is_drive(&raidlist->el[current_raiddev].
155 parity_disks, device) >= 0
156 || where_in_drivelist_is_drive(&raidlist->el[current_raiddev].
157 failed_disks, device) >= 0) {
158 break;
159 }
160 }
161 if (current_raiddev == raidlist->entries) {
162 return (-1);
163 } else {
164 return (current_raiddev);
165 }
166}
167
168/**
169 * Write an @c int variable to a list of RAID variables.
170 * @param raidrec The RAID device record to write to.
171 * @param lino The variable index number to modify/create.
172 * @param label The label to write.
173 * @param value The value to write.
174 */
175void
176write_variableINT_to_raid_var_line(struct raid_device_record *raidrec,
177 int lino, char *label, int value)
178{
179 /*@ buffers ***************************************************** */
180 char *sz_value;
181
182 assert(raidrec != NULL);
183 assert(label != NULL);
184
185 asprintf(&sz_value, "%d", value);
186 strcpy(raidrec->additional_vars.el[lino].label, label);
187 strcpy(raidrec->additional_vars.el[lino].value, sz_value);
188 paranoid_free(sz_value);
189}
190#endif
191
192#ifdef __FreeBSD__
193/**
194 * Add a disk to a RAID plex.
195 * @param p The plex to add the device to.
196 * @param device_to_add The device to add to @p p.
197 */
198void add_disk_to_raid_device(struct vinum_plex *p, char *device_to_add)
199{
200 strcpy(p->sd[p->subdisks].which_device, device_to_add);
201 ++p->subdisks;
202
203}
204#else
205/**
206 * Add a disk to a RAID device.
207 * @param disklist The disklist to add the device to.
208 * @param device_to_add The device to add to @p disklist.
209 * @param index The index number of the disklist entry we're creating.
210 */
211void add_disk_to_raid_device(struct list_of_disks *disklist,
212 char *device_to_add, int index)
213{
214 int items;
215
216 assert(disklist != NULL);
217 assert_string_is_neither_NULL_nor_zerolength(device_to_add);
218 items = disklist->entries;
219 strcpy(disklist->el[items].device, device_to_add);
220 disklist->el[items].index = index;
221 items++;
222 disklist->entries = items;
223}
224#endif
225
226/**
227 * Save the additional RAID variables to a stream.
228 * @param vars The RAID variable list to save.
229 * @param fout The FILE pointer to save them to.
230 */
231void
232save_additional_vars_to_file(struct additional_raid_variables *vars,
233 FILE * fout)
234{
235 int i;
236
237 assert(vars != NULL);
238 assert(fout != NULL);
239
240 for (i = 0; i < vars->entries; i++) {
241 fprintf(fout, " %-21s %s\n", vars->el[i].label,
242 vars->el[i].value);
243 }
244}
245
246
247/**
248 * Save a raidlist structure to disk in raidtab format.
249 * @param raidlist The raidlist to save.
250 * @param fname The file to save it to.
251 * @return 0, always.
252 * @bug Return value is redundant.
253 */
254int save_raidlist_to_raidtab(struct raidlist_itself *raidlist, char *fname)
255{
256 FILE *fout;
257 int current_raid_device;
258#ifdef __FreeBSD__
259 int i;
260#else
261// Linux
262#endif
263
264 assert(raidlist != NULL);
265 assert_string_is_neither_NULL_nor_zerolength(fname);
266
267 if (raidlist->entries <= 0) {
268 unlink(fname);
269 log_it("Deleting raidtab (no RAID devs anyway)");
270 return (0);
271 }
272 if (!(fout = fopen(fname, "w"))) {
273 log_OS_error("Failed to save raidlist");
274 return (1);
275 }
276 fprintf(fout, "# Generated by Mondo Rescue\n");
277
278#ifdef __FreeBSD__
279 for (i = 0; i < raidlist->disks.entries; ++i) {
280 fprintf(fout, "drive %s device %s\n", raidlist->disks.el[i].name,
281 raidlist->disks.el[i].device);
282 }
283 for (i = 0; i < (raidlist->spares.entries); ++i) {
284 fprintf(fout, "drive %s device %s hotspare\n",
285 raidlist->spares.el[i].name,
286 raidlist->spares.el[i].device);
287 }
288#endif
289
290 for (current_raid_device = 0; current_raid_device < raidlist->entries;
291 current_raid_device++) {
292 save_raidrec_to_file(&raidlist->el[current_raid_device], fout);
293 }
294 paranoid_fclose(fout);
295 return (0);
296}
297
298
299/**
300 * Save an individual RAID device record to a stream.
301 * @param raidrec The RAID device record to save.
302 * @param fout The stream to save it to.
303 */
304void save_raidrec_to_file(struct
305#ifdef __FreeBSD__
306 vinum_volume
307#else
308 raid_device_record
309#endif
310 * raidrec, FILE * fout)
311{
312#ifdef __FreeBSD__
313 int i, j;
314
315 fprintf(fout, "\nvolume %s\n", raidrec->volname);
316 for (i = 0; i < raidrec->plexes; ++i) {
317 char *org;
318 switch (raidrec->plex[i].raidlevel) {
319 case -1:
320 asprintf(&org, "%s", "concat");
321 break;
322 case 0:
323 asprintf(&org, "%s", "striped");
324 break;
325 case 5:
326 asprintf(&org, "%s", "raid5");
327 break;
328 }
329 fprintf(fout, " plex org %s", org);
330 paranoid_free(org);
331
332 if (raidrec->plex[i].raidlevel != -1) {
333 fprintf(fout, " %ik", raidrec->plex[i].stripesize);
334 }
335 fprintf(fout, "\n");
336
337 for (j = 0; j < raidrec->plex[i].subdisks; ++j) {
338 fprintf(fout, " sd drive %s size 0\n",
339 raidrec->plex[i].sd[j].which_device);
340 }
341 }
342#else
343 assert(raidrec != NULL);
344 assert(fout != NULL);
345
346 fprintf(fout, "raiddev %s\n", raidrec->raid_device);
347 if (raidrec->raid_level == -1) {
348 fprintf(fout, " raid-level linear\n");
349 } else {
350 fprintf(fout, " raid-level %d\n",
351 raidrec->raid_level);
352 }
353 fprintf(fout, " chunk-size %d\n", raidrec->chunk_size);
354 fprintf(fout, " nr-raid-disks %d\n",
355 raidrec->data_disks.entries);
356 fprintf(fout, " nr-spare-disks %d\n",
357 raidrec->spare_disks.entries);
358 if (raidrec->parity_disks.entries > 0) {
359 fprintf(fout, " nr-parity-disks %d\n",
360 raidrec->parity_disks.entries);
361 }
362
363 fprintf(fout, " persistent-superblock %d\n",
364 raidrec->persistent_superblock);
365 save_additional_vars_to_file(&raidrec->additional_vars, fout);
366 fprintf(fout, "\n");
367 save_disklist_to_file("raid-disk", &raidrec->data_disks, fout);
368 save_disklist_to_file("spare-disk", &raidrec->spare_disks, fout);
369 save_disklist_to_file("parity-disk", &raidrec->parity_disks, fout);
370 save_disklist_to_file("failed-disk", &raidrec->failed_disks, fout);
371 fprintf(fout, "\n");
372#endif
373}
374
375/**
376 * Retrieve the next line from a raidtab stream.
377 * @param fin The file to read the input from.
378 * @param label Where to put the line's label.
379 * @param value Where to put the line's value.
380 * @return 0 if the line was read and stored successfully, 1 if we're at end of file.
381 */
382int get_next_raidtab_line(FILE * fin, char *label, char *value)
383{
384 char *incoming = NULL;
385 char *p;
386 size_t n = 0;
387
388 assert(fin != NULL);
389 assert(label != NULL);
390 assert(value != NULL);
391
392 label[0] = value[0] = '\0';
393 if (feof(fin)) {
394 return (1);
395 }
396
397 for (getline(&incoming, &n, fin); !feof(fin);
398 getline(&incoming, &n, fin)) {
399 strip_spaces(incoming);
400 p = strchr(incoming, ' ');
401 if (strlen(incoming) < 3 || incoming[0] == '#' || !p) {
402 continue;
403 }
404 *(p++) = '\0';
405 while (*p == ' ') {
406 p++;
407 }
408 strcpy(label, incoming);
409 strcpy(value, p);
410 paranoid_free(incoming);
411 return (0);
412 }
413 paranoid_free(incoming);
414 return (1);
415}
416
417
418/**
419 * Load a raidtab file into a raidlist structure.
420 * @param raidlist The raidlist to fill.
421 * @param fname The file to read from.
422 * @return 0 for success, 1 for failure.
423 */
424#ifdef __FreeBSD__
425int load_raidtab_into_raidlist(struct raidlist_itself *raidlist,
426 char *fname)
427{
428 FILE *fin;
429 char *tmp1;
430 int items;
431
432 raidlist->spares.entries = 0;
433 raidlist->disks.entries = 0;
434 if (length_of_file(fname) < 5) {
435 log_it("Raidtab is very small or non-existent. Ignoring it.");
436 raidlist->entries = 0;
437 return (0);
438 }
439 if (!(fin = fopen(fname, "r"))) {
440 log_it("Cannot open raidtab");
441 return (1);
442 }
443 items = 0;
444 log_it("Loading raidtab...");
445 while (!feof(fin)) {
446 int argc;
447 char **argv = get_next_vinum_conf_line(fin, &argc);
448 if (!argv)
449 break;
450 if (!strcmp(argv[0], "drive")) {
451 char *drivename, *devname;
452 if (argc < 4)
453 continue;
454 drivename = argv[1];
455 devname = get_option_val(argc, argv, "device");
456 if (!devname)
457 continue;
458
459 if (get_option_state(argc, argv, "hotspare")) {
460 strcpy(raidlist->spares.el[raidlist->spares.entries].name,
461 drivename);
462 strcpy(raidlist->spares.el[raidlist->spares.entries].
463 device, devname);
464 raidlist->spares.el[raidlist->spares.entries].index =
465 raidlist->disks.entries;
466 raidlist->spares.entries++;
467 } else {
468 strcpy(raidlist->disks.el[raidlist->disks.entries].name,
469 drivename);
470 strcpy(raidlist->disks.el[raidlist->disks.entries].device,
471 devname);
472 raidlist->disks.el[raidlist->disks.entries].index =
473 raidlist->disks.entries;
474 raidlist->disks.entries++;
475 }
476 } else if (!strcmp(argv[0], "volume")) {
477 char *volname;
478 if (argc < 2)
479 continue;
480 volname = argv[1];
481 strcpy(raidlist->el[raidlist->entries].volname, volname);
482 raidlist->el[raidlist->entries].plexes = 0;
483 raidlist->entries++;
484 } else if (!strcmp(argv[0], "plex")) {
485 int raidlevel, stripesize;
486 char *org = 0;
487 char **tmp = 0;
488 if (argc < 3)
489 continue;
490 org = get_option_val(argc, argv, "org");
491 if (!org)
492 continue;
493 if (strcmp(org, "concat")) {
494 tmp = get_option_vals(argc, argv, "org", 2);
495 if (tmp && tmp[1]) {
496 stripesize = (int) (size_spec(tmp[1]) / 1024);
497 } else
498 stripesize = 279;
499 } else
500 stripesize = 0;
501
502 if (!strcmp(org, "concat")) {
503 raidlevel = -1;
504 } else if (!strcmp(org, "striped")) {
505 raidlevel = 0;
506 } else if (!strcmp(org, "raid5")) {
507 raidlevel = 5;
508 } else
509 continue;
510
511 raidlist->el[raidlist->entries - 1].plex
512 [raidlist->el[raidlist->entries - 1].plexes].raidlevel =
513 raidlevel;
514 raidlist->el[raidlist->entries -
515 1].plex[raidlist->el[raidlist->entries -
516 1].plexes].stripesize =
517 stripesize;
518 raidlist->el[raidlist->entries -
519 1].plex[raidlist->el[raidlist->entries -
520 1].plexes].subdisks = 0;
521 raidlist->el[raidlist->entries - 1].plexes++;
522 } else if ((!strcmp(argv[0], "sd"))
523 || (!strcmp(argv[0], "subdisk"))) {
524 char *drive = 0;
525 if (argc < 3)
526 continue;
527 drive = get_option_val(argc, argv, "drive");
528 if (!drive)
529 continue;
530
531 strcpy(raidlist->el[raidlist->entries - 1].plex
532 [raidlist->el[raidlist->entries - 1].plexes - 1].sd
533 [raidlist->el[raidlist->entries - 1].plex
534 [raidlist->el[raidlist->entries - 1].plexes -
535 1].subdisks].which_device, drive);
536 raidlist->el[raidlist->entries -
537 1].plex[raidlist->el[raidlist->entries -
538 1].plexes - 1].subdisks++;
539 }
540 }
541 fclose(fin);
542 log_it("Raidtab loaded successfully.");
543 asprintf(&tmp1, "%d RAID devices in raidtab", raidlist->entries);
544 log_it(tmp1);
545 paranoid_free(tmp1);
546 return (0);
547}
548
549
550#else
551
552int load_raidtab_into_raidlist(struct raidlist_itself *raidlist,
553 char *fname)
554{
555 FILE *fin;
556 char *label;
557 char *value;
558 int items;
559 int v;
560
561 assert(raidlist != NULL);
562 assert_string_is_neither_NULL_nor_zerolength(fname);
563
564 if (length_of_file(fname) < 5) {
565 log_it("Raidtab is very small or non-existent. Ignoring it.");
566 raidlist->entries = 0;
567 return (0);
568 }
569 if (!(fin = fopen(fname, "r"))) {
570 log_it("Cannot open raidtab");
571 return (1);
572 }
573 items = 0;
574 log_it("Loading raidtab...");
575 malloc_string(label);
576 malloc_string(value);
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 = NULL;
907 char *p, *q, *r;
908 int diskno;
909 size_t n = 0;
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 (getline(&incoming, &n, fin); !feof(fin);
918 getline(&incoming, &n, 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
967 log_msg(8, "/dev/md%d : disk#%d : %s (%d)",
968 mdstat->el[mdstat->entries].md, diskno, tmp,
969 mdstat->el[mdstat->entries].disks.el[diskno].index);
970 strcpy(mdstat->el[mdstat->entries].disks.el[diskno].device,
971 tmp);
972 paranoid_free(tmp);
973
974 while (*p != ' ' && *p) {
975 p++;
976 }
977 while (*p == ' ' && *p) {
978 p++;
979 }
980 }
981 mdstat->el[mdstat->entries].disks.entries = diskno;
982// next line --- skip it
983 if (!feof(fin)) {
984 getline(&incoming, &n, fin);
985 } else {
986 continue;
987 }
988// next line --- the 'progress' line
989 if (!feof(fin)) {
990 getline(&incoming, &n, fin);
991 } else {
992 continue;
993 }
994// log_msg(1, "Percentage line = '%s'", incoming);
995 if (!(p = strchr(incoming, '\%'))) {
996 mdstat->el[mdstat->entries].progress = 999; // not found
997 } else if (strstr(incoming, "DELAYED")) {
998 mdstat->el[mdstat->entries].progress = -1; // delayed (therefore, stuck at 0%)
999 } else {
1000 for (*p = '\0'; *p != ' '; p--);
1001 mdstat->el[mdstat->entries].progress = atoi(p);
1002 }
1003 log_msg(8, "progress =%d", mdstat->el[mdstat->entries].progress);
1004 mdstat->entries++;
1005 }
1006 fclose(fin);
1007 paranoid_free(incoming);
1008 return (0);
1009}
1010
1011
1012
1013int create_raidtab_from_mdstat(char *raidtab_fname, char *mdstat_fname)
1014{
1015 struct raidlist_itself *raidlist;
1016 struct s_mdstat *mdstat;
1017 int retval = 0;
1018 int i;
1019
1020 raidlist = malloc(sizeof(struct raidlist_itself));
1021 mdstat = malloc(sizeof(struct s_mdstat));
1022
1023 if (read_mdstat(mdstat, mdstat_fname)) {
1024 log_to_screen("Sorry, cannot read %s", mdstat_fname);
1025 return (1);
1026 }
1027
1028 for (i = 0; i < mdstat->entries; i++) {
1029 sprintf(raidlist->el[i].raid_device, "/dev/md%d",
1030 mdstat->el[i].md);
1031 raidlist->el[i].raid_level = mdstat->el[i].raidlevel;
1032 raidlist->el[i].persistent_superblock = 1;
1033 raidlist->el[i].chunk_size = 4;
1034 memcpy((void *) &raidlist->el[i].data_disks,
1035 (void *) &mdstat->el[i].disks,
1036 sizeof(struct list_of_disks));
1037 // FIXME --- the above line does not allow for spare disks
1038 log_to_screen
1039 ("FIXME - create_raidtab_from_mdstat does not allow for spare disks");
1040 }
1041 raidlist->entries = i;
1042 retval += save_raidlist_to_raidtab(raidlist, raidtab_fname);
1043 return (retval);
1044}
1045
1046
1047/* @} - end of raidGroup */
Note: See TracBrowser for help on using the repository browser.