source: MondoRescue/branches/3.3/mondo/src/mondorestore/mondo-rstr-mountlist.c

Last change on this file was 3879, checked in by Bruno Cornec, 2 months ago

Fix all remaining compiler errors

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