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

Last change on this file since 1178 was 1173, checked in by Bruno Cornec, 17 years ago

merges from trunk for memory management for libmondo-mountlist.c mainly

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