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

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