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

Last change on this file since 68 was 59, checked in by bcornec, 19 years ago

Trunk: indent on all source files

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