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
|
/*
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
* Copyright (C) 2004-2017 Red Hat, Inc. All rights reserved.
*
* This file is part of LVM2.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v.2.1.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "base/memory/zalloc.h"
#include "lib/misc/lib.h"
#include "import-export.h"
#include "lvm-version.h"
#include "lib/metadata/metadata.h"
#include "lib/display/display.h"
#include "lib/misc/lvm-string.h"
#include "lib/metadata/segtype.h"
#include "lib/format_text/text_export.h"
#include "lib/commands/toolcontext.h"
#include "lib/device/device_id.h"
#include "libdaemon/client/config-util.h"
#include "base/data-struct/radix-tree.h"
#include <stdarg.h>
#include <time.h>
#include <sys/utsname.h>
struct formatter;
__attribute__((format(printf, 3, 0)))
typedef int (*out_with_comment_fn) (struct formatter * f, const char *comment,
const char *fmt, va_list ap);
typedef int (*nl_fn) (struct formatter * f);
/*
* Macro for formatted output.
* out_with_comment_fn returns -1 if data didn't fit and buffer was expanded.
* Then argument list is reset and out_with_comment_fn is called again.
*/
#define _out_with_comment(f, buffer, fmt, ap) \
do { \
va_start(ap, fmt); \
r = (f)->out_with_comment((f), (buffer), (fmt), ap); \
va_end(ap); \
} while (r == -1)
/*
* The first half of this file deals with
* exporting the vg, ie. writing it to a file.
*/
struct formatter {
struct radix_tree *pv_idx; /* dev_name id -> pv_name (eg, pv1) */
union {
FILE *fp; /* where we're writing to */
struct {
char *start;
uint32_t size;
uint32_t used;
} buf;
} data;
out_with_comment_fn out_with_comment;
nl_fn nl;
int indent; /* current level of indentation */
int error;
int header; /* 1 => comments at start; 0 => end */
int with_comment; /* 1 => prepare comment sting */
};
static struct utsname _utsname;
static void _init(void)
{
static int _initialised = 0;
if (_initialised)
return;
if (uname(&_utsname)) {
log_error("uname failed: %s", strerror(errno));
memset(&_utsname, 0, sizeof(_utsname));
}
_initialised = 1;
}
/*
* Formatting functions.
*/
#define MAX_INDENT 5
static void _inc_indent(struct formatter *f)
{
if (++f->indent > MAX_INDENT)
f->indent = MAX_INDENT;
}
static void _dec_indent(struct formatter *f)
{
if (!f->indent--) {
log_error(INTERNAL_ERROR "problem tracking indentation");
f->indent = 0;
}
}
/*
* Newline function for prettier layout.
*/
static int _nl_file(struct formatter *f)
{
fprintf(f->data.fp, "\n");
return 1;
}
static int _extend_buffer(struct formatter *f)
{
char *newbuf;
log_debug_metadata("Doubling metadata output buffer to " FMTu32,
f->data.buf.size * 2);
if (!(newbuf = realloc(f->data.buf.start,
f->data.buf.size * 2))) {
log_error("Buffer reallocation failed.");
return 0;
}
memset(newbuf + f->data.buf.size, 0, f->data.buf.size);
f->data.buf.start = newbuf;
f->data.buf.size *= 2;
return 1;
}
static int _nl_raw(struct formatter *f)
{
/* If metadata doesn't fit, extend buffer */
if ((f->data.buf.used + 2 > f->data.buf.size) &&
(!_extend_buffer(f)))
return_0;
*(f->data.buf.start + f->data.buf.used) = '\n';
f->data.buf.used += 1;
*(f->data.buf.start + f->data.buf.used) = '\0';
return 1;
}
#define COMMENT_TAB 6
__attribute__((format(printf, 3, 0)))
static int _out_with_comment_file(struct formatter *f, const char *comment,
const char *fmt, va_list ap)
{
int i;
char white_space[MAX_INDENT + 1];
if (ferror(f->data.fp))
return 0;
for (i = 0; i < f->indent; i++)
white_space[i] = '\t';
white_space[i] = '\0';
fputs(white_space, f->data.fp);
i = vfprintf(f->data.fp, fmt, ap);
if (comment) {
/*
* line comments up if possible.
*/
i += 8 * f->indent;
i /= 8;
i++;
do
fputc('\t', f->data.fp);
while (++i < COMMENT_TAB);
fputs(comment, f->data.fp);
}
fputc('\n', f->data.fp);
return 1;
}
__attribute__((format(printf, 3, 0)))
static int _out_with_comment_raw(struct formatter *f,
const char *comment __attribute__((unused)),
const char *fmt, va_list ap)
{
int n;
va_list apc;
va_copy(apc, ap);
n = vsnprintf(f->data.buf.start + f->data.buf.used,
f->data.buf.size - f->data.buf.used, fmt, apc);
va_end(apc);
/* If metadata doesn't fit, extend buffer */
if (n < 0 || (n + f->data.buf.used + 2 > f->data.buf.size)) {
if (!_extend_buffer(f))
return_0;
return -1; /* Retry */
}
f->data.buf.used += n;
outnl(f);
return 1;
}
/*
* Formats a string, converting a size specified
* in 512-byte sectors to a more human readable
* form (eg, megabytes). We may want to lift this
* for other code to use.
*/
static int _sectors_to_units(uint64_t sectors, char *buffer, size_t s)
{
static const char _units[][16] = {
"Kilobytes",
"Megabytes",
"Gigabytes",
"Terabytes",
"Petabytes",
"Exabytes",
};
unsigned i;
double d = (double) sectors;
/* to convert to K */
d /= 2.0;
for (i = 0; (d > 1024.0) && i < DM_ARRAY_SIZE(_units); ++i)
d /= 1024.0;
return dm_snprintf(buffer, s, "# %g %s", d, _units[i]) > 0;
}
/* increment indentation level */
void out_inc_indent(struct formatter *f)
{
_inc_indent(f);
}
/* decrement indentation level */
void out_dec_indent(struct formatter *f)
{
_dec_indent(f);
}
/* insert new line */
int out_newline(struct formatter *f)
{
return f->nl(f);
}
/*
* Appends a comment giving a size in more easily
* readable form (eg, 4M instead of 8096).
*/
int out_size(struct formatter *f, uint64_t size, const char *fmt, ...)
{
char buffer[64];
va_list ap;
int r;
if (f->with_comment) {
if (!_sectors_to_units(size, buffer, sizeof(buffer)))
return 0;
} else
buffer[0] = 0;
_out_with_comment(f, buffer, fmt, ap);
return r;
}
/*
* Appends a comment indicating that the line is
* only a hint.
*/
int out_hint(struct formatter *f, const char *fmt, ...)
{
va_list ap;
int r;
_out_with_comment(f, "# Hint only", fmt, ap);
return r;
}
/*
* The normal output function with comment
*/
int out_text_with_comment(struct formatter *f, const char *comment, const char *fmt, ...)
{
va_list ap;
int r;
_out_with_comment(f, comment, fmt, ap);
return r;
}
/*
* The normal output function.
*/
int out_text(struct formatter *f, const char *fmt, ...)
{
va_list ap;
int r;
_out_with_comment(f, NULL, fmt, ap);
return r;
}
static int _out_line(const char *line, void *_f) {
struct formatter *f = (struct formatter *) _f;
return out_text(f, "%s", line);
}
int out_config_node(struct formatter *f, const struct dm_config_node *cn)
{
return dm_config_write_node(cn, _out_line, f);
}
static int _print_header(struct cmd_context *cmd, struct formatter *f,
const char *desc)
{
char *buf;
time_t t;
t = time(NULL);
outf(f, "# Generated by LVM2 version %s: %s", LVM_VERSION, ctime(&t));
outf(f, CONTENTS_FIELD " = \"" CONTENTS_VALUE "\"");
outf(f, FORMAT_VERSION_FIELD " = %d", FORMAT_VERSION_VALUE);
outnl(f);
buf = alloca(dm_escaped_len(desc));
outf(f, "description = \"%s\"", dm_escape_double_quotes(buf, desc));
outnl(f);
outf(f, "creation_host = \"%s\"\t# %s %s %s %s %s", _utsname.nodename,
_utsname.sysname, _utsname.nodename, _utsname.release,
_utsname.version, _utsname.machine);
if (cmd->system_id && *cmd->system_id)
outf(f, "creation_host_system_id = \"%s\"", cmd->system_id);
outf(f, "creation_time = " FMTu64 "\t# %s", (uint64_t)t, ctime(&t));
return 1;
}
static int _print_flag_config(struct formatter *f, uint64_t status, enum pv_vg_lv_e type)
{
char buffer[4096];
if (!print_flags(buffer, sizeof(buffer), type, STATUS_FLAG, status))
return_0;
outf(f, "status = [%s]", buffer);
if (!print_flags(buffer, sizeof(buffer), type, COMPATIBLE_FLAG, status))
return_0;
outf(f, "flags = [%s]", buffer);
return 1;
}
static char *_alloc_printed_str_list(struct dm_list *list)
{
struct dm_str_list *sl;
int first = 1;
size_t size = 0;
char *buffer, *buf;
dm_list_iterate_items(sl, list)
/* '"' + item + '"' + ',' + ' ' */
size += strlen(sl->str) + 4;
/* '[' + ']' + '\0' */
size += 3;
if (!(buffer = buf = malloc(size))) {
log_error("Could not allocate memory for string list buffer.");
return NULL;
}
buffer[0] = 0;
dm_list_iterate_items(sl, list) {
if (!emit_to_buffer(&buf, &size, "%s\"%s\"",
(!first) ? ", " : "",
sl->str)) {
free(buffer);
return_NULL;
}
first = 0;
}
return buffer;
}
static int _out_list(struct formatter *f, struct dm_list *list,
const char *list_name)
{
char *buffer;
if (!dm_list_empty(list)) {
if (!(buffer = _alloc_printed_str_list(list)))
return_0;
if (!out_text(f, "%s = [%s]", list_name, buffer)) {
free(buffer);
return_0;
}
free(buffer);
}
return 1;
}
static int _print_vg(struct formatter *f, struct volume_group *vg)
{
char buffer[4096];
const struct format_type *fmt = NULL;
uint64_t status = vg->status;
if (!id_write_format(&vg->id, buffer, sizeof(buffer)))
return_0;
outf(f, "id = \"%s\"", buffer);
outf(f, "seqno = %u", vg->seqno);
if (vg->original_fmt)
fmt = vg->original_fmt;
else if (vg->fid)
fmt = vg->fid->fmt;
if (fmt)
outfc(f, "# informational", "format = \"%s\"", fmt->name);
/*
* Removing WRITE and adding LVM_WRITE_LOCKED makes it read-only
* to old versions of lvm that only look for LVM_WRITE.
*/
if ((status & LVM_WRITE) && vg_flag_write_locked(vg)) {
status &= ~LVM_WRITE;
status |= LVM_WRITE_LOCKED;
}
if (!_print_flag_config(f, status, VG_FLAGS))
return_0;
if (!_out_list(f, &vg->tags, "tags"))
return_0;
if (vg->system_id && *vg->system_id)
outf(f, "system_id = \"%s\"", vg->system_id);
if (vg->lock_type) {
outf(f, "lock_type = \"%s\"", vg->lock_type);
if (vg->lock_args)
outf(f, "lock_args = \"%s\"", vg->lock_args);
}
outsize(f, (uint64_t) vg->extent_size, "extent_size = %u",
vg->extent_size);
outf(f, "max_lv = %u", vg->max_lv);
outf(f, "max_pv = %u", vg->max_pv);
/* Default policy is NORMAL; INHERIT is meaningless */
if (vg->alloc != ALLOC_NORMAL && vg->alloc != ALLOC_INHERIT) {
outnl(f);
outf(f, "allocation_policy = \"%s\"",
get_alloc_string(vg->alloc));
}
if (vg->profile)
outf(f, "profile = \"%s\"", vg->profile->name);
outf(f, "metadata_copies = %u", vg->mda_copies);
return 1;
}
/* Get the pv index %d name from the formatters radix tree. */
static int _get_pv_idx(const struct formatter *f, const struct physical_volume *pv)
{
union radix_value idx;
if (!pv || !radix_tree_lookup(f->pv_idx, &pv, sizeof(pv), &idx)) {
log_error(INTERNAL_ERROR "PV name for %s missing in metadata export radix tree.",
(pv) ? pv_dev_name(pv) : "");
return -1;
}
return (int) idx.n;
}
static int _print_pvs(struct formatter *f, struct volume_group *vg)
{
struct pv_list *pvl;
struct physical_volume *pv;
char buffer[PATH_MAX * 2];
int idx;
const char *idtype, *idname;
outf(f, "physical_volumes {");
_inc_indent(f);
dm_list_iterate_items(pvl, &vg->pvs) {
pv = pvl->pv;
if (!id_write_format(&pv->id, buffer, sizeof(buffer)))
return_0;
if ((idx = _get_pv_idx(f, pv)) < 0)
return_0;
outnl(f);
outf(f, "pv%d {", idx);
_inc_indent(f);
outf(f, "id = \"%s\"", buffer);
if (strlen(pv_dev_name(pv)) >= PATH_MAX) {
log_error("pv device name size is out of bounds.");
return 0;
}
outhint(f, "device = \"%s\"",
dm_escape_double_quotes(buffer, pv_dev_name(pv)));
outnl(f);
idtype = dev_idtype_for_metadata(vg->cmd, pv->dev);
idname = dev_idname_for_metadata(vg->cmd, pv->dev);
if (idtype && idname) {
outf(f, "device_id_type = \"%s\"", idtype);
outf(f, "device_id = \"%s\"", idname);
}
if (!_print_flag_config(f, pv->status, PV_FLAGS))
return_0;
if (!_out_list(f, &pv->tags, "tags"))
return_0;
outsize(f, pv->size, "dev_size = " FMTu64, pv->size);
outf(f, "pe_start = " FMTu64, pv->pe_start);
outsize(f, vg->extent_size * (uint64_t) pv->pe_count,
"pe_count = %u", pv->pe_count);
if (pv->ba_start && pv->ba_size) {
outf(f, "ba_start = " FMTu64, pv->ba_start);
outsize(f, pv->ba_size, "ba_size = " FMTu64, pv->ba_size);
}
_dec_indent(f);
outf(f, "}");
}
_dec_indent(f);
outf(f, "}");
return 1;
}
static int _print_segment(struct formatter *f, struct volume_group *vg,
int count, struct lv_segment *seg)
{
char buffer[2048];
if (!print_segtype_lvflags(buffer, sizeof(buffer), seg->lv->status))
return_0;
outf(f, "segment%u {", count);
_inc_indent(f);
outf(f, "start_extent = %u", seg->le);
outsize(f, (uint64_t) seg->len * vg->extent_size,
"extent_count = %u", seg->len);
outnl(f);
if (seg->reshape_len)
outsize(f, (uint64_t) seg->reshape_len * vg->extent_size,
"reshape_count = %u", seg->reshape_len);
outf(f, "type = \"%s%s\"", seg->segtype->name, buffer);
if (!_out_list(f, &seg->tags, "tags"))
return_0;
if (seg->segtype->ops->text_export &&
!seg->segtype->ops->text_export(seg, f))
return_0;
_dec_indent(f);
outf(f, "}");
return 1;
}
int out_areas(struct formatter *f, const struct lv_segment *seg,
const char *type)
{
unsigned int s;
int idx;
struct physical_volume *pv;
outnl(f);
outf(f, "%ss = [", type);
_inc_indent(f);
for (s = 0; s < seg->area_count; s++) {
switch (seg_type(seg, s)) {
case AREA_PV:
if (!(pv = seg_pv(seg, s))) {
log_error(INTERNAL_ERROR "Missing PV for area " FMTu32 " of %s segment of LV %s.",
s, type, display_lvname(seg->lv));
return 0;
}
if ((idx = _get_pv_idx(f, pv)) < 0)
return_0;
outf(f, "\"pv%d\", %u%s", idx,
seg_pe(seg, s),
(s == seg->area_count - 1) ? "" : ",");
break;
case AREA_LV:
/* FIXME This helper code should be target-independent! Check for metadata LV property. */
if (!seg_is_raid(seg)) {
outf(f, "\"%s\", %u%s",
seg_lv(seg, s)->name,
seg_le(seg, s),
(s == seg->area_count - 1) ? "" : ",");
continue;
}
/* RAID devices are laid-out in metadata/data pairs */
/* FIXME Validation should be elsewhere, not here! */
if (!lv_is_raid_image(seg_lv(seg, s)) ||
(seg->meta_areas && seg_metalv(seg, s) && !lv_is_raid_metadata(seg_metalv(seg, s)))) {
log_error("RAID segment has non-RAID areas");
return 0;
}
if (seg->meta_areas && seg_metalv(seg,s))
outf(f, "\"%s\", \"%s\"%s",
(seg->meta_areas && seg_metalv(seg, s)) ? seg_metalv(seg, s)->name : "",
seg_lv(seg, s)->name, (s == seg->area_count - 1) ? "" : ",");
else
outf(f, "\"%s\"%s", seg_lv(seg, s)->name, (s == seg->area_count - 1) ? "" : ",");
break;
case AREA_UNASSIGNED:
log_error(INTERNAL_ERROR "Invalid type for area " FMTu32 " of %s segment of LV %s.",
s, type, display_lvname(seg->lv));
return 0;
}
}
_dec_indent(f);
outf(f, "]");
return 1;
}
static int _print_timestamp(struct formatter *f,
const char *name, time_t ts,
char *buf, size_t buf_size)
{
struct tm *local_tm;
if (ts) {
if (f->with_comment) {
/* generate timestamp only for commented output */
strncpy(buf, "# ", buf_size);
if (!(local_tm = localtime(&ts)) ||
!strftime(buf + 2, buf_size - 2,
"%Y-%m-%d %T %z", local_tm))
buf[0] = 0;
} else
buf[0] = 0;
outfc(f, buf, "%s = " FMTu64, name, (uint64_t) ts);
}
return 1;
}
static int _print_lv(struct formatter *f, struct logical_volume *lv)
{
struct lv_segment *seg;
char buffer[4096];
int seg_count;
uint64_t status = lv->status;
outnl(f);
outf(f, "%s {", lv->name);
_inc_indent(f);
/* FIXME: Write full lvid */
if (!id_write_format(&lv->lvid.id[1], buffer, sizeof(buffer)))
return_0;
outf(f, "id = \"%s\"", buffer);
/*
* Removing WRITE and adding LVM_WRITE_LOCKED makes it read-only
* to old versions of lvm that only look for LVM_WRITE.
*/
if ((status & LVM_WRITE) && vg_flag_write_locked(lv->vg)) {
status &= ~LVM_WRITE;
status |= LVM_WRITE_LOCKED;
}
if (!_print_flag_config(f, status, LV_FLAGS))
return_0;
if (!_out_list(f, &lv->tags, "tags"))
return_0;
if (lv->timestamp) {
if (!_print_timestamp(f, "creation_time", lv->timestamp,
buffer, sizeof(buffer)))
return_0;
outf(f, "creation_host = \"%s\"", lv->hostname);
}
if (lv->lock_args)
outf(f, "lock_args = \"%s\"", lv->lock_args);
if (lv->alloc != ALLOC_INHERIT)
outf(f, "allocation_policy = \"%s\"",
get_alloc_string(lv->alloc));
if (lv->profile)
outf(f, "profile = \"%s\"", lv->profile->name);
switch (lv->read_ahead) {
case DM_READ_AHEAD_NONE:
outfc(f, "# None", "read_ahead = -1");
break;
case DM_READ_AHEAD_AUTO:
/* No output - use default */
break;
default:
outf(f, "read_ahead = %u", lv->read_ahead);
}
if (lv->major >= 0)
outf(f, "major = %d", lv->major);
if (lv->minor >= 0)
outf(f, "minor = %d", lv->minor);
outf(f, "segment_count = %u", dm_list_size(&lv->segments));
outnl(f);
seg_count = 1;
dm_list_iterate_items(seg, &lv->segments) {
if (!_print_segment(f, lv->vg, seg_count++, seg))
return_0;
}
_dec_indent(f);
outf(f, "}");
return 1;
}
static int _print_lvs(struct formatter *f, struct volume_group *vg)
{
struct lv_list *lvl;
/*
* Don't bother with an lv section if there are no lvs.
*/
if (dm_list_empty(&vg->lvs))
return 1;
outf(f, "logical_volumes {");
_inc_indent(f);
/*
* Write visible LVs first
*/
dm_list_iterate_items(lvl, &vg->lvs) {
if (!(lv_is_visible(lvl->lv)))
continue;
if (!_print_lv(f, lvl->lv))
return_0;
}
dm_list_iterate_items(lvl, &vg->lvs) {
if ((lv_is_visible(lvl->lv)))
continue;
if (!_print_lv(f, lvl->lv))
return_0;
}
_dec_indent(f);
outf(f, "}");
return 1;
}
static int _alloc_printed_indirect_descendants(struct dm_list *indirect_glvs, char **buffer)
{
struct glv_list *user_glvl;
size_t buf_size = 0;
int first = 1;
char *buf;
*buffer = NULL;
dm_list_iterate_items(user_glvl, indirect_glvs) {
if (user_glvl->glv->is_historical)
continue;
/* '"' + name + '"' + ',' + ' ' */
buf_size += strlen(user_glvl->glv->live->name) + 4;
}
if (!buf_size)
return 1;
/* '[' + ']' + '\0' */
buf_size += 3;
if (!(*buffer = malloc(buf_size))) {
log_error("Could not allocate memory for ancestor list buffer.");
return 0;
}
buf = *buffer;
buf[0] = 0;
dm_list_iterate_items(user_glvl, indirect_glvs) {
if (user_glvl->glv->is_historical)
continue;
if (!emit_to_buffer(&buf, &buf_size, "%s\"%s\"",
(!first) ? ", " : "",
user_glvl->glv->live->name))
goto_bad;
first = 0;
}
return 1;
bad:
if (*buffer) {
free(*buffer);
*buffer = NULL;
}
return 0;
}
static int _print_historical_lv_with_descendants(struct formatter *f, struct historical_logical_volume *hlv,
char *descendants_buffer)
{
char buffer[40];
if (!id_write_format(&hlv->lvid.id[1], buffer, sizeof(buffer)))
return_0;
outnl(f);
outf(f, "%s {", hlv->name);
_inc_indent(f);
outf(f, "id = \"%s\"", buffer);
if (!_print_timestamp(f, "creation_time", hlv->timestamp, buffer, sizeof(buffer)))
return_0;
if (!_print_timestamp(f, "removal_time", hlv->timestamp_removed, buffer, sizeof(buffer)))
return_0;
if (hlv->indirect_origin) {
if (hlv->indirect_origin->is_historical)
outf(f, "origin = \"%s%s\"", HISTORICAL_LV_PREFIX, hlv->indirect_origin->historical->name);
else
outf(f, "origin = \"%s\"", hlv->indirect_origin->live->name);
}
if (descendants_buffer)
outf(f, "descendants = [%s]", descendants_buffer);
_dec_indent(f);
outf(f, "}");
return 1;
}
static int _print_historical_lv(struct formatter *f, struct historical_logical_volume *hlv)
{
char *descendants_buffer = NULL;
int r = 0;
if (_alloc_printed_indirect_descendants(&hlv->indirect_glvs, &descendants_buffer))
r = _print_historical_lv_with_descendants(f, hlv, descendants_buffer);
free(descendants_buffer);
return r;
}
static int _print_historical_lvs(struct formatter *f, struct volume_group *vg)
{
struct glv_list *glvl;
if (dm_list_empty(&vg->historical_lvs))
return 1;
outf(f, "historical_logical_volumes {");
_inc_indent(f);
dm_list_iterate_items(glvl, &vg->historical_lvs) {
if (!_print_historical_lv(f, glvl->glv->historical))
return_0;
}
_dec_indent(f);
outf(f, "}");
return 1;
}
/*
* In the text format we refer to pv's as 'pv1',
* 'pv2' etc. This function builds a hash table
* to enable a quick lookup from device -> name.
*/
static int _build_pv_idx(struct formatter *f, struct volume_group *vg)
{
union radix_value count = { 0 };
struct pv_list *pvl;
struct physical_volume *pv;
if (!(f->pv_idx = radix_tree_create(NULL, NULL)))
return_0;
dm_list_iterate_items(pvl, &vg->pvs) {
pv = pvl->pv;
/* FIXME But skip if there's already an LV called pv%d ! */
if (!radix_tree_insert(f->pv_idx, &pv, sizeof(pv), count))
return_0;
count.n++;
}
return 1;
}
static int _text_vg_export(struct formatter *f,
struct volume_group *vg, const char *desc)
{
int r = 0;
if (!_build_pv_idx(f, vg))
goto_out;
if (f->header && !_print_header(vg->cmd, f, desc))
goto_out;
if (!out_text(f, "%s {", vg->name))
goto_out;
_inc_indent(f);
if (!_print_vg(f, vg))
goto_out;
outnl(f);
if (!_print_pvs(f, vg))
goto_out;
outnl(f);
if (!_print_lvs(f, vg))
goto_out;
outnl(f);
if (!_print_historical_lvs(f, vg))
goto_out;
_dec_indent(f);
if (!out_text(f, "}"))
goto_out;
if (!f->header && !_print_header(vg->cmd, f, desc))
goto_out;
r = 1;
out:
if (f->pv_idx) {
radix_tree_destroy(f->pv_idx);
f->pv_idx = NULL;
}
return r;
}
int text_vg_export_file(struct volume_group *vg, const char *desc, FILE *fp)
{
int r;
struct formatter f = {
.indent = 0,
.header = 1,
.out_with_comment = &_out_with_comment_file,
.with_comment = 1,
.nl = &_nl_file,
.data.fp = fp,
};
_init();
if ((r = _text_vg_export(&f, vg, desc)))
r = !ferror(f.data.fp);
return r;
}
/* Returns amount of buffer used incl. terminating NUL */
size_t text_vg_export_raw(struct volume_group *vg, const char *desc, char **buf, uint32_t *buf_size)
{
size_t r;
struct formatter f = {
.indent = 0,
.header = 0,
.out_with_comment = &_out_with_comment_raw,
.nl = &_nl_raw,
.data.buf.size = vg->buffer_size_hint + 16384, /* Initial metadata limit */
};
_init();
if (!(f.data.buf.start = zalloc(f.data.buf.size))) {
log_error("text_export buffer allocation failed");
return 0;
}
if (!_text_vg_export(&f, vg, desc)) {
free(f.data.buf.start);
return 0;
}
r = f.data.buf.used + 1;
*buf = f.data.buf.start;
if (buf_size)
*buf_size = f.data.buf.size;
return r;
}
static size_t _export_vg_to_buffer(struct volume_group *vg, char **buf)
{
return text_vg_export_raw(vg, "", buf, NULL);
}
struct dm_config_tree *export_vg_to_config_tree(struct volume_group *vg)
{
char *buf = NULL;
struct dm_config_tree *vg_cft;
if (!_export_vg_to_buffer(vg, &buf)) {
log_error("Could not format metadata for VG %s.", vg->name);
return NULL;
}
if (!(vg_cft = config_tree_from_string_without_dup_node_check(buf))) {
log_error("Error parsing metadata for VG %s.", vg->name);
free(buf);
return NULL;
}
free(buf);
return vg_cft;
}
#undef outf
#undef outnl
|