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

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

r3202@localhost: bruno | 2009-07-06 18:51:32 +0200

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