source: MondoRescue/branches/3.1/mondo/src/common/libmondo-raid.c@ 2937

Last change on this file since 2937 was 2937, checked in by Bruno Cornec, 12 years ago

svn merge -r 2849:2935 ../3.0

  • Adds 3 binaries called potentially by udev o support USB key mount at restore time (Victor Gattegno)
  • mindi only sets the boot flag of a USB device if none is present already (should avoid flip/flop situation)
  • Fix a compatibility bug in analyze-my-lvm whete exclude lists with '|' was incorrectly analyzed (' ' still expected)
  • Precise that -J is not supported by mondorestore yet.
  • Fix a compatibility bug in mindi where exclude lists with '|' was incorrectly analyzed (' ' still expected)
  • Install an MBR on USB devices when possible
  • Improve error message in case mondorestore isn't found
  • Adds support for more virtio devices for KVM restore
  • Improve mbr.bin handling
  • Fix an issue in test of FindKernel for Xen and non Xen cases
    • Fix #492 by removing an extra useless fi
    • Move fvat nd fat modules to CDROM_MODS in order to have them in the initrd to support saving logs to a USB device e.g.
    • Fix an error when dmidecode reports comments before system name
    • Fix previous broken patch on comment management for bkphw
    • Improve msgs printed during kbd analysis
    • Fix a small remaining pb with new comment support
    • Try to again improve the mount/umount stuff in mondo -Adds mention to OEL, CentOS, ScientifLinux as RHEL clones
  • Fix a panadoid_free done on the wrong variable (solve a memory leak)
  • Solve #495 by skipping the potential (auto-read-only) when analyzin /proc/mdstat result
  • Fix some markup in man pages
  • adds the post-nuke script sample for Debian (Guillaume Pernot/Andrée Leidenfrost)
  • Avoid a useless umount when changing of afio set, let insist_on_cd check and perform if needed
  • Copy everything under .ssh of root account to allow for sshfs mount without passwd at restore time.
  • Adds support for bnx2i driver
  • Try to fix #496 by supporting the protocol in the output of the mount command (sshfs#...)
  • Uses the -d option of the mount command to avoid leaving loop devices in use. Probably provoked by the usage of a more recent busybox, but anyway that option doesn't hurt, and does nothing for non-loop devices. Should fix the problems reported on the ML with NFS and more than 8 ISO images (8 being the default number of loop devices available)
  • Attempt to fix #436 by adding some newtDrawForm where appropriate.
  • Fix bugs in the kernel detection routine of mindi (some echo were done without capturing their output, confusing the return). This should also help the Xen detection issue
  • Change the logging function to be nearer from 2.2.10, and start using some
  • Initialize some global fields with an empty string, now that newt Draws more regularly progress forms
  • Mindi progression still not good, but showing now 3% when calling mindi. Would need a specific handling for the progress bar to evolve more afterwards
  • Add compatibility with the latest SSSTK + PSP content for HP ProLiant + branding homogeneous now. We only take conrep out of the SSSTK pending a package availability. Using hpacuscripting now. Review of parameters called following test made.
  • Fix #501 by pushing BOOT_SIZE to 36864 for certain Ubuntu cases
  • Fix some remaining inconsistencis in the ProLiant HW backup. ready for usage.
  • Fix an issue on udevadm call (needs an intermediate variable)
  • Fix a remaining ProLiant tool call
  • Remove again some wrong variable initialization for Net in order to not have strange messages now we call Draw more often
  • Adds support for hwaddr (MAC addresses) at restore time, and map to the correct NIC if it's found at restore time (based on code from Stefan Heijmans)
  • Try to Fix #499 by changing init in order to never exit, and rebooting using sysrq in /proc (not portable). Maybe should move to using a real init, and a separated rcS script.
  • Fixes the shell msg error by using setsid as per Busybox FAQ
  • Fix #491 by correctly checking Xen kernel used for a dom0 env (apollo13)
  • Improves logging in Die()
  • Improve again ProLiant HW support, by adding references to the yet to come SSSTK rpm so that conrep is called at its future right place.
  • Improve mindi support for kernel 3.x
  • Fix #499 for good, by using a real init from busybox and moving our init script to etc/init.d/rcS. We also now call the reboot from busybox to exit, which indeed reset the system correctly (in a VM at least which wasn't the case before). Reauires a new mindi-busybox as well.
  • Attempt to solve #503 by handling /lib64 separately for udev and only if not a link (to /lib) for Ubuntu 64
  • Improve again previous tests for bug #503
  • Improve -d option description for NFS
  • Allow http download for ftp content
  • New italian mirror
  • Fix #505 by documenting environment variables
    • Fix a bug where on OBDR mode compression was turned off to create the OBDR content but not back on (report from the mailing list)
  • solve http://trac.mondorescue.org/ticket/520 by Victor Gattegno
  • Fix #524 as option -U needs a size also
  • Removes useless gendf option
  • Fix #511 now stabgrub is called also after conf file modifications
  • Adds support for SUID binaries
  • remove a useless xenkernelname variable from mindi
  • Should fix #512 by clearing old scratchdir before changing to a new
  • Simplify the function engaging SCSI for Smart Array controllers, and support as many controller as we have
  • Add some binaries used on SLES 10 by udev
  • Fix a syntax issue in newt-specific.c
  • Fix a memory free bug where a used variable was freed to early
  • Fix a memory free bug where a used variable was misused in asprintf/free
  • Fix #498 by using an external program (hpsa_obdr_mode) which can set the mode of the tape to CD-ROM or Sequential at will.

