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

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

-Preparation to integrate some stable functions

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