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