source: MondoRescue/trunk/mondo/mondo/common/libmondo-mountlist.c@ 729

Last change on this file since 729 was 688, checked in by bcornec, 18 years ago

Huge memory management patch.
Still not finished but a lot as been done.
What remains is around some functions returning strings, and some structure members.
(Could not finish due to laptop failure !)

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