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