source: MondoRescue/branches/stable/mondo/mondo/common/libmondo-mountlist.c@ 501

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