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