2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2009 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 struct lvresize_params
{
28 const struct segment_type
*segtype
;
34 percent_type_t percent
;
49 static int _validate_stripesize(struct cmd_context
*cmd
,
50 const struct volume_group
*vg
,
51 struct lvresize_params
*lp
)
53 if (arg_sign_value(cmd
, stripesize_ARG
, 0) == SIGN_MINUS
) {
54 log_error("Stripesize may not be negative.");
58 if (arg_uint64_value(cmd
, stripesize_ARG
, 0) > STRIPE_SIZE_LIMIT
* 2) {
59 log_error("Stripe size cannot be larger than %s",
60 display_size(cmd
, (uint64_t) STRIPE_SIZE_LIMIT
));
64 if (!(vg
->fid
->fmt
->features
& FMT_SEGMENTS
))
65 log_warn("Varied stripesize not supported. Ignoring.");
66 else if (arg_uint_value(cmd
, stripesize_ARG
, 0) > vg
->extent_size
* 2) {
67 log_error("Reducing stripe size %s to maximum, "
68 "physical extent size %s",
70 (uint64_t) arg_uint_value(cmd
, stripesize_ARG
, 0)),
71 display_size(cmd
, (uint64_t) vg
->extent_size
));
72 lp
->stripe_size
= vg
->extent_size
;
74 lp
->stripe_size
= arg_uint_value(cmd
, stripesize_ARG
, 0);
76 if (lp
->stripe_size
& (lp
->stripe_size
- 1)) {
77 log_error("Stripe size must be power of 2");
84 static int _request_confirmation(struct cmd_context
*cmd
,
85 const struct volume_group
*vg
,
86 const struct logical_volume
*lv
,
87 const struct lvresize_params
*lp
)
91 memset(&info
, 0, sizeof(info
));
93 if (!lv_info(cmd
, lv
, 0, &info
, 1, 0) && driver_version(NULL
, 0)) {
94 log_error("lv_info failed: aborting");
100 log_error("Logical volume %s must be activated "
101 "before resizing filesystem", lp
->lv_name
);
110 log_warn("WARNING: Reducing active%s logical volume to %s",
111 info
.open_count
? " and open" : "",
112 display_size(cmd
, (uint64_t) lp
->extents
* vg
->extent_size
));
114 log_warn("THIS MAY DESTROY YOUR DATA (filesystem etc.)");
116 if (!arg_count(cmd
, force_ARG
)) {
117 if (yes_no_prompt("Do you really want to reduce %s? [y/n]: ",
118 lp
->lv_name
) == 'n') {
119 log_error("Logical volume %s NOT reduced", lp
->lv_name
);
129 enum fsadm_cmd_e
{ FSADM_CMD_CHECK
, FSADM_CMD_RESIZE
};
130 #define FSADM_CMD "fsadm"
131 #define FSADM_CMD_MAX_ARGS 6
132 #define FSADM_CHECK_FAILS_FOR_MOUNTED 3 /* shell exist status code */
135 * FSADM_CMD --dry-run --verbose --force check lv_path
136 * FSADM_CMD --dry-run --verbose --force resize lv_path size
138 static int _fsadm_cmd(struct cmd_context
*cmd
,
139 const struct volume_group
*vg
,
140 const struct lvresize_params
*lp
,
141 enum fsadm_cmd_e fcmd
,
144 char lv_path
[PATH_MAX
];
145 char size_buf
[SIZE_BUF
];
146 const char *argv
[FSADM_CMD_MAX_ARGS
+ 2];
149 argv
[i
++] = FSADM_CMD
;
152 argv
[i
++] = "--dry-run";
154 if (verbose_level() >= _LOG_NOTICE
)
155 argv
[i
++] = "--verbose";
157 if (arg_count(cmd
, force_ARG
))
158 argv
[i
++] = "--force";
160 argv
[i
++] = (fcmd
== FSADM_CMD_RESIZE
) ? "resize" : "check";
162 if (dm_snprintf(lv_path
, PATH_MAX
, "%s%s/%s", cmd
->dev_dir
, lp
->vg_name
,
164 log_error("Couldn't create LV path for %s", lp
->lv_name
);
170 if (fcmd
== FSADM_CMD_RESIZE
) {
171 if (dm_snprintf(size_buf
, SIZE_BUF
, "%" PRIu64
"K",
172 (uint64_t) lp
->extents
* vg
->extent_size
/ 2) < 0) {
173 log_error("Couldn't generate new LV size string");
177 argv
[i
++] = size_buf
;
182 return exec_cmd(cmd
, argv
, status
, 1);
185 static int _lvresize_params(struct cmd_context
*cmd
, int argc
, char **argv
,
186 struct lvresize_params
*lp
)
188 const char *cmd_name
;
190 unsigned dev_dir_found
= 0;
191 int use_policy
= arg_count(cmd
, use_policies_ARG
);
193 lp
->sign
= SIGN_NONE
;
196 cmd_name
= command_name(cmd
);
197 if (!strcmp(cmd_name
, "lvreduce"))
198 lp
->resize
= LV_REDUCE
;
199 if (!strcmp(cmd_name
, "lvextend"))
200 lp
->resize
= LV_EXTEND
;
203 /* do nothing; _lvresize will handle --use-policies itself */
205 lp
->sign
= SIGN_PLUS
;
206 lp
->percent
= PERCENT_LV
;
209 * Allow omission of extents and size if the user has given us
210 * one or more PVs. Most likely, the intent was "resize this
211 * LV the best you can with these PVs"
213 if ((arg_count(cmd
, extents_ARG
) + arg_count(cmd
, size_ARG
) == 0) &&
216 lp
->percent
= PERCENT_PVS
;
217 lp
->sign
= SIGN_PLUS
;
218 } else if ((arg_count(cmd
, extents_ARG
) +
219 arg_count(cmd
, size_ARG
) != 1)) {
220 log_error("Please specify either size or extents but not "
225 if (arg_count(cmd
, extents_ARG
)) {
226 lp
->extents
= arg_uint_value(cmd
, extents_ARG
, 0);
227 lp
->sign
= arg_sign_value(cmd
, extents_ARG
, SIGN_NONE
);
228 lp
->percent
= arg_percent_value(cmd
, extents_ARG
, PERCENT_NONE
);
231 /* Size returned in kilobyte units; held in sectors */
232 if (arg_count(cmd
, size_ARG
)) {
233 lp
->size
= arg_uint64_value(cmd
, size_ARG
, 0);
234 lp
->sign
= arg_sign_value(cmd
, size_ARG
, SIGN_NONE
);
235 lp
->percent
= PERCENT_NONE
;
239 if (lp
->resize
== LV_EXTEND
&& lp
->sign
== SIGN_MINUS
) {
240 log_error("Negative argument not permitted - use lvreduce");
244 if (lp
->resize
== LV_REDUCE
&& lp
->sign
== SIGN_PLUS
) {
245 log_error("Positive sign not permitted - use lvextend");
249 lp
->resizefs
= arg_is_set(cmd
, resizefs_ARG
);
250 lp
->nofsck
= arg_is_set(cmd
, nofsck_ARG
);
253 log_error("Please provide the logical volume name");
257 lp
->lv_name
= argv
[0];
261 if (!(lp
->lv_name
= skip_dev_dir(cmd
, lp
->lv_name
, &dev_dir_found
)) ||
262 !(lp
->vg_name
= extract_vgname(cmd
, lp
->lv_name
))) {
263 log_error("Please provide a volume group name");
267 if (!validate_name(lp
->vg_name
)) {
268 log_error("Volume group name %s has invalid characters",
273 if ((st
= strrchr(lp
->lv_name
, '/')))
274 lp
->lv_name
= st
+ 1;
282 static int _adjust_policy_params(struct cmd_context
*cmd
,
283 struct logical_volume
*lv
, struct lvresize_params
*lp
)
286 int policy_threshold
, policy_amount
;
289 find_config_tree_int(cmd
, "activation/snapshot_autoextend_threshold",
290 DEFAULT_SNAPSHOT_AUTOEXTEND_THRESHOLD
) * PERCENT_1
;
292 find_config_tree_int(cmd
, "activation/snapshot_autoextend_percent",
293 DEFAULT_SNAPSHOT_AUTOEXTEND_PERCENT
);
295 if (policy_threshold
>= PERCENT_100
)
296 return 1; /* nothing to do */
298 if (!lv_snapshot_percent(lv
, &percent
))
301 if (!(PERCENT_0
< percent
&& percent
< PERCENT_100
) || percent
<= policy_threshold
)
302 return 1; /* nothing to do */
304 lp
->extents
= policy_amount
;
308 static uint32_t lvseg_get_stripes(struct lv_segment
*seg
, uint32_t *stripesize
)
311 struct lv_segment
*seg_mirr
;
313 /* If segment mirrored, check if images are striped */
314 if (seg_is_mirrored(seg
))
315 for (s
= 0; s
< seg
->area_count
; s
++) {
316 if (seg_type(seg
, s
) != AREA_LV
)
318 seg_mirr
= first_seg(seg_lv(seg
, s
));
320 if (seg_is_striped(seg_mirr
)) {
327 if (seg_is_striped(seg
)) {
328 *stripesize
= seg
->stripe_size
;
329 return seg
->area_count
;
336 static int _lvresize(struct cmd_context
*cmd
, struct volume_group
*vg
,
337 struct lvresize_params
*lp
)
339 struct logical_volume
*lv
;
341 uint32_t stripesize_extents
= 0;
342 uint32_t seg_stripes
= 0, seg_stripesize
= 0, seg_size
= 0;
343 uint32_t seg_mirrors
= 0;
344 uint32_t extents_used
= 0;
346 uint32_t pv_extent_count
= 0;
347 alloc_policy_t alloc
;
348 struct logical_volume
*lock_lv
;
350 struct lv_segment
*seg
, *uninitialized_var(mirr_seg
);
351 uint32_t seg_extents
;
354 struct dm_list
*pvh
= NULL
;
355 int use_policy
= arg_count(cmd
, use_policies_ARG
);
358 if (!(lvl
= find_lv_in_vg(vg
, lp
->lv_name
))) {
359 log_error("Logical volume %s not found in volume group %s",
360 lp
->lv_name
, lp
->vg_name
);
364 if (arg_count(cmd
, stripes_ARG
)) {
365 if (vg
->fid
->fmt
->features
& FMT_SEGMENTS
)
366 lp
->stripes
= arg_uint_value(cmd
, stripes_ARG
, 1);
368 log_warn("Varied striping not supported. Ignoring.");
371 if (arg_count(cmd
, mirrors_ARG
)) {
372 if (vg
->fid
->fmt
->features
& FMT_SEGMENTS
)
373 lp
->mirrors
= arg_uint_value(cmd
, mirrors_ARG
, 1) + 1;
375 log_warn("Mirrors not supported. Ignoring.");
376 if (arg_sign_value(cmd
, mirrors_ARG
, 0) == SIGN_MINUS
) {
377 log_error("Mirrors argument may not be negative");
378 return EINVALID_CMD_LINE
;
382 if (arg_count(cmd
, stripesize_ARG
) &&
383 !_validate_stripesize(cmd
, vg
, lp
))
384 return EINVALID_CMD_LINE
;
389 if (!lv_is_cow(lv
)) {
390 log_error("Can't use policy-based resize for non-snapshot volumes.");
393 _adjust_policy_params(cmd
, lv
, lp
);
396 if (!lv_is_visible(lv
)) {
397 log_error("Can't resize internal logical volume %s", lv
->name
);
401 if (lv
->status
& LOCKED
) {
402 log_error("Can't resize locked LV %s", lv
->name
);
406 if (lv
->status
& CONVERTING
) {
407 log_error("Can't resize %s while lvconvert in progress", lv
->name
);
411 alloc
= arg_uint_value(cmd
, alloc_ARG
, lv
->alloc
);
414 if (lp
->size
% vg
->extent_size
) {
415 if (lp
->sign
== SIGN_MINUS
)
416 lp
->size
-= lp
->size
% vg
->extent_size
;
418 lp
->size
+= vg
->extent_size
-
419 (lp
->size
% vg
->extent_size
);
421 log_print("Rounding up size to full physical extent %s",
422 display_size(cmd
, (uint64_t) lp
->size
));
425 lp
->extents
= lp
->size
/ vg
->extent_size
;
428 if (!(pvh
= lp
->argc
? create_pv_list(cmd
->mem
, vg
, lp
->argc
,
429 lp
->argv
, 1) : &vg
->pvs
)) {
434 switch(lp
->percent
) {
436 lp
->extents
= lp
->extents
* vg
->extent_count
/ 100;
439 lp
->extents
= lp
->extents
* vg
->free_count
/ 100;
442 lp
->extents
= lp
->extents
* lv
->le_count
/ 100;
446 pv_extent_count
= pv_list_extents_free(pvh
);
447 lp
->extents
= lp
->extents
* pv_extent_count
/ 100;
449 lp
->extents
= lp
->extents
* vg
->extent_count
/ 100;
452 if (!lv_is_cow(lv
)) {
453 log_error("Specified LV does not have an origin LV.");
454 return EINVALID_CMD_LINE
;
456 lp
->extents
= lp
->extents
* origin_from_cow(lv
)->le_count
/ 100;
462 if (lp
->sign
== SIGN_PLUS
)
463 lp
->extents
+= lv
->le_count
;
465 if (lp
->sign
== SIGN_MINUS
) {
466 if (lp
->extents
>= lv
->le_count
) {
467 log_error("Unable to reduce %s below 1 extent",
469 return EINVALID_CMD_LINE
;
472 lp
->extents
= lv
->le_count
- lp
->extents
;
476 log_error("New size of 0 not permitted");
477 return EINVALID_CMD_LINE
;
480 if (lp
->extents
== lv
->le_count
) {
482 return ECMD_PROCESSED
; /* Nothing to do. */
484 log_error("New size (%d extents) matches existing size "
485 "(%d extents)", lp
->extents
, lv
->le_count
);
486 return EINVALID_CMD_LINE
;
488 lp
->resize
= LV_EXTEND
; /* lets pretend zero size extension */
491 seg_size
= lp
->extents
- lv
->le_count
;
493 /* Use segment type of last segment */
494 dm_list_iterate_items(seg
, &lv
->segments
) {
495 lp
->segtype
= seg
->segtype
;
498 /* FIXME Support LVs with mixed segment types */
499 if (lp
->segtype
!= get_segtype_from_string(cmd
, arg_str_value(cmd
, type_ARG
,
500 lp
->segtype
->name
))) {
501 log_error("VolumeType does not match (%s)", lp
->segtype
->name
);
502 return EINVALID_CMD_LINE
;
505 /* If extending, find mirrors of last segment */
506 if ((lp
->extents
> lv
->le_count
)) {
507 dm_list_iterate_back_items(mirr_seg
, &lv
->segments
) {
508 if (seg_is_mirrored(mirr_seg
))
509 seg_mirrors
= lv_mirror_count(mirr_seg
->lv
);
514 if (!arg_count(cmd
, mirrors_ARG
) && seg_mirrors
) {
515 log_print("Extending %" PRIu32
" mirror images.",
517 lp
->mirrors
= seg_mirrors
;
519 if ((arg_count(cmd
, mirrors_ARG
) || seg_mirrors
) &&
520 (lp
->mirrors
!= seg_mirrors
)) {
521 log_error("Cannot vary number of mirrors in LV yet.");
522 return EINVALID_CMD_LINE
;
526 /* If extending, find stripes, stripesize & size of last segment */
527 if ((lp
->extents
> lv
->le_count
) &&
528 !(lp
->stripes
== 1 || (lp
->stripes
> 1 && lp
->stripe_size
))) {
529 /* FIXME Don't assume mirror seg will always be AREA_LV */
530 dm_list_iterate_items(seg
, seg_mirrors
? &seg_lv(mirr_seg
, 0)->segments
: &lv
->segments
) {
531 if (!seg_is_striped(seg
))
534 sz
= seg
->stripe_size
;
535 str
= seg
->area_count
;
537 if ((seg_stripesize
&& seg_stripesize
!= sz
&&
538 sz
&& !lp
->stripe_size
) ||
539 (seg_stripes
&& seg_stripes
!= str
&& !lp
->stripes
)) {
540 log_error("Please specify number of "
541 "stripes (-i) and stripesize (-I)");
542 return EINVALID_CMD_LINE
;
550 lp
->stripes
= seg_stripes
;
552 if (!lp
->stripe_size
&& lp
->stripes
> 1) {
553 if (seg_stripesize
) {
554 log_print("Using stripesize of last segment %s",
555 display_size(cmd
, (uint64_t) seg_stripesize
));
556 lp
->stripe_size
= seg_stripesize
;
559 find_config_tree_int(cmd
,
560 "metadata/stripesize",
561 DEFAULT_STRIPESIZE
) * 2;
562 log_print("Using default stripesize %s",
563 display_size(cmd
, (uint64_t) lp
->stripe_size
));
568 /* If reducing, find stripes, stripesize & size of last segment */
569 if (lp
->extents
< lv
->le_count
) {
572 if (lp
->stripes
|| lp
->stripe_size
|| lp
->mirrors
)
573 log_error("Ignoring stripes, stripesize and mirrors "
574 "arguments when reducing");
576 dm_list_iterate_items(seg
, &lv
->segments
) {
577 seg_extents
= seg
->len
;
579 /* Check for underlying stripe sizes */
580 seg_stripes
= lvseg_get_stripes(seg
, &seg_stripesize
);
582 if (seg_is_mirrored(seg
))
583 seg_mirrors
= lv_mirror_count(seg
->lv
);
587 if (lp
->extents
<= extents_used
+ seg_extents
)
590 extents_used
+= seg_extents
;
593 seg_size
= lp
->extents
- extents_used
;
594 lp
->stripe_size
= seg_stripesize
;
595 lp
->stripes
= seg_stripes
;
596 lp
->mirrors
= seg_mirrors
;
599 if (lp
->stripes
> 1 && !lp
->stripe_size
) {
600 log_error("Stripesize for striped segment should not be 0!");
601 return EINVALID_CMD_LINE
;
604 if (lp
->stripes
> 1) {
605 if (!(stripesize_extents
= lp
->stripe_size
/ vg
->extent_size
))
606 stripesize_extents
= 1;
608 size_rest
= seg_size
% (lp
->stripes
* stripesize_extents
);
609 if (size_rest
&& lp
->resize
== LV_REDUCE
) {
610 log_print("Rounding size (%d extents) up to stripe "
611 "boundary size for segment (%d extents)",
612 lp
->extents
, lp
->extents
- size_rest
+
613 (lp
->stripes
* stripesize_extents
));
614 lp
->extents
= lp
->extents
- size_rest
+
615 (lp
->stripes
* stripesize_extents
);
616 } else if (size_rest
) {
617 log_print("Rounding size (%d extents) down to stripe "
618 "boundary size for segment (%d extents)",
619 lp
->extents
, lp
->extents
- size_rest
);
620 lp
->extents
= lp
->extents
- size_rest
;
623 if (lp
->stripe_size
< STRIPE_SIZE_MIN
) {
624 log_error("Invalid stripe size %s",
625 display_size(cmd
, (uint64_t) lp
->stripe_size
));
626 return EINVALID_CMD_LINE
;
630 if (lp
->extents
< lv
->le_count
) {
631 if (lp
->resize
== LV_EXTEND
) {
632 log_error("New size given (%d extents) not larger "
633 "than existing size (%d extents)",
634 lp
->extents
, lv
->le_count
);
635 return EINVALID_CMD_LINE
;
637 lp
->resize
= LV_REDUCE
;
638 } else if (lp
->extents
> lv
->le_count
) {
639 if (lp
->resize
== LV_REDUCE
) {
640 log_error("New size given (%d extents) not less than "
641 "existing size (%d extents)", lp
->extents
,
643 return EINVALID_CMD_LINE
;
645 lp
->resize
= LV_EXTEND
;
648 if (lv_is_origin(lv
)) {
649 if (lp
->resize
== LV_REDUCE
) {
650 log_error("Snapshot origin volumes cannot be reduced "
655 memset(&info
, 0, sizeof(info
));
657 if (lv_info(cmd
, lv
, 0, &info
, 0, 0) && info
.exists
) {
658 log_error("Snapshot origin volumes can be resized "
659 "only while inactive: try lvchange -an");
664 if ((lp
->resize
== LV_REDUCE
) && lp
->argc
)
665 log_warn("Ignoring PVs on command line when reducing");
667 /* Request confirmation before operations that are often mistakes. */
668 if ((lp
->resizefs
|| (lp
->resize
== LV_REDUCE
)) &&
669 !_request_confirmation(cmd
, vg
, lv
, lp
)) {
676 !_fsadm_cmd(cmd
, vg
, lp
, FSADM_CMD_CHECK
, &status
)) {
677 if (status
!= FSADM_CHECK_FAILS_FOR_MOUNTED
) {
681 /* some filesystems supports online resize */
684 if ((lp
->resize
== LV_REDUCE
) &&
685 !_fsadm_cmd(cmd
, vg
, lp
, FSADM_CMD_RESIZE
, NULL
)) {
696 log_print("%sing logical volume %s to %s",
697 (lp
->resize
== LV_REDUCE
) ? "Reduc" : "Extend",
699 display_size(cmd
, (uint64_t) lp
->extents
* vg
->extent_size
));
701 if (lp
->resize
== LV_REDUCE
) {
702 if (!lv_reduce(lv
, lv
->le_count
- lp
->extents
)) {
706 } else if ((lp
->extents
> lv
->le_count
) && /* Ensure we extend */
707 !lv_extend(lv
, lp
->segtype
,
708 lp
->stripes
, lp
->stripe_size
,
709 lp
->mirrors
, first_seg(lv
)->region_size
,
710 lp
->extents
- lv
->le_count
,
716 /* store vg on disk(s) */
722 /* If snapshot, must suspend all associated devices */
724 lock_lv
= origin_from_cow(lv
);
728 if (!suspend_lv(cmd
, lock_lv
)) {
729 log_error("Failed to suspend %s", lp
->lv_name
);
735 if (!vg_commit(vg
)) {
737 if (!resume_lv(cmd
, lock_lv
))
743 if (!resume_lv(cmd
, lock_lv
)) {
744 log_error("Problem reactivating %s", lp
->lv_name
);
751 log_print("Logical volume %s successfully resized", lp
->lv_name
);
753 if (lp
->resizefs
&& (lp
->resize
== LV_EXTEND
) &&
754 !_fsadm_cmd(cmd
, vg
, lp
, FSADM_CMD_RESIZE
, NULL
)) {
759 return ECMD_PROCESSED
;
762 int lvresize(struct cmd_context
*cmd
, int argc
, char **argv
)
764 struct lvresize_params lp
;
765 struct volume_group
*vg
;
768 memset(&lp
, 0, sizeof(lp
));
770 if (!_lvresize_params(cmd
, argc
, argv
, &lp
))
771 return EINVALID_CMD_LINE
;
773 log_verbose("Finding volume group %s", lp
.vg_name
);
774 vg
= vg_read_for_update(cmd
, lp
.vg_name
, NULL
, 0);
775 if (vg_read_error(vg
)) {
781 if (!(r
= _lvresize(cmd
, vg
, &lp
)))
784 unlock_and_free_vg(cmd
, vg
, lp
.vg_name
);