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