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

Last change on this file since 2324 was 2324, checked in by Bruno Cornec, 15 years ago

r3335@localhost: bruno | 2009-08-08 23:04:12 +0200

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