source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-mountlist.c@ 2715

Last change on this file since 2715 was 2696, checked in by Bruno Cornec, 13 years ago
  • Adds preliminary support for hpacucli. Needs test
  • Fix a compilation issue due to bad fatal_error call (no param allowed)
  • Fix some HTML syntax issues in the docs page + link to Wiki articles
  • Boot size pushed to 20MB
  • A bit more precise for error msg from PBDI
  • Exits if the protocol used is not recognized (error with -n fs:// typo)
  • Adds support for r8169 net driver
  • Fix a cast issue
  • Precise a test case to avoid ambiguity
  • Die and related functions placed before any other code as used early, and ash doesn't support function declaration. So will solve #446 by exiting mindi before doing anything weird in it.
  • Fix #445 with another technical solution to avoid vg with similar names to b wrongly excluded (vgroot and vgroot-san e.g.) (Michael Shapiro)
  • Fix a bug on exclusion of path with common content (/home and /path/home e.g.) which was handled correctly only in a certain order (John Pearson <johnp_at_gtagalenco.com.au>)
  • mount-media function is now placed in libmondo-devices.c as used by more programs.
  • Avoids calling mount_media when it's not mandatory (change the way mondorestore was working up to now but could solve some bugs reported)
  • Add xhci support to mindi.
  • Rewrite Xen kernel support to use TryToFindKernelPath systematically.
  • mindi now copies also the /lib/firmware content in order to have it for drivers needing it (bnx2 reported)
  • Push MAX_STR_LEN to 512 to allow supporting more exclude dir (2.2.10 and dyn allocation is the way to go there)
  • Add virtio driver to mindi
  • Initialize extra_cdrom_params in any case tp avoid passing null to cdrecord later on
  • Also escapes white spaces in gen_aux_list to avoid issues in getfacl call
  • Adds multiple missing drivers to mindi (mega_sr, ide_gd_mod, pata_jmicron, cp210x, dca, raid6_pq, xor async_tx, async_memcpy, async_xor)
  • Fix #434 by really testing thet udevd is not running already (chucky)
  • Adds support of pata_sil680 driver
  • Fix again #412 !! by removing calls to tee which voids the return value of the previous command
  • The way grub.unsupported was called for opensuse 11.2 was wrong. It should be done bfore calling grub-install which also exists. And tested for existence. Now this should fix #412.
  • Try to provide a workaround in code to the #401 (over-allocation of space due to rounding errors)
  • Fix a bug when using ACLs and file with spaces in their names by adding double quotes in getfacl invocation (Tom Mortell tomm_at_dslextreme.com). Also adding the same quotes on the touch commands made earlier.
  • Points to the current latest presentation instead of the old one
  • mondo-ref-card added to the docs web page
  • Add the mondoarchive reference card (Lester Wade)
  • Change useless comments for more useful ones
  • mindi is now able to handle compressed kernel with .gz or .bz2 suffix (case of OpenSuSE 11.2)
  • Improves logging for external binary
  • Improve Xen kernel detection and avoid false detection (Michael Shapiro)
  • Updated P2V doc from Lester Wade (lester.wade_at_hp.com)

Baclports from 2.2.9
svn merge -r 2650:2695 svn+ssh://bruno@svn.mondorescue.org/mondo/svn/mondorescue/branches/2.2.9 .

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