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

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

Fix a flaw in libmondo-mountlist.c (there since rev [1] !!) dur to the change of log_it macro.

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