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