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