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
21 struct lvcreate_cmdline_params {
22 percent_type_t percent;
28 static int _lvcreate_name_params(struct lvcreate_params *lp,
29 struct cmd_context *cmd,
30 int *pargc, char ***pargv)
33 char **argv = *pargv, *ptr;
36 lp->lv_name = arg_str_value(cmd, name_ARG, NULL);
38 if (lp->snapshot && !arg_count(cmd, virtualsize_ARG)) {
40 log_error("Please specify a logical volume to act as "
41 "the snapshot origin.");
46 (*pargv)++, (*pargc)--;
47 if (!(lp->vg_name = extract_vgname(cmd, lp->origin))) {
48 log_error("The origin name should include the "
53 /* Strip the volume group from the origin */
54 if ((ptr = strrchr(lp->origin, (int) '/')))
59 * If VG not on command line, try -n arg and then
63 if (!(lp->vg_name = extract_vgname(cmd, lp->lv_name))) {
64 log_error("Please provide a volume group name");
69 vg_name = skip_dev_dir(cmd, argv[0], NULL);
70 if (strrchr(vg_name, '/')) {
71 log_error("Volume group name expected "
77 * Ensure lv_name doesn't contain a
80 if (lp->lv_name && strchr(lp->lv_name, '/')) {
82 extract_vgname(cmd, lp->lv_name)))
85 if (strcmp(lp->vg_name, vg_name)) {
86 log_error("Inconsistent volume group "
88 "given: \"%s\" and \"%s\"",
89 lp->vg_name, vg_name);
94 lp->vg_name = vg_name;
95 (*pargv)++, (*pargc)--;
99 if (!validate_name(lp->vg_name)) {
100 log_error("Volume group name %s has invalid characters",
106 if ((ptr = strrchr(lp->lv_name, '/')))
107 lp->lv_name = ptr + 1;
109 if (!apply_lvname_restrictions(lp->lv_name))
112 if (!validate_name(lp->lv_name)) {
113 log_error("Logical volume name \"%s\" is invalid",
123 * Update extents parameters based on other parameters which affect the size
125 * NOTE: We must do this here because of the percent_t typedef and because we
128 static int _update_extents_params(struct volume_group *vg,
129 struct lvcreate_params *lp,
130 struct lvcreate_cmdline_params *lcp)
132 uint32_t pv_extent_count;
133 struct logical_volume *origin = NULL;
136 !(lp->extents = extents_from_size(vg->cmd, lcp->size,
140 if (lp->voriginsize &&
141 !(lp->voriginextents = extents_from_size(vg->cmd, lp->voriginsize,
146 * Create the pv list before we parse lcp->percent - might be
150 if (!(lp->pvh = create_pv_list(vg->cmd->mem, vg,
151 lcp->pv_count, lcp->pvs, 1)))
156 switch(lcp->percent) {
158 lp->extents = lp->extents * vg->extent_count / 100;
161 lp->extents = lp->extents * vg->free_count / 100;
165 lp->extents = lp->extents * vg->extent_count / 100;
167 pv_extent_count = pv_list_extents_free(lp->pvh);
168 lp->extents = lp->extents * pv_extent_count / 100;
172 log_error("Please express size as %%VG, %%PVS, or "
176 if (lp->snapshot && lp->origin &&
177 !(origin = find_lv(vg, lp->origin))) {
178 log_error("Couldn't find origin volume '%s'.",
183 log_error(INTERNAL_ERROR "Couldn't find origin volume.");
186 lp->extents = lp->extents * origin->le_count / 100;
194 static int _read_size_params(struct lvcreate_params *lp,
195 struct lvcreate_cmdline_params *lcp,
196 struct cmd_context *cmd)
198 if (arg_count(cmd, extents_ARG) + arg_count(cmd, size_ARG) != 1) {
199 log_error("Please specify either size or extents (not both)");
203 if (arg_count(cmd, extents_ARG)) {
204 if (arg_sign_value(cmd, extents_ARG, 0) == SIGN_MINUS) {
205 log_error("Negative number of extents is invalid");
208 lp->extents = arg_uint_value(cmd, extents_ARG, 0);
209 lcp->percent = arg_percent_value(cmd, extents_ARG, PERCENT_NONE);
212 /* Size returned in kilobyte units; held in sectors */
213 if (arg_count(cmd, size_ARG)) {
214 if (arg_sign_value(cmd, size_ARG, 0) == SIGN_MINUS) {
215 log_error("Negative size is invalid");
218 lcp->size = arg_uint64_value(cmd, size_ARG, UINT64_C(0));
219 lcp->percent = PERCENT_NONE;
222 /* Size returned in kilobyte units; held in sectors */
223 if (arg_count(cmd, virtualsize_ARG)) {
224 if (arg_sign_value(cmd, virtualsize_ARG, 0) == SIGN_MINUS) {
225 log_error("Negative virtual origin size is invalid");
228 lp->voriginsize = arg_uint64_value(cmd, virtualsize_ARG,
230 if (!lp->voriginsize) {
231 log_error("Virtual origin size may not be zero");
240 * Generic mirror parameter checks.
241 * FIXME: Should eventually be moved into lvm library.
243 static int _validate_mirror_params(const struct cmd_context *cmd __attribute__((unused)),
244 const struct lvcreate_params *lp)
246 int pagesize = lvm_getpagesize();
248 if (lp->region_size & (lp->region_size - 1)) {
249 log_error("Region size (%" PRIu32 ") must be a power of 2",
254 if (lp->region_size % (pagesize >> SECTOR_SHIFT)) {
255 log_error("Region size (%" PRIu32 ") must be a multiple of "
256 "machine memory page size (%d)",
257 lp->region_size, pagesize >> SECTOR_SHIFT);
261 if (!lp->region_size) {
262 log_error("Non-zero region size must be supplied.");
269 static int _read_mirror_params(struct lvcreate_params *lp,
270 struct cmd_context *cmd)
273 const char *mirrorlog;
274 int corelog = arg_count(cmd, corelog_ARG);
276 mirrorlog = arg_str_value(cmd, mirrorlog_ARG,
277 corelog ? "core" : DEFAULT_MIRRORLOG);
279 if (strcmp("core", mirrorlog) && corelog) {
280 log_error("Please use only one of --mirrorlog or --corelog");
284 if (!strcmp("mirrored", mirrorlog)) {
286 } else if (!strcmp("disk", mirrorlog)) {
288 } else if (!strcmp("core", mirrorlog))
291 log_error("Unknown mirrorlog type: %s", mirrorlog);
295 log_verbose("Setting logging type to %s", mirrorlog);
297 lp->nosync = arg_is_set(cmd, nosync_ARG);
299 if (arg_count(cmd, regionsize_ARG)) {
300 if (arg_sign_value(cmd, regionsize_ARG, 0) == SIGN_MINUS) {
301 log_error("Negative regionsize is invalid");
304 lp->region_size = arg_uint_value(cmd, regionsize_ARG, 0);
306 region_size = 2 * find_config_tree_int(cmd,
307 "activation/mirror_region_size",
308 DEFAULT_MIRROR_REGION_SIZE);
309 if (region_size < 0) {
310 log_error("Negative regionsize in configuration file "
314 lp->region_size = region_size;
317 if (!_validate_mirror_params(cmd, lp))
323 static int _lvcreate_params(struct lvcreate_params *lp,
324 struct lvcreate_cmdline_params *lcp,
325 struct cmd_context *cmd,
326 int argc, char **argv)
330 struct arg_value_group_list *current_group;
333 memset(lp, 0, sizeof(*lp));
334 memset(lcp, 0, sizeof(*lcp));
335 dm_list_init(&lp->tags);
338 * Check selected options are compatible and determine segtype
340 lp->segtype = get_segtype_from_string(cmd, arg_str_value(cmd, type_ARG, "striped"));
342 if (arg_count(cmd, snapshot_ARG) || seg_is_snapshot(lp) ||
343 arg_count(cmd, virtualsize_ARG))
348 /* Default to 2 mirrored areas if --type mirror */
349 if (seg_is_mirrored(lp))
352 if (arg_count(cmd, mirrors_ARG)) {
353 lp->mirrors = arg_uint_value(cmd, mirrors_ARG, 0) + 1;
354 if (lp->mirrors == 1)
355 log_print("Redundant mirrors argument: default is 0");
356 if (arg_sign_value(cmd, mirrors_ARG, 0) == SIGN_MINUS) {
357 log_error("Mirrors argument may not be negative");
363 if (arg_count(cmd, zero_ARG)) {
364 log_error("-Z is incompatible with snapshots");
367 if (arg_sign_value(cmd, chunksize_ARG, 0) == SIGN_MINUS) {
368 log_error("Negative chunk size is invalid");
371 lp->chunk_size = arg_uint_value(cmd, chunksize_ARG, 8);
372 if (lp->chunk_size < 8 || lp->chunk_size > 1024 ||
373 (lp->chunk_size & (lp->chunk_size - 1))) {
374 log_error("Chunk size must be a power of 2 in the "
378 log_verbose("Setting chunksize to %d sectors.", lp->chunk_size);
380 if (!(lp->segtype = get_segtype_from_string(cmd, "snapshot")))
383 if (arg_count(cmd, chunksize_ARG)) {
384 log_error("-c is only available with snapshots");
389 if (lp->mirrors > 1) {
391 log_error("mirrors and snapshots are currently "
396 if (!(lp->segtype = get_segtype_from_string(cmd, "striped")))
399 if (arg_count(cmd, corelog_ARG)) {
400 log_error("--corelog is only available with mirrors");
404 if (arg_count(cmd, mirrorlog_ARG)) {
405 log_error("--mirrorlog is only available with mirrors");
409 if (arg_count(cmd, nosync_ARG)) {
410 log_error("--nosync is only available with mirrors");
415 if (activation() && lp->segtype->ops->target_present &&
416 !lp->segtype->ops->target_present(cmd, NULL, NULL)) {
417 log_error("%s: Required device-mapper target(s) not "
418 "detected in your kernel", lp->segtype->name);
422 if (!get_activation_monitoring_mode(cmd, NULL,
423 &lp->activation_monitoring))
426 if (!_lvcreate_name_params(lp, cmd, &argc, &argv) ||
427 !_read_size_params(lp, lcp, cmd) ||
428 !get_stripe_params(cmd, &lp->stripes, &lp->stripe_size) ||
429 !_read_mirror_params(lp, cmd))
433 * Should we zero the lv.
435 lp->zero = strcmp(arg_str_value(cmd, zero_ARG,
436 (lp->segtype->flags & SEG_CANNOT_BE_ZEROED) ? "n" : "y"), "n");
441 contiguous = strcmp(arg_str_value(cmd, contiguous_ARG, "n"), "n");
443 lp->alloc = contiguous ? ALLOC_CONTIGUOUS : ALLOC_INHERIT;
445 lp->alloc = arg_uint_value(cmd, alloc_ARG, lp->alloc);
447 if (contiguous && (lp->alloc != ALLOC_CONTIGUOUS)) {
448 log_error("Conflicting contiguous and alloc arguments");
452 if (lp->mirrors > DEFAULT_MIRROR_MAX_IMAGES) {
453 log_error("Only up to %d images in mirror supported currently.",
454 DEFAULT_MIRROR_MAX_IMAGES);
461 lp->read_ahead = arg_uint_value(cmd, readahead_ARG,
462 cmd->default_settings.read_ahead);
463 pagesize = lvm_getpagesize() >> SECTOR_SHIFT;
464 if (lp->read_ahead != DM_READ_AHEAD_AUTO &&
465 lp->read_ahead != DM_READ_AHEAD_NONE &&
466 lp->read_ahead % pagesize) {
467 if (lp->read_ahead < pagesize)
468 lp->read_ahead = pagesize;
470 lp->read_ahead = (lp->read_ahead / pagesize) * pagesize;
471 log_warn("WARNING: Overriding readahead to %u sectors, a multiple "
472 "of %uK page size.", lp->read_ahead, pagesize >> 1);
478 lp->permission = arg_uint_value(cmd, permission_ARG,
479 LVM_READ | LVM_WRITE);
481 /* Must not zero read only volume */
482 if (!(lp->permission & LVM_WRITE))
485 lp->minor = arg_int_value(cmd, minor_ARG, -1);
486 lp->major = arg_int_value(cmd, major_ARG, -1);
488 /* Persistent minor */
489 if (arg_count(cmd, persistent_ARG)) {
490 if (!strcmp(arg_str_value(cmd, persistent_ARG, "n"), "y")) {
491 if (lp->minor == -1) {
492 log_error("Please specify minor number with "
493 "--minor when using -My");
496 if (lp->major == -1) {
497 log_error("Please specify major number with "
498 "--major when using -My");
502 if ((lp->minor != -1) || (lp->major != -1)) {
503 log_error("--major and --minor incompatible "
508 } else if (arg_count(cmd, minor_ARG) || arg_count(cmd, major_ARG)) {
509 log_error("--major and --minor require -My");
513 dm_list_iterate_items(current_group, &cmd->arg_value_groups) {
514 if (!grouped_arg_is_set(current_group->arg_values, addtag_ARG))
517 if (!(tag = grouped_arg_str_value(current_group->arg_values, addtag_ARG, NULL))) {
518 log_error("Failed to get tag");
522 if (!str_list_add(cmd->mem, &lp->tags, tag)) {
523 log_error("Unable to allocate memory for tag %s", tag);
528 lcp->pv_count = argc;
534 int lvcreate(struct cmd_context *cmd, int argc, char **argv)
536 int r = ECMD_PROCESSED;
537 struct lvcreate_params lp;
538 struct lvcreate_cmdline_params lcp;
539 struct volume_group *vg;
541 memset(&lp, 0, sizeof(lp));
543 if (!_lvcreate_params(&lp, &lcp, cmd, argc, argv))
544 return EINVALID_CMD_LINE;
546 log_verbose("Finding volume group \"%s\"", lp.vg_name);
547 vg = vg_read_for_update(cmd, lp.vg_name, NULL, 0);
548 if (vg_read_error(vg)) {
554 if (!_update_extents_params(vg, &lp, &lcp)) {
559 if (!lv_create_single(vg, &lp)) {
564 unlock_and_free_vg(cmd, vg, lp.vg_name);