]> sourceware.org Git - lvm2.git/blob - tools/lvresize.c
Replace free_vg with release_vg
[lvm2.git] / tools / lvresize.c
1 /*
2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2009 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, 0) == 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) > 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 (dm_snprintf(lv_path, PATH_MAX, "%s%s/%s", cmd->dev_dir, lp->vg_name,
163 lp->lv_name) < 0) {
164 log_error("Couldn't create LV path for %s", lp->lv_name);
165 return 0;
166 }
167
168 argv[i++] = lv_path;
169
170 if (fcmd == FSADM_CMD_RESIZE) {
171 if (dm_snprintf(size_buf, SIZE_BUF, "%" PRIu64 "K",
172 (uint64_t) lp->extents * vg->extent_size / 2) < 0) {
173 log_error("Couldn't generate new LV size string");
174 return 0;
175 }
176
177 argv[i++] = size_buf;
178 }
179
180 argv[i] = NULL;
181
182 return exec_cmd(cmd, argv, status, 1);
183 }
184
185 static int _lvresize_params(struct cmd_context *cmd, int argc, char **argv,
186 struct lvresize_params *lp)
187 {
188 const char *cmd_name;
189 char *st;
190 unsigned dev_dir_found = 0;
191 int use_policy = arg_count(cmd, use_policies_ARG);
192
193 lp->sign = SIGN_NONE;
194 lp->resize = LV_ANY;
195
196 cmd_name = command_name(cmd);
197 if (!strcmp(cmd_name, "lvreduce"))
198 lp->resize = LV_REDUCE;
199 if (!strcmp(cmd_name, "lvextend"))
200 lp->resize = LV_EXTEND;
201
202 if (use_policy) {
203 /* do nothing; _lvresize will handle --use-policies itself */
204 lp->extents = 0;
205 lp->sign = SIGN_PLUS;
206 lp->percent = PERCENT_LV;
207 } else {
208 /*
209 * Allow omission of extents and size if the user has given us
210 * one or more PVs. Most likely, the intent was "resize this
211 * LV the best you can with these PVs"
212 */
213 if ((arg_count(cmd, extents_ARG) + arg_count(cmd, size_ARG) == 0) &&
214 (argc >= 2)) {
215 lp->extents = 100;
216 lp->percent = PERCENT_PVS;
217 lp->sign = SIGN_PLUS;
218 } else if ((arg_count(cmd, extents_ARG) +
219 arg_count(cmd, size_ARG) != 1)) {
220 log_error("Please specify either size or extents but not "
221 "both.");
222 return 0;
223 }
224
225 if (arg_count(cmd, extents_ARG)) {
226 lp->extents = arg_uint_value(cmd, extents_ARG, 0);
227 lp->sign = arg_sign_value(cmd, extents_ARG, SIGN_NONE);
228 lp->percent = arg_percent_value(cmd, extents_ARG, PERCENT_NONE);
229 }
230
231 /* Size returned in kilobyte units; held in sectors */
232 if (arg_count(cmd, size_ARG)) {
233 lp->size = arg_uint64_value(cmd, size_ARG, 0);
234 lp->sign = arg_sign_value(cmd, size_ARG, SIGN_NONE);
235 lp->percent = PERCENT_NONE;
236 }
237 }
238
239 if (lp->resize == LV_EXTEND && lp->sign == SIGN_MINUS) {
240 log_error("Negative argument not permitted - use lvreduce");
241 return 0;
242 }
243
244 if (lp->resize == LV_REDUCE && lp->sign == SIGN_PLUS) {
245 log_error("Positive sign not permitted - use lvextend");
246 return 0;
247 }
248
249 lp->resizefs = arg_is_set(cmd, resizefs_ARG);
250 lp->nofsck = arg_is_set(cmd, nofsck_ARG);
251
252 if (!argc) {
253 log_error("Please provide the logical volume name");
254 return 0;
255 }
256
257 lp->lv_name = argv[0];
258 argv++;
259 argc--;
260
261 if (!(lp->lv_name = skip_dev_dir(cmd, lp->lv_name, &dev_dir_found)) ||
262 !(lp->vg_name = extract_vgname(cmd, lp->lv_name))) {
263 log_error("Please provide a volume group name");
264 return 0;
265 }
266
267 if (!validate_name(lp->vg_name)) {
268 log_error("Volume group name %s has invalid characters",
269 lp->vg_name);
270 return 0;
271 }
272
273 if ((st = strrchr(lp->lv_name, '/')))
274 lp->lv_name = st + 1;
275
276 lp->argc = argc;
277 lp->argv = argv;
278
279 return 1;
280 }
281
282 static int _adjust_policy_params(struct cmd_context *cmd,
283 struct logical_volume *lv, struct lvresize_params *lp)
284 {
285 percent_t percent;
286 int policy_threshold, policy_amount;
287
288 policy_threshold =
289 find_config_tree_int(cmd, "activation/snapshot_autoextend_threshold",
290 DEFAULT_SNAPSHOT_AUTOEXTEND_THRESHOLD) * PERCENT_1;
291 policy_amount =
292 find_config_tree_int(cmd, "activation/snapshot_autoextend_percent",
293 DEFAULT_SNAPSHOT_AUTOEXTEND_PERCENT);
294
295 if (policy_threshold >= PERCENT_100)
296 return 1; /* nothing to do */
297
298 if (!lv_snapshot_percent(lv, &percent))
299 return_0;
300
301 if (!(PERCENT_0 < percent && percent < PERCENT_100) || percent <= policy_threshold)
302 return 1; /* nothing to do */
303
304 lp->extents = policy_amount;
305 return 1;
306 }
307
308 static uint32_t lvseg_get_stripes(struct lv_segment *seg, uint32_t *stripesize)
309 {
310 uint32_t s;
311 struct lv_segment *seg_mirr;
312
313 /* If segment mirrored, check if images are striped */
314 if (seg_is_mirrored(seg))
315 for (s = 0; s < seg->area_count; s++) {
316 if (seg_type(seg, s) != AREA_LV)
317 continue;
318 seg_mirr = first_seg(seg_lv(seg, s));
319
320 if (seg_is_striped(seg_mirr)) {
321 seg = seg_mirr;
322 break;
323 }
324 }
325
326
327 if (seg_is_striped(seg)) {
328 *stripesize = seg->stripe_size;
329 return seg->area_count;
330 }
331
332 *stripesize = 0;
333 return 0;
334 }
335
336 static int _lvresize(struct cmd_context *cmd, struct volume_group *vg,
337 struct lvresize_params *lp)
338 {
339 struct logical_volume *lv;
340 struct lvinfo info;
341 uint32_t stripesize_extents = 0;
342 uint32_t seg_stripes = 0, seg_stripesize = 0, seg_size = 0;
343 uint32_t seg_mirrors = 0;
344 uint32_t extents_used = 0;
345 uint32_t size_rest;
346 uint32_t pv_extent_count = 0;
347 alloc_policy_t alloc;
348 struct logical_volume *lock_lv;
349 struct lv_list *lvl;
350 struct lv_segment *seg, *uninitialized_var(mirr_seg);
351 uint32_t seg_extents;
352 uint32_t sz, str;
353 int status;
354 struct dm_list *pvh = NULL;
355 int use_policy = arg_count(cmd, use_policies_ARG);
356
357 /* does LV exist? */
358 if (!(lvl = find_lv_in_vg(vg, lp->lv_name))) {
359 log_error("Logical volume %s not found in volume group %s",
360 lp->lv_name, lp->vg_name);
361 return ECMD_FAILED;
362 }
363
364 if (arg_count(cmd, stripes_ARG)) {
365 if (vg->fid->fmt->features & FMT_SEGMENTS)
366 lp->stripes = arg_uint_value(cmd, stripes_ARG, 1);
367 else
368 log_warn("Varied striping not supported. Ignoring.");
369 }
370
371 if (arg_count(cmd, mirrors_ARG)) {
372 if (vg->fid->fmt->features & FMT_SEGMENTS)
373 lp->mirrors = arg_uint_value(cmd, mirrors_ARG, 1) + 1;
374 else
375 log_warn("Mirrors not supported. Ignoring.");
376 if (arg_sign_value(cmd, mirrors_ARG, 0) == SIGN_MINUS) {
377 log_error("Mirrors argument may not be negative");
378 return EINVALID_CMD_LINE;
379 }
380 }
381
382 if (arg_count(cmd, stripesize_ARG) &&
383 !_validate_stripesize(cmd, vg, lp))
384 return EINVALID_CMD_LINE;
385
386 lv = lvl->lv;
387
388 if (use_policy) {
389 if (!lv_is_cow(lv)) {
390 log_error("Can't use policy-based resize for non-snapshot volumes.");
391 return ECMD_FAILED;
392 }
393 _adjust_policy_params(cmd, lv, lp);
394 }
395
396 if (!lv_is_visible(lv)) {
397 log_error("Can't resize internal logical volume %s", lv->name);
398 return ECMD_FAILED;
399 }
400
401 if (lv->status & LOCKED) {
402 log_error("Can't resize locked LV %s", lv->name);
403 return ECMD_FAILED;
404 }
405
406 if (lv->status & CONVERTING) {
407 log_error("Can't resize %s while lvconvert in progress", lv->name);
408 return ECMD_FAILED;
409 }
410
411 alloc = arg_uint_value(cmd, alloc_ARG, lv->alloc);
412
413 if (lp->size) {
414 if (lp->size % vg->extent_size) {
415 if (lp->sign == SIGN_MINUS)
416 lp->size -= lp->size % vg->extent_size;
417 else
418 lp->size += vg->extent_size -
419 (lp->size % vg->extent_size);
420
421 log_print("Rounding up size to full physical extent %s",
422 display_size(cmd, (uint64_t) lp->size));
423 }
424
425 lp->extents = lp->size / vg->extent_size;
426 }
427
428 if (!(pvh = lp->argc ? create_pv_list(cmd->mem, vg, lp->argc,
429 lp->argv, 1) : &vg->pvs)) {
430 stack;
431 return ECMD_FAILED;
432 }
433
434 switch(lp->percent) {
435 case PERCENT_VG:
436 lp->extents = lp->extents * vg->extent_count / 100;
437 break;
438 case PERCENT_FREE:
439 lp->extents = lp->extents * vg->free_count / 100;
440 break;
441 case PERCENT_LV:
442 lp->extents = lp->extents * lv->le_count / 100;
443 break;
444 case PERCENT_PVS:
445 if (lp->argc) {
446 pv_extent_count = pv_list_extents_free(pvh);
447 lp->extents = lp->extents * pv_extent_count / 100;
448 } else
449 lp->extents = lp->extents * vg->extent_count / 100;
450 break;
451 case PERCENT_ORIGIN:
452 if (!lv_is_cow(lv)) {
453 log_error("Specified LV does not have an origin LV.");
454 return EINVALID_CMD_LINE;
455 }
456 lp->extents = lp->extents * origin_from_cow(lv)->le_count / 100;
457 break;
458 case PERCENT_NONE:
459 break;
460 }
461
462 if (lp->sign == SIGN_PLUS)
463 lp->extents += lv->le_count;
464
465 if (lp->sign == SIGN_MINUS) {
466 if (lp->extents >= lv->le_count) {
467 log_error("Unable to reduce %s below 1 extent",
468 lp->lv_name);
469 return EINVALID_CMD_LINE;
470 }
471
472 lp->extents = lv->le_count - lp->extents;
473 }
474
475 if (!lp->extents) {
476 log_error("New size of 0 not permitted");
477 return EINVALID_CMD_LINE;
478 }
479
480 if (lp->extents == lv->le_count) {
481 if (use_policy)
482 return ECMD_PROCESSED; /* Nothing to do. */
483 if (!lp->resizefs) {
484 log_error("New size (%d extents) matches existing size "
485 "(%d extents)", lp->extents, lv->le_count);
486 return EINVALID_CMD_LINE;
487 }
488 lp->resize = LV_EXTEND; /* lets pretend zero size extension */
489 }
490
491 seg_size = lp->extents - lv->le_count;
492
493 /* Use segment type of last segment */
494 dm_list_iterate_items(seg, &lv->segments) {
495 lp->segtype = seg->segtype;
496 }
497
498 /* FIXME Support LVs with mixed segment types */
499 if (lp->segtype != get_segtype_from_string(cmd, arg_str_value(cmd, type_ARG,
500 lp->segtype->name))) {
501 log_error("VolumeType does not match (%s)", lp->segtype->name);
502 return EINVALID_CMD_LINE;
503 }
504
505 /* If extending, find mirrors of last segment */
506 if ((lp->extents > lv->le_count)) {
507 dm_list_iterate_back_items(mirr_seg, &lv->segments) {
508 if (seg_is_mirrored(mirr_seg))
509 seg_mirrors = lv_mirror_count(mirr_seg->lv);
510 else
511 seg_mirrors = 0;
512 break;
513 }
514 if (!arg_count(cmd, mirrors_ARG) && seg_mirrors) {
515 log_print("Extending %" PRIu32 " mirror images.",
516 seg_mirrors);
517 lp->mirrors = seg_mirrors;
518 }
519 if ((arg_count(cmd, mirrors_ARG) || seg_mirrors) &&
520 (lp->mirrors != seg_mirrors)) {
521 log_error("Cannot vary number of mirrors in LV yet.");
522 return EINVALID_CMD_LINE;
523 }
524 }
525
526 /* If extending, find stripes, stripesize & size of last segment */
527 if ((lp->extents > lv->le_count) &&
528 !(lp->stripes == 1 || (lp->stripes > 1 && lp->stripe_size))) {
529 /* FIXME Don't assume mirror seg will always be AREA_LV */
530 dm_list_iterate_items(seg, seg_mirrors ? &seg_lv(mirr_seg, 0)->segments : &lv->segments) {
531 if (!seg_is_striped(seg))
532 continue;
533
534 sz = seg->stripe_size;
535 str = seg->area_count;
536
537 if ((seg_stripesize && seg_stripesize != sz &&
538 sz && !lp->stripe_size) ||
539 (seg_stripes && seg_stripes != str && !lp->stripes)) {
540 log_error("Please specify number of "
541 "stripes (-i) and stripesize (-I)");
542 return EINVALID_CMD_LINE;
543 }
544
545 seg_stripesize = sz;
546 seg_stripes = str;
547 }
548
549 if (!lp->stripes)
550 lp->stripes = seg_stripes;
551
552 if (!lp->stripe_size && lp->stripes > 1) {
553 if (seg_stripesize) {
554 log_print("Using stripesize of last segment %s",
555 display_size(cmd, (uint64_t) seg_stripesize));
556 lp->stripe_size = seg_stripesize;
557 } else {
558 lp->stripe_size =
559 find_config_tree_int(cmd,
560 "metadata/stripesize",
561 DEFAULT_STRIPESIZE) * 2;
562 log_print("Using default stripesize %s",
563 display_size(cmd, (uint64_t) lp->stripe_size));
564 }
565 }
566 }
567
568 /* If reducing, find stripes, stripesize & size of last segment */
569 if (lp->extents < lv->le_count) {
570 extents_used = 0;
571
572 if (lp->stripes || lp->stripe_size || lp->mirrors)
573 log_error("Ignoring stripes, stripesize and mirrors "
574 "arguments when reducing");
575
576 dm_list_iterate_items(seg, &lv->segments) {
577 seg_extents = seg->len;
578
579 /* Check for underlying stripe sizes */
580 seg_stripes = lvseg_get_stripes(seg, &seg_stripesize);
581
582 if (seg_is_mirrored(seg))
583 seg_mirrors = lv_mirror_count(seg->lv);
584 else
585 seg_mirrors = 0;
586
587 if (lp->extents <= extents_used + seg_extents)
588 break;
589
590 extents_used += seg_extents;
591 }
592
593 seg_size = lp->extents - extents_used;
594 lp->stripe_size = seg_stripesize;
595 lp->stripes = seg_stripes;
596 lp->mirrors = seg_mirrors;
597 }
598
599 if (lp->stripes > 1 && !lp->stripe_size) {
600 log_error("Stripesize for striped segment should not be 0!");
601 return EINVALID_CMD_LINE;
602 }
603
604 if (lp->stripes > 1) {
605 if (!(stripesize_extents = lp->stripe_size / vg->extent_size))
606 stripesize_extents = 1;
607
608 size_rest = seg_size % (lp->stripes * stripesize_extents);
609 if (size_rest && lp->resize == LV_REDUCE) {
610 log_print("Rounding size (%d extents) up to stripe "
611 "boundary size for segment (%d extents)",
612 lp->extents, lp->extents - size_rest +
613 (lp->stripes * stripesize_extents));
614 lp->extents = lp->extents - size_rest +
615 (lp->stripes * stripesize_extents);
616 } else if (size_rest) {
617 log_print("Rounding size (%d extents) down to stripe "
618 "boundary size for segment (%d extents)",
619 lp->extents, lp->extents - size_rest);
620 lp->extents = lp->extents - size_rest;
621 }
622
623 if (lp->stripe_size < STRIPE_SIZE_MIN) {
624 log_error("Invalid stripe size %s",
625 display_size(cmd, (uint64_t) lp->stripe_size));
626 return EINVALID_CMD_LINE;
627 }
628 }
629
630 if (lp->extents < lv->le_count) {
631 if (lp->resize == LV_EXTEND) {
632 log_error("New size given (%d extents) not larger "
633 "than existing size (%d extents)",
634 lp->extents, lv->le_count);
635 return EINVALID_CMD_LINE;
636 }
637 lp->resize = LV_REDUCE;
638 } else if (lp->extents > lv->le_count) {
639 if (lp->resize == LV_REDUCE) {
640 log_error("New size given (%d extents) not less than "
641 "existing size (%d extents)", lp->extents,
642 lv->le_count);
643 return EINVALID_CMD_LINE;
644 }
645 lp->resize = LV_EXTEND;
646 }
647
648 if (lv_is_origin(lv)) {
649 if (lp->resize == LV_REDUCE) {
650 log_error("Snapshot origin volumes cannot be reduced "
651 "in size yet.");
652 return ECMD_FAILED;
653 }
654
655 memset(&info, 0, sizeof(info));
656
657 if (lv_info(cmd, lv, 0, &info, 0, 0) && info.exists) {
658 log_error("Snapshot origin volumes can be resized "
659 "only while inactive: try lvchange -an");
660 return ECMD_FAILED;
661 }
662 }
663
664 if ((lp->resize == LV_REDUCE) && lp->argc)
665 log_warn("Ignoring PVs on command line when reducing");
666
667 /* Request confirmation before operations that are often mistakes. */
668 if ((lp->resizefs || (lp->resize == LV_REDUCE)) &&
669 !_request_confirmation(cmd, vg, lv, lp)) {
670 stack;
671 return ECMD_FAILED;
672 }
673
674 if (lp->resizefs) {
675 if (!lp->nofsck &&
676 !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_CHECK, &status)) {
677 if (status != FSADM_CHECK_FAILS_FOR_MOUNTED) {
678 stack;
679 return ECMD_FAILED;
680 }
681 /* some filesystems supports online resize */
682 }
683
684 if ((lp->resize == LV_REDUCE) &&
685 !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_RESIZE, NULL)) {
686 stack;
687 return ECMD_FAILED;
688 }
689 }
690
691 if (!archive(vg)) {
692 stack;
693 return ECMD_FAILED;
694 }
695
696 log_print("%sing logical volume %s to %s",
697 (lp->resize == LV_REDUCE) ? "Reduc" : "Extend",
698 lp->lv_name,
699 display_size(cmd, (uint64_t) lp->extents * vg->extent_size));
700
701 if (lp->resize == LV_REDUCE) {
702 if (!lv_reduce(lv, lv->le_count - lp->extents)) {
703 stack;
704 return ECMD_FAILED;
705 }
706 } else if ((lp->extents > lv->le_count) && /* Ensure we extend */
707 !lv_extend(lv, lp->segtype,
708 lp->stripes, lp->stripe_size,
709 lp->mirrors, first_seg(lv)->region_size,
710 lp->extents - lv->le_count,
711 pvh, alloc)) {
712 stack;
713 return ECMD_FAILED;
714 }
715
716 /* store vg on disk(s) */
717 if (!vg_write(vg)) {
718 stack;
719 return ECMD_FAILED;
720 }
721
722 /* If snapshot, must suspend all associated devices */
723 if (lv_is_cow(lv))
724 lock_lv = origin_from_cow(lv);
725 else
726 lock_lv = lv;
727
728 if (!suspend_lv(cmd, lock_lv)) {
729 log_error("Failed to suspend %s", lp->lv_name);
730 vg_revert(vg);
731 backup(vg);
732 return ECMD_FAILED;
733 }
734
735 if (!vg_commit(vg)) {
736 stack;
737 if (!resume_lv(cmd, lock_lv))
738 stack;
739 backup(vg);
740 return ECMD_FAILED;
741 }
742
743 if (!resume_lv(cmd, lock_lv)) {
744 log_error("Problem reactivating %s", lp->lv_name);
745 backup(vg);
746 return ECMD_FAILED;
747 }
748
749 backup(vg);
750
751 log_print("Logical volume %s successfully resized", lp->lv_name);
752
753 if (lp->resizefs && (lp->resize == LV_EXTEND) &&
754 !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_RESIZE, NULL)) {
755 stack;
756 return ECMD_FAILED;
757 }
758
759 return ECMD_PROCESSED;
760 }
761
762 int lvresize(struct cmd_context *cmd, int argc, char **argv)
763 {
764 struct lvresize_params lp;
765 struct volume_group *vg;
766 int r;
767
768 memset(&lp, 0, sizeof(lp));
769
770 if (!_lvresize_params(cmd, argc, argv, &lp))
771 return EINVALID_CMD_LINE;
772
773 log_verbose("Finding volume group %s", lp.vg_name);
774 vg = vg_read_for_update(cmd, lp.vg_name, NULL, 0);
775 if (vg_read_error(vg)) {
776 release_vg(vg);
777 stack;
778 return ECMD_FAILED;
779 }
780
781 if (!(r = _lvresize(cmd, vg, &lp)))
782 stack;
783
784 unlock_and_release_vg(cmd, vg, lp.vg_name);
785
786 return r;
787 }
This page took 0.07564 seconds and 6 git commands to generate.