source: MondoRescue/branches/3.1/mondo/src/common/libmondo-mountlist.c@ 3147

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