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