source: MondoRescue/branches/2.2.4/mondo/src/common/libmondo-mountlist.c@ 1439

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

Unallocated space is not an error, just a warning (res should not be incremented)

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