source: MondoRescue/branches/3.2/mondo/src/common/libmondo-mountlist.c@ 3373

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