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

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

r3341@localhost: bruno | 2009-08-13 22:36:24 +0200

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