]>
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 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
) {
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
) {
67 log_error("Reducing stripe size %s to maximum, "
68 "physical extent size %s",
70 (uint64_t) arg_uint_value(cmd
, stripesize_ARG
, 0) * 2),
71 display_size(cmd
, (uint64_t) vg
->extent_size
));
72 lp
->stripe_size
= vg
->extent_size
;
74 lp
->stripe_size
= 2 * arg_uint_value(cmd
,
78 log_error("Mirrors and striping cannot be combined yet.");
81 if (lp
->stripe_size
& (lp
->stripe_size
- 1)) {
82 log_error("Stripe size must be power of 2");
89 static int confirm_resizefs_reduce(struct cmd_context
*cmd
,
90 struct volume_group
*vg
,
91 struct logical_volume
*lv
,
92 struct lvresize_params
*lp
)
96 memset(&info
, 0, sizeof(info
));
98 if (!lv_info(cmd
, lv
, &info
, 1) && driver_version(NULL
, 0)) {
99 log_error("lv_info failed: aborting");
103 if (lp
->resizefs
&& !info
.exists
) {
104 log_error("Logical volume %s must be activated "
105 "before resizing filesystem", lp
->lv_name
);
109 if (info
.exists
&& !lp
->resizefs
&& (lp
->resize
== LV_REDUCE
)) {
110 log_warn("WARNING: Reducing active%s logical volume "
111 "to %s", info
.open_count
? " and open" : "",
112 display_size(cmd
, (uint64_t) lp
->extents
*
115 log_warn("THIS MAY DESTROY YOUR DATA "
116 "(filesystem etc.)");
118 if (!arg_count(cmd
, force_ARG
)) {
119 if (yes_no_prompt("Do you really want to "
120 "reduce %s? [y/n]: ",
121 lp
->lv_name
) == 'n') {
122 log_print("Logical volume %s NOT "
123 "reduced", lp
->lv_name
);
134 static int do_resizefs_reduce(struct cmd_context
*cmd
, struct volume_group
*vg
,
135 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
;
185 if (arg_count(cmd
, extents_ARG
) + arg_count(cmd
, size_ARG
) != 1) {
186 log_error("Please specify either size or extents (not both)");
190 if (arg_count(cmd
, extents_ARG
)) {
191 lp
->extents
= arg_uint_value(cmd
, extents_ARG
, 0);
192 lp
->sign
= arg_sign_value(cmd
, extents_ARG
, SIGN_NONE
);
193 lp
->percent
= arg_percent_value(cmd
, extents_ARG
, PERCENT_NONE
);
196 /* Size returned in kilobyte units; held in sectors */
197 if (arg_count(cmd
, size_ARG
)) {
198 lp
->size
= arg_uint64_value(cmd
, size_ARG
, UINT64_C(0)) * 2;
199 lp
->sign
= arg_sign_value(cmd
, size_ARG
, SIGN_NONE
);
200 lp
->percent
= PERCENT_NONE
;
203 if (lp
->resize
== LV_EXTEND
&& lp
->sign
== SIGN_MINUS
) {
204 log_error("Negative argument not permitted - use lvreduce");
208 if (lp
->resize
== LV_REDUCE
&& lp
->sign
== SIGN_PLUS
) {
209 log_error("Positive sign not permitted - use lvextend");
213 lp
->resizefs
= arg_count(cmd
, resizefs_ARG
) ? 1 : 0;
214 lp
->nofsck
= arg_count(cmd
, nofsck_ARG
) ? 1 : 0;
217 log_error("Please provide the logical volume name");
221 lp
->lv_name
= argv
[0];
225 if (!(lp
->vg_name
= extract_vgname(cmd
, lp
->lv_name
))) {
226 log_error("Please provide a volume group name");
230 if ((st
= strrchr(lp
->lv_name
, '/')))
231 lp
->lv_name
= st
+ 1;
239 static int _lvresize(struct cmd_context
*cmd
, struct lvresize_params
*lp
)
241 struct volume_group
*vg
;
242 struct logical_volume
*lv
;
244 uint32_t stripesize_extents
= 0;
245 uint32_t seg_stripes
= 0, seg_stripesize
= 0, seg_size
= 0;
246 uint32_t seg_mirrors
= 0;
247 uint32_t extents_used
= 0;
249 alloc_policy_t alloc
;
250 struct logical_volume
*lock_lv
;
253 struct lv_segment
*seg
;
254 uint32_t seg_extents
;
256 struct list
*pvh
= NULL
;
257 char size_buf
[SIZE_BUF
];
258 char lv_path
[PATH_MAX
];
260 if (!(vg
= vg_read(cmd
, lp
->vg_name
, NULL
, &consistent
))) {
261 log_error("Volume group %s doesn't exist", lp
->vg_name
);
265 if (!vg_check_status(vg
, CLUSTERED
| EXPORTED_VG
| LVM_WRITE
))
269 if (!(lvl
= find_lv_in_vg(vg
, lp
->lv_name
))) {
270 log_error("Logical volume %s not found in volume group %s",
271 lp
->lv_name
, lp
->vg_name
);
275 if (arg_count(cmd
, stripes_ARG
)) {
276 if (vg
->fid
->fmt
->features
& FMT_SEGMENTS
)
277 lp
->stripes
= arg_uint_value(cmd
, stripes_ARG
, 1);
279 log_warn("Varied striping not supported. Ignoring.");
282 if (arg_count(cmd
, mirrors_ARG
)) {
283 if (vg
->fid
->fmt
->features
& FMT_SEGMENTS
)
284 lp
->mirrors
= arg_uint_value(cmd
, mirrors_ARG
, 1) + 1;
286 log_warn("Mirrors not supported. Ignoring.");
287 if (arg_sign_value(cmd
, mirrors_ARG
, 0) == SIGN_MINUS
) {
288 log_error("Mirrors argument may not be negative");
289 return EINVALID_CMD_LINE
;
293 if (arg_count(cmd
, stripesize_ARG
)) {
294 if (!validate_stripesize(cmd
, vg
, lp
))
295 return EINVALID_CMD_LINE
;
300 if (lv
->status
& LOCKED
) {
301 log_error("Can't resize locked LV %s", lv
->name
);
305 alloc
= arg_uint_value(cmd
, alloc_ARG
, lv
->alloc
);
308 if (lp
->size
% vg
->extent_size
) {
309 if (lp
->sign
== SIGN_MINUS
)
310 lp
->size
-= lp
->size
% vg
->extent_size
;
312 lp
->size
+= vg
->extent_size
-
313 (lp
->size
% vg
->extent_size
);
315 log_print("Rounding up size to full physical extent %s",
316 display_size(cmd
, (uint64_t) lp
->size
));
319 lp
->extents
= lp
->size
/ vg
->extent_size
;
322 switch(lp
->percent
) {
324 lp
->extents
= lp
->extents
* vg
->extent_count
/ 100;
327 lp
->extents
= lp
->extents
* vg
->free_count
/ 100;
330 lp
->extents
= lp
->extents
* lv
->le_count
/ 100;
336 if (lp
->sign
== SIGN_PLUS
)
337 lp
->extents
+= lv
->le_count
;
339 if (lp
->sign
== SIGN_MINUS
) {
340 if (lp
->extents
>= lv
->le_count
) {
341 log_error("Unable to reduce %s below 1 extent",
343 return EINVALID_CMD_LINE
;
346 lp
->extents
= lv
->le_count
- lp
->extents
;
350 log_error("New size of 0 not permitted");
351 return EINVALID_CMD_LINE
;
354 if (lp
->extents
== lv
->le_count
) {
355 log_error("New size (%d extents) matches existing size "
356 "(%d extents)", lp
->extents
, lv
->le_count
);
357 return EINVALID_CMD_LINE
;
360 seg_size
= lp
->extents
- lv
->le_count
;
362 /* Use segment type of last segment */
363 list_iterate_items(seg
, &lv
->segments
) {
364 lp
->segtype
= seg
->segtype
;
367 /* FIXME Support LVs with mixed segment types */
368 if (lp
->segtype
!= arg_ptr_value(cmd
, type_ARG
, lp
->segtype
)) {
369 log_error("VolumeType does not match (%s)", lp
->segtype
->name
);
370 return EINVALID_CMD_LINE
;
373 /* If extending, find stripes, stripesize & size of last segment */
374 if ((lp
->extents
> lv
->le_count
) &&
375 !(lp
->stripes
== 1 || (lp
->stripes
> 1 && lp
->stripe_size
))) {
376 list_iterate_items(seg
, &lv
->segments
) {
377 if (!seg_is_striped(seg
))
380 sz
= seg
->stripe_size
;
381 str
= seg
->area_count
;
383 if ((seg_stripesize
&& seg_stripesize
!= sz
384 && !lp
->stripe_size
) ||
385 (seg_stripes
&& seg_stripes
!= str
&& !lp
->stripes
)) {
386 log_error("Please specify number of "
387 "stripes (-i) and stripesize (-I)");
388 return EINVALID_CMD_LINE
;
396 lp
->stripes
= seg_stripes
;
398 if (!lp
->stripe_size
&& lp
->stripes
> 1) {
399 if (seg_stripesize
) {
400 log_print("Using stripesize of last segment %s",
401 display_size(cmd
, (uint64_t) seg_stripesize
));
402 lp
->stripe_size
= seg_stripesize
;
405 find_config_tree_int(cmd
,
406 "metadata/stripesize",
407 DEFAULT_STRIPESIZE
) * 2;
408 log_print("Using default stripesize %s",
409 display_size(cmd
, (uint64_t) lp
->stripe_size
));
414 /* If extending, find mirrors of last segment */
415 if ((lp
->extents
> lv
->le_count
)) {
416 list_iterate_back_items(seg
, &lv
->segments
) {
417 if (seg_is_mirrored(seg
))
418 seg_mirrors
= seg
->area_count
;
423 if (!arg_count(cmd
, mirrors_ARG
) && seg_mirrors
) {
424 log_print("Extending %" PRIu32
" mirror images.",
426 lp
->mirrors
= seg_mirrors
;
428 if ((arg_count(cmd
, mirrors_ARG
) || seg_mirrors
) &&
429 (lp
->mirrors
!= seg_mirrors
)) {
430 log_error("Cannot vary number of mirrors in LV yet.");
431 return EINVALID_CMD_LINE
;
435 /* If reducing, find stripes, stripesize & size of last segment */
436 if (lp
->extents
< lv
->le_count
) {
439 if (lp
->stripes
|| lp
->stripe_size
|| lp
->mirrors
)
440 log_error("Ignoring stripes, stripesize and mirrors "
441 "arguments when reducing");
443 list_iterate_items(seg
, &lv
->segments
) {
444 seg_extents
= seg
->len
;
446 if (seg_is_striped(seg
)) {
447 seg_stripesize
= seg
->stripe_size
;
448 seg_stripes
= seg
->area_count
;
451 if (seg_is_mirrored(seg
))
452 seg_mirrors
= seg
->area_count
;
456 if (lp
->extents
<= extents_used
+ seg_extents
)
459 extents_used
+= seg_extents
;
462 seg_size
= lp
->extents
- extents_used
;
463 lp
->stripe_size
= seg_stripesize
;
464 lp
->stripes
= seg_stripes
;
465 lp
->mirrors
= seg_mirrors
;
468 if (lp
->stripes
> 1 && !lp
->stripe_size
) {
469 log_error("Stripesize for striped segment should not be 0!");
470 return EINVALID_CMD_LINE
;
473 if ((lp
->stripes
> 1)) {
474 if (!(stripesize_extents
= lp
->stripe_size
/ vg
->extent_size
))
475 stripesize_extents
= 1;
477 if ((size_rest
= seg_size
% (lp
->stripes
* stripesize_extents
))) {
478 log_print("Rounding size (%d extents) down to stripe "
479 "boundary size for segment (%d extents)",
480 lp
->extents
, lp
->extents
- size_rest
);
481 lp
->extents
= lp
->extents
- size_rest
;
484 if (lp
->stripe_size
< STRIPE_SIZE_MIN
) {
485 log_error("Invalid stripe size %s",
486 display_size(cmd
, (uint64_t) lp
->stripe_size
));
487 return EINVALID_CMD_LINE
;
491 if (lp
->extents
== lv
->le_count
) {
492 log_error("New size (%d extents) matches existing size "
493 "(%d extents)", lp
->extents
, lv
->le_count
);
494 return EINVALID_CMD_LINE
;
497 if (lp
->extents
< lv
->le_count
) {
498 if (lp
->resize
== LV_EXTEND
) {
499 log_error("New size given (%d extents) not larger "
500 "than existing size (%d extents)",
501 lp
->extents
, lv
->le_count
);
502 return EINVALID_CMD_LINE
;
504 lp
->resize
= LV_REDUCE
;
507 if (lp
->extents
> lv
->le_count
) {
508 if (lp
->resize
== LV_REDUCE
) {
509 log_error("New size given (%d extents) not less than "
510 "existing size (%d extents)", lp
->extents
,
512 return EINVALID_CMD_LINE
;
514 lp
->resize
= LV_EXTEND
;
517 if (lp
->mirrors
&& activation() &&
518 lv_info(cmd
, lv
, &info
, 0) && info
.exists
) {
519 log_error("Mirrors cannot be resized while active yet.");
523 if (lv_is_origin(lv
)) {
524 if (lp
->resize
== LV_REDUCE
) {
525 log_error("Snapshot origin volumes cannot be reduced "
530 memset(&info
, 0, sizeof(info
));
532 if (lv_info(cmd
, lv
, &info
, 0) && info
.exists
) {
533 log_error("Snapshot origin volumes can be resized "
534 "only while inactive: try lvchange -an");
539 if (lp
->resize
== LV_REDUCE
) {
541 log_warn("Ignoring PVs on command line when reducing");
542 } else if (!(pvh
= lp
->argc
? create_pv_list(cmd
->mem
, vg
, lp
->argc
,
543 lp
->argv
, 1) : &vg
->pvs
)) {
548 if (lp
->resize
== LV_REDUCE
|| lp
->resizefs
) {
549 if (!confirm_resizefs_reduce(cmd
, vg
, lv
, lp
))
554 if (!do_resizefs_reduce(cmd
, vg
, lp
))
563 log_print("%sing logical volume %s to %s",
564 (lp
->resize
== LV_REDUCE
) ? "Reduc" : "Extend",
566 display_size(cmd
, (uint64_t) lp
->extents
* vg
->extent_size
));
568 if (lp
->resize
== LV_REDUCE
) {
569 if (!lv_reduce(lv
, lv
->le_count
- lp
->extents
)) {
573 } else if (!lv_extend(lv
, lp
->segtype
, lp
->stripes
,
574 lp
->stripe_size
, lp
->mirrors
,
575 lp
->extents
- lv
->le_count
,
576 NULL
, 0u, 0u, pvh
, alloc
)) {
581 /* store vg on disk(s) */
589 /* If snapshot, must suspend all associated devices */
591 lock_lv
= origin_from_cow(lv
);
595 if (!suspend_lv(cmd
, lock_lv
)) {
596 log_error("Failed to suspend %s", lp
->lv_name
);
601 if (!vg_commit(vg
)) {
603 resume_lv(cmd
, lock_lv
);
607 if (!resume_lv(cmd
, lock_lv
)) {
608 log_error("Problem reactivating %s", lp
->lv_name
);
612 log_print("Logical volume %s successfully resized", lp
->lv_name
);
614 if (lp
->resizefs
&& (lp
->resize
== LV_EXTEND
)) {
615 if (!exec_cmd("fsadm", "resize", lv_path
, size_buf
)) {
621 return ECMD_PROCESSED
;
624 int lvresize(struct cmd_context
*cmd
, int argc
, char **argv
)
626 struct lvresize_params lp
;
629 memset(&lp
, 0, sizeof(lp
));
631 if (!_lvresize_params(cmd
, argc
, argv
, &lp
))
632 return EINVALID_CMD_LINE
;
634 log_verbose("Finding volume group %s", lp
.vg_name
);
635 if (!lock_vol(cmd
, lp
.vg_name
, LCK_VG_WRITE
)) {
636 log_error("Can't get lock for %s", lp
.vg_name
);
640 if (!(r
= _lvresize(cmd
, &lp
)))
643 unlock_vg(cmd
, lp
.vg_name
);
This page took 0.071087 seconds and 6 git commands to generate.