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

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