source: MondoRescue/branches/2.2.9/mondo/src/common/libmondo-mountlist.c@ 2380

Last change on this file since 2380 was 2380, checked in by Bruno Cornec, 15 years ago
  • Change NFS support into a NetFS support to allow for multiple protocol in addition to NFS (NEEDS TESTING)
  • Better logging to detect a potential nuke issue
  • Property svn:keywords set to Id
File size: 29.8 KB
Line 
1/* subroutines for handling mountlist
2 $Id: libmondo-mountlist.c 2380 2009-09-09 18:30:47Z bruno $
3*/
4
5
6/**
7 * @file
8 * Functions which manipulate the mountlist.
9 */
10
11#include "my-stuff.h"
12#include "mr_mem.h"
13#include "mondostructures.h"
14#include "libmondo-mountlist.h"
15#include "lib-common-externs.h"
16#include "libmondo-raid-EXT.h"
17#include "libmondo-devices-EXT.h"
18#include "libmondo-tools-EXT.h"
19#include "libmondo-string-EXT.h"
20#include "newt-specific-EXT.h"
21
22/*@unused@*/
23//static char cvsid[] = "$Id: libmondo-mountlist.c 2380 2009-09-09 18:30:47Z bruno $";
24
25/* Reference to global bkpinfo */
26extern struct s_bkpinfo *bkpinfo;
27
28/**
29 * @addtogroup mountlistGroup
30 * @{
31 */
32/**
33 * Evaluate a drive within the mountlist for flaws. For example, too many
34 * primary partitions, the first logical isn't 5, duplicate partitions,
35 * ovar-allocated or under-allocated, unsupported format, silly size, or
36 * silly mountpoint. Under FreeBSD, this checks the disklabel too, for the above-mentioned
37 * errors as well as too many BSD partitions (more than 'h').
38 * @param mountlist The mountlist to check.
39 * @param drive The drive to check (e.g. @c /dev/hda).
40 * @return The flaws string found (NULL for success) allocated to be freed
41 * @see evaluate_mountlist
42 */
43char *evaluate_drive_within_mountlist(struct mountlist_itself *mountlist, char *drive)
44#ifdef __FreeBSD__
45{
46// FreeBSD-specific version of evaluate_drive_within_mountlist()
47 /*@ int ************************************************************* */
48 int prev_part_no = 0;
49 int curr_part_no = 0;
50 int pos = 0, npos = 0;
51 int res = 0;
52 int mountpoint_copies = 0;
53 int device_copies = 0;
54 int i = 0;
55 int cur_sp_no = 0;
56 int prev_sp_no = 0;
57 int foundsome = FALSE;
58
59 /*@ buffers ******************************************************** */
60 char tmp[MAX_STR_LEN];
61 char device[MAX_STR_LEN];
62 char mountpoint[MAX_STR_LEN];
63
64 char *flaws_str = NULL;
65
66 /*@ long *********************************************************** */
67 long physical_drive_size = 0;
68 long amount_allocated = 0;
69
70 /*@ pointers ******************************************************* */
71 char *part_table_fmt;
72
73 /*@ initialize ***************************************************** */
74 prev_part_no = 0;
75 tmp[0] = '\0';
76 mr_asprintf(&flaws_str, "%s", "");
77
78
79 physical_drive_size = get_phys_size_of_drive(drive);
80
81 if (physical_drive_size < 0) {
82 sprintf(tmp, " %s does not exist.", drive);
83 mr_strcat(flaws_str, "%s", tmp);
84 } else {
85 sprintf(tmp, "%s is %ld MB", drive, physical_drive_size);
86 }
87 log_it(tmp);
88
89
90 /* check DD */
91 for (cur_sp_no = 'a'; cur_sp_no < 'z'; ++cur_sp_no) {
92 sprintf(device, "%s%c", drive, cur_sp_no);
93 if (find_device_in_mountlist(mountlist, device) >= 0)
94 foundsome = TRUE;
95 }
96 if (foundsome) {
97 for (cur_sp_no = 'a'; cur_sp_no < 'z'; ++cur_sp_no) {
98 sprintf(device, "%s%c", drive, cur_sp_no);
99 pos = find_device_in_mountlist(mountlist, device);
100 if (pos < 0) {
101 continue;
102 }
103 strcpy(mountpoint, mountlist->el[pos].mountpoint);
104 /* is it too big? */
105 if (curr_part_no > 'h') {
106 sprintf(tmp, " Can only have up to 'h' in disklabel.");
107 log_it(tmp);
108 mr_strcat(flaws_str, tmp);
109 res++;
110 }
111 /* does partition /dev/adXsYZ exist more than once in the mountlist? */
112 for (i = 0, mountpoint_copies = 0, device_copies = 0;
113 i < mountlist->entries; i++) {
114 if (!strcmp(device, mountlist->el[i].device)) {
115 device_copies++;
116 }
117 }
118 if (device_copies > 1) {
119 sprintf(tmp, " %s %s's.", number_to_text(device_copies),
120 device);
121 if (!strstr(flaws_str, tmp)) {
122 log_it(tmp);
123 mr_strcat(flaws_str, tmp);
124 res++;
125 }
126 }
127 /* silly partition size? */
128 if (mountlist->el[pos].size < 8192
129 && strcmp(mountlist->el[pos].mountpoint, "lvm")) {
130 sprintf(tmp, " %s is tiny!", device);
131 log_it(tmp);
132 mr_strcat(flaws_str, tmp);
133 res++;
134 }
135 /* mountpoint should begin with / unless it is swap, lvm or raid */
136 if (strcmp(mountlist->el[pos].mountpoint, "swap")
137 && strcmp(mountlist->el[pos].mountpoint, "lvm")
138 && strcmp(mountlist->el[pos].mountpoint, "raid")
139 && strcmp(mountlist->el[pos].mountpoint, "image")
140 && strcmp(mountlist->el[pos].mountpoint, "none")
141 && mountlist->el[pos].mountpoint[0] != '/') {
142 sprintf(tmp, " %s has a weird mountpoint.", device);
143 log_it(tmp);
144 mr_strcat(flaws_str, tmp);
145 res++;
146 }
147 /* is format sensible? */
148 if (!is_this_a_valid_disk_format(mountlist->el[pos].format)) {
149 sprintf(tmp, " %s has unsupported format %s.", device, mountlist->el[pos].format);
150 log_it(tmp);
151 mr_strcat(flaws_str, tmp);
152 res++;
153 }
154 amount_allocated += mountlist->el[pos].size / 1024L;
155 prev_sp_no = cur_sp_no;
156 }
157 }
158
159 npos = pos = 0;
160 for (curr_part_no = 1; curr_part_no < 99; curr_part_no++) {
161 build_partition_name(device, drive, curr_part_no);
162 pos = find_device_in_mountlist(mountlist, device);
163 npos = 0;
164 for (cur_sp_no = 'a'; cur_sp_no <= 'h'; cur_sp_no++) {
165 sprintf(device, "%ss%i%c", device, curr_part_no, cur_sp_no);
166 if (find_device_in_mountlist(mountlist, device) >= 0)
167 npos++;
168 }
169 if (((pos >= 0) || npos) && foundsome) {
170 mr_strcat(flaws_str, " %s has both DD and PC-style partitions.", drive);
171 return(flaws_str); // fatal error
172 }
173
174 build_partition_name(device, drive, curr_part_no);
175 strcpy(mountpoint, mountlist->el[pos].mountpoint);
176 if (pos > 0 && !npos) {
177 /* gap in the partition list? */
178 if (curr_part_no - prev_part_no > 1) {
179 if (prev_part_no == 0) {
180 sprintf(tmp, " Gap prior to %s.", device);
181 log_it(tmp);
182 mr_strcat(flaws_str, "%s", tmp);
183 res++;
184 } else if (curr_part_no > 5
185 || (curr_part_no <= 4 && prev_part_no > 0)) {
186 sprintf(tmp, " Gap between %ss%d and %d.", drive,
187 prev_part_no, curr_part_no);
188 log_it(tmp);
189 mr_strcat(flaws_str, "%s", tmp);
190 res++;
191 }
192 }
193 /* GPT allows more than 4 primary partitions */
194 part_table_fmt = which_partition_format(drive);
195 /* no spare primary partitions to help accommodate the logical(s)? */
196 if ((curr_part_no >= 5 && prev_part_no == 4)
197 && (strcmp(part_table_fmt, "MBR") == 0)) {
198 sprintf(tmp, " Partition %ss4 is occupied.", drive);
199 log_it(tmp);
200 mr_strcat(flaws_str, "%s", tmp);
201 res++;
202 }
203 /* does partition /dev/adXsY exist more than once in the mountlist? */
204 for (i = 0, mountpoint_copies = 0, device_copies = 0;
205 i < mountlist->entries; i++) {
206 if (!strcmp(device, mountlist->el[i].device)) {
207 device_copies++;
208 }
209 }
210 if (device_copies > 1) {
211 sprintf(tmp, " %s %s's.", number_to_text(device_copies),
212 device);
213 if (!strstr(flaws_str, "%s", tmp)) {
214 log_it(tmp);
215 mr_strcat(flaws_str, "%s", tmp);
216 res++;
217 }
218 }
219 /* silly partition size? */
220 if (mountlist->el[pos].size < 8192
221 && strcmp(mountlist->el[pos].mountpoint, "lvm")) {
222 sprintf(tmp, " %s is tiny!", device);
223 log_it(tmp);
224 mr_strcat(flaws_str, "%s", tmp);
225 res++;
226 }
227 /* mountpoint should begin with / unless it is swap, lvm or raid */
228 if (strcmp(mountlist->el[pos].mountpoint, "swap")
229 && strcmp(mountlist->el[pos].mountpoint, "lvm")
230 && strcmp(mountlist->el[pos].mountpoint, "raid")
231 && strcmp(mountlist->el[pos].mountpoint, "image")
232 && strcmp(mountlist->el[pos].mountpoint, "none")
233 && mountlist->el[pos].mountpoint[0] != '/') {
234 sprintf(tmp, " %s has a weird mountpoint.", device);
235 log_it(tmp);
236 mr_strcat(flaws_str, "%s", tmp);
237 res++;
238 }
239 /* is format sensible? */
240 if (!is_this_a_valid_disk_format(mountlist->el[pos].format)) {
241 sprintf(tmp, " %s has unsupported format %s.", device, mountlist->el[pos].format);
242 log_it(tmp);
243 mr_strcat(flaws_str, "%s", tmp);
244 res++;
245 }
246 } else {
247 /* Check subpartitions */
248 for (cur_sp_no = 'a'; cur_sp_no < 'z'; ++cur_sp_no) {
249 sprintf(device, "%ss%d%c", drive, curr_part_no, cur_sp_no);
250 pos = find_device_in_mountlist(mountlist, device);
251 if (pos < 0) {
252 continue;
253 }
254 strcpy(mountpoint, mountlist->el[pos].mountpoint);
255 /* is it too big? */
256 if (curr_part_no > 'h') {
257 sprintf(tmp, " Can only have up to 'h' in disklabel.");
258 log_it(tmp);
259 mr_strcat(flaws_str, "%s", tmp);
260 res++;
261 }
262 /* does partition /dev/adXsYZ exist more than once in the mountlist? */
263 for (i = 0, mountpoint_copies = 0, device_copies = 0;
264 i < mountlist->entries; i++) {
265 if (!strcmp(device, mountlist->el[i].device)) {
266 device_copies++;
267 }
268 }
269 if (device_copies > 1) {
270 sprintf(tmp, " %s %s's.",
271 number_to_text(device_copies), device);
272 if (!strstr(flaws_str, tmp)) {
273 log_it(tmp);
274 mr_strcat(flaws_str, "%s", tmp);
275 res++;
276 }
277 }
278 /* silly partition size? */
279 if (mountlist->el[pos].size < 8192
280 && strcmp(mountlist->el[pos].mountpoint, "lvm")) {
281 sprintf(tmp, " %s is tiny!", device);
282 log_it(tmp);
283 mr_strcat(flaws_str, "%s", tmp);
284 res++;
285 }
286 /* mountpoint should begin with / unless it is swap, lvm or raid */
287 if (strcmp(mountlist->el[pos].mountpoint, "swap")
288 && strcmp(mountlist->el[pos].mountpoint, "lvm")
289 && strcmp(mountlist->el[pos].mountpoint, "raid")
290 && strcmp(mountlist->el[pos].mountpoint, "image")
291 && strcmp(mountlist->el[pos].mountpoint, "none")
292 && mountlist->el[pos].mountpoint[0] != '/') {
293 sprintf(tmp, " %s has a weird mountpoint.", device);
294 log_it(tmp);
295 mr_strcat(flaws_str, "%s", tmp);
296 res++;
297 }
298 /* is format sensible? */
299 if (!is_this_a_valid_disk_format
300 (mountlist->el[pos].format)) {
301 sprintf(tmp, " %s has unsupported format %s.", device, mountlist->el[pos].format);
302 log_it(tmp);
303 mr_strcat(flaws_str, "%s", tmp);
304 res++;
305 }
306 amount_allocated += mountlist->el[pos].size / 1024L;
307 prev_sp_no = cur_sp_no;
308 }
309 }
310
311 /* OK, continue with main loop */
312 amount_allocated += mountlist->el[pos].size / 1024L;
313 prev_part_no = curr_part_no;
314 }
315
316 /* Over-allocated the disk? Unallocated space on disk? */
317 if (amount_allocated > physical_drive_size) // Used to be +1, but what if you're 1 MB too high?
318 {
319 sprintf(tmp, " %ld MB over-allocated on %s.",
320 amount_allocated - physical_drive_size, drive);
321 log_it(tmp);
322 mr_strcat(flaws_str, "%s", tmp);
323 res++;
324 } else if (amount_allocated < physical_drive_size - 1) { /* NOT AN ERROR, JUST A WARNING :-) */
325 sprintf(tmp, " %ld MB unallocated on %s.",
326 physical_drive_size - amount_allocated, drive);
327 log_it(tmp);
328 mr_strcat(flaws_str, "%s", tmp);
329 }
330 if (res) {
331 return (NULL);
332 } else {
333 return (flaws_str);
334 }
335}
336
337#else
338// Linux-specific version of evaluate_drive_within_mountlist()
339{
340
341 /*@ int ************************************************************* */
342 int prev_part_no = 0;
343 int curr_part_no = 0;
344 int pos = 0;
345 int res = 0;
346 int mountpoint_copies = 0;
347 int device_copies = 0;
348 int i = 0;
349
350 /*@ buffers ******************************************************** */
351 char *tmp;
352 char *device;
353 char *flaws_str = NULL;
354
355 /*@ long *********************************************************** */
356 long physical_drive_size = 0L;
357 long amount_allocated = 0L;
358
359 /*@ pointers ******************************************************* */
360 char *part_table_fmt = NULL;
361
362 /*@ initialize ***************************************************** */
363 assert_string_is_neither_NULL_nor_zerolength(drive);
364 assert(mountlist != NULL);
365 mr_asprintf(&flaws_str, "%s", "");
366
367 malloc_string(tmp);
368 malloc_string(device);
369 prev_part_no = 0;
370 tmp[0] = '\0';
371
372
373 physical_drive_size = get_phys_size_of_drive(drive);
374
375 if (physical_drive_size < 0) {
376 sprintf(tmp, " %s does not exist.", drive);
377 log_it(tmp);
378 mr_strcat(flaws_str, "%s", tmp);
379 res++;
380 goto endoffunc;
381 } else {
382 sprintf(tmp, "%s is %ld MB", drive, physical_drive_size);
383 log_it(tmp);
384 }
385
386 for (curr_part_no = 1; curr_part_no < 99; curr_part_no++) {
387 build_partition_name(device, drive, curr_part_no);
388 pos = find_device_in_mountlist(mountlist, device);
389 if (pos < 0) {
390 continue;
391 }
392 log_msg(2, "Processing partition %s on %s #%d in mountlist", device, drive, pos);
393 /* gap in the partition list? */
394 if (curr_part_no - prev_part_no > 1) {
395 if (prev_part_no == 0) {
396 sprintf(tmp, " Gap prior to %s.", device);
397 log_it(tmp);
398 mr_strcat(flaws_str, "%s", tmp);
399 res++;
400 } else if (curr_part_no > 5
401 || (curr_part_no <= 4 && prev_part_no > 0)) {
402 sprintf(tmp, " Gap on %s between %d and %d.", drive,
403 prev_part_no, curr_part_no);
404 log_it(tmp);
405 mr_strcat(flaws_str, "%s", tmp);
406 res++;
407 }
408 }
409 /* GPT allows more than 4 primary partitions */
410 part_table_fmt = which_partition_format(drive);
411 /* no spare primary partitions to help accommodate the logical(s)? */
412 if ((curr_part_no >= 5 && prev_part_no == 4)
413 && (strcmp(part_table_fmt, "MBR") == 0)) {
414 sprintf(tmp, " Partition 4 of %s is occupied.", drive);
415 log_it(tmp);
416 mr_strcat(flaws_str, "%s", tmp);
417 res++;
418 }
419 /* does partition /dev/hdNX exist more than once in the mountlist? */
420 for (i = 0, mountpoint_copies = 0, device_copies = 0;
421 i < mountlist->entries; i++) {
422 if (!strcmp(device, mountlist->el[i].device)) {
423 device_copies++;
424 }
425 }
426 if (device_copies > 1) {
427 sprintf(tmp, " %s %s's.", number_to_text(device_copies),
428 device);
429 if (!strstr(flaws_str, tmp)) {
430 log_it(tmp);
431 mr_strcat(flaws_str, "%s", tmp);
432 res++;
433 }
434 }
435 /* silly partition size? */
436 if (mountlist->el[pos].size < 8192
437 && strcmp(mountlist->el[pos].mountpoint, "lvm")) {
438 sprintf(tmp, " %s is tiny!", device);
439 log_it(tmp);
440 mr_strcat(flaws_str, "%s", tmp);
441 res++;
442 }
443 /* mountpoint should begin with / unless it is swap, lvm or raid */
444 if (strcmp(mountlist->el[pos].mountpoint, "swap")
445 && strcmp(mountlist->el[pos].mountpoint, "lvm")
446 && strcmp(mountlist->el[pos].mountpoint, "raid")
447 && strcmp(mountlist->el[pos].mountpoint, "image")
448 && mountlist->el[pos].mountpoint[0] != '/') {
449 sprintf(tmp, " %s has a weird mountpoint.", device);
450 log_it(tmp);
451 mr_strcat(flaws_str, "%s", tmp);
452 res++;
453 }
454 /* is format sensible? */
455 if (!is_this_a_valid_disk_format(mountlist->el[pos].format)) {
456 sprintf(tmp, " %s has unsupported format %s.", device, mountlist->el[pos].format);
457 log_it(tmp);
458 mr_strcat(flaws_str, "%s", tmp);
459 res++;
460 }
461 /* OK, continue with main loop */
462 amount_allocated += mountlist->el[pos].size / 1024L;
463 prev_part_no = curr_part_no;
464 }
465
466 /* Over-allocated the disk? Unallocated space on disk? */
467 if (amount_allocated > physical_drive_size + 1) {
468 sprintf(tmp, " %ld MB over-allocated on %s.",
469 amount_allocated - physical_drive_size, drive);
470 log_it(tmp);
471 mr_strcat(flaws_str, "%s", tmp);
472 res++;
473 } else if (amount_allocated < physical_drive_size - 1) { /* NOT AN ERROR, JUST A WARNING :-) */
474 sprintf(tmp, " %ld MB unallocated on %s.",
475 physical_drive_size - amount_allocated, drive);
476 log_it(tmp);
477 mr_strcat(flaws_str, "%s", tmp);
478 }
479
480 endoffunc:
481 paranoid_free(tmp);
482 paranoid_free(device);
483
484 if (res == 0) {
485 mr_free(flaws_str);
486 log_msg(2, "Fine, no error in evaluate_drive_within_mountlist");
487 return (NULL);
488 } else {
489 log_msg(2, "Error in evaluate_drive_within_mountlist: %s", flaws_str);
490 return (flaws_str);
491 }
492}
493#endif
494
495
496/**
497 * Evaluate a whole mountlist for flaws. Calls evaluate_drive_within_mountlist()
498 * for each drive, and then spreads the flaws across three lines.
499 * @param mountlist The mountlist to evaluate.
500 * @param flaws_str_A Where to put the first line listing errors found.
501 * @param flaws_str_B Where to put the second line listing errors found.
502 * @param flaws_str_C Where to put the third line listing errors found.
503 * @return The number of flaws found (0 for success).
504 * @see evaluate_drive_within_mountlist
505 */
506char *evaluate_mountlist(struct mountlist_itself *mountlist, int *res) {
507
508 /*@ buffer *********************************************************** */
509 struct list_of_disks *drivelist;
510 char *tmp = NULL;
511 char *flaws_str = NULL;
512
513 /*@ int ************************************************************** */
514 int i = 0;
515
516 /*@ initialize ******************************************************* */
517
518 *res = 0;
519 drivelist = malloc(sizeof(struct list_of_disks));
520 assert(mountlist != NULL);
521
522 mr_asprintf(&flaws_str, "%s", "");
523
524 make_list_of_drives_in_mountlist(mountlist, drivelist);
525
526 log_it("Evaluating mountlist...");
527
528 for (i = 0; i < drivelist->entries; i++) {
529 if (strstr
530 (drivelist->el[i].device,
531 DONT_KNOW_HOW_TO_EVALUATE_THIS_DEVICE_TYPE)) {
532 mr_asprintf(&tmp, " Not evaluating %s (I don't know how yet)", drivelist->el[i].device);
533 log_it(tmp);
534 paranoid_free(tmp);
535 } else {
536 log_msg(8, "Evaluating drive #%d (%s) within mountlist", i, drivelist->el[i].device);
537 tmp = evaluate_drive_within_mountlist(mountlist, drivelist->el[i].device);
538 if (tmp != NULL) {
539 (*res)++;
540 }
541 }
542 log_msg(8,"Entry: %d (%s)", i, drivelist->el[i].device);
543 /* BCO: tmp can be NULL */
544 if (tmp != NULL) {
545 log_msg(8,"Adding: %s to %s", tmp, flaws_str);
546 mr_strcat(flaws_str, "%s", tmp);
547 paranoid_free(tmp);
548 }
549 }
550 *res += look_for_duplicate_mountpoints(mountlist, flaws_str);
551 return(flaws_str);
552}
553
554
555/**
556 * Find the index number of @p device in the mountlist.
557 * The device given must match @p mountlist->el[N].device exactly, case-sensitive.
558 * @param mountlist The mountlist to search in.
559 * @param device The device to search for.
560 * @return The zero-based index of the device, or -1 if it could not be found.
561 */
562int
563find_device_in_mountlist(struct mountlist_itself *mountlist, char *device)
564{
565
566 /*@ int ************************************************************** */
567 int i = 0;
568
569 assert(mountlist != NULL);
570 assert_string_is_neither_NULL_nor_zerolength(device);
571 for (i = 0;
572 i < mountlist->entries
573 && strcmp(mountlist->el[i].device, device) != 0; i++);
574
575 if (i == mountlist->entries) {
576 return (-1);
577 } else {
578 return (i);
579 }
580}
581
582
583/**
584 * Look for duplicate mountpoints in @p mountlist.
585 * @param mountlist The mountlist to check.
586 * @param flaws_str The flaws string to append the results to.
587 * @return The number of mountpoints that have duplicates, or 0 for success.
588 */
589int
590look_for_duplicate_mountpoints(struct mountlist_itself *mountlist, char *flaws_str)
591{
592
593 /*@ int ************************************************************* */
594 int res = 0;
595 int currline = 0;
596 int i = 0;
597 int copies = 0;
598 int last_copy = 0;
599
600 /*@ buffetr ********************************************************* */
601 char *curr_mountpoint = NULL;
602 char *tmp = NULL;
603
604 assert(mountlist != NULL);
605 assert(flaws_str != NULL);
606
607 for (currline = 0; currline < mountlist->entries; currline++) {
608 mr_asprintf(&curr_mountpoint, "%s", mountlist->el[currline].mountpoint);
609 for (i = 0, copies = 0, last_copy = -1; i < mountlist->entries;
610 i++) {
611 if (!strcmp(mountlist->el[i].mountpoint, curr_mountpoint)
612 && strcmp(mountlist->el[i].mountpoint, "lvm")
613 && strcmp(mountlist->el[i].mountpoint, "swap")) {
614 last_copy = i;
615 copies++;
616 }
617 }
618 if (copies > 1 && last_copy == currline
619 && strcmp(curr_mountpoint, "raid")) {
620 mr_asprintf(&tmp, " %s %s's.", number_to_text(copies), curr_mountpoint);
621 mr_strcat(flaws_str, "%s", tmp);
622 log_it(tmp);
623 paranoid_free(tmp);
624 res++;
625 }
626 paranoid_free(curr_mountpoint);
627 }
628 return (res);
629}
630
631
632/**
633 * Make a list of the drives mentioned in the mountlist.
634 * @param mountlist The mountlist to examine.
635 * @param drivelist Where to put the list of drives found.
636 * @return The number of physical (non-RAID non-LVM) drives found, or \<= 0 for error.
637 */
638int
639make_list_of_drives_in_mountlist(struct mountlist_itself *mountlist,
640 struct list_of_disks *drivelist)
641{
642
643 /*@ int ************************************************************* */
644 int lino;
645 int noof_drives;
646 int j;
647
648 /*@ buffers ********************************************************* */
649 char *drive = NULL;
650 char *truncdrive = NULL;
651
652 long long size;
653
654 assert(mountlist != NULL);
655 assert(drivelist != NULL);
656 log_it("Making list of drives");
657 for (lino = 0, noof_drives = 0; lino < mountlist->entries; lino++) {
658
659 mr_asprintf(&drive, "%s", mountlist->el[lino].device);
660 if (!strncmp(drive, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB))) {
661 log_msg(8, "Not putting %s in list of drives: it's a virtual drive", drive);
662 paranoid_free(drive);
663 continue;
664 }
665
666 size = mountlist->el[lino].size;
667 if (size == 0) {
668 log_msg(8, "Not putting %s in list of drives: it has zero size (maybe an LVM volume)", drive);
669 paranoid_free(drive);
670 continue;
671 }
672
673 log_msg(8, "Putting %s with size %lli in list of drives", drive, size);
674
675 /* memory allocation */
676 truncdrive = truncate_to_drive_name(drive);
677 paranoid_free(drive);
678
679 log_msg(8, "drive truncated to %s", truncdrive);
680
681 for (j = 0;
682 j < noof_drives
683 && strcmp(drivelist->el[j].device, truncdrive) != 0; j++) {
684 continue;
685 }
686 if (j == noof_drives) {
687 strncpy(drivelist->el[noof_drives].device, truncdrive, 63);
688 drivelist->el[noof_drives].device[63] = '\0';
689 log_msg(8,"Adding drive %s to list", drivelist->el[noof_drives].device);
690 noof_drives++;
691 }
692 paranoid_free(truncdrive);
693 if (noof_drives >= MAXIMUM_DISKS_PER_RAID_DEV) {
694 log_msg(0, "Unable to handle mountlist with more than %d lines", MAXIMUM_DISKS_PER_RAID_DEV);
695 log_to_screen("Unable to handle a so big mountlist");
696 finish(1);
697 }
698 }
699 drivelist->entries = noof_drives;
700 log_msg(8, "Made list of %d drives",noof_drives);
701
702 return (noof_drives);
703}
704
705
706/**
707 * Make a list of RAID partitions not currently associated with any RAID device.
708 * The user may add any of these partitions to the RAID device.
709 * @param output_list Where to put the list of unallocated RAID partitions.
710 * @param mountlist The mountlist to examine.
711 * @param raidlist The raidlist to examine.
712 */
713void make_list_of_unallocated_raid_partitions(struct mountlist_itself
714 *output_list,
715 struct mountlist_itself
716 *mountlist,
717 struct raidlist_itself
718 *raidlist)
719{
720
721 /*@ int ************************************************************* */
722 int items = 0;
723 int i = 0;
724 int used_by = 0;
725
726 /*@ buffers ********************************************************* */
727 char *tmp = NULL;
728
729 assert(output_list != NULL);
730 assert(mountlist != NULL);
731 assert(raidlist != NULL);
732 log_it("MLOURP -- starting");
733 items = 0;
734
735
736 for (i = 0; i < mountlist->entries; i++) {
737 if (strstr(mountlist->el[i].mountpoint, "raid")) {
738 used_by =
739 which_raid_device_is_using_this_partition(raidlist,
740 mountlist->el[i].
741 device);
742 if (used_by < 0) {
743 memcpy((void *) &output_list->el[items++],
744 (void *) &mountlist->el[i],
745 sizeof(struct mountlist_line));
746 mr_asprintf(&tmp, "%s is available; user may choose to add it to raid device", output_list->el[items - 1].device);
747 log_it(tmp);
748 paranoid_free(tmp);
749 }
750 }
751 }
752 output_list->entries = items;
753 log_it("MLUORP -- ending");
754}
755
756
757/**
758 * Get the size of a mountlist entry by the @c device field.
759 * @param mountlist The mountlist to search in.
760 * @param device The device to search for
761 * @return The size of the device (in KB), or -1 if it could not be found.
762 */
763long long
764size_of_specific_device_in_mountlist(struct mountlist_itself *mountlist,
765 char *device)
766{
767 /*@ int ************************************************************** */
768 int i = 0;
769
770
771 assert(mountlist != NULL);
772 assert_string_is_neither_NULL_nor_zerolength(device);
773
774 for (i = 0;
775 i < mountlist->entries && strcmp(mountlist->el[i].device, device);
776 i++);
777 if (i == mountlist->entries) {
778 return (-1);
779 } else {
780 return (mountlist->el[i].size);
781 }
782}
783
784
785/**
786 * Load a file on disk into @p mountlist.
787 * The file on disk should consist of multiple lines, each containing 4 or 5
788 * columns: the device, the mountpoint, the filesystem type, the size in kilobytes, and optionally the filesystem label.
789 * Comments begin with a '#' without any leading whitespace. Any duplicate
790 * entries are renamed.
791 * @param mountlist The mountlist to load into.
792 * @param fname The name of the file to load the mountlist from.
793 * @return 0 for success, 1 for failure.
794 */
795int load_mountlist(struct mountlist_itself *mountlist, char *fname)
796{
797 FILE *fin = NULL;
798 /* malloc ** */
799 char *incoming = NULL;
800 char *siz = NULL;
801 char *tmp = NULL;
802 char *p = NULL;
803
804 int items = 0;
805 int j = 0;
806 int res = 0;
807
808 assert(mountlist != NULL);
809 assert_string_is_neither_NULL_nor_zerolength(fname);
810
811 if (!(fin = fopen(fname, "r"))) {
812 log_it("Unable to open mountlist - '%s'", fname);
813 log_to_screen("Cannot open mountlist");
814 return (1);
815 }
816 malloc_string(incoming);
817 malloc_string(siz);
818 (void) fgets(incoming, MAX_STR_LEN - 1, fin);
819 log_it("Loading mountlist...");
820 while (!feof(fin)) {
821#if linux
822 res = sscanf(incoming,
823 "%s %s %s %s %s",
824 mountlist->el[items].device,
825 mountlist->el[items].mountpoint,
826 mountlist->el[items].format,
827 siz, mountlist->el[items].label);
828 if (res < 5) {
829 /* no label found */
830 log_msg(4, "no label found for %s",mountlist->el[items].device);
831 strcpy(mountlist->el[items].label,"");
832 }
833#elif __FreeBSD__
834 res = sscanf(incoming,
835 "%s %s %s %s",
836 mountlist->el[items].device,
837 mountlist->el[items].mountpoint,
838 mountlist->el[items].format, siz);
839 strcpy(mountlist->el[items].label,"");
840#endif
841
842 if (!strcmp(mountlist->el[items].device, "/proc") ||
843 !strcmp(mountlist->el[items].device, "proc") ||
844 !strcmp(mountlist->el[items].device, "/sys") ||
845 !strcmp(mountlist->el[items].device, "sys") ||
846 !strcmp(mountlist->el[items].device, "/devpts") ||
847 !strcmp(mountlist->el[items].device, "devpts")
848 ) {
849 log_msg(1,
850 "Ignoring %s in mountlist - not loading that line :) ",
851 mountlist->el[items].device);
852 (void) fgets(incoming, MAX_STR_LEN - 1, fin);
853 continue;
854 }
855 mountlist->el[items].size = atoll(siz);
856 if (mountlist->el[items].device[0] != '\0'
857 && mountlist->el[items].device[0] != '#') {
858 for (j = 0;
859 j < items
860 && strcmp(mountlist->el[j].device,
861 mountlist->el[items].device); j++);
862 if (j < items) {
863 strcat(mountlist->el[items].device, "_dup");
864 mr_asprintf(&tmp, "Duplicate entry in mountlist - renaming to %s", mountlist->el[items].device);
865 log_it(tmp);
866 paranoid_free(tmp);
867 }
868 mr_asprintf(&tmp, "%s", mountlist->el[items].device);
869 if (strstr(tmp, "/dev/md/")) {
870 log_it("format_device() --- Contracting %s", tmp);
871 p = strrchr(tmp, '/');
872 if (p) {
873 *p = *(p + 1);
874 *(p + 1) = *(p + 2);
875 *(p + 2) = *(p + 3);
876 }
877 log_it("It was %s; it is now %s",
878 mountlist->el[items].device, tmp);
879 strcpy(mountlist->el[items].device, tmp);
880 }
881 paranoid_free(tmp);
882
883 log_it("%s %s %s %lld %s",
884 mountlist->el[items].device,
885 mountlist->el[items].mountpoint,
886 mountlist->el[items].format,
887 mountlist->el[items].size,
888 mountlist->el[items].label);
889 items++;
890 if (items >= MAX_MOUNTLIST_ENTRIES) {
891 log_to_screen("Too many lines in mountlist.. ABORTING");
892 finish(1);
893 }
894 }
895 (void) fgets(incoming, MAX_STR_LEN - 1, fin);
896 }
897 paranoid_fclose(fin);
898 mountlist->entries = items;
899
900 log_it("Mountlist loaded successfully.");
901 log_it("%d entries in mountlist", items);
902
903 paranoid_free(incoming);
904 paranoid_free(siz);
905 return (0);
906}
907
908
909
910/**
911 * Save @p mountlist to a file on disk.
912 * @param mountlist The mountlist to save.
913 * @param fname The file to save it to.
914 * @return 0 for success, 1 for failure.
915 * @see load_mountlist
916 */
917int save_mountlist_to_disk(struct mountlist_itself *mountlist, char *fname)
918{
919 FILE *fout;
920 int i;
921
922 assert(mountlist != NULL);
923 assert_string_is_neither_NULL_nor_zerolength(fname);
924
925 log_it("save_mountlist_to_disk() --- saving to %s", fname);
926 if (!(fout = fopen(fname, "w"))) {
927 log_OS_error("WMTD - Cannot openout mountlist");
928 return (1);
929 }
930 for (i = 0; i < mountlist->entries; i++) {
931 fprintf(fout,
932 "%-15s %-15s %-15s %-15lld %-15s\n",
933 mountlist->el[i].device, mountlist->el[i].mountpoint,
934 mountlist->el[i].format, mountlist->el[i].size,
935 mountlist->el[i].label);
936 }
937 paranoid_fclose(fout);
938 return (0);
939}
940
941
942/**
943 * Sort the mountlist alphabetically by device.
944 * The sorting is done in-place.
945 * @param mountlist The mountlist to sort.
946 */
947void sort_mountlist_by_device(struct mountlist_itself *mountlist)
948{
949 int diff;
950 int lino = -999;
951
952 assert(mountlist != NULL);
953
954 while (lino < mountlist->entries) {
955 for (lino = 1; lino < mountlist->entries; lino++) {
956 diff =
957 strcmp_inc_numbers(mountlist->el[lino - 1].device,
958 mountlist->el[lino].device);
959 if (diff > 0) {
960 swap_mountlist_entries(mountlist, lino - 1, lino);
961 break;
962 }
963 }
964 }
965}
966
967
968/**
969 * Sort the mountlist alphabetically by mountpoint.
970 * The sorting is done in-place.
971 * @param mountlist The mountlist to sort.
972 * @param reverse If TRUE, then do a reverse sort.
973 */
974void
975sort_mountlist_by_mountpoint(struct mountlist_itself *mountlist,
976 bool reverse)
977{
978 int diff;
979 int lino = -999;
980
981 assert(mountlist != NULL);
982
983 while (lino < mountlist->entries) {
984 for (lino = 1; lino < mountlist->entries; lino++) {
985 diff =
986 strcmp(mountlist->el[lino - 1].mountpoint,
987 mountlist->el[lino].mountpoint);
988 if ((diff > 0 && !reverse) || ((diff < 0 && reverse))) {
989 swap_mountlist_entries(mountlist, lino - 1, lino);
990 break;
991 }
992 }
993 }
994}
995
996
997/**
998 * Swap two entries in the mountlist in-place.
999 * @param mountlist The mountlist to swap the entries in.
1000 * @param a The index number of the first entry.
1001 * @param b The index number of the second entry.
1002 */
1003void
1004swap_mountlist_entries(struct mountlist_itself *mountlist, int a, int b)
1005{
1006 /*@ mallocs *** */
1007 char *device = NULL;
1008 char *mountpoint = NULL;
1009 char *format = NULL;
1010
1011 long long size;
1012
1013 assert(mountlist != NULL);
1014 assert(a >= 0);
1015 assert(b >= 0);
1016
1017 mr_asprintf(&device, "%s", mountlist->el[a].device);
1018 mr_asprintf(&mountpoint, "%s", mountlist->el[a].mountpoint);
1019 mr_asprintf(&format, "%s", mountlist->el[a].format);
1020
1021 size = mountlist->el[a].size;
1022
1023 strcpy(mountlist->el[a].device, mountlist->el[b].device);
1024 strcpy(mountlist->el[a].mountpoint, mountlist->el[b].mountpoint);
1025 strcpy(mountlist->el[a].format, mountlist->el[b].format);
1026
1027 mountlist->el[a].size = mountlist->el[b].size;
1028
1029 strcpy(mountlist->el[b].device, device);
1030 strcpy(mountlist->el[b].mountpoint, mountpoint);
1031 strcpy(mountlist->el[b].format, format);
1032
1033 mountlist->el[b].size = size;
1034 paranoid_free(device);
1035 paranoid_free(mountpoint);
1036 paranoid_free(format);
1037}
1038
1039/* @} - end of mountlistGroup */
Note: See TracBrowser for help on using the repository browser.