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

Last change on this file since 1548 was 1548, checked in by Bruno Cornec, 17 years ago
  • Add the possibiilty to edit in interactive mode mtab and device.map for grub
  • Remove blkid cache files after restore to avoid problems in cloning mode
  • Fix what seems to appear a huge number of bugs in hack-fstab (illustration of 1 LOC = 1 bug :-)
  • Especially improve LABEL and UUID support.
  • Should fix #185
  • Exclude_path should be 4*MAX_STR_LEN everywhere. Fixed now.
  • Increasing that value will allow to having larger exclude paths.
  • Should solve bug #137 (and maybe #3 as well)
  • Adds support for fedora 7

(merge -r 1533:1547 $SVN_M/branches/2.2.5)

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