1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
|
/* lvm.c - module to read Logical Volumes. */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2006,2007,2008,2009,2011 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/dl.h>
#include <grub/disk.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/misc.h>
#include <grub/lvm.h>
#include <grub/partition.h>
#include <grub/i18n.h>
#include <grub/safemath.h>
#ifdef GRUB_UTIL
#include <grub/emu/misc.h>
#include <grub/emu/hostdisk.h>
#endif
GRUB_MOD_LICENSE ("GPLv3+");
struct ignored_feature_lv
{
struct grub_diskfilter_lv *lv;
char *origin;
struct ignored_feature_lv *next;
};
/* Go the string STR and return the number after STR. *P will point
at the number. In case STR is not found, *P will be NULL and the
return value will be 0. */
static grub_uint64_t
grub_lvm_getvalue (const char ** const p, const char *str)
{
*p = grub_strstr (*p, str);
if (! *p)
return 0;
*p += grub_strlen (str);
return grub_strtoull (*p, p, 10);
}
#if 0
static int
grub_lvm_checkvalue (char **p, char *str, char *tmpl)
{
int tmpllen = grub_strlen (tmpl);
*p = grub_strstr (*p, str);
if (! *p)
return 0;
*p += grub_strlen (str);
if (**p != '"')
return 0;
return (grub_memcmp (*p + 1, tmpl, tmpllen) == 0 && (*p)[tmpllen + 1] == '"');
}
#endif
static int
grub_lvm_check_flag (const char *p, const char *str, const char *flag)
{
grub_size_t len_str = grub_strlen (str), len_flag = grub_strlen (flag);
while (1)
{
const char *q;
p = grub_strstr (p, str);
if (! p)
return 0;
p += len_str;
if (grub_memcmp (p, " = [", sizeof (" = [") - 1) != 0)
continue;
q = p + sizeof (" = [") - 1;
while (1)
{
while (grub_isspace (*q))
q++;
if (*q != '"')
return 0;
q++;
if (grub_memcmp (q, flag, len_flag) == 0 && q[len_flag] == '"')
return 1;
while (*q != '"')
q++;
q++;
if (*q == ']')
return 0;
q++;
}
}
}
static void
grub_lvm_free_ignored_feature_lvs (struct ignored_feature_lv *ignored_feature_lvs)
{
struct ignored_feature_lv *ignored_feature;
while ((ignored_feature = ignored_feature_lvs))
{
ignored_feature_lvs = ignored_feature_lvs->next;
if (ignored_feature->lv)
{
unsigned int i;
for (i = 0; i < ignored_feature->lv->segment_count; ++i)
if (ignored_feature->lv->segments)
grub_free (ignored_feature->lv->segments[i].nodes);
grub_free (ignored_feature->lv->segments);
grub_free (ignored_feature->lv->fullname);
grub_free (ignored_feature->lv->idname);
grub_free (ignored_feature->lv->name);
}
grub_free (ignored_feature->lv);
grub_free (ignored_feature->origin);
grub_free (ignored_feature);
}
}
static struct grub_diskfilter_vg *
grub_lvm_detect (grub_disk_t disk,
struct grub_diskfilter_pv_id *id,
grub_disk_addr_t *start_sector)
{
grub_err_t err;
grub_uint64_t mda_offset, mda_size;
grub_size_t ptr;
char buf[GRUB_LVM_LABEL_SIZE];
char vg_id[GRUB_LVM_ID_STRLEN+1];
char pv_id[GRUB_LVM_ID_STRLEN+1];
char *metadatabuf, *mda_end, *vgname;
const char *p, *q;
struct grub_lvm_label_header *lh = (struct grub_lvm_label_header *) buf;
struct grub_lvm_pv_header *pvh;
struct grub_lvm_disk_locn *dlocn;
struct grub_lvm_mda_header *mdah;
struct grub_lvm_raw_locn *rlocn;
unsigned int i, j;
grub_size_t vgname_len;
struct grub_diskfilter_vg *vg;
struct grub_diskfilter_pv *pv;
/* Search for label. */
for (i = 0; i < GRUB_LVM_LABEL_SCAN_SECTORS; i++)
{
err = grub_disk_read (disk, i, 0, sizeof(buf), buf);
if (err)
goto fail;
if ((! grub_strncmp ((char *)lh->id, GRUB_LVM_LABEL_ID,
sizeof (lh->id)))
&& (! grub_strncmp ((char *)lh->type, GRUB_LVM_LVM2_LABEL,
sizeof (lh->type))))
break;
}
/* Return if we didn't find a label. */
if (i == GRUB_LVM_LABEL_SCAN_SECTORS)
{
#ifdef GRUB_UTIL
grub_util_info ("no LVM signature found");
#endif
goto fail;
}
/*
* We read a grub_lvm_pv_header and then 2 grub_lvm_disk_locns that
* immediately follow the PV header. Make sure we have space for both.
*/
if (grub_le_to_cpu32 (lh->offset_xl) >=
GRUB_LVM_LABEL_SIZE - sizeof (struct grub_lvm_pv_header) -
2 * sizeof (struct grub_lvm_disk_locn))
{
#ifdef GRUB_UTIL
grub_util_info ("LVM PV header/disk locations are beyond the end of the block");
#endif
goto fail;
}
pvh = (struct grub_lvm_pv_header *) (buf + grub_le_to_cpu32(lh->offset_xl));
for (i = 0, j = 0; i < GRUB_LVM_ID_LEN; i++)
{
pv_id[j++] = pvh->pv_uuid[i];
if ((i != 1) && (i != 29) && (i % 4 == 1))
pv_id[j++] = '-';
}
pv_id[j] = '\0';
dlocn = pvh->disk_areas_xl;
dlocn++;
/* Is it possible to have multiple data/metadata areas? I haven't
seen devices that have it. */
if (dlocn->offset)
{
grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
"we don't support multiple LVM data areas");
#ifdef GRUB_UTIL
grub_util_info ("we don't support multiple LVM data areas");
#endif
goto fail;
}
dlocn++;
mda_offset = grub_le_to_cpu64 (dlocn->offset);
mda_size = grub_le_to_cpu64 (dlocn->size);
/* It's possible to have multiple copies of metadata areas, we just use the
first one. */
/* Allocate buffer space for the circular worst-case scenario. */
metadatabuf = grub_calloc (2, mda_size);
if (! metadatabuf)
goto fail;
err = grub_disk_read (disk, 0, mda_offset, mda_size, metadatabuf);
if (err)
goto fail2;
mdah = (struct grub_lvm_mda_header *) metadatabuf;
if ((grub_strncmp ((char *)mdah->magic, GRUB_LVM_FMTT_MAGIC,
sizeof (mdah->magic)))
|| (grub_le_to_cpu32 (mdah->version) != GRUB_LVM_FMTT_VERSION))
{
grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
"unknown LVM metadata header");
#ifdef GRUB_UTIL
grub_util_info ("unknown LVM metadata header");
#endif
goto fail2;
}
rlocn = mdah->raw_locns;
if (grub_le_to_cpu64 (rlocn->offset) >= grub_le_to_cpu64 (mda_size))
{
#ifdef GRUB_UTIL
grub_util_info ("metadata offset is beyond end of metadata area");
#endif
goto fail2;
}
if (grub_le_to_cpu64 (rlocn->offset) + grub_le_to_cpu64 (rlocn->size) >
grub_le_to_cpu64 (mdah->size))
{
if (2 * mda_size < GRUB_LVM_MDA_HEADER_SIZE ||
(grub_le_to_cpu64 (rlocn->offset) + grub_le_to_cpu64 (rlocn->size) -
grub_le_to_cpu64 (mdah->size) > mda_size - GRUB_LVM_MDA_HEADER_SIZE))
{
#ifdef GRUB_UTIL
grub_util_info ("cannot copy metadata wrap in circular buffer");
#endif
goto fail2;
}
/* Metadata is circular. Copy the wrap in place. */
grub_memcpy (metadatabuf + mda_size,
metadatabuf + GRUB_LVM_MDA_HEADER_SIZE,
grub_le_to_cpu64 (rlocn->offset) +
grub_le_to_cpu64 (rlocn->size) -
grub_le_to_cpu64 (mdah->size));
}
if (grub_add ((grub_size_t)metadatabuf,
(grub_size_t)grub_le_to_cpu64 (rlocn->offset),
&ptr))
{
error_parsing_metadata:
#ifdef GRUB_UTIL
grub_util_info ("error parsing metadata");
#endif
goto fail2;
}
p = q = (char *)ptr;
if (grub_add (ptr, (grub_size_t) grub_le_to_cpu64 (rlocn->size), &ptr))
goto error_parsing_metadata;
mda_end = (char *)ptr;
while (*q != ' ' && q < mda_end)
q++;
if (q == mda_end)
goto error_parsing_metadata;
vgname_len = q - p;
vgname = grub_malloc (vgname_len + 1);
if (!vgname)
goto fail2;
grub_memcpy (vgname, p, vgname_len);
vgname[vgname_len] = '\0';
p = grub_strstr (q, "id = \"");
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("couldn't find ID");
#endif
goto fail3;
}
p += sizeof ("id = \"") - 1;
grub_memcpy (vg_id, p, GRUB_LVM_ID_STRLEN);
vg_id[GRUB_LVM_ID_STRLEN] = '\0';
vg = grub_diskfilter_get_vg_by_uuid (GRUB_LVM_ID_STRLEN, vg_id);
if (! vg)
{
struct ignored_feature_lv *ignored_feature_lvs = NULL;
/* First time we see this volume group. We've to create the
whole volume group structure. */
vg = grub_malloc (sizeof (*vg));
if (! vg)
goto fail3;
vg->name = vgname;
vg->uuid = grub_malloc (GRUB_LVM_ID_STRLEN);
if (! vg->uuid)
goto fail3;
grub_memcpy (vg->uuid, vg_id, GRUB_LVM_ID_STRLEN);
vg->uuid_len = GRUB_LVM_ID_STRLEN;
vg->extent_size = grub_lvm_getvalue (&p, "extent_size = ");
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("unknown extent size");
#endif
goto fail4;
}
vg->lvs = NULL;
vg->pvs = NULL;
p = grub_strstr (p, "physical_volumes {");
if (p)
{
p += sizeof ("physical_volumes {") - 1;
/* Add all the pvs to the volume group. */
while (1)
{
grub_ssize_t s;
while (grub_isspace (*p) && p < mda_end)
p++;
if (p == mda_end)
goto fail4;
if (*p == '}')
break;
pv = grub_zalloc (sizeof (*pv));
if (pv == NULL)
goto fail4;
q = p;
while (*q != ' ' && q < mda_end)
q++;
if (q == mda_end)
goto pvs_fail_noname;
s = q - p;
pv->name = grub_malloc (s + 1);
if (pv->name == NULL)
goto pvs_fail_noname;
grub_memcpy (pv->name, p, s);
pv->name[s] = '\0';
p = grub_strstr (p, "id = \"");
if (p == NULL)
goto pvs_fail;
p += sizeof("id = \"") - 1;
pv->id.uuid = grub_malloc (GRUB_LVM_ID_STRLEN);
if (!pv->id.uuid)
goto pvs_fail;
grub_memcpy (pv->id.uuid, p, GRUB_LVM_ID_STRLEN);
pv->id.uuidlen = GRUB_LVM_ID_STRLEN;
pv->start_sector = grub_lvm_getvalue (&p, "pe_start = ");
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("unknown pe_start");
#endif
goto pvs_fail;
}
p = grub_strchr (p, '}');
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("error parsing pe_start");
#endif
goto pvs_fail;
}
p++;
pv->disk = NULL;
pv->next = vg->pvs;
vg->pvs = pv;
continue;
pvs_fail:
grub_free (pv->name);
pvs_fail_noname:
grub_free (pv);
goto fail4;
}
}
else
goto fail4;
p = grub_strstr (p, "logical_volumes {");
if (p)
{
p += sizeof ("logical_volumes {") - 1;
/* And add all the lvs to the volume group. */
while (1)
{
grub_ssize_t s;
int skip_lv = 0;
struct grub_diskfilter_lv *lv;
struct grub_diskfilter_segment *seg;
int is_pvmove;
while (grub_isspace (*p) && p < mda_end)
p++;
if (p == mda_end)
goto fail4;
if (*p == '}')
break;
lv = grub_zalloc (sizeof (*lv));
if (lv == NULL)
goto fail4;
q = p;
while (*q != ' ' && q < mda_end)
q++;
if (q == mda_end)
goto lvs_fail;
s = q - p;
lv->name = grub_strndup (p, s);
if (!lv->name)
goto lvs_fail;
{
const char *iptr;
char *optr;
/*
* This is kind of hard to read with our safe (but rather
* baroque) math primatives, but it boils down to:
*
* sz0 = vgname_len * 2 + 1 +
* s * 2 + 1 +
* sizeof ("lvm/") - 1;
*/
grub_size_t sz0 = vgname_len, sz1 = s;
if (grub_mul (sz0, 2, &sz0) ||
grub_add (sz0, 1, &sz0) ||
grub_mul (sz1, 2, &sz1) ||
grub_add (sz1, 1, &sz1) ||
grub_add (sz0, sz1, &sz0) ||
grub_add (sz0, sizeof ("lvm/") - 1, &sz0))
goto lvs_fail;
lv->fullname = grub_malloc (sz0);
if (!lv->fullname)
goto lvs_fail;
grub_memcpy (lv->fullname, "lvm/", sizeof ("lvm/") - 1);
optr = lv->fullname + sizeof ("lvm/") - 1;
for (iptr = vgname; iptr < vgname + vgname_len; iptr++)
{
*optr++ = *iptr;
if (*iptr == '-')
*optr++ = '-';
}
*optr++ = '-';
for (iptr = p; iptr < p + s; iptr++)
{
*optr++ = *iptr;
if (*iptr == '-')
*optr++ = '-';
}
*optr++ = 0;
lv->idname = grub_malloc (sizeof ("lvmid/")
+ 2 * GRUB_LVM_ID_STRLEN + 1);
if (!lv->idname)
goto lvs_fail;
grub_memcpy (lv->idname, "lvmid/",
sizeof ("lvmid/") - 1);
grub_memcpy (lv->idname + sizeof ("lvmid/") - 1,
vg_id, GRUB_LVM_ID_STRLEN);
lv->idname[sizeof ("lvmid/") - 1 + GRUB_LVM_ID_STRLEN] = '/';
p = grub_strstr (q, "id = \"");
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("couldn't find ID");
#endif
goto lvs_fail;
}
p += sizeof ("id = \"") - 1;
grub_memcpy (lv->idname + sizeof ("lvmid/") - 1
+ GRUB_LVM_ID_STRLEN + 1,
p, GRUB_LVM_ID_STRLEN);
lv->idname[sizeof ("lvmid/") - 1 + 2 * GRUB_LVM_ID_STRLEN + 1] = '\0';
}
lv->size = 0;
lv->visible = grub_lvm_check_flag (p, "status", "VISIBLE");
is_pvmove = grub_lvm_check_flag (p, "status", "PVMOVE");
lv->segment_count = grub_lvm_getvalue (&p, "segment_count = ");
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("unknown segment_count");
#endif
goto lvs_fail;
}
lv->segments = grub_calloc (lv->segment_count, sizeof (*seg));
if (lv->segments == NULL)
goto lvs_fail;
seg = lv->segments;
for (i = 0; i < lv->segment_count; i++)
{
p = grub_strstr (p, "segment");
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("unknown segment");
#endif
goto lvs_segment_fail;
}
seg->start_extent = grub_lvm_getvalue (&p, "start_extent = ");
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("unknown start_extent");
#endif
goto lvs_segment_fail;
}
seg->extent_count = grub_lvm_getvalue (&p, "extent_count = ");
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("unknown extent_count");
#endif
goto lvs_segment_fail;
}
p = grub_strstr (p, "type = \"");
if (p == NULL)
goto lvs_segment_fail;
p += sizeof("type = \"") - 1;
lv->size += seg->extent_count * vg->extent_size;
if (grub_memcmp (p, "striped\"",
sizeof ("striped\"") - 1) == 0)
{
struct grub_diskfilter_node *stripe;
seg->type = GRUB_DISKFILTER_STRIPED;
seg->node_count = grub_lvm_getvalue (&p, "stripe_count = ");
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("unknown stripe_count");
#endif
goto lvs_segment_fail;
}
if (seg->node_count != 1)
{
seg->stripe_size = grub_lvm_getvalue (&p, "stripe_size = ");
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("unknown stripe_size");
#endif
goto lvs_segment_fail;
}
}
seg->nodes = grub_calloc (seg->node_count,
sizeof (*stripe));
if (seg->nodes == NULL)
goto lvs_segment_fail;
stripe = seg->nodes;
p = grub_strstr (p, "stripes = [");
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("unknown stripes");
#endif
goto lvs_segment_fail2;
}
p += sizeof("stripes = [") - 1;
for (j = 0; j < seg->node_count; j++)
{
p = grub_strchr (p, '"');
if (p == NULL)
goto lvs_segment_fail2;
q = ++p;
while (q < mda_end && *q != '"')
q++;
if (q == mda_end)
goto lvs_segment_fail2;
s = q - p;
stripe->name = grub_malloc (s + 1);
if (stripe->name == NULL)
goto lvs_segment_fail2;
grub_memcpy (stripe->name, p, s);
stripe->name[s] = '\0';
p = q + 1;
stripe->start = grub_lvm_getvalue (&p, ",")
* vg->extent_size;
if (p == NULL)
{
grub_free (stripe->name);
goto lvs_segment_fail2;
}
stripe++;
}
}
else if (grub_memcmp (p, "mirror\"", sizeof ("mirror\"") - 1)
== 0)
{
seg->type = GRUB_DISKFILTER_MIRROR;
seg->node_count = grub_lvm_getvalue (&p, "mirror_count = ");
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("unknown mirror_count");
#endif
goto lvs_segment_fail;
}
seg->nodes = grub_calloc (seg->node_count, sizeof (seg->nodes[0]));
if (seg->nodes == NULL)
goto lvs_segment_fail;
p = grub_strstr (p, "mirrors = [");
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("unknown mirrors");
#endif
goto lvs_segment_fail2;
}
p += sizeof("mirrors = [") - 1;
for (j = 0; j < seg->node_count; j++)
{
char *lvname;
p = grub_strchr (p, '"');
if (p == NULL)
goto lvs_segment_fail2;
q = ++p;
while (q < mda_end && *q != '"')
q++;
if (q == mda_end)
goto lvs_segment_fail2;
s = q - p;
lvname = grub_malloc (s + 1);
if (lvname == NULL)
goto lvs_segment_fail2;
grub_memcpy (lvname, p, s);
lvname[s] = '\0';
seg->nodes[j].name = lvname;
p = q + 1;
}
/* Only first (original) is ok with in progress pvmove. */
if (is_pvmove)
seg->node_count = 1;
}
else if (grub_memcmp (p, "raid", sizeof ("raid") - 1) == 0
&& ((p[sizeof ("raid") - 1] >= '4'
&& p[sizeof ("raid") - 1] <= '6')
|| p[sizeof ("raid") - 1] == '1')
&& p[sizeof ("raidX") - 1] == '"')
{
switch (p[sizeof ("raid") - 1])
{
case '1':
seg->type = GRUB_DISKFILTER_MIRROR;
break;
case '4':
seg->type = GRUB_DISKFILTER_RAID4;
seg->layout = GRUB_RAID_LAYOUT_LEFT_ASYMMETRIC;
break;
case '5':
seg->type = GRUB_DISKFILTER_RAID5;
seg->layout = GRUB_RAID_LAYOUT_LEFT_SYMMETRIC;
break;
case '6':
seg->type = GRUB_DISKFILTER_RAID6;
seg->layout = (GRUB_RAID_LAYOUT_RIGHT_ASYMMETRIC
| GRUB_RAID_LAYOUT_MUL_FROM_POS);
break;
}
seg->node_count = grub_lvm_getvalue (&p, "device_count = ");
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("unknown device_count");
#endif
goto lvs_segment_fail;
}
if (seg->type != GRUB_DISKFILTER_MIRROR)
{
seg->stripe_size = grub_lvm_getvalue (&p, "stripe_size = ");
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("unknown stripe_size");
#endif
goto lvs_segment_fail;
}
}
seg->nodes = grub_calloc (seg->node_count, sizeof (seg->nodes[0]));
if (seg->nodes == NULL)
goto lvs_segment_fail;
p = grub_strstr (p, "raids = [");
if (p == NULL)
{
#ifdef GRUB_UTIL
grub_util_info ("unknown raids");
#endif
goto lvs_segment_fail2;
}
p += sizeof("raids = [") - 1;
for (j = 0; j < seg->node_count; j++)
{
char *lvname;
p = grub_strchr (p, '"');
p = p ? grub_strchr (p + 1, '"') : 0;
p = p ? grub_strchr (p + 1, '"') : 0;
if (p == NULL)
goto lvs_segment_fail2;
q = ++p;
while (*q != '"')
q++;
s = q - p;
lvname = grub_malloc (s + 1);
if (lvname == NULL)
goto lvs_segment_fail2;
grub_memcpy (lvname, p, s);
lvname[s] = '\0';
seg->nodes[j].name = lvname;
p = q + 1;
}
if (seg->type == GRUB_DISKFILTER_RAID4)
{
char *tmp;
tmp = seg->nodes[0].name;
grub_memmove (seg->nodes, seg->nodes + 1,
sizeof (seg->nodes[0])
* (seg->node_count - 1));
seg->nodes[seg->node_count - 1].name = tmp;
}
}
/*
* Cache and integrity LVs have extra parts that
* we can ignore for our read-only access.
*/
else if (grub_strncmp (p, "cache\"", sizeof ("cache\"") - 1) == 0 ||
grub_strncmp (p, "cache+CACHE_USES_CACHEVOL\"", sizeof ("cache+CACHE_USES_CACHEVOL\"") - 1) == 0 ||
grub_strncmp (p, "integrity\"", sizeof ("integrity\"") - 1) == 0)
{
struct ignored_feature_lv *ignored_feature = NULL;
char *p2, *p3;
grub_size_t sz;
#ifdef GRUB_UTIL
p2 = grub_strchr (p, '"');
if (p2)
*p2 = '\0';
grub_util_info ("Ignoring extra metadata type '%s' for %s", p, lv->name);
if (p2)
*p2 ='"';
#endif
ignored_feature = grub_zalloc (sizeof (*ignored_feature));
if (!ignored_feature)
goto ignored_feature_lv_fail;
ignored_feature->lv = grub_zalloc (sizeof (*ignored_feature->lv));
if (!ignored_feature->lv)
goto ignored_feature_lv_fail;
grub_memcpy (ignored_feature->lv, lv, sizeof (*ignored_feature->lv));
if (lv->fullname)
{
ignored_feature->lv->fullname = grub_strdup (lv->fullname);
if (!ignored_feature->lv->fullname)
goto ignored_feature_lv_fail;
}
if (lv->idname)
{
ignored_feature->lv->idname = grub_strdup (lv->idname);
if (!ignored_feature->lv->idname)
goto ignored_feature_lv_fail;
}
if (lv->name)
{
ignored_feature->lv->name = grub_strdup (lv->name);
if (!ignored_feature->lv->name)
goto ignored_feature_lv_fail;
}
skip_lv = 1;
p2 = grub_strstr (p, "origin = \"");
if (!p2)
goto ignored_feature_lv_fail;
p2 = grub_strchr (p2, '"');
if (!p2)
goto ignored_feature_lv_fail;
p3 = ++p2;
if (p3 == mda_end)
goto ignored_feature_lv_fail;
p3 = grub_strchr (p3, '"');
if (!p3)
goto ignored_feature_lv_fail;
sz = p3 - p2;
ignored_feature->origin = grub_malloc (sz + 1);
if (!ignored_feature->origin)
goto ignored_feature_lv_fail;
grub_memcpy (ignored_feature->origin, p2, sz);
ignored_feature->origin[sz] = '\0';
ignored_feature->next = ignored_feature_lvs;
ignored_feature_lvs = ignored_feature;
break;
ignored_feature_lv_fail:
if (ignored_feature)
{
grub_free (ignored_feature->origin);
if (ignored_feature->lv)
{
grub_free (ignored_feature->lv->fullname);
grub_free (ignored_feature->lv->idname);
grub_free (ignored_feature->lv->name);
}
grub_free (ignored_feature->lv);
grub_free (ignored_feature);
}
grub_lvm_free_ignored_feature_lvs (ignored_feature_lvs);
goto fail4;
}
else
{
#ifdef GRUB_UTIL
char *p2;
p2 = grub_strchr (p, '"');
if (p2)
*p2 = '\0';
grub_util_info ("unknown LVM type %s", p);
if (p2)
*p2 ='"';
#endif
/* Found a non-supported type, give up and move on. */
skip_lv = 1;
break;
}
seg++;
continue;
lvs_segment_fail2:
grub_free (seg->nodes);
lvs_segment_fail:
goto fail4;
}
if (p != NULL)
p = grub_strchr (p, '}');
if (p == NULL)
goto lvs_fail;
p += 3;
if (skip_lv)
{
grub_free (lv->name);
grub_free (lv);
continue;
}
lv->vg = vg;
lv->next = vg->lvs;
vg->lvs = lv;
continue;
lvs_fail:
grub_free (lv->name);
grub_free (lv);
goto fail4;
}
}
{
struct ignored_feature_lv *ignored_feature;
for (ignored_feature = ignored_feature_lvs; ignored_feature; ignored_feature = ignored_feature->next)
{
struct grub_diskfilter_lv *lv;
for (lv = vg->lvs; lv; lv = lv->next)
if (grub_strcmp (lv->name, ignored_feature->origin) == 0)
break;
if (lv)
{
ignored_feature->lv->segments = grub_calloc (lv->segment_count, sizeof (*lv->segments));
if (!ignored_feature->lv->segments)
{
grub_lvm_free_ignored_feature_lvs (ignored_feature_lvs);
goto fail4;
}
grub_memcpy (ignored_feature->lv->segments, lv->segments, lv->segment_count * sizeof (*lv->segments));
for (i = 0; i < lv->segment_count; ++i)
{
struct grub_diskfilter_node *nodes = lv->segments[i].nodes;
grub_size_t node_count = lv->segments[i].node_count;
ignored_feature->lv->segments[i].nodes = grub_calloc (node_count, sizeof (*nodes));
if (!ignored_feature->lv->segments[i].nodes)
{
for (j = 0; j < i; ++j)
grub_free (ignored_feature->lv->segments[j].nodes);
grub_free (ignored_feature->lv->segments);
ignored_feature->lv->segments = NULL;
grub_lvm_free_ignored_feature_lvs (ignored_feature_lvs);
goto fail4;
}
grub_memcpy (ignored_feature->lv->segments[i].nodes, nodes, node_count * sizeof (*nodes));
}
if (ignored_feature->lv->segments)
{
ignored_feature->lv->segment_count = lv->segment_count;
ignored_feature->lv->vg = vg;
ignored_feature->lv->next = vg->lvs;
vg->lvs = ignored_feature->lv;
ignored_feature->lv = NULL;
}
}
else
{
#ifdef GRUB_UTIL
grub_util_info ("Couldn't find LVM part of ignored feature on %s", ignored_feature->origin);
#endif
}
}
}
/* Match LVs. Must be done after cache and integrity are found. */
{
struct grub_diskfilter_lv *lv1;
struct grub_diskfilter_lv *lv2;
for (lv1 = vg->lvs; lv1; lv1 = lv1->next)
for (i = 0; i < lv1->segment_count; i++)
for (j = 0; j < lv1->segments[i].node_count; j++)
{
if (vg->pvs)
for (pv = vg->pvs; pv; pv = pv->next)
{
if (! grub_strcmp (pv->name,
lv1->segments[i].nodes[j].name))
{
lv1->segments[i].nodes[j].pv = pv;
break;
}
}
if (lv1->segments[i].nodes[j].pv == NULL)
for (lv2 = vg->lvs; lv2; lv2 = lv2->next)
{
if (lv1 == lv2)
continue;
if (grub_strcmp (lv2->name,
lv1->segments[i].nodes[j].name) == 0)
lv1->segments[i].nodes[j].lv = lv2;
}
}
}
grub_lvm_free_ignored_feature_lvs (ignored_feature_lvs);
if (grub_diskfilter_vg_register (vg))
goto fail4;
}
else
{
grub_free (vgname);
}
id->uuid = grub_malloc (GRUB_LVM_ID_STRLEN);
if (!id->uuid)
goto fail4;
grub_memcpy (id->uuid, pv_id, GRUB_LVM_ID_STRLEN);
id->uuidlen = GRUB_LVM_ID_STRLEN;
grub_free (metadatabuf);
*start_sector = -1;
return vg;
/* Failure path. */
fail4:
grub_free (vg);
fail3:
grub_free (vgname);
fail2:
grub_free (metadatabuf);
fail:
return NULL;
}
static struct grub_diskfilter grub_lvm_dev = {
.name = "lvm",
.detect = grub_lvm_detect,
.next = 0
};
GRUB_MOD_INIT (lvm)
{
grub_diskfilter_register_back (&grub_lvm_dev);
}
GRUB_MOD_FINI (lvm)
{
grub_diskfilter_unregister (&grub_lvm_dev);
}
|