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

Last change on this file since 3263 was 3263, checked in by Bruno Cornec, 10 years ago
  • Fix a bug due to lack of memory allocation for a tmp var to display
  • Fix another bug in the call to labeling which used a bad variable
  • Property svn:keywords set to Id
File size: 30.0 KB
Line 
1/* subroutines for handling mountlist
2 $Id: libmondo-mountlist.c 3263 2014-04-22 09:06:38Z 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 3263 2014-04-22 09:06:38Z 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
308 (mountlist->el[pos].format)) {
309 mr_asprintf(tmp, " %s has unsupported format %s.", device, mountlist->el[pos].format);
310 log_it(tmp);
311 mr_strcat(flaws_str, "%s", tmp);
312 mr_free(tmp);
313 res++;
314 }
315 amount_allocated += mountlist->el[pos].size / 1024L;
316 prev_sp_no = cur_sp_no;
317 }
318 }
319
320 /* OK, continue with main loop */
321 amount_allocated += mountlist->el[pos].size / 1024L;
322
323 /* This should only happen at the end but not tested now */
324 if (amount_allocated > physical_drive_size) {
325 // Used to be +1, but what if you're 1 MB too high?
326 mr_asprintf(tmp, " %ld MB over-allocated on %s.",amount_allocated - physical_drive_size, drive);
327 log_it(tmp);
328
329 /* Do we have more than 1 MB per partition ? */
330 if (amount_allocated - physical_drive_size <= curr_part_no) {
331 /* Probably a rouding error, just passing over
332 * by reducing the last partition if possible */
333 amount_allocated -= mountlist->el[pos].size / 1024L;
334 mountlist->el[pos].size = (mountlist->el[pos].size - curr_part_no*1024L);
335 amount_allocated += mountlist->el[pos].size / 1024L;
336 } else {
337 mr_strcat(flaws_str, "%s", tmp);
338 res++;
339 }
340 mr_free(tmp);
341 }
342 prev_part_no = curr_part_no;
343 }
344
345 /* Over-allocated the disk? Unallocated space on disk? */
346 if (amount_allocated > physical_drive_size) // Used to be +1, but what if you're 1 MB too high?
347 {
348 mr_asprintf(tmp, " %ld MB over-allocated on %s.", amount_allocated - physical_drive_size, drive);
349 log_it(tmp);
350 mr_strcat(flaws_str, "%s", tmp);
351 mr_free(tmp);
352 res++;
353 } else if (amount_allocated < physical_drive_size - 1) { /* NOT AN ERROR, JUST A WARNING :-) */
354 mr_asprintf(tmp, " %ld MB unallocated on %s.", physical_drive_size - amount_allocated, drive);
355 log_it(tmp);
356 mr_strcat(flaws_str, "%s", tmp);
357 mr_free(tmp);
358 }
359 if (res) {
360 return (NULL);
361 } else {
362 return (flaws_str);
363 }
364}
365
366#else
367// Linux-specific version of evaluate_drive_within_mountlist()
368{
369
370 /*@ int ************************************************************* */
371 int prev_part_no = 0;
372 int curr_part_no = 0;
373 int pos = 0;
374 int res = 0;
375 int device_copies = 0;
376 int i = 0;
377
378 /*@ buffers ******************************************************** */
379 char *tmp = NULL;
380 char *device = NULL;
381 char *flaws_str = NULL;
382
383 /*@ long *********************************************************** */
384 long physical_drive_size = 0L;
385 long amount_allocated = 0L;
386
387 /*@ pointers ******************************************************* */
388 char *part_table_fmt = NULL;
389
390 /*@ initialize ***************************************************** */
391 assert_string_is_neither_NULL_nor_zerolength(drive);
392 assert(mountlist != NULL);
393 mr_asprintf(flaws_str, "%s", "");
394
395 malloc_string(device);
396 prev_part_no = 0;
397
398 physical_drive_size = get_phys_size_of_drive(drive);
399
400 if (physical_drive_size < 0) {
401 if (strstr(drive,"/dev/dm-") == NULL) {
402 mr_asprintf(tmp, " %s does not exist.", drive);
403 log_it(tmp);
404 mr_strcat(flaws_str, "%s", tmp);
405 res++;
406 mr_free(tmp);
407 goto endoffunc;
408 } else {
409 log_it(" %s (dm) will be setup later on", drive);
410 }
411 } else {
412 log_it("%s is %ld MB", drive, physical_drive_size);
413 }
414
415 /* GPT allows more than 4 primary partitions - detect partition type now */
416 part_table_fmt = which_partition_format(drive);
417
418 for (curr_part_no = 1; curr_part_no < 99; curr_part_no++) {
419 build_partition_name(device, drive, curr_part_no);
420 pos = find_device_in_mountlist(mountlist, device);
421 if (pos < 0) {
422 continue;
423 }
424 log_msg(2, "Processing partition %s on %s #%d in mountlist", device, drive, pos);
425 /* gap in the partition list? */
426 if (curr_part_no - prev_part_no > 1) {
427 if (prev_part_no == 0) {
428 mr_asprintf(tmp, " Gap prior to %s.", device);
429 log_it(tmp);
430 mr_strcat(flaws_str, "%s", tmp);
431 mr_free(tmp);
432 res++;
433 } else if (curr_part_no > 5 || (curr_part_no <= 4 && prev_part_no > 0)) {
434 mr_asprintf(tmp, " Gap on %s between %d and %d.", drive, prev_part_no, curr_part_no);
435 log_it(tmp);
436 mr_strcat(flaws_str, "%s", tmp);
437 mr_free(tmp);
438 res++;
439 }
440 }
441 /* no spare primary partitions to help accommodate the logical(s)? */
442 if ((curr_part_no >= 5 && prev_part_no == 4) && (strcmp(part_table_fmt, "MBR") == 0)) {
443 mr_asprintf(tmp, " Partition 4 of %s is occupied.", drive);
444 log_it(tmp);
445 mr_strcat(flaws_str, "%s", tmp);
446 mr_free(tmp);
447 res++;
448 }
449
450 /* does partition /dev/hdNX exist more than once in the mountlist? */
451 for (i = 0, device_copies = 0;
452 i < mountlist->entries; i++) {
453 if (!strcmp(device, mountlist->el[i].device)) {
454 device_copies++;
455 }
456 }
457 if (device_copies > 1) {
458 mr_asprintf(tmp, " %s %s's.", number_to_text(device_copies), device);
459 if (!strstr(flaws_str, tmp)) {
460 log_it(tmp);
461 mr_strcat(flaws_str, "%s", tmp);
462 res++;
463 }
464 mr_free(tmp);
465 }
466 /* silly partition size? */
467 if (mountlist->el[pos].size < 8192
468 && strcmp(mountlist->el[pos].mountpoint, "lvm")) {
469 mr_asprintf(tmp, " %s is tiny!", device);
470 log_it(tmp);
471 mr_strcat(flaws_str, "%s", tmp);
472 mr_free(tmp);
473 res++;
474 }
475 /* mountpoint should begin with / unless it is swap, lvm or raid */
476 if (strcmp(mountlist->el[pos].mountpoint, "swap")
477 && strcmp(mountlist->el[pos].mountpoint, "lvm")
478 && strcmp(mountlist->el[pos].mountpoint, "raid")
479 && strcmp(mountlist->el[pos].mountpoint, "image")
480 && mountlist->el[pos].mountpoint[0] != '/') {
481 mr_asprintf(tmp, " %s has a weird mountpoint.", device);
482 log_it(tmp);
483 mr_strcat(flaws_str, "%s", tmp);
484 mr_free(tmp);
485 res++;
486 }
487 /* is format sensible? */
488 if (!is_this_a_valid_disk_format(mountlist->el[pos].format)) {
489 mr_asprintf(tmp, " %s has unsupported format %s.", device, mountlist->el[pos].format);
490 log_it(tmp);
491 mr_strcat(flaws_str, "%s", tmp);
492 mr_free(tmp);
493 res++;
494 }
495 /* OK, continue with main loop */
496 amount_allocated += mountlist->el[pos].size / 1024L;
497 prev_part_no = curr_part_no;
498 }
499 mr_free(part_table_fmt);
500
501 /* Over-allocated the disk? Unallocated space on disk? */
502 if (amount_allocated > physical_drive_size + 1) {
503 mr_asprintf(tmp, " %ld MB over-allocated on %s.", amount_allocated - physical_drive_size, drive);
504 log_it(tmp);
505 mr_strcat(flaws_str, "%s", tmp);
506 mr_free(tmp);
507 res++;
508 } else if (amount_allocated < physical_drive_size - 1) { /* NOT AN ERROR, JUST A WARNING :-) */
509 mr_asprintf(tmp, " %ld MB unallocated on %s.", physical_drive_size - amount_allocated, drive);
510 log_it(tmp);
511 mr_strcat(flaws_str, "%s", tmp);
512 mr_free(tmp);
513 }
514
515 endoffunc:
516 paranoid_free(device);
517
518 if (res == 0) {
519 mr_free(flaws_str);
520 log_msg(2, "Fine, no error in evaluate_drive_within_mountlist");
521 return (NULL);
522 } else {
523 log_msg(2, "Error in evaluate_drive_within_mountlist: %s", flaws_str);
524 return (flaws_str);
525 }
526}
527#endif
528
529
530/**
531 * Evaluate a whole mountlist for flaws. Calls evaluate_drive_within_mountlist()
532 * for each drive, and then spreads the flaws across three lines.
533 * @param mountlist The mountlist to evaluate.
534 * @return The flaws string (NULL for success).
535 * @see evaluate_drive_within_mountlist
536 */
537char *evaluate_mountlist(struct mountlist_itself *mountlist) {
538
539 /*@ buffer *********************************************************** */
540 struct list_of_disks *drivelist;
541 char *tmp = NULL;
542 char *flaws_str = NULL;
543
544 int currline = 0;
545 int copies = 0;
546 int last_copy = 0;
547
548 /*@ buffetr ********************************************************* */
549 char *curr_mountpoint = NULL;
550
551 /*@ int ************************************************************** */
552 int i = 0;
553
554 /*@ initialize ******************************************************* */
555
556 drivelist = malloc(sizeof(struct list_of_disks));
557 assert(mountlist != NULL);
558 mr_asprintf(flaws_str, "%s", "");
559
560 make_list_of_drives_in_mountlist(mountlist, drivelist);
561
562 log_it("Evaluating mountlist...");
563
564 for (i = 0; i < drivelist->entries; i++) {
565 if (strstr(drivelist->el[i].device, DONT_KNOW_HOW_TO_EVALUATE_THIS_DEVICE_TYPE)) {
566 log_it(" Not evaluating %s (I don't know how yet)", drivelist->el[i].device);
567 } else {
568 log_msg(8, "Evaluating drive #%d (%s) within mountlist", i, drivelist->el[i].device);
569 tmp = evaluate_drive_within_mountlist(mountlist, drivelist->el[i].device);
570 }
571 log_msg(8,"Entry: %d (%s)", i, drivelist->el[i].device);
572 /* BCO: tmp can be NULL */
573 if (tmp != NULL) {
574 log_msg(8,"Adding: %s to flaws_str", tmp);
575 mr_strcat(flaws_str, "%s", tmp);
576 mr_free(tmp);
577 }
578 }
579
580 /* Look for duplicate mountpoints in mountlist. */
581 for (currline = 0; currline < mountlist->entries; currline++) {
582 mr_asprintf(curr_mountpoint, "%s", mountlist->el[currline].mountpoint);
583 for (i = 0, copies = 0, last_copy = -1; i < mountlist->entries; i++) {
584 if (!strcmp(mountlist->el[i].mountpoint, curr_mountpoint)
585 && strcmp(mountlist->el[i].mountpoint, "lvm")
586 && strcmp(mountlist->el[i].mountpoint, "swap")) {
587 last_copy = i;
588 copies++;
589 }
590 }
591 if (copies > 1 && last_copy == currline && strcmp(curr_mountpoint, "raid")) {
592 mr_asprintf(tmp, " %s %s's.", number_to_text(copies), curr_mountpoint);
593 mr_strcat(flaws_str, "%s", tmp);
594 log_msg(8,"Adding: %s to flaws_str", tmp);
595 mr_free(tmp);
596 }
597 mr_free(curr_mountpoint);
598 }
599
600 return(flaws_str);
601}
602
603
604/**
605 * Find the index number of @p device in the mountlist.
606 * The device given must match @p mountlist->el[N].device exactly, case-sensitive.
607 * @param mountlist The mountlist to search in.
608 * @param device The device to search for.
609 * @return The zero-based index of the device, or -1 if it could not be found.
610 */
611int
612find_device_in_mountlist(struct mountlist_itself *mountlist, char *device)
613{
614
615 /*@ int ************************************************************** */
616 int i = 0;
617
618 assert(mountlist != NULL);
619 assert_string_is_neither_NULL_nor_zerolength(device);
620 for (i = 0;
621 i < mountlist->entries
622 && strcmp(mountlist->el[i].device, device) != 0; i++);
623
624 if (i == mountlist->entries) {
625 return (-1);
626 } else {
627 return (i);
628 }
629}
630
631
632/**
633 * Make a list of the drives mentioned in the mountlist.
634 * @param mountlist The mountlist to examine.
635 * @param drivelist Where to put the list of drives found.
636 * @return The number of physical (non-RAID non-LVM) drives found, or \<= 0 for error.
637 */
638int
639make_list_of_drives_in_mountlist(struct mountlist_itself *mountlist,
640 struct list_of_disks *drivelist)
641{
642
643 /*@ int ************************************************************* */
644 int lino;
645 int noof_drives;
646 int j;
647
648 /*@ buffers ********************************************************* */
649 char *drive = NULL;
650 char *truncdrive = NULL;
651
652 long long size;
653
654 assert(mountlist != NULL);
655 assert(drivelist != NULL);
656 log_it("Making list of drives");
657 for (lino = 0, noof_drives = 0; lino < mountlist->entries; lino++) {
658
659 mr_asprintf(drive, "%s", mountlist->el[lino].device);
660 if (!strncmp(drive, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB))) {
661 log_msg(8, "Not putting %s in list of drives: it's a virtual drive", drive);
662 mr_free(drive);
663 continue;
664 }
665
666 size = mountlist->el[lino].size;
667 if (size == 0) {
668 log_msg(8, "Not putting %s in list of drives: it has zero size (maybe an LVM volume)", drive);
669 mr_free(drive);
670 continue;
671 }
672
673 log_msg(8, "Putting %s with size %lli in list of drives", drive, size);
674
675 /* memory allocation */
676 truncdrive = truncate_to_drive_name(drive);
677 mr_free(drive);
678
679 log_msg(8, "drive truncated to %s", truncdrive);
680
681 for (j = 0;
682 j < noof_drives
683 && strcmp(drivelist->el[j].device, truncdrive) != 0; j++) {
684 continue;
685 }
686 if (j == noof_drives) {
687 strncpy(drivelist->el[noof_drives].device, truncdrive, 63);
688 drivelist->el[noof_drives].device[63] = '\0';
689 log_msg(8,"Adding drive %s to list", drivelist->el[noof_drives].device);
690 noof_drives++;
691 }
692 paranoid_free(truncdrive);
693 if (noof_drives >= MAXIMUM_DISKS_PER_RAID_DEV) {
694 log_msg(0, "Unable to handle mountlist with more than %d lines", MAXIMUM_DISKS_PER_RAID_DEV);
695 log_to_screen("Unable to handle a so big mountlist");
696 finish(1);
697 }
698 }
699 drivelist->entries = noof_drives;
700 log_msg(8, "Made list of %d drives",noof_drives);
701
702 return (noof_drives);
703}
704
705
706/**
707 * Make a list of RAID partitions not currently associated with any RAID device.
708 * The user may add any of these partitions to the RAID device.
709 * @param output_list Where to put the list of unallocated RAID partitions.
710 * @param mountlist The mountlist to examine.
711 * @param raidlist The raidlist to examine.
712 */
713void make_list_of_unallocated_raid_partitions(struct mountlist_itself
714 *output_list,
715 struct mountlist_itself
716 *mountlist,
717 struct raidlist_itself
718 *raidlist)
719{
720
721 /*@ int ************************************************************* */
722 int items = 0;
723 int i = 0;
724 int used_by = 0;
725
726 assert(output_list != NULL);
727 assert(mountlist != NULL);
728 assert(raidlist != NULL);
729 log_it("MLOURP -- starting");
730 items = 0;
731
732
733 for (i = 0; i < mountlist->entries; i++) {
734 if (strstr(mountlist->el[i].mountpoint, "raid")) {
735 used_by = which_raid_device_is_using_this_partition(raidlist, mountlist->el[i].device);
736 if (used_by < 0) {
737 memcpy((void *) &output_list->el[items++],
738 (void *) &mountlist->el[i],
739 sizeof(struct mountlist_line));
740 log_it("%s is available; user may choose to add it to raid device", output_list->el[items - 1].device);
741 }
742 }
743 }
744 output_list->entries = items;
745 log_it("MLUORP -- ending");
746}
747
748
749/**
750 * Get the size of a mountlist entry by the @c device field.
751 * @param mountlist The mountlist to search in.
752 * @param device The device to search for
753 * @return The size of the device (in KB), or -1 if it could not be found.
754 */
755long long
756size_of_specific_device_in_mountlist(struct mountlist_itself *mountlist,
757 char *device)
758{
759 /*@ int ************************************************************** */
760 int i = 0;
761
762
763 assert(mountlist != NULL);
764 assert_string_is_neither_NULL_nor_zerolength(device);
765
766 for (i = 0;
767 i < mountlist->entries && strcmp(mountlist->el[i].device, device);
768 i++);
769 if (i == mountlist->entries) {
770 return (-1);
771 } else {
772 return (mountlist->el[i].size);
773 }
774}
775
776
777/**
778 * Load a file on disk into @p mountlist.
779 * The file on disk should consist of multiple lines, each containing 4 or 5
780 * columns: the device, the mountpoint, the filesystem type, the size in kilobytes, and optionally the filesystem label.
781 * Comments begin with a '#' without any leading whitespace. Any duplicate
782 * entries are renamed.
783 * @param mountlist The mountlist to load into.
784 * @param fname The name of the file to load the mountlist from.
785 * @return 0 for success, 1 for failure.
786 */
787int load_mountlist(struct mountlist_itself *mountlist, char *fname)
788{
789 FILE *fin = NULL;
790 /* malloc ** */
791 char *incoming = NULL;
792 char *siz = NULL;
793 char *tmp = NULL;
794 char *p = NULL;
795
796 int items = 0;
797 int j = 0;
798 int res = 0;
799
800 assert(mountlist != NULL);
801 assert_string_is_neither_NULL_nor_zerolength(fname);
802
803 if (!(fin = fopen(fname, "r"))) {
804 log_it("Unable to open mountlist - '%s'", fname);
805 log_to_screen("Cannot open mountlist");
806 return (1);
807 }
808 malloc_string(siz);
809 log_it("Loading mountlist...");
810 mr_getline(incoming, fin);
811 while (!feof(fin)) {
812#if linux
813 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);
814 if (res < 5) {
815 /* no label found */
816 log_msg(4, "no label found for %s",mountlist->el[items].device);
817 strcpy(mountlist->el[items].label,"");
818 }
819#elif __FreeBSD__
820 res = sscanf(incoming, "%s %s %s %s", mountlist->el[items].device, mountlist->el[items].mountpoint, mountlist->el[items].format, siz);
821 strcpy(mountlist->el[items].label,"");
822#endif
823
824 if (!strcmp(mountlist->el[items].device, "/proc") ||
825 !strcmp(mountlist->el[items].device, "proc") ||
826 !strcmp(mountlist->el[items].device, "/sys") ||
827 !strcmp(mountlist->el[items].device, "sys") ||
828 !strcmp(mountlist->el[items].device, "/run") ||
829 !strcmp(mountlist->el[items].device, "run") ||
830 !strcmp(mountlist->el[items].device, "/devpts") ||
831 !strcmp(mountlist->el[items].device, "devpts")
832 ) {
833 log_msg(1, "Ignoring %s in mountlist - not loading that line :) ", mountlist->el[items].device);
834 mr_free(incoming);
835 mr_getline(incoming, fin);
836 continue;
837 }
838 mountlist->el[items].size = atoll(siz);
839 if (mountlist->el[items].device[0] != '\0'
840 && mountlist->el[items].device[0] != '#') {
841 for (j = 0;
842 j < items
843 && strcmp(mountlist->el[j].device,
844 mountlist->el[items].device); j++);
845 if (j < items) {
846 strcat(mountlist->el[items].device, "_dup");
847 log_it("Duplicate entry in mountlist - renaming to %s", mountlist->el[items].device);
848 }
849 mr_asprintf(tmp, "%s", mountlist->el[items].device);
850 if (strstr(tmp, "/dev/md/")) {
851 log_it("format_device() --- Contracting %s", tmp);
852 p = strrchr(tmp, '/');
853 if (p) {
854 *p = *(p + 1);
855 *(p + 1) = *(p + 2);
856 *(p + 2) = *(p + 3);
857 }
858 log_it("It was %s; it is now %s", mountlist->el[items].device, tmp);
859 strcpy(mountlist->el[items].device, tmp);
860 }
861 paranoid_free(tmp);
862
863 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);
864 items++;
865 if (items >= MAX_MOUNTLIST_ENTRIES) {
866 log_to_screen("Too many lines in mountlist.. ABORTING");
867 finish(1);
868 }
869 }
870 mr_free(incoming);
871 mr_getline(incoming, fin);
872 }
873 mr_free(incoming);
874 paranoid_fclose(fin);
875 mountlist->entries = items;
876
877 log_it("Mountlist loaded successfully.");
878 log_it("%d entries in mountlist", items);
879
880 paranoid_free(siz);
881 return (0);
882}
883
884
885
886/**
887 * Save @p mountlist to a file on disk.
888 * @param mountlist The mountlist to save.
889 * @param fname The file to save it to.
890 * @return 0 for success, 1 for failure.
891 * @see load_mountlist
892 */
893int save_mountlist_to_disk(struct mountlist_itself *mountlist, char *fname)
894{
895 FILE *fout;
896 int i;
897
898 assert(mountlist != NULL);
899 assert_string_is_neither_NULL_nor_zerolength(fname);
900
901 log_it("save_mountlist_to_disk() --- saving to %s", fname);
902 if (!(fout = fopen(fname, "w"))) {
903 log_OS_error("WMTD - Cannot openout mountlist");
904 return (1);
905 }
906 for (i = 0; i < mountlist->entries; i++) {
907 fprintf(fout,
908 "%-15s %-15s %-15s %-15lld %-15s\n",
909 mountlist->el[i].device, mountlist->el[i].mountpoint,
910 mountlist->el[i].format, mountlist->el[i].size,
911 mountlist->el[i].label);
912 }
913 paranoid_fclose(fout);
914 if (!(fout = fopen(MONDO_MNTLISTCHG, "w"))) {
915 log_OS_error("WMTD - Cannot openout "MONDO_MNTLISTCHG);
916 return (1);
917 }
918 fprintf(fout, "the mountlist was changed by mondorestore\n");
919 paranoid_fclose(fout);
920 return (0);
921}
922
923
924/**
925 * Sort the mountlist alphabetically by device.
926 * The sorting is done in-place.
927 * @param mountlist The mountlist to sort.
928 */
929void sort_mountlist_by_device(struct mountlist_itself *mountlist)
930{
931 int diff;
932 int lino = -999;
933
934 assert(mountlist != NULL);
935
936 while (lino < mountlist->entries) {
937 for (lino = 1; lino < mountlist->entries; lino++) {
938 diff =
939 strcmp_inc_numbers(mountlist->el[lino - 1].device,
940 mountlist->el[lino].device);
941 if (diff > 0) {
942 swap_mountlist_entries(mountlist, lino - 1, lino);
943 break;
944 }
945 }
946 }
947}
948
949
950/**
951 * Sort the mountlist alphabetically by mountpoint.
952 * The sorting is done in-place.
953 * @param mountlist The mountlist to sort.
954 * @param reverse If TRUE, then do a reverse sort.
955 */
956void
957sort_mountlist_by_mountpoint(struct mountlist_itself *mountlist,
958 bool reverse)
959{
960 int diff;
961 int lino = -999;
962
963 assert(mountlist != NULL);
964
965 while (lino < mountlist->entries) {
966 for (lino = 1; lino < mountlist->entries; lino++) {
967 diff =
968 strcmp(mountlist->el[lino - 1].mountpoint,
969 mountlist->el[lino].mountpoint);
970 if ((diff > 0 && !reverse) || ((diff < 0 && reverse))) {
971 swap_mountlist_entries(mountlist, lino - 1, lino);
972 break;
973 }
974 }
975 }
976}
977
978
979/**
980 * Swap two entries in the mountlist in-place.
981 * @param mountlist The mountlist to swap the entries in.
982 * @param a The index number of the first entry.
983 * @param b The index number of the second entry.
984 */
985void
986swap_mountlist_entries(struct mountlist_itself *mountlist, int a, int b)
987{
988 /*@ mallocs *** */
989 char *device = NULL;
990 char *mountpoint = NULL;
991 char *format = NULL;
992
993 long long size;
994
995 assert(mountlist != NULL);
996 assert(a >= 0);
997 assert(b >= 0);
998
999 mr_asprintf(device, "%s", mountlist->el[a].device);
1000 mr_asprintf(mountpoint, "%s", mountlist->el[a].mountpoint);
1001 mr_asprintf(format, "%s", mountlist->el[a].format);
1002
1003 size = mountlist->el[a].size;
1004
1005 strcpy(mountlist->el[a].device, mountlist->el[b].device);
1006 strcpy(mountlist->el[a].mountpoint, mountlist->el[b].mountpoint);
1007 strcpy(mountlist->el[a].format, mountlist->el[b].format);
1008
1009 mountlist->el[a].size = mountlist->el[b].size;
1010
1011 strcpy(mountlist->el[b].device, device);
1012 strcpy(mountlist->el[b].mountpoint, mountpoint);
1013 strcpy(mountlist->el[b].format, format);
1014
1015 mountlist->el[b].size = size;
1016 mr_free(device);
1017 mr_free(mountpoint);
1018 mr_free(format);
1019}
1020
1021/* @} - end of mountlistGroup */
Note: See TracBrowser for help on using the repository browser.