source: MondoRescue/branches/3.2/mondo/src/mondorestore/mondo-rstr-mountlist.c@ 3564

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