]>
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);
77 log_error("Mirrors and striping cannot be combined yet.");
80 if (lp
->stripe_size
& (lp
->stripe_size
- 1)) {
81 log_error("Stripe size must be power of 2");
88 static int _request_confirmation(struct cmd_context
*cmd
,
89 const struct volume_group
*vg
,
90 const struct logical_volume
*lv
,
91 const struct lvresize_params
*lp
)
95 memset(&info
, 0, sizeof(info
));
97 if (!lv_info(cmd
, lv
, &info
, 1, 0) && driver_version(NULL
, 0)) {
98 log_error("lv_info failed: aborting");
104 log_error("Logical volume %s must be activated "
105 "before resizing filesystem", lp
->lv_name
);
114 log_warn("WARNING: Reducing active%s logical volume to %s",
115 info
.open_count
? " and open" : "",
116 display_size(cmd
, (uint64_t) lp
->extents
* vg
->extent_size
));
118 log_warn("THIS MAY DESTROY YOUR DATA (filesystem etc.)");
120 if (!arg_count(cmd
, force_ARG
)) {
121 if (yes_no_prompt("Do you really want to reduce %s? [y/n]: ",
122 lp
->lv_name
) == 'n') {
123 log_print("Logical volume %s NOT reduced", lp
->lv_name
);
133 enum fsadm_cmd_e
{ FSADM_CMD_CHECK
, FSADM_CMD_RESIZE
};
134 #define FSADM_CMD "fsadm"
135 #define FSADM_CMD_MAX_ARGS 6
138 * FSADM_CMD --dry-run --verbose --force check lv_path
139 * FSADM_CMD --dry-run --verbose --force resize lv_path size
141 static int _fsadm_cmd(struct cmd_context
*cmd
,
142 const struct volume_group
*vg
,
143 const struct lvresize_params
*lp
,
144 enum fsadm_cmd_e fcmd
)
146 char lv_path
[PATH_MAX
];
147 char size_buf
[SIZE_BUF
];
148 const char *argv
[FSADM_CMD_MAX_ARGS
+ 2];
151 argv
[i
++] = FSADM_CMD
;
154 argv
[i
++] = "--dry-run";
156 if (verbose_level() >= _LOG_NOTICE
)
157 argv
[i
++] = "--verbose";
159 if (arg_count(cmd
, force_ARG
))
160 argv
[i
++] = "--force";
162 argv
[i
++] = (fcmd
== FSADM_CMD_RESIZE
) ? "resize" : "check";
164 if (dm_snprintf(lv_path
, PATH_MAX
, "%s%s/%s", cmd
->dev_dir
, lp
->vg_name
,
166 log_error("Couldn't create LV path for %s", lp
->lv_name
);
172 if (fcmd
== FSADM_CMD_RESIZE
) {
173 if (dm_snprintf(size_buf
, SIZE_BUF
, "%" PRIu64
"K",
174 (uint64_t) lp
->extents
* vg
->extent_size
/ 2) < 0) {
175 log_error("Couldn't generate new LV size string");
179 argv
[i
++] = size_buf
;
184 return exec_cmd(cmd
, argv
);
187 static int _lvresize_params(struct cmd_context
*cmd
, int argc
, char **argv
,
188 struct lvresize_params
*lp
)
190 const char *cmd_name
;
192 unsigned dev_dir_found
= 0;
194 lp
->sign
= SIGN_NONE
;
197 cmd_name
= command_name(cmd
);
198 if (!strcmp(cmd_name
, "lvreduce"))
199 lp
->resize
= LV_REDUCE
;
200 if (!strcmp(cmd_name
, "lvextend"))
201 lp
->resize
= LV_EXTEND
;
204 * Allow omission of extents and size if the user has given us
205 * one or more PVs. Most likely, the intent was "resize this
206 * LV the best you can with these PVs"
208 if ((arg_count(cmd
, extents_ARG
) + arg_count(cmd
, size_ARG
) == 0) &&
211 lp
->percent
= PERCENT_PVS
;
212 lp
->sign
= SIGN_PLUS
;
213 } else if ((arg_count(cmd
, extents_ARG
) +
214 arg_count(cmd
, size_ARG
) != 1)) {
215 log_error("Please specify either size or extents but not "
220 if (arg_count(cmd
, extents_ARG
)) {
221 lp
->extents
= arg_uint_value(cmd
, extents_ARG
, 0);
222 lp
->sign
= arg_sign_value(cmd
, extents_ARG
, SIGN_NONE
);
223 lp
->percent
= arg_percent_value(cmd
, extents_ARG
, PERCENT_NONE
);
226 /* Size returned in kilobyte units; held in sectors */
227 if (arg_count(cmd
, size_ARG
)) {
228 lp
->size
= arg_uint64_value(cmd
, size_ARG
, UINT64_C(0));
229 lp
->sign
= arg_sign_value(cmd
, size_ARG
, SIGN_NONE
);
230 lp
->percent
= PERCENT_NONE
;
233 if (lp
->resize
== LV_EXTEND
&& lp
->sign
== SIGN_MINUS
) {
234 log_error("Negative argument not permitted - use lvreduce");
238 if (lp
->resize
== LV_REDUCE
&& lp
->sign
== SIGN_PLUS
) {
239 log_error("Positive sign not permitted - use lvextend");
243 lp
->resizefs
= arg_count(cmd
, resizefs_ARG
) ? 1 : 0;
244 lp
->nofsck
= arg_count(cmd
, nofsck_ARG
) ? 1 : 0;
247 log_error("Please provide the logical volume name");
251 lp
->lv_name
= argv
[0];
255 if (!(lp
->lv_name
= skip_dev_dir(cmd
, lp
->lv_name
, &dev_dir_found
)) ||
256 !(lp
->vg_name
= extract_vgname(cmd
, lp
->lv_name
))) {
257 log_error("Please provide a volume group name");
261 if (!validate_name(lp
->vg_name
)) {
262 log_error("Volume group name %s has invalid characters",
267 if ((st
= strrchr(lp
->lv_name
, '/')))
268 lp
->lv_name
= st
+ 1;
276 static int _lvresize(struct cmd_context
*cmd
, struct volume_group
*vg
,
277 struct lvresize_params
*lp
)
279 struct logical_volume
*lv
;
281 uint32_t stripesize_extents
= 0;
282 uint32_t seg_stripes
= 0, seg_stripesize
= 0, seg_size
= 0;
283 uint32_t seg_mirrors
= 0;
284 uint32_t extents_used
= 0;
286 uint32_t pv_extent_count
= 0;
287 alloc_policy_t alloc
;
288 struct logical_volume
*lock_lv
;
290 struct lv_segment
*seg
;
291 uint32_t seg_extents
;
293 struct dm_list
*pvh
= NULL
;
296 if (!(lvl
= find_lv_in_vg(vg
, lp
->lv_name
))) {
297 log_error("Logical volume %s not found in volume group %s",
298 lp
->lv_name
, lp
->vg_name
);
302 if (arg_count(cmd
, stripes_ARG
)) {
303 if (vg
->fid
->fmt
->features
& FMT_SEGMENTS
)
304 lp
->stripes
= arg_uint_value(cmd
, stripes_ARG
, 1);
306 log_warn("Varied striping not supported. Ignoring.");
309 if (arg_count(cmd
, mirrors_ARG
)) {
310 if (vg
->fid
->fmt
->features
& FMT_SEGMENTS
)
311 lp
->mirrors
= arg_uint_value(cmd
, mirrors_ARG
, 1) + 1;
313 log_warn("Mirrors not supported. Ignoring.");
314 if (arg_sign_value(cmd
, mirrors_ARG
, 0) == SIGN_MINUS
) {
315 log_error("Mirrors argument may not be negative");
316 return EINVALID_CMD_LINE
;
320 if (arg_count(cmd
, stripesize_ARG
) &&
321 !_validate_stripesize(cmd
, vg
, lp
))
322 return EINVALID_CMD_LINE
;
326 if (lv
->status
& LOCKED
) {
327 log_error("Can't resize locked LV %s", lv
->name
);
331 if (lv
->status
& CONVERTING
) {
332 log_error("Can't resize %s while lvconvert in progress", lv
->name
);
336 alloc
= arg_uint_value(cmd
, alloc_ARG
, lv
->alloc
);
339 if (lp
->size
% vg
->extent_size
) {
340 if (lp
->sign
== SIGN_MINUS
)
341 lp
->size
-= lp
->size
% vg
->extent_size
;
343 lp
->size
+= vg
->extent_size
-
344 (lp
->size
% vg
->extent_size
);
346 log_print("Rounding up size to full physical extent %s",
347 display_size(cmd
, (uint64_t) lp
->size
));
350 lp
->extents
= lp
->size
/ vg
->extent_size
;
353 if (!(pvh
= lp
->argc
? create_pv_list(cmd
->mem
, vg
, lp
->argc
,
354 lp
->argv
, 1) : &vg
->pvs
)) {
359 switch(lp
->percent
) {
361 lp
->extents
= lp
->extents
* vg
->extent_count
/ 100;
364 lp
->extents
= lp
->extents
* vg
->free_count
/ 100;
367 lp
->extents
= lp
->extents
* lv
->le_count
/ 100;
370 pv_extent_count
= pv_list_extents_free(pvh
);
371 lp
->extents
= lp
->extents
* pv_extent_count
/ 100;
377 if (lp
->sign
== SIGN_PLUS
)
378 lp
->extents
+= lv
->le_count
;
380 if (lp
->sign
== SIGN_MINUS
) {
381 if (lp
->extents
>= lv
->le_count
) {
382 log_error("Unable to reduce %s below 1 extent",
384 return EINVALID_CMD_LINE
;
387 lp
->extents
= lv
->le_count
- lp
->extents
;
391 log_error("New size of 0 not permitted");
392 return EINVALID_CMD_LINE
;
395 if (lp
->extents
== lv
->le_count
) {
397 log_error("New size (%d extents) matches existing size "
398 "(%d extents)", lp
->extents
, lv
->le_count
);
399 return EINVALID_CMD_LINE
;
401 lp
->resize
= LV_EXTEND
; /* lets pretend zero size extension */
404 seg_size
= lp
->extents
- lv
->le_count
;
406 /* Use segment type of last segment */
407 dm_list_iterate_items(seg
, &lv
->segments
) {
408 lp
->segtype
= seg
->segtype
;
411 /* FIXME Support LVs with mixed segment types */
412 if (lp
->segtype
!= arg_ptr_value(cmd
, type_ARG
, lp
->segtype
)) {
413 log_error("VolumeType does not match (%s)", lp
->segtype
->name
);
414 return EINVALID_CMD_LINE
;
417 /* If extending, find stripes, stripesize & size of last segment */
418 if ((lp
->extents
> lv
->le_count
) &&
419 !(lp
->stripes
== 1 || (lp
->stripes
> 1 && lp
->stripe_size
))) {
420 dm_list_iterate_items(seg
, &lv
->segments
) {
421 if (!seg_is_striped(seg
))
424 sz
= seg
->stripe_size
;
425 str
= seg
->area_count
;
427 if ((seg_stripesize
&& seg_stripesize
!= sz
&&
429 (seg_stripes
&& seg_stripes
!= str
&& !lp
->stripes
)) {
430 log_error("Please specify number of "
431 "stripes (-i) and stripesize (-I)");
432 return EINVALID_CMD_LINE
;
440 lp
->stripes
= seg_stripes
;
442 if (!lp
->stripe_size
&& lp
->stripes
> 1) {
443 if (seg_stripesize
) {
444 log_print("Using stripesize of last segment %s",
445 display_size(cmd
, (uint64_t) seg_stripesize
));
446 lp
->stripe_size
= seg_stripesize
;
449 find_config_tree_int(cmd
,
450 "metadata/stripesize",
451 DEFAULT_STRIPESIZE
) * 2;
452 log_print("Using default stripesize %s",
453 display_size(cmd
, (uint64_t) lp
->stripe_size
));
458 /* If extending, find mirrors of last segment */
459 if ((lp
->extents
> lv
->le_count
)) {
460 dm_list_iterate_back_items(seg
, &lv
->segments
) {
461 if (seg_is_mirrored(seg
))
462 seg_mirrors
= lv_mirror_count(seg
->lv
);
467 if (!arg_count(cmd
, mirrors_ARG
) && seg_mirrors
) {
468 log_print("Extending %" PRIu32
" mirror images.",
470 lp
->mirrors
= seg_mirrors
;
472 if ((arg_count(cmd
, mirrors_ARG
) || seg_mirrors
) &&
473 (lp
->mirrors
!= seg_mirrors
)) {
474 log_error("Cannot vary number of mirrors in LV yet.");
475 return EINVALID_CMD_LINE
;
479 /* If reducing, find stripes, stripesize & size of last segment */
480 if (lp
->extents
< lv
->le_count
) {
483 if (lp
->stripes
|| lp
->stripe_size
|| lp
->mirrors
)
484 log_error("Ignoring stripes, stripesize and mirrors "
485 "arguments when reducing");
487 dm_list_iterate_items(seg
, &lv
->segments
) {
488 seg_extents
= seg
->len
;
490 if (seg_is_striped(seg
)) {
491 seg_stripesize
= seg
->stripe_size
;
492 seg_stripes
= seg
->area_count
;
495 if (seg_is_mirrored(seg
))
496 seg_mirrors
= lv_mirror_count(seg
->lv
);
500 if (lp
->extents
<= extents_used
+ seg_extents
)
503 extents_used
+= seg_extents
;
506 seg_size
= lp
->extents
- extents_used
;
507 lp
->stripe_size
= seg_stripesize
;
508 lp
->stripes
= seg_stripes
;
509 lp
->mirrors
= seg_mirrors
;
512 if (lp
->stripes
> 1 && !lp
->stripe_size
) {
513 log_error("Stripesize for striped segment should not be 0!");
514 return EINVALID_CMD_LINE
;
517 if ((lp
->stripes
> 1)) {
518 if (!(stripesize_extents
= lp
->stripe_size
/ vg
->extent_size
))
519 stripesize_extents
= 1;
521 if ((size_rest
= seg_size
% (lp
->stripes
* stripesize_extents
))) {
522 log_print("Rounding size (%d extents) down to stripe "
523 "boundary size for segment (%d extents)",
524 lp
->extents
, lp
->extents
- size_rest
);
525 lp
->extents
= lp
->extents
- size_rest
;
528 if (lp
->stripe_size
< STRIPE_SIZE_MIN
) {
529 log_error("Invalid stripe size %s",
530 display_size(cmd
, (uint64_t) lp
->stripe_size
));
531 return EINVALID_CMD_LINE
;
535 if (lp
->extents
< lv
->le_count
) {
536 if (lp
->resize
== LV_EXTEND
) {
537 log_error("New size given (%d extents) not larger "
538 "than existing size (%d extents)",
539 lp
->extents
, lv
->le_count
);
540 return EINVALID_CMD_LINE
;
542 lp
->resize
= LV_REDUCE
;
543 } else if (lp
->extents
> lv
->le_count
) {
544 if (lp
->resize
== LV_REDUCE
) {
545 log_error("New size given (%d extents) not less than "
546 "existing size (%d extents)", lp
->extents
,
548 return EINVALID_CMD_LINE
;
550 lp
->resize
= LV_EXTEND
;
553 if (lv_is_origin(lv
)) {
554 if (lp
->resize
== LV_REDUCE
) {
555 log_error("Snapshot origin volumes cannot be reduced "
560 memset(&info
, 0, sizeof(info
));
562 if (lv_info(cmd
, lv
, &info
, 0, 0) && info
.exists
) {
563 log_error("Snapshot origin volumes can be resized "
564 "only while inactive: try lvchange -an");
569 if ((lp
->resize
== LV_REDUCE
) && lp
->argc
)
570 log_warn("Ignoring PVs on command line when reducing");
572 /* Request confirmation before operations that are often mistakes. */
573 if ((lp
->resizefs
|| (lp
->resize
== LV_REDUCE
)) &&
574 !_request_confirmation(cmd
, vg
, lv
, lp
)) {
581 !_fsadm_cmd(cmd
, vg
, lp
, FSADM_CMD_CHECK
)) {
586 if ((lp
->resize
== LV_REDUCE
) &&
587 !_fsadm_cmd(cmd
, vg
, lp
, FSADM_CMD_RESIZE
)) {
598 log_print("%sing logical volume %s to %s",
599 (lp
->resize
== LV_REDUCE
) ? "Reduc" : "Extend",
601 display_size(cmd
, (uint64_t) lp
->extents
* vg
->extent_size
));
603 if (lp
->resize
== LV_REDUCE
) {
604 if (!lv_reduce(lv
, lv
->le_count
- lp
->extents
)) {
608 } else if ((lp
->extents
> lv
->le_count
) && /* Ensure we extend */
609 !lv_extend(lv
, lp
->segtype
, lp
->stripes
,
610 lp
->stripe_size
, lp
->mirrors
,
611 lp
->extents
- lv
->le_count
,
612 NULL
, 0u, 0u, pvh
, alloc
)) {
617 /* store vg on disk(s) */
623 /* If snapshot, must suspend all associated devices */
625 lock_lv
= origin_from_cow(lv
);
629 if (!suspend_lv(cmd
, lock_lv
)) {
630 log_error("Failed to suspend %s", lp
->lv_name
);
636 if (!vg_commit(vg
)) {
638 resume_lv(cmd
, lock_lv
);
643 if (!resume_lv(cmd
, lock_lv
)) {
644 log_error("Problem reactivating %s", lp
->lv_name
);
651 log_print("Logical volume %s successfully resized", lp
->lv_name
);
653 if (lp
->resizefs
&& (lp
->resize
== LV_EXTEND
) &&
654 !_fsadm_cmd(cmd
, vg
, lp
, FSADM_CMD_RESIZE
)) {
659 return ECMD_PROCESSED
;
662 int lvresize(struct cmd_context
*cmd
, int argc
, char **argv
)
664 struct lvresize_params lp
;
665 struct volume_group
*vg
;
668 memset(&lp
, 0, sizeof(lp
));
670 if (!_lvresize_params(cmd
, argc
, argv
, &lp
))
671 return EINVALID_CMD_LINE
;
673 log_verbose("Finding volume group %s", lp
.vg_name
);
674 vg
= vg_read_for_update(cmd
, lp
.vg_name
, NULL
, 0);
675 if (vg_read_error(vg
)) {
681 if (!(r
= _lvresize(cmd
, vg
, &lp
)))
684 unlock_and_release_vg(cmd
, vg
, lp
.vg_name
);
This page took 0.073199 seconds and 6 git commands to generate.