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