That program will have to be downloaded from http://cciss.sf.net

  • Adds support for the route command in mindi-busybox
  • Force block size back to 32768 in OBDR mode after skipping the 2 tape marks, to read the additional rootfs data corerctly - Fix #498 for good it seems
  • Adds support for a new pata_atiixp driver
  • Fix #525 and improve printing when no /etc/raidtab, but still md devices
  • Exclude /run similarly to /sys and /proc
  • Fix a bug in mindi when having multiple swap partiions with same starting string (/dev/dm-1 and /dev/dm-16). We now add a space in the grep to avoid mischoice.
  • Fix grub installation if we had to go in the hack part which wasn't using the correct variables to make decisions !
  • Remove all mention of mondorescue.com from code
  • Fix some website typo on the download page and precise that we now develop on Mageia
  • Exclude sysfs devpts and tmpfs partitions found in RHEL 6.2 from analyze by mindi (made error messages)
  • Force activation of first partition which should not be optional as everything is destroyed before
  • Fix a bug in analyze-my-lvm where LVs were created in random order (the one of lvscan) instead of using the dm-xxx order, which creates issues at restore time between the mapping created then and the one we had at backup time which could be inconssistent

-mondoarchive now exists if the mt command is missing when needed

  • Proper fix for analyze-my-lvm
  • Fix #500 and #473 by adding support of old metadata format for mdadm if using grub < 0.9x or lilo on a md boot device (to be tested)
  • Really never inject or eject when the -e option is passed or device is inappropriate (USB, Net, ...)
  • Fix #577 by having factor back as a float (not a long ! it's a decimal value) and making the appropriate cast.
  • Fix #578 by improving the regexp used for CD/DVD detection with -scanbus option
  • Adds more verbosity when LVM creation fails.
  • Fix #579 by checking with pipes on both sides of the string
  • debugfs should be excluded from the device considered for fstab analysis
  • Property svn:keywords set to Id
File size: 31.9 KB
Line 
1/* libmondo-raid.c subroutines for handling RAID
2 $Id: libmondo-raid.c 2937 2012-01-28 00:51:28Z 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 "lib-common-externs.h"
18#include "libmondo-raid.h"
19#include "mr_mem.h"
20#include "mr_str.h"
21
22#ifdef __FreeBSD__
23/* Nonstandard library functions: */
24extern void errx(int exitval, const char *fmt, ...);
25extern char *strsep(char **stringp, const char *delim);
26#endif
27
28/*@unused@*/
29//static char cvsid[] = "$Id: libmondo-raid.c 2937 2012-01-28 00:51:28Z 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 */
42bool 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 = NULL;
50 int res;
51
52 mr_asprintf(command, "grep \"");
53 if (raidno == -1) {
54 mr_strcat(command, "linear");
55 } else {
56 mr_strcat(command, "raid%d", raidno);
57 }
58 mr_strcat(command, "\" /proc/mdstat > /dev/null 2> /dev/null");
59 log_it("Is raid %d registered? Command = '%s'", raidno, command);
60 res = system(command);
61 paranoid_free(command);
62
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 */
82int
83where_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 */
117int
118which_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 */
189void
190write_variableINT_to_raid_var_line(struct raid_device_record *raidrec,
191 int lino, char *label, int value)
192{
193 /*@ buffers ***************************************************** */
194 char *sz_value = NULL;
195
196 assert(raidrec != NULL);
197 assert(label != NULL);
198
199 mr_asprintf(sz_value, "%d", value);
200 strcpy(raidrec->additional_vars.el[lino].label, label);
201 strcpy(raidrec->additional_vars.el[lino].value, sz_value);
202 mr_free(sz_value);
203}
204#endif
205
206
207
208
209
210
211
212
213#ifdef __FreeBSD__
214/**
215 * Add a disk to a RAID plex.
216 * @param p The plex to add the device to.
217 * @param device_to_add The device to add to @p p.
218 */
219void add_disk_to_raid_device(struct vinum_plex *p, char *device_to_add)
220{
221 strcpy(p->sd[p->subdisks].which_device, device_to_add);
222 ++p->subdisks;
223
224}
225#else
226/**
227 * Add a disk to a RAID device.
228 * @param disklist The disklist to add the device to.
229 * @param device_to_add The device to add to @p disklist.
230 * @param index The index number of the disklist entry we're creating.
231 */
232void add_disk_to_raid_device(struct list_of_disks *disklist,
233 char *device_to_add, int index)
234{
235 int items;
236
237 assert(disklist != NULL);
238 assert_string_is_neither_NULL_nor_zerolength(device_to_add);
239 items = disklist->entries;
240 strcpy(disklist->el[items].device, device_to_add);
241 disklist->el[items].index = index;
242 items++;
243 disklist->entries = items;
244}
245#endif
246
247
248/**
249 * Save the additional RAID variables to a stream.
250 * @param vars The RAID variable list to save.
251 * @param fout The FILE pointer to save them to.
252 */
253void
254save_additional_vars_to_file(struct additional_raid_variables *vars,
255 FILE * fout)
256{
257 int i;
258
259 assert(vars != NULL);
260 assert(fout != NULL);
261
262 for (i = 0; i < vars->entries; i++) {
263 fprintf(fout, " %-21s %s\n", vars->el[i].label,
264 vars->el[i].value);
265 }
266}
267
268
269/**
270 * Save a raidlist structure to disk in raidtab format.
271 * @param raidlist The raidlist to save.
272 * @param fname The file to save it to.
273 * @return 0, always.
274 * @bug Return value is redundant.
275 */
276int save_raidlist_to_raidtab(struct raidlist_itself *raidlist, char *fname)
277{
278 FILE *fout;
279 int current_raid_device;
280#ifdef __FreeBSD__
281 int i;
282#else
283// Linux
284#endif
285
286 assert(raidlist != NULL);
287 assert_string_is_neither_NULL_nor_zerolength(fname);
288
289 if (raidlist->entries <= 0) {
290 unlink(fname);
291 log_it("Deleting raidtab (no RAID devs anyway)");
292 return (0);
293 }
294 if (!(fout = fopen(fname, "w"))) {
295 log_OS_error("Failed to save raidlist");
296 return (1);
297 }
298 fprintf(fout, "# Generated by Mondo Rescue\n");
299
300#ifdef __FreeBSD__
301 for (i = 0; i < raidlist->disks.entries; ++i) {
302 fprintf(fout, "drive %s device %s\n", raidlist->disks.el[i].name,
303 raidlist->disks.el[i].device);
304 }
305 for (i = 0; i < (raidlist->spares.entries); ++i) {
306 fprintf(fout, "drive %s device %s hotspare\n",
307 raidlist->spares.el[i].name,
308 raidlist->spares.el[i].device);
309 }
310#endif
311
312 for (current_raid_device = 0; current_raid_device < raidlist->entries;
313 current_raid_device++) {
314 save_raidrec_to_file(&raidlist->el[current_raid_device], fout);
315 }
316 paranoid_fclose(fout);
317 return (0);
318}
319
320
321/**
322 * Save an individual RAID device record to a stream.
323 * @param raidrec The RAID device record to save.
324 * @param fout The stream to save it to.
325 */
326void save_raidrec_to_file(struct
327#ifdef __FreeBSD__
328 vinum_volume
329#else
330 raid_device_record
331#endif
332 * raidrec, FILE * fout)
333{
334#ifdef __FreeBSD__
335 int i, j;
336 char *org = NULL;
337
338 fprintf(fout, "\nvolume %s\n", raidrec->volname);
339 for (i = 0; i < raidrec->plexes; ++i) {
340 switch (raidrec->plex[i].raidlevel) {
341 case -1:
342 mr_asprintf(org, "concat");
343 break;
344 case 0:
345 mr_asprintf(org, "striped");
346 break;
347 case 5:
348 mr_asprintf(org, "raid5");
349 break;
350 }
351 fprintf(fout, " plex org %s", org);
352 mr_free(org);
353
354 if (raidrec->plex[i].raidlevel != -1) {
355 fprintf(fout, " %ik", raidrec->plex[i].stripesize);
356 }
357 fprintf(fout, "\n");
358
359 for (j = 0; j < raidrec->plex[i].subdisks; ++j) {
360 fprintf(fout, " sd drive %s size 0\n", 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 */
427void get_next_raidtab_line(FILE * fin, char **label, char **value)
428{
429 char *incoming = NULL;
430 char *p;
431
432 assert(fin != NULL);
433
434 if (feof(fin)) {
435 return;
436 }
437 for (mr_getline(incoming, fin); !feof(fin); mr_getline(incoming, fin)) {
438 mr_strip_spaces(incoming);
439 p = strchr(incoming, ' ');
440 if (strlen(incoming) < 3 || incoming[0] == '#' || !p) {
441 mr_free(incoming);
442 continue;
443 }
444 *(p++) = '\0';
445 while (*p == ' ') {
446 p++;
447 }
448 mr_asprintf(*label, "%s", incoming);
449 mr_asprintf(*value, "%s", p);
450 mr_free(incoming);
451 return;
452 }
453 mr_free(incoming);
454 return;
455}
456
457
458
459/**
460 * Load a raidtab file into a raidlist structure.
461 * @param raidlist The raidlist to fill.
462 * @param fname The file to read from.
463 * @return 0 for success, 1 for failure.
464 */
465#ifdef __FreeBSD__
466int load_raidtab_into_raidlist(struct raidlist_itself *raidlist,
467 char *fname)
468{
469 FILE *fin;
470 int items;
471
472 raidlist->spares.entries = 0;
473 raidlist->disks.entries = 0;
474 if (length_of_file(fname) < 5) {
475 log_it("Raidtab is very small or non-existent. Ignoring it.");
476 raidlist->entries = 0;
477 return (0);
478 }
479 if (!(fin = fopen(fname, "r"))) {
480 log_it("Cannot open raidtab");
481 return (1);
482 }
483 items = 0;
484 log_it("Loading raidtab...");
485 while (!feof(fin)) {
486 int argc;
487 char **argv = get_next_vinum_conf_line(fin, &argc);
488 if (!argv)
489 break;
490 if (!strcmp(argv[0], "drive")) {
491 char *drivename, *devname;
492 if (argc < 4)
493 continue;
494 drivename = argv[1];
495 devname = get_option_val(argc, argv, "device");
496 if (!devname)
497 continue;
498
499 if (get_option_state(argc, argv, "hotspare")) {
500 strcpy(raidlist->spares.el[raidlist->spares.entries].name, drivename);
501 strcpy(raidlist->spares.el[raidlist->spares.entries]. device, devname);
502 raidlist->spares.el[raidlist->spares.entries].index =
503 raidlist->disks.entries;
504 raidlist->spares.entries++;
505 } else {
506 strcpy(raidlist->disks.el[raidlist->disks.entries].name, drivename);
507 strcpy(raidlist->disks.el[raidlist->disks.entries].device, devname);
508 raidlist->disks.el[raidlist->disks.entries].index =
509 raidlist->disks.entries;
510 raidlist->disks.entries++;
511 }
512 } else if (!strcmp(argv[0], "volume")) {
513 char *volname;
514 if (argc < 2)
515 continue;
516 volname = argv[1];
517 strcpy(raidlist->el[raidlist->entries].volname, volname);
518 raidlist->el[raidlist->entries].plexes = 0;
519 raidlist->entries++;
520 } else if (!strcmp(argv[0], "plex")) {
521 int raidlevel, stripesize;
522 char *org = 0;
523 char **tmp = 0;
524 if (argc < 3)
525 continue;
526 org = get_option_val(argc, argv, "org");
527 if (!org)
528 continue;
529 if (strcmp(org, "concat")) {
530 tmp = get_option_vals(argc, argv, "org", 2);
531 if (tmp && tmp[1]) {
532 stripesize = (int) (size_spec(tmp[1]) / 1024);
533 } else
534 stripesize = 279;
535 } else
536 stripesize = 0;
537
538 if (!strcmp(org, "concat")) {
539 raidlevel = -1;
540 } else if (!strcmp(org, "striped")) {
541 raidlevel = 0;
542 } else if (!strcmp(org, "raid5")) {
543 raidlevel = 5;
544 } else
545 continue;
546
547 raidlist->el[raidlist->entries - 1].plex
548 [raidlist->el[raidlist->entries - 1].plexes].raidlevel =
549 raidlevel;
550 raidlist->el[raidlist->entries -
551 1].plex[raidlist->el[raidlist->entries -
552 1].plexes].stripesize =
553 stripesize;
554 raidlist->el[raidlist->entries -
555 1].plex[raidlist->el[raidlist->entries -
556 1].plexes].subdisks = 0;
557 raidlist->el[raidlist->entries - 1].plexes++;
558 } else if ((!strcmp(argv[0], "sd"))
559 || (!strcmp(argv[0], "subdisk"))) {
560 char *drive = 0;
561 if (argc < 3)
562 continue;
563 drive = get_option_val(argc, argv, "drive");
564 if (!drive)
565 continue;
566
567 strcpy(raidlist->el[raidlist->entries - 1].plex
568 [raidlist->el[raidlist->entries - 1].plexes - 1].sd
569 [raidlist->el[raidlist->entries - 1].plex
570 [raidlist->el[raidlist->entries - 1].plexes -
571 1].subdisks].which_device, drive);
572 raidlist->el[raidlist->entries -
573 1].plex[raidlist->el[raidlist->entries -
574 1].plexes - 1].subdisks++;
575 }
576 }
577 fclose(fin);
578 log_it("Raidtab loaded successfully.");
579 log_it("%d RAID devices in raidtab", raidlist->entries);
580 return (0);
581}
582
583
584#else
585
586int load_raidtab_into_raidlist(struct raidlist_itself *raidlist, char *fname) {
587
588 FILE *fin = NULL;
589 char *label = NULL;
590 char *value = NULL;
591 int items;
592 int v;
593
594 assert(raidlist != NULL);
595 assert_string_is_neither_NULL_nor_zerolength(fname);
596
597 if (length_of_file(fname) < 5) {
598 log_it("Raidtab is very small or non-existent. Ignoring it.");
599 raidlist->entries = 0;
600 return (0);
601 }
602 if (!(fin = fopen(fname, "r"))) {
603 log_it("Cannot open raidtab");
604 return (1);
605 }
606 items = 0;
607 log_it("Loading raidtab...");
608 get_next_raidtab_line(fin, &label, &value);
609 while (!feof(fin)) {
610 log_msg(1, "Looking for raid record #%d", items);
611 initialize_raidrec(&raidlist->el[items]);
612 v = 0;
613 /* find the 'raiddev' entry, indicating the start of the block of info */
614 while (!feof(fin) && strcmp(label, "raiddev")) {
615 strcpy(raidlist->el[items].additional_vars.el[v].label, label);
616 strcpy(raidlist->el[items].additional_vars.el[v].value, value);
617 v++;
618 mr_free(label);
619 mr_free(value);
620 get_next_raidtab_line(fin, &label, &value);
621 }
622 raidlist->el[items].additional_vars.entries = v;
623 if (feof(fin)) {
624 log_msg(1, "No more records.");
625 continue;
626 }
627 log_msg(2, "Record #%d (%s) found", items, value);
628 strcpy(raidlist->el[items].raid_device, value);
629 mr_free(label);
630 mr_free(value);
631 for (get_next_raidtab_line(fin, &label, &value); !feof(fin) && strcmp(label, "raiddev"); get_next_raidtab_line(fin, &label, &value)) {
632 process_raidtab_line(fin, &raidlist->el[items], label, value);
633 mr_free(label);
634 mr_free(value);
635 }
636 mr_free(label);
637 mr_free(value);
638 items++;
639 }
640 mr_free(label);
641 mr_free(value);
642 paranoid_fclose(fin);
643 raidlist->entries = items;
644 log_msg(1, "Raidtab loaded successfully.");
645 log_msg(1, "%d RAID devices in raidtab", items);
646 return (0);
647}
648#endif
649
650
651
652
653
654
655
656
657#ifndef __FreeBSD__
658/**
659 * Process a single line from the raidtab and store the results into @p raidrec.
660 * @param fin The stream to read the line from.
661 * @param raidrec The RAID device record to update.
662 * @param label Where to put the label processed.
663 * @param value Where to put the value processed.
664 */
665void
666process_raidtab_line(FILE * fin,
667 struct raid_device_record *raidrec,
668 char *label, char *value)
669{
670
671 /*@ add mallocs * */
672 char *labelB = NULL;
673 char *valueB = NULL;
674
675 struct list_of_disks *disklist;
676 int index;
677 int v;
678
679 assert(fin != NULL);
680 assert(raidrec != NULL);
681 assert_string_is_neither_NULL_nor_zerolength(label);
682 assert(value != NULL);
683
684 if (!strcmp(label, "raid-level")) {
685 if (!strcmp(value, "multipath")) {
686 raidrec->raid_level = -2;
687 } else if (!strcmp(value, "linear")) {
688 raidrec->raid_level = -1;
689 } else {
690 raidrec->raid_level = atoi(value);
691 }
692 } else if (!strcmp(label, "nr-raid-disks")) { /* ignore it */
693 } else if (!strcmp(label, "nr-spare-disks")) { /* ignore it */
694 } else if (!strcmp(label, "nr-parity-disks")) { /* ignore it */
695 } else if (!strcmp(label, "nr-failed-disks")) { /* ignore it */
696 } else if (!strcmp(label, "persistent-superblock")) {
697 raidrec->persistent_superblock = atoi(value);
698 } else if (!strcmp(label, "chunk-size")) {
699 raidrec->chunk_size = atoi(value);
700 } else if (!strcmp(label, "parity-algorithm")) {
701 if (!strcmp(value, "left-asymmetric")) {
702 raidrec->parity = 0;
703 } else if (!strcmp(value, "right-asymmetric")) {
704 raidrec->parity = 1;
705 } else if (!strcmp(value, "left-symmetric")) {
706 raidrec->parity = 2;
707 } else if (!strcmp(value, "right-symmetric")) {
708 raidrec->parity = 3;
709 } else {
710 log_msg(1, "Unknown RAID parity algorithm '%s'\n.", value);
711 }
712 } else if (!strcmp(label, "device")) {
713 get_next_raidtab_line(fin, &labelB, &valueB);
714 if (!strcmp(labelB, "raid-disk")) {
715 disklist = &raidrec->data_disks;
716 } else if (!strcmp(labelB, "spare-disk")) {
717 disklist = &raidrec->spare_disks;
718 } else if (!strcmp(labelB, "parity-disk")) {
719 disklist = &raidrec->parity_disks;
720 } else if (!strcmp(labelB, "failed-disk")) {
721 disklist = &raidrec->failed_disks;
722 } else {
723 disklist = NULL;
724 }
725 if (!disklist) {
726 log_it("Ignoring '%s %s' pair of disk %s", labelB, valueB, label);
727 } else {
728 index = atoi(valueB);
729 add_disk_to_raid_device(disklist, value, index);
730 }
731 mr_free(labelB);
732 mr_free(valueB);
733 } else {
734 v = raidrec->additional_vars.entries;
735 strcpy(raidrec->additional_vars.el[v].label, label);
736 strcpy(raidrec->additional_vars.el[v].value, value);
737 raidrec->additional_vars.entries = ++v;
738 }
739}
740#endif
741
742
743/**
744 * Save a disklist to a stream in raidtab format.
745 * @param listname One of "raid-disk", "spare-disk", "parity-disk", or "failed-disk".
746 * @param disklist The disklist to save to @p fout.
747 * @param fout The stream to write to.
748 */
749void
750save_disklist_to_file(char *listname,
751 struct list_of_disks *disklist, FILE * fout)
752{
753 int i;
754
755 assert_string_is_neither_NULL_nor_zerolength(listname);
756 assert(disklist != NULL);
757 assert(fout != NULL);
758
759 for (i = 0; i < disklist->entries; i++) {
760 fprintf(fout, " device %s\n",
761 disklist->el[i].device);
762 fprintf(fout, " %-21s %d\n", listname, disklist->el[i].index);
763 }
764}
765
766
767
768
769
770#ifdef __FreeBSD__
771/**
772 * Add a new plex to a volume. The index of the plex will be <tt>v-\>plexes - 1</tt>.
773 * @param v The volume to operate on.
774 * @param raidlevel The RAID level of the new plex.
775 * @param stripesize The stripe size (chunk size) of the new plex.
776 */
777void add_plex_to_volume(struct vinum_volume *v, int raidlevel,
778 int stripesize)
779{
780 v->plex[v->plexes].raidlevel = raidlevel;
781 v->plex[v->plexes].stripesize = stripesize;
782 v->plex[v->plexes].subdisks = 0;
783 ++v->plexes;
784}
785
786/**
787 * For internal use only.
788 */
789char **get_next_vinum_conf_line(FILE * f, int *argc)
790{
791 int cnt = 0;
792 static char *argv[64];
793 char **ap;
794 char *line = NULL;
795
796 mr_getline(line, f);
797 if (feof(f)) {
798 log_it("[GNVCL] Uh... I reached the EOF.");
799 return 0;
800 }
801
802 for (ap = argv; (*ap = strsep(&line, " \t")) != NULL;)
803 if (**ap != '\0') {
804 if (++ap >= &argv[64])
805 break;
806 cnt++;
807 }
808 mr_free(line);
809
810 if (strchr(argv[cnt - 1], '\n')) {
811 *(strchr(argv[cnt - 1], '\n')) = '\0';
812 }
813
814 if (argc)
815 *argc = cnt;
816 return argv;
817}
818
819/**
820 * For internal use only.
821 */
822char *get_option_val(int argc, char **argv, char *option)
823{
824 int i;
825 for (i = 0; i < (argc - 1); ++i) {
826 if (!strcmp(argv[i], option)) {
827 return argv[i + 1];
828 }
829 }
830 return 0;
831}
832
833/**
834 * For internal use only.
835 */
836char **get_option_vals(int argc, char **argv, char *option, int nval)
837{
838 int i, j;
839 static char **ret;
840 ret = (char **) malloc(nval * sizeof(char *));
841 for (i = 0; i < (argc - nval); ++i) {
842 if (!strcmp(argv[i], option)) {
843 for (j = 0; j < nval; ++j) {
844 ret[j] = (char *) malloc(strlen(argv[i + j + 1]) + 1);
845 strcpy(ret[j], argv[i + j + 1]);
846 }
847 return ret;
848 }
849 }
850 return 0;
851}
852
853/**
854 * For internal use only.
855 */
856bool get_option_state(int argc, char **argv, char *option)
857{
858 int i;
859 for (i = 0; i < argc; ++i)
860 if (!strcmp(argv[i], option))
861 return TRUE;
862
863 return FALSE;
864}
865
866/**
867 * Taken from Vinum source -- for internal use only.
868 */
869long long size_spec(char *spec)
870{
871 u_int64_t size;
872 char *s;
873 int sign = 1; /* -1 if negative */
874
875 size = 0;
876 if (spec != NULL) { /* we have a parameter */
877 s = spec;
878 if (*s == '-') { /* negative, */
879 sign = -1;
880 s++; /* skip */
881 }
882 if ((*s >= '0') && (*s <= '9')) { /* it's numeric */
883 while ((*s >= '0') && (*s <= '9')) /* it's numeric */
884 size = size * 10 + *s++ - '0'; /* convert it */
885 switch (*s) {
886 case '\0':
887 return size * sign;
888
889 case 'B':
890 case 'b':
891 case 'S':
892 case 's':
893 return size * sign * 512;
894
895 case 'K':
896 case 'k':
897 return size * sign * 1024;
898
899 case 'M':
900 case 'm':
901 return size * sign * 1024 * 1024;
902
903 case 'G':
904 case 'g':
905 return size * sign * 1024 * 1024 * 1024;
906
907 case 'T':
908 case 't':
909 log_it
910 ("Ok, I'm scared... Someone did a TERABYTE+ size-spec");
911 return size * sign * 1024 * 1024 * 1024 * 1024;
912
913 case 'P':
914 case 'p':
915 log_it
916 ("If I was scared last time, I'm freaked out now. Someone actually has a PETABYTE?!?!?!?!");
917 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024;
918
919 case 'E':
920 case 'e':
921 log_it
922 ("Okay, I'm REALLY freaked out. Who could devote a whole EXABYTE to their data?!?!");
923 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
924 1024;
925
926 case 'Z':
927 case 'z':
928 log_it
929 ("WHAT!?!? A ZETABYTE!?!? You've GOT to be kidding me!!!");
930 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
931 1024 * 1024;
932
933 case 'Y':
934 case 'y':
935 log_it
936 ("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?!?!");
937 popup_and_OK
938 ("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 :-)");
939 return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
940 1024 * 1024 * 1024;
941 }
942 }
943 }
944 return size * sign;
945}
946
947#endif
948
949
950
951
952int parse_mdstat(struct raidlist_itself *raidlist, char *device_prefix) {
953
954 const char delims[] = " ";
955
956 FILE *fin;
957 int res = 0, row, i, index_min;
958 int lastpos = 0;
959 size_t len = 0;
960 char *token;
961 char *string = NULL;
962 char *pos;
963 char type;
964 char *strtmp;
965
966 // open file
967 if (!(fin = fopen(MDSTAT_FILE, "r"))) {
968 log_msg(1, "Could not open %s.\n", MDSTAT_FILE);
969 return 1;
970 }
971 // initialise record, build progress and row counters
972 raidlist->entries = 0;
973 raidlist->el[raidlist->entries].progress = 999;
974 row = 1;
975 // skip first output row - contains registered RAID levels
976 res = getline(&string, &len, fin);
977 // parse the rest
978 while ( !feof_unlocked(fin) ) {
979 res = getline(&string, &len, fin);
980 if (res <= 0) break;
981 // trim leading spaces
982 pos = string;
983 while (*pos == ' ') pos += 1;
984 mr_asprintf(strtmp, "%s", pos);
985 strcpy(string, strtmp);
986 mr_free(strtmp);
987 // if we have newline after only spaces, this is a blank line, update
988 // counters, otherwise do normal parsing
989 if (*string == '\n') {
990 row = 1;
991 raidlist->entries++;
992 raidlist->el[raidlist->entries].progress = 999;
993 } else {
994 switch (row) {
995 case 1: // device information
996 // check whether last line of record and if so skip
997 pos = strcasestr(string, "unused devices: ");
998 if (pos == string) {
999 //raidlist->entries--;
1000 break;
1001 }
1002 // tokenise string
1003 token = mr_strtok(string, delims, &lastpos);
1004 // get RAID device name
1005 mr_asprintf(strtmp,"%s%s", device_prefix, token);
1006 strcpy(raidlist->el[raidlist->entries].raid_device, strtmp);
1007 mr_free(strtmp);
1008 mr_free(token);
1009 // skip ':' and status
1010 token = mr_strtok(string, delims, &lastpos);
1011 mr_free(token);
1012 token = mr_strtok(string, delims, &lastpos);
1013 if (!strcmp(token, "inactive")) {
1014 log_msg(1, "RAID device '%s' inactive.\n",
1015 raidlist->el[raidlist->entries].raid_device);
1016 paranoid_free(string);
1017 mr_free(token);
1018 return 1;
1019 }
1020 mr_free(token);
1021
1022 // get RAID level
1023 token = mr_strtok(string, delims, &lastpos);
1024 // skip potential auto-read-only entry
1025 if (!strcmp(token, "(auto-read-only)")) {
1026 mr_free(token);
1027 token = mr_strtok (string, delims, &lastpos);
1028 }
1029 if (!strcmp(token, "multipath")) {
1030 raidlist->el[raidlist->entries].raid_level = -2;
1031 } else if (!strcmp(token, "linear")) {
1032 raidlist->el[raidlist->entries].raid_level = -1;
1033 } else if (!strcmp(token, "raid0")) {
1034 raidlist->el[raidlist->entries].raid_level = 0;
1035 } else if (!strcmp(token, "raid1")) {
1036 raidlist->el[raidlist->entries].raid_level = 1;
1037 } else if (!strcmp(token, "raid4")) {
1038 raidlist->el[raidlist->entries].raid_level = 4;
1039 } else if (!strcmp(token, "raid5")) {
1040 raidlist->el[raidlist->entries].raid_level = 5;
1041 } else if (!strcmp(token, "raid6")) {
1042 raidlist->el[raidlist->entries].raid_level = 6;
1043 } else if (!strcmp(token, "raid10")) {
1044 raidlist->el[raidlist->entries].raid_level = 10;
1045 } else {
1046 log_msg(1, "Unknown RAID level '%s'.\n", token);
1047 paranoid_free(string);
1048 paranoid_free(token);
1049 return 1;
1050 }
1051 mr_free(token);
1052
1053 // get RAID devices (type, index, device)
1054 // Note: parity disk for RAID4 is last normal disk, there is no '(P)'
1055 raidlist->el[raidlist->entries].data_disks.entries = 0;
1056 raidlist->el[raidlist->entries].spare_disks.entries = 0;
1057 raidlist->el[raidlist->entries].failed_disks.entries = 0;
1058 while((token = mr_strtok (string, delims, &lastpos))) {
1059 if ((pos = strstr(token, "("))) {
1060 type = *(pos+1);
1061 } else {
1062 type = ' ';
1063 }
1064 pos = strstr(token, "[");
1065 *pos = '\0';
1066 switch(type) {
1067 case ' ': // normal data disks
1068 raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].index = atoi(pos + 1);
1069 mr_asprintf(strtmp,"%s%s", device_prefix, token);
1070 strcpy(raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].device, strtmp);
1071 mr_free(strtmp);
1072 raidlist->el[raidlist->entries].data_disks.entries++;
1073 break;
1074 case 'S': // spare disks
1075 raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].index = atoi(pos + 1);
1076 mr_asprintf(strtmp,"%s%s", device_prefix, token);
1077 strcpy(raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].device, strtmp);
1078 mr_free(strtmp);
1079 raidlist->el[raidlist->entries].spare_disks.entries++;
1080 break;
1081 case 'F': // failed disks
1082 raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].index = atoi(pos + 1);
1083 mr_asprintf(strtmp,"%s%s", device_prefix, token);
1084 strcpy(raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].device, strtmp);
1085 mr_free(strtmp);
1086 raidlist->el[raidlist->entries].failed_disks.entries++;
1087 log_it("At least one failed disk found in RAID array.\n");
1088 break;
1089 default: // error
1090 log_msg(1, "Unknown device type '%c'\n", type);
1091 paranoid_free(string);
1092 paranoid_free(token);
1093 return 1;
1094 break;
1095 }
1096 mr_free(token);
1097 }
1098
1099 // adjust index for each device so that it starts with 0 for every type
1100 index_min = 99;
1101 for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
1102 if (raidlist->el[raidlist->entries].data_disks.el[i].index < index_min) {
1103 index_min = raidlist->el[raidlist->entries].data_disks.el[i].index;
1104 }
1105 }
1106 if (index_min > 0) {
1107 for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
1108 raidlist->el[raidlist->entries].data_disks.el[i].index = raidlist->el[raidlist->entries].data_disks.el[i].index - index_min;
1109 }
1110 }
1111 index_min = 99;
1112 for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
1113 if (raidlist->el[raidlist->entries].spare_disks.el[i].index < index_min) {
1114 index_min = raidlist->el[raidlist->entries].spare_disks.el[i].index;
1115 }
1116 }
1117 if (index_min > 0) {
1118 for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
1119 raidlist->el[raidlist->entries].spare_disks.el[i].index = raidlist->el[raidlist->entries].spare_disks.el[i].index - index_min;
1120 }
1121 }
1122 index_min = 99;
1123 for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
1124 if (raidlist->el[raidlist->entries].failed_disks.el[i].index < index_min) {
1125 index_min = raidlist->el[raidlist->entries].failed_disks.el[i].index;
1126 }
1127 }
1128 if (index_min > 0) {
1129 for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
1130 raidlist->el[raidlist->entries].failed_disks.el[i].index = raidlist->el[raidlist->entries].failed_disks.el[i].index - index_min;
1131 }
1132 }
1133 break;
1134 case 2: // config information
1135 // check for persistent super block
1136 if (strcasestr(string, "super non-persistent")) {
1137 raidlist->el[raidlist->entries].persistent_superblock = 0;
1138 } else {
1139 raidlist->el[raidlist->entries].persistent_superblock = 1;
1140 }
1141 // extract chunk size
1142 if (!(pos = strcasestr(string, "k chunk"))) {
1143 raidlist->el[raidlist->entries].chunk_size = -1;
1144 } else {
1145 while (*pos != ' ') {
1146 pos -= 1;
1147 if (pos < string) {
1148 log_it("String underflow!\n");
1149 paranoid_free(string);
1150 return 1;
1151 }
1152 }
1153 raidlist->el[raidlist->entries].chunk_size = atoi(pos + 1);
1154 }
1155 // extract parity if present
1156 if ((pos = strcasestr(string, "algorithm"))) {
1157 raidlist->el[raidlist->entries].parity = atoi(pos + 9);
1158 } else {
1159 raidlist->el[raidlist->entries].parity = -1;
1160 }
1161 break;
1162 case 3: // optional build status information
1163 if (!(pos = strchr(string, '\%'))) {
1164 if (strcasestr(string, "delayed")) {
1165 raidlist->el[raidlist->entries].progress = -1; // delayed (therefore, stuck at 0%)
1166 } else {
1167 raidlist->el[raidlist->entries].progress = 999; // not found
1168 }
1169 } else {
1170 while (*pos != ' ') {
1171 pos -= 1;
1172 if (pos < string) {
1173 printf("ERROR: String underflow!\n");
1174 paranoid_free(string);
1175 return 1;
1176 }
1177 }
1178 raidlist->el[raidlist->entries].progress = atoi(pos);
1179 }
1180 break;
1181 default: // error or IN PROGRESS
1182 if (raidlist->el[raidlist->entries].progress != -1 &&
1183 raidlist->el[raidlist->entries].progress != 999) {
1184 log_msg(1, "Row %d should not occur in record!\n", row);
1185 }
1186 break;
1187 }
1188 row++;
1189 }
1190 }
1191 // close file
1192 fclose(fin);
1193 // free string
1194 paranoid_free(string);
1195 // return success
1196 return 0;
1197
1198}
1199
1200
1201
1202
1203int create_raidtab_from_mdstat(char *raidtab_fname)
1204{
1205 struct raidlist_itself *raidlist;
1206 int retval = 0;
1207
1208 raidlist = malloc(sizeof(struct raidlist_itself));
1209
1210 // FIXME: Prefix '/dev/' should really be dynamic!
1211 if (parse_mdstat(raidlist, "/dev/")) {
1212 log_to_screen("Sorry, cannot read %s", MDSTAT_FILE);
1213 return (1);
1214 }
1215
1216 retval += save_raidlist_to_raidtab(raidlist, raidtab_fname);
1217 return (retval);
1218}
1219
1220
1221
1222/* @} - end of raidGroup */
Note: See TracBrowser for help on using the repository browser.