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