source: MondoRescue/branches/stable/mondo/src/common/libmondo-mountlist.c@ 1297

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