]>
sourceware.org Git - lvm2.git/blob - tools/lvresize.c
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
;
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_uint_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
, &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
134 * FSADM_CMD --dry-run --verbose --force check lv_path
135 * FSADM_CMD --dry-run --verbose --force resize lv_path size
137 static int _fsadm_cmd(struct cmd_context
*cmd
,
138 const struct volume_group
*vg
,
139 const struct lvresize_params
*lp
,
140 enum fsadm_cmd_e fcmd
)
142 char lv_path
[PATH_MAX
];
143 char size_buf
[SIZE_BUF
];
144 const char *argv
[FSADM_CMD_MAX_ARGS
+ 2];
147 argv
[i
++] = FSADM_CMD
;
150 argv
[i
++] = "--dry-run";
152 if (verbose_level() >= _LOG_NOTICE
)
153 argv
[i
++] = "--verbose";
155 if (arg_count(cmd
, force_ARG
))
156 argv
[i
++] = "--force";
158 argv
[i
++] = (fcmd
== FSADM_CMD_RESIZE
) ? "resize" : "check";
160 if (dm_snprintf(lv_path
, PATH_MAX
, "%s%s/%s", cmd
->dev_dir
, lp
->vg_name
,
162 log_error("Couldn't create LV path for %s", lp
->lv_name
);
168 if (fcmd
== FSADM_CMD_RESIZE
) {
169 if (dm_snprintf(size_buf
, SIZE_BUF
, "%" PRIu64
"K",
170 (uint64_t) lp
->extents
* vg
->extent_size
/ 2) < 0) {
171 log_error("Couldn't generate new LV size string");
175 argv
[i
++] = size_buf
;
180 return exec_cmd(cmd
, argv
);
183 static int _lvresize_params(struct cmd_context
*cmd
, int argc
, char **argv
,
184 struct lvresize_params
*lp
)
186 const char *cmd_name
;
188 unsigned dev_dir_found
= 0;
190 lp
->sign
= SIGN_NONE
;
193 cmd_name
= command_name(cmd
);
194 if (!strcmp(cmd_name
, "lvreduce"))
195 lp
->resize
= LV_REDUCE
;
196 if (!strcmp(cmd_name
, "lvextend"))
197 lp
->resize
= LV_EXTEND
;
200 * Allow omission of extents and size if the user has given us
201 * one or more PVs. Most likely, the intent was "resize this
202 * LV the best you can with these PVs"
204 if ((arg_count(cmd
, extents_ARG
) + arg_count(cmd
, size_ARG
) == 0) &&
207 lp
->percent
= PERCENT_PVS
;
208 lp
->sign
= SIGN_PLUS
;
209 } else if ((arg_count(cmd
, extents_ARG
) +
210 arg_count(cmd
, size_ARG
) != 1)) {
211 log_error("Please specify either size or extents but not "
216 if (arg_count(cmd
, extents_ARG
)) {
217 lp
->extents
= arg_uint_value(cmd
, extents_ARG
, 0);
218 lp
->sign
= arg_sign_value(cmd
, extents_ARG
, SIGN_NONE
);
219 lp
->percent
= arg_percent_value(cmd
, extents_ARG
, PERCENT_NONE
);
222 /* Size returned in kilobyte units; held in sectors */
223 if (arg_count(cmd
, size_ARG
)) {
224 lp
->size
= arg_uint64_value(cmd
, size_ARG
, UINT64_C(0));
225 lp
->sign
= arg_sign_value(cmd
, size_ARG
, SIGN_NONE
);
226 lp
->percent
= PERCENT_NONE
;
229 if (lp
->resize
== LV_EXTEND
&& lp
->sign
== SIGN_MINUS
) {
230 log_error("Negative argument not permitted - use lvreduce");
234 if (lp
->resize
== LV_REDUCE
&& lp
->sign
== SIGN_PLUS
) {
235 log_error("Positive sign not permitted - use lvextend");
239 lp
->resizefs
= arg_is_set(cmd
, resizefs_ARG
);
240 lp
->nofsck
= arg_is_set(cmd
, nofsck_ARG
);
243 log_error("Please provide the logical volume name");
247 lp
->lv_name
= argv
[0];
251 if (!(lp
->lv_name
= skip_dev_dir(cmd
, lp
->lv_name
, &dev_dir_found
)) ||
252 !(lp
->vg_name
= extract_vgname(cmd
, lp
->lv_name
))) {
253 log_error("Please provide a volume group name");
257 if (!validate_name(lp
->vg_name
)) {
258 log_error("Volume group name %s has invalid characters",
263 if ((st
= strrchr(lp
->lv_name
, '/')))
264 lp
->lv_name
= st
+ 1;
272 static int _lvresize(struct cmd_context
*cmd
, struct volume_group
*vg
,
273 struct lvresize_params
*lp
)
275 struct logical_volume
*lv
;
277 uint32_t stripesize_extents
= 0;
278 uint32_t seg_stripes
= 0, seg_stripesize
= 0, seg_size
= 0;
279 uint32_t seg_mirrors
= 0;
280 uint32_t extents_used
= 0;
282 uint32_t pv_extent_count
= 0;
283 alloc_policy_t alloc
;
284 struct logical_volume
*lock_lv
;
286 struct lv_segment
*seg
, *uninitialized_var(mirr_seg
);
287 uint32_t seg_extents
;
289 struct dm_list
*pvh
= NULL
;
292 if (!(lvl
= find_lv_in_vg(vg
, lp
->lv_name
))) {
293 log_error("Logical volume %s not found in volume group %s",
294 lp
->lv_name
, lp
->vg_name
);
298 if (arg_count(cmd
, stripes_ARG
)) {
299 if (vg
->fid
->fmt
->features
& FMT_SEGMENTS
)
300 lp
->stripes
= arg_uint_value(cmd
, stripes_ARG
, 1);
302 log_warn("Varied striping not supported. Ignoring.");
305 if (arg_count(cmd
, mirrors_ARG
)) {
306 if (vg
->fid
->fmt
->features
& FMT_SEGMENTS
)
307 lp
->mirrors
= arg_uint_value(cmd
, mirrors_ARG
, 1) + 1;
309 log_warn("Mirrors not supported. Ignoring.");
310 if (arg_sign_value(cmd
, mirrors_ARG
, 0) == SIGN_MINUS
) {
311 log_error("Mirrors argument may not be negative");
312 return EINVALID_CMD_LINE
;
316 if (arg_count(cmd
, stripesize_ARG
) &&
317 !_validate_stripesize(cmd
, vg
, lp
))
318 return EINVALID_CMD_LINE
;
322 if (!lv_is_visible(lv
)) {
323 log_error("Can't resize internal logical volume %s", lv
->name
);
327 if (lv
->status
& LOCKED
) {
328 log_error("Can't resize locked LV %s", lv
->name
);
332 if (lv
->status
& CONVERTING
) {
333 log_error("Can't resize %s while lvconvert in progress", lv
->name
);
337 alloc
= arg_uint_value(cmd
, alloc_ARG
, lv
->alloc
);
340 if (lp
->size
% vg
->extent_size
) {
341 if (lp
->sign
== SIGN_MINUS
)
342 lp
->size
-= lp
->size
% vg
->extent_size
;
344 lp
->size
+= vg
->extent_size
-
345 (lp
->size
% vg
->extent_size
);
347 log_print("Rounding up size to full physical extent %s",
348 display_size(cmd
, (uint64_t) lp
->size
));
351 lp
->extents
= lp
->size
/ vg
->extent_size
;
354 if (!(pvh
= lp
->argc
? create_pv_list(cmd
->mem
, vg
, lp
->argc
,
355 lp
->argv
, 1) : &vg
->pvs
)) {
360 switch(lp
->percent
) {
362 lp
->extents
= lp
->extents
* vg
->extent_count
/ 100;
365 lp
->extents
= lp
->extents
* vg
->free_count
/ 100;
368 lp
->extents
= lp
->extents
* lv
->le_count
/ 100;
372 pv_extent_count
= pv_list_extents_free(pvh
);
373 lp
->extents
= lp
->extents
* pv_extent_count
/ 100;
375 lp
->extents
= lp
->extents
* vg
->extent_count
/ 100;
378 if (!lv_is_cow(lv
)) {
379 log_error("Specified LV does not have an origin LV.");
380 return EINVALID_CMD_LINE
;
382 lp
->extents
= lp
->extents
* origin_from_cow(lv
)->le_count
/ 100;
388 if (lp
->sign
== SIGN_PLUS
)
389 lp
->extents
+= lv
->le_count
;
391 if (lp
->sign
== SIGN_MINUS
) {
392 if (lp
->extents
>= lv
->le_count
) {
393 log_error("Unable to reduce %s below 1 extent",
395 return EINVALID_CMD_LINE
;
398 lp
->extents
= lv
->le_count
- lp
->extents
;
402 log_error("New size of 0 not permitted");
403 return EINVALID_CMD_LINE
;
406 if (lp
->extents
== lv
->le_count
) {
408 log_error("New size (%d extents) matches existing size "
409 "(%d extents)", lp
->extents
, lv
->le_count
);
410 return EINVALID_CMD_LINE
;
412 lp
->resize
= LV_EXTEND
; /* lets pretend zero size extension */
415 seg_size
= lp
->extents
- lv
->le_count
;
417 /* Use segment type of last segment */
418 dm_list_iterate_items(seg
, &lv
->segments
) {
419 lp
->segtype
= seg
->segtype
;
422 /* FIXME Support LVs with mixed segment types */
423 if (lp
->segtype
!= arg_ptr_value(cmd
, type_ARG
, lp
->segtype
)) {
424 log_error("VolumeType does not match (%s)", lp
->segtype
->name
);
425 return EINVALID_CMD_LINE
;
428 /* If extending, find mirrors of last segment */
429 if ((lp
->extents
> lv
->le_count
)) {
430 dm_list_iterate_back_items(mirr_seg
, &lv
->segments
) {
431 if (seg_is_mirrored(mirr_seg
))
432 seg_mirrors
= lv_mirror_count(mirr_seg
->lv
);
437 if (!arg_count(cmd
, mirrors_ARG
) && seg_mirrors
) {
438 log_print("Extending %" PRIu32
" mirror images.",
440 lp
->mirrors
= seg_mirrors
;
442 if ((arg_count(cmd
, mirrors_ARG
) || seg_mirrors
) &&
443 (lp
->mirrors
!= seg_mirrors
)) {
444 log_error("Cannot vary number of mirrors in LV yet.");
445 return EINVALID_CMD_LINE
;
449 /* If extending, find stripes, stripesize & size of last segment */
450 if ((lp
->extents
> lv
->le_count
) &&
451 !(lp
->stripes
== 1 || (lp
->stripes
> 1 && lp
->stripe_size
))) {
452 /* FIXME Don't assume mirror seg will always be AREA_LV */
453 dm_list_iterate_items(seg
, seg_mirrors
? &seg_lv(mirr_seg
, 0)->segments
: &lv
->segments
) {
454 if (!seg_is_striped(seg
))
457 sz
= seg
->stripe_size
;
458 str
= seg
->area_count
;
460 if ((seg_stripesize
&& seg_stripesize
!= sz
&&
461 sz
&& !lp
->stripe_size
) ||
462 (seg_stripes
&& seg_stripes
!= str
&& !lp
->stripes
)) {
463 log_error("Please specify number of "
464 "stripes (-i) and stripesize (-I)");
465 return EINVALID_CMD_LINE
;
473 lp
->stripes
= seg_stripes
;
475 if (!lp
->stripe_size
&& lp
->stripes
> 1) {
476 if (seg_stripesize
) {
477 log_print("Using stripesize of last segment %s",
478 display_size(cmd
, (uint64_t) seg_stripesize
));
479 lp
->stripe_size
= seg_stripesize
;
482 find_config_tree_int(cmd
,
483 "metadata/stripesize",
484 DEFAULT_STRIPESIZE
) * 2;
485 log_print("Using default stripesize %s",
486 display_size(cmd
, (uint64_t) lp
->stripe_size
));
491 /* If reducing, find stripes, stripesize & size of last segment */
492 if (lp
->extents
< lv
->le_count
) {
495 if (lp
->stripes
|| lp
->stripe_size
|| lp
->mirrors
)
496 log_error("Ignoring stripes, stripesize and mirrors "
497 "arguments when reducing");
499 dm_list_iterate_items(seg
, &lv
->segments
) {
500 seg_extents
= seg
->len
;
502 if (seg_is_striped(seg
)) {
503 seg_stripesize
= seg
->stripe_size
;
504 seg_stripes
= seg
->area_count
;
507 if (seg_is_mirrored(seg
))
508 seg_mirrors
= lv_mirror_count(seg
->lv
);
512 if (lp
->extents
<= extents_used
+ seg_extents
)
515 extents_used
+= seg_extents
;
518 seg_size
= lp
->extents
- extents_used
;
519 lp
->stripe_size
= seg_stripesize
;
520 lp
->stripes
= seg_stripes
;
521 lp
->mirrors
= seg_mirrors
;
524 if (lp
->stripes
> 1 && !lp
->stripe_size
) {
525 log_error("Stripesize for striped segment should not be 0!");
526 return EINVALID_CMD_LINE
;
529 if ((lp
->stripes
> 1)) {
530 if (!(stripesize_extents
= lp
->stripe_size
/ vg
->extent_size
))
531 stripesize_extents
= 1;
533 if ((size_rest
= seg_size
% (lp
->stripes
* stripesize_extents
))) {
534 log_print("Rounding size (%d extents) down to stripe "
535 "boundary size for segment (%d extents)",
536 lp
->extents
, lp
->extents
- size_rest
);
537 lp
->extents
= lp
->extents
- size_rest
;
540 if (lp
->stripe_size
< STRIPE_SIZE_MIN
) {
541 log_error("Invalid stripe size %s",
542 display_size(cmd
, (uint64_t) lp
->stripe_size
));
543 return EINVALID_CMD_LINE
;
547 if (lp
->extents
< lv
->le_count
) {
548 if (lp
->resize
== LV_EXTEND
) {
549 log_error("New size given (%d extents) not larger "
550 "than existing size (%d extents)",
551 lp
->extents
, lv
->le_count
);
552 return EINVALID_CMD_LINE
;
554 lp
->resize
= LV_REDUCE
;
555 } else if (lp
->extents
> lv
->le_count
) {
556 if (lp
->resize
== LV_REDUCE
) {
557 log_error("New size given (%d extents) not less than "
558 "existing size (%d extents)", lp
->extents
,
560 return EINVALID_CMD_LINE
;
562 lp
->resize
= LV_EXTEND
;
565 if (lv_is_origin(lv
)) {
566 if (lp
->resize
== LV_REDUCE
) {
567 log_error("Snapshot origin volumes cannot be reduced "
572 memset(&info
, 0, sizeof(info
));
574 if (lv_info(cmd
, lv
, &info
, 0, 0) && info
.exists
) {
575 log_error("Snapshot origin volumes can be resized "
576 "only while inactive: try lvchange -an");
581 if ((lp
->resize
== LV_REDUCE
) && lp
->argc
)
582 log_warn("Ignoring PVs on command line when reducing");
584 /* Request confirmation before operations that are often mistakes. */
585 if ((lp
->resizefs
|| (lp
->resize
== LV_REDUCE
)) &&
586 !_request_confirmation(cmd
, vg
, lv
, lp
)) {
593 !_fsadm_cmd(cmd
, vg
, lp
, FSADM_CMD_CHECK
)) {
598 if ((lp
->resize
== LV_REDUCE
) &&
599 !_fsadm_cmd(cmd
, vg
, lp
, FSADM_CMD_RESIZE
)) {
610 log_print("%sing logical volume %s to %s",
611 (lp
->resize
== LV_REDUCE
) ? "Reduc" : "Extend",
613 display_size(cmd
, (uint64_t) lp
->extents
* vg
->extent_size
));
615 if (lp
->resize
== LV_REDUCE
) {
616 if (!lv_reduce(lv
, lv
->le_count
- lp
->extents
)) {
620 } else if ((lp
->extents
> lv
->le_count
) && /* Ensure we extend */
621 !lv_extend(lv
, lp
->segtype
, lp
->stripes
,
622 lp
->stripe_size
, lp
->mirrors
,
623 lp
->extents
- lv
->le_count
,
624 NULL
, 0u, 0u, pvh
, alloc
)) {
629 /* store vg on disk(s) */
635 /* If snapshot, must suspend all associated devices */
637 lock_lv
= origin_from_cow(lv
);
641 if (!suspend_lv(cmd
, lock_lv
)) {
642 log_error("Failed to suspend %s", lp
->lv_name
);
648 if (!vg_commit(vg
)) {
650 if (!resume_lv(cmd
, lock_lv
))
656 if (!resume_lv(cmd
, lock_lv
)) {
657 log_error("Problem reactivating %s", lp
->lv_name
);
664 log_print("Logical volume %s successfully resized", lp
->lv_name
);
666 if (lp
->resizefs
&& (lp
->resize
== LV_EXTEND
) &&
667 !_fsadm_cmd(cmd
, vg
, lp
, FSADM_CMD_RESIZE
)) {
672 return ECMD_PROCESSED
;
675 int lvresize(struct cmd_context
*cmd
, int argc
, char **argv
)
677 struct lvresize_params lp
;
678 struct volume_group
*vg
;
681 memset(&lp
, 0, sizeof(lp
));
683 if (!_lvresize_params(cmd
, argc
, argv
, &lp
))
684 return EINVALID_CMD_LINE
;
686 log_verbose("Finding volume group %s", lp
.vg_name
);
687 vg
= vg_read_for_update(cmd
, lp
.vg_name
, NULL
, 0);
688 if (vg_read_error(vg
)) {
694 if (!(r
= _lvresize(cmd
, vg
, &lp
)))
697 unlock_and_release_vg(cmd
, vg
, lp
.vg_name
);
This page took 0.07001 seconds and 6 git commands to generate.