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