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