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

Last change on this file since 1904 was 1904, checked in by Bruno Cornec, 16 years ago

merge -r 1889:1902 svn+ssh://bruno@svn.mondorescue.org/mondo/svn/mondorescue/branches/2.2.6

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