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