source: MondoRescue/branches/3.0/mondo/src/common/libmondo-mountlist.c@ 3188

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