]>
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 _lvresize_params(struct cmd_context
*cmd
, int argc
, char **argv
,
50 struct lvresize_params
*lp
)
58 cmd_name
= command_name(cmd
);
59 if (!strcmp(cmd_name
, "lvreduce"))
60 lp
->resize
= LV_REDUCE
;
61 if (!strcmp(cmd_name
, "lvextend"))
62 lp
->resize
= LV_EXTEND
;
64 if (arg_count(cmd
, extents_ARG
) + arg_count(cmd
, size_ARG
) != 1) {
65 log_error("Please specify either size or extents (not both)");
69 if (arg_count(cmd
, extents_ARG
)) {
70 lp
->extents
= arg_uint_value(cmd
, extents_ARG
, 0);
71 lp
->sign
= arg_sign_value(cmd
, extents_ARG
, SIGN_NONE
);
72 lp
->percent
= arg_percent_value(cmd
, extents_ARG
, PERCENT_NONE
);
75 /* Size returned in kilobyte units; held in sectors */
76 if (arg_count(cmd
, size_ARG
)) {
77 lp
->size
= arg_uint64_value(cmd
, size_ARG
, UINT64_C(0)) * 2;
78 lp
->sign
= arg_sign_value(cmd
, size_ARG
, SIGN_NONE
);
79 lp
->percent
= PERCENT_NONE
;
82 if (lp
->resize
== LV_EXTEND
&& lp
->sign
== SIGN_MINUS
) {
83 log_error("Negative argument not permitted - use lvreduce");
87 if (lp
->resize
== LV_REDUCE
&& lp
->sign
== SIGN_PLUS
) {
88 log_error("Positive sign not permitted - use lvextend");
92 lp
->resizefs
= arg_count(cmd
, resizefs_ARG
) ? 1 : 0;
93 lp
->nofsck
= arg_count(cmd
, nofsck_ARG
) ? 1 : 0;
96 log_error("Please provide the logical volume name");
100 lp
->lv_name
= argv
[0];
104 if (!(lp
->vg_name
= extract_vgname(cmd
, lp
->lv_name
))) {
105 log_error("Please provide a volume group name");
109 if ((st
= strrchr(lp
->lv_name
, '/')))
110 lp
->lv_name
= st
+ 1;
118 static int _lvresize(struct cmd_context
*cmd
, struct lvresize_params
*lp
)
120 struct volume_group
*vg
;
121 struct logical_volume
*lv
;
123 uint32_t stripesize_extents
= 0;
124 uint32_t seg_stripes
= 0, seg_stripesize
= 0, seg_size
= 0;
125 uint32_t seg_mirrors
= 0;
126 uint32_t extents_used
= 0;
128 alloc_policy_t alloc
;
129 struct logical_volume
*lock_lv
;
132 struct lv_segment
*seg
;
133 uint32_t seg_extents
;
135 struct list
*pvh
= NULL
;
136 char size_buf
[SIZE_BUF
];
137 char lv_path
[PATH_MAX
];
139 if (!(vg
= vg_read(cmd
, lp
->vg_name
, NULL
, &consistent
))) {
140 log_error("Volume group %s doesn't exist", lp
->vg_name
);
144 if (!vg_check_status(vg
, CLUSTERED
| EXPORTED_VG
| LVM_WRITE
))
148 if (!(lvl
= find_lv_in_vg(vg
, lp
->lv_name
))) {
149 log_error("Logical volume %s not found in volume group %s",
150 lp
->lv_name
, lp
->vg_name
);
154 if (arg_count(cmd
, stripes_ARG
)) {
155 if (vg
->fid
->fmt
->features
& FMT_SEGMENTS
)
156 lp
->stripes
= arg_uint_value(cmd
, stripes_ARG
, 1);
158 log_warn("Varied striping not supported. Ignoring.");
161 if (arg_count(cmd
, mirrors_ARG
)) {
162 if (vg
->fid
->fmt
->features
& FMT_SEGMENTS
)
163 lp
->mirrors
= arg_uint_value(cmd
, mirrors_ARG
, 1) + 1;
165 log_warn("Mirrors not supported. Ignoring.");
166 if (arg_sign_value(cmd
, mirrors_ARG
, 0) == SIGN_MINUS
) {
167 log_error("Mirrors argument may not be negative");
172 if (arg_count(cmd
, stripesize_ARG
)) {
173 if (arg_sign_value(cmd
, stripesize_ARG
, 0) == SIGN_MINUS
) {
174 log_error("Stripesize may not be negative.");
178 if (arg_uint_value(cmd
, stripesize_ARG
, 0) > STRIPE_SIZE_LIMIT
) {
179 log_error("Stripe size cannot be larger than %s",
180 display_size(cmd
, (uint64_t) STRIPE_SIZE_LIMIT
));
184 if (!(vg
->fid
->fmt
->features
& FMT_SEGMENTS
))
185 log_warn("Varied stripesize not supported. Ignoring.");
186 else if (arg_uint_value(cmd
, stripesize_ARG
, 0) > vg
->extent_size
) {
187 log_error("Reducing stripe size %s to maximum, "
188 "physical extent size %s",
190 (uint64_t) arg_uint_value(cmd
, stripesize_ARG
, 0) * 2),
191 display_size(cmd
, (uint64_t) vg
->extent_size
));
192 lp
->stripe_size
= vg
->extent_size
;
194 lp
->stripe_size
= 2 * arg_uint_value(cmd
,
198 log_error("Mirrors and striping cannot be combined yet.");
201 if (lp
->stripe_size
& (lp
->stripe_size
- 1)) {
202 log_error("Stripe size must be power of 2");
209 if (lv
->status
& LOCKED
) {
210 log_error("Can't resize locked LV %s", lv
->name
);
214 alloc
= arg_uint_value(cmd
, alloc_ARG
, lv
->alloc
);
217 if (lp
->size
% vg
->extent_size
) {
218 if (lp
->sign
== SIGN_MINUS
)
219 lp
->size
-= lp
->size
% vg
->extent_size
;
221 lp
->size
+= vg
->extent_size
-
222 (lp
->size
% vg
->extent_size
);
224 log_print("Rounding up size to full physical extent %s",
225 display_size(cmd
, (uint64_t) lp
->size
));
228 lp
->extents
= lp
->size
/ vg
->extent_size
;
231 switch(lp
->percent
) {
233 lp
->extents
= lp
->extents
* vg
->extent_count
/ 100;
236 lp
->extents
= lp
->extents
* vg
->free_count
/ 100;
239 lp
->extents
= lp
->extents
* lv
->le_count
/ 100;
245 if (lp
->sign
== SIGN_PLUS
)
246 lp
->extents
+= lv
->le_count
;
248 if (lp
->sign
== SIGN_MINUS
) {
249 if (lp
->extents
>= lv
->le_count
) {
250 log_error("Unable to reduce %s below 1 extent",
252 return EINVALID_CMD_LINE
;
255 lp
->extents
= lv
->le_count
- lp
->extents
;
259 log_error("New size of 0 not permitted");
260 return EINVALID_CMD_LINE
;
263 if (lp
->extents
== lv
->le_count
) {
264 log_error("New size (%d extents) matches existing size "
265 "(%d extents)", lp
->extents
, lv
->le_count
);
266 return EINVALID_CMD_LINE
;
269 seg_size
= lp
->extents
- lv
->le_count
;
271 /* Use segment type of last segment */
272 list_iterate_items(seg
, &lv
->segments
) {
273 lp
->segtype
= seg
->segtype
;
276 /* FIXME Support LVs with mixed segment types */
277 if (lp
->segtype
!= arg_ptr_value(cmd
, type_ARG
, lp
->segtype
)) {
278 log_error("VolumeType does not match (%s)", lp
->segtype
->name
);
279 return EINVALID_CMD_LINE
;
282 /* If extending, find stripes, stripesize & size of last segment */
283 if ((lp
->extents
> lv
->le_count
) &&
284 !(lp
->stripes
== 1 || (lp
->stripes
> 1 && lp
->stripe_size
))) {
285 list_iterate_items(seg
, &lv
->segments
) {
286 if (!seg_is_striped(seg
))
289 sz
= seg
->stripe_size
;
290 str
= seg
->area_count
;
292 if ((seg_stripesize
&& seg_stripesize
!= sz
293 && !lp
->stripe_size
) ||
294 (seg_stripes
&& seg_stripes
!= str
&& !lp
->stripes
)) {
295 log_error("Please specify number of "
296 "stripes (-i) and stripesize (-I)");
297 return EINVALID_CMD_LINE
;
305 lp
->stripes
= seg_stripes
;
307 if (!lp
->stripe_size
&& lp
->stripes
> 1) {
308 if (seg_stripesize
) {
309 log_print("Using stripesize of last segment %s",
310 display_size(cmd
, (uint64_t) seg_stripesize
));
311 lp
->stripe_size
= seg_stripesize
;
314 find_config_tree_int(cmd
,
315 "metadata/stripesize",
316 DEFAULT_STRIPESIZE
) * 2;
317 log_print("Using default stripesize %s",
318 display_size(cmd
, (uint64_t) lp
->stripe_size
));
323 /* If extending, find mirrors of last segment */
324 if ((lp
->extents
> lv
->le_count
)) {
325 list_iterate_back_items(seg
, &lv
->segments
) {
326 if (seg_is_mirrored(seg
))
327 seg_mirrors
= seg
->area_count
;
332 if (!arg_count(cmd
, mirrors_ARG
) && seg_mirrors
) {
333 log_print("Extending %" PRIu32
" mirror images.",
335 lp
->mirrors
= seg_mirrors
;
337 if ((arg_count(cmd
, mirrors_ARG
) || seg_mirrors
) &&
338 (lp
->mirrors
!= seg_mirrors
)) {
339 log_error("Cannot vary number of mirrors in LV yet.");
340 return EINVALID_CMD_LINE
;
344 /* If reducing, find stripes, stripesize & size of last segment */
345 if (lp
->extents
< lv
->le_count
) {
348 if (lp
->stripes
|| lp
->stripe_size
|| lp
->mirrors
)
349 log_error("Ignoring stripes, stripesize and mirrors "
350 "arguments when reducing");
352 list_iterate_items(seg
, &lv
->segments
) {
353 seg_extents
= seg
->len
;
355 if (seg_is_striped(seg
)) {
356 seg_stripesize
= seg
->stripe_size
;
357 seg_stripes
= seg
->area_count
;
360 if (seg_is_mirrored(seg
))
361 seg_mirrors
= seg
->area_count
;
365 if (lp
->extents
<= extents_used
+ seg_extents
)
368 extents_used
+= seg_extents
;
371 seg_size
= lp
->extents
- extents_used
;
372 lp
->stripe_size
= seg_stripesize
;
373 lp
->stripes
= seg_stripes
;
374 lp
->mirrors
= seg_mirrors
;
377 if (lp
->stripes
> 1 && !lp
->stripe_size
) {
378 log_error("Stripesize for striped segment should not be 0!");
379 return EINVALID_CMD_LINE
;
382 if ((lp
->stripes
> 1)) {
383 if (!(stripesize_extents
= lp
->stripe_size
/ vg
->extent_size
))
384 stripesize_extents
= 1;
386 if ((size_rest
= seg_size
% (lp
->stripes
* stripesize_extents
))) {
387 log_print("Rounding size (%d extents) down to stripe "
388 "boundary size for segment (%d extents)",
389 lp
->extents
, lp
->extents
- size_rest
);
390 lp
->extents
= lp
->extents
- size_rest
;
393 if (lp
->stripe_size
< STRIPE_SIZE_MIN
) {
394 log_error("Invalid stripe size %s",
395 display_size(cmd
, (uint64_t) lp
->stripe_size
));
400 if (lp
->extents
== lv
->le_count
) {
401 log_error("New size (%d extents) matches existing size "
402 "(%d extents)", lp
->extents
, lv
->le_count
);
403 return EINVALID_CMD_LINE
;
406 if (lp
->extents
< lv
->le_count
) {
407 if (lp
->resize
== LV_EXTEND
) {
408 log_error("New size given (%d extents) not larger "
409 "than existing size (%d extents)",
410 lp
->extents
, lv
->le_count
);
411 return EINVALID_CMD_LINE
;
413 lp
->resize
= LV_REDUCE
;
416 if (lp
->extents
> lv
->le_count
) {
417 if (lp
->resize
== LV_REDUCE
) {
418 log_error("New size given (%d extents) not less than "
419 "existing size (%d extents)", lp
->extents
,
421 return EINVALID_CMD_LINE
;
423 lp
->resize
= LV_EXTEND
;
426 if (lp
->mirrors
&& activation() &&
427 lv_info(cmd
, lv
, &info
, 0) && info
.exists
) {
428 log_error("Mirrors cannot be resized while active yet.");
432 if (lv_is_origin(lv
)) {
433 if (lp
->resize
== LV_REDUCE
) {
434 log_error("Snapshot origin volumes cannot be reduced "
439 memset(&info
, 0, sizeof(info
));
441 if (lv_info(cmd
, lv
, &info
, 0) && info
.exists
) {
442 log_error("Snapshot origin volumes can be resized "
443 "only while inactive: try lvchange -an");
448 if (lp
->resize
== LV_REDUCE
) {
450 log_warn("Ignoring PVs on command line when reducing");
451 } else if (!(pvh
= lp
->argc
? create_pv_list(cmd
->mem
, vg
, lp
->argc
,
452 lp
->argv
, 1) : &vg
->pvs
)) {
457 if (lp
->resize
== LV_REDUCE
|| lp
->resizefs
) {
458 memset(&info
, 0, sizeof(info
));
460 if (!lv_info(cmd
, lv
, &info
, 1) && driver_version(NULL
, 0)) {
461 log_error("lv_info failed: aborting");
465 if (lp
->resizefs
&& !info
.exists
) {
466 log_error("Logical volume %s must be activated "
467 "before resizing filesystem", lp
->lv_name
);
471 if (info
.exists
&& !lp
->resizefs
&& (lp
->resize
== LV_REDUCE
)) {
472 log_warn("WARNING: Reducing active%s logical volume "
473 "to %s", info
.open_count
? " and open" : "",
474 display_size(cmd
, (uint64_t) lp
->extents
*
477 log_warn("THIS MAY DESTROY YOUR DATA "
478 "(filesystem etc.)");
480 if (!arg_count(cmd
, force_ARG
)) {
481 if (yes_no_prompt("Do you really want to "
482 "reduce %s? [y/n]: ",
483 lp
->lv_name
) == 'n') {
484 log_print("Logical volume %s NOT "
485 "reduced", lp
->lv_name
);
495 if (dm_snprintf(lv_path
, PATH_MAX
, "%s%s/%s", cmd
->dev_dir
,
496 lp
->vg_name
, lp
->lv_name
) < 0) {
497 log_error("Couldn't create LV path for %s",
502 if (dm_snprintf(size_buf
, SIZE_BUF
, "%" PRIu64
,
503 (uint64_t) lp
->extents
* vg
->extent_size
/ 2)
505 log_error("Couldn't generate new LV size string");
510 if (!exec_cmd("fsadm", "check", lv_path
, NULL
)) {
516 if (lp
->resize
== LV_REDUCE
) {
517 if (!exec_cmd("fsadm", "resize", lv_path
, size_buf
)) {
529 log_print("%sing logical volume %s to %s",
530 (lp
->resize
== LV_REDUCE
) ? "Reduc" : "Extend",
532 display_size(cmd
, (uint64_t) lp
->extents
* vg
->extent_size
));
534 if (lp
->resize
== LV_REDUCE
) {
535 if (!lv_reduce(lv
, lv
->le_count
- lp
->extents
)) {
539 } else if (!lv_extend(lv
, lp
->segtype
, lp
->stripes
,
540 lp
->stripe_size
, lp
->mirrors
,
541 lp
->extents
- lv
->le_count
,
542 NULL
, 0u, 0u, pvh
, alloc
)) {
547 /* store vg on disk(s) */
555 /* If snapshot, must suspend all associated devices */
557 lock_lv
= origin_from_cow(lv
);
561 if (!suspend_lv(cmd
, lock_lv
)) {
562 log_error("Failed to suspend %s", lp
->lv_name
);
567 if (!vg_commit(vg
)) {
569 resume_lv(cmd
, lock_lv
);
573 if (!resume_lv(cmd
, lock_lv
)) {
574 log_error("Problem reactivating %s", lp
->lv_name
);
578 log_print("Logical volume %s successfully resized", lp
->lv_name
);
580 if (lp
->resizefs
&& (lp
->resize
== LV_EXTEND
)) {
581 if (!exec_cmd("fsadm", "resize", lv_path
, size_buf
)) {
587 return ECMD_PROCESSED
;
590 int lvresize(struct cmd_context
*cmd
, int argc
, char **argv
)
592 struct lvresize_params lp
;
595 memset(&lp
, 0, sizeof(lp
));
597 if (!_lvresize_params(cmd
, argc
, argv
, &lp
))
598 return EINVALID_CMD_LINE
;
600 log_verbose("Finding volume group %s", lp
.vg_name
);
601 if (!lock_vol(cmd
, lp
.vg_name
, LCK_VG_WRITE
)) {
602 log_error("Can't get lock for %s", lp
.vg_name
);
606 if (!(r
= _lvresize(cmd
, &lp
)))
609 unlock_vg(cmd
, lp
.vg_name
);
This page took 0.186323 seconds and 6 git commands to generate.