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

Last change on this file since 2323 was 2323, checked in by Bruno Cornec, 15 years ago

r3334@localhost: bruno | 2009-08-08 12:17:37 +0200

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