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

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