source: MondoRescue/branches/3.2/mondo/src/common/libmondo-mountlist.c@ 3279

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