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

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

Suppress lib-common-externs.h useless

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