source: MondoRescue/branches/stable/mondo/src/common/libmondo-mountlist.c@ 1304

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