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