source: MondoRescue/branches/2.2.9/mondo/src/common/libmondo-mountlist.c@ 2665

Last change on this file since 2665 was 2665, checked in by Bruno Cornec, 14 years ago

r3925@localhost: bruno | 2010-06-23 00:13:12 +0200

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