]> sourceware.org Git - lvm2.git/blob - tools/lvresize.c
Remove 'up' from rounding message that sometimes rounds down.
[lvm2.git] / tools / lvresize.c
1 /*
2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2012 Red Hat, Inc. All rights reserved.
4 *
5 * This file is part of LVM2.
6 *
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.
10 *
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
14 */
15
16 #include "tools.h"
17
18 #define SIZE_BUF 128
19
20 struct lvresize_params {
21 const char *vg_name;
22 const char *lv_name;
23
24 uint32_t stripes;
25 uint32_t stripe_size;
26 uint32_t mirrors;
27
28 const struct segment_type *segtype;
29
30 /* size */
31 uint32_t extents;
32 uint64_t size;
33 sign_t sign;
34 percent_type_t percent;
35
36 enum {
37 LV_ANY = 0,
38 LV_REDUCE = 1,
39 LV_EXTEND = 2
40 } resize;
41
42 int resizefs;
43 int nofsck;
44
45 int argc;
46 char **argv;
47 };
48
49 static int _validate_stripesize(struct cmd_context *cmd,
50 const struct volume_group *vg,
51 struct lvresize_params *lp)
52 {
53 if (arg_sign_value(cmd, stripesize_ARG, SIGN_NONE) == SIGN_MINUS) {
54 log_error("Stripesize may not be negative.");
55 return 0;
56 }
57
58 if (arg_uint64_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));
61 return 0;
62 }
63
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) > (uint64_t) vg->extent_size * 2) {
67 log_error("Reducing stripe size %s to maximum, "
68 "physical extent size %s",
69 display_size(cmd,
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;
73 } else
74 lp->stripe_size = arg_uint_value(cmd, stripesize_ARG, 0);
75
76 if (lp->stripe_size & (lp->stripe_size - 1)) {
77 log_error("Stripe size must be power of 2");
78 return 0;
79 }
80
81 return 1;
82 }
83
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)
88 {
89 struct lvinfo info;
90
91 memset(&info, 0, sizeof(info));
92
93 if (!lv_info(cmd, lv, 0, &info, 1, 0) && driver_version(NULL, 0)) {
94 log_error("lv_info failed: aborting");
95 return 0;
96 }
97
98 if (lp->resizefs) {
99 if (!info.exists) {
100 log_error("Logical volume %s must be activated "
101 "before resizing filesystem", lp->lv_name);
102 return 0;
103 }
104 return 1;
105 }
106
107 if (!info.exists)
108 return 1;
109
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));
113
114 log_warn("THIS MAY DESTROY YOUR DATA (filesystem etc.)");
115
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);
120 return 0;
121 }
122 if (sigint_caught())
123 return 0;
124 }
125
126 return 1;
127 }
128
129 enum fsadm_cmd_e { FSADM_CMD_CHECK, FSADM_CMD_RESIZE };
130 #define FSADM_CMD "fsadm"
131 #define FSADM_CMD_MAX_ARGS 6
132 #define FSADM_CHECK_FAILS_FOR_MOUNTED 3 /* shell exist status code */
133
134 /*
135 * FSADM_CMD --dry-run --verbose --force check lv_path
136 * FSADM_CMD --dry-run --verbose --force resize lv_path size
137 */
138 static int _fsadm_cmd(struct cmd_context *cmd,
139 const struct volume_group *vg,
140 const struct lvresize_params *lp,
141 enum fsadm_cmd_e fcmd,
142 int *status)
143 {
144 char lv_path[PATH_MAX];
145 char size_buf[SIZE_BUF];
146 const char *argv[FSADM_CMD_MAX_ARGS + 2];
147 unsigned i = 0;
148
149 argv[i++] = FSADM_CMD;
150
151 if (test_mode())
152 argv[i++] = "--dry-run";
153
154 if (verbose_level() >= _LOG_NOTICE)
155 argv[i++] = "--verbose";
156
157 if (arg_count(cmd, force_ARG))
158 argv[i++] = "--force";
159
160 argv[i++] = (fcmd == FSADM_CMD_RESIZE) ? "resize" : "check";
161
162 if (status)
163 *status = -1;
164
165 if (dm_snprintf(lv_path, PATH_MAX, "%s%s/%s", cmd->dev_dir, lp->vg_name,
166 lp->lv_name) < 0) {
167 log_error("Couldn't create LV path for %s", lp->lv_name);
168 return 0;
169 }
170
171 argv[i++] = lv_path;
172
173 if (fcmd == FSADM_CMD_RESIZE) {
174 if (dm_snprintf(size_buf, SIZE_BUF, "%" PRIu64 "K",
175 (uint64_t) lp->extents * vg->extent_size / 2) < 0) {
176 log_error("Couldn't generate new LV size string");
177 return 0;
178 }
179
180 argv[i++] = size_buf;
181 }
182
183 argv[i] = NULL;
184
185 return exec_cmd(cmd, argv, status, 1);
186 }
187
188 static int _lvresize_params(struct cmd_context *cmd, int argc, char **argv,
189 struct lvresize_params *lp)
190 {
191 const char *cmd_name;
192 char *st;
193 unsigned dev_dir_found = 0;
194 int use_policy = arg_count(cmd, use_policies_ARG);
195
196 lp->sign = SIGN_NONE;
197 lp->resize = LV_ANY;
198
199 cmd_name = command_name(cmd);
200 if (!strcmp(cmd_name, "lvreduce"))
201 lp->resize = LV_REDUCE;
202 if (!strcmp(cmd_name, "lvextend"))
203 lp->resize = LV_EXTEND;
204
205 if (use_policy) {
206 /* do nothing; _lvresize will handle --use-policies itself */
207 lp->extents = 0;
208 lp->sign = SIGN_PLUS;
209 lp->percent = PERCENT_LV;
210 } else {
211 /*
212 * Allow omission of extents and size if the user has given us
213 * one or more PVs. Most likely, the intent was "resize this
214 * LV the best you can with these PVs"
215 */
216 if ((arg_count(cmd, extents_ARG) + arg_count(cmd, size_ARG) == 0) &&
217 (argc >= 2)) {
218 lp->extents = 100;
219 lp->percent = PERCENT_PVS;
220 lp->sign = SIGN_PLUS;
221 } else if ((arg_count(cmd, extents_ARG) +
222 arg_count(cmd, size_ARG) != 1)) {
223 log_error("Please specify either size or extents but not "
224 "both.");
225 return 0;
226 }
227
228 if (arg_count(cmd, extents_ARG)) {
229 lp->extents = arg_uint_value(cmd, extents_ARG, 0);
230 lp->sign = arg_sign_value(cmd, extents_ARG, SIGN_NONE);
231 lp->percent = arg_percent_value(cmd, extents_ARG, PERCENT_NONE);
232 }
233
234 /* Size returned in kilobyte units; held in sectors */
235 if (arg_count(cmd, size_ARG)) {
236 lp->size = arg_uint64_value(cmd, size_ARG, 0);
237 lp->sign = arg_sign_value(cmd, size_ARG, SIGN_NONE);
238 lp->percent = PERCENT_NONE;
239 }
240 }
241
242 if (lp->resize == LV_EXTEND && lp->sign == SIGN_MINUS) {
243 log_error("Negative argument not permitted - use lvreduce");
244 return 0;
245 }
246
247 if (lp->resize == LV_REDUCE && lp->sign == SIGN_PLUS) {
248 log_error("Positive sign not permitted - use lvextend");
249 return 0;
250 }
251
252 lp->resizefs = arg_is_set(cmd, resizefs_ARG);
253 lp->nofsck = arg_is_set(cmd, nofsck_ARG);
254
255 if (!argc) {
256 log_error("Please provide the logical volume name");
257 return 0;
258 }
259
260 lp->lv_name = argv[0];
261 argv++;
262 argc--;
263
264 if (!(lp->lv_name = skip_dev_dir(cmd, lp->lv_name, &dev_dir_found)) ||
265 !(lp->vg_name = extract_vgname(cmd, lp->lv_name))) {
266 log_error("Please provide a volume group name");
267 return 0;
268 }
269
270 if (!validate_name(lp->vg_name)) {
271 log_error("Volume group name %s has invalid characters",
272 lp->vg_name);
273 return 0;
274 }
275
276 if ((st = strrchr(lp->lv_name, '/')))
277 lp->lv_name = st + 1;
278
279 lp->argc = argc;
280 lp->argv = argv;
281
282 return 1;
283 }
284
285 static int _adjust_policy_params(struct cmd_context *cmd,
286 struct logical_volume *lv, struct lvresize_params *lp)
287 {
288 percent_t percent;
289 int policy_threshold, policy_amount;
290
291 if (lv_is_thin_pool(lv)) {
292 policy_threshold =
293 find_config_tree_int(cmd, "activation/thin_pool_autoextend_threshold",
294 DEFAULT_THIN_POOL_AUTOEXTEND_THRESHOLD) * PERCENT_1;
295 policy_amount =
296 find_config_tree_int(cmd, "activation/thin_pool_autoextend_percent",
297 DEFAULT_THIN_POOL_AUTOEXTEND_PERCENT);
298 } else {
299 policy_threshold =
300 find_config_tree_int(cmd, "activation/snapshot_autoextend_threshold",
301 DEFAULT_SNAPSHOT_AUTOEXTEND_THRESHOLD) * PERCENT_1;
302 policy_amount =
303 find_config_tree_int(cmd, "activation/snapshot_autoextend_percent",
304 DEFAULT_SNAPSHOT_AUTOEXTEND_PERCENT);
305 }
306
307 if (policy_threshold >= PERCENT_100)
308 return 1; /* nothing to do */
309
310 if (lv_is_thin_pool(lv)) {
311 if (!lv_thin_pool_percent(lv, 1, &percent))
312 return_0;
313 if (percent > policy_threshold) {
314 /* FIXME: metadata resize support missing */
315 log_error("Resize for %s/%s is not yet supported.",
316 lp->vg_name, lp->lv_name);
317 return ECMD_FAILED;
318 }
319
320 if (!lv_thin_pool_percent(lv, 0, &percent))
321 return_0;
322 if (!(PERCENT_0 < percent && percent <= PERCENT_100) ||
323 percent <= policy_threshold)
324 return 1; /* nothing to do */
325 } else {
326 if (!lv_snapshot_percent(lv, &percent))
327 return_0;
328 if (!(PERCENT_0 < percent && percent < PERCENT_100) || percent <= policy_threshold)
329 return 1; /* nothing to do */
330 }
331
332 lp->extents = policy_amount;
333
334 return 1;
335 }
336
337 static uint32_t lvseg_get_stripes(struct lv_segment *seg, uint32_t *stripesize)
338 {
339 uint32_t s;
340 struct lv_segment *seg_mirr;
341
342 /* If segment mirrored, check if images are striped */
343 if (seg_is_mirrored(seg))
344 for (s = 0; s < seg->area_count; s++) {
345 if (seg_type(seg, s) != AREA_LV)
346 continue;
347 seg_mirr = first_seg(seg_lv(seg, s));
348
349 if (seg_is_striped(seg_mirr)) {
350 seg = seg_mirr;
351 break;
352 }
353 }
354
355
356 if (seg_is_striped(seg)) {
357 *stripesize = seg->stripe_size;
358 return seg->area_count;
359 }
360
361 *stripesize = 0;
362 return 0;
363 }
364
365 static int _lvresize(struct cmd_context *cmd, struct volume_group *vg,
366 struct lvresize_params *lp)
367 {
368 struct logical_volume *lv;
369 struct lvinfo info;
370 uint32_t stripesize_extents;
371 uint32_t seg_stripes = 0, seg_stripesize = 0, seg_size;
372 uint32_t seg_mirrors = 0;
373 uint32_t extents_used;
374 uint32_t size_rest;
375 uint32_t pv_extent_count;
376 alloc_policy_t alloc;
377 struct logical_volume *lock_lv;
378 struct lv_list *lvl;
379 struct lv_segment *seg, *uninitialized_var(mirr_seg);
380 uint32_t seg_extents;
381 uint32_t sz, str;
382 int status;
383 struct dm_list *pvh = NULL;
384 int use_policy = arg_count(cmd, use_policies_ARG);
385
386 /* does LV exist? */
387 if (!(lvl = find_lv_in_vg(vg, lp->lv_name))) {
388 log_error("Logical volume %s not found in volume group %s",
389 lp->lv_name, lp->vg_name);
390 return ECMD_FAILED;
391 }
392
393 if (lvl->lv->status & (RAID_IMAGE | RAID_META)) {
394 log_error("Cannot resize a RAID %s directly",
395 (lvl->lv->status & RAID_IMAGE) ? "image" :
396 "metadata area");
397 return ECMD_FAILED;
398 }
399
400 if (lv_is_raid_with_tracking(lvl->lv)) {
401 log_error("Cannot resize %s while it is tracking a split image",
402 lvl->lv->name);
403 return ECMD_FAILED;
404 }
405
406 if (arg_count(cmd, stripes_ARG)) {
407 if (vg->fid->fmt->features & FMT_SEGMENTS)
408 lp->stripes = arg_uint_value(cmd, stripes_ARG, 1);
409 else
410 log_warn("Varied striping not supported. Ignoring.");
411 }
412
413 if (arg_count(cmd, mirrors_ARG)) {
414 if (vg->fid->fmt->features & FMT_SEGMENTS)
415 lp->mirrors = arg_uint_value(cmd, mirrors_ARG, 1) + 1;
416 else
417 log_warn("Mirrors not supported. Ignoring.");
418 if (arg_sign_value(cmd, mirrors_ARG, SIGN_NONE) == SIGN_MINUS) {
419 log_error("Mirrors argument may not be negative");
420 return EINVALID_CMD_LINE;
421 }
422 }
423
424 if (arg_count(cmd, stripesize_ARG) &&
425 !_validate_stripesize(cmd, vg, lp))
426 return EINVALID_CMD_LINE;
427
428 lv = lvl->lv;
429
430 if (use_policy) {
431 if (!lv_is_cow(lv) &&
432 !lv_is_thin_pool(lv)) {
433 log_error("Policy-based resize is supported only for snapshot and thin pool volumes.");
434 return ECMD_FAILED;
435 }
436 if (!_adjust_policy_params(cmd, lv, lp))
437 return ECMD_FAILED;
438 }
439
440 if (!lv_is_visible(lv)) {
441 log_error("Can't resize internal logical volume %s", lv->name);
442 return ECMD_FAILED;
443 }
444
445 if (lv->status & LOCKED) {
446 log_error("Can't resize locked LV %s", lv->name);
447 return ECMD_FAILED;
448 }
449
450 if (lv->status & CONVERTING) {
451 log_error("Can't resize %s while lvconvert in progress", lv->name);
452 return ECMD_FAILED;
453 }
454
455 alloc = (alloc_policy_t) arg_uint_value(cmd, alloc_ARG, lv->alloc);
456
457 /*
458 * First adjust to an exact multiple of extent size.
459 * When extending by a relative amount we round that amount up.
460 * When reducing by a relative amount we remove at most that amount.
461 * When changing to an absolute size, we round that size up.
462 */
463 if (lp->size) {
464 if (lp->size % vg->extent_size) {
465 if (lp->sign == SIGN_MINUS)
466 lp->size -= lp->size % vg->extent_size;
467 else
468 lp->size += vg->extent_size -
469 (lp->size % vg->extent_size);
470
471 log_print("Rounding size to boundary between physical extents: %s",
472 display_size(cmd, lp->size));
473 }
474
475 lp->extents = lp->size / vg->extent_size;
476 }
477
478 if (!(pvh = lp->argc ? create_pv_list(cmd->mem, vg, lp->argc,
479 lp->argv, 1) : &vg->pvs)) {
480 stack;
481 return ECMD_FAILED;
482 }
483
484 switch(lp->percent) {
485 case PERCENT_VG:
486 lp->extents = percent_of_extents(lp->extents, vg->extent_count,
487 (lp->sign != SIGN_MINUS));
488 break;
489 case PERCENT_FREE:
490 lp->extents = percent_of_extents(lp->extents, vg->free_count,
491 (lp->sign != SIGN_MINUS));
492 break;
493 case PERCENT_LV:
494 lp->extents = percent_of_extents(lp->extents, lv->le_count,
495 (lp->sign != SIGN_MINUS));
496 break;
497 case PERCENT_PVS:
498 if (lp->argc) {
499 pv_extent_count = pv_list_extents_free(pvh);
500 lp->extents = percent_of_extents(lp->extents, pv_extent_count,
501 (lp->sign != SIGN_MINUS));
502 } else
503 lp->extents = percent_of_extents(lp->extents, vg->extent_count,
504 (lp->sign != SIGN_MINUS));
505 break;
506 case PERCENT_ORIGIN:
507 if (!lv_is_cow(lv)) {
508 log_error("Specified LV does not have an origin LV.");
509 return EINVALID_CMD_LINE;
510 }
511 lp->extents = percent_of_extents(lp->extents, origin_from_cow(lv)->le_count,
512 (lp->sign != SIGN_MINUS));
513 break;
514 case PERCENT_NONE:
515 break;
516 }
517
518 if (lp->sign == SIGN_PLUS) {
519 if (lp->extents >= (MAX_EXTENT_COUNT - lv->le_count)) {
520 log_error("Unable to extend %s by %u extents, exceeds limit (%u).",
521 lp->lv_name, lv->le_count, MAX_EXTENT_COUNT);
522 return EINVALID_CMD_LINE;
523 }
524 lp->extents += lv->le_count;
525 }
526
527 if (lp->sign == SIGN_MINUS) {
528 if (lp->extents >= lv->le_count) {
529 log_error("Unable to reduce %s below 1 extent",
530 lp->lv_name);
531 return EINVALID_CMD_LINE;
532 }
533
534 lp->extents = lv->le_count - lp->extents;
535 }
536
537 if (!lp->extents) {
538 log_error("New size of 0 not permitted");
539 return EINVALID_CMD_LINE;
540 }
541
542 if (lp->extents == lv->le_count) {
543 if (use_policy)
544 return ECMD_PROCESSED; /* Nothing to do. */
545 if (!lp->resizefs) {
546 log_error("New size (%d extents) matches existing size "
547 "(%d extents)", lp->extents, lv->le_count);
548 return EINVALID_CMD_LINE;
549 }
550 lp->resize = LV_EXTEND; /* lets pretend zero size extension */
551 }
552
553 seg_size = lp->extents - lv->le_count;
554
555 /* Use segment type of last segment */
556 dm_list_iterate_items(seg, &lv->segments) {
557 lp->segtype = seg->segtype;
558 }
559
560 /* FIXME Support LVs with mixed segment types */
561 if (lp->segtype != get_segtype_from_string(cmd, arg_str_value(cmd, type_ARG,
562 lp->segtype->name))) {
563 log_error("VolumeType does not match (%s)", lp->segtype->name);
564 return EINVALID_CMD_LINE;
565 }
566
567 /* If extending, find mirrors of last segment */
568 if ((lp->extents > lv->le_count)) {
569 /*
570 * Has the user specified that they would like the additional
571 * extents of a mirror not to have an initial sync?
572 */
573 if (seg_is_mirrored(first_seg(lv)) && arg_count(cmd, nosync_ARG))
574 lv->status |= LV_NOTSYNCED;
575
576 dm_list_iterate_back_items(mirr_seg, &lv->segments) {
577 if (seg_is_mirrored(mirr_seg))
578 seg_mirrors = lv_mirror_count(mirr_seg->lv);
579 else
580 seg_mirrors = 0;
581 break;
582 }
583 if (!arg_count(cmd, mirrors_ARG) && seg_mirrors) {
584 log_print("Extending %" PRIu32 " mirror images.",
585 seg_mirrors);
586 lp->mirrors = seg_mirrors;
587 }
588 if ((arg_count(cmd, mirrors_ARG) || seg_mirrors) &&
589 (lp->mirrors != seg_mirrors)) {
590 log_error("Cannot vary number of mirrors in LV yet.");
591 return EINVALID_CMD_LINE;
592 }
593 }
594
595 /* If extending, find stripes, stripesize & size of last segment */
596 if ((lp->extents > lv->le_count) &&
597 !(lp->stripes == 1 || (lp->stripes > 1 && lp->stripe_size))) {
598 /* FIXME Don't assume mirror seg will always be AREA_LV */
599 /* FIXME We will need to support resize for metadata LV as well,
600 * and data LV could be any type (i.e. mirror)) */
601 dm_list_iterate_items(seg, seg_mirrors ? &seg_lv(mirr_seg, 0)->segments :
602 lv_is_thin_pool(lv) ? &seg_lv(first_seg(lv), 0)->segments : &lv->segments) {
603 if (!seg_is_striped(seg))
604 continue;
605
606 sz = seg->stripe_size;
607 str = seg->area_count;
608
609 if ((seg_stripesize && seg_stripesize != sz &&
610 sz && !lp->stripe_size) ||
611 (seg_stripes && seg_stripes != str && !lp->stripes)) {
612 log_error("Please specify number of "
613 "stripes (-i) and stripesize (-I)");
614 return EINVALID_CMD_LINE;
615 }
616
617 seg_stripesize = sz;
618 seg_stripes = str;
619 }
620
621 if (!lp->stripes)
622 lp->stripes = seg_stripes;
623
624 if (!lp->stripe_size && lp->stripes > 1) {
625 if (seg_stripesize) {
626 log_print("Using stripesize of last segment %s",
627 display_size(cmd, (uint64_t) seg_stripesize));
628 lp->stripe_size = seg_stripesize;
629 } else {
630 lp->stripe_size =
631 find_config_tree_int(cmd,
632 "metadata/stripesize",
633 DEFAULT_STRIPESIZE) * 2;
634 log_print("Using default stripesize %s",
635 display_size(cmd, (uint64_t) lp->stripe_size));
636 }
637 }
638 }
639
640 /* If reducing, find stripes, stripesize & size of last segment */
641 if (lp->extents < lv->le_count) {
642 extents_used = 0;
643
644 if (lp->stripes || lp->stripe_size || lp->mirrors)
645 log_error("Ignoring stripes, stripesize and mirrors "
646 "arguments when reducing");
647
648 dm_list_iterate_items(seg, &lv->segments) {
649 seg_extents = seg->len;
650
651 /* Check for underlying stripe sizes */
652 seg_stripes = lvseg_get_stripes(seg, &seg_stripesize);
653
654 if (seg_is_mirrored(seg))
655 seg_mirrors = lv_mirror_count(seg->lv);
656 else
657 seg_mirrors = 0;
658
659 if (lp->extents <= extents_used + seg_extents)
660 break;
661
662 extents_used += seg_extents;
663 }
664
665 seg_size = lp->extents - extents_used;
666 lp->stripe_size = seg_stripesize;
667 lp->stripes = seg_stripes;
668 lp->mirrors = seg_mirrors;
669 }
670
671 if (lp->stripes > 1 && !lp->stripe_size) {
672 log_error("Stripesize for striped segment should not be 0!");
673 return EINVALID_CMD_LINE;
674 }
675
676 if (lp->stripes > 1) {
677 if (lp->stripe_size < STRIPE_SIZE_MIN) {
678 log_error("Invalid stripe size %s",
679 display_size(cmd, (uint64_t) lp->stripe_size));
680 return EINVALID_CMD_LINE;
681 }
682
683 if (!(stripesize_extents = lp->stripe_size / vg->extent_size))
684 stripesize_extents = 1;
685
686 size_rest = seg_size % (lp->stripes * stripesize_extents);
687 /* Round toward the original size. */
688 if (size_rest &&
689 ((lp->extents < lv->le_count) ||
690 !lp->percent ||
691 (vg->free_count >= (lp->extents - lv->le_count - size_rest +
692 (lp->stripes * stripesize_extents))))) {
693 log_print("Rounding size (%d extents) up to stripe "
694 "boundary size for segment (%d extents)",
695 lp->extents, lp->extents - size_rest +
696 (lp->stripes * stripesize_extents));
697 lp->extents = lp->extents - size_rest +
698 (lp->stripes * stripesize_extents);
699 } else if (size_rest) {
700 log_print("Rounding size (%d extents) down to stripe "
701 "boundary size for segment (%d extents)",
702 lp->extents, lp->extents - size_rest);
703 lp->extents = lp->extents - size_rest;
704 }
705 }
706
707 if (lp->extents < lv->le_count) {
708 if (lp->resize == LV_EXTEND) {
709 log_error("New size given (%d extents) not larger "
710 "than existing size (%d extents)",
711 lp->extents, lv->le_count);
712 return EINVALID_CMD_LINE;
713 }
714 lp->resize = LV_REDUCE;
715 } else if (lp->extents > lv->le_count) {
716 if (lp->resize == LV_REDUCE) {
717 log_error("New size given (%d extents) not less than "
718 "existing size (%d extents)", lp->extents,
719 lv->le_count);
720 return EINVALID_CMD_LINE;
721 }
722 lp->resize = LV_EXTEND;
723 } else if (lp->extents == lv->le_count) {
724 if (use_policy)
725 return ECMD_PROCESSED; /* Nothing to do. */
726 if (!lp->resizefs) {
727 log_error("New size (%d extents) matches existing size "
728 "(%d extents)", lp->extents, lv->le_count);
729 return EINVALID_CMD_LINE;
730 }
731 lp->resize = LV_EXTEND;
732 }
733
734 if (lv_is_origin(lv)) {
735 if (lp->resize == LV_REDUCE) {
736 log_error("Snapshot origin volumes cannot be reduced "
737 "in size yet.");
738 return ECMD_FAILED;
739 }
740
741 memset(&info, 0, sizeof(info));
742
743 if (lv_info(cmd, lv, 0, &info, 0, 0) && info.exists) {
744 log_error("Snapshot origin volumes can be resized "
745 "only while inactive: try lvchange -an");
746 return ECMD_FAILED;
747 }
748 }
749
750 if (lv_is_thin_pool(lv)) {
751 if (lp->resize == LV_REDUCE) {
752 log_error("Thin pool volumes cannot be reduced in size yet.");
753 return ECMD_FAILED;
754 }
755
756 if (lp->resizefs) {
757 log_warn("Thin pool volumes do not have filesystem.");
758 lp->resizefs = 0;
759 }
760 }
761
762 if ((lp->resize == LV_REDUCE) && lp->argc)
763 log_warn("Ignoring PVs on command line when reducing");
764
765 /* Request confirmation before operations that are often mistakes. */
766 if ((lp->resizefs || (lp->resize == LV_REDUCE)) &&
767 !_request_confirmation(cmd, vg, lv, lp)) {
768 stack;
769 return ECMD_FAILED;
770 }
771
772 if (lp->resizefs) {
773 if (!lp->nofsck &&
774 !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_CHECK, &status)) {
775 if (status != FSADM_CHECK_FAILS_FOR_MOUNTED) {
776 log_error("Filesystem check failed.");
777 return ECMD_FAILED;
778 }
779 /* some filesystems supports online resize */
780 }
781
782 if ((lp->resize == LV_REDUCE) &&
783 !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_RESIZE, NULL)) {
784 log_error("Filesystem resize failed.");
785 return ECMD_FAILED;
786 }
787 }
788
789 if (!archive(vg)) {
790 stack;
791 return ECMD_FAILED;
792 }
793
794 log_print("%sing logical volume %s to %s",
795 (lp->resize == LV_REDUCE) ? "Reduc" : "Extend",
796 lp->lv_name,
797 display_size(cmd, (uint64_t) lp->extents * vg->extent_size));
798
799 if (lp->resize == LV_REDUCE) {
800 if (!lv_reduce(lv, lv->le_count - lp->extents)) {
801 stack;
802 return ECMD_FAILED;
803 }
804 } else if ((lp->extents > lv->le_count) && /* Ensure we extend */
805 !lv_extend(lv, lp->segtype,
806 lp->stripes, lp->stripe_size,
807 lp->mirrors, first_seg(lv)->region_size,
808 lp->extents - lv->le_count, NULL,
809 pvh, alloc)) {
810 stack;
811 return ECMD_FAILED;
812 }
813
814 /* store vg on disk(s) */
815 if (!vg_write(vg)) {
816 stack;
817 return ECMD_FAILED;
818 }
819
820 /* If snapshot, must suspend all associated devices */
821 if (lv_is_cow(lv))
822 lock_lv = origin_from_cow(lv);
823 else
824 lock_lv = lv;
825
826 if (!suspend_lv(cmd, lock_lv)) {
827 log_error("Failed to suspend %s", lp->lv_name);
828 vg_revert(vg);
829 backup(vg);
830 return ECMD_FAILED;
831 }
832
833 if (!vg_commit(vg)) {
834 stack;
835 if (!resume_lv(cmd, lock_lv))
836 stack;
837 backup(vg);
838 return ECMD_FAILED;
839 }
840
841 if (!resume_lv(cmd, lock_lv)) {
842 log_error("Problem reactivating %s", lp->lv_name);
843 backup(vg);
844 return ECMD_FAILED;
845 }
846
847 backup(vg);
848
849 /*
850 * Update lvm pool metadata (drop messages) if the pool has been
851 * resumed and do a pool active/deactivate in other case.
852 *
853 * Note: Active thin pool can be waiting for resize.
854 *
855 * FIXME: Activate only when thin volume is active
856 */
857 if (lv_is_thin_pool(lv) &&
858 !update_pool_lv(lv, !lv_is_active(lv))) {
859 stack;
860 return ECMD_FAILED;
861 }
862
863 log_print("Logical volume %s successfully resized", lp->lv_name);
864
865 if (lp->resizefs && (lp->resize == LV_EXTEND) &&
866 !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_RESIZE, NULL)) {
867 stack;
868 return ECMD_FAILED;
869 }
870
871 return ECMD_PROCESSED;
872 }
873
874 int lvresize(struct cmd_context *cmd, int argc, char **argv)
875 {
876 struct lvresize_params lp;
877 struct volume_group *vg;
878 int r;
879
880 memset(&lp, 0, sizeof(lp));
881
882 if (!_lvresize_params(cmd, argc, argv, &lp))
883 return EINVALID_CMD_LINE;
884
885 log_verbose("Finding volume group %s", lp.vg_name);
886 vg = vg_read_for_update(cmd, lp.vg_name, NULL, 0);
887 if (vg_read_error(vg)) {
888 release_vg(vg);
889 stack;
890 return ECMD_FAILED;
891 }
892
893 if (!(r = _lvresize(cmd, vg, &lp)))
894 stack;
895
896 unlock_and_release_vg(cmd, vg, lp.vg_name);
897
898 return r;
899 }
This page took 0.080898 seconds and 6 git commands to generate.