source: MondoRescue/branches/stable/mondo/src/common/libmondo-mountlist.c@ 1166

Last change on this file since 1166 was 1166, checked in by Bruno Cornec, 17 years ago

Suppress g_bkpinfo_DONTUSETHIS (Idea from M. Loiseleur)

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