[1] | 1 | /* libmondo-raid.c subroutines for handling RAID
|
---|
[128] | 2 | $Id: libmondo-raid.c 3893 2024-03-13 15:49:30Z 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 3893 2024-03-13 15:49:30Z 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 | }
|
---|
[3892] | 58 | mr_strcat(command, "\" " MDSTAT_FILE " > /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;
|
---|
[3893] | 361 | default:
|
---|
| 362 | mr_asprintf(org, "Unknown RAID level %d", raidrec->plex[i].raidlevel);
|
---|
| 363 | fatal_error(org);
|
---|
| 364 | break;
|
---|
[128] | 365 | }
|
---|
| 366 | fprintf(fout, " plex org %s", org);
|
---|
[3191] | 367 | mr_free(org);
|
---|
| 368 |
|
---|
[128] | 369 | if (raidrec->plex[i].raidlevel != -1) {
|
---|
| 370 | fprintf(fout, " %ik", raidrec->plex[i].stripesize);
|
---|
| 371 | }
|
---|
| 372 | fprintf(fout, "\n");
|
---|
| 373 |
|
---|
| 374 | for (j = 0; j < raidrec->plex[i].subdisks; ++j) {
|
---|
[3191] | 375 | fprintf(fout, " sd drive %s size 0\n", raidrec->plex[i].sd[j].which_device);
|
---|
[128] | 376 | }
|
---|
[1] | 377 | }
|
---|
[128] | 378 | #else
|
---|
| 379 | assert(raidrec != NULL);
|
---|
| 380 | assert(fout != NULL);
|
---|
| 381 |
|
---|
| 382 | fprintf(fout, "raiddev %s\n", raidrec->raid_device);
|
---|
[558] | 383 | if (raidrec->raid_level == -2) {
|
---|
| 384 | fprintf(fout, " raid-level multipath\n");
|
---|
| 385 | } else if (raidrec->raid_level == -1) {
|
---|
[128] | 386 | fprintf(fout, " raid-level linear\n");
|
---|
| 387 | } else {
|
---|
[3566] | 388 | fprintf(fout, " raid-level %d\n", raidrec->raid_level);
|
---|
[1] | 389 | }
|
---|
[3566] | 390 | fprintf(fout, " nr-raid-disks %d\n", raidrec->data_disks.entries);
|
---|
[558] | 391 | if (raidrec->spare_disks.entries > 0) {
|
---|
[3566] | 392 | fprintf(fout, " nr-spare-disks %d\n", raidrec->spare_disks.entries);
|
---|
[558] | 393 | }
|
---|
[128] | 394 | if (raidrec->parity_disks.entries > 0) {
|
---|
[3566] | 395 | fprintf(fout, " nr-parity-disks %d\n", raidrec->parity_disks.entries);
|
---|
[1] | 396 | }
|
---|
[3566] | 397 | fprintf(fout, " persistent-superblock %d\n", raidrec->persistent_superblock);
|
---|
[558] | 398 | if (raidrec->chunk_size > -1) {
|
---|
| 399 | fprintf(fout, " chunk-size %d\n", raidrec->chunk_size);
|
---|
| 400 | }
|
---|
| 401 | if (raidrec->parity > -1) {
|
---|
| 402 | switch(raidrec->parity) {
|
---|
| 403 | case 0:
|
---|
| 404 | fprintf(fout, " parity-algorithm left-asymmetric\n");
|
---|
| 405 | break;
|
---|
| 406 | case 1:
|
---|
| 407 | fprintf(fout, " parity-algorithm right-asymmetric\n");
|
---|
| 408 | break;
|
---|
| 409 | case 2:
|
---|
| 410 | fprintf(fout, " parity-algorithm left-symmetric\n");
|
---|
| 411 | break;
|
---|
| 412 | case 3:
|
---|
| 413 | fprintf(fout, " parity-algorithm right-symmetric\n");
|
---|
| 414 | break;
|
---|
| 415 | default:
|
---|
| 416 | fatal_error("Unknown RAID parity algorithm.");
|
---|
| 417 | break;
|
---|
| 418 | }
|
---|
| 419 | }
|
---|
[128] | 420 | save_additional_vars_to_file(&raidrec->additional_vars, fout);
|
---|
| 421 | fprintf(fout, "\n");
|
---|
| 422 | save_disklist_to_file("raid-disk", &raidrec->data_disks, fout);
|
---|
| 423 | save_disklist_to_file("spare-disk", &raidrec->spare_disks, fout);
|
---|
| 424 | save_disklist_to_file("parity-disk", &raidrec->parity_disks, fout);
|
---|
| 425 | save_disklist_to_file("failed-disk", &raidrec->failed_disks, fout);
|
---|
| 426 | fprintf(fout, "\n");
|
---|
[1] | 427 | #endif
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 | /**
|
---|
[3878] | 431 | * Save a raidlist structure to disk in raidtab format.
|
---|
| 432 | * @param raidlist The raidlist to save.
|
---|
| 433 | * @param fname The file to save it to.
|
---|
| 434 | * @return 0, always.
|
---|
| 435 | * @bug Return value is redundant.
|
---|
| 436 | */
|
---|
| 437 | int save_raidlist_to_raidtab(struct raidlist_itself *raidlist, char *fname)
|
---|
| 438 | {
|
---|
| 439 | FILE *fout;
|
---|
| 440 | int current_raid_device;
|
---|
| 441 | #ifdef __FreeBSD__
|
---|
| 442 | int i;
|
---|
| 443 | #else
|
---|
| 444 | // Linux
|
---|
| 445 | #endif
|
---|
| 446 |
|
---|
| 447 | assert(raidlist != NULL);
|
---|
| 448 | assert_string_is_neither_NULL_nor_zerolength(fname);
|
---|
| 449 |
|
---|
| 450 | if (raidlist->entries <= 0) {
|
---|
| 451 | unlink(fname);
|
---|
| 452 | log_it("Deleting raidtab (no RAID devs anyway)");
|
---|
| 453 | return (0);
|
---|
| 454 | }
|
---|
| 455 | if (!(fout = fopen(fname, "w"))) {
|
---|
| 456 | log_OS_error("Failed to save raidlist");
|
---|
| 457 | return (1);
|
---|
| 458 | }
|
---|
| 459 | fprintf(fout, "# Generated by Mondo Rescue\n");
|
---|
| 460 |
|
---|
| 461 | #ifdef __FreeBSD__
|
---|
| 462 | for (i = 0; i < raidlist->disks.entries; ++i) {
|
---|
| 463 | fprintf(fout, "drive %s device %s\n", raidlist->disks.el[i].name,
|
---|
| 464 | raidlist->disks.el[i].device);
|
---|
| 465 | }
|
---|
| 466 | for (i = 0; i < (raidlist->spares.entries); ++i) {
|
---|
| 467 | fprintf(fout, "drive %s device %s hotspare\n",
|
---|
| 468 | raidlist->spares.el[i].name,
|
---|
| 469 | raidlist->spares.el[i].device);
|
---|
| 470 | }
|
---|
| 471 | #endif
|
---|
| 472 |
|
---|
| 473 | for (current_raid_device = 0; current_raid_device < raidlist->entries;
|
---|
| 474 | current_raid_device++) {
|
---|
| 475 | save_raidrec_to_file(&raidlist->el[current_raid_device], fout);
|
---|
| 476 | }
|
---|
| 477 | paranoid_fclose(fout);
|
---|
| 478 | return (0);
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 |
|
---|
| 482 | #ifndef __FreeBSD__
|
---|
| 483 | /**
|
---|
| 484 | * Process a single line from the raidtab and store the results into @p raidrec.
|
---|
| 485 | * @param fin The stream to read the line from.
|
---|
| 486 | * @param raidrec The RAID device record to update.
|
---|
| 487 | * @param label Where to put the label processed.
|
---|
| 488 | * @param value Where to put the value processed.
|
---|
| 489 | */
|
---|
| 490 | void
|
---|
| 491 | process_raidtab_line(FILE * fin, struct raid_device_record *raidrec, char *label, char *value)
|
---|
| 492 | {
|
---|
[1] | 493 |
|
---|
[3878] | 494 | /*@ add mallocs * */
|
---|
| 495 | char *labelB = NULL;
|
---|
| 496 | char *valueB = NULL;
|
---|
| 497 |
|
---|
| 498 | struct list_of_disks *disklist;
|
---|
| 499 | int index;
|
---|
| 500 | int v;
|
---|
| 501 |
|
---|
| 502 | assert(fin != NULL);
|
---|
| 503 | assert(raidrec != NULL);
|
---|
| 504 | assert_string_is_neither_NULL_nor_zerolength(label);
|
---|
| 505 | assert(value != NULL);
|
---|
| 506 |
|
---|
| 507 | if (!strcmp(label, "raid-level")) {
|
---|
| 508 | if (!strcmp(value, "multipath")) {
|
---|
| 509 | raidrec->raid_level = -2;
|
---|
| 510 | } else if (!strcmp(value, "linear")) {
|
---|
| 511 | raidrec->raid_level = -1;
|
---|
| 512 | } else {
|
---|
| 513 | raidrec->raid_level = atoi(value);
|
---|
| 514 | }
|
---|
| 515 | log_msg(4,"Found raid level %d",raidrec->raid_level);
|
---|
| 516 | } else if (!strcmp(label, "nr-raid-disks")) { /* ignore it */
|
---|
| 517 | } else if (!strcmp(label, "nr-spare-disks")) { /* ignore it */
|
---|
| 518 | } else if (!strcmp(label, "nr-parity-disks")) { /* ignore it */
|
---|
| 519 | } else if (!strcmp(label, "nr-failed-disks")) { /* ignore it */
|
---|
| 520 | } else if (!strcmp(label, "persistent-superblock")) {
|
---|
| 521 | raidrec->persistent_superblock = atoi(value);
|
---|
| 522 | } else if (!strcmp(label, "chunk-size")) {
|
---|
| 523 | raidrec->chunk_size = atoi(value);
|
---|
| 524 | } else if (!strcmp(label, "parity-algorithm")) {
|
---|
| 525 | if (!strcmp(value, "left-asymmetric")) {
|
---|
| 526 | raidrec->parity = 0;
|
---|
| 527 | } else if (!strcmp(value, "right-asymmetric")) {
|
---|
| 528 | raidrec->parity = 1;
|
---|
| 529 | } else if (!strcmp(value, "left-symmetric")) {
|
---|
| 530 | raidrec->parity = 2;
|
---|
| 531 | } else if (!strcmp(value, "right-symmetric")) {
|
---|
| 532 | raidrec->parity = 3;
|
---|
| 533 | } else {
|
---|
| 534 | log_msg(1, "Unknown RAID parity algorithm '%s'", value);
|
---|
| 535 | }
|
---|
| 536 | log_msg(4,"Found raid parity %d",raidrec->parity);
|
---|
| 537 | } else if (!strcmp(label, "device")) {
|
---|
| 538 | get_next_raidtab_line(fin, &labelB, &valueB);
|
---|
| 539 | if (!strcmp(labelB, "raid-disk")) {
|
---|
| 540 | disklist = &raidrec->data_disks;
|
---|
| 541 | } else if (!strcmp(labelB, "spare-disk")) {
|
---|
| 542 | disklist = &raidrec->spare_disks;
|
---|
| 543 | } else if (!strcmp(labelB, "parity-disk")) {
|
---|
| 544 | disklist = &raidrec->parity_disks;
|
---|
| 545 | } else if (!strcmp(labelB, "failed-disk")) {
|
---|
| 546 | disklist = &raidrec->failed_disks;
|
---|
| 547 | } else {
|
---|
| 548 | disklist = NULL;
|
---|
| 549 | }
|
---|
| 550 | if (!disklist) {
|
---|
| 551 | log_it("Ignoring '%s %s' pair of disk %s", labelB, valueB, label);
|
---|
| 552 | } else {
|
---|
| 553 | index = atoi(valueB);
|
---|
| 554 | add_disk_to_raid_device(disklist, value, index);
|
---|
| 555 | }
|
---|
| 556 | mr_free(labelB);
|
---|
| 557 | } else {
|
---|
| 558 | v = raidrec->additional_vars.entries;
|
---|
| 559 | strcpy(raidrec->additional_vars.el[v].label, label);
|
---|
| 560 | strcpy(raidrec->additional_vars.el[v].value, value);
|
---|
| 561 | log_msg(4,"Found additional raid pair #%d: %s / %s",v,raidrec->additional_vars.el[v].label,raidrec->additional_vars.el[v].value);
|
---|
| 562 | v++;
|
---|
| 563 | raidrec->additional_vars.entries = v;
|
---|
| 564 | }
|
---|
| 565 | }
|
---|
| 566 | #endif
|
---|
| 567 |
|
---|
| 568 |
|
---|
| 569 |
|
---|
| 570 |
|
---|
[1] | 571 | /**
|
---|
| 572 | * Load a raidtab file into a raidlist structure.
|
---|
| 573 | * @param raidlist The raidlist to fill.
|
---|
| 574 | * @param fname The file to read from.
|
---|
| 575 | * @return 0 for success, 1 for failure.
|
---|
| 576 | */
|
---|
| 577 | #ifdef __FreeBSD__
|
---|
[3566] | 578 | int load_raidtab_into_raidlist(struct raidlist_itself *raidlist, char *fname)
|
---|
[1] | 579 | {
|
---|
[128] | 580 | FILE *fin;
|
---|
| 581 | int items;
|
---|
[1] | 582 |
|
---|
[128] | 583 | raidlist->spares.entries = 0;
|
---|
| 584 | raidlist->disks.entries = 0;
|
---|
| 585 | if (length_of_file(fname) < 5) {
|
---|
| 586 | log_it("Raidtab is very small or non-existent. Ignoring it.");
|
---|
| 587 | raidlist->entries = 0;
|
---|
| 588 | return (0);
|
---|
[1] | 589 | }
|
---|
[128] | 590 | if (!(fin = fopen(fname, "r"))) {
|
---|
| 591 | log_it("Cannot open raidtab");
|
---|
| 592 | return (1);
|
---|
[1] | 593 | }
|
---|
[128] | 594 | items = 0;
|
---|
| 595 | log_it("Loading raidtab...");
|
---|
| 596 | while (!feof(fin)) {
|
---|
| 597 | int argc;
|
---|
| 598 | char **argv = get_next_vinum_conf_line(fin, &argc);
|
---|
| 599 | if (!argv)
|
---|
| 600 | break;
|
---|
| 601 | if (!strcmp(argv[0], "drive")) {
|
---|
| 602 | char *drivename, *devname;
|
---|
| 603 | if (argc < 4)
|
---|
| 604 | continue;
|
---|
| 605 | drivename = argv[1];
|
---|
| 606 | devname = get_option_val(argc, argv, "device");
|
---|
| 607 | if (!devname)
|
---|
| 608 | continue;
|
---|
[1] | 609 |
|
---|
[128] | 610 | if (get_option_state(argc, argv, "hotspare")) {
|
---|
[3191] | 611 | strcpy(raidlist->spares.el[raidlist->spares.entries].name, drivename);
|
---|
[3566] | 612 | strcpy(raidlist->spares.el[raidlist->spares.entries].device, devname);
|
---|
| 613 | raidlist->spares.el[raidlist->spares.entries].index = raidlist->disks.entries;
|
---|
[128] | 614 | raidlist->spares.entries++;
|
---|
| 615 | } else {
|
---|
[3191] | 616 | strcpy(raidlist->disks.el[raidlist->disks.entries].name, drivename);
|
---|
| 617 | strcpy(raidlist->disks.el[raidlist->disks.entries].device, devname);
|
---|
[3566] | 618 | raidlist->disks.el[raidlist->disks.entries].index = raidlist->disks.entries;
|
---|
[128] | 619 | raidlist->disks.entries++;
|
---|
| 620 | }
|
---|
| 621 | } else if (!strcmp(argv[0], "volume")) {
|
---|
| 622 | char *volname;
|
---|
| 623 | if (argc < 2)
|
---|
| 624 | continue;
|
---|
| 625 | volname = argv[1];
|
---|
| 626 | strcpy(raidlist->el[raidlist->entries].volname, volname);
|
---|
| 627 | raidlist->el[raidlist->entries].plexes = 0;
|
---|
| 628 | raidlist->entries++;
|
---|
| 629 | } else if (!strcmp(argv[0], "plex")) {
|
---|
| 630 | int raidlevel, stripesize;
|
---|
| 631 | char *org = 0;
|
---|
| 632 | char **tmp = 0;
|
---|
| 633 | if (argc < 3)
|
---|
| 634 | continue;
|
---|
| 635 | org = get_option_val(argc, argv, "org");
|
---|
| 636 | if (!org)
|
---|
| 637 | continue;
|
---|
| 638 | if (strcmp(org, "concat")) {
|
---|
| 639 | tmp = get_option_vals(argc, argv, "org", 2);
|
---|
| 640 | if (tmp && tmp[1]) {
|
---|
| 641 | stripesize = (int) (size_spec(tmp[1]) / 1024);
|
---|
| 642 | } else
|
---|
| 643 | stripesize = 279;
|
---|
| 644 | } else
|
---|
| 645 | stripesize = 0;
|
---|
[1] | 646 |
|
---|
[128] | 647 | if (!strcmp(org, "concat")) {
|
---|
| 648 | raidlevel = -1;
|
---|
| 649 | } else if (!strcmp(org, "striped")) {
|
---|
| 650 | raidlevel = 0;
|
---|
| 651 | } else if (!strcmp(org, "raid5")) {
|
---|
| 652 | raidlevel = 5;
|
---|
| 653 | } else
|
---|
| 654 | continue;
|
---|
| 655 |
|
---|
| 656 | raidlist->el[raidlist->entries - 1].plex
|
---|
| 657 | [raidlist->el[raidlist->entries - 1].plexes].raidlevel =
|
---|
| 658 | raidlevel;
|
---|
| 659 | raidlist->el[raidlist->entries -
|
---|
| 660 | 1].plex[raidlist->el[raidlist->entries -
|
---|
| 661 | 1].plexes].stripesize =
|
---|
| 662 | stripesize;
|
---|
| 663 | raidlist->el[raidlist->entries -
|
---|
| 664 | 1].plex[raidlist->el[raidlist->entries -
|
---|
| 665 | 1].plexes].subdisks = 0;
|
---|
| 666 | raidlist->el[raidlist->entries - 1].plexes++;
|
---|
| 667 | } else if ((!strcmp(argv[0], "sd"))
|
---|
| 668 | || (!strcmp(argv[0], "subdisk"))) {
|
---|
| 669 | char *drive = 0;
|
---|
| 670 | if (argc < 3)
|
---|
| 671 | continue;
|
---|
| 672 | drive = get_option_val(argc, argv, "drive");
|
---|
| 673 | if (!drive)
|
---|
| 674 | continue;
|
---|
| 675 |
|
---|
| 676 | strcpy(raidlist->el[raidlist->entries - 1].plex
|
---|
| 677 | [raidlist->el[raidlist->entries - 1].plexes - 1].sd
|
---|
| 678 | [raidlist->el[raidlist->entries - 1].plex
|
---|
| 679 | [raidlist->el[raidlist->entries - 1].plexes -
|
---|
| 680 | 1].subdisks].which_device, drive);
|
---|
| 681 | raidlist->el[raidlist->entries -
|
---|
| 682 | 1].plex[raidlist->el[raidlist->entries -
|
---|
| 683 | 1].plexes - 1].subdisks++;
|
---|
| 684 | }
|
---|
[1] | 685 | }
|
---|
[128] | 686 | fclose(fin);
|
---|
| 687 | log_it("Raidtab loaded successfully.");
|
---|
[3191] | 688 | log_it("%d RAID devices in raidtab", raidlist->entries);
|
---|
[128] | 689 | return (0);
|
---|
[1] | 690 | }
|
---|
| 691 |
|
---|
| 692 |
|
---|
| 693 | #else
|
---|
| 694 |
|
---|
[3191] | 695 | int load_raidtab_into_raidlist(struct raidlist_itself *raidlist, char *fname) {
|
---|
| 696 |
|
---|
| 697 | FILE *fin = NULL;
|
---|
| 698 | char *label = NULL;
|
---|
| 699 | char *value = NULL;
|
---|
[3566] | 700 | int items = 0;
|
---|
| 701 | int v = 0;
|
---|
[1] | 702 |
|
---|
[128] | 703 | assert(raidlist != NULL);
|
---|
| 704 | assert_string_is_neither_NULL_nor_zerolength(fname);
|
---|
[1] | 705 |
|
---|
[128] | 706 | if (length_of_file(fname) < 5) {
|
---|
| 707 | log_it("Raidtab is very small or non-existent. Ignoring it.");
|
---|
| 708 | raidlist->entries = 0;
|
---|
| 709 | return (0);
|
---|
[1] | 710 | }
|
---|
[128] | 711 | if (!(fin = fopen(fname, "r"))) {
|
---|
| 712 | log_it("Cannot open raidtab");
|
---|
| 713 | return (1);
|
---|
[1] | 714 | }
|
---|
[128] | 715 | log_it("Loading raidtab...");
|
---|
[3566] | 716 | get_next_raidtab_line(fin, &label, &value);
|
---|
[128] | 717 | while (!feof(fin)) {
|
---|
| 718 | log_msg(1, "Looking for raid record #%d", items);
|
---|
| 719 | initialize_raidrec(&raidlist->el[items]);
|
---|
| 720 | v = 0;
|
---|
| 721 | /* find the 'raiddev' entry, indicating the start of the block of info */
|
---|
| 722 | while (!feof(fin) && strcmp(label, "raiddev")) {
|
---|
| 723 | strcpy(raidlist->el[items].additional_vars.el[v].label, label);
|
---|
| 724 | strcpy(raidlist->el[items].additional_vars.el[v].value, value);
|
---|
[3566] | 725 | mr_free(label);
|
---|
[2989] | 726 | 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] | 727 | v++;
|
---|
[3566] | 728 | get_next_raidtab_line(fin, &label, &value);
|
---|
[128] | 729 | }
|
---|
[3566] | 730 |
|
---|
[128] | 731 | raidlist->el[items].additional_vars.entries = v;
|
---|
| 732 | if (feof(fin)) {
|
---|
| 733 | log_msg(1, "No more records.");
|
---|
| 734 | continue;
|
---|
| 735 | }
|
---|
| 736 | log_msg(2, "Record #%d (%s) found", items, value);
|
---|
| 737 | strcpy(raidlist->el[items].raid_device, value);
|
---|
[3566] | 738 | mr_free(label);
|
---|
| 739 | for (get_next_raidtab_line(fin, &label, &value); !feof(fin) && strcmp(label, "raiddev"); get_next_raidtab_line(fin, &label, &value)) {
|
---|
[128] | 740 | process_raidtab_line(fin, &raidlist->el[items], label, value);
|
---|
[3566] | 741 | mr_free(label);
|
---|
[128] | 742 | }
|
---|
| 743 | items++;
|
---|
[1] | 744 | }
|
---|
[3566] | 745 | mr_free(label);
|
---|
[128] | 746 | paranoid_fclose(fin);
|
---|
| 747 | raidlist->entries = items;
|
---|
| 748 | log_msg(1, "Raidtab loaded successfully.");
|
---|
| 749 | log_msg(1, "%d RAID devices in raidtab", items);
|
---|
| 750 | return (0);
|
---|
[1] | 751 | }
|
---|
| 752 | #endif
|
---|
| 753 |
|
---|
| 754 |
|
---|
| 755 |
|
---|
| 756 | #ifdef __FreeBSD__
|
---|
| 757 | /**
|
---|
| 758 | * Add a new plex to a volume. The index of the plex will be <tt>v-\>plexes - 1</tt>.
|
---|
| 759 | * @param v The volume to operate on.
|
---|
| 760 | * @param raidlevel The RAID level of the new plex.
|
---|
| 761 | * @param stripesize The stripe size (chunk size) of the new plex.
|
---|
| 762 | */
|
---|
[128] | 763 | void add_plex_to_volume(struct vinum_volume *v, int raidlevel,
|
---|
| 764 | int stripesize)
|
---|
[1] | 765 | {
|
---|
[128] | 766 | v->plex[v->plexes].raidlevel = raidlevel;
|
---|
| 767 | v->plex[v->plexes].stripesize = stripesize;
|
---|
| 768 | v->plex[v->plexes].subdisks = 0;
|
---|
| 769 | ++v->plexes;
|
---|
[1] | 770 | }
|
---|
| 771 |
|
---|
| 772 | /**
|
---|
| 773 | * For internal use only.
|
---|
| 774 | */
|
---|
[128] | 775 | char **get_next_vinum_conf_line(FILE * f, int *argc)
|
---|
[1] | 776 | {
|
---|
[128] | 777 | int cnt = 0;
|
---|
| 778 | static char *argv[64];
|
---|
| 779 | char **ap;
|
---|
[3191] | 780 | char *line = NULL;
|
---|
[3060] | 781 |
|
---|
[3191] | 782 | mr_getline(line, f);
|
---|
| 783 | if (feof(f)) {
|
---|
[128] | 784 | log_it("[GNVCL] Uh... I reached the EOF.");
|
---|
| 785 | return 0;
|
---|
[1] | 786 | }
|
---|
| 787 |
|
---|
[128] | 788 | for (ap = argv; (*ap = strsep(&line, " \t")) != NULL;)
|
---|
| 789 | if (**ap != '\0') {
|
---|
| 790 | if (++ap >= &argv[64])
|
---|
| 791 | break;
|
---|
| 792 | cnt++;
|
---|
| 793 | }
|
---|
[3191] | 794 | mr_free(line);
|
---|
[1] | 795 |
|
---|
[128] | 796 | if (strchr(argv[cnt - 1], '\n')) {
|
---|
| 797 | *(strchr(argv[cnt - 1], '\n')) = '\0';
|
---|
| 798 | }
|
---|
| 799 |
|
---|
| 800 | if (argc)
|
---|
| 801 | *argc = cnt;
|
---|
| 802 | return argv;
|
---|
[1] | 803 | }
|
---|
| 804 |
|
---|
| 805 | /**
|
---|
| 806 | * For internal use only.
|
---|
| 807 | */
|
---|
[128] | 808 | char *get_option_val(int argc, char **argv, char *option)
|
---|
[1] | 809 | {
|
---|
[128] | 810 | int i;
|
---|
| 811 | for (i = 0; i < (argc - 1); ++i) {
|
---|
| 812 | if (!strcmp(argv[i], option)) {
|
---|
| 813 | return argv[i + 1];
|
---|
| 814 | }
|
---|
[1] | 815 | }
|
---|
[128] | 816 | return 0;
|
---|
[1] | 817 | }
|
---|
| 818 |
|
---|
| 819 | /**
|
---|
| 820 | * For internal use only.
|
---|
| 821 | */
|
---|
[128] | 822 | char **get_option_vals(int argc, char **argv, char *option, int nval)
|
---|
[1] | 823 | {
|
---|
[128] | 824 | int i, j;
|
---|
| 825 | static char **ret;
|
---|
| 826 | ret = (char **) malloc(nval * sizeof(char *));
|
---|
| 827 | for (i = 0; i < (argc - nval); ++i) {
|
---|
| 828 | if (!strcmp(argv[i], option)) {
|
---|
| 829 | for (j = 0; j < nval; ++j) {
|
---|
| 830 | ret[j] = (char *) malloc(strlen(argv[i + j + 1]) + 1);
|
---|
| 831 | strcpy(ret[j], argv[i + j + 1]);
|
---|
| 832 | }
|
---|
| 833 | return ret;
|
---|
| 834 | }
|
---|
[1] | 835 | }
|
---|
[128] | 836 | return 0;
|
---|
[1] | 837 | }
|
---|
| 838 |
|
---|
| 839 | /**
|
---|
| 840 | * For internal use only.
|
---|
| 841 | */
|
---|
[128] | 842 | bool get_option_state(int argc, char **argv, char *option)
|
---|
[1] | 843 | {
|
---|
[128] | 844 | int i;
|
---|
| 845 | for (i = 0; i < argc; ++i)
|
---|
| 846 | if (!strcmp(argv[i], option))
|
---|
| 847 | return TRUE;
|
---|
[1] | 848 |
|
---|
[128] | 849 | return FALSE;
|
---|
[1] | 850 | }
|
---|
| 851 |
|
---|
| 852 | /**
|
---|
| 853 | * Taken from Vinum source -- for internal use only.
|
---|
| 854 | */
|
---|
| 855 | long long size_spec(char *spec)
|
---|
| 856 | {
|
---|
[128] | 857 | u_int64_t size;
|
---|
| 858 | char *s;
|
---|
| 859 | int sign = 1; /* -1 if negative */
|
---|
[1] | 860 |
|
---|
[128] | 861 | size = 0;
|
---|
| 862 | if (spec != NULL) { /* we have a parameter */
|
---|
| 863 | s = spec;
|
---|
| 864 | if (*s == '-') { /* negative, */
|
---|
| 865 | sign = -1;
|
---|
| 866 | s++; /* skip */
|
---|
| 867 | }
|
---|
| 868 | if ((*s >= '0') && (*s <= '9')) { /* it's numeric */
|
---|
| 869 | while ((*s >= '0') && (*s <= '9')) /* it's numeric */
|
---|
| 870 | size = size * 10 + *s++ - '0'; /* convert it */
|
---|
| 871 | switch (*s) {
|
---|
| 872 | case '\0':
|
---|
| 873 | return size * sign;
|
---|
[1] | 874 |
|
---|
[128] | 875 | case 'B':
|
---|
| 876 | case 'b':
|
---|
| 877 | case 'S':
|
---|
| 878 | case 's':
|
---|
| 879 | return size * sign * 512;
|
---|
[1] | 880 |
|
---|
[128] | 881 | case 'K':
|
---|
| 882 | case 'k':
|
---|
| 883 | return size * sign * 1024;
|
---|
[1] | 884 |
|
---|
[128] | 885 | case 'M':
|
---|
| 886 | case 'm':
|
---|
| 887 | return size * sign * 1024 * 1024;
|
---|
[1] | 888 |
|
---|
[128] | 889 | case 'G':
|
---|
| 890 | case 'g':
|
---|
| 891 | return size * sign * 1024 * 1024 * 1024;
|
---|
[1] | 892 |
|
---|
[128] | 893 | case 'T':
|
---|
| 894 | case 't':
|
---|
| 895 | log_it
|
---|
| 896 | ("Ok, I'm scared... Someone did a TERABYTE+ size-spec");
|
---|
| 897 | return size * sign * 1024 * 1024 * 1024 * 1024;
|
---|
[1] | 898 |
|
---|
[128] | 899 | case 'P':
|
---|
| 900 | case 'p':
|
---|
| 901 | log_it
|
---|
| 902 | ("If I was scared last time, I'm freaked out now. Someone actually has a PETABYTE?!?!?!?!");
|
---|
| 903 | return size * sign * 1024 * 1024 * 1024 * 1024 * 1024;
|
---|
[1] | 904 |
|
---|
[128] | 905 | case 'E':
|
---|
| 906 | case 'e':
|
---|
| 907 | log_it
|
---|
| 908 | ("Okay, I'm REALLY freaked out. Who could devote a whole EXABYTE to their data?!?!");
|
---|
| 909 | return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
|
---|
| 910 | 1024;
|
---|
[1] | 911 |
|
---|
[128] | 912 | case 'Z':
|
---|
| 913 | case 'z':
|
---|
| 914 | log_it
|
---|
| 915 | ("WHAT!?!? A ZETABYTE!?!? You've GOT to be kidding me!!!");
|
---|
| 916 | return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
|
---|
| 917 | 1024 * 1024;
|
---|
| 918 |
|
---|
| 919 | case 'Y':
|
---|
| 920 | case 'y':
|
---|
| 921 | log_it
|
---|
| 922 | ("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?!?!");
|
---|
| 923 | popup_and_OK
|
---|
[541] | 924 | ("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] | 925 | return size * sign * 1024 * 1024 * 1024 * 1024 * 1024 *
|
---|
| 926 | 1024 * 1024 * 1024;
|
---|
| 927 | }
|
---|
| 928 | }
|
---|
[1] | 929 | }
|
---|
[128] | 930 | return size * sign;
|
---|
[1] | 931 | }
|
---|
| 932 |
|
---|
| 933 | #endif
|
---|
| 934 |
|
---|
| 935 |
|
---|
| 936 |
|
---|
| 937 |
|
---|
[2982] | 938 | int parse_mdstat(char *mdstat_fname, struct raidlist_itself *raidlist, char *device_prefix) {
|
---|
[128] | 939 |
|
---|
[2982] | 940 | const char delims[] = " ";
|
---|
[558] | 941 |
|
---|
[2982] | 942 | FILE *fin = NULL;
|
---|
| 943 | int res = 0, row, i, index_min;
|
---|
| 944 | int v = 0;
|
---|
[3893] | 945 | size_t lastpos = 0;
|
---|
[2982] | 946 | size_t len = 0;
|
---|
| 947 | char *token = NULL;
|
---|
| 948 | char *string = NULL;
|
---|
| 949 | char *cmd = NULL;
|
---|
| 950 | char *pos = NULL;
|
---|
| 951 | char type;
|
---|
| 952 | char *strtmp = NULL;
|
---|
| 953 | char *strtmp2 = NULL;
|
---|
[558] | 954 |
|
---|
[2982] | 955 | // open file
|
---|
| 956 | if (!(fin = fopen(mdstat_fname, "r"))) {
|
---|
[3380] | 957 | log_msg(1, "Could not open %s.", mdstat_fname);
|
---|
[2982] | 958 | return 1;
|
---|
| 959 | }
|
---|
| 960 | // initialise record, build progress and row counters
|
---|
| 961 | raidlist->entries = 0;
|
---|
| 962 | raidlist->el[raidlist->entries].progress = 999;
|
---|
| 963 | row = 1;
|
---|
| 964 | // skip first output row - contains registered RAID levels
|
---|
| 965 | res = getline(&strtmp2, &len, fin);
|
---|
| 966 | mr_free(strtmp2);
|
---|
| 967 | // parse the rest
|
---|
| 968 | while ( !feof_unlocked(fin) ) {
|
---|
| 969 | res = getline(&string, &len, fin);
|
---|
[3380] | 970 | log_msg(8, "mdstat line '%s' read.", string);
|
---|
[2982] | 971 | if (res <= 0) break;
|
---|
| 972 | // trim spaces
|
---|
| 973 | strip_spaces(string);
|
---|
[3380] | 974 | log_msg(8, "mdstat line 2 '%s' read.", string);
|
---|
[2982] | 975 | // if we have newline after only spaces, this is a blank line, update
|
---|
| 976 | // counters, otherwise do normal parsing
|
---|
| 977 | if (!strcmp(string,"")) {
|
---|
| 978 | row = 1;
|
---|
| 979 | raidlist->entries++;
|
---|
| 980 | raidlist->el[raidlist->entries].progress = 999;
|
---|
[558] | 981 | } else {
|
---|
[2982] | 982 | switch (row) {
|
---|
| 983 | case 1: // device information
|
---|
| 984 | // check whether last line of record and if so skip
|
---|
[3380] | 985 | log_msg(8, "This is the device line");
|
---|
[2982] | 986 | pos = strcasestr(string, "unused devices: ");
|
---|
| 987 | if (pos != NULL) {
|
---|
| 988 | break;
|
---|
| 989 | }
|
---|
| 990 | // tokenise string
|
---|
| 991 | token = mr_strtok(string, delims, &lastpos);
|
---|
| 992 | if (token == NULL) {
|
---|
| 993 | // should not happen !
|
---|
| 994 | break;
|
---|
| 995 | }
|
---|
| 996 | // get RAID device name
|
---|
[3185] | 997 | mr_asprintf(strtmp,"%s%s", device_prefix, token);
|
---|
[2982] | 998 | strncpy(raidlist->el[raidlist->entries].raid_device, strtmp, 63);
|
---|
[3060] | 999 | raidlist->el[raidlist->entries].raid_device[63] = '\0';
|
---|
[2982] | 1000 | mr_free(strtmp);
|
---|
| 1001 | mr_free(token);
|
---|
| 1002 | // store the UUID value in the additional_vars structure
|
---|
| 1003 | v = raidlist->el[raidlist->entries].additional_vars.entries;
|
---|
| 1004 | strcpy(raidlist->el[raidlist->entries].additional_vars.el[v].label, "UUID");
|
---|
[3185] | 1005 | mr_asprintf(cmd,"mdadm --detail %s | grep UUID | cut -d: -f2- | sed 's/^ *//'", raidlist->el[raidlist->entries].raid_device);
|
---|
[3610] | 1006 | strtmp = call_program_and_get_last_line_of_output(cmd);
|
---|
[2982] | 1007 | strcpy(raidlist->el[raidlist->entries].additional_vars.el[v].value, strtmp);
|
---|
| 1008 | mr_free(strtmp);
|
---|
| 1009 | v++;
|
---|
[2989] | 1010 | // store the Version value in the additional_vars structure
|
---|
| 1011 | strcpy(raidlist->el[raidlist->entries].additional_vars.el[v].label, "Version");
|
---|
[3185] | 1012 | mr_asprintf(cmd,"mdadm --detail %s | grep Version | cut -d: -f2- | sed 's/^ *//'", raidlist->el[raidlist->entries].raid_device);
|
---|
[3610] | 1013 | strtmp = call_program_and_get_last_line_of_output(cmd);
|
---|
[2989] | 1014 | strcpy(raidlist->el[raidlist->entries].additional_vars.el[v].value, strtmp);
|
---|
| 1015 | mr_free(strtmp);
|
---|
| 1016 | v++;
|
---|
[2982] | 1017 | raidlist->el[raidlist->entries].additional_vars.entries = v;
|
---|
| 1018 | // skip ':' and status
|
---|
[3154] | 1019 | token = mr_strtok(string, delims, &lastpos);
|
---|
[2982] | 1020 | if (token == NULL) {
|
---|
| 1021 | // should not happen !
|
---|
| 1022 | break;
|
---|
| 1023 | }
|
---|
| 1024 | mr_free(token);
|
---|
[3154] | 1025 | token = mr_strtok(string, delims, &lastpos);
|
---|
[2982] | 1026 | if (token == NULL) {
|
---|
| 1027 | // should not happen !
|
---|
| 1028 | break;
|
---|
| 1029 | }
|
---|
| 1030 | if (!strcmp(token, "inactive")) {
|
---|
[3380] | 1031 | log_msg(1, "RAID device '%s' inactive.", raidlist->el[raidlist->entries].raid_device);
|
---|
[2982] | 1032 | mr_free(string);
|
---|
| 1033 | mr_free(token);
|
---|
| 1034 | return 1;
|
---|
| 1035 | }
|
---|
| 1036 | mr_free(token);
|
---|
[559] | 1037 |
|
---|
[2982] | 1038 | // get RAID level
|
---|
[3154] | 1039 | token = mr_strtok(string, delims, &lastpos);
|
---|
[2982] | 1040 | if (token == NULL) {
|
---|
| 1041 | // should not happen !
|
---|
| 1042 | break;
|
---|
| 1043 | }
|
---|
| 1044 | // skip potential auto-read-only entry
|
---|
| 1045 | if (!strcmp(token, "(auto-read-only)")) {
|
---|
| 1046 | mr_free(token);
|
---|
| 1047 | token = mr_strtok (string, delims, &lastpos);
|
---|
| 1048 | if (token == NULL) {
|
---|
| 1049 | // should not happen !
|
---|
| 1050 | break;
|
---|
| 1051 | }
|
---|
| 1052 | }
|
---|
| 1053 | if (!strcmp(token, "multipath")) {
|
---|
| 1054 | raidlist->el[raidlist->entries].raid_level = -2;
|
---|
| 1055 | } else if (!strcmp(token, "linear")) {
|
---|
| 1056 | raidlist->el[raidlist->entries].raid_level = -1;
|
---|
| 1057 | } else if (!strcmp(token, "raid0")) {
|
---|
| 1058 | raidlist->el[raidlist->entries].raid_level = 0;
|
---|
| 1059 | } else if (!strcmp(token, "raid1")) {
|
---|
| 1060 | raidlist->el[raidlist->entries].raid_level = 1;
|
---|
| 1061 | } else if (!strcmp(token, "raid4")) {
|
---|
| 1062 | raidlist->el[raidlist->entries].raid_level = 4;
|
---|
| 1063 | } else if (!strcmp(token, "raid5")) {
|
---|
| 1064 | raidlist->el[raidlist->entries].raid_level = 5;
|
---|
| 1065 | } else if (!strcmp(token, "raid6")) {
|
---|
| 1066 | raidlist->el[raidlist->entries].raid_level = 6;
|
---|
| 1067 | } else if (!strcmp(token, "raid10")) {
|
---|
| 1068 | raidlist->el[raidlist->entries].raid_level = 10;
|
---|
| 1069 | } else {
|
---|
[3380] | 1070 | log_msg(1, "Unknown RAID level '%s'", token);
|
---|
[2982] | 1071 | mr_free(string);
|
---|
| 1072 | mr_free(token);
|
---|
| 1073 | return 1;
|
---|
| 1074 | }
|
---|
| 1075 | mr_free(token);
|
---|
[559] | 1076 |
|
---|
[2982] | 1077 | // get RAID devices (type, index, device)
|
---|
| 1078 | // Note: parity disk for RAID4 is last normal disk, there is no '(P)'
|
---|
| 1079 | raidlist->el[raidlist->entries].data_disks.entries = 0;
|
---|
| 1080 | raidlist->el[raidlist->entries].spare_disks.entries = 0;
|
---|
| 1081 | raidlist->el[raidlist->entries].failed_disks.entries = 0;
|
---|
| 1082 | while((token = mr_strtok (string, delims, &lastpos))) {
|
---|
| 1083 | if ((pos = strstr(token, "("))) {
|
---|
[3191] | 1084 | type = *(pos+1);
|
---|
[2982] | 1085 | } else {
|
---|
[3191] | 1086 | type = ' ';
|
---|
[2982] | 1087 | }
|
---|
| 1088 | pos = strstr(token, "[");
|
---|
| 1089 | *pos = '\0';
|
---|
| 1090 | switch(type) {
|
---|
| 1091 | case ' ': // normal data disks
|
---|
[3191] | 1092 | raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].index = atoi(pos + 1);
|
---|
| 1093 | mr_asprintf(strtmp,"%s%s", device_prefix, token);
|
---|
| 1094 | strcpy(raidlist->el[raidlist->entries].data_disks.el[raidlist->el[raidlist->entries].data_disks.entries].device, strtmp);
|
---|
| 1095 | mr_free(strtmp);
|
---|
| 1096 | raidlist->el[raidlist->entries].data_disks.entries++;
|
---|
| 1097 | break;
|
---|
[2982] | 1098 | case 'S': // spare disks
|
---|
[3191] | 1099 | raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].index = atoi(pos + 1);
|
---|
| 1100 | mr_asprintf(strtmp,"%s%s", device_prefix, token);
|
---|
| 1101 | strcpy(raidlist->el[raidlist->entries].spare_disks.el[raidlist->el[raidlist->entries].spare_disks.entries].device, strtmp);
|
---|
| 1102 | mr_free(strtmp);
|
---|
| 1103 | raidlist->el[raidlist->entries].spare_disks.entries++;
|
---|
| 1104 | break;
|
---|
[2982] | 1105 | case 'F': // failed disks
|
---|
[3191] | 1106 | raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].index = atoi(pos + 1);
|
---|
| 1107 | mr_asprintf(strtmp,"%s%s", device_prefix, token);
|
---|
| 1108 | strcpy(raidlist->el[raidlist->entries].failed_disks.el[raidlist->el[raidlist->entries].failed_disks.entries].device, strtmp);
|
---|
| 1109 | mr_free(strtmp);
|
---|
| 1110 | raidlist->el[raidlist->entries].failed_disks.entries++;
|
---|
| 1111 | log_it("At least one failed disk found in RAID array.\n");
|
---|
| 1112 | break;
|
---|
[2982] | 1113 | default: // error
|
---|
[3380] | 1114 | log_msg(1, "Unknown device type '%c'", type);
|
---|
[3191] | 1115 | mr_free(string);
|
---|
| 1116 | mr_free(token);
|
---|
| 1117 | return 1;
|
---|
| 1118 | break;
|
---|
[2982] | 1119 | }
|
---|
| 1120 | mr_free(token);
|
---|
| 1121 | }
|
---|
| 1122 |
|
---|
| 1123 | // adjust index for each device so that it starts with 0 for every type
|
---|
| 1124 | index_min = 99;
|
---|
| 1125 | for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
|
---|
| 1126 | if (raidlist->el[raidlist->entries].data_disks.el[i].index < index_min) {
|
---|
[3191] | 1127 | index_min = raidlist->el[raidlist->entries].data_disks.el[i].index;
|
---|
[2982] | 1128 | }
|
---|
| 1129 | }
|
---|
| 1130 | if (index_min > 0) {
|
---|
| 1131 | for (i=0; i<raidlist->el[raidlist->entries].data_disks.entries;i++) {
|
---|
[3191] | 1132 | raidlist->el[raidlist->entries].data_disks.el[i].index = raidlist->el[raidlist->entries].data_disks.el[i].index - index_min;
|
---|
[2982] | 1133 | }
|
---|
| 1134 | }
|
---|
| 1135 | index_min = 99;
|
---|
| 1136 | for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
|
---|
| 1137 | if (raidlist->el[raidlist->entries].spare_disks.el[i].index < index_min) {
|
---|
[3191] | 1138 | index_min = raidlist->el[raidlist->entries].spare_disks.el[i].index;
|
---|
[2982] | 1139 | }
|
---|
| 1140 | }
|
---|
| 1141 | if (index_min > 0) {
|
---|
| 1142 | for (i=0; i<raidlist->el[raidlist->entries].spare_disks.entries;i++) {
|
---|
[3191] | 1143 | raidlist->el[raidlist->entries].spare_disks.el[i].index = raidlist->el[raidlist->entries].spare_disks.el[i].index - index_min;
|
---|
[2982] | 1144 | }
|
---|
| 1145 | }
|
---|
| 1146 | index_min = 99;
|
---|
| 1147 | for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
|
---|
| 1148 | if (raidlist->el[raidlist->entries].failed_disks.el[i].index < index_min) {
|
---|
[3191] | 1149 | index_min = raidlist->el[raidlist->entries].failed_disks.el[i].index;
|
---|
[2982] | 1150 | }
|
---|
| 1151 | }
|
---|
| 1152 | if (index_min > 0) {
|
---|
| 1153 | for (i=0; i<raidlist->el[raidlist->entries].failed_disks.entries;i++) {
|
---|
[3191] | 1154 | raidlist->el[raidlist->entries].failed_disks.el[i].index = raidlist->el[raidlist->entries].failed_disks.el[i].index - index_min;
|
---|
[2982] | 1155 | }
|
---|
| 1156 | }
|
---|
| 1157 | break;
|
---|
| 1158 | case 2: // config information
|
---|
| 1159 | // check for persistent super block
|
---|
| 1160 | if (strcasestr(string, "super non-persistent")) {
|
---|
| 1161 | raidlist->el[raidlist->entries].persistent_superblock = 0;
|
---|
| 1162 | } else {
|
---|
| 1163 | raidlist->el[raidlist->entries].persistent_superblock = 1;
|
---|
| 1164 | }
|
---|
| 1165 | // extract chunk size
|
---|
| 1166 | if (!(pos = strcasestr(string, "k chunk"))) {
|
---|
| 1167 | raidlist->el[raidlist->entries].chunk_size = -1;
|
---|
| 1168 | } else {
|
---|
| 1169 | while (*pos != ' ') {
|
---|
[3191] | 1170 | pos -= 1;
|
---|
| 1171 | if (pos < string) {
|
---|
| 1172 | log_it("String underflow!\n");
|
---|
| 1173 | mr_free(string);
|
---|
| 1174 | return 1;
|
---|
| 1175 | }
|
---|
[2982] | 1176 | }
|
---|
| 1177 | raidlist->el[raidlist->entries].chunk_size = atoi(pos + 1);
|
---|
| 1178 | }
|
---|
| 1179 | // extract parity if present
|
---|
| 1180 | if ((pos = strcasestr(string, "algorithm"))) {
|
---|
| 1181 | raidlist->el[raidlist->entries].parity = atoi(pos + 9);
|
---|
| 1182 | } else {
|
---|
| 1183 | raidlist->el[raidlist->entries].parity = -1;
|
---|
| 1184 | }
|
---|
| 1185 | break;
|
---|
| 1186 | case 3: // optional build status information
|
---|
| 1187 | if (!(pos = strchr(string, '\%'))) {
|
---|
| 1188 | if (strcasestr(string, "delayed")) {
|
---|
[3191] | 1189 | raidlist->el[raidlist->entries].progress = -1; // delayed (therefore, stuck at 0%)
|
---|
[2982] | 1190 | } else {
|
---|
[3191] | 1191 | raidlist->el[raidlist->entries].progress = 999; // not found
|
---|
[2982] | 1192 | }
|
---|
| 1193 | } else {
|
---|
| 1194 | while (*pos != ' ') {
|
---|
[3191] | 1195 | pos -= 1;
|
---|
| 1196 | if (pos < string) {
|
---|
| 1197 | printf("ERROR: String underflow!\n");
|
---|
| 1198 | mr_free(string);
|
---|
| 1199 | return 1;
|
---|
| 1200 | }
|
---|
[2982] | 1201 | }
|
---|
| 1202 | raidlist->el[raidlist->entries].progress = atoi(pos);
|
---|
| 1203 | }
|
---|
| 1204 | break;
|
---|
[3191] | 1205 | default: // error or IN PROGRESS
|
---|
[2982] | 1206 | if (raidlist->el[raidlist->entries].progress != -1 &&
|
---|
| 1207 | raidlist->el[raidlist->entries].progress != 999) {
|
---|
[3380] | 1208 | log_msg(1, "Row %d should not occur in record!", row);
|
---|
[2982] | 1209 | }
|
---|
| 1210 | break;
|
---|
[3191] | 1211 | }
|
---|
| 1212 | row++;
|
---|
[558] | 1213 | }
|
---|
[2982] | 1214 | // free string
|
---|
| 1215 | mr_free(string);
|
---|
| 1216 | }
|
---|
| 1217 | // close file
|
---|
| 1218 | fclose(fin);
|
---|
| 1219 | // return success
|
---|
| 1220 | return 0;
|
---|
[558] | 1221 |
|
---|
[1] | 1222 | }
|
---|
| 1223 |
|
---|
| 1224 |
|
---|
| 1225 |
|
---|
[558] | 1226 |
|
---|
[2982] | 1227 | int create_raidtab_from_mdstat(char *mdstat_fname,char *raidtab_fname)
|
---|
[1] | 1228 | {
|
---|
[128] | 1229 | struct raidlist_itself *raidlist;
|
---|
| 1230 | int retval = 0;
|
---|
[1] | 1231 |
|
---|
[128] | 1232 | raidlist = malloc(sizeof(struct raidlist_itself));
|
---|
[1] | 1233 |
|
---|
[558] | 1234 | // FIXME: Prefix '/dev/' should really be dynamic!
|
---|
[2982] | 1235 | if (parse_mdstat(mdstat_fname,raidlist, "/dev/")) {
|
---|
| 1236 | log_to_screen("Sorry, cannot read %s", mdstat_fname);
|
---|
[128] | 1237 | return (1);
|
---|
| 1238 | }
|
---|
| 1239 |
|
---|
| 1240 | retval += save_raidlist_to_raidtab(raidlist, raidtab_fname);
|
---|
| 1241 | return (retval);
|
---|
[1] | 1242 | }
|
---|
| 1243 |
|
---|
| 1244 |
|
---|
| 1245 |
|
---|
| 1246 | /* @} - end of raidGroup */
|
---|