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