source: MondoRescue/branches/2.2.9/mondo/src/common/libmondo-mountlist.c@ 2350

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

Change inerface of evaluate_mountlist and spread_flaws_across_three_lines in order to solve bugs linked to strings management in these functions. May fix a restoration crash seen by some customers

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