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

Last change on this file since 2202 was 2202, checked in by Bruno Cornec, 15 years ago
  • Remove useless function look_for_weird_formats
  • Adds a test program for mountlist management to find a seg. fault bug when using a large mountlist file
  • Property svn:keywords set to Id
File size: 28.8 KB
Line 
1/* subroutines for handling mountlist
2 $Id: libmondo-mountlist.c 2202 2009-05-13 02:34:00Z 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 2202 2009-05-13 02:34:00Z 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 = NULL;
511 char *flaws_str = NULL;
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 return (spread_flaws_across_three_lines
550 (flaws_str, flaws_str_A, flaws_str_B, flaws_str_C, res));
551}
552
553
554/**
555 * Find the index number of @p device in the mountlist.
556 * The device given must match @p mountlist->el[N].device exactly, case-sensitive.
557 * @param mountlist The mountlist to search in.
558 * @param device The device to search for.
559 * @return The zero-based index of the device, or -1 if it could not be found.
560 */
561int
562find_device_in_mountlist(struct mountlist_itself *mountlist, char *device)
563{
564
565 /*@ int ************************************************************** */
566 int i = 0;
567 char *tmp;
568 char *flaws_str;
569
570 malloc_string(tmp);
571 malloc_string(flaws_str);
572
573 assert(mountlist != NULL);
574 assert_string_is_neither_NULL_nor_zerolength(device);
575 for (i = 0;
576 i < mountlist->entries
577 && strcmp(mountlist->el[i].device, device) != 0; i++);
578
579 paranoid_free(tmp);
580 paranoid_free(flaws_str);
581
582 if (i == mountlist->entries) {
583 return (-1);
584 } else {
585 return (i);
586 }
587}
588
589
590
591
592
593/**
594 * Look for duplicate mountpoints in @p mountlist.
595 * @param mountlist The mountlist to check.
596 * @param flaws_str The flaws string to append the results to.
597 * @return The number of mountpoints that have duplicates, or 0 for success.
598 */
599int
600look_for_duplicate_mountpoints(struct mountlist_itself *mountlist,
601 char *flaws_str)
602{
603
604 /*@ int ************************************************************* */
605 int res = 0;
606 int currline = 0;
607 int i = 0;
608 int copies = 0;
609 int last_copy = 0;
610
611 /*@ buffetr ********************************************************* */
612 char *curr_mountpoint;
613 char *tmp;
614
615 malloc_string(curr_mountpoint);
616 malloc_string(tmp);
617 assert(mountlist != NULL);
618 assert(flaws_str != NULL);
619 for (currline = 0; currline < mountlist->entries; currline++) {
620 strcpy(curr_mountpoint, mountlist->el[currline].mountpoint);
621 for (i = 0, copies = 0, last_copy = -1; i < mountlist->entries;
622 i++) {
623 if (!strcmp(mountlist->el[i].mountpoint, curr_mountpoint)
624 && strcmp(mountlist->el[i].mountpoint, "lvm")
625 && strcmp(mountlist->el[i].mountpoint, "swap")) {
626 last_copy = i;
627 copies++;
628 }
629 }
630 if (copies > 1 && last_copy == currline
631 && strcmp(curr_mountpoint, "raid")) {
632 sprintf(tmp, " %s %s's.", number_to_text(copies),
633 curr_mountpoint);
634 strcat(flaws_str, tmp);
635 log_it(tmp);
636 res++;
637 }
638 }
639 paranoid_free(curr_mountpoint);
640 paranoid_free(tmp);
641 return (res);
642}
643
644
645/**
646 * Make a list of the drives mentioned in the mountlist.
647 * @param mountlist The mountlist to examine.
648 * @param drivelist Where to put the list of drives found.
649 * @return The number of physical (non-RAID non-LVM) drives found, or \<= 0 for error.
650 */
651int
652make_list_of_drives_in_mountlist(struct mountlist_itself *mountlist,
653 struct list_of_disks *drivelist)
654{
655
656 /*@ int ************************************************************* */
657 int lino;
658 int noof_drives;
659 int j;
660
661 /*@ buffers ********************************************************* */
662 char *drive;
663 char *tmp;
664
665 long long size;
666
667 malloc_string(drive);
668 malloc_string(tmp);
669 assert(mountlist != NULL);
670 assert(drivelist != NULL);
671 log_it("Making list of drives");
672 for (lino = 0, noof_drives = 0; lino < mountlist->entries; lino++) {
673
674 strcpy(drive, mountlist->el[lino].device);
675 if (!strncmp(drive, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB))) {
676 sprintf(tmp,
677 "Not putting %s in list of drives: it's a virtual drive",
678 drive);
679 log_msg(8, tmp);
680 continue;
681 }
682
683 size = mountlist->el[lino].size;
684 if (size == 0) {
685 sprintf(tmp,
686 "Not putting %s in list of drives: it has zero size (maybe an LVM volume)",
687 drive);
688 log_msg(8, tmp);
689 continue;
690 }
691
692 sprintf(tmp,
693 "Putting %s with size %lli in list of drives",
694 drive, size);
695 log_msg(8, tmp);
696
697 (void) truncate_to_drive_name(drive);
698 for (j = 0;
699 j < noof_drives
700 && strcmp(drivelist->el[j].device, drive) != 0; j++)
701 continue;
702 if (j == noof_drives) {
703 strcpy(drivelist->el[noof_drives++].device, drive);
704 }
705 }
706 drivelist->entries = noof_drives;
707 log_msg(8, "Made list of drives");
708 paranoid_free(drive);
709 paranoid_free(tmp);
710
711 return (noof_drives);
712}
713
714
715
716
717
718
719/**
720 * Make a list of RAID partitions not currently associated with any RAID device.
721 * The user may add any of these partitions to the RAID device.
722 * @param output_list Where to put the list of unallocated RAID partitions.
723 * @param mountlist The mountlist to examine.
724 * @param raidlist The raidlist to examine.
725 */
726void make_list_of_unallocated_raid_partitions(struct mountlist_itself
727 *output_list,
728 struct mountlist_itself
729 *mountlist,
730 struct raidlist_itself
731 *raidlist)
732{
733
734 /*@ int ************************************************************* */
735 int items = 0;
736 int i = 0;
737 int used_by = 0;
738
739 /*@ buffers ********************************************************* */
740 char *tmp;
741
742 malloc_string(tmp);
743 assert(output_list != NULL);
744 assert(mountlist != NULL);
745 assert(raidlist != NULL);
746 log_it("MLOURP -- starting");
747 items = 0;
748
749
750 for (i = 0; i < mountlist->entries; i++) {
751 if (strstr(mountlist->el[i].mountpoint, "raid")) {
752 used_by =
753 which_raid_device_is_using_this_partition(raidlist,
754 mountlist->el[i].
755 device);
756 if (used_by < 0) {
757 memcpy((void *) &output_list->el[items++],
758 (void *) &mountlist->el[i],
759 sizeof(struct mountlist_line));
760 sprintf(tmp,
761 "%s is available; user may choose to add it to raid device",
762 output_list->el[items - 1].device);
763 log_it(tmp);
764 }
765 }
766 }
767 output_list->entries = items;
768 log_it("MLUORP -- ending");
769 paranoid_free(tmp);
770}
771
772
773
774
775
776/**
777 * Get the size of a mountlist entry by the @c device field.
778 * @param mountlist The mountlist to search in.
779 * @param device The device to search for
780 * @return The size of the device (in KB), or -1 if it could not be found.
781 */
782long long
783size_of_specific_device_in_mountlist(struct mountlist_itself *mountlist,
784 char *device)
785{
786 /*@ int ************************************************************** */
787 int i = 0;
788
789
790 assert(mountlist != NULL);
791 assert_string_is_neither_NULL_nor_zerolength(device);
792
793 for (i = 0;
794 i < mountlist->entries && strcmp(mountlist->el[i].device, device);
795 i++);
796 if (i == mountlist->entries) {
797 return (-1);
798 } else {
799 return (mountlist->el[i].size);
800 }
801}
802
803
804
805
806/**
807 * Load a file on disk into @p mountlist.
808 * The file on disk should consist of multiple lines, each containing 4 or 5
809 * columns: the device, the mountpoint, the filesystem type, the size in kilobytes, and optionally the filesystem label.
810 * Comments begin with a '#' without any leading whitespace. Any duplicate
811 * entries are renamed.
812 * @param mountlist The mountlist to load into.
813 * @param fname The name of the file to load the mountlist from.
814 * @return 0 for success, 1 for failure.
815 */
816int load_mountlist(struct mountlist_itself *mountlist, char *fname)
817{
818 FILE *fin;
819 /* malloc ** */
820 char *incoming;
821 char *siz;
822 char *tmp;
823 char *p;
824
825 int items;
826 int j;
827
828 assert(mountlist != NULL);
829 assert_string_is_neither_NULL_nor_zerolength(fname);
830 malloc_string(incoming);
831 malloc_string(siz);
832 malloc_string(tmp);
833 if (!(fin = fopen(fname, "r"))) {
834 log_it("Unable to open mountlist - '%s'", fname);
835 log_to_screen("Cannot open mountlist");
836 paranoid_free(incoming);
837 paranoid_free(siz);
838 paranoid_free(tmp);
839 return (1);
840 }
841 items = 0;
842 (void) fgets(incoming, MAX_STR_LEN - 1, fin);
843 log_it("Loading mountlist...");
844 while (!feof(fin)) {
845#if linux
846 sscanf(incoming,
847 "%s %s %s %s %s",
848 mountlist->el[items].device,
849 mountlist->el[items].mountpoint,
850 mountlist->el[items].format,
851 siz, mountlist->el[items].label);
852#elif __FreeBSD__
853 sscanf(incoming,
854 "%s %s %s %s",
855 mountlist->el[items].device,
856 mountlist->el[items].mountpoint,
857 mountlist->el[items].format, siz);
858 strcpy(mountlist->el[items].label, "");
859#endif
860
861 if (!strcmp(mountlist->el[items].device, "/proc") ||
862 !strcmp(mountlist->el[items].device, "proc") ||
863 !strcmp(mountlist->el[items].device, "/sys") ||
864 !strcmp(mountlist->el[items].device, "sys") ||
865 !strcmp(mountlist->el[items].device, "/devpts") ||
866 !strcmp(mountlist->el[items].device, "devpts")
867 ) {
868 log_msg(1,
869 "Ignoring %s in mountlist - not loading that line :) ",
870 mountlist->el[items].device);
871 (void) fgets(incoming, MAX_STR_LEN - 1, fin);
872 continue;
873 }
874 mountlist->el[items].size = atoll(siz);
875 if (mountlist->el[items].device[0] != '\0'
876 && mountlist->el[items].device[0] != '#') {
877 if (items >= ARBITRARY_MAXIMUM) {
878 log_to_screen("Too many lines in mountlist.. ABORTING");
879 finish(1);
880 }
881 for (j = 0;
882 j < items
883 && strcmp(mountlist->el[j].device,
884 mountlist->el[items].device); j++);
885 if (j < items) {
886 strcat(mountlist->el[items].device, "_dup");
887 sprintf(tmp,
888 "Duplicate entry in mountlist - renaming to %s",
889 mountlist->el[items].device);
890 log_it(tmp);
891 }
892 strcpy(tmp, mountlist->el[items].device);
893 if (strstr(tmp, "/dev/md/")) {
894 log_it("format_device() --- Contracting %s", tmp);
895 p = strrchr(tmp, '/');
896 if (p) {
897 *p = *(p + 1);
898 *(p + 1) = *(p + 2);
899 *(p + 2) = *(p + 3);
900 }
901 log_it("It was %s; it is now %s",
902 mountlist->el[items].device, tmp);
903 strcpy(mountlist->el[items].device, tmp);
904 }
905
906 sprintf(tmp,
907 "%s %s %s %lld %s",
908 mountlist->el[items].device,
909 mountlist->el[items].mountpoint,
910 mountlist->el[items].format,
911 mountlist->el[items].size,
912 mountlist->el[items].label);
913
914 log_it(tmp);
915 items++;
916 }
917 (void) fgets(incoming, MAX_STR_LEN - 1, fin);
918 }
919 paranoid_fclose(fin);
920 mountlist->entries = items;
921
922 log_it("Mountlist loaded successfully.");
923 sprintf(tmp, "%d entries in mountlist", items);
924 log_it(tmp);
925 paranoid_free(incoming);
926 paranoid_free(siz);
927 paranoid_free(tmp);
928 return (0);
929}
930
931
932
933/**
934 * Save @p mountlist to a file on disk.
935 * @param mountlist The mountlist to save.
936 * @param fname The file to save it to.
937 * @return 0 for success, 1 for failure.
938 * @see load_mountlist
939 */
940int save_mountlist_to_disk(struct mountlist_itself *mountlist, char *fname)
941{
942 FILE *fout;
943 int i;
944
945 assert(mountlist != NULL);
946 assert_string_is_neither_NULL_nor_zerolength(fname);
947
948 log_it("save_mountlist_to_disk() --- saving to %s", fname);
949 if (!(fout = fopen(fname, "w"))) {
950 log_OS_error("WMTD - Cannot openout mountlist");
951 return (1);
952 }
953 for (i = 0; i < mountlist->entries; i++) {
954 fprintf(fout,
955 "%-15s %-15s %-15s %-15lld %-15s\n",
956 mountlist->el[i].device, mountlist->el[i].mountpoint,
957 mountlist->el[i].format, mountlist->el[i].size,
958 mountlist->el[i].label);
959 }
960 paranoid_fclose(fout);
961 return (0);
962}
963
964
965
966/**
967 * Sort the mountlist alphabetically by device.
968 * The sorting is done in-place.
969 * @param mountlist The mountlist to sort.
970 */
971void sort_mountlist_by_device(struct mountlist_itself *mountlist)
972{
973 int diff;
974 int lino = -999;
975
976 assert(mountlist != NULL);
977
978 while (lino < mountlist->entries) {
979 for (lino = 1; lino < mountlist->entries; lino++) {
980 diff =
981 strcmp_inc_numbers(mountlist->el[lino - 1].device,
982 mountlist->el[lino].device);
983 if (diff > 0) {
984 swap_mountlist_entries(mountlist, lino - 1, lino);
985 break;
986 }
987 }
988 }
989}
990
991/**
992 * Sort the mountlist alphabetically by mountpoint.
993 * The sorting is done in-place.
994 * @param mountlist The mountlist to sort.
995 * @param reverse If TRUE, then do a reverse sort.
996 */
997void
998sort_mountlist_by_mountpoint(struct mountlist_itself *mountlist,
999 bool reverse)
1000{
1001 int diff;
1002 int lino = -999;
1003
1004 assert(mountlist != NULL);
1005
1006 while (lino < mountlist->entries) {
1007 for (lino = 1; lino < mountlist->entries; lino++) {
1008 diff =
1009 strcmp(mountlist->el[lino - 1].mountpoint,
1010 mountlist->el[lino].mountpoint);
1011 if ((diff > 0 && !reverse) || ((diff < 0 && reverse))) {
1012 swap_mountlist_entries(mountlist, lino - 1, lino);
1013 break;
1014 }
1015 }
1016 }
1017}
1018
1019
1020/**
1021 * Swap two entries in the mountlist in-place.
1022 * @param mountlist The mountlist to swap the entries in.
1023 * @param a The index number of the first entry.
1024 * @param b The index number of the second entry.
1025 */
1026void
1027swap_mountlist_entries(struct mountlist_itself *mountlist, int a, int b)
1028{
1029 /*@ mallocs *** */
1030 char device[64];
1031 char mountpoint[256];
1032 char format[64];
1033
1034 long long size;
1035
1036 assert(mountlist != NULL);
1037 assert(a >= 0);
1038 assert(b >= 0);
1039
1040 strcpy(device, mountlist->el[a].device);
1041 strcpy(mountpoint, mountlist->el[a].mountpoint);
1042 strcpy(format, mountlist->el[a].format);
1043
1044 size = mountlist->el[a].size;
1045
1046 strcpy(mountlist->el[a].device, mountlist->el[b].device);
1047 strcpy(mountlist->el[a].mountpoint, mountlist->el[b].mountpoint);
1048 strcpy(mountlist->el[a].format, mountlist->el[b].format);
1049
1050 mountlist->el[a].size = mountlist->el[b].size;
1051
1052 strcpy(mountlist->el[b].device, device);
1053 strcpy(mountlist->el[b].mountpoint, mountpoint);
1054 strcpy(mountlist->el[b].format, format);
1055
1056 mountlist->el[b].size = size;
1057}
1058
1059/* @} - end of mountlistGroup */
Note: See TracBrowser for help on using the repository browser.