source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-mountlist.c@ 2315

Last change on this file since 2315 was 2306, checked in by Bruno Cornec, 15 years ago
  • popup_and_get_string needs an allocated buffer for the moment so fixing the calls with dyn. ones
  • Fix a bug in mem.c for mr_strcat, which wasn't modifying the right pointer level

Ported from 2.2.9

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