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

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

Fix interface of evaluate_mountlist (remove 2nd param useless) and fix nuke mode which wasn't working.

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