2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "toolcontext.h"
25 typedef enum { SIZE_LONG = 0, SIZE_SHORT = 1, SIZE_UNIT = 2 } size_len_t;
29 const char str[14]; /* must be changed when size extends 13 chars */
33 ALLOC_CONTIGUOUS, "contiguous", 'c'}, {
34 ALLOC_CLING, "cling", 'l'}, {
35 ALLOC_CLING_BY_TAGS, "cling_by_tags", 't'}, { /* Only used in log mesgs */
36 ALLOC_NORMAL, "normal", 'n'}, {
37 ALLOC_ANYWHERE, "anywhere", 'a'}, {
38 ALLOC_INHERIT, "inherit", 'i'}
41 static const int _num_policies = sizeof(_policies) / sizeof(*_policies);
43 uint64_t units_to_bytes(const char *units, char *unit_type)
48 if (isdigit(*units)) {
49 v = (uint64_t) strtod(units, &ptr);
71 #define KILO UINT64_C(1024)
83 v *= KILO * KILO * KILO;
86 v *= KILO * KILO * KILO * KILO;
89 v *= KILO * KILO * KILO * KILO * KILO;
92 v *= KILO * KILO * KILO * KILO * KILO * KILO;
95 #define KILO UINT64_C(1000)
103 v *= KILO * KILO * KILO;
106 v *= KILO * KILO * KILO * KILO;
109 v *= KILO * KILO * KILO * KILO * KILO;
112 v *= KILO * KILO * KILO * KILO * KILO * KILO;
125 const char alloc_policy_char(alloc_policy_t alloc)
129 for (i = 0; i < _num_policies; i++)
130 if (_policies[i].alloc == alloc)
131 return _policies[i].repchar;
136 const char *get_alloc_string(alloc_policy_t alloc)
140 for (i = 0; i < _num_policies; i++)
141 if (_policies[i].alloc == alloc)
142 return _policies[i].str;
147 alloc_policy_t get_alloc_from_string(const char *str)
151 /* cling_by_tags is part of cling */
152 if (!strcmp("cling_by_tags", str))
155 for (i = 0; i < _num_policies; i++)
156 if (!strcmp(_policies[i].str, str))
157 return _policies[i].alloc;
159 /* Special case for old metadata */
160 if (!strcmp("next free", str))
163 log_error("Unrecognised allocation policy %s", str);
164 return ALLOC_INVALID;
167 #define BASE_UNKNOWN 0
168 #define BASE_SHARED 1
171 #define BASE_SPECIAL 19
172 #define NUM_UNIT_PREFIXES 6
173 #define NUM_SPECIAL 3
175 /* Size supplied in sectors */
176 static const char *_display_size(const struct cmd_context *cmd,
177 uint64_t size, size_len_t sl)
179 unsigned base = BASE_UNKNOWN;
181 int suffix = 1, precision;
182 uint64_t byte = UINT64_C(0);
183 uint64_t units = UINT64_C(1024);
184 char *size_buf = NULL;
185 const char * const size_str[][3] = {
187 {" ", " ", " "}, /* [0] */
189 /* BASE_SHARED - Used if cmd->si_unit_consistency = 0 */
190 {" Exabyte", " EB", "E"}, /* [1] */
191 {" Petabyte", " PB", "P"}, /* [2] */
192 {" Terabyte", " TB", "T"}, /* [3] */
193 {" Gigabyte", " GB", "G"}, /* [4] */
194 {" Megabyte", " MB", "M"}, /* [5] */
195 {" Kilobyte", " KB", "K"}, /* [6] */
197 /* BASE_1024 - Used if cmd->si_unit_consistency = 1 */
198 {" Exbibyte", " EiB", "e"}, /* [7] */
199 {" Pebibyte", " PiB", "p"}, /* [8] */
200 {" Tebibyte", " TiB", "t"}, /* [9] */
201 {" Gibibyte", " GiB", "g"}, /* [10] */
202 {" Mebibyte", " MiB", "m"}, /* [11] */
203 {" Kibibyte", " KiB", "k"}, /* [12] */
205 /* BASE_1000 - Used if cmd->si_unit_consistency = 1 */
206 {" Exabyte", " EB", "E"}, /* [13] */
207 {" Petabyte", " PB", "P"}, /* [14] */
208 {" Terabyte", " TB", "T"}, /* [15] */
209 {" Gigabyte", " GB", "G"}, /* [16] */
210 {" Megabyte", " MB", "M"}, /* [17] */
211 {" Kilobyte", " kB", "K"}, /* [18] */
214 {" Byte ", " B ", "B"}, /* [19] */
215 {" Units ", " Un", "U"}, /* [20] */
216 {" Sectors ", " Se", "S"}, /* [21] */
219 if (!(size_buf = dm_pool_alloc(cmd->mem, SIZE_BUF))) {
220 log_error("no memory for size display buffer");
224 suffix = cmd->current_settings.suffix;
226 if (!cmd->si_unit_consistency) {
227 /* Case-independent match */
228 for (s = 0; s < NUM_UNIT_PREFIXES; s++)
229 if (toupper((int) cmd->current_settings.unit_type) ==
230 *size_str[BASE_SHARED + s][2]) {
235 /* Case-dependent match for powers of 1000 */
236 for (s = 0; s < NUM_UNIT_PREFIXES; s++)
237 if (cmd->current_settings.unit_type ==
238 *size_str[BASE_1000 + s][2]) {
243 /* Case-dependent match for powers of 1024 */
244 if (base == BASE_UNKNOWN)
245 for (s = 0; s < NUM_UNIT_PREFIXES; s++)
246 if (cmd->current_settings.unit_type ==
247 *size_str[BASE_1024 + s][2]) {
253 if (base == BASE_UNKNOWN)
254 /* Check for special units - s, b or u */
255 for (s = 0; s < NUM_SPECIAL; s++)
256 if (toupper((int) cmd->current_settings.unit_type) ==
257 *size_str[BASE_SPECIAL + s][2]) {
262 if (size == UINT64_C(0)) {
263 if (base == BASE_UNKNOWN)
265 sprintf(size_buf, "0%s", suffix ? size_str[base + s][sl] : "");
269 size *= UINT64_C(512);
271 if (base != BASE_UNKNOWN)
272 byte = cmd->current_settings.unit_factor;
274 /* Human-readable style */
275 if (cmd->current_settings.unit_type == 'H') {
276 units = UINT64_C(1000);
279 units = UINT64_C(1024);
283 if (!cmd->si_unit_consistency)
286 byte = units * units * units * units * units * units;
288 for (s = 0; s < NUM_UNIT_PREFIXES && size < byte; s++)
294 /* FIXME Make precision configurable */
295 switch(toupper((int) cmd->current_settings.unit_type)) {
304 snprintf(size_buf, SIZE_BUF - 1, "%.*f%s", precision,
305 (double) size / byte, suffix ? size_str[base + s][sl] : "");
310 const char *display_size_long(const struct cmd_context *cmd, uint64_t size)
312 return _display_size(cmd, size, SIZE_LONG);
315 const char *display_size_units(const struct cmd_context *cmd, uint64_t size)
317 return _display_size(cmd, size, SIZE_UNIT);
320 const char *display_size(const struct cmd_context *cmd, uint64_t size)
322 return _display_size(cmd, size, SIZE_SHORT);
325 void pvdisplay_colons(const struct physical_volume *pv)
327 char uuid[64] __attribute__((aligned(8)));
332 if (!id_write_format(&pv->id, uuid, sizeof(uuid))) {
337 log_print("%s:%s:%" PRIu64 ":-1:%" PRIu64 ":%" PRIu64 ":-1:%" PRIu32 ":%u:%u:%u:%s",
338 pv_dev_name(pv), pv->vg_name, pv->size,
339 /* FIXME pv->pv_number, Derive or remove? */
340 pv->status, /* FIXME Support old or new format here? */
341 pv->status & ALLOCATABLE_PV, /* FIXME remove? */
342 /* FIXME pv->lv_cur, Remove? */
345 pv->pe_count - pv->pe_alloc_count,
346 pv->pe_alloc_count, *uuid ? uuid : "none");
349 void pvdisplay_segments(const struct physical_volume *pv)
351 const struct pv_segment *pvseg;
354 log_print("--- Physical Segments ---");
356 dm_list_iterate_items(pvseg, &pv->segments) {
357 log_print("Physical extent %u to %u:",
358 pvseg->pe, pvseg->pe + pvseg->len - 1);
360 if (pvseg_is_allocated(pvseg)) {
361 log_print(" Logical volume\t%s%s/%s",
362 pvseg->lvseg->lv->vg->cmd->dev_dir,
363 pvseg->lvseg->lv->vg->name,
364 pvseg->lvseg->lv->name);
365 log_print(" Logical extents\t%d to %d",
366 pvseg->lvseg->le, pvseg->lvseg->le +
367 pvseg->lvseg->len - 1);
375 /* FIXME Include label fields */
376 void pvdisplay_full(const struct cmd_context *cmd,
377 const struct physical_volume *pv,
378 void *handle __attribute__((unused)))
380 char uuid[64] __attribute__((aligned(8)));
384 uint64_t data_size, pvsize, unusable;
389 if (!id_write_format(&pv->id, uuid, sizeof(uuid))) {
394 log_print("--- %sPhysical volume ---", pv->pe_size ? "" : "NEW ");
395 log_print("PV Name %s", pv_dev_name(pv));
396 log_print("VG Name %s%s",
397 is_orphan(pv) ? "" : pv->vg_name,
398 pv->status & EXPORTED_VG ? " (exported)" : "");
400 data_size = (uint64_t) pv->pe_count * pv->pe_size;
401 if (pv->size > data_size + pv->pe_start) {
403 unusable = pvsize - data_size;
405 pvsize = data_size + pv->pe_start;
406 unusable = pvsize - pv->size;
409 size = display_size(cmd, pvsize);
411 log_print("PV Size %s / not usable %s", /* [LVM: %s]", */
412 size, display_size(cmd, unusable));
414 log_print("PV Size %s", size);
416 /* PV number not part of LVM2 design
417 log_print("PV# %u", pv->pv_number);
420 pe_free = pv->pe_count - pv->pe_alloc_count;
421 if (pv->pe_count && (pv->status & ALLOCATABLE_PV))
422 log_print("Allocatable yes %s",
423 (!pe_free && pv->pe_count) ? "(but full)" : "");
425 log_print("Allocatable NO");
427 /* LV count is no longer available when displaying PV
428 log_print("Cur LV %u", vg->lv_count);
431 if (cmd->si_unit_consistency)
432 log_print("PE Size %s", display_size(cmd, (uint64_t) pv->pe_size));
434 log_print("PE Size (KByte) %" PRIu32, pv->pe_size / 2);
436 log_print("Total PE %u", pv->pe_count);
437 log_print("Free PE %" PRIu32, pe_free);
438 log_print("Allocated PE %u", pv->pe_alloc_count);
439 log_print("PV UUID %s", *uuid ? uuid : "none");
443 int pvdisplay_short(const struct cmd_context *cmd __attribute__((unused)),
444 const struct volume_group *vg __attribute__((unused)),
445 const struct physical_volume *pv,
446 void *handle __attribute__((unused)))
448 char uuid[64] __attribute__((aligned(8)));
453 if (!id_write_format(&pv->id, uuid, sizeof(uuid)))
456 log_print("PV Name %s ", pv_dev_name(pv));
457 /* FIXME pv->pv_number); */
458 log_print("PV UUID %s", *uuid ? uuid : "none");
459 log_print("PV Status %sallocatable",
460 (pv->status & ALLOCATABLE_PV) ? "" : "NOT ");
461 log_print("Total PE / Free PE %u / %u",
462 pv->pe_count, pv->pe_count - pv->pe_alloc_count);
468 void lvdisplay_colons(const struct logical_volume *lv)
472 inkernel = lv_info(lv->vg->cmd, lv, 0, &info, 1, 0) && info.exists;
474 log_print("%s%s/%s:%s:%" PRIu64 ":%d:-1:%d:%" PRIu64 ":%d:-1:%d:%d:%d:%d",
475 lv->vg->cmd->dev_dir,
479 (lv->status & (LVM_READ | LVM_WRITE)) >> 8, inkernel ? 1 : 0,
480 /* FIXME lv->lv_number, */
481 inkernel ? info.open_count : 0, lv->size, lv->le_count,
482 /* FIXME Add num allocated to struct! lv->lv_allocated_le, */
483 (lv->alloc == ALLOC_CONTIGUOUS ? 2 : 0), lv->read_ahead,
484 inkernel ? info.major : -1, inkernel ? info.minor : -1);
487 int lvdisplay_full(struct cmd_context *cmd,
488 const struct logical_volume *lv,
489 void *handle __attribute__((unused)))
492 int inkernel, snap_active = 0;
493 char uuid[64] __attribute__((aligned(8)));
494 struct lv_segment *snap_seg = NULL, *mirror_seg = NULL;
495 percent_t snap_percent;
497 if (!id_write_format(&lv->lvid.id[1], uuid, sizeof(uuid)))
500 inkernel = lv_info(cmd, lv, 0, &info, 1, 1) && info.exists;
502 log_print("--- Logical volume ---");
504 log_print("LV Name %s%s/%s", lv->vg->cmd->dev_dir,
505 lv->vg->name, lv->name);
506 log_print("VG Name %s", lv->vg->name);
508 log_print("LV UUID %s", uuid);
510 log_print("LV Write Access %s",
511 (lv->status & LVM_WRITE) ? "read/write" : "read only");
513 if (lv_is_origin(lv)) {
514 log_print("LV snapshot status source of");
516 dm_list_iterate_items_gen(snap_seg, &lv->snapshot_segs,
519 (snap_active = lv_snapshot_percent(snap_seg->cow,
521 if (snap_percent == PERCENT_INVALID)
523 log_print(" %s%s/%s [%s]",
524 lv->vg->cmd->dev_dir, lv->vg->name,
526 snap_active ? "active" : "INACTIVE");
529 } else if ((snap_seg = find_cow(lv))) {
531 (snap_active = lv_snapshot_percent(snap_seg->cow,
533 if (snap_percent == PERCENT_INVALID)
536 log_print("LV snapshot status %s destination for %s%s/%s",
537 snap_active ? "active" : "INACTIVE",
538 lv->vg->cmd->dev_dir, lv->vg->name,
539 snap_seg->origin->name);
542 if (inkernel && info.suspended)
543 log_print("LV Status suspended");
545 log_print("LV Status %savailable",
546 inkernel ? "" : "NOT ");
548 /********* FIXME lv_number
549 log_print("LV # %u", lv->lv_number + 1);
553 log_print("# open %u", info.open_count);
555 log_print("LV Size %s",
557 snap_seg ? snap_seg->origin->size : lv->size));
559 log_print("Current LE %u",
560 snap_seg ? snap_seg->origin->le_count : lv->le_count);
563 log_print("COW-table size %s",
564 display_size(cmd, (uint64_t) lv->size));
565 log_print("COW-table LE %u", lv->le_count);
568 log_print("Allocated to snapshot %.2f%% ",
569 percent_to_float(snap_percent));
571 log_print("Snapshot chunk size %s",
572 display_size(cmd, (uint64_t) snap_seg->chunk_size));
575 if (lv->status & MIRRORED) {
576 mirror_seg = first_seg(lv);
577 log_print("Mirrored volumes %" PRIu32, mirror_seg->area_count);
578 if (lv->status & CONVERTING)
579 log_print("LV type Mirror undergoing conversion");
582 log_print("Segments %u", dm_list_size(&lv->segments));
584 /********* FIXME Stripes & stripesize for each segment
585 log_print("Stripe size %s", display_size(cmd, (uint64_t) lv->stripesize));
588 log_print("Allocation %s", get_alloc_string(lv->alloc));
589 if (lv->read_ahead == DM_READ_AHEAD_AUTO)
590 log_print("Read ahead sectors auto");
591 else if (lv->read_ahead == DM_READ_AHEAD_NONE)
592 log_print("Read ahead sectors 0");
594 log_print("Read ahead sectors %u", lv->read_ahead);
596 if (inkernel && lv->read_ahead != info.read_ahead)
597 log_print("- currently set to %u", info.read_ahead);
599 if (lv->status & FIXED_MINOR) {
601 log_print("Persistent major %d", lv->major);
602 log_print("Persistent minor %d", lv->minor);
606 log_print("Block device %d:%d", info.major,
614 void display_stripe(const struct lv_segment *seg, uint32_t s, const char *pre)
616 switch (seg_type(seg, s)) {
618 /* FIXME Re-check the conditions for 'Missing' */
619 log_print("%sPhysical volume\t%s", pre,
621 pv_dev_name(seg_pv(seg, s)) :
625 log_print("%sPhysical extents\t%d to %d", pre,
627 seg_pe(seg, s) + seg->area_len - 1);
630 log_print("%sLogical volume\t%s", pre,
632 seg_lv(seg, s)->name : "Missing");
635 log_print("%sLogical extents\t%d to %d", pre,
637 seg_le(seg, s) + seg->area_len - 1);
639 case AREA_UNASSIGNED:
640 log_print("%sUnassigned area", pre);
644 int lvdisplay_segments(const struct logical_volume *lv)
646 const struct lv_segment *seg;
648 log_print("--- Segments ---");
650 dm_list_iterate_items(seg, &lv->segments) {
651 log_print("Logical extent %u to %u:",
652 seg->le, seg->le + seg->len - 1);
654 log_print(" Type\t\t%s", seg->segtype->ops->name(seg));
656 if (seg->segtype->ops->display)
657 seg->segtype->ops->display(seg);
664 void vgdisplay_extents(const struct volume_group *vg __attribute__((unused)))
668 void vgdisplay_full(const struct volume_group *vg)
672 char uuid[64] __attribute__((aligned(8)));
674 active_pvs = vg->pv_count - vg_missing_pv_count(vg);
676 log_print("--- Volume group ---");
677 log_print("VG Name %s", vg->name);
678 log_print("System ID %s", vg->system_id);
679 log_print("Format %s", vg->fid->fmt->name);
680 if (vg->fid->fmt->features & FMT_MDAS) {
681 log_print("Metadata Areas %d",
683 log_print("Metadata Sequence No %d", vg->seqno);
685 access_str = vg->status & (LVM_READ | LVM_WRITE);
686 log_print("VG Access %s%s%s%s",
687 access_str == (LVM_READ | LVM_WRITE) ? "read/write" : "",
688 access_str == LVM_READ ? "read" : "",
689 access_str == LVM_WRITE ? "write" : "",
690 access_str == 0 ? "error" : "");
691 log_print("VG Status %s%sresizable",
692 vg_is_exported(vg) ? "exported/" : "",
693 vg_is_resizeable(vg) ? "" : "NOT ");
694 /* vg number not part of LVM2 design
695 log_print ("VG # %u\n", vg->vg_number);
697 if (vg_is_clustered(vg)) {
698 log_print("Clustered yes");
699 log_print("Shared %s",
700 vg->status & SHARED ? "yes" : "no");
703 log_print("MAX LV %u", vg->max_lv);
704 log_print("Cur LV %u", vg_visible_lvs(vg));
705 log_print("Open LV %u", lvs_in_vg_opened(vg));
706 /****** FIXME Max LV Size
707 log_print ( "MAX LV Size %s",
708 ( s1 = display_size ( LVM_LV_SIZE_MAX(vg))));
711 log_print("Max PV %u", vg->max_pv);
712 log_print("Cur PV %u", vg->pv_count);
713 log_print("Act PV %u", active_pvs);
715 log_print("VG Size %s",
716 display_size(vg->cmd,
717 (uint64_t) vg->extent_count * vg->extent_size));
719 log_print("PE Size %s",
720 display_size(vg->cmd, (uint64_t) vg->extent_size));
722 log_print("Total PE %u", vg->extent_count);
724 log_print("Alloc PE / Size %u / %s",
725 vg->extent_count - vg->free_count,
726 display_size(vg->cmd,
727 ((uint64_t) vg->extent_count - vg->free_count) *
730 log_print("Free PE / Size %u / %s", vg->free_count,
731 display_size(vg->cmd, vg_free(vg)));
733 if (!id_write_format(&vg->id, uuid, sizeof(uuid))) {
738 log_print("VG UUID %s", uuid);
742 void vgdisplay_colons(const struct volume_group *vg)
745 const char *access_str;
746 char uuid[64] __attribute__((aligned(8)));
748 active_pvs = vg->pv_count - vg_missing_pv_count(vg);
750 switch (vg->status & (LVM_READ | LVM_WRITE)) {
751 case LVM_READ | LVM_WRITE:
764 if (!id_write_format(&vg->id, uuid, sizeof(uuid))) {
769 log_print("%s:%s:%" PRIu64 ":-1:%u:%u:%u:-1:%u:%u:%u:%" PRIu64 ":%" PRIu32
774 /* internal volume group number; obsolete */
777 lvs_in_vg_opened(vg),
778 /* FIXME: maximum logical volume size */
782 (uint64_t) vg->extent_count * (vg->extent_size / 2),
785 vg->extent_count - vg->free_count,
787 uuid[0] ? uuid : "none");
790 void vgdisplay_short(const struct volume_group *vg)
792 log_print("\"%s\" %-9s [%-9s used / %s free]", vg->name,
793 /********* FIXME if "open" print "/used" else print "/idle"??? ******/
794 display_size(vg->cmd,
795 (uint64_t) vg->extent_count * vg->extent_size),
796 display_size(vg->cmd,
797 ((uint64_t) vg->extent_count -
798 vg->free_count) * vg->extent_size),
799 display_size(vg->cmd, vg_free(vg)));
802 void display_formats(const struct cmd_context *cmd)
804 const struct format_type *fmt;
806 dm_list_iterate_items(fmt, &cmd->formats) {
807 log_print("%s", fmt->name);
811 void display_segtypes(const struct cmd_context *cmd)
813 const struct segment_type *segtype;
815 dm_list_iterate_items(segtype, &cmd->segtypes) {
816 log_print("%s", segtype->name);
820 char yes_no_prompt(const char *prompt, ...)
827 if (c == '\n' || !c) {
828 va_start(ap, prompt);
834 if ((c = getchar()) == EOF) {
840 if ((c == 'y') || (c == 'n'))
842 } while (!ret || c != '\n');