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

Last change on this file since 2816 was 2816, checked in by Bruno Cornec, 13 years ago

svn merge (in svk) -r 2714:2773 2.2.9 in 2.2.10

r4207@localhost (orig r2716): bruno | 2011-02-03 01:29:51 +0100

  • Increase boot size again to support better fedora 13/14


r4208@localhost (orig r2717): bruno | 2011-02-12 15:43:36 +0100

  • Excluding vmhgfs FS for VMWare guests mounting areas of the host.


r4209@localhost (orig r2718): bruno | 2011-02-18 00:30:35 +0100
-Extend EXTRA-SIZE for Debian 6.0 (at least)


r4210@localhost (orig r2719): bruno | 2011-02-18 00:45:40 +0100

  • Fix #462 (removes temp dir if analyze-my-lvm launched in stand alonae mode)


r4213@localhost (orig r2722): bruno | 2011-02-25 18:44:47 +0100

  • Fixes for RHEL6 support at least around dm extensions, bnx2x and cp210x dependencies


r4214@localhost (orig r2723): bruno | 2011-02-25 19:00:17 +0100

  • Adds a restore quiet option


r4215@localhost (orig r2724): bruno | 2011-02-25 19:43:50 +0100

  • Adds support for dm-log used on RHEL6 as well.


r4216@localhost (orig r2725): bruno | 2011-02-25 21:26:54 +0100

  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3


r4217@localhost (orig r2726): bruno | 2011-02-26 11:38:29 +0100

  • Fix error introduced in previous patch in the if/then/else case without member in the then


r4218@localhost (orig r2727): bruno | 2011-02-26 12:05:33 +0100

  • With mindi-busybox 1.18.3 the syntax of openvt changed. This patch keeps a compatible init script for both previous and current version.


r4219@localhost (orig r2728): bruno | 2011-02-26 15:27:40 +0100

  • Latest lvdisplay print MiB instead of MB. Impact on analyze-my-lvm fixed.


r4220@localhost (orig r2729): bruno | 2011-02-26 16:39:32 +0100

  • Adds feature rpc to mindi-busybox in case of need



r4221@localhost (orig r2730): bruno | 2011-02-26 16:42:43 +0100

  • Adds the rebot command to the list of mandatory ones at restore time


r4222@localhost (orig r2731): bruno | 2011-02-26 17:37:13 +0100

  • Adds functions and /bin/loadkeys (new location) to minimal set of commands to have
  • provides a makemessage option to mindi and this function now works with pbdistrocheck
  • Do not create an error when a phys device has 0 size and is a /dev/dm-* device


r4223@localhost (orig r2732): bruno | 2011-02-26 19:07:43 +0100

  • Syntax fix


r4224@localhost (orig r2733): bruno | 2011-02-26 19:51:16 +0100

  • Fix test on dm which was made in wrong sense
  • Adds a driver to allow raid456 to work


r4225@localhost (orig r2734): bruno | 2011-02-26 20:55:00 +0100

  • Fix 2 redirections issues


r4317@localhost (orig r2737): bruno | 2011-03-13 13:12:57 +0100

  • Add support for libahci driver, mandatory for debian 6.


r4318@localhost (orig r2738): bruno | 2011-03-13 14:46:37 +0100

  • Update from Lester Wade (lester.wade_at_hp.com) incorporating latest ML feedbacks


r4319@localhost (orig r2739): bruno | 2011-03-13 17:49:15 +0100

  • Updated Lab for TES 2011


r4323@localhost (orig r2743): bruno | 2011-03-20 01:37:40 +0100

  • Fix a seg fault bug when using the GUI in case exclude_paths was NULL (mc_at_carreira.com.pt)


r4324@localhost (orig r2744): bruno | 2011-03-20 19:38:14 +0100

  • Adds Arch Linux Keyboard support (mc_at_carreira.com.pt)


r4325@localhost (orig r2745): bruno | 2011-03-20 19:48:01 +0100

  • Avoids coredump again when exclude_path is NULL with mr_make_devlist_from_pathlist (feedback from mc_at_carreira.com.pt)


r4326@localhost (orig r2746): bruno | 2011-03-20 19:39:47 +0100

  • Remove blkid from busybox to solve #435


r4327@localhost (orig r2747): bruno | 2011-03-20 19:43:22 +0100

  • Adds reboot to fix #435


r4328@localhost (orig r2748): bruno | 2011-03-22 00:58:07 +0100

  • Fix Arch Linux kbd printed in log file now instead of screen.


r4329@localhost (orig r2749): bruno | 2011-03-22 00:59:58 +0100

  • Again increase size for boot env to support better fedora 14 and other latest distros


r4356@localhost (orig r2750): bruno | 2011-03-25 19:30:01 +0100

  • Improve Arch Linux support for mindi tools (mc_at_carreira.com.pt)


r4357@localhost (orig r2751): bruno | 2011-03-25 19:48:33 +0100

  • Accept to work with either wodim or cdreord and genisoimage or mkisofs


r4358@localhost (orig r2752): bruno | 2011-03-25 20:20:49 +0100

  • Documents option -v of mondoarchive


r4359@localhost (orig r2753): bruno | 2011-03-29 01:34:30 +0200

  • Use modprobe instead of insmod to load loop, cd, ... modules (in order to force them at rstore time). Also drop very old aes support for mdk 9.2


r4360@localhost (orig r2754): bruno | 2011-03-29 01:38:49 +0200

  • Arch Linux support for boot message of mindi (mc_at_carreira.com.pt) using /etc/arch-release and pacman.log


r4383@localhost (orig r2755): bruno | 2011-04-07 09:49:38 +0200

  • Adds a UdevadmTrigger function at restore time to deal with the various syntax formats changes ddepending on version (mc_at_carreira.com.pt)


r4384@localhost (orig r2756): bruno | 2011-04-07 10:05:24 +0200

  • Increase again sizes for Debian 6 support (victor.gattegno_at_hp.com)


r4385@localhost (orig r2757): bruno | 2011-04-07 10:13:00 +0200
Conform to the report of size really !


r4386@localhost (orig r2758): bruno | 2011-04-07 11:31:12 +0200

  • Improved message for Arch Linux for rebuilding the initrd (mc_at_carreira.com.pt)


r4390@localhost (orig r2759): bruno | 2011-04-12 01:15:00 +0200

  • Typo in Web site


r4392@localhost (orig r2761): bruno | 2011-04-14 00:23:38 +0200

  • Adds download references for latest versions.


r4394@localhost (orig r2763): bruno | 2011-04-16 00:35:08 +0200

  • Adds support for MT back mandatory for OBDR.


r4395@localhost (orig r2764): bruno | 2011-04-20 12:18:46 +0200
Function resize_drive_proportionately_to_suit_new_drives

  • Integrate some mr_asprintf
  • Use systematically long long data types in all computation to match the struct mountlist_reference size member and have correct precision
  • Apply JB general_at_itpsg.com patch to make the partition resizing work properly according to the drive size (keeping all calculation in KB and converting only for display)


r4396@localhost (orig r2765): bruno | 2011-04-21 11:20:58 +0200

  • Remove very old code creating confusing symlinks pico to nano in init (patch from MC). Fix #477


r4397@localhost (orig r2766): bruno | 2011-04-21 11:57:44 +0200

  • text's are properly centered in open_and_list_dir (Patch from Chucky). Fix #404.


r4398@localhost (orig r2767): bruno | 2011-04-21 12:49:40 +0200

  • Attempt to fix #432 with a better message at a start.


r4399@localhost (orig r2768): bruno | 2011-04-21 13:00:37 +0200

Log content of mondo-restore.cfg


r4400@localhost (orig r2769): bruno | 2011-04-21 13:48:52 +0200

  • Introduce a new bkpinfo member (subdir) to support storing the subdir under which ISO images are stored on an external HDD. Should fix mostly #433.


r4401@localhost (orig r2770): bruno | 2011-04-21 14:14:52 +0200

  • Fix compilation issues due to level of visibility of functions


r4402@localhost (orig r2771): bruno | 2011-04-22 01:49:03 +0200

  • Adds GUI support for compresion type. Fix half of #468.


r4403@localhost (orig r2772): bruno | 2011-04-22 02:00:07 +0200

  • Adds support of ext attr through the GUI. Fix second part of #468


r4404@localhost (orig r2773): bruno | 2011-04-22 02:22:24 +0200

  • Propose more variables in the mindi.conf example
  • More explicit messages when size unsufficient in mindi for kernel + initrd as per #471
  • Better loging of labeling in mondo when in nuke mode


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