source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-mountlist.c@ 2407

Last change on this file since 2407 was 2395, checked in by Bruno Cornec, 15 years ago
  • Fix interface of evaluate_mountlist (remove 2nd param useless) and fix nuke mode which wasn't working.
  • Tries to add support for bzip2 and lzma initramfs (preliminary, not tested) for 2.6.30

(Backport from 2.2.9)

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