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