source: MondoRescue/branches/2.2.2/mondo/src/common/libmondo-mountlist.c@ 1303

Last change on this file since 1303 was 1303, checked in by Bruno Cornec, 17 years ago
  • Some FreeBSD fixes
  • Better messages for partition names
  • May improve the gap issue on cciss
  • Property svn:keywords set to Id
File size: 31.9 KB
Line 
1/* libmondo-mountlist.c subroutines for handling mountlist
2 $Id: libmondo-mountlist.c 1303 2007-04-15 00:33:22Z 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 1303 2007-04-15 00:33:22Z 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 /* BERLIOS: Flawed since rev 1 !! */
374 res++;
375 }
376 if (res) {
377 return (FALSE);
378 } else {
379 return (TRUE);
380 }
381}
382
383#else
384// Linux-specific version of evaluate_drive_within_mountlist()
385{
386
387 /*@ int ************************************************************* */
388 int prev_part_no = 0;
389 int curr_part_no = 0;
390 int pos = 0;
391 int res = 0;
392 int mountpoint_copies = 0;
393 int device_copies = 0;
394 int i = 0;
395
396 /*@ buffers ******************************************************** */
397 char *tmp;
398 char *device;
399
400 /*@ long *********************************************************** */
401 long physical_drive_size = 0L;
402 long amount_allocated = 0L;
403
404 /*@ pointers ******************************************************* */
405 char *part_table_fmt;
406
407 /*@ initialize ***************************************************** */
408 assert_string_is_neither_NULL_nor_zerolength(drive);
409 assert(mountlist != NULL);
410 assert(flaws_str != NULL);
411
412 malloc_string(tmp);
413 malloc_string(device);
414 flaws_str[0] = '\0';
415 prev_part_no = 0;
416 tmp[0] = '\0';
417
418
419 physical_drive_size = get_phys_size_of_drive(drive);
420
421 if (physical_drive_size < 0) {
422 sprintf(tmp, " %s does not exist.", drive);
423 strcat(flaws_str, tmp);
424 res++;
425 log_msg(1, tmp);
426 goto endoffunc;
427 } else {
428 sprintf(tmp, "%s is %ld MB", drive, physical_drive_size);
429 log_it(tmp);
430 }
431
432 for (curr_part_no = 1; curr_part_no < 99; curr_part_no++) {
433 build_partition_name(device, drive, curr_part_no);
434 pos = find_device_in_mountlist(mountlist, device);
435 if (pos < 0) {
436 continue;
437 }
438 /* gap in the partition list? */
439 if (curr_part_no - prev_part_no > 1) {
440 if (prev_part_no == 0) {
441 sprintf(tmp, " Gap prior to %s.", device);
442 log_it(tmp);
443 strcat(flaws_str, tmp);
444 res++;
445 } else if (curr_part_no > 5
446 || (curr_part_no <= 4 && prev_part_no > 0)) {
447 sprintf(tmp, " Gap on %s between %d and %d.", drive,
448 prev_part_no, curr_part_no);
449 log_it(tmp);
450 strcat(flaws_str, tmp);
451 res++;
452 }
453 }
454 /* GPT allows more than 4 primary partitions */
455 part_table_fmt = which_partition_format(drive);
456 /* no spare primary partitions to help accommodate the logical(s)? */
457 if ((curr_part_no >= 5 && prev_part_no == 4)
458 && (strcmp(part_table_fmt, "MBR") == 0)) {
459 sprintf(tmp, " Partition 4 of %s is occupied.", drive);
460 log_it(tmp);
461 strcat(flaws_str, tmp);
462 res++;
463 }
464 /* does partition /dev/hdNX exist more than once in the mountlist? */
465 for (i = 0, mountpoint_copies = 0, device_copies = 0;
466 i < mountlist->entries; i++) {
467 if (!strcmp(device, mountlist->el[i].device)) {
468 device_copies++;
469 }
470 }
471 if (device_copies > 1) {
472 sprintf(tmp, " %s %s's.", number_to_text(device_copies),
473 device);
474 if (!strstr(flaws_str, tmp)) {
475 log_it(tmp);
476 strcat(flaws_str, tmp);
477 res++;
478 }
479 }
480 /* silly partition size? */
481 if (mountlist->el[pos].size < 8192
482 && strcmp(mountlist->el[pos].mountpoint, "lvm")) {
483 sprintf(tmp, " %s is tiny!", device);
484 log_it(tmp);
485 strcat(flaws_str, tmp);
486 res++;
487 }
488 /* mountpoint should begin with / unless it is swap, lvm or raid */
489 if (strcmp(mountlist->el[pos].mountpoint, "swap")
490 && strcmp(mountlist->el[pos].mountpoint, "lvm")
491 && strcmp(mountlist->el[pos].mountpoint, "raid")
492 && strcmp(mountlist->el[pos].mountpoint, "image")
493 && mountlist->el[pos].mountpoint[0] != '/') {
494 sprintf(tmp, " %s has a weird mountpoint.", device);
495 log_it(tmp);
496 strcat(flaws_str, tmp);
497 res++;
498 }
499 /* is format sensible? */
500 if (!is_this_a_valid_disk_format(mountlist->el[pos].format)) {
501 sprintf(tmp, " %s has unsupported format %s.", device, mountlist->el[pos].format);
502 log_it(tmp);
503 strcat(flaws_str, tmp);
504 res++;
505 }
506 /* OK, continue with main loop */
507 amount_allocated += mountlist->el[pos].size / 1024;
508 prev_part_no = curr_part_no;
509 }
510
511 /* Over-allocated the disk? Unallocated space on disk? */
512 if (amount_allocated > physical_drive_size + 1) {
513 sprintf(tmp, " %ld MB over-allocated on %s.",
514 amount_allocated - physical_drive_size, drive);
515 log_it(tmp);
516 strcat(flaws_str, tmp);
517 res++;
518 } else if (amount_allocated < physical_drive_size - 1) { /* NOT AN ERROR, JUST A WARNING :-) */
519 sprintf(tmp, " %ld MB unallocated on %s.",
520 physical_drive_size - amount_allocated, drive);
521 log_it(tmp);
522 strcat(flaws_str, tmp);
523 /* BERLIOS: Flawed since rev 1 !! */
524 res++;
525 }
526
527 endoffunc:
528 paranoid_free(tmp);
529 paranoid_free(device);
530
531 if (res) {
532 return (FALSE);
533 } else {
534 return (TRUE);
535 }
536}
537#endif
538
539
540
541/**
542 * Evaluate a whole mountlist for flaws. Calls evaluate_drive_within_mountlist()
543 * for each drive, and then spreads the flaws across three lines.
544 * @param mountlist The mountlist to evaluate.
545 * @param flaws_str_A Where to put the first line listing errors found.
546 * @param flaws_str_B Where to put the second line listing errors found.
547 * @param flaws_str_C Where to put the third line listing errors found.
548 * @return The number of flaws found (0 for success).
549 * @see evaluate_drive_within_mountlist
550 */
551int
552evaluate_mountlist(struct mountlist_itself *mountlist, char *flaws_str_A,
553 char *flaws_str_B, char *flaws_str_C)
554{
555
556 /*@ buffer *********************************************************** */
557 struct list_of_disks *drivelist;
558 char *tmp;
559 char *flaws_str;
560
561 /*@ int ************************************************************** */
562 int i = 0;
563 int res = 0;
564
565 /*@ initialize ******************************************************* */
566
567 drivelist = malloc(sizeof(struct list_of_disks));
568 malloc_string(tmp);
569 malloc_string(flaws_str);
570 assert(mountlist != NULL);
571 assert(flaws_str_A != NULL);
572 assert(flaws_str_B != NULL);
573 assert(flaws_str_C != NULL);
574 flaws_str[0] = '\0';
575
576 make_list_of_drives_in_mountlist(mountlist, drivelist);
577
578 log_it("Evaluating mountlist...");
579
580 for (i = 0; i < drivelist->entries; i++) {
581 if (strstr
582 (drivelist->el[i].device,
583 DONT_KNOW_HOW_TO_EVALUATE_THIS_DEVICE_TYPE)) {
584 sprintf(tmp, " Not evaluating %s (I don't know how yet)",
585 drivelist->el[i].device);
586 log_it(tmp);
587 tmp[0] = '\0';
588 } else {
589 if (!evaluate_drive_within_mountlist
590 (mountlist, drivelist->el[i].device, tmp)) {
591 res++;
592 }
593 }
594 strcat(flaws_str, tmp);
595 }
596 res += look_for_duplicate_mountpoints(mountlist, flaws_str);
597/* res+=look_for_weird_formats(mountlist,flaws_str); .. not necessary, now that we can check to see
598 which formarts are actually _supported_ by the kernel */
599 /* log_it(flaws_str); */
600 return (spread_flaws_across_three_lines
601 (flaws_str, flaws_str_A, flaws_str_B, flaws_str_C, res));
602}
603
604
605/**
606 * Find the index number of @p device in the mountlist.
607 * The device given must match @p mountlist->el[N].device exactly, case-sensitive.
608 * @param mountlist The mountlist to search in.
609 * @param device The device to search for.
610 * @return The zero-based index of the device, or -1 if it could not be found.
611 */
612int
613find_device_in_mountlist(struct mountlist_itself *mountlist, char *device)
614{
615
616 /*@ int ************************************************************** */
617 int i = 0;
618 char *tmp;
619 char *flaws_str;
620
621 malloc_string(tmp);
622 malloc_string(flaws_str);
623
624 assert(mountlist != NULL);
625 assert_string_is_neither_NULL_nor_zerolength(device);
626 for (i = 0;
627 i < mountlist->entries
628 && strcmp(mountlist->el[i].device, device) != 0; i++);
629
630 paranoid_free(tmp);
631 paranoid_free(flaws_str);
632
633 if (i == mountlist->entries) {
634 return (-1);
635 } else {
636 return (i);
637 }
638}
639
640
641
642
643
644/**
645 * Look for duplicate mountpoints in @p mountlist.
646 * @param mountlist The mountlist to check.
647 * @param flaws_str The flaws string to append the results to.
648 * @return The number of mountpoints that have duplicates, or 0 for success.
649 */
650int
651look_for_duplicate_mountpoints(struct mountlist_itself *mountlist,
652 char *flaws_str)
653{
654
655 /*@ int ************************************************************* */
656 int res = 0;
657 int currline = 0;
658 int i = 0;
659 int copies = 0;
660 int last_copy = 0;
661
662 /*@ buffetr ********************************************************* */
663 char *curr_mountpoint;
664 char *tmp;
665
666 malloc_string(curr_mountpoint);
667 malloc_string(tmp);
668 assert(mountlist != NULL);
669 assert(flaws_str != NULL);
670 for (currline = 0; currline < mountlist->entries; currline++) {
671 strcpy(curr_mountpoint, mountlist->el[currline].mountpoint);
672 for (i = 0, copies = 0, last_copy = -1; i < mountlist->entries;
673 i++) {
674 if (!strcmp(mountlist->el[i].mountpoint, curr_mountpoint)
675 && strcmp(mountlist->el[i].mountpoint, "lvm")
676 && strcmp(mountlist->el[i].mountpoint, "swap")) {
677 last_copy = i;
678 copies++;
679 }
680 }
681 if (copies > 1 && last_copy == currline
682 && strcmp(curr_mountpoint, "raid")) {
683 sprintf(tmp, " %s %s's.", number_to_text(copies),
684 curr_mountpoint);
685 strcat(flaws_str, tmp);
686 log_it(tmp);
687 res++;
688 }
689 }
690 paranoid_free(curr_mountpoint);
691 paranoid_free(tmp);
692 return (res);
693}
694
695/**
696 * Look for strange formats. Does not respect /proc/filesystems.
697 * @param mountlist The mountlist to check.
698 * @param flaws_str The flaws string to append the results to.
699 * @return The number of weird formats found, or 0 for success.
700 * @bug Seems orphaned; please remove.
701 */
702int
703look_for_weird_formats(struct mountlist_itself *mountlist, char *flaws_str)
704{
705
706 /*@ int ************************************************************* */
707 int i = 0;
708 int res = 0;
709
710 /*@ buffers ********************************************************* */
711 char *tmp;
712 char *format_sz;
713
714 malloc_string(tmp);
715 malloc_string(format_sz);
716
717 assert(mountlist != NULL);
718 assert(flaws_str != NULL);
719
720 for (i = 0; i < mountlist->entries; i++) {
721 sprintf(format_sz, " %s ", mountlist->el[i].format);
722 if (!strstr(SANE_FORMATS, format_sz)
723 && strcmp(mountlist->el[i].mountpoint, "image") != 0) {
724 sprintf(tmp, " %s has unknown format.",
725 mountlist->el[i].device);
726 log_it(tmp);
727 strcat(flaws_str, tmp);
728 res++;
729 } else if ((!strcmp(mountlist->el[i].format, "swap")
730 && strcmp(mountlist->el[i].mountpoint, "swap")
731 && strcmp(mountlist->el[i].mountpoint, "none"))
732 || (strcmp(mountlist->el[i].format, "swap")
733 && !strcmp(mountlist->el[i].mountpoint, "swap")
734 && !strcmp(mountlist->el[i].mountpoint, "none"))) {
735 sprintf(tmp, " %s is half-swap.", mountlist->el[i].device);
736 log_it(tmp);
737 strcat(flaws_str, tmp);
738 res++;
739 }
740 }
741 paranoid_free(tmp);
742 paranoid_free(format_sz);
743 return (res);
744}
745
746
747
748/**
749 * Make a list of the drives mentioned in the mountlist.
750 * @param mountlist The mountlist to examine.
751 * @param drivelist Where to put the list of drives found.
752 * @return The number of physical (non-RAID non-LVM) drives found, or \<= 0 for error.
753 */
754int
755make_list_of_drives_in_mountlist(struct mountlist_itself *mountlist,
756 struct list_of_disks *drivelist)
757{
758
759 /*@ int ************************************************************* */
760 int lino;
761 int noof_drives;
762 int j;
763
764 /*@ buffers ********************************************************* */
765 char *drive;
766 char *tmp;
767
768 long long size;
769
770 malloc_string(drive);
771 malloc_string(tmp);
772 assert(mountlist != NULL);
773 assert(drivelist != NULL);
774 log_it("Making list of drives");
775 for (lino = 0, noof_drives = 0; lino < mountlist->entries; lino++) {
776
777 strcpy(drive, mountlist->el[lino].device);
778 if (!strncmp(drive, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB))) {
779 sprintf(tmp,
780 "Not putting %s in list of drives: it's a virtual drive",
781 drive);
782 log_msg(8, tmp);
783 continue;
784 }
785
786 size = mountlist->el[lino].size;
787 if (size == 0) {
788 sprintf(tmp,
789 "Not putting %s in list of drives: it has zero size (maybe an LVM volume)",
790 drive);
791 log_msg(8, tmp);
792 continue;
793 }
794
795/*
796 for (i = strlen (drive); isdigit (drive[i - 1]); i--);
797 drive[i] = '\0';
798 if (get_phys_size_of_drive (drive) <= 0 && drive[i - 1] == 'p')
799 {
800 i--;
801 drive[i] = '\0';
802 }
803 for (j = 0; j < noof_drives && strcmp (drivelist[j], drive) != 0; j++);
804*/
805
806 sprintf(tmp,
807 "Putting %s with size %lli in list of drives",
808 drive, size);
809 log_msg(8, tmp);
810
811 (void) truncate_to_drive_name(drive);
812 for (j = 0;
813 j < noof_drives
814 && strcmp(drivelist->el[j].device, drive) != 0; j++)
815 continue;
816 if (j == noof_drives) {
817 strcpy(drivelist->el[noof_drives++].device, drive);
818 }
819 }
820 drivelist->entries = noof_drives;
821 log_msg(8, "Made list of drives");
822 paranoid_free(drive);
823 paranoid_free(tmp);
824
825 return (noof_drives);
826}
827
828
829
830
831
832
833/**
834 * Make a list of RAID partitions not currently associated with any RAID device.
835 * The user may add any of these partitions to the RAID device.
836 * @param output_list Where to put the list of unallocated RAID partitions.
837 * @param mountlist The mountlist to examine.
838 * @param raidlist The raidlist to examine.
839 */
840void make_list_of_unallocated_raid_partitions(struct mountlist_itself
841 *output_list,
842 struct mountlist_itself
843 *mountlist,
844 struct raidlist_itself
845 *raidlist)
846{
847
848 /*@ int ************************************************************* */
849 int items = 0;
850 int i = 0;
851 int used_by = 0;
852
853 /*@ buffers ********************************************************* */
854 char *tmp;
855
856 malloc_string(tmp);
857 assert(output_list != NULL);
858 assert(mountlist != NULL);
859 assert(raidlist != NULL);
860 log_it("MLOURP -- starting");
861 items = 0;
862
863
864 for (i = 0; i < mountlist->entries; i++) {
865 if (strstr(mountlist->el[i].mountpoint, "raid")) {
866 used_by =
867 which_raid_device_is_using_this_partition(raidlist,
868 mountlist->el[i].
869 device);
870 if (used_by < 0) {
871 memcpy((void *) &output_list->el[items++],
872 (void *) &mountlist->el[i],
873 sizeof(struct mountlist_line));
874 sprintf(tmp,
875 "%s is available; user may choose to add it to raid device",
876 output_list->el[items - 1].device);
877 log_it(tmp);
878 }
879 }
880 }
881 output_list->entries = items;
882 log_it("MLUORP -- ending");
883 paranoid_free(tmp);
884}
885
886
887
888
889
890/**
891 * Get the size of a mountlist entry by the @c device field.
892 * @param mountlist The mountlist to search in.
893 * @param device The device to search for
894 * @return The size of the device (in KB), or -1 if it could not be found.
895 */
896long long
897size_of_specific_device_in_mountlist(struct mountlist_itself *mountlist,
898 char *device)
899{
900 /*@ int ************************************************************** */
901 int i = 0;
902
903
904 assert(mountlist != NULL);
905 assert_string_is_neither_NULL_nor_zerolength(device);
906
907 for (i = 0;
908 i < mountlist->entries && strcmp(mountlist->el[i].device, device);
909 i++);
910 if (i == mountlist->entries) {
911 return (-1);
912 } else {
913 return (mountlist->el[i].size);
914 }
915}
916
917
918
919
920/**
921 * Load a file on disk into @p mountlist.
922 * The file on disk should consist of multiple lines, each containing 4 or 5
923 * columns: the device, the mountpoint, the filesystem type, the size in kilobytes, and optionally the filesystem label.
924 * Comments begin with a '#' without any leading whitespace. Any duplicate
925 * entries are renamed.
926 * @param mountlist The mountlist to load into.
927 * @param fname The name of the file to load the mountlist from.
928 * @return 0 for success, 1 for failure.
929 */
930int load_mountlist(struct mountlist_itself *mountlist, char *fname)
931{
932 FILE *fin;
933 /* malloc ** */
934 char *incoming;
935 char *siz;
936 char *tmp;
937 char *p;
938
939 int items;
940 int j;
941
942 assert(mountlist != NULL);
943 assert_string_is_neither_NULL_nor_zerolength(fname);
944 malloc_string(incoming);
945 malloc_string(siz);
946 malloc_string(tmp);
947 if (!(fin = fopen(fname, "r"))) {
948 log_it("Unable to open mountlist - '%s'", fname);
949 log_to_screen("Cannot open mountlist");
950 paranoid_free(incoming);
951 paranoid_free(siz);
952 paranoid_free(tmp);
953 return (1);
954 }
955 items = 0;
956 (void) fgets(incoming, MAX_STR_LEN - 1, fin);
957 log_it("Loading mountlist...");
958 while (!feof(fin)) {
959#if linux
960 sscanf(incoming,
961 "%s %s %s %s %s %s",
962 mountlist->el[items].device,
963 mountlist->el[items].mountpoint,
964 mountlist->el[items].format,
965 siz, mountlist->el[items].label, mountlist->el[items].uuid);
966#elif __FreeBSD__
967 sscanf(incoming,
968 "%s %s %s %s",
969 mountlist->el[items].device,
970 mountlist->el[items].mountpoint,
971 mountlist->el[items].format, siz);
972 strcpy(mountlist->el[items].label, "");
973 strcpy(mountlist->el[items].uuid, "");
974#endif
975
976 if (!strcmp(mountlist->el[items].device, "/proc") ||
977 !strcmp(mountlist->el[items].device, "proc") ||
978 !strcmp(mountlist->el[items].device, "/sys") ||
979 !strcmp(mountlist->el[items].device, "sys") ||
980 !strcmp(mountlist->el[items].device, "/devpts") ||
981 !strcmp(mountlist->el[items].device, "devpts")
982 ) {
983 log_msg(1,
984 "Ignoring %s in mountlist - not loading that line :) ",
985 mountlist->el[items].device);
986 (void) fgets(incoming, MAX_STR_LEN - 1, fin);
987 continue;
988 }
989 mountlist->el[items].size = atoll(siz);
990 if (mountlist->el[items].device[0] != '\0'
991 && mountlist->el[items].device[0] != '#') {
992 if (items >= ARBITRARY_MAXIMUM) {
993 log_to_screen("Too many lines in mountlist.. ABORTING");
994 finish(1);
995 }
996 for (j = 0;
997 j < items
998 && strcmp(mountlist->el[j].device,
999 mountlist->el[items].device); j++);
1000 if (j < items) {
1001 strcat(mountlist->el[items].device, "_dup");
1002 sprintf(tmp,
1003 "Duplicate entry in mountlist - renaming to %s",
1004 mountlist->el[items].device);
1005 log_it(tmp);
1006 }
1007 strcpy(tmp, mountlist->el[items].device);
1008 if (strstr(tmp, "/dev/md/")) {
1009 log_it("format_device() --- Contracting %s", tmp);
1010 p = strrchr(tmp, '/');
1011 if (p) {
1012 *p = *(p + 1);
1013 *(p + 1) = *(p + 2);
1014 *(p + 2) = *(p + 3);
1015 }
1016 log_it("It was %s; it is now %s",
1017 mountlist->el[items].device, tmp);
1018 strcpy(mountlist->el[items].device, tmp);
1019 }
1020
1021 sprintf(tmp,
1022 "%s %s %s %lld %s %s",
1023 mountlist->el[items].device,
1024 mountlist->el[items].mountpoint,
1025 mountlist->el[items].format,
1026 mountlist->el[items].size,
1027 mountlist->el[items].label,
1028 mountlist->el[items].uuid);
1029
1030 log_it(tmp);
1031 items++;
1032 }
1033 (void) fgets(incoming, MAX_STR_LEN - 1, fin);
1034 }
1035 paranoid_fclose(fin);
1036 mountlist->entries = items;
1037
1038 log_it("Mountlist loaded successfully.");
1039 sprintf(tmp, "%d entries in mountlist", items);
1040 log_it(tmp);
1041 paranoid_free(incoming);
1042 paranoid_free(siz);
1043 paranoid_free(tmp);
1044 return (0);
1045}
1046
1047
1048
1049/**
1050 * Save @p mountlist to a file on disk.
1051 * @param mountlist The mountlist to save.
1052 * @param fname The file to save it to.
1053 * @return 0 for success, 1 for failure.
1054 * @see load_mountlist
1055 */
1056int save_mountlist_to_disk(struct mountlist_itself *mountlist, char *fname)
1057{
1058 FILE *fout;
1059 int i;
1060
1061 assert(mountlist != NULL);
1062 assert_string_is_neither_NULL_nor_zerolength(fname);
1063
1064 log_it("save_mountlist_to_disk() --- saving to %s", fname);
1065 if (!(fout = fopen(fname, "w"))) {
1066 log_OS_error("WMTD - Cannot openout mountlist");
1067 return (1);
1068 }
1069 for (i = 0; i < mountlist->entries; i++) {
1070 fprintf(fout,
1071 "%-15s %-15s %-15s %-15lld %-15s\n",
1072 mountlist->el[i].device, mountlist->el[i].mountpoint,
1073 mountlist->el[i].format, mountlist->el[i].size,
1074 mountlist->el[i].label, mountlist->el[i].uuid);
1075 }
1076 paranoid_fclose(fout);
1077 return (0);
1078}
1079
1080
1081
1082/**
1083 * Sort the mountlist alphabetically by device.
1084 * The sorting is done in-place.
1085 * @param mountlist The mountlist to sort.
1086 */
1087void sort_mountlist_by_device(struct mountlist_itself *mountlist)
1088{
1089 int diff;
1090 int lino = -999;
1091
1092 assert(mountlist != NULL);
1093
1094 while (lino < mountlist->entries) {
1095 for (lino = 1; lino < mountlist->entries; lino++) {
1096 diff =
1097 strcmp_inc_numbers(mountlist->el[lino - 1].device,
1098 mountlist->el[lino].device);
1099 if (diff > 0) {
1100 swap_mountlist_entries(mountlist, lino - 1, lino);
1101 break;
1102 }
1103 }
1104 }
1105}
1106
1107/**
1108 * Sort the mountlist alphabetically by mountpoint.
1109 * The sorting is done in-place.
1110 * @param mountlist The mountlist to sort.
1111 * @param reverse If TRUE, then do a reverse sort.
1112 */
1113void
1114sort_mountlist_by_mountpoint(struct mountlist_itself *mountlist,
1115 bool reverse)
1116{
1117 int diff;
1118 int lino = -999;
1119
1120 assert(mountlist != NULL);
1121
1122 while (lino < mountlist->entries) {
1123 for (lino = 1; lino < mountlist->entries; lino++) {
1124 diff =
1125 strcmp(mountlist->el[lino - 1].mountpoint,
1126 mountlist->el[lino].mountpoint);
1127 if ((diff > 0 && !reverse) || ((diff < 0 && reverse))) {
1128 swap_mountlist_entries(mountlist, lino - 1, lino);
1129 break;
1130 }
1131 }
1132 }
1133}
1134
1135
1136/**
1137 * Swap two entries in the mountlist in-place.
1138 * @param mountlist The mountlist to swap the entries in.
1139 * @param a The index number of the first entry.
1140 * @param b The index number of the second entry.
1141 */
1142void
1143swap_mountlist_entries(struct mountlist_itself *mountlist, int a, int b)
1144{
1145 /*@ mallocs *** */
1146 char device[64];
1147 char mountpoint[256];
1148 char format[64];
1149
1150 long long size;
1151
1152 assert(mountlist != NULL);
1153 assert(a >= 0);
1154 assert(b >= 0);
1155
1156 strcpy(device, mountlist->el[a].device);
1157 strcpy(mountpoint, mountlist->el[a].mountpoint);
1158 strcpy(format, mountlist->el[a].format);
1159
1160 size = mountlist->el[a].size;
1161
1162 strcpy(mountlist->el[a].device, mountlist->el[b].device);
1163 strcpy(mountlist->el[a].mountpoint, mountlist->el[b].mountpoint);
1164 strcpy(mountlist->el[a].format, mountlist->el[b].format);
1165
1166 mountlist->el[a].size = mountlist->el[b].size;
1167
1168 strcpy(mountlist->el[b].device, device);
1169 strcpy(mountlist->el[b].mountpoint, mountpoint);
1170 strcpy(mountlist->el[b].format, format);
1171
1172 mountlist->el[b].size = size;
1173}
1174
1175/* @} - end of mountlistGroup */
Note: See TracBrowser for help on using the repository browser.