]>
sourceware.org Git - lvm2.git/blob - tools/lvresize.c
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
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 confirm_resizefs_reduce(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");
102 if (lp
->resizefs
&& !info
.exists
) {
103 log_error("Logical volume %s must be activated "
104 "before resizing filesystem", lp
->lv_name
);
108 if (info
.exists
&& !lp
->resizefs
&& (lp
->resize
== LV_REDUCE
)) {
109 log_warn("WARNING: Reducing active%s logical volume "
110 "to %s", info
.open_count
? " and open" : "",
111 display_size(cmd
, (uint64_t) lp
->extents
*
114 log_warn("THIS MAY DESTROY YOUR DATA "
115 "(filesystem etc.)");
117 if (!arg_count(cmd
, force_ARG
)) {
118 if (yes_no_prompt("Do you really want to "
119 "reduce %s? [y/n]: ",
120 lp
->lv_name
) == 'n') {
121 log_print("Logical volume %s NOT "
122 "reduced", lp
->lv_name
);
133 static int do_resizefs_reduce(const struct cmd_context
*cmd
,
134 const struct volume_group
*vg
,
135 const struct lvresize_params
*lp
)
137 char lv_path
[PATH_MAX
];
138 char size_buf
[SIZE_BUF
];
140 if (dm_snprintf(lv_path
, PATH_MAX
, "%s%s/%s", cmd
->dev_dir
,
141 lp
->vg_name
, lp
->lv_name
) < 0) {
142 log_error("Couldn't create LV path for %s",
147 if (dm_snprintf(size_buf
, SIZE_BUF
, "%" PRIu64
,
148 (uint64_t) lp
->extents
* vg
->extent_size
/ 2) < 0) {
149 log_error("Couldn't generate new LV size string");
154 if (!exec_cmd("fsadm", "check", lv_path
, NULL
)) {
160 if (lp
->resize
== LV_REDUCE
) {
161 if (!exec_cmd("fsadm", "resize", lv_path
, size_buf
)) {
170 static int _lvresize_params(struct cmd_context
*cmd
, int argc
, char **argv
,
171 struct lvresize_params
*lp
)
173 const char *cmd_name
;
176 lp
->sign
= SIGN_NONE
;
179 cmd_name
= command_name(cmd
);
180 if (!strcmp(cmd_name
, "lvreduce"))
181 lp
->resize
= LV_REDUCE
;
182 if (!strcmp(cmd_name
, "lvextend"))
183 lp
->resize
= LV_EXTEND
;
186 * Allow omission of extents and size if the user has given us
187 * one or more PVs. Most likely, the intent was "resize this
188 * LV the best you can with these PVs"
190 if ((arg_count(cmd
, extents_ARG
) + arg_count(cmd
, size_ARG
) == 0) &&
193 lp
->percent
= PERCENT_PVS
;
194 lp
->sign
= SIGN_PLUS
;
195 } else if ((arg_count(cmd
, extents_ARG
) +
196 arg_count(cmd
, size_ARG
) != 1)) {
197 log_error("Please specify either size or extents but not "
202 if (arg_count(cmd
, extents_ARG
)) {
203 lp
->extents
= arg_uint_value(cmd
, extents_ARG
, 0);
204 lp
->sign
= arg_sign_value(cmd
, extents_ARG
, SIGN_NONE
);
205 lp
->percent
= arg_percent_value(cmd
, extents_ARG
, PERCENT_NONE
);
208 /* Size returned in kilobyte units; held in sectors */
209 if (arg_count(cmd
, size_ARG
)) {
210 lp
->size
= arg_uint64_value(cmd
, size_ARG
, UINT64_C(0));
211 lp
->sign
= arg_sign_value(cmd
, size_ARG
, SIGN_NONE
);
212 lp
->percent
= PERCENT_NONE
;
215 if (lp
->resize
== LV_EXTEND
&& lp
->sign
== SIGN_MINUS
) {
216 log_error("Negative argument not permitted - use lvreduce");
220 if (lp
->resize
== LV_REDUCE
&& lp
->sign
== SIGN_PLUS
) {
221 log_error("Positive sign not permitted - use lvextend");
225 lp
->resizefs
= arg_count(cmd
, resizefs_ARG
) ? 1 : 0;
226 lp
->nofsck
= arg_count(cmd
, nofsck_ARG
) ? 1 : 0;
229 log_error("Please provide the logical volume name");
233 lp
->lv_name
= argv
[0];
237 if (!(lp
->vg_name
= extract_vgname(cmd
, lp
->lv_name
))) {
238 log_error("Please provide a volume group name");
242 if (!validate_name(lp
->vg_name
)) {
243 log_error("Volume group name %s has invalid characters",
248 if ((st
= strrchr(lp
->lv_name
, '/')))
249 lp
->lv_name
= st
+ 1;
257 static int _lvresize(struct cmd_context
*cmd
, struct volume_group
*vg
,
258 struct lvresize_params
*lp
)
260 struct logical_volume
*lv
;
262 uint32_t stripesize_extents
= 0;
263 uint32_t seg_stripes
= 0, seg_stripesize
= 0, seg_size
= 0;
264 uint32_t seg_mirrors
= 0;
265 uint32_t extents_used
= 0;
267 uint32_t pv_extent_count
= 0;
268 alloc_policy_t alloc
;
269 struct logical_volume
*lock_lv
;
271 struct lv_segment
*seg
;
272 uint32_t seg_extents
;
274 struct list
*pvh
= NULL
;
275 char size_buf
[SIZE_BUF
];
276 char lv_path
[PATH_MAX
];
279 if (!(lvl
= find_lv_in_vg(vg
, lp
->lv_name
))) {
280 log_error("Logical volume %s not found in volume group %s",
281 lp
->lv_name
, lp
->vg_name
);
285 if (arg_count(cmd
, stripes_ARG
)) {
286 if (vg
->fid
->fmt
->features
& FMT_SEGMENTS
)
287 lp
->stripes
= arg_uint_value(cmd
, stripes_ARG
, 1);
289 log_warn("Varied striping not supported. Ignoring.");
292 if (arg_count(cmd
, mirrors_ARG
)) {
293 if (vg
->fid
->fmt
->features
& FMT_SEGMENTS
)
294 lp
->mirrors
= arg_uint_value(cmd
, mirrors_ARG
, 1) + 1;
296 log_warn("Mirrors not supported. Ignoring.");
297 if (arg_sign_value(cmd
, mirrors_ARG
, 0) == SIGN_MINUS
) {
298 log_error("Mirrors argument may not be negative");
299 return EINVALID_CMD_LINE
;
303 if (arg_count(cmd
, stripesize_ARG
)) {
304 if (!validate_stripesize(cmd
, vg
, lp
))
305 return EINVALID_CMD_LINE
;
310 if (lv
->status
& LOCKED
) {
311 log_error("Can't resize locked LV %s", lv
->name
);
315 alloc
= arg_uint_value(cmd
, alloc_ARG
, lv
->alloc
);
318 if (lp
->size
% vg
->extent_size
) {
319 if (lp
->sign
== SIGN_MINUS
)
320 lp
->size
-= lp
->size
% vg
->extent_size
;
322 lp
->size
+= vg
->extent_size
-
323 (lp
->size
% vg
->extent_size
);
325 log_print("Rounding up size to full physical extent %s",
326 display_size(cmd
, (uint64_t) lp
->size
));
329 lp
->extents
= lp
->size
/ vg
->extent_size
;
332 if (!(pvh
= lp
->argc
? create_pv_list(cmd
->mem
, vg
, lp
->argc
,
333 lp
->argv
, 1) : &vg
->pvs
)) {
338 switch(lp
->percent
) {
340 lp
->extents
= lp
->extents
* vg
->extent_count
/ 100;
343 lp
->extents
= lp
->extents
* vg
->free_count
/ 100;
346 lp
->extents
= lp
->extents
* lv
->le_count
/ 100;
349 pv_extent_count
= pv_list_extents_free(pvh
);
350 lp
->extents
= lp
->extents
* pv_extent_count
/ 100;
356 if (lp
->sign
== SIGN_PLUS
)
357 lp
->extents
+= lv
->le_count
;
359 if (lp
->sign
== SIGN_MINUS
) {
360 if (lp
->extents
>= lv
->le_count
) {
361 log_error("Unable to reduce %s below 1 extent",
363 return EINVALID_CMD_LINE
;
366 lp
->extents
= lv
->le_count
- lp
->extents
;
370 log_error("New size of 0 not permitted");
371 return EINVALID_CMD_LINE
;
374 if (lp
->extents
== lv
->le_count
) {
375 log_error("New size (%d extents) matches existing size "
376 "(%d extents)", lp
->extents
, lv
->le_count
);
377 return EINVALID_CMD_LINE
;
380 seg_size
= lp
->extents
- lv
->le_count
;
382 /* Use segment type of last segment */
383 list_iterate_items(seg
, &lv
->segments
) {
384 lp
->segtype
= seg
->segtype
;
387 /* FIXME Support LVs with mixed segment types */
388 if (lp
->segtype
!= arg_ptr_value(cmd
, type_ARG
, lp
->segtype
)) {
389 log_error("VolumeType does not match (%s)", lp
->segtype
->name
);
390 return EINVALID_CMD_LINE
;
393 /* If extending, find stripes, stripesize & size of last segment */
394 if ((lp
->extents
> lv
->le_count
) &&
395 !(lp
->stripes
== 1 || (lp
->stripes
> 1 && lp
->stripe_size
))) {
396 list_iterate_items(seg
, &lv
->segments
) {
397 if (!seg_is_striped(seg
))
400 sz
= seg
->stripe_size
;
401 str
= seg
->area_count
;
403 if ((seg_stripesize
&& seg_stripesize
!= sz
404 && !lp
->stripe_size
) ||
405 (seg_stripes
&& seg_stripes
!= str
&& !lp
->stripes
)) {
406 log_error("Please specify number of "
407 "stripes (-i) and stripesize (-I)");
408 return EINVALID_CMD_LINE
;
416 lp
->stripes
= seg_stripes
;
418 if (!lp
->stripe_size
&& lp
->stripes
> 1) {
419 if (seg_stripesize
) {
420 log_print("Using stripesize of last segment %s",
421 display_size(cmd
, (uint64_t) seg_stripesize
));
422 lp
->stripe_size
= seg_stripesize
;
425 find_config_tree_int(cmd
,
426 "metadata/stripesize",
427 DEFAULT_STRIPESIZE
) * 2;
428 log_print("Using default stripesize %s",
429 display_size(cmd
, (uint64_t) lp
->stripe_size
));
434 /* If extending, find mirrors of last segment */
435 if ((lp
->extents
> lv
->le_count
)) {
436 list_iterate_back_items(seg
, &lv
->segments
) {
437 if (seg_is_mirrored(seg
))
438 seg_mirrors
= seg
->area_count
;
443 if (!arg_count(cmd
, mirrors_ARG
) && seg_mirrors
) {
444 log_print("Extending %" PRIu32
" mirror images.",
446 lp
->mirrors
= seg_mirrors
;
448 if ((arg_count(cmd
, mirrors_ARG
) || seg_mirrors
) &&
449 (lp
->mirrors
!= seg_mirrors
)) {
450 log_error("Cannot vary number of mirrors in LV yet.");
451 return EINVALID_CMD_LINE
;
455 /* If reducing, find stripes, stripesize & size of last segment */
456 if (lp
->extents
< lv
->le_count
) {
459 if (lp
->stripes
|| lp
->stripe_size
|| lp
->mirrors
)
460 log_error("Ignoring stripes, stripesize and mirrors "
461 "arguments when reducing");
463 list_iterate_items(seg
, &lv
->segments
) {
464 seg_extents
= seg
->len
;
466 if (seg_is_striped(seg
)) {
467 seg_stripesize
= seg
->stripe_size
;
468 seg_stripes
= seg
->area_count
;
471 if (seg_is_mirrored(seg
))
472 seg_mirrors
= seg
->area_count
;
476 if (lp
->extents
<= extents_used
+ seg_extents
)
479 extents_used
+= seg_extents
;
482 seg_size
= lp
->extents
- extents_used
;
483 lp
->stripe_size
= seg_stripesize
;
484 lp
->stripes
= seg_stripes
;
485 lp
->mirrors
= seg_mirrors
;
488 if (lp
->stripes
> 1 && !lp
->stripe_size
) {
489 log_error("Stripesize for striped segment should not be 0!");
490 return EINVALID_CMD_LINE
;
493 if ((lp
->stripes
> 1)) {
494 if (!(stripesize_extents
= lp
->stripe_size
/ vg
->extent_size
))
495 stripesize_extents
= 1;
497 if ((size_rest
= seg_size
% (lp
->stripes
* stripesize_extents
))) {
498 log_print("Rounding size (%d extents) down to stripe "
499 "boundary size for segment (%d extents)",
500 lp
->extents
, lp
->extents
- size_rest
);
501 lp
->extents
= lp
->extents
- size_rest
;
504 if (lp
->stripe_size
< STRIPE_SIZE_MIN
) {
505 log_error("Invalid stripe size %s",
506 display_size(cmd
, (uint64_t) lp
->stripe_size
));
507 return EINVALID_CMD_LINE
;
511 if (lp
->extents
== lv
->le_count
) {
512 log_error("New size (%d extents) matches existing size "
513 "(%d extents)", lp
->extents
, lv
->le_count
);
514 return EINVALID_CMD_LINE
;
517 if (lp
->extents
< lv
->le_count
) {
518 if (lp
->resize
== LV_EXTEND
) {
519 log_error("New size given (%d extents) not larger "
520 "than existing size (%d extents)",
521 lp
->extents
, lv
->le_count
);
522 return EINVALID_CMD_LINE
;
524 lp
->resize
= LV_REDUCE
;
527 if (lp
->extents
> lv
->le_count
) {
528 if (lp
->resize
== LV_REDUCE
) {
529 log_error("New size given (%d extents) not less than "
530 "existing size (%d extents)", lp
->extents
,
532 return EINVALID_CMD_LINE
;
534 lp
->resize
= LV_EXTEND
;
537 if (lp
->mirrors
&& activation() &&
538 lv_info(cmd
, lv
, &info
, 0, 0) && info
.exists
) {
539 log_error("Mirrors cannot be resized while active yet.");
543 if (lv_is_origin(lv
)) {
544 if (lp
->resize
== LV_REDUCE
) {
545 log_error("Snapshot origin volumes cannot be reduced "
550 memset(&info
, 0, sizeof(info
));
552 if (lv_info(cmd
, lv
, &info
, 0, 0) && info
.exists
) {
553 log_error("Snapshot origin volumes can be resized "
554 "only while inactive: try lvchange -an");
559 if (lp
->resize
== LV_REDUCE
) {
561 log_warn("Ignoring PVs on command line when reducing");
564 if (lp
->resize
== LV_REDUCE
|| lp
->resizefs
) {
565 if (!confirm_resizefs_reduce(cmd
, vg
, lv
, lp
))
570 if (!do_resizefs_reduce(cmd
, vg
, lp
))
579 log_print("%sing logical volume %s to %s",
580 (lp
->resize
== LV_REDUCE
) ? "Reduc" : "Extend",
582 display_size(cmd
, (uint64_t) lp
->extents
* vg
->extent_size
));
584 if (lp
->resize
== LV_REDUCE
) {
585 if (!lv_reduce(lv
, lv
->le_count
- lp
->extents
)) {
589 } else if (!lv_extend(lv
, lp
->segtype
, lp
->stripes
,
590 lp
->stripe_size
, lp
->mirrors
,
591 lp
->extents
- lv
->le_count
,
592 NULL
, 0u, 0u, pvh
, alloc
)) {
597 /* store vg on disk(s) */
605 /* If snapshot, must suspend all associated devices */
607 lock_lv
= origin_from_cow(lv
);
611 if (!suspend_lv(cmd
, lock_lv
)) {
612 log_error("Failed to suspend %s", lp
->lv_name
);
617 if (!vg_commit(vg
)) {
619 resume_lv(cmd
, lock_lv
);
623 if (!resume_lv(cmd
, lock_lv
)) {
624 log_error("Problem reactivating %s", lp
->lv_name
);
628 log_print("Logical volume %s successfully resized", lp
->lv_name
);
630 if (lp
->resizefs
&& (lp
->resize
== LV_EXTEND
)) {
631 if (!exec_cmd("fsadm", "resize", lv_path
, size_buf
)) {
637 return ECMD_PROCESSED
;
640 int lvresize(struct cmd_context
*cmd
, int argc
, char **argv
)
642 struct lvresize_params lp
;
643 struct volume_group
*vg
;
646 memset(&lp
, 0, sizeof(lp
));
648 if (!_lvresize_params(cmd
, argc
, argv
, &lp
))
649 return EINVALID_CMD_LINE
;
651 log_verbose("Finding volume group %s", lp
.vg_name
);
652 if (!(vg
= vg_lock_and_read(cmd
, lp
.vg_name
, NULL
, LCK_VG_WRITE
,
653 CLUSTERED
| EXPORTED_VG
| LVM_WRITE
,
654 CORRECT_INCONSISTENT
))) {
655 log_error("Volume group %s doesn't exist", lp
.vg_name
);
659 if (!(r
= _lvresize(cmd
, vg
, &lp
)))
662 unlock_vg(cmd
, lp
.vg_name
);
This page took 0.092721 seconds and 6 git commands to generate.