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

Last change on this file since 1548 was 1548, checked in by Bruno Cornec, 17 years ago
  • Add the possibiilty to edit in interactive mode mtab and device.map for grub
  • Remove blkid cache files after restore to avoid problems in cloning mode
  • Fix what seems to appear a huge number of bugs in hack-fstab (illustration of 1 LOC = 1 bug :-)
  • Especially improve LABEL and UUID support.
  • Should fix #185
  • Exclude_path should be 4*MAX_STR_LEN everywhere. Fixed now.
  • Increasing that value will allow to having larger exclude paths.
  • Should solve bug #137 (and maybe #3 as well)
  • Adds support for fedora 7

(merge -r 1533:1547 $SVN_M/branches/2.2.5)

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