[1] | 1 | /* libmondo-raid.c subroutines for handling RAID
|
---|
[128] | 2 | $Id: libmondo-raid.c 3871 2024-03-07 17:06:33Z bruno $
|
---|
[1] | 3 | */
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 | /**
|
---|
| 7 | * @file
|
---|
| 8 | * Functions for handling RAID (especially during restore).
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 | #include "my-stuff.h"
|
---|
| 12 | #include "mondostructures.h"
|
---|
[541] | 13 | #include "libmondo-gui-EXT.h"
|
---|
[1] | 14 | #include "libmondo-files-EXT.h"
|
---|
| 15 | #include "libmondo-tools-EXT.h"
|
---|
| 16 | #include "libmondo-string-EXT.h"
|
---|
[3043] | 17 | #include "libmondo-fork-EXT.h"
|
---|
[3871] | 18 |
|
---|
[2211] | 19 | #include "mr_mem.h"
|
---|
[2241] | 20 | #include "mr_str.h"
|
---|
[1] | 21 |
|
---|
| 22 | #ifdef __FreeBSD__
|
---|
| 23 | /* Nonstandard library functions: */
|
---|
[128] | 24 | extern void errx(int exitval, const char *fmt, ...);
|
---|
| 25 | extern char *strsep(char **stringp, const char *delim);
|
---|
[1] | 26 | #endif
|
---|
| 27 |
|
---|
| 28 | /*@unused@*/
|
---|
[128] | 29 | //static char cvsid[] = "$Id: libmondo-raid.c 3871 2024-03-07 17:06:33Z bruno $";
|
---|
[1] | 30 |
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * @addtogroup raidGroup
|
---|
| 34 | * @{
|
---|
| 35 | */
|
---|
| 36 | /**
|
---|
| 37 | * See if a particular RAID level is supported by the kernel.
|
---|
| 38 | * @param raidno The RAID level (-1 through 5) to check. -1 means "linear" under Linux and
|
---|
| 39 | * "concatenated" under FreeBSD. It's really the same thing, just different wording.
|
---|
| 40 | * @return TRUE if it's supported, FALSE if not.
|
---|
| 41 | */
|
---|
[128] | 42 | bool is_this_raid_personality_registered(int raidno)
|
---|
[1] | 43 | {
|
---|
| 44 | #ifdef __FreeBSD__
|
---|
[128] | 45 | return ((raidno == -1) || (raidno == 0) || (raidno == 1)
|
---|
| 46 | || (raidno == 5)) ? TRUE : FALSE;
|
---|
[1] | 47 | #else
|
---|
[128] | 48 | /*@ buffer ********************************************************** */
|
---|
[2211] | 49 | char *command = NULL;
|
---|
[128] | 50 | int res;
|
---|
| 51 |
|
---|
[3185] | 52 | mr_asprintf(command, "grep \"");
|
---|
[128] | 53 | if (raidno == -1) {
|
---|
[2211] | 54 | mr_strcat(command, "linear");
|
---|
[128] | 55 | } else {
|
---|
[2211] | 56 | mr_strcat(command, "raid%d", raidno);
|
---|
[128] | 57 | }
|
---|
[2211] | 58 | mr_strcat(command, "\" /proc/mdstat > /dev/null 2> /dev/null");
|
---|
[128] | 59 | log_it("Is raid %d registered? Command = '%s'", raidno, command);
|
---|
| 60 | res = system(command);
|
---|
| 61 | paranoid_free(command);
|
---|
[2211] | 62 |
|
---|
[128] | 63 | if (res) {
|
---|
| 64 | return (FALSE);
|
---|
| 65 | } else {
|
---|
| 66 | return (TRUE);
|
---|
| 67 | }
|
---|
[1] | 68 | #endif
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * Search for @p device in @p disklist.
|
---|
| 78 | * @param disklist The disklist to search in.
|
---|
| 79 | * @param device The device to search for.
|
---|
| 80 | * @return The index number of @p device, or -1 if it does not exist.
|
---|
| 81 | */
|
---|
| 82 | int
|
---|
[128] | 83 | where_in_drivelist_is_drive(struct list_of_disks *disklist, char *device)
|
---|
[1] | 84 | {
|
---|
| 85 |
|
---|
[128] | 86 | /*@ int ************************************************************* */
|
---|
| 87 | int i = 0;
|
---|
[1] | 88 |
|
---|
[128] | 89 | assert(disklist != NULL);
|
---|
| 90 | assert_string_is_neither_NULL_nor_zerolength(device);
|
---|
[1] | 91 |
|
---|
[128] | 92 | for (i = 0; i < disklist->entries; i++) {
|
---|
| 93 | if (!strcmp(disklist->el[i].device, device)) {
|
---|
| 94 | break;
|
---|
| 95 | }
|
---|
[1] | 96 | }
|
---|
[128] | 97 | if (i == disklist->entries) {
|
---|
| 98 | return (-1);
|
---|
| 99 | } else {
|
---|
| 100 | return (i);
|
---|
| 101 | }
|
---|
[1] | 102 | }
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
| 112 | * Determine which RAID device is using a particular partition.
|
---|
| 113 | * @param raidlist The RAID information structure.
|
---|
| 114 | * @param device The partition to find out about.
|
---|
| 115 | * @return The index number of the RAID device using @p device, or -1 if there is none.
|
---|
| 116 | */
|
---|
| 117 | int
|
---|
[128] | 118 | which_raid_device_is_using_this_partition(struct raidlist_itself *raidlist,
|
---|
| 119 | char *device)
|
---|
[1] | 120 | {
|
---|
| 121 | #ifdef __FreeBSD__
|
---|
| 122 | // FreeBSD-specific version of which_raid_device_is_using_this_partition()
|
---|
[128] | 123 | /*@ int ********************************************************* */
|
---|
| 124 | int i = 0;
|
---|
[1] | 125 |
|
---|
[128] | 126 | for (i = 0; i < raidlist->entries; i++) {
|
---|
| 127 | bool thisone = FALSE;
|
---|
| 128 | int j, k, l;
|
---|
| 129 |
|
---|
| 130 | for (j = 0; j < raidlist->el[i].plexes; ++j) {
|
---|
| 131 | for (k = 0; k < raidlist->el[i].plex[j].subdisks; ++k) {
|
---|
| 132 | for (l = 0; l < raidlist->disks.entries; ++l) {
|
---|
| 133 | if (!strcmp(raidlist->disks.el[l].device,
|
---|
| 134 | device) &&
|
---|
| 135 | !strcmp(raidlist->el[i].plex[j].sd[k].which_device,
|
---|
| 136 | raidlist->disks.el[l].name))
|
---|
| 137 | thisone = TRUE;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
[1] | 140 | }
|
---|
[128] | 141 |
|
---|
| 142 | if (thisone) {
|
---|
| 143 | break;
|
---|
| 144 | }
|
---|
[1] | 145 | }
|
---|
[128] | 146 | if (i == raidlist->entries) {
|
---|
| 147 | return (-1);
|
---|
| 148 | } else {
|
---|
| 149 | return (i);
|
---|
[1] | 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | #else
|
---|
| 154 | // Linux-specific version of which_raid_device_is_using_this_partition()
|
---|
| 155 | // and one other function which FreeBSD doesn't use
|
---|
| 156 |
|
---|
[128] | 157 | int current_raiddev = 0;
|
---|
[1] | 158 |
|
---|
[128] | 159 | assert_string_is_neither_NULL_nor_zerolength(device);
|
---|
| 160 | assert(raidlist != NULL);
|
---|
[1] | 161 |
|
---|
[128] | 162 | for (current_raiddev = 0; current_raiddev < raidlist->entries;
|
---|
| 163 | current_raiddev++) {
|
---|
| 164 | if (where_in_drivelist_is_drive
|
---|
| 165 | (&raidlist->el[current_raiddev].data_disks, device) >= 0
|
---|
| 166 | || where_in_drivelist_is_drive(&raidlist->el[current_raiddev].
|
---|
| 167 | spare_disks, device) >= 0
|
---|
| 168 | || where_in_drivelist_is_drive(&raidlist->el[current_raiddev].
|
---|
| 169 | parity_disks, device) >= 0
|
---|
| 170 | || where_in_drivelist_is_drive(&raidlist->el[current_raiddev].
|
---|
| 171 | failed_disks, device) >= 0) {
|
---|
| 172 | break;
|
---|
| 173 | }
|
---|
[1] | 174 | }
|
---|
[128] | 175 | if (current_raiddev == raidlist->entries) {
|
---|
| 176 | return (-1);
|
---|
| 177 | } else {
|
---|
| 178 | return (current_raiddev);
|
---|
| 179 | }
|
---|
[1] | 180 | }
|
---|
| 181 |
|
---|
| 182 | /**
|
---|
| 183 | * Write an @c int variable to a list of RAID variables.
|
---|
| 184 | * @param raidrec The RAID device record to write to.
|
---|
| 185 | * @param lino The variable index number to modify/create.
|
---|
| 186 | * @param label The label to write.
|
---|
| 187 | * @param value The value to write.
|
---|
| 188 | */
|
---|
| 189 | void
|
---|
[128] | 190 | write_variableINT_to_raid_var_line(struct raid_device_record *raidrec,
|
---|
| 191 | int lino, char *label, int value)
|
---|
[1] | 192 | {
|
---|
[128] | 193 | /*@ buffers ***************************************************** */
|
---|
[2515] | 194 | char *sz_value = NULL;
|
---|
[1] | 195 |
|
---|
[128] | 196 | assert(raidrec != NULL);
|
---|
| 197 | assert(label != NULL);
|
---|
[1] | 198 |
|
---|
[3185] | 199 | mr_asprintf(sz_value, "%d", value);
|
---|
[128] | 200 | strcpy(raidrec->additional_vars.el[lino].label, label);
|
---|
| 201 | strcpy(raidrec->additional_vars.el[lino].value, sz_value);
|
---|
[2515] | 202 | mr_free(sz_value);
|
---|
[1] | 203 | }
|
---|
| 204 | #endif
|
---|
| 205 |
|
---|
| 206 |
|
---|
| 207 |
|
---|
| 208 |
|
---|
| 209 |
|
---|
| 210 |
|
---|
| 211 |
|
---|
| 212 |
|
---|
| 213 | #ifdef __FreeBSD__
|
---|
| 214 | /**
|
---|
| 215 | * Add a disk to a RAID plex.
|
---|
| 216 | * @param p The plex to add the device to.
|
---|
| 217 | * @param device_to_add The device to add to @p p.
|
---|
| 218 | */
|
---|
[128] | 219 | void add_disk_to_raid_device(struct vinum_plex *p, char *device_to_add)
|
---|
[1] | 220 | {
|
---|
[128] | 221 | strcpy(p->sd[p->subdisks].which_device, device_to_add);
|
---|
| 222 | ++p->subdisks;
|
---|
[1] | 223 |
|
---|
| 224 | }
|
---|
| 225 | #else
|
---|
| 226 | /**
|
---|
| 227 | * Add a disk to a RAID device.
|
---|
| 228 | * @param disklist The disklist to add the device to.
|
---|
| 229 | * @param device_to_add The device to add to @p disklist.
|
---|
| 230 | * @param index The index number of the disklist entry we're creating.
|
---|
| 231 | */
|
---|
[128] | 232 | void add_disk_to_raid_device(struct list_of_disks *disklist,
|
---|
| 233 | char *device_to_add, int index)
|
---|
[1] | 234 | {
|
---|
[128] | 235 | int items;
|
---|
[1] | 236 |
|
---|
[128] | 237 | assert(disklist != NULL);
|
---|
| 238 | assert_string_is_neither_NULL_nor_zerolength(device_to_add);
|
---|
| 239 | items = disklist->entries;
|
---|
| 240 | strcpy(disklist->el[items].device, device_to_add);
|
---|
| 241 | disklist->el[items].index = index;
|
---|
| 242 | items++;
|
---|
| 243 | disklist->entries = items;
|
---|
[3566] | 244 | log_msg(3, "Adding device %s (%d)", device_to_add, index);
|
---|
[1] | 245 | }
|
---|
| 246 | #endif
|
---|
| 247 |
|
---|
| 248 |
|
---|
| 249 | /**
|
---|
| 250 | * Save the additional RAID variables to a stream.
|
---|
| 251 | * @param vars The RAID variable list to save.
|
---|
| 252 | * @param fout The FILE pointer to save them to.
|
---|
| 253 | */
|
---|
[128] | 254 | void
|
---|
| 255 | save_additional_vars_to_file(struct additional_raid_variables *vars,
|
---|
| 256 | FILE * fout)
|
---|
[1] | 257 | {
|
---|
[128] | 258 | int i;
|
---|
[1] | 259 |
|
---|
[128] | 260 | assert(vars != NULL);
|
---|
| 261 | assert(fout != NULL);
|
---|
[1] | 262 |
|
---|
[128] | 263 | for (i = 0; i < vars->entries; i++) {
|
---|
| 264 | fprintf(fout, " %-21s %s\n", vars->el[i].label,
|
---|
| 265 | vars->el[i].value);
|
---|
| 266 | }
|
---|
[1] | 267 | }
|
---|
| 268 |
|
---|
| 269 |
|
---|
| 270 | /**
|
---|
| 271 | * Save a raidlist structure to disk in raidtab format.
|
---|
| 272 | * @param raidlist The raidlist to save.
|
---|
| 273 | * @param fname The file to save it to.
|
---|
| 274 | * @return 0, always.
|
---|
| 275 | * @bug Return value is redundant.
|
---|
| 276 | */
|
---|
[128] | 277 | int save_raidlist_to_raidtab(struct raidlist_itself *raidlist, char *fname)
|
---|
[1] | 278 | {
|
---|
[128] | 279 | FILE *fout;
|
---|
| 280 | int current_raid_device;
|
---|
[1] | 281 | #ifdef __FreeBSD__
|
---|
[128] | 282 | int i;
|
---|
[1] | 283 | #else
|
---|
| 284 | // Linux
|
---|
| 285 | #endif
|
---|
| 286 |
|
---|
[128] | 287 | assert(raidlist != NULL);
|
---|
| 288 | assert_string_is_neither_NULL_nor_zerolength(fname);
|
---|
[1] | 289 |
|
---|
[128] | 290 | if (raidlist->entries <= 0) {
|
---|
| 291 | unlink(fname);
|
---|
| 292 | log_it("Deleting raidtab (no RAID devs anyway)");
|
---|
| 293 | return (0);
|
---|
| 294 | }
|
---|
| 295 | if (!(fout = fopen(fname, "w"))) {
|
---|
| 296 | log_OS_error("Failed to save raidlist");
|
---|
| 297 | return (1);
|
---|
| 298 | }
|
---|
| 299 | fprintf(fout, "# Generated by Mondo Rescue\n");
|
---|
[1] | 300 |
|
---|
| 301 | #ifdef __FreeBSD__
|
---|
[128] | 302 | for (i = 0; i < raidlist->disks.entries; ++i) {
|
---|
| 303 | fprintf(fout, "drive %s device %s\n", raidlist->disks.el[i].name,
|
---|
| 304 | raidlist->disks.el[i].device);
|
---|
| 305 | }
|
---|
| 306 | for (i = 0; i < (raidlist->spares.entries); ++i) {
|
---|
| 307 | fprintf(fout, "drive %s device %s hotspare\n",
|
---|
| 308 | raidlist->spares.el[i].name,
|
---|
| 309 | raidlist->spares.el[i].device);
|
---|
| 310 | }
|
---|
[1] | 311 | #endif
|
---|
| 312 |
|
---|
[128] | 313 | for (current_raid_device = 0; current_raid_device < raidlist->entries;
|
---|
| 314 | current_raid_device++) {
|
---|
| 315 | save_raidrec_to_file(&raidlist->el[current_raid_device], fout);
|
---|
| 316 | }
|
---|
| 317 | paranoid_fclose(fout);
|
---|
| 318 | return (0);
|
---|
[1] | 319 | }
|
---|
| 320 |
|
---|
| 321 |
|
---|
| 322 | /**
|
---|
[3855] | 323 | * Save a disklist to a stream in raidtab format.
|
---|
| 324 | * @param listname One of "raid-disk", "spare-disk", "parity-disk", or "failed-disk".
|
---|
| 325 | * @param disklist The disklist to save to @p fout.
|
---|
| 326 | * @param fout The stream to write to.
|
---|
| 327 | */
|
---|
| 328 | void save_disklist_to_file(const char *listname, struct list_of_disks *disklist, FILE * fout)
|
---|
| 329 | {
|
---|
| 330 | int i;
|
---|
| 331 |
|
---|
| 332 | assert_string_is_neither_NULL_nor_zerolength(listname);
|
---|
| 333 | assert(disklist != NULL);
|
---|
| 334 | assert(fout != NULL);
|
---|
| 335 |
|
---|
| 336 | for (i = 0; i < disklist->entries; i++) {
|
---|
| 337 | fprintf(fout, " device %s\n", disklist->el[i].device);
|
---|
| 338 | fprintf(fout, " %-21s %d\n", listname, disklist->el[i].index);
|
---|
| 339 | }
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | /**
|
---|
[1] | 343 | * Save an individual RAID device record to a stream.
|
---|
| 344 | * @param raidrec The RAID device record to save.
|
---|
| 345 | * @param fout The stream to save it to.
|
---|
| 346 | */
|
---|
[128] | 347 | void save_raidrec_to_file(struct
|
---|
[1] | 348 | #ifdef __FreeBSD__
|
---|
[128] | 349 | vinum_volume
|
---|
[1] | 350 | #else
|
---|
[128] | 351 | raid_device_record
|
---|
[1] | 352 | #endif
|
---|
[128] | 353 | * raidrec, FILE * fout)
|
---|
[1] | 354 | {
|
---|
| 355 | #ifdef __FreeBSD__
|
---|
[128] | 356 | int i, j;
|
---|
[3191] | 357 | char *org = NULL;
|
---|
[1] | 358 |
|
---|
[128] | 359 | fprintf(fout, "\nvolume %s\n", raidrec->volname);
|
---|
| 360 | for (i = 0; i < raidrec->plexes; ++i) {
|
---|
| 361 | switch (raidrec->plex[i].raidlevel) {
|
---|
| 362 | case -1:
|
---|
[3191] | 363 | mr_asprintf(org, "concat");
|
---|
[128] | 364 | break;
|
---|
| 365 | case 0:
|
---|
[3191] | 366 | mr_asprintf(org, "striped");
|
---|
[128] | 367 | break;
|
---|
| 368 | case 5:
|
---|
[3191] | 369 | mr_asprintf(org, "raid5");
|
---|
[128] | 370 | break;
|
---|
| 371 | }
|
---|
| 372 | fprintf(fout, " plex org %s", org);
|
---|
[3191] | 373 | mr_free(org);
|
---|
| 374 |
|
---|
[128] | 375 | if (raidrec->plex[i].raidlevel != -1) {
|
---|
| 376 | fprintf(fout, " %ik", raidrec->plex[i].stripesize);
|
---|
| 377 | }
|
---|
| 378 | fprintf(fout, "\n");
|
---|
| 379 |
|
---|
| 380 | for (j = 0; j < raidrec->plex[i].subdisks; ++j) {
|
---|
[3191] | 381 | fprintf(fout, " sd drive %s size 0\n", raidrec->plex[i].sd[j].which_device);
|
---|
[128] | 382 | }
|
---|
[1] | 383 | }
|
---|
[128] | 384 | #else
|
---|
| 385 | assert(raidrec != NULL);
|
---|
| 386 | assert(fout != NULL);
|
---|
| 387 |
|
---|
| 388 | fprintf(fout, "raiddev %s\n", raidrec->raid_device);
|
---|
[558] | 389 | if (raidrec->raid_level == -2) {
|
---|
| 390 | fprintf(fout, " raid-level multipath\n");
|
---|
| 391 | } else if (raidrec->raid_level == -1) {
|
---|
[128] | 392 | fprintf(fout, " raid-level linear\n");
|
---|
| 393 | } else {
|
---|
[3566] | 394 | fprintf(fout, " raid-level %d\n", raidrec->raid_level);
|
---|
[1] | 395 | }
|
---|
[3566] | 396 | fprintf(fout, " nr-raid-disks %d\n", raidrec->data_disks.entries);
|
---|
[558] | 397 | if (raidrec->spare_disks.entries > 0) {
|
---|
[3566] | 398 | fprintf(fout, " nr-spare-disks %d\n", raidrec->spare_disks.entries);
|
---|
[558] | 399 | }
|
---|
[128] | 400 | if (raidrec->parity_disks.entries > 0) {
|
---|
[3566] | 401 | fprintf(fout, " nr-parity-disks %d\n", raidrec->parity_disks.entries);
|
---|
[1] | 402 | }
|
---|
[3566] | 403 | fprintf(fout, " persistent-superblock %d\n", raidrec->persistent_superblock);
|
---|
[558] | 404 | if (raidrec->chunk_size > -1) {
|
---|
| 405 | fprintf(fout, " chunk-size %d\n", raidrec->chunk_size);
|
---|
| 406 | }
|
---|
| 407 | if (raidrec->parity > -1) {
|
---|
| 408 | switch(raidrec->parity) {
|
---|
| 409 | case 0:
|
---|
| 410 | fprintf(fout, " parity-algorithm left-asymmetric\n");
|
---|
| 411 | break;
|
---|
| 412 | case 1:
|
---|
| 413 | fprintf(fout, " parity-algorithm right-asymmetric\n");
|
---|
| 414 | break;
|
---|
| 415 | case 2:
|
---|
| 416 | fprintf(fout, " parity-algorithm left-symmetric\n");
|
---|
| 417 | break;
|
---|
| 418 | case 3:
|
---|
| 419 | fprintf(fout, " parity-algorithm right-symmetric\n");
|
---|
| 420 | break;
|
---|
| 421 | default:
|
---|
| 422 | fatal_error("Unknown RAID parity algorithm.");
|
---|
| 423 | break;
|
---|
| 424 | }
|
---|
| 425 | }
|
---|
[128] | 426 | save_additional_vars_to_file(&raidrec->additional_vars, fout);
|
---|
| 427 | fprintf(fout, "\n");
|
---|
| 428 | save_disklist_to_file("raid-disk", &raidrec->data_disks, fout);
|
---|
| 429 | save_disklist_to_file("spare-disk", &raidrec->spare_disks, fout);
|
---|
| 430 | save_disklist_to_file("parity-disk", &raidrec->parity_disks, fout);
|
---|
| 431 | save_disklist_to_file("failed-disk", &raidrec->failed_disks, fout);
|
---|
| 432 | fprintf(fout, "\n");
|
---|
[1] | 433 | #endif
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | /**
|
---|
| 437 | * Retrieve the next line from a raidtab stream.
|
---|
| 438 | * @param fin The file to read the input from.
|
---|
| 439 | * @param label Where to put the line's label.
|
---|
| 440 | * @param value Where to put the line's value.
|
---|
| 441 | * @return 0 if the line was read and stored successfully, 1 if we're at end of file.
|
---|
| 442 | */
|
---|
[3566] | 443 | static int get_next_raidtab_line(FILE * fin, char **label, char **value)
|
---|
[1] | 444 | {
|
---|
[3191] | 445 | char *incoming = NULL;
|
---|
[3566] | 446 | char *p = NULL;
|
---|
[3614] | 447 | char *res = NULL;
|
---|
[1] | 448 |
|
---|
[128] | 449 | assert(fin != NULL);
|
---|
[1] | 450 |
|
---|
[128] | 451 | if (feof(fin)) {
|
---|
| 452 | return (1);
|
---|
[1] | 453 | }
|
---|
[3566] | 454 |
|
---|
[3614] | 455 | for (mr_getline(res, fin); !feof(fin); mr_getline(res, fin)) {
|
---|
| 456 | log_msg(10,"Found in raidtab line: %s",res);
|
---|
| 457 | incoming = mr_strip_spaces(res);
|
---|
| 458 | mr_free(res);
|
---|
[128] | 459 | p = strchr(incoming, ' ');
|
---|
| 460 | if (strlen(incoming) < 3 || incoming[0] == '#' || !p) {
|
---|
[3191] | 461 | mr_free(incoming);
|
---|
[128] | 462 | continue;
|
---|
| 463 | }
|
---|
| 464 | *(p++) = '\0';
|
---|
| 465 | while (*p == ' ') {
|
---|
| 466 | p++;
|
---|
| 467 | }
|
---|
[3566] | 468 | *label = incoming;
|
---|
| 469 | *value = p;
|
---|
| 470 | log_msg(10,"Found in raidtab pair: %s|%s",*label,*value);
|
---|
[128] | 471 | return (0);
|
---|
| 472 | }
|
---|
| 473 | return (1);
|
---|
[1] | 474 | }
|
---|
| 475 |
|
---|
| 476 |
|
---|
| 477 |
|
---|
| 478 | /**
|
---|
| 479 | * Load a raidtab file into a raidlist structure.
|
---|
| 480 | * @param raidlist The raidlist to fill.
|
---|
| 481 | * @param fname The file to read from.
|
---|
| 482 | * @return 0 for success, 1 for failure.
|
---|
| 483 | */
|
---|
| 484 | #ifdef __FreeBSD__
|
---|
[3566] | 485 | int load_raidtab_into_raidlist(struct raidlist_itself *raidlist, char *fname)
|
---|
[1] | 486 | {
|
---|
[128] | 487 | FILE *fin;
|
---|
| 488 | int items;
|
---|
[1] | 489 |
|
---|
[128] | 490 | raidlist->spares.entries = 0;
|
---|
| 491 | raidlist->disks.entries = 0;
|
---|
| 492 | if (length_of_file(fname) < 5) {
|
---|
| 493 | log_it("Raidtab is very small or non-existent. Ignoring it.");
|
---|
| 494 | raidlist->entries = 0;
|
---|
| 495 | return (0);
|
---|
[1] | 496 | }
|
---|
[128] | 497 | if (!(fin = fopen(fname, "r"))) {
|
---|
| 498 | log_it("Cannot open raidtab");
|
---|
| 499 | return (1);
|
---|
[1] | 500 | }
|
---|
[128] | 501 | items = 0;
|
---|
| 502 | log_it("Loading raidtab...");
|
---|
| 503 | while (!feof(fin)) {
|
---|
| 504 | int argc;
|
---|
| 505 | char **argv = get_next_vinum_conf_line(fin, &argc);
|
---|
| 506 | if (!argv)
|
---|
| 507 | break;
|
---|
| 508 | if (!strcmp(argv[0], "drive")) {
|
---|
| 509 | char *drivename, *devname;
|
---|
| 510 | if (argc < 4)
|
---|
| 511 | continue;
|
---|
| 512 | drivename = argv[1];
|
---|
| 513 | devname = get_option_val(argc, argv, "device");
|
---|
| 514 | if (!devname)
|
---|
| 515 | continue;
|
---|
[1] | 516 |
|
---|
[128] | 517 | if (get_option_state(argc, argv, "hotspare")) {
|
---|
[3191] | 518 | strcpy(raidlist->spares.el[raidlist->spares.entries].name, drivename);
|
---|
[3566] | 519 | strcpy(raidlist->spares.el[raidlist->spares.entries].device, devname);
|
---|
| 520 | raidlist->spares.el[raidlist->spares.entries].index = raidlist->disks.entries;
|
---|
[128] | 521 | raidlist->spares.entries++;
|
---|
| 522 | } else {
|
---|
[3191] | 523 | strcpy(raidlist->disks.el[raidlist->disks.entries].name, drivename);
|
---|
| 524 | strcpy(raidlist->disks.el[raidlist->disks.entries].device, devname);
|
---|
[3566] | 525 | raidlist->disks.el[raidlist->disks.entries].index = raidlist->disks.entries;
|
---|
[128] | 526 | raidlist->disks.entries++;
|
---|
| 527 | }
|
---|
| 528 | } else if (!strcmp(argv[0], "volume")) {
|
---|
| 529 | char *volname;
|
---|
| 530 | if (argc < 2)
|
---|
| 531 | continue;
|
---|
| 532 | volname = argv[1];
|
---|
| 533 | strcpy(raidlist->el[raidlist->entries].volname, volname);
|
---|
| 534 | raidlist->el[raidlist->entries].plexes = 0;
|
---|
| 535 | raidlist->entries++;
|
---|
| 536 | } else if (!strcmp(argv[0], "plex")) {
|
---|
| 537 | int raidlevel, stripesize;
|
---|
| 538 | char *org = 0;
|
---|
| 539 | char **tmp = 0;
|
---|
| 540 | if (argc < 3)
|
---|
| 541 | continue;
|
---|
| 542 | org = get_option_val(argc, argv, "org");
|
---|
| 543 | if (!org)
|
---|
| 544 | continue;
|
---|
| 545 | if (strcmp(org, "concat")) {
|
---|
| 546 | tmp = get_option_vals(argc, argv, "org", 2);
|
---|
| 547 | if (tmp && tmp[1]) {
|
---|
| 548 | stripesize = (int) (size_spec(tmp[1]) / 1024);
|
---|
| 549 | } else
|
---|
| 550 | stripesize = 279;
|
---|
| 551 | } else
|
---|
| 552 | stripesize = 0;
|
---|
[1] | 553 |
|
---|
[128] | 554 | if (!strcmp(org, "concat")) {
|
---|
| 555 | raidlevel = -1;
|
---|
| 556 | } else if (!strcmp(org, "striped")) {
|
---|
| 557 | raidlevel = 0;
|
---|
| 558 | } else if (!strcmp(org, "raid5")) {
|
---|
| 559 | raidlevel = 5;
|
---|
| 560 | } else
|
---|
| 561 | continue;
|
---|
| 562 |
|
---|
| 563 | raidlist->el[raidlist->entries - 1].plex
|
---|
| 564 | [raidlist->el[raidlist->entries - 1].plexes].raidlevel =
|
---|
| 565 | raidlevel;
|
---|
| 566 | raidlist->el[raidlist->entries -
|
---|
| 567 | 1].plex[raidlist->el[raidlist->entries -
|
---|
| 568 | 1].plexes].stripesize =
|
---|
| 569 | stripesize;
|
---|
| 570 | raidlist->el[raidlist->entries -
|
---|
| 571 | 1].plex[raidlist->el[raidlist->entries -
|
---|
| 572 | 1].plexes].subdisks = 0;
|
---|
| 573 | raidlist->el[raidlist->entries - 1].plexes++;
|
---|
| 574 | } else if ((!strcmp(argv[0], "sd"))
|
---|
| 575 | || (!strcmp(argv[0], "subdisk"))) {
|
---|
| 576 | char *drive = 0;
|
---|
| 577 | if (argc < 3)
|
---|
| 578 | continue;
|
---|
| 579 | drive = get_option_val(argc, argv, "drive");
|
---|
| 580 | if (!drive)
|
---|
| 581 | continue;
|
---|
| 582 |
|
---|
| 583 | strcpy(raidlist->el[raidlist->entries - 1].plex
|
---|
| 584 | [raidlist->el[raidlist->entries - 1].plexes - 1].sd
|
---|
| 585 | [raidlist->el[raidlist->entries - 1].plex
|
---|
| 586 | [raidlist->el[raidlist->entries - 1].plexes -
|
---|
| 587 | 1].subdisks].which_device, drive);
|
---|
| 588 | raidlist->el[raidlist->entries -
|
---|
| 589 | 1].plex[raidlist->el[raidlist->entries -
|
---|
| 590 | 1].plexes - 1].subdisks++;
|
---|
| 591 | }
|
---|
[1] | 592 | }
|
---|
[128] | 593 | fclose(fin);
|
---|
| 594 | log_it("Raidtab loaded successfully.");
|
---|
[3191] | 595 | log_it("%d RAID devices in raidtab", raidlist->entries);
|
---|
[128] | 596 | return (0);
|
---|
[1] | 597 | }
|
---|
| 598 |
|
---|
| 599 |
|
---|
| 600 | #else
|
---|
| 601 |
|
---|
[3191] | 602 | int load_raidtab_into_raidlist(struct raidlist_itself *raidlist, char *fname) {
|
---|
| 603 |
|
---|
| 604 | FILE *fin = NULL;
|
---|
| 605 | char *label = NULL;
|
---|
| 606 | char *value = NULL;
|
---|
[3566] | 607 | int items = 0;
|
---|
| 608 | int v = 0;
|
---|
[1] | 609 |
|
---|
[128] | 610 | assert(raidlist != NULL);
|
---|
| 611 | assert_string_is_neither_NULL_nor_zerolength(fname);
|
---|
[1] | 612 |
|
---|
[128] | 613 | if (length_of_file(fname) < 5) {
|
---|
| 614 | log_it("Raidtab is very small or non-existent. Ignoring it.");
|
---|
| 615 | raidlist->entries = 0;
|
---|
| 616 | return (0);
|
---|
[1] | 617 | }
|
---|
[128] | 618 | if (!(fin = fopen(fname, "r"))) {
|
---|
| 619 | log_it("Cannot open raidtab");
|
---|
| 620 | return (1);
|
---|
[1] | 621 | }
|
---|
[128] | 622 | log_it("Loading raidtab...");
|
---|
[3566] | 623 | get_next_raidtab_line(fin, &label, &value);
|
---|
[128] | 624 | while (!feof(fin)) {
|
---|
| 625 | log_msg(1, "Looking for raid record #%d", items);
|
---|
| 626 | initialize_raidrec(&raidlist->el[items]);
|
---|
| 627 | v = 0;
|
---|
| 628 | /* find the 'raiddev' entry, indicating the start of the block of info */
|
---|
| 629 | while (!feof(fin) && strcmp(label, "raiddev")) {
|
---|
| 630 | strcpy(raidlist->el[items].additional_vars.el[v].label, label);
|
---|
| 631 | strcpy(raidlist->el[items].additional_vars.el[v].value, value);
|
---|
[3566] | 632 | mr_free(label);
|
---|
[2989] | 633 | log_msg(2,"Found raidtab entry Label: %s Value: %s",raidlist->el[items].additional_vars.el[v].label,raidlist->el[items].additional_vars.el[v].value);
|
---|
[128] | 634 | v++;
|
---|
[3566] | 635 | get_next_raidtab_line(fin, &label, &value);
|
---|
[128] | 636 | }
|
---|
[3566] | 637 |
|
---|
[128] | 638 | raidlist->el[items].additional_vars.entries = v;
|
---|
| 639 | if (feof(fin)) {
|
---|
| 640 | log_msg(1, "No more records.");
|
---|
| 641 | continue;
|
---|
| 642 | }
|
---|
| 643 | log_msg(2, "Record #%d (%s) found", items, value);
|
---|
| 644 | strcpy(raidlist->el[items].raid_device, value);
|
---|
[3566] | 645 | mr_free(label);
|
---|
| 646 | for (get_next_raidtab_line(fin, &label, &value); !feof(fin) && strcmp(label, "raiddev"); get_next_raidtab_line(fin, &label, &value)) {
|
---|
[128] | 647 | process_raidtab_line(fin, &raidlist->el[items], label, value);
|
---|
[3566] | 648 | mr_free(label);
|
---|
[128] | 649 | }
|
---|
| 650 | items++;
|
---|
[1] | 651 | }
|
---|
[3566] | 652 | mr_free(label);
|
---|
[128] | 653 | paranoid_fclose(fin);
|
---|
| 654 | raidlist->entries = items;
|
---|
| 655 | log_msg(1, "Raidtab loaded successfully.");
|
---|
| 656 | log_msg(1, "%d RAID devices in raidtab", items);
|
---|
| 657 | return (0);
|
---|
[1] | 658 | }
|
---|
| 659 | #endif
|
---|
| 660 |
|
---|
| 661 |
|
---|
| 662 |
|
---|
| 663 |
|
---|
| 664 |
|
---|
| 665 |
|
---|
| 666 |
|
---|
| 667 |
|
---|
| 668 | #ifndef __FreeBSD__
|
---|
| 669 | /**
|
---|
| 670 | * Process a single line from the raidtab and store the results into @p raidrec.
|
---|
| 671 | * @param fin The stream to read the line from.
|
---|
| 672 | * @param raidrec The RAID device record to update.
|
---|
| 673 | * @param label Where to put the label processed.
|
---|
| 674 | * @param value Where to put the value processed.
|
---|
| 675 | */
|
---|
| 676 | void
|
---|
[3566] | 677 | process_raidtab_line(FILE * fin, struct raid_device_record *raidrec, char *label, char *value)
|
---|
[1] | 678 | {
|
---|
| 679 |
|
---|
[128] | 680 | /*@ add mallocs * */
|
---|
[3566] | 681 | char *labelB = NULL;
|
---|
| 682 | char *valueB = NULL;
|
---|
[1] | 683 |
|
---|
[128] | 684 | struct list_of_disks *disklist;
|
---|
| 685 | int index;
|
---|
| 686 | int v;
|
---|
[1] | 687 |
|
---|
[128] | 688 | assert(fin != NULL);
|
---|
| 689 | assert(raidrec != NULL);
|
---|
| 690 | assert_string_is_neither_NULL_nor_zerolength(label);
|
---|
| 691 | assert(value != NULL);
|
---|
[1] | 692 |
|
---|
[128] | 693 | if (!strcmp(label, "raid-level")) {
|
---|
[558] | 694 | if (!strcmp(value, "multipath")) {
|
---|
| 695 | raidrec->raid_level = -2;
|
---|
| 696 | } else if (!strcmp(value, "linear")) {
|
---|
[128] | 697 | raidrec->raid_level = -1;
|
---|
| 698 | } else {
|
---|
| 699 | raidrec->raid_level = atoi(value);
|
---|
| 700 | }
|
---|
[2989] | 701 | log_msg(4,"Found raid level %d",raidrec->raid_level);
|
---|
[128] | 702 | } else if (!strcmp(label, "nr-raid-disks")) { /* ignore it */
|
---|
| 703 | } else if (!strcmp(label, "nr-spare-disks")) { /* ignore it */
|
---|
| 704 | } else if (!strcmp(label, "nr-parity-disks")) { /* ignore it */
|
---|
| 705 | } else if (!strcmp(label, "nr-failed-disks")) { /* ignore it */
|
---|
| 706 | } else if (!strcmp(label, "persistent-superblock")) {
|
---|
| 707 | raidrec->persistent_superblock = atoi(value);
|
---|
| 708 | } else if (!strcmp(label, "chunk-size")) {
|
---|
| 709 | raidrec->chunk_size = atoi(value);
|
---|
[558] | 710 | } else if (!strcmp(label, "parity-algorithm")) {
|
---|
| 711 | if (!strcmp(value, "left-asymmetric")) {
|
---|
| 712 | raidrec->parity = 0;
|
---|
| 713 | } else if (!strcmp(value, "right-asymmetric")) {
|
---|
| 714 | raidrec->parity = 1;
|
---|
| 715 | } else if (!strcmp(value, "left-symmetric")) {
|
---|
| 716 | raidrec->parity = 2;
|
---|
| 717 | } else if (!strcmp(value, "right-symmetric")) {
|
---|
| 718 | raidrec->parity = 3;
|
---|
| 719 | } else {
|
---|
[3380] | 720 | log_msg(1, "Unknown RAID parity algorithm '%s'", value);
|
---|
[558] | 721 | }
|
---|
[2989] | 722 | log_msg(4,"Found raid parity %d",raidrec->parity);
|
---|
[128] | 723 | } else if (!strcmp(label, "device")) {
|
---|
[3566] | 724 | get_next_raidtab_line(fin, &labelB, &valueB);
|
---|
[128] | 725 | if (!strcmp(labelB, "raid-disk")) {
|
---|
| 726 | disklist = &raidrec->data_disks;
|
---|
| 727 | } else if (!strcmp(labelB, "spare-disk")) {
|
---|
| 728 | disklist = &raidrec->spare_disks;
|
---|
| 729 | } else if (!strcmp(labelB, "parity-disk")) {
|
---|
| 730 | disklist = &raidrec->parity_disks;
|
---|
| 731 | } else if (!strcmp(labelB, "failed-disk")) {
|
---|
| 732 | disklist = &raidrec->failed_disks;
|
---|
| 733 | } else {
|
---|
| 734 | disklist = NULL;
|
---|
| 735 | }
|
---|
| 736 | if (!disklist) {
|
---|
[3191] | 737 | log_it("Ignoring '%s %s' pair of disk %s", labelB, valueB, label);
|
---|
[128] | 738 | } else {
|
---|
| 739 | index = atoi(valueB);
|
---|
| 740 | add_disk_to_raid_device(disklist, value, index);
|
---|
| 741 | }
|
---|
[3566] | 742 | mr_free(labelB);
|
---|
[128] | 743 | } else {
|
---|
| 744 | v = raidrec->additional_vars.entries;
|
---|
| 745 | strcpy(raidrec->additional_vars.el[v].label, label);
|
---|
| 746 | strcpy(raidrec->additional_vars.el[v].value, value);
|
---|
[2989] | 747 | log_msg(4,"Found additional raid pair #%d: %s / %s",v,raidrec->additional_vars.el[v].label,raidrec->additional_vars.el[v].value);
|
---|
| 748 | v++;
|
---|
| 749 | raidrec->additional_vars.entries = v;
|
---|
[1] | 750 | }
|
---|
| 751 | }
|
---|
| 752 | #endif
|
---|
| 753 |
|
---|
| 754 |
|
---|
| 755 | #ifdef __FreeBSD__
|
---|
| 756 | /**
|
---|
| 757 | * Add a new plex to a volume. The index of the plex will be <tt>v-\>plexes - 1</tt>.
|
---|
| 758 | * @param v The volume to operate on.
|
---|
| 759 | * @param raidlevel The RAID level of the new plex.
|
---|
| 760 | * @param stripesize The stripe size (chunk size) of the new plex.
|
---|
| 761 | */
|
---|
[128] | 762 | void add_plex_to_volume(struct vinum_volume *v, int raidlevel,
|
---|
| 763 | int stripesize)
|
---|
[1] | 764 | {
|
---|
[128] | 765 | v->plex[v->plexes].raidlevel = raidlevel;
|
---|
| 766 | v->plex[v->plexes].stripesize = stripesize;
|
---|
| 767 | v->plex[v->plexes].subdisks = 0;
|
---|
| 768 | ++v->plexes;
|
---|
[1] | 769 | }
|
---|
| 770 |
|
---|
| 771 | /**
|
---|
| 772 | * For internal use only.
|
---|
| 773 | */
|
---|
[128] | 774 | char **get_next_vinum_conf_line(FILE * f, int *argc)
|
---|
[1] | 775 | {
|
---|
[128] | 776 | int cnt = 0;
|
---|
| 777 | static char *argv[64];
|
---|
| 778 | char **ap;
|
---|
[3191] | 779 | char *line = NULL;
|
---|
[3060] | 780 |
|
---|
[3191] | 781 | mr_getline(line, f);
|
---|
| 782 | if (feof(f)) {
|
---|
[128] | 783 | log_it("[GNVCL] Uh... I reached the EOF.");
|
---|
| 784 | return 0;
|
---|
[1] | 785 | }
|
---|
| 786 |
|
---|
[128] | 787 | for (ap = argv; (*ap = strsep(&line, " \t")) != NULL;)
|
---|
| 788 | if (**ap != '\0') {
|
---|
| 789 | if (++ap >= &argv[64])
|
---|
| 790 | break;
|
---|
| 791 | cnt++;
|
---|
| 792 | }
|
---|
[3191] | 793 | mr_free(line);
|
---|
[1] | 794 |
|
---|
[128] | 795 | if (strchr(argv[cnt - 1], '\n')) {
|
---|
| 796 | *(strchr(argv[cnt - 1], '\n')) = '\0';
|
---|
| 797 | }
|
---|
| 798 |
|
---|
| 799 | if (argc)
|
---|
| 800 | *argc = cnt;
|
---|
| 801 | return argv;
|
---|
[1] | 802 | }
|
---|
| 803 |
|
---|
| 804 | /**
|
---|
| 805 | * For internal use only.
|
---|
| 806 | */
|
---|
[128] | 807 | char *get_option_val(int argc, char **argv, char *option)
|
---|
[1] | 808 | {
|
---|
[128] | 809 | int i;
|
---|
| 810 | for (i = 0; i < (argc - 1); ++i) {
|
---|
| 811 | if (!strcmp(argv[i], option)) {
|
---|
| 812 | return argv[i + 1];
|
---|
| 813 | }
|
---|
[1] | 814 | }
|
---|
[128] | 815 | return 0;
|
---|
[1] | 816 | }
|
---|
| 817 |
|
---|
| 818 | /**
|
---|
| 819 | * For internal use only.
|
---|
| 820 | */
|
---|
[128] | 821 | char **get_option_vals(int argc, char **argv, char *option, int nval)
|
---|
[1] | 822 | {
|
---|
[128] | 823 | int i, j;
|
---|
| 824 | static char **ret;
|
---|
| 825 | ret = (char **) malloc(nval * sizeof(char *));
|
---|
| 826 | for (i = 0; i < (argc - nval); ++i) {
|
---|
| 827 | if (!strcmp(argv[i], option)) {
|
---|
| 828 | for (j = 0; j < nval; ++j) {
|
---|
| 829 | ret[j] = (char *) malloc(strlen(argv[i + j + 1]) + 1);
|
---|
| 830 | strcpy(ret[j], argv[i + j + 1]);
|
---|
| 831 | }
|
---|
| 832 | return ret;
|
---|
| 833 | }
|
---|
[1] | 834 | }
|
---|
[128] | 835 | return 0;
|
---|
[1] | 836 | }
|
---|
| 837 |
|
---|
| 838 | /**
|
---|
| 839 | * For internal use only.
|
---|
| 840 | */
|
---|
[128] | 841 | bool get_option_state(int argc, char **argv, char *option)
|
---|
[1] | 842 | {
|
---|
[128] | 843 | int i;
|
---|
| 844 | for (i = 0; i < argc; ++i)
|
---|
| 845 | if (!strcmp(argv[i], option))
|
---|
| 846 | return TRUE;
|
---|
[1] | 847 |
|
---|
[128] | 848 | return FALSE;
|
---|
[1] | 849 | }
|
---|
| 850 |
|
---|
| 851 | /**
|
---|
| 852 | * Taken from Vinum source -- for internal use only.
|
---|
| 853 | */
|
---|
| 854 | long long size_spec(char *spec)
|
---|
| 855 | {
|
---|
[128] | 856 | u_int64_t size;
|
---|
| 857 | char *s;
|
---|
| 858 | int sign = 1; /* -1 if negative */
|
---|
[1] | 859 |
|
---|
[128] | 860 | size = 0;
|
---|
| 861 | if (spec != NULL) { /* we have a parameter */
|
---|
| 862 | s = spec;
|
---|
| 863 | if (*s == '-') { /* negative, */
|
---|
| 864 | sign = -1;
|
---|
| 865 | s++; /* skip */
|
---|
| 866 | }
|
---|
| 867 | if ((*s >= '0') && (*s <= '9')) { /* it's numeric */
|
---|
| 868 | while ((*s >= '0') && (*s <= '9')) /* it's numeric */
|
---|
| 869 | size = size * 10 + *s++ - '0'; /* convert it */
|
---|
| 870 | switch (*s) {
|
---|
| 871 | case '\0':
|
---|
| 872 | return size * sign;
|
---|
[1] | 873 |
|
---|
[128] | 874 | case 'B':
|
---|
| 875 | case 'b':
|
---|
| 876 | case 'S':
|
---|
| 877 | case 's':
|
---|
| 878 | return size * sign * 512;
|
---|
[1] | 879 |
|
---|
[128] | 880 | case 'K':
|
---|
| 881 | case 'k':
|
---|
| 882 | return size * sign * 1024;
|
---|
[1] | 883 |
|
---|
[128] | 884 | case 'M':
|
---|
| 885 | case 'm':
|
---|
| 886 | return size * sign * 1024 * 1024;
|
---|
[1] | 887 |
|
---|
[128] | 888 | case 'G':
|
---|
| 889 | case 'g':
|
---|
| 890 | return size * sign * 1024 * 1024 * 1024;
|
---|
[1] | 891 |
|
---|
[128] | 892 | case 'T':
|
---|
| 893 | case 't':
|
---|
| 894 | log_it
|
---|
| 895 | ("Ok, I'm scared... Someone did a TERABYTE+ size-spec");
|
---|
| 896 | return size * sign * 1024 * 1024 * 1024 * 1024;
|
---|
[1] | 897 |
|
---|
[128] | 898 | case 'P':
|
---|
| 899 | case 'p':
|
---|
| 900 | log_it
|
---|
| 901 | ("If I was scared last time, I'm freaked out now. Someone actually has a PETABYTE?!?!?!?!");
|
---|
| 902 | return size * sign * 1024 * 1024 * 1024 * 1024 * 1024;
|
---|
[1] | 903 |
|
---|
[128] | 904 | case 'E':
|
---|
| 905 | case 'e':
|
---|
| 906 | log_it
|
---|
| 907 | ("Okay, I'm REALLY freaked out. Who could devote a whole EXABYTE to their data?!?!");
|
---|
| 908 | return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
|
---|
| 909 | 1024;
|
---|
[1] | 910 |
|
---|
[128] | 911 | case 'Z':
|
---|
| 912 | case 'z':
|
---|
| 913 | log_it
|
---|
| 914 | ("WHAT!?!? A ZETABYTE!?!? You've GOT to be kidding me!!!");
|
---|
| 915 | return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
|
---|
| 916 | 1024 * 1024;
|
---|
| 917 |
|
---|
| 918 | case 'Y':
|
---|
| 919 | case 'y':
|
---|
| 920 | log_it
|
---|
| 921 | ("Oh my gosh. You actually think a YOTTABYTE will get you anywhere? What're you going to do with 1,208,925,819,614,629,174,706,176 bytes?!?!");
|
---|
| 922 | popup_and_OK
|
---|
[541] | 923 | ("That sizespec is more than 1,208,925,819,614,629,174,706,176 bytes. You have a shocking amount of data. Please send a screenshot to the list :-)");
|
---|
[128] | 924 | return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
|
---|
| 925 | 1024 * 1024 * 1024;
|
---|
| 926 | }
|
---|
| 927 | }
|
---|
[1] | 928 | }
|
---|
[128] | 929 | return size * sign;
|
---|
[1] | 930 | }
|
---|
| 931 |
|
---|
| 932 | #endif
|
---|
| 933 |
|
---|
| 934 |
|
---|
| 935 |
|
---|
| 936 |
|
---|
[2982] | 937 | int parse_mdstat(char *mdstat_fname, struct raidlist_itself *raidlist, char *device_prefix) {
|
---|
[128] | 938 |
|
---|
[2982] | 939 | const char delims[] = " ";
|
---|
[558] | 940 |
|
---|
[2982] | 941 | FILE *fin = NULL;
|
---|
| 942 | int res = 0, row, i, index_min;
|
---|
| 943 | int v = 0;
|
---|
| 944 | int lastpos = 0;
|
---|
| 945 | size_t len = 0;
|
---|
| 946 | char *token = NULL;
|
---|
| 947 | char *string = NULL;
|
---|
| 948 | char *cmd = NULL;
|
---|
| 949 | char *pos = NULL;
|
---|
| 950 | char type;
|
---|
| 951 | char *strtmp = NULL;
|
---|
| 952 | char *strtmp2 = NULL;
|
---|
[558] | 953 |
|
---|
[2982] | 954 | // open file
|
---|
| 955 | if (!(fin = fopen(mdstat_fname, "r"))) {
|
---|
[3380] | 956 | log_msg(1, "Could not open %s.", mdstat_fname);
|
---|
[2982] | 957 | return 1;
|
---|
| 958 | }
|
---|
| 959 | // initialise record, build progress and row counters
|
---|
| 960 | raidlist->entries = 0;
|
---|
| 961 | raidlist->el[raidlist->entries].progress = 999;
|
---|
| 962 | row = 1;
|
---|
| 963 | // skip first output row - contains registered RAID levels
|
---|
| 964 | res = getline(&strtmp2, &len, fin);
|
---|
| 965 | mr_free(strtmp2);
|
---|
| 966 | // parse the rest
|
---|
| 967 | while ( !feof_unlocked(fin) ) {
|
---|
| 968 | res = getline(&string, &len, fin);
|
---|
[3380] | 969 | log_msg(8, "mdstat line '%s' read.", string);
|
---|
[2982] | 970 | if (res <= 0) break;
|
---|
| 971 | // trim spaces
|
---|
| 972 | strip_spaces(string);
|
---|
[3380] | 973 | log_msg(8, "mdstat line 2 '%s' read.", string);
|
---|
[2982] | 974 | // if we have newline after only spaces, this is a blank line, update
|
---|
| 975 | // counters, otherwise do normal parsing
|
---|
| 976 | if (!strcmp(string,"")) {
|
---|
| 977 | row = 1;
|
---|
| 978 | raidlist->entries++;
|
---|
| 979 | raidlist->el[raidlist->entries].progress = 999;
|
---|
[558] | 980 | } else {
|
---|
[2982] | 981 | switch (row) {
|
---|
| 982 | case 1: // device information
|
---|
| 983 | // check whether last line of record and if so skip
|
---|
[3380] | 984 | log_msg(8, "This is the device line");
|
---|
[2982] | 985 | pos = strcasestr(string, "unused devices: ");
|
---|
| 986 | if (pos != NULL) {
|
---|
| 987 | break;
|
---|
| 988 | }
|
---|
| 989 | // tokenise string
|
---|
| 990 | token = mr_strtok(string, delims, &lastpos);
|
---|
| 991 | if (token == NULL) {
|
---|
| 992 | // should not happen !
|
---|
| 993 | break;
|
---|
| 994 | }
|
---|
| 995 | // get RAID device name
|
---|
[3185] | 996 | mr_asprintf(strtmp,"%s%s", device_prefix, token);
|
---|
[2982] | 997 | strncpy(raidlist->el[raidlist->entries].raid_device, strtmp, 63);
|
---|
[3060] | 998 | raidlist->el[raidlist->entries].raid_device[63] = '\0';
|
---|
[2982] | 999 | mr_free(strtmp);
|
---|
| 1000 | mr_free(token);
|
---|
| 1001 | // store the UUID value in the additional_vars structure
|
---|
| 1002 | v = raidlist->el[raidlist->entries].additional_vars.entries;
|
---|
| 1003 | strcpy(raidlist->el[raidlist->entries].additional_vars.el[v].label, "UUID");
|
---|
[3185] | 1004 | mr_asprintf(cmd,"mdadm --detail %s | grep UUID | cut -d: -f2- | sed 's/^ *//'", raidlist->el[raidlist->entries].raid_device);
|
---|
[3610] | 1005 | strtmp = call_program_and_get_last_line_of_output(cmd);
|
---|
[2982] | 1006 | strcpy(raidlist->el[raidlist->entries].additional_vars.el[v].value, strtmp);
|
---|
| 1007 | mr_free(strtmp);
|
---|
| 1008 | v++;
|
---|
[2989] | 1009 | // store the Version value in the additional_vars structure
|
---|
| 1010 | strcpy(raidlist->el[raidlist->entries].additional_vars.el[v].label, "Version");
|
---|
[3185] | 1011 | mr_asprintf(cmd,"mdadm --detail %s | grep Version | cut -d: -f2- | sed 's/^ *//'", raidlist->el[raidlist->entries].raid_device);
|
---|
[3610] | 1012 | strtmp = call_program_and_get_last_line_of_output(cmd);
|
---|
[2989] | 1013 | strcpy(raidlist->el[raidlist->entries].additional_vars.el[v].value, strtmp);
|
---|
| 1014 | mr_free(strtmp);
|
---|
| 1015 | v++;
|
---|
[2982] | 1016 | raidlist->el[raidlist->entries].additional_vars.entries = v;
|
---|
| 1017 | // skip ':' and status
|
---|
[3154] | 1018 | token = mr_strtok(string, delims, &lastpos);
|
---|
[2982] | 1019 | if (token == NULL) {
|
---|
| 1020 | // should not happen !
|
---|
| 1021 | break;
|
---|
| 1022 | }
|
---|
| 1023 | mr_free(token);
|
---|
[3154] | 1024 | token = mr_strtok(string, delims, &lastpos);
|
---|
[2982] | 1025 | if (token == NULL) {
|
---|
| 1026 | // should not happen !
|
---|
| 1027 | break;
|
---|
| 1028 | }
|
---|
| 1029 | if (!strcmp(token, "inactive")) {
|
---|
[3380] | 1030 | log_msg(1, "RAID device '%s' inactive.", raidlist->el[raidlist->entries].raid_device);
|
---|
[2982] | 1031 | mr_free(string);
|
---|
| 1032 | mr_free(token);
|
---|
| 1033 | return 1;
|
---|
| 1034 | }
|
---|
| 1035 | mr_free(token);
|
---|
[559] | 1036 |
|
---|
[2982] | 1037 | // get RAID level
|
---|
[3154] | 1038 | token = mr_strtok(string, delims, &lastpos);
|
---|
[2982] | 1039 | if (token == NULL) {
|
---|
| 1040 | // should not happen !
|
---|
| 1041 | break;
|
---|
| 1042 | }
|
---|
| 1043 | // skip potential auto-read-only entry
|
---|
| 1044 | if (!strcmp(token, "(auto-read-only)")) {
|
---|
| 1045 | mr_free(token);
|
---|
| 1046 | token = mr_strtok (string, delims, &lastpos);
|
---|
| 1047 | if (token == NULL) {
|
---|
| 1048 | // should not happen !
|
---|
| 1049 | break;
|
---|
| 1050 | }
|
---|
| 1051 | }
|
---|
| 1052 | if (!strcmp(token, "multipath")) {
|
---|
| 1053 | raidlist->el[raidlist->entries].raid_level = -2;
|
---|
| 1054 | } else if (!strcmp(token, "linear")) {
|
---|
| 1055 | raidlist->el[raidlist->entries].raid_level = -1;
|
---|
| 1056 | } else if (!strcmp(token, "raid0")) {
|
---|
| 1057 | raidlist->el[raidlist->entries].raid_level = 0;
|
---|
| 1058 | } else if (!strcmp(token, "raid1")) {
|
---|
| 1059 | raidlist->el[raidlist->entries].raid_level = 1;
|
---|
| 1060 | } else if (!strcmp(token, "raid4")) {
|
---|
| 1061 | raidlist->el[raidlist->entries].raid_level = 4;
|
---|
| 1062 | } else if (!strcmp(token, "raid5")) {
|
---|
| 1063 | raidlist->el[raidlist->entries].raid_level = 5;
|
---|
| 1064 | } else if (!strcmp(token, "raid6")) {
|
---|
| 1065 | raidlist->el[raidlist->entries].raid_level = 6;
|
---|
| 1066 | } else if (!strcmp(token, "raid10")) {
|
---|
| 1067 | raidlist->el[raidlist->entries].raid_level = 10;
|
---|
| 1068 | } else {
|
---|
[3380] | 1069 | log_msg(1, "Unknown RAID level '%s'", token);
|
---|
[2982] | 1070 | mr_free(string);
|
---|
| 1071 | mr_free(token);
|
---|
| 1072 | return 1;
|
---|
| 1073 | }
|
---|
| 1074 | mr_free(token);
|
---|
[559] | 1075 |
|
---|
[2982] | 1076 | // get RAID devices (type, index, device)
|
---|
| 1077 | // Note: parity disk for RAID4 is last normal disk, there is no '(P)'
|
---|
| 1078 | raidlist->el[raidlist->entries].data_disks.entries = 0;
|
---|
| 1079 | raidlist->el[raidlist->entries].spare_disks.entries = 0;
|
---|
| 1080 | raidlist->el[raidlist->entries].failed_disks.entries = 0;
|
---|
| 1081 | while((token = mr_strtok (string, delims, &lastpos))) {
|
---|
| 1082 | if ((pos = strstr(token, "("))) {
|
---|
[3191] | 1083 | type = *(pos+1);
|
---|
[2982] | 1084 | } else {
|
---|
[3191] | 1085 | type = ' ';
|
---|
[2982] | 1086 | }
|
---|
| 1087 | pos = strstr(token, "[");
|
---|
| 1088 | *pos = '\0';
|
---|
| 1089 | switch(type) {
|
---|
| 1090 | case ' ': // normal data disks
|
---|
[3191] | 1091 | raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].index = atoi(pos + 1);
|
---|
| 1092 | mr_asprintf(strtmp,"%s%s", device_prefix, token);
|
---|
| 1093 | strcpy(raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].device, strtmp);
|
---|
| 1094 | mr_free(strtmp);
|
---|
| 1095 | raidlist->el[raidlist->entries].data_disks.entries++;
|
---|
| 1096 | break;
|
---|
[2982] | 1097 | case 'S': // spare disks
|
---|
[3191] | 1098 | raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].index = atoi(pos + 1);
|
---|
| 1099 | mr_asprintf(strtmp,"%s%s", device_prefix, token);
|
---|
| 1100 | strcpy(raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].device, strtmp);
|
---|
| 1101 | mr_free(strtmp);
|
---|
| 1102 | raidlist->el[raidlist->entries].spare_disks.entries++;
|
---|
| 1103 | break;
|
---|
[2982] | 1104 | case 'F': // failed disks
|
---|
[3191] | 1105 | raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].index = atoi(pos + 1);
|
---|
| 1106 | mr_asprintf(strtmp,"%s%s", device_prefix, token);
|
---|
| 1107 | strcpy(raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].device, strtmp);
|
---|
| 1108 | mr_free(strtmp);
|
---|
| 1109 | raidlist->el[raidlist->entries].failed_disks.entries++;
|
---|
| 1110 | log_it("At least one failed disk found in RAID array.\n");
|
---|
| 1111 | break;
|
---|
[2982] | 1112 | default: // error
|
---|
[3380] | 1113 | log_msg(1, "Unknown device type '%c'", type);
|
---|
[3191] | 1114 | mr_free(string);
|
---|
| 1115 | mr_free(token);
|
---|
| 1116 | return 1;
|
---|
| 1117 | break;
|
---|
[2982] | 1118 | }
|
---|
| 1119 | mr_free(token);
|
---|
| 1120 | }
|
---|
| 1121 |
|
---|
| 1122 | // adjust index for each device so that it starts with 0 for every type
|
---|
| 1123 | index_min = 99;
|
---|
| 1124 | for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
|
---|
| 1125 | if (raidlist->el[raidlist->entries].data_disks.el[i].index < index_min) {
|
---|
[3191] | 1126 | index_min = raidlist->el[raidlist->entries].data_disks.el[i].index;
|
---|
[2982] | 1127 | }
|
---|
| 1128 | }
|
---|
| 1129 | if (index_min > 0) {
|
---|
| 1130 | for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
|
---|
[3191] | 1131 | raidlist->el[raidlist->entries].data_disks.el[i].index = raidlist->el[raidlist->entries].data_disks.el[i].index - index_min;
|
---|
[2982] | 1132 | }
|
---|
| 1133 | }
|
---|
| 1134 | index_min = 99;
|
---|
| 1135 | for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
|
---|
| 1136 | if (raidlist->el[raidlist->entries].spare_disks.el[i].index < index_min) {
|
---|
[3191] | 1137 | index_min = raidlist->el[raidlist->entries].spare_disks.el[i].index;
|
---|
[2982] | 1138 | }
|
---|
| 1139 | }
|
---|
| 1140 | if (index_min > 0) {
|
---|
| 1141 | for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
|
---|
[3191] | 1142 | raidlist->el[raidlist->entries].spare_disks.el[i].index = raidlist->el[raidlist->entries].spare_disks.el[i].index - index_min;
|
---|
[2982] | 1143 | }
|
---|
| 1144 | }
|
---|
| 1145 | index_min = 99;
|
---|
| 1146 | for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
|
---|
| 1147 | if (raidlist->el[raidlist->entries].failed_disks.el[i].index < index_min) {
|
---|
[3191] | 1148 | index_min = raidlist->el[raidlist->entries].failed_disks.el[i].index;
|
---|
[2982] | 1149 | }
|
---|
| 1150 | }
|
---|
| 1151 | if (index_min > 0) {
|
---|
| 1152 | for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
|
---|
[3191] | 1153 | raidlist->el[raidlist->entries].failed_disks.el[i].index = raidlist->el[raidlist->entries].failed_disks.el[i].index - index_min;
|
---|
[2982] | 1154 | }
|
---|
| 1155 | }
|
---|
| 1156 | break;
|
---|
| 1157 | case 2: // config information
|
---|
| 1158 | // check for persistent super block
|
---|
| 1159 | if (strcasestr(string, "super non-persistent")) {
|
---|
| 1160 | raidlist->el[raidlist->entries].persistent_superblock = 0;
|
---|
| 1161 | } else {
|
---|
| 1162 | raidlist->el[raidlist->entries].persistent_superblock = 1;
|
---|
| 1163 | }
|
---|
| 1164 | // extract chunk size
|
---|
| 1165 | if (!(pos = strcasestr(string, "k chunk"))) {
|
---|
| 1166 | raidlist->el[raidlist->entries].chunk_size = -1;
|
---|
| 1167 | } else {
|
---|
| 1168 | while (*pos != ' ') {
|
---|
[3191] | 1169 | pos -= 1;
|
---|
| 1170 | if (pos < string) {
|
---|
| 1171 | log_it("String underflow!\n");
|
---|
| 1172 | mr_free(string);
|
---|
| 1173 | return 1;
|
---|
| 1174 | }
|
---|
[2982] | 1175 | }
|
---|
| 1176 | raidlist->el[raidlist->entries].chunk_size = atoi(pos + 1);
|
---|
| 1177 | }
|
---|
| 1178 | // extract parity if present
|
---|
| 1179 | if ((pos = strcasestr(string, "algorithm"))) {
|
---|
| 1180 | raidlist->el[raidlist->entries].parity = atoi(pos + 9);
|
---|
| 1181 | } else {
|
---|
| 1182 | raidlist->el[raidlist->entries].parity = -1;
|
---|
| 1183 | }
|
---|
| 1184 | break;
|
---|
| 1185 | case 3: // optional build status information
|
---|
| 1186 | if (!(pos = strchr(string, '\%'))) {
|
---|
| 1187 | if (strcasestr(string, "delayed")) {
|
---|
[3191] | 1188 | raidlist->el[raidlist->entries].progress = -1; // delayed (therefore, stuck at 0%)
|
---|
[2982] | 1189 | } else {
|
---|
[3191] | 1190 | raidlist->el[raidlist->entries].progress = 999; // not found
|
---|
[2982] | 1191 | }
|
---|
| 1192 | } else {
|
---|
| 1193 | while (*pos != ' ') {
|
---|
[3191] | 1194 | pos -= 1;
|
---|
| 1195 | if (pos < string) {
|
---|
| 1196 | printf("ERROR: String underflow!\n");
|
---|
| 1197 | mr_free(string);
|
---|
| 1198 | return 1;
|
---|
| 1199 | }
|
---|
[2982] | 1200 | }
|
---|
| 1201 | raidlist->el[raidlist->entries].progress = atoi(pos);
|
---|
| 1202 | }
|
---|
| 1203 | break;
|
---|
[3191] | 1204 | default: // error or IN PROGRESS
|
---|
[2982] | 1205 | if (raidlist->el[raidlist->entries].progress != -1 &&
|
---|
| 1206 | raidlist->el[raidlist->entries].progress != 999) {
|
---|
[3380] | 1207 | log_msg(1, "Row %d should not occur in record!", row);
|
---|
[2982] | 1208 | }
|
---|
| 1209 | break;
|
---|
[3191] | 1210 | }
|
---|
| 1211 | row++;
|
---|
[558] | 1212 | }
|
---|
[2982] | 1213 | // free string
|
---|
| 1214 | mr_free(string);
|
---|
| 1215 | }
|
---|
| 1216 | // close file
|
---|
| 1217 | fclose(fin);
|
---|
| 1218 | // return success
|
---|
| 1219 | return 0;
|
---|
[558] | 1220 |
|
---|
[1] | 1221 | }
|
---|
| 1222 |
|
---|
| 1223 |
|
---|
| 1224 |
|
---|
[558] | 1225 |
|
---|
[2982] | 1226 | int create_raidtab_from_mdstat(char *mdstat_fname,char *raidtab_fname)
|
---|
[1] | 1227 | {
|
---|
[128] | 1228 | struct raidlist_itself *raidlist;
|
---|
| 1229 | int retval = 0;
|
---|
[1] | 1230 |
|
---|
[128] | 1231 | raidlist = malloc(sizeof(struct raidlist_itself));
|
---|
[1] | 1232 |
|
---|
[558] | 1233 | // FIXME: Prefix '/dev/' should really be dynamic!
|
---|
[2982] | 1234 | if (parse_mdstat(mdstat_fname,raidlist, "/dev/")) {
|
---|
| 1235 | log_to_screen("Sorry, cannot read %s", mdstat_fname);
|
---|
[128] | 1236 | return (1);
|
---|
| 1237 | }
|
---|
| 1238 |
|
---|
| 1239 | retval += save_raidlist_to_raidtab(raidlist, raidtab_fname);
|
---|
| 1240 | return (retval);
|
---|
[1] | 1241 | }
|
---|
| 1242 |
|
---|
| 1243 |
|
---|
| 1244 |
|
---|
| 1245 | /* @} - end of raidGroup */
|
---|