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