4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 const char *command_name(struct cmd_context
*cmd
)
27 return cmd
->command
->name
;
31 * Strip dev_dir if present
33 char *skip_dev_dir(struct cmd_context
*cmd
, const char *vg_name
,
34 unsigned *dev_dir_found
)
36 const char *dmdir
= dm_dir();
37 size_t dmdir_len
= strlen(dmdir
), vglv_sz
;
38 char *vgname
, *lvname
, *layer
, *vglv
;
40 /* FIXME Do this properly */
41 if (*vg_name
== '/') {
42 while (*vg_name
== '/')
47 /* Reformat string if /dev/mapper found */
48 if (!strncmp(vg_name
, dmdir
, dmdir_len
) && vg_name
[dmdir_len
] == '/') {
52 while (*vg_name
== '/')
55 if (!dm_split_lvm_name(cmd
->mem
, vg_name
, &vgname
, &lvname
, &layer
) ||
57 log_error("skip_dev_dir: Couldn't split up device name %s",
59 return (char *) vg_name
;
61 vglv_sz
= strlen(vgname
) + strlen(lvname
) + 2;
62 if (!(vglv
= dm_pool_alloc(cmd
->mem
, vglv_sz
)) ||
63 dm_snprintf(vglv
, vglv_sz
, "%s%s%s", vgname
,
66 log_error("vg/lv string alloc failed");
67 return (char *) vg_name
;
72 if (!strncmp(vg_name
, cmd
->dev_dir
, strlen(cmd
->dev_dir
))) {
75 vg_name
+= strlen(cmd
->dev_dir
);
76 while (*vg_name
== '/')
78 } else if (dev_dir_found
)
81 return (char *) vg_name
;
85 * Metadata iteration functions
87 int process_each_lv_in_vg(struct cmd_context
*cmd
,
88 const struct volume_group
*vg
,
89 const struct dm_list
*arg_lvnames
,
90 const struct dm_list
*tags
,
92 process_single_lv_fn_t process_single
)
94 int ret_max
= ECMD_PROCESSED
;
96 unsigned process_all
= 0;
97 unsigned process_lv
= 0;
98 unsigned tags_supplied
= 0;
99 unsigned lvargs_supplied
= 0;
100 unsigned lvargs_matched
= 0;
104 if (!vg_check_status(vg
, EXPORTED_VG
))
107 if (tags
&& !dm_list_empty(tags
))
110 if (arg_lvnames
&& !dm_list_empty(arg_lvnames
))
113 /* Process all LVs in this VG if no restrictions given */
114 if (!tags_supplied
&& !lvargs_supplied
)
117 /* Or if VG tags match */
118 if (!process_lv
&& tags_supplied
&&
119 str_list_match_list(tags
, &vg
->tags
)) {
123 dm_list_iterate_items(lvl
, &vg
->lvs
) {
124 if (lvl
->lv
->status
& SNAPSHOT
)
127 if (lv_is_virtual_origin(lvl
->lv
) && !arg_count(cmd
, all_ARG
))
130 /* Should we process this LV? */
137 if (!process_lv
&& tags_supplied
&&
138 str_list_match_list(tags
, &lvl
->lv
->tags
)) {
143 if (lvargs_supplied
&&
144 str_list_match_item(arg_lvnames
, lvl
->lv
->name
)) {
152 ret
= process_single(cmd
, lvl
->lv
, handle
);
159 if (lvargs_supplied
&& lvargs_matched
!= dm_list_size(arg_lvnames
)) {
160 log_error("One or more specified logical volume(s) not found.");
161 if (ret_max
< ECMD_FAILED
)
162 ret_max
= ECMD_FAILED
;
168 int process_each_lv(struct cmd_context
*cmd
, int argc
, char **argv
,
169 uint32_t flags
, void *handle
,
170 int (*process_single
) (struct cmd_context
* cmd
,
171 struct logical_volume
* lv
,
175 int ret_max
= ECMD_PROCESSED
;
178 struct dm_list
*tags_arg
;
179 struct dm_list
*vgnames
; /* VGs to process */
180 struct str_list
*sll
, *strl
;
181 struct volume_group
*vg
;
182 struct dm_list tags
, lvnames
;
183 struct dm_list arg_lvnames
; /* Cmdline vgname or vgname/lvname */
190 dm_list_init(&arg_lvnames
);
193 struct dm_list arg_vgnames
;
195 log_verbose("Using logical volume(s) on command line");
196 dm_list_init(&arg_vgnames
);
198 for (; opt
< argc
; opt
++) {
199 const char *lv_name
= argv
[opt
];
201 unsigned dev_dir_found
= 0;
203 /* Do we have a tag or vgname or lvname? */
206 if (*vgname
== '@') {
207 if (!validate_name(vgname
+ 1)) {
208 log_error("Skipping invalid tag %s",
212 if (!str_list_add(cmd
->mem
, &tags
,
213 dm_pool_strdup(cmd
->mem
,
215 log_error("strlist allocation failed");
221 /* FIXME Jumbled parsing */
222 vgname
= skip_dev_dir(cmd
, vgname
, &dev_dir_found
);
224 if (*vgname
== '/') {
225 log_error("\"%s\": Invalid path for Logical "
226 "Volume", argv
[opt
]);
227 if (ret_max
< ECMD_FAILED
)
228 ret_max
= ECMD_FAILED
;
232 if (strchr(vgname
, '/')) {
234 lv_name
= strchr(vgname
, '/');
235 while (*lv_name
== '/')
237 if (!(vgname
= extract_vgname(cmd
, vgname
))) {
238 if (ret_max
< ECMD_FAILED
)
239 ret_max
= ECMD_FAILED
;
242 } else if (!dev_dir_found
&&
243 (vgname_def
= default_vgname(cmd
))) {
248 if (!str_list_add(cmd
->mem
, &arg_vgnames
,
249 dm_pool_strdup(cmd
->mem
, vgname
))) {
250 log_error("strlist allocation failed");
255 if (!str_list_add(cmd
->mem
, &arg_lvnames
,
256 dm_pool_strdup(cmd
->mem
,
258 log_error("strlist allocation failed");
262 vglv_sz
= strlen(vgname
) + strlen(lv_name
) + 2;
263 if (!(vglv
= dm_pool_alloc(cmd
->mem
, vglv_sz
)) ||
264 dm_snprintf(vglv
, vglv_sz
, "%s/%s", vgname
,
266 log_error("vg/lv string alloc failed");
269 if (!str_list_add(cmd
->mem
, &arg_lvnames
, vglv
)) {
270 log_error("strlist allocation failed");
275 vgnames
= &arg_vgnames
;
278 if (!argc
|| !dm_list_empty(&tags
)) {
279 log_verbose("Finding all logical volumes");
280 if (!(vgnames
= get_vgnames(cmd
, 0)) || dm_list_empty(vgnames
)) {
281 log_error("No volume groups found");
287 dm_list_iterate_items(strl
, vgnames
) {
289 if (is_orphan_vg(vgname
))
290 continue; /* FIXME Unnecessary? */
291 vg
= vg_read(cmd
, vgname
, NULL
, flags
);
293 if (vg_read_error(vg
)) {
295 if (ret_max
< ECMD_FAILED
) {
296 log_error("Skipping volume group %s", vgname
);
297 ret_max
= ECMD_FAILED
;
304 dm_list_init(&lvnames
); /* LVs to be processed in this VG */
305 dm_list_iterate_items(sll
, &arg_lvnames
) {
306 const char *vg_name
= sll
->str
;
307 const char *lv_name
= strchr(vg_name
, '/');
309 if ((!lv_name
&& !strcmp(vg_name
, vgname
))) {
310 /* Process all LVs in this VG */
312 dm_list_init(&lvnames
);
314 } else if (!strncmp(vg_name
, vgname
, strlen(vgname
)) &&
315 strlen(vgname
) == (size_t) (lv_name
- vg_name
)) {
316 if (!str_list_add(cmd
->mem
, &lvnames
,
317 dm_pool_strdup(cmd
->mem
,
319 log_error("strlist allocation failed");
326 ret
= process_each_lv_in_vg(cmd
, vg
, &lvnames
, tags_arg
,
327 handle
, process_single
);
328 unlock_and_release_vg(cmd
, vg
, vgname
);
338 int process_each_segment_in_pv(struct cmd_context
*cmd
,
339 struct volume_group
*vg
,
340 struct physical_volume
*pv
,
342 int (*process_single
) (struct cmd_context
* cmd
,
343 struct volume_group
* vg
,
344 struct pv_segment
* pvseg
,
347 struct pv_segment
*pvseg
;
349 const char *vg_name
= NULL
;
350 int ret_max
= ECMD_PROCESSED
;
352 struct volume_group
*old_vg
= vg
;
353 struct pv_segment _free_pv_segment
= { .pv
= pv
};
355 if (is_pv(pv
) && !vg
&& !is_orphan(pv
)) {
356 vg_name
= pv_vg_name(pv
);
358 vg
= vg_read(cmd
, vg_name
, NULL
, 0);
359 if (vg_read_error(vg
)) {
361 log_error("Skipping volume group %s", vg_name
);
366 * Replace possibly incomplete PV structure with new one
367 * allocated in vg_read_internal() path.
369 if (!(pvl
= find_pv_in_vg(vg
, pv_dev_name(pv
)))) {
370 log_error("Unable to find %s in volume group %s",
371 pv_dev_name(pv
), vg_name
);
379 if (dm_list_empty(&pv
->segments
)) {
380 ret
= process_single(cmd
, NULL
, &_free_pv_segment
, handle
);
384 dm_list_iterate_items(pvseg
, &pv
->segments
) {
385 ret
= process_single(cmd
, vg
, pvseg
, handle
);
393 unlock_vg(cmd
, vg_name
);
400 int process_each_segment_in_lv(struct cmd_context
*cmd
,
401 struct logical_volume
*lv
,
403 int (*process_single
) (struct cmd_context
* cmd
,
404 struct lv_segment
* seg
,
407 struct lv_segment
*seg
;
408 int ret_max
= ECMD_PROCESSED
;
411 dm_list_iterate_items(seg
, &lv
->segments
) {
412 ret
= process_single(cmd
, seg
, handle
);
422 static int _process_one_vg(struct cmd_context
*cmd
, const char *vg_name
,
424 struct dm_list
*tags
, struct dm_list
*arg_vgnames
,
425 uint32_t flags
, void *handle
, int ret_max
,
426 int (*process_single
) (struct cmd_context
* cmd
,
428 struct volume_group
* vg
,
431 struct volume_group
*vg
;
434 log_verbose("Finding volume group \"%s\"", vg_name
);
436 vg
= vg_read(cmd
, vg_name
, vgid
, flags
);
437 /* Allow FAILED_INCONSISTENT through only for vgcfgrestore */
438 if (vg_read_error(vg
) &&
439 !((vg_read_error(vg
) == FAILED_INCONSISTENT
) &&
440 (flags
& READ_ALLOW_INCONSISTENT
))) {
441 ret_max
= ECMD_FAILED
;
445 if (!dm_list_empty(tags
)) {
446 /* Only process if a tag matches or it's on arg_vgnames */
447 if (!str_list_match_item(arg_vgnames
, vg_name
) &&
448 !str_list_match_list(tags
, &vg
->tags
))
452 if ((ret
= process_single(cmd
, vg_name
, vg
,
457 if (vg_read_error(vg
))
460 unlock_and_release_vg(cmd
, vg
, vg_name
);
464 int process_each_vg(struct cmd_context
*cmd
, int argc
, char **argv
,
465 uint32_t flags
, void *handle
,
466 int (*process_single
) (struct cmd_context
* cmd
,
468 struct volume_group
* vg
,
472 int ret_max
= ECMD_PROCESSED
;
475 struct dm_list
*vgnames
, *vgids
;
476 struct dm_list arg_vgnames
, tags
;
478 const char *vg_name
, *vgid
;
481 dm_list_init(&arg_vgnames
);
484 log_verbose("Using volume group(s) on command line");
486 for (; opt
< argc
; opt
++) {
488 if (*vg_name
== '@') {
489 if (!validate_name(vg_name
+ 1)) {
490 log_error("Skipping invalid tag %s",
492 if (ret_max
< EINVALID_CMD_LINE
)
493 ret_max
= EINVALID_CMD_LINE
;
496 if (!str_list_add(cmd
->mem
, &tags
,
497 dm_pool_strdup(cmd
->mem
,
499 log_error("strlist allocation failed");
505 vg_name
= skip_dev_dir(cmd
, vg_name
, NULL
);
506 if (strchr(vg_name
, '/')) {
507 log_error("Invalid volume group name: %s",
509 if (ret_max
< EINVALID_CMD_LINE
)
510 ret_max
= EINVALID_CMD_LINE
;
513 if (!str_list_add(cmd
->mem
, &arg_vgnames
,
514 dm_pool_strdup(cmd
->mem
, vg_name
))) {
515 log_error("strlist allocation failed");
520 vgnames
= &arg_vgnames
;
523 if (!argc
|| !dm_list_empty(&tags
)) {
524 log_verbose("Finding all volume groups");
525 if (!(vgids
= get_vgids(cmd
, 0)) || dm_list_empty(vgids
)) {
526 log_error("No volume groups found");
529 dm_list_iterate_items(sl
, vgids
) {
531 if (!vgid
|| !(vg_name
= vgname_from_vgid(cmd
->mem
, vgid
)) ||
532 is_orphan_vg(vg_name
))
534 ret_max
= _process_one_vg(cmd
, vg_name
, vgid
, &tags
,
537 ret_max
, process_single
);
542 dm_list_iterate_items(sl
, vgnames
) {
544 if (is_orphan_vg(vg_name
))
545 continue; /* FIXME Unnecessary? */
546 ret_max
= _process_one_vg(cmd
, vg_name
, NULL
, &tags
,
549 ret_max
, process_single
);
558 int process_each_pv_in_vg(struct cmd_context
*cmd
, struct volume_group
*vg
,
559 const struct dm_list
*tags
, void *handle
,
560 process_single_pv_fn_t process_single
)
562 int ret_max
= ECMD_PROCESSED
;
566 dm_list_iterate_items(pvl
, &vg
->pvs
) {
567 if (tags
&& !dm_list_empty(tags
) &&
568 !str_list_match_list(tags
, &pvl
->pv
->tags
)) {
571 if ((ret
= process_single(cmd
, vg
, pvl
->pv
, handle
)) > ret_max
)
580 static int _process_all_devs(struct cmd_context
*cmd
, void *handle
,
581 int (*process_single
) (struct cmd_context
* cmd
,
582 struct volume_group
* vg
,
583 struct physical_volume
* pv
,
586 struct physical_volume
*pv
;
587 struct physical_volume pv_dummy
;
588 struct dev_iter
*iter
;
591 int ret_max
= ECMD_PROCESSED
;
594 if (!scan_vgs_for_pvs(cmd
)) {
599 if (!(iter
= dev_iter_create(cmd
->filter
, 1))) {
600 log_error("dev_iter creation failed");
604 while ((dev
= dev_iter_get(iter
))) {
605 if (!(pv
= pv_read(cmd
, dev_name(dev
), NULL
, NULL
, 0, 0))) {
606 memset(&pv_dummy
, 0, sizeof(pv_dummy
));
607 dm_list_init(&pv_dummy
.tags
);
608 dm_list_init(&pv_dummy
.segments
);
613 ret
= process_single(cmd
, NULL
, pv
, handle
);
620 dev_iter_destroy(iter
);
626 * If the lock_type is LCK_VG_READ (used only in reporting commands),
627 * we lock VG_GLOBAL to enable use of metadata cache.
628 * This can pause alongide pvscan or vgscan process for a while.
630 int process_each_pv(struct cmd_context
*cmd
, int argc
, char **argv
,
631 struct volume_group
*vg
, uint32_t flags
,
632 int scan_label_only
, void *handle
,
633 int (*process_single
) (struct cmd_context
* cmd
,
634 struct volume_group
* vg
,
635 struct physical_volume
* pv
,
639 int ret_max
= ECMD_PROCESSED
;
641 int lock_global
= !(flags
& READ_WITHOUT_LOCK
) && !(flags
& READ_FOR_UPDATE
);
644 struct physical_volume
*pv
;
645 struct dm_list
*pvslist
, *vgnames
;
647 struct str_list
*sll
;
653 if (lock_global
&& !lock_vol(cmd
, VG_GLOBAL
, LCK_VG_READ
)) {
654 log_error("Unable to obtain global lock.");
659 log_verbose("Using physical volume(s) on command line");
660 for (; opt
< argc
; opt
++) {
661 if (*argv
[opt
] == '@') {
662 tagname
= argv
[opt
] + 1;
664 if (!validate_name(tagname
)) {
665 log_error("Skipping invalid tag %s",
667 if (ret_max
< EINVALID_CMD_LINE
)
668 ret_max
= EINVALID_CMD_LINE
;
671 if (!str_list_add(cmd
->mem
, &tags
,
672 dm_pool_strdup(cmd
->mem
,
674 log_error("strlist allocation failed");
680 if (!(pvl
= find_pv_in_vg(vg
, argv
[opt
]))) {
681 log_error("Physical Volume \"%s\" not "
682 "found in Volume Group "
685 ret_max
= ECMD_FAILED
;
690 if (!(pv
= pv_read(cmd
, argv
[opt
], NULL
,
691 NULL
, 1, scan_label_only
))) {
692 log_error("Failed to read physical "
693 "volume \"%s\"", argv
[opt
]);
694 ret_max
= ECMD_FAILED
;
699 * If a PV has no MDAs it may appear to be an
700 * orphan until the metadata is read off
701 * another PV in the same VG. Detecting this
702 * means checking every VG by scanning every
705 if (!scanned
&& is_orphan(pv
)) {
706 if (!scan_label_only
&&
707 !scan_vgs_for_pvs(cmd
)) {
709 ret_max
= ECMD_FAILED
;
713 if (!(pv
= pv_read(cmd
, argv
[opt
],
716 log_error("Failed to read "
718 "\"%s\"", argv
[opt
]);
719 ret_max
= ECMD_FAILED
;
725 ret
= process_single(cmd
, vg
, pv
, handle
);
731 if (!dm_list_empty(&tags
) && (vgnames
= get_vgnames(cmd
, 0)) &&
732 !dm_list_empty(vgnames
)) {
733 dm_list_iterate_items(sll
, vgnames
) {
734 vg
= vg_read(cmd
, sll
->str
, NULL
, flags
);
735 if (vg_read_error(vg
)) {
736 ret_max
= ECMD_FAILED
;
742 ret
= process_each_pv_in_vg(cmd
, vg
, &tags
,
746 unlock_and_release_vg(cmd
, vg
, sll
->str
);
756 log_verbose("Using all physical volume(s) in "
758 ret
= process_each_pv_in_vg(cmd
, vg
, NULL
, handle
,
764 } else if (arg_count(cmd
, all_ARG
)) {
765 ret
= _process_all_devs(cmd
, handle
, process_single
);
771 log_verbose("Scanning for physical volume names");
773 if (!(pvslist
= get_pvs(cmd
)))
776 dm_list_iterate_items(pvl
, pvslist
) {
777 ret
= process_single(cmd
, NULL
, pvl
->pv
,
788 unlock_vg(cmd
, VG_GLOBAL
);
792 unlock_vg(cmd
, VG_GLOBAL
);
798 * Determine volume group name from a logical volume name
800 const char *extract_vgname(struct cmd_context
*cmd
, const char *lv_name
)
802 const char *vg_name
= lv_name
;
804 char *dev_dir
= cmd
->dev_dir
;
805 int dev_dir_provided
= 0;
808 if (vg_name
&& strchr(vg_name
, '/')) {
809 /* Strip dev_dir (optional) */
810 if (*vg_name
== '/') {
811 while (*vg_name
== '/')
815 if (!strncmp(vg_name
, dev_dir
, strlen(dev_dir
))) {
816 vg_name
+= strlen(dev_dir
);
817 dev_dir_provided
= 1;
818 while (*vg_name
== '/')
821 if (*vg_name
== '/') {
822 log_error("\"%s\": Invalid path for Logical "
827 /* Require exactly one set of consecutive slashes */
828 if ((st
= strchr(vg_name
, '/')))
832 if (!strchr(vg_name
, '/') || strchr(st
, '/')) {
833 log_error("\"%s\": Invalid path for Logical Volume",
838 vg_name
= dm_pool_strdup(cmd
->mem
, vg_name
);
840 log_error("Allocation of vg_name failed");
844 *strchr(vg_name
, '/') = '\0';
848 if (!(vg_name
= default_vgname(cmd
))) {
850 log_error("Path required for Logical Volume \"%s\"",
859 * Extract default volume group name from environment
861 char *default_vgname(struct cmd_context
*cmd
)
865 /* Take default VG from environment? */
866 vg_path
= getenv("LVM_VG_NAME");
870 vg_path
= skip_dev_dir(cmd
, vg_path
, NULL
);
872 if (strchr(vg_path
, '/')) {
873 log_error("Environment Volume Group in LVM_VG_NAME invalid: "
878 return dm_pool_strdup(cmd
->mem
, vg_path
);
882 * Process physical extent range specifiers
884 static int _add_pe_range(struct dm_pool
*mem
, const char *pvname
,
885 struct dm_list
*pe_ranges
, uint32_t start
, uint32_t count
)
887 struct pe_range
*per
;
889 log_debug("Adding PE range: start PE %" PRIu32
" length %" PRIu32
890 " on %s", start
, count
, pvname
);
892 /* Ensure no overlap with existing areas */
893 dm_list_iterate_items(per
, pe_ranges
) {
894 if (((start
< per
->start
) && (start
+ count
- 1 >= per
->start
))
895 || ((start
>= per
->start
) &&
896 (per
->start
+ per
->count
- 1) >= start
)) {
897 log_error("Overlapping PE ranges specified (%" PRIu32
898 "-%" PRIu32
", %" PRIu32
"-%" PRIu32
")"
900 start
, start
+ count
- 1, per
->start
,
901 per
->start
+ per
->count
- 1, pvname
);
906 if (!(per
= dm_pool_alloc(mem
, sizeof(*per
)))) {
907 log_error("Allocation of list failed");
913 dm_list_add(pe_ranges
, &per
->list
);
918 static int xstrtouint32(const char *s
, char **p
, int base
, uint32_t *result
)
923 ul
= strtoul(s
, p
, base
);
924 if (errno
|| *p
== s
|| (uint32_t) ul
!= ul
)
930 static int _parse_pes(struct dm_pool
*mem
, char *c
, struct dm_list
*pe_ranges
,
931 const char *pvname
, uint32_t size
)
936 /* Default to whole PV */
938 if (!_add_pe_range(mem
, pvname
, pe_ranges
, UINT32_C(0), size
))
949 /* Disallow :: and :\0 */
950 if (*c
== ':' || !*c
)
953 /* Default to whole range */
957 /* Start extent given? */
959 if (xstrtouint32(c
, &endptr
, 10, &start
))
962 /* Just one number given? */
963 if (!*c
|| *c
== ':')
970 if (xstrtouint32(c
, &endptr
, 10, &end
))
978 if ((start
> end
) || (end
> size
- 1)) {
979 log_error("PE range error: start extent %" PRIu32
" to "
980 "end extent %" PRIu32
, start
, end
);
984 if (!_add_pe_range(mem
, pvname
, pe_ranges
, start
, end
- start
+ 1))
992 log_error("Physical extent parsing error at %s", c
);
996 static int _create_pv_entry(struct dm_pool
*mem
, struct pv_list
*pvl
,
997 char *colon
, int allocatable_only
, struct dm_list
*r
)
1000 struct pv_list
*new_pvl
= NULL
, *pvl2
;
1001 struct dm_list
*pe_ranges
;
1003 pvname
= pv_dev_name(pvl
->pv
);
1004 if (allocatable_only
&& !(pvl
->pv
->status
& ALLOCATABLE_PV
)) {
1005 log_error("Physical volume %s not allocatable", pvname
);
1009 if (allocatable_only
&& (pvl
->pv
->status
& MISSING_PV
)) {
1010 log_error("Physical volume %s is missing", pvname
);
1014 if (allocatable_only
&&
1015 (pvl
->pv
->pe_count
== pvl
->pv
->pe_alloc_count
)) {
1016 log_error("No free extents on physical volume \"%s\"", pvname
);
1020 dm_list_iterate_items(pvl2
, r
)
1021 if (pvl
->pv
->dev
== pvl2
->pv
->dev
) {
1027 if (!(new_pvl
= dm_pool_alloc(mem
, sizeof(*new_pvl
)))) {
1028 log_error("Unable to allocate physical volume list.");
1032 memcpy(new_pvl
, pvl
, sizeof(*new_pvl
));
1034 if (!(pe_ranges
= dm_pool_alloc(mem
, sizeof(*pe_ranges
)))) {
1035 log_error("Allocation of pe_ranges list failed");
1038 dm_list_init(pe_ranges
);
1039 new_pvl
->pe_ranges
= pe_ranges
;
1040 dm_list_add(r
, &new_pvl
->list
);
1043 /* Determine selected physical extents */
1044 if (!_parse_pes(mem
, colon
, new_pvl
->pe_ranges
, pv_dev_name(pvl
->pv
),
1051 struct dm_list
*create_pv_list(struct dm_pool
*mem
, struct volume_group
*vg
, int argc
,
1052 char **argv
, int allocatable_only
)
1055 struct pv_list
*pvl
;
1056 struct dm_list tags
, arg_pvnames
;
1057 const char *pvname
= NULL
;
1058 char *colon
, *tagname
;
1061 /* Build up list of PVs */
1062 if (!(r
= dm_pool_alloc(mem
, sizeof(*r
)))) {
1063 log_error("Allocation of list failed");
1068 dm_list_init(&tags
);
1069 dm_list_init(&arg_pvnames
);
1071 for (i
= 0; i
< argc
; i
++) {
1072 if (*argv
[i
] == '@') {
1073 tagname
= argv
[i
] + 1;
1074 if (!validate_name(tagname
)) {
1075 log_error("Skipping invalid tag %s", tagname
);
1078 dm_list_iterate_items(pvl
, &vg
->pvs
) {
1079 if (str_list_match_item(&pvl
->pv
->tags
,
1081 if (!_create_pv_entry(mem
, pvl
, NULL
,
1092 if ((colon
= strchr(pvname
, ':'))) {
1093 if (!(pvname
= dm_pool_strndup(mem
, pvname
,
1096 log_error("Failed to clone PV name");
1101 if (!(pvl
= find_pv_in_vg(vg
, pvname
))) {
1102 log_error("Physical Volume \"%s\" not found in "
1103 "Volume Group \"%s\"", pvname
, vg
->name
);
1106 if (!_create_pv_entry(mem
, pvl
, colon
, allocatable_only
, r
))
1110 if (dm_list_empty(r
))
1111 log_error("No specified PVs have space available");
1113 return dm_list_empty(r
) ? NULL
: r
;
1116 struct dm_list
*clone_pv_list(struct dm_pool
*mem
, struct dm_list
*pvsl
)
1119 struct pv_list
*pvl
, *new_pvl
;
1121 /* Build up list of PVs */
1122 if (!(r
= dm_pool_alloc(mem
, sizeof(*r
)))) {
1123 log_error("Allocation of list failed");
1128 dm_list_iterate_items(pvl
, pvsl
) {
1129 if (!(new_pvl
= dm_pool_zalloc(mem
, sizeof(*new_pvl
)))) {
1130 log_error("Unable to allocate physical volume list.");
1134 memcpy(new_pvl
, pvl
, sizeof(*new_pvl
));
1135 dm_list_add(r
, &new_pvl
->list
);
1141 int apply_lvname_restrictions(const char *name
)
1143 if (!strncmp(name
, "snapshot", 8)) {
1144 log_error("Names starting \"snapshot\" are reserved. "
1145 "Please choose a different LV name.");
1149 if (!strncmp(name
, "pvmove", 6)) {
1150 log_error("Names starting \"pvmove\" are reserved. "
1151 "Please choose a different LV name.");
1155 if (strstr(name
, "_mlog")) {
1156 log_error("Names including \"_mlog\" are reserved. "
1157 "Please choose a different LV name.");
1161 if (strstr(name
, "_mimage")) {
1162 log_error("Names including \"_mimage\" are reserved. "
1163 "Please choose a different LV name.");
1167 if (strstr(name
, "_vorigin")) {
1168 log_error("Names including \"_vorigin\" are reserved. "
1169 "Please choose a different LV name.");
1176 int is_reserved_lvname(const char *name
)
1178 int rc
, old_suppress
;
1180 old_suppress
= log_suppress(2);
1181 rc
= !apply_lvname_restrictions(name
);
1182 log_suppress(old_suppress
);
1187 void vgcreate_params_set_defaults(struct vgcreate_params
*vp_def
,
1188 struct volume_group
*vg
)
1191 vp_def
->vg_name
= NULL
;
1192 vp_def
->extent_size
= vg
->extent_size
;
1193 vp_def
->max_pv
= vg
->max_pv
;
1194 vp_def
->max_lv
= vg
->max_lv
;
1195 vp_def
->alloc
= vg
->alloc
;
1196 vp_def
->clustered
= vg_is_clustered(vg
);
1198 vp_def
->vg_name
= NULL
;
1199 vp_def
->extent_size
= DEFAULT_EXTENT_SIZE
* 2;
1200 vp_def
->max_pv
= DEFAULT_MAX_PV
;
1201 vp_def
->max_lv
= DEFAULT_MAX_LV
;
1202 vp_def
->alloc
= DEFAULT_ALLOC_POLICY
;
1203 vp_def
->clustered
= DEFAULT_CLUSTERED
;
1208 * Set members of struct vgcreate_params from cmdline arguments.
1209 * Do preliminary validation with arg_*() interface.
1210 * Further, more generic validation is done in validate_vgcreate_params().
1211 * This function is to remain in tools directory.
1213 int vgcreate_params_set_from_args(struct cmd_context
*cmd
,
1214 struct vgcreate_params
*vp_new
,
1215 struct vgcreate_params
*vp_def
)
1217 vp_new
->vg_name
= skip_dev_dir(cmd
, vp_def
->vg_name
, NULL
);
1218 vp_new
->max_lv
= arg_uint_value(cmd
, maxlogicalvolumes_ARG
,
1220 vp_new
->max_pv
= arg_uint_value(cmd
, maxphysicalvolumes_ARG
,
1222 vp_new
->alloc
= arg_uint_value(cmd
, alloc_ARG
, vp_def
->alloc
);
1224 /* Units of 512-byte sectors */
1225 vp_new
->extent_size
=
1226 arg_uint_value(cmd
, physicalextentsize_ARG
, vp_def
->extent_size
);
1228 if (arg_count(cmd
, clustered_ARG
))
1230 !strcmp(arg_str_value(cmd
, clustered_ARG
,
1231 vp_def
->clustered
? "y":"n"), "y");
1233 /* Default depends on current locking type */
1234 vp_new
->clustered
= locking_is_clustered();
1236 if (arg_sign_value(cmd
, physicalextentsize_ARG
, 0) == SIGN_MINUS
) {
1237 log_error("Physical extent size may not be negative");
1241 if (arg_sign_value(cmd
, maxlogicalvolumes_ARG
, 0) == SIGN_MINUS
) {
1242 log_error("Max Logical Volumes may not be negative");
1246 if (arg_sign_value(cmd
, maxphysicalvolumes_ARG
, 0) == SIGN_MINUS
) {
1247 log_error("Max Physical Volumes may not be negative");
1254 int lv_refresh(struct cmd_context
*cmd
, struct logical_volume
*lv
)
1256 return suspend_lv(cmd
, lv
) && resume_lv(cmd
, lv
);
1259 int vg_refresh_visible(struct cmd_context
*cmd
, struct volume_group
*vg
)
1261 struct lv_list
*lvl
;
1264 dm_list_iterate_items(lvl
, &vg
->lvs
)
1265 if (lv_is_visible(lvl
->lv
))
1266 if (!lv_refresh(cmd
, lvl
->lv
))
1272 void lv_spawn_background_polling(struct cmd_context
*cmd
,
1273 struct logical_volume
*lv
)
1277 if ((lv
->status
& PVMOVE
) &&
1278 (pvname
= get_pvmove_pvname_from_lv_mirr(lv
))) {
1279 log_verbose("Spawning background pvmove process for %s",
1281 pvmove_poll(cmd
, pvname
, 1);
1282 } else if ((lv
->status
& LOCKED
) &&
1283 (pvname
= get_pvmove_pvname_from_lv(lv
))) {
1284 log_verbose("Spawning background pvmove process for %s",
1286 pvmove_poll(cmd
, pvname
, 1);
1289 if (lv
->status
& CONVERTING
) {
1290 log_verbose("Spawning background lvconvert process for %s",
1292 lvconvert_poll(cmd
, lv
, 1);
1297 * Intial sanity checking of non-recovery related command-line arguments.
1300 * pp: structure allocated by caller, fields written / validated here
1302 int pvcreate_params_validate(struct cmd_context
*cmd
,
1303 int argc
, char **argv
,
1304 struct pvcreate_params
*pp
)
1307 log_error("Please enter a physical volume path");
1311 if (arg_count(cmd
, yes_ARG
) && !arg_count(cmd
, force_ARG
)) {
1312 log_error("Option y can only be given with option f");
1316 pp
->yes
= arg_count(cmd
, yes_ARG
);
1317 pp
->force
= arg_count(cmd
, force_ARG
);
1319 if (arg_int_value(cmd
, labelsector_ARG
, 0) >= LABEL_SCAN_SECTORS
) {
1320 log_error("labelsector must be less than %lu",
1321 LABEL_SCAN_SECTORS
);
1324 pp
->labelsector
= arg_int64_value(cmd
, labelsector_ARG
,
1325 DEFAULT_LABELSECTOR
);
1328 if (!(cmd
->fmt
->features
& FMT_MDAS
) &&
1329 (arg_count(cmd
, pvmetadatacopies_ARG
) ||
1330 arg_count(cmd
, metadatasize_ARG
) ||
1331 arg_count(cmd
, dataalignment_ARG
) ||
1332 arg_count(cmd
, dataalignmentoffset_ARG
))) {
1333 log_error("Metadata and data alignment parameters only "
1334 "apply to text format.");
1338 if (arg_count(cmd
, pvmetadatacopies_ARG
) &&
1339 arg_int_value(cmd
, pvmetadatacopies_ARG
, -1) > 2) {
1340 log_error("Metadatacopies may only be 0, 1 or 2");
1344 if (arg_count(cmd
, zero_ARG
))
1345 pp
->zero
= strcmp(arg_str_value(cmd
, zero_ARG
, "y"), "n");
1347 if (arg_sign_value(cmd
, dataalignment_ARG
, 0) == SIGN_MINUS
) {
1348 log_error("Physical volume data alignment may not be negative");
1351 pp
->data_alignment
= arg_uint64_value(cmd
, dataalignment_ARG
, UINT64_C(0));
1353 if (pp
->data_alignment
> ULONG_MAX
) {
1354 log_error("Physical volume data alignment is too big.");
1358 if (pp
->data_alignment
&& pp
->pe_start
) {
1359 if (pp
->pe_start
% pp
->data_alignment
)
1360 log_warn("WARNING: Ignoring data alignment %" PRIu64
1361 " incompatible with --restorefile value (%"
1362 PRIu64
").", pp
->data_alignment
, pp
->pe_start
);
1363 pp
->data_alignment
= 0;
1366 if (arg_sign_value(cmd
, dataalignmentoffset_ARG
, 0) == SIGN_MINUS
) {
1367 log_error("Physical volume data alignment offset may not be negative");
1370 pp
->data_alignment_offset
= arg_uint64_value(cmd
, dataalignmentoffset_ARG
, UINT64_C(0));
1372 if (pp
->data_alignment_offset
> ULONG_MAX
) {
1373 log_error("Physical volume data alignment offset is too big.");
1377 if (pp
->data_alignment_offset
&& pp
->pe_start
) {
1378 log_warn("WARNING: Ignoring data alignment offset %" PRIu64
1379 " incompatible with --restorefile value (%"
1380 PRIu64
").", pp
->data_alignment_offset
, pp
->pe_start
);
1381 pp
->data_alignment_offset
= 0;
1384 if (arg_sign_value(cmd
, metadatasize_ARG
, 0) == SIGN_MINUS
) {
1385 log_error("Metadata size may not be negative");
1389 pp
->pvmetadatasize
= arg_uint64_value(cmd
, metadatasize_ARG
, UINT64_C(0));
1390 if (!pp
->pvmetadatasize
)
1391 pp
->pvmetadatasize
= find_config_tree_int(cmd
,
1392 "metadata/pvmetadatasize",
1393 DEFAULT_PVMETADATASIZE
);
1395 pp
->pvmetadatacopies
= arg_int_value(cmd
, pvmetadatacopies_ARG
, -1);
1396 if (pp
->pvmetadatacopies
< 0)
1397 pp
->pvmetadatacopies
= find_config_tree_int(cmd
,
1398 "metadata/pvmetadatacopies",
1399 DEFAULT_PVMETADATACOPIES
);