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