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

Last change on this file since 1123 was 1123, checked in by Bruno Cornec, 17 years ago

Again linker fixes (hopefully last time for now)

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