source: MondoRescue/branches/2.2.9/mondo/src/common/libmondo-mountlist.c@ 2176

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