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

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

merge -r 542:560 $SVN_M/branches/stable

  • Property svn:keywords set to Id
File size: 31.8 KB
RevLine 
[45]1/* $Id: libmondo-raid.c 561 2006-05-20 15:51:21Z 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 561 2006-05-20 15:51:21Z 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 */
[43]405int get_next_raidtab_line(FILE * fin, char *label, char *value)
[1]406{
[45]407 char *incoming = NULL;
[43]408 char *p;
[45]409 size_t n = 0;
[1]410
[43]411 assert(fin != NULL);
412 assert(label != NULL);
413 assert(value != NULL);
[1]414
[43]415 label[0] = value[0] = '\0';
416 if (feof(fin)) {
417 return (1);
[1]418 }
[43]419
[45]420 for (getline(&incoming, &n, fin); !feof(fin);
421 getline(&incoming, &n, fin)) {
[43]422 strip_spaces(incoming);
423 p = strchr(incoming, ' ');
424 if (strlen(incoming) < 3 || incoming[0] == '#' || !p) {
425 continue;
426 }
427 *(p++) = '\0';
428 while (*p == ' ') {
429 p++;
430 }
431 strcpy(label, incoming);
432 strcpy(value, p);
433 paranoid_free(incoming);
434 return (0);
435 }
436 paranoid_free(incoming);
437 return (1);
[1]438}
439
440
441/**
442 * Load a raidtab file into a raidlist structure.
443 * @param raidlist The raidlist to fill.
444 * @param fname The file to read from.
445 * @return 0 for success, 1 for failure.
446 */
447#ifdef __FreeBSD__
[43]448int load_raidtab_into_raidlist(struct raidlist_itself *raidlist,
449 char *fname)
[1]450{
[43]451 FILE *fin;
452 char *tmp1;
453 int items;
[1]454
[43]455 raidlist->spares.entries = 0;
456 raidlist->disks.entries = 0;
457 if (length_of_file(fname) < 5) {
458 log_it("Raidtab is very small or non-existent. Ignoring it.");
459 raidlist->entries = 0;
460 return (0);
[1]461 }
[43]462 if (!(fin = fopen(fname, "r"))) {
463 log_it("Cannot open raidtab");
464 return (1);
[1]465 }
[43]466 items = 0;
467 log_it("Loading raidtab...");
468 while (!feof(fin)) {
469 int argc;
470 char **argv = get_next_vinum_conf_line(fin, &argc);
471 if (!argv)
472 break;
473 if (!strcmp(argv[0], "drive")) {
474 char *drivename, *devname;
475 if (argc < 4)
476 continue;
477 drivename = argv[1];
478 devname = get_option_val(argc, argv, "device");
479 if (!devname)
480 continue;
[1]481
[43]482 if (get_option_state(argc, argv, "hotspare")) {
483 strcpy(raidlist->spares.el[raidlist->spares.entries].name,
484 drivename);
485 strcpy(raidlist->spares.el[raidlist->spares.entries].
486 device, devname);
487 raidlist->spares.el[raidlist->spares.entries].index =
488 raidlist->disks.entries;
489 raidlist->spares.entries++;
490 } else {
491 strcpy(raidlist->disks.el[raidlist->disks.entries].name,
492 drivename);
493 strcpy(raidlist->disks.el[raidlist->disks.entries].device,
494 devname);
495 raidlist->disks.el[raidlist->disks.entries].index =
496 raidlist->disks.entries;
497 raidlist->disks.entries++;
498 }
499 } else if (!strcmp(argv[0], "volume")) {
500 char *volname;
501 if (argc < 2)
502 continue;
503 volname = argv[1];
504 strcpy(raidlist->el[raidlist->entries].volname, volname);
505 raidlist->el[raidlist->entries].plexes = 0;
506 raidlist->entries++;
507 } else if (!strcmp(argv[0], "plex")) {
508 int raidlevel, stripesize;
509 char *org = 0;
510 char **tmp = 0;
511 if (argc < 3)
512 continue;
513 org = get_option_val(argc, argv, "org");
514 if (!org)
515 continue;
516 if (strcmp(org, "concat")) {
517 tmp = get_option_vals(argc, argv, "org", 2);
518 if (tmp && tmp[1]) {
519 stripesize = (int) (size_spec(tmp[1]) / 1024);
520 } else
521 stripesize = 279;
522 } else
523 stripesize = 0;
[1]524
[43]525 if (!strcmp(org, "concat")) {
526 raidlevel = -1;
527 } else if (!strcmp(org, "striped")) {
528 raidlevel = 0;
529 } else if (!strcmp(org, "raid5")) {
530 raidlevel = 5;
531 } else
532 continue;
533
534 raidlist->el[raidlist->entries - 1].plex
535 [raidlist->el[raidlist->entries - 1].plexes].raidlevel =
536 raidlevel;
537 raidlist->el[raidlist->entries -
538 1].plex[raidlist->el[raidlist->entries -
539 1].plexes].stripesize =
540 stripesize;
541 raidlist->el[raidlist->entries -
542 1].plex[raidlist->el[raidlist->entries -
543 1].plexes].subdisks = 0;
544 raidlist->el[raidlist->entries - 1].plexes++;
545 } else if ((!strcmp(argv[0], "sd"))
546 || (!strcmp(argv[0], "subdisk"))) {
547 char *drive = 0;
548 if (argc < 3)
549 continue;
550 drive = get_option_val(argc, argv, "drive");
551 if (!drive)
552 continue;
553
554 strcpy(raidlist->el[raidlist->entries - 1].plex
555 [raidlist->el[raidlist->entries - 1].plexes - 1].sd
556 [raidlist->el[raidlist->entries - 1].plex
557 [raidlist->el[raidlist->entries - 1].plexes -
558 1].subdisks].which_device, drive);
559 raidlist->el[raidlist->entries -
560 1].plex[raidlist->el[raidlist->entries -
561 1].plexes - 1].subdisks++;
562 }
[1]563 }
[43]564 fclose(fin);
565 log_it("Raidtab loaded successfully.");
566 asprintf(&tmp1, "%d RAID devices in raidtab", raidlist->entries);
567 log_it(tmp1);
568 paranoid_free(tmp1);
569 return (0);
[1]570}
571
572
573#else
574
[43]575int load_raidtab_into_raidlist(struct raidlist_itself *raidlist,
576 char *fname)
[1]577{
[43]578 FILE *fin;
579 char *label;
580 char *value;
581 int items;
582 int v;
[1]583
[43]584 assert(raidlist != NULL);
585 assert_string_is_neither_NULL_nor_zerolength(fname);
[1]586
[43]587 if (length_of_file(fname) < 5) {
588 log_it("Raidtab is very small or non-existent. Ignoring it.");
589 raidlist->entries = 0;
590 return (0);
[1]591 }
[43]592 if (!(fin = fopen(fname, "r"))) {
593 log_it("Cannot open raidtab");
594 return (1);
[1]595 }
[43]596 items = 0;
597 log_it("Loading raidtab...");
[45]598 malloc_string(label);
599 malloc_string(value);
[43]600 get_next_raidtab_line(fin, label, value);
601 while (!feof(fin)) {
602 log_msg(1, "Looking for raid record #%d", items);
603 initialize_raidrec(&raidlist->el[items]);
604 v = 0;
605 /* find the 'raiddev' entry, indicating the start of the block of info */
606 while (!feof(fin) && strcmp(label, "raiddev")) {
607 strcpy(raidlist->el[items].additional_vars.el[v].label, label);
608 strcpy(raidlist->el[items].additional_vars.el[v].value, value);
609 v++;
610 get_next_raidtab_line(fin, label, value);
611 }
612 raidlist->el[items].additional_vars.entries = v;
613 if (feof(fin)) {
614 log_msg(1, "No more records.");
615 continue;
616 }
617 log_msg(2, "Record #%d (%s) found", items, value);
618 strcpy(raidlist->el[items].raid_device, value);
619 for (get_next_raidtab_line(fin, label, value);
620 !feof(fin) && strcmp(label, "raiddev");
621 get_next_raidtab_line(fin, label, value)) {
622 process_raidtab_line(fin, &raidlist->el[items], label, value);
623 }
624 items++;
[1]625 }
[43]626 paranoid_fclose(fin);
627 raidlist->entries = items;
628 log_msg(1, "Raidtab loaded successfully.");
629 log_msg(1, "%d RAID devices in raidtab", items);
630 paranoid_free(label);
631 paranoid_free(value);
632 return (0);
[1]633}
634#endif
635
636
637#ifndef __FreeBSD__
638/**
639 * Process a single line from the raidtab and store the results into @p raidrec.
640 * @param fin The stream to read the line from.
641 * @param raidrec The RAID device record to update.
642 * @param label Where to put the label processed.
643 * @param value Where to put the value processed.
644 */
645void
[43]646process_raidtab_line(FILE * fin,
647 struct raid_device_record *raidrec,
648 char *label, char *value)
[1]649{
650
[43]651 /*@ add mallocs * */
652 char *tmp;
653 char *labelB;
654 char *valueB;
[1]655
[43]656 struct list_of_disks *disklist;
657 int index;
658 int v;
[1]659
[43]660 malloc_string(labelB);
661 malloc_string(valueB);
662 assert(fin != NULL);
663 assert(raidrec != NULL);
664 assert_string_is_neither_NULL_nor_zerolength(label);
665 assert(value != NULL);
[1]666
[43]667 if (!strcmp(label, "raid-level")) {
[561]668 if (!strcmp(value, "multipath")) {
669 raidrec->raid_level = -2;
670 } else if (!strcmp(value, "linear")) {
[43]671 raidrec->raid_level = -1;
672 } else {
673 raidrec->raid_level = atoi(value);
674 }
675 } else if (!strcmp(label, "nr-raid-disks")) { /* ignore it */
676 } else if (!strcmp(label, "nr-spare-disks")) { /* ignore it */
677 } else if (!strcmp(label, "nr-parity-disks")) { /* ignore it */
678 } else if (!strcmp(label, "nr-failed-disks")) { /* ignore it */
679 } else if (!strcmp(label, "persistent-superblock")) {
680 raidrec->persistent_superblock = atoi(value);
681 } else if (!strcmp(label, "chunk-size")) {
682 raidrec->chunk_size = atoi(value);
[561]683 } else if (!strcmp(label, "parity-algorithm")) {
684 if (!strcmp(value, "left-asymmetric")) {
685 raidrec->parity = 0;
686 } else if (!strcmp(value, "right-asymmetric")) {
687 raidrec->parity = 1;
688 } else if (!strcmp(value, "left-symmetric")) {
689 raidrec->parity = 2;
690 } else if (!strcmp(value, "right-symmetric")) {
691 raidrec->parity = 3;
692 } else {
693 log_msg(1, "Unknown RAID parity algorithm '%s'\n.", value);
694 }
[43]695 } else if (!strcmp(label, "device")) {
696 get_next_raidtab_line(fin, labelB, valueB);
697 if (!strcmp(labelB, "raid-disk")) {
698 disklist = &raidrec->data_disks;
699 } else if (!strcmp(labelB, "spare-disk")) {
700 disklist = &raidrec->spare_disks;
701 } else if (!strcmp(labelB, "parity-disk")) {
702 disklist = &raidrec->parity_disks;
703 } else if (!strcmp(labelB, "failed-disk")) {
704 disklist = &raidrec->failed_disks;
705 } else {
706 disklist = NULL;
707 }
708 if (!disklist) {
709 asprintf(&tmp,
[45]710 "Ignoring '%s %s' pair of disk %s", labelB, valueB,
711 label);
[43]712 log_it(tmp);
713 paranoid_free(tmp);
714 } else {
715 index = atoi(valueB);
716 add_disk_to_raid_device(disklist, value, index);
717 }
718 } else {
719 v = raidrec->additional_vars.entries;
720 strcpy(raidrec->additional_vars.el[v].label, label);
721 strcpy(raidrec->additional_vars.el[v].value, value);
722 raidrec->additional_vars.entries = ++v;
[1]723 }
[43]724 paranoid_free(labelB);
725 paranoid_free(valueB);
[1]726}
727#endif
728
729
730/**
731 * Save a disklist to a stream in raidtab format.
732 * @param listname One of "raid-disk", "spare-disk", "parity-disk", or "failed-disk".
733 * @param disklist The disklist to save to @p fout.
734 * @param fout The stream to write to.
735 */
[43]736void
737save_disklist_to_file(char *listname,
738 struct list_of_disks *disklist, FILE * fout)
[1]739{
[43]740 int i;
[1]741
[43]742 assert_string_is_neither_NULL_nor_zerolength(listname);
743 assert(disklist != NULL);
744 assert(fout != NULL);
[1]745
[43]746 for (i = 0; i < disklist->entries; i++) {
747 fprintf(fout, " device %s\n",
748 disklist->el[i].device);
749 fprintf(fout, " %-21s %d\n", listname, disklist->el[i].index);
750 }
[1]751}
752
753
754#ifdef __FreeBSD__
755/**
756 * Add a new plex to a volume. The index of the plex will be <tt>v-\>plexes - 1</tt>.
757 * @param v The volume to operate on.
758 * @param raidlevel The RAID level of the new plex.
759 * @param stripesize The stripe size (chunk size) of the new plex.
760 */
[43]761void add_plex_to_volume(struct vinum_volume *v, int raidlevel,
762 int stripesize)
[1]763{
[43]764 v->plex[v->plexes].raidlevel = raidlevel;
765 v->plex[v->plexes].stripesize = stripesize;
766 v->plex[v->plexes].subdisks = 0;
767 ++v->plexes;
[1]768}
769
770/**
771 * For internal use only.
772 */
[43]773char **get_next_vinum_conf_line(FILE * f, int *argc)
[1]774{
[43]775 int cnt = 0;
776 static char *argv[64];
777 char **ap;
778 char *line = (char *) malloc(MAX_STR_LEN);
779 if (!line)
780 errx(1,
781 "unable to allocate %i bytes of memory for `char *line' at %s:%i",
782 MAX_STR_LEN, __FILE__, __LINE__);
783 (void) fgets(line, MAX_STR_LEN, f);
784 if (feof(f)) {
785 log_it("[GNVCL] Uh... I reached the EOF.");
786 return 0;
[1]787 }
788
[43]789 for (ap = argv; (*ap = strsep(&line, " \t")) != NULL;)
790 if (**ap != '\0') {
791 if (++ap >= &argv[64])
792 break;
793 cnt++;
794 }
[1]795
[43]796 if (strchr(argv[cnt - 1], '\n')) {
797 *(strchr(argv[cnt - 1], '\n')) = '\0';
798 }
799
800 if (argc)
801 *argc = cnt;
802 return argv;
[1]803}
804
805/**
806 * For internal use only.
807 */
[43]808char *get_option_val(int argc, char **argv, char *option)
[1]809{
[43]810 int i;
811 for (i = 0; i < (argc - 1); ++i) {
812 if (!strcmp(argv[i], option)) {
813 return argv[i + 1];
814 }
[1]815 }
[43]816 return 0;
[1]817}
818
819/**
820 * For internal use only.
821 */
[43]822char **get_option_vals(int argc, char **argv, char *option, int nval)
[1]823{
[43]824 int i, j;
825 static char **ret;
826 ret = (char **) malloc(nval * sizeof(char *));
827 for (i = 0; i < (argc - nval); ++i) {
828 if (!strcmp(argv[i], option)) {
829 for (j = 0; j < nval; ++j) {
830 ret[j] = (char *) malloc(strlen(argv[i + j + 1]) + 1);
831 strcpy(ret[j], argv[i + j + 1]);
832 }
833 return ret;
834 }
[1]835 }
[43]836 return 0;
[1]837}
838
839/**
840 * For internal use only.
841 */
[43]842bool get_option_state(int argc, char **argv, char *option)
[1]843{
[43]844 int i;
845 for (i = 0; i < argc; ++i)
846 if (!strcmp(argv[i], option))
847 return TRUE;
[1]848
[43]849 return FALSE;
[1]850}
851
852/**
853 * Taken from Vinum source -- for internal use only.
854 */
855long long size_spec(char *spec)
856{
[43]857 u_int64_t size;
858 char *s;
859 int sign = 1; /* -1 if negative */
[1]860
[43]861 size = 0;
862 if (spec != NULL) { /* we have a parameter */
863 s = spec;
864 if (*s == '-') { /* negative, */
865 sign = -1;
866 s++; /* skip */
867 }
868 if ((*s >= '0') && (*s <= '9')) { /* it's numeric */
869 while ((*s >= '0') && (*s <= '9')) /* it's numeric */
870 size = size * 10 + *s++ - '0'; /* convert it */
871 switch (*s) {
872 case '\0':
873 return size * sign;
[1]874
[43]875 case 'B':
876 case 'b':
877 case 'S':
878 case 's':
879 return size * sign * 512;
[1]880
[43]881 case 'K':
882 case 'k':
883 return size * sign * 1024;
[1]884
[43]885 case 'M':
886 case 'm':
887 return size * sign * 1024 * 1024;
[1]888
[43]889 case 'G':
890 case 'g':
891 return size * sign * 1024 * 1024 * 1024;
[1]892
[43]893 case 'T':
894 case 't':
895 log_it
896 ("Ok, I'm scared... Someone did a TERABYTE+ size-spec");
897 return size * sign * 1024 * 1024 * 1024 * 1024;
[1]898
[43]899 case 'P':
900 case 'p':
901 log_it
902 ("If I was scared last time, I'm freaked out now. Someone actually has a PETABYTE?!?!?!?!");
903 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024;
[1]904
[43]905 case 'E':
906 case 'e':
907 log_it
908 ("Okay, I'm REALLY freaked out. Who could devote a whole EXABYTE to their data?!?!");
909 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
910 1024;
[1]911
[43]912 case 'Z':
913 case 'z':
914 log_it
915 ("WHAT!?!? A ZETABYTE!?!? You've GOT to be kidding me!!!");
916 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
917 1024 * 1024;
918
919 case 'Y':
920 case 'y':
921 log_it
922 ("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?!?!");
923 popup_and_OK
[507]924 (_("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]925 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
926 1024 * 1024 * 1024;
927 }
928 }
[1]929 }
[43]930 return size * sign;
[1]931}
932
933#endif
934
935
936
937
[561]938int parse_mdstat(struct raidlist_itself *raidlist, char *device_prefix) {
[43]939
[561]940 const char delims[] = " ";
941
942 FILE *fin;
943 int res = 0, row, i, index_min;
944 int lastpos = 0;
945 size_t len = 0;
946 char *token;
947 char *string = NULL;
948 char *pos;
949 char type;
950 char *strtmp;
951
952 // open file
953 if (!(fin = fopen(MDSTAT_FILE, "r"))) {
954 log_msg(1, "Could not open %s.\n", MDSTAT_FILE);
955 return 1;
956 }
957 // initialise record, build progress and row counters
958 raidlist->entries = 0;
959 raidlist->el[raidlist->entries].progress = 999;
960 row = 1;
961 // skip first output row - contains registered RAID levels
962 res = getline(&string, &len, fin);
963 // parse the rest
964 while ( !feof_unlocked(fin) ) {
965 res = getline(&string, &len, fin);
966 if (res <= 0) break;
967 // trim leading spaces
968 pos = string;
969 while (*pos == ' ') *pos++;
970 asprintf(&string, pos);
971 //
972 // if we have newline after only spaces, this is a blank line, update
973 // counters, otherwise do normal parsing
974 if (*string == '\n') {
975 row = 1;
976 raidlist->entries++;
977 raidlist->el[raidlist->entries].progress = 999;
978 } else {
979 switch (row) {
980 case 1: // device information
981 // check whether last line of record and if so skip
982 pos = strcasestr(string, "unused devices: ");
983 if (pos == string) {
984 //raidlist->entries--;
985 break;
[43]986 }
[561]987 // tokenise string
988 token = mr_strtok (string, delims, &lastpos);
989 // get RAID device name
990 asprintf(&strtmp,"%s%s", device_prefix, token);
991 strcpy(raidlist->el[raidlist->entries].raid_device, strtmp);
992 paranoid_free(strtmp);
993 paranoid_free(token);
994 // skip ':' and status
995 token = strtok (string, delims, &lastpos);
996 paranoid_free(token);
997 token = strtok (string, delims, &lastpos);
998 if (!strcmp(token, "inactive")) {
999 log_msg(1, "RAID device '%s' inactive.\n",
1000 raidlist->el[raidlist->entries].raid_device);
1001 paranoid_free(string);
1002 paranoid_free(token);
1003 return 1;
1004 }
1005 paranoid_free(token);
[45]1006
[561]1007 // get RAID level
1008 token = strtok (string, delims, &lastpos);
1009 if (!strcmp(token, "multipath")) {
1010 raidlist->el[raidlist->entries].raid_level = -2;
1011 } else if (!strcmp(token, "linear")) {
1012 raidlist->el[raidlist->entries].raid_level = -1;
1013 } else if (!strcmp(token, "raid0")) {
1014 raidlist->el[raidlist->entries].raid_level = 0;
1015 } else if (!strcmp(token, "raid1")) {
1016 raidlist->el[raidlist->entries].raid_level = 1;
1017 } else if (!strcmp(token, "raid4")) {
1018 raidlist->el[raidlist->entries].raid_level = 4;
1019 } else if (!strcmp(token, "raid5")) {
1020 raidlist->el[raidlist->entries].raid_level = 5;
1021 } else if (!strcmp(token, "raid6")) {
1022 raidlist->el[raidlist->entries].raid_level = 6;
1023 } else if (!strcmp(token, "raid10")) {
1024 raidlist->el[raidlist->entries].raid_level = 10;
1025 } else {
1026 log_msg(1, "Unknown RAID level '%s'.\n", token);
1027 paranoid_free(string);
1028 paranoid_free(token);
1029 return 1;
1030 }
1031 paranoid_free(token);
[45]1032
[561]1033 // get RAID devices (type, index, device)
1034 // Note: parity disk for RAID4 is last normal disk, there is no '(P)'
1035 raidlist->el[raidlist->entries].data_disks.entries = 0;
1036 raidlist->el[raidlist->entries].spare_disks.entries = 0;
1037 raidlist->el[raidlist->entries].failed_disks.entries = 0;
1038 while((token = strtok (string, delims, &lastpos))) {
1039 if ((pos = strstr(token, "("))) {
1040 type = *(pos+1);
1041 } else {
1042 type = ' ';
1043 }
1044 pos = strstr(token, "[");
1045 *pos = '\0';
1046 switch(type) {
1047 case ' ': // normal data disks
1048 raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].index = atoi(pos + 1);
1049 asprintf(&strtmp,"%s%s", device_prefix, token);
1050 strcpy(raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].device, strtmp);
1051 paranoid_free(strtmp);
1052 raidlist->el[raidlist->entries].data_disks.entries++;
1053 break;
1054 case 'S': // spare disks
1055 raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].index = atoi(pos + 1);
1056 asprintf(&strtmp,"%s%s", device_prefix, token);
1057 strcpy(raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].device, strtmp);
1058 paranoid_free(strtmp);
1059 raidlist->el[raidlist->entries].spare_disks.entries++;
1060 break;
1061 case 'F': // failed disks
1062 raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].index = atoi(pos + 1);
1063 asprintf(&strtmp,"%s%s", device_prefix, token);
1064 strcpy(raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].device, strtmp);
1065 paranoid_free(strtmp);
1066 raidlist->el[raidlist->entries].failed_disks.entries++;
1067 log_it("At least one failed disk found in RAID array.\n");
1068 break;
1069 default: // error
1070 log_msg(1, "Unknown device type '%c'\n", type);
1071 paranoid_free(string);
1072 paranoid_free(token);
1073 return 1;
1074 break;
1075 }
1076 paranoid_free(token);
[43]1077 }
[561]1078
1079 // adjust index for each device so that it starts with 0 for every type
1080 index_min = 99;
1081 for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
1082 if (raidlist->el[raidlist->entries].data_disks.el[i].index < index_min) {
1083 index_min = raidlist->el[raidlist->entries].data_disks.el[i].index;
1084 }
1085 }
1086 if (index_min > 0) {
1087 for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
1088 raidlist->el[raidlist->entries].data_disks.el[i].index = raidlist->el[raidlist->entries].data_disks.el[i].index - index_min;
1089 }
1090 }
1091 index_min = 99;
1092 for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
1093 if (raidlist->el[raidlist->entries].spare_disks.el[i].index < index_min) {
1094 index_min = raidlist->el[raidlist->entries].spare_disks.el[i].index;
1095 }
1096 }
1097 if (index_min > 0) {
1098 for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
1099 raidlist->el[raidlist->entries].spare_disks.el[i].index = raidlist->el[raidlist->entries].spare_disks.el[i].index - index_min;
1100 }
1101 }
1102 index_min = 99;
1103 for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
1104 if (raidlist->el[raidlist->entries].failed_disks.el[i].index < index_min) {
1105 index_min = raidlist->el[raidlist->entries].failed_disks.el[i].index;
1106 }
1107 }
1108 if (index_min > 0) {
1109 for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
1110 raidlist->el[raidlist->entries].failed_disks.el[i].index = raidlist->el[raidlist->entries].failed_disks.el[i].index - index_min;
1111 }
1112 }
1113 break;
1114 case 2: // config information
1115 // check for persistent super block
1116 if (strcasestr(string, "super non-persistent")) {
1117 raidlist->el[raidlist->entries].persistent_superblock = 0;
1118 } else {
1119 raidlist->el[raidlist->entries].persistent_superblock = 1;
1120 }
1121 // extract chunk size
1122 if (!(pos = strcasestr(string, "k chunk"))) {
1123 raidlist->el[raidlist->entries].chunk_size = -1;
1124 } else {
1125 while (*pos != ' ') {
1126 *pos--;
1127 if (pos < string) {
1128 log_it("String underflow!\n");
1129 paranoid_free(string);
1130 return 1;
1131 }
1132 }
1133 raidlist->el[raidlist->entries].chunk_size = atoi(pos + 1);
1134 }
1135 // extract parity if present
1136 if ((pos = strcasestr(string, "algorithm"))) {
1137 raidlist->el[raidlist->entries].parity = atoi(pos + 9);
1138 } else {
1139 raidlist->el[raidlist->entries].parity = -1;
1140 }
1141 break;
1142 case 3: // optional build status information
1143 if (!(pos = strchr(string, '\%'))) {
1144 if (strcasestr(string, "delayed")) {
1145 raidlist->el[raidlist->entries].progress = -1; // delayed (therefore, stuck at 0%)
1146 } else {
1147 raidlist->el[raidlist->entries].progress = 999; // not found
1148 }
1149 } else {
1150 while (*pos != ' ') {
1151 *pos--;
1152 if (pos < string) {
1153 printf("ERROR: String underflow!\n");
1154 paranoid_free(string);
1155 return 1;
1156 }
1157 }
1158 raidlist->el[raidlist->entries].progress = atoi(pos);
1159 }
1160 break;
1161 default: // error
1162 log_msg(1, "Row %d should not occur in record!\n", row);
1163 break;
1164 }
1165 row++;
1166 }
1167 }
1168 // close file
1169 fclose(fin);
1170 // free string
1171 paranoid_free(string);
1172 // return success
1173 return 0;
1174
[1]1175}
1176
1177
1178
[561]1179
1180int create_raidtab_from_mdstat(char *raidtab_fname)
[1]1181{
[43]1182 struct raidlist_itself *raidlist;
1183 int retval = 0;
[1]1184
[43]1185 raidlist = malloc(sizeof(struct raidlist_itself));
[1]1186
[561]1187 // FIXME: Prefix '/dev/' should really be dynamic!
1188 if (parse_mdstat(raidlist, "/dev/")) {
1189 log_to_screen("Sorry, cannot read %s", MDSTAT_FILE);
[43]1190 return (1);
1191 }
1192
1193 retval += save_raidlist_to_raidtab(raidlist, raidtab_fname);
1194 return (retval);
[1]1195}
1196
1197
1198/* @} - end of raidGroup */
Note: See TracBrowser for help on using the repository browser.