]> sourceware.org Git - lvm2.git/blame - tools/lvresize.c
Convert pvchange, pvdisplay, pvscan to use is_orphan()
[lvm2.git] / tools / lvresize.c
CommitLineData
03a8a07d 1/*
1832f310 2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
be684599 3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
03a8a07d 4 *
6606c3ae 5 * This file is part of LVM2.
03a8a07d 6 *
6606c3ae
AK
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
be684599 9 * of the GNU Lesser General Public License v.2.1.
03a8a07d 10 *
be684599 11 * You should have received a copy of the GNU Lesser General Public License
6606c3ae
AK
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
03a8a07d
AK
14 */
15
16#include "tools.h"
17
1a9ea74d
AK
18#define SIZE_BUF 128
19
241913fe 20struct lvresize_params {
8ef2b021 21 const char *vg_name;
241913fe
AK
22 const char *lv_name;
23
24 uint32_t stripes;
25 uint32_t stripe_size;
ffb0e538 26 uint32_t mirrors;
241913fe 27
72b2cb61 28 const struct segment_type *segtype;
241913fe 29
241913fe
AK
30 /* size */
31 uint32_t extents;
32 uint64_t size;
33 sign_t sign;
34fadac4 34 percent_t percent;
03a8a07d
AK
35
36 enum {
37 LV_ANY = 0,
38 LV_REDUCE = 1,
39 LV_EXTEND = 2
241913fe
AK
40 } resize;
41
1a9ea74d
AK
42 int resizefs;
43 int nofsck;
44
241913fe
AK
45 int argc;
46 char **argv;
47};
48
406a9754 49static int validate_stripesize(struct cmd_context *cmd,
bf4f5b21 50 const struct volume_group *vg,
406a9754
DW
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_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));
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) {
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) * 2),
71 display_size(cmd, (uint64_t) vg->extent_size));
72 lp->stripe_size = vg->extent_size;
73 } else
74 lp->stripe_size = 2 * arg_uint_value(cmd,
75 stripesize_ARG, 0);
76
77 if (lp->mirrors) {
78 log_error("Mirrors and striping cannot be combined yet.");
79 return 0;
80 }
81 if (lp->stripe_size & (lp->stripe_size - 1)) {
82 log_error("Stripe size must be power of 2");
83 return 0;
84 }
85
86 return 1;
87}
88
89static int confirm_resizefs_reduce(struct cmd_context *cmd,
bf4f5b21
DW
90 const struct volume_group *vg,
91 const struct logical_volume *lv,
92 const struct lvresize_params *lp)
406a9754
DW
93{
94 struct lvinfo info;
95
96 memset(&info, 0, sizeof(info));
97
98 if (!lv_info(cmd, lv, &info, 1) && driver_version(NULL, 0)) {
99 log_error("lv_info failed: aborting");
100 return 0;
101 }
102
103 if (lp->resizefs && !info.exists) {
104 log_error("Logical volume %s must be activated "
105 "before resizing filesystem", lp->lv_name);
106 return 0;
107 }
108
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 *
113 vg->extent_size));
114
115 log_warn("THIS MAY DESTROY YOUR DATA "
116 "(filesystem etc.)");
117
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);
124 return 0;
125 }
126 if (sigint_caught())
127 return 0;
128 }
129 }
130
131 return 1;
132}
133
bf4f5b21
DW
134static int do_resizefs_reduce(const struct cmd_context *cmd,
135 const struct volume_group *vg,
136 const struct lvresize_params *lp)
406a9754
DW
137{
138 char lv_path[PATH_MAX];
139 char size_buf[SIZE_BUF];
140
141 if (dm_snprintf(lv_path, PATH_MAX, "%s%s/%s", cmd->dev_dir,
142 lp->vg_name, lp->lv_name) < 0) {
143 log_error("Couldn't create LV path for %s",
144 lp->lv_name);
145 return 0;
146 }
147
148 if (dm_snprintf(size_buf, SIZE_BUF, "%" PRIu64,
149 (uint64_t) lp->extents * vg->extent_size / 2) < 0) {
150 log_error("Couldn't generate new LV size string");
151 return 0;
152 }
153
154 if (!lp->nofsck) {
155 if (!exec_cmd("fsadm", "check", lv_path, NULL)) {
156 stack;
157 return 0;
158 }
159 }
160
161 if (lp->resize == LV_REDUCE) {
162 if (!exec_cmd("fsadm", "resize", lv_path, size_buf)) {
163 stack;
164 return 0;
165 }
166 }
167
168 return 1;
169}
170
8a2fc586
AK
171static int _lvresize_params(struct cmd_context *cmd, int argc, char **argv,
172 struct lvresize_params *lp)
241913fe
AK
173{
174 const char *cmd_name;
175 char *st;
176
177 lp->sign = SIGN_NONE;
178 lp->resize = LV_ANY;
03a8a07d 179
60274aba 180 cmd_name = command_name(cmd);
03a8a07d 181 if (!strcmp(cmd_name, "lvreduce"))
241913fe 182 lp->resize = LV_REDUCE;
03a8a07d 183 if (!strcmp(cmd_name, "lvextend"))
241913fe 184 lp->resize = LV_EXTEND;
03a8a07d 185
dfef7f69
DW
186 /*
187 * Allow omission of extents and size if the user has given us
188 * one or more PVs. Most likely, the intent was "resize this
189 * LV the best you can with these PVs"
190 */
191 if ((arg_count(cmd, extents_ARG) + arg_count(cmd, size_ARG) == 0) &&
192 (argc >= 2)) {
193 lp->extents = 100;
194 lp->percent = PERCENT_PVS;
195 lp->sign = SIGN_PLUS;
196 } else if ((arg_count(cmd, extents_ARG) +
197 arg_count(cmd, size_ARG) != 1)) {
198 log_error("Please specify either size or extents but not "
199 "both.");
241913fe 200 return 0;
03a8a07d
AK
201 }
202
6fda126d 203 if (arg_count(cmd, extents_ARG)) {
241913fe
AK
204 lp->extents = arg_uint_value(cmd, extents_ARG, 0);
205 lp->sign = arg_sign_value(cmd, extents_ARG, SIGN_NONE);
34fadac4 206 lp->percent = arg_percent_value(cmd, extents_ARG, PERCENT_NONE);
03a8a07d
AK
207 }
208
1832f310 209 /* Size returned in kilobyte units; held in sectors */
6fda126d 210 if (arg_count(cmd, size_ARG)) {
241913fe
AK
211 lp->size = arg_uint64_value(cmd, size_ARG, UINT64_C(0)) * 2;
212 lp->sign = arg_sign_value(cmd, size_ARG, SIGN_NONE);
34fadac4 213 lp->percent = PERCENT_NONE;
03a8a07d
AK
214 }
215
241913fe 216 if (lp->resize == LV_EXTEND && lp->sign == SIGN_MINUS) {
03a8a07d 217 log_error("Negative argument not permitted - use lvreduce");
241913fe 218 return 0;
03a8a07d
AK
219 }
220
241913fe 221 if (lp->resize == LV_REDUCE && lp->sign == SIGN_PLUS) {
03a8a07d 222 log_error("Positive sign not permitted - use lvextend");
241913fe 223 return 0;
03a8a07d
AK
224 }
225
1a9ea74d
AK
226 lp->resizefs = arg_count(cmd, resizefs_ARG) ? 1 : 0;
227 lp->nofsck = arg_count(cmd, nofsck_ARG) ? 1 : 0;
228
03a8a07d
AK
229 if (!argc) {
230 log_error("Please provide the logical volume name");
241913fe 231 return 0;
03a8a07d
AK
232 }
233
241913fe 234 lp->lv_name = argv[0];
03a8a07d
AK
235 argv++;
236 argc--;
237
241913fe 238 if (!(lp->vg_name = extract_vgname(cmd, lp->lv_name))) {
03a8a07d 239 log_error("Please provide a volume group name");
241913fe 240 return 0;
03a8a07d
AK
241 }
242
241913fe
AK
243 if ((st = strrchr(lp->lv_name, '/')))
244 lp->lv_name = st + 1;
03a8a07d 245
241913fe
AK
246 lp->argc = argc;
247 lp->argv = argv;
248
249 return 1;
250}
7d0e6e80 251
241913fe
AK
252static int _lvresize(struct cmd_context *cmd, struct lvresize_params *lp)
253{
254 struct volume_group *vg;
255 struct logical_volume *lv;
241913fe
AK
256 struct lvinfo info;
257 uint32_t stripesize_extents = 0;
258 uint32_t seg_stripes = 0, seg_stripesize = 0, seg_size = 0;
ffb0e538 259 uint32_t seg_mirrors = 0;
241913fe
AK
260 uint32_t extents_used = 0;
261 uint32_t size_rest;
dfef7f69 262 uint32_t pv_extent_count = 0;
a0a23eff 263 alloc_policy_t alloc;
0fb173aa 264 struct logical_volume *lock_lv;
241913fe
AK
265 struct lv_list *lvl;
266 int consistent = 1;
267 struct lv_segment *seg;
268 uint32_t seg_extents;
269 uint32_t sz, str;
7f0dc9c4 270 struct list *pvh = NULL;
1a9ea74d
AK
271 char size_buf[SIZE_BUF];
272 char lv_path[PATH_MAX];
241913fe 273
a5fe5a7c 274 if (!(vg = vg_read(cmd, lp->vg_name, NULL, &consistent))) {
241913fe 275 log_error("Volume group %s doesn't exist", lp->vg_name);
7f0dc9c4 276 return ECMD_FAILED;
03a8a07d
AK
277 }
278
c221b0bc 279 if (!vg_check_status(vg, CLUSTERED | EXPORTED_VG | LVM_WRITE))
dc4d7417 280 return ECMD_FAILED;
f53c6aa6 281
03a8a07d 282 /* does LV exist? */
241913fe 283 if (!(lvl = find_lv_in_vg(vg, lp->lv_name))) {
03a8a07d 284 log_error("Logical volume %s not found in volume group %s",
241913fe 285 lp->lv_name, lp->vg_name);
7f0dc9c4 286 return ECMD_FAILED;
03a8a07d
AK
287 }
288
25b73380
AK
289 if (arg_count(cmd, stripes_ARG)) {
290 if (vg->fid->fmt->features & FMT_SEGMENTS)
241913fe 291 lp->stripes = arg_uint_value(cmd, stripes_ARG, 1);
25b73380 292 else
e7ddf416 293 log_warn("Varied striping not supported. Ignoring.");
25b73380
AK
294 }
295
ffb0e538
AK
296 if (arg_count(cmd, mirrors_ARG)) {
297 if (vg->fid->fmt->features & FMT_SEGMENTS)
298 lp->mirrors = arg_uint_value(cmd, mirrors_ARG, 1) + 1;
299 else
e7ddf416 300 log_warn("Mirrors not supported. Ignoring.");
7424de5b
AK
301 if (arg_sign_value(cmd, mirrors_ARG, 0) == SIGN_MINUS) {
302 log_error("Mirrors argument may not be negative");
406a9754 303 return EINVALID_CMD_LINE;
7424de5b 304 }
ffb0e538
AK
305 }
306
25b73380 307 if (arg_count(cmd, stripesize_ARG)) {
406a9754
DW
308 if (!validate_stripesize(cmd, vg, lp))
309 return EINVALID_CMD_LINE;
25b73380
AK
310 }
311
f868d635 312 lv = lvl->lv;
03a8a07d 313
6e03b44c
AK
314 if (lv->status & LOCKED) {
315 log_error("Can't resize locked LV %s", lv->name);
7f0dc9c4 316 return ECMD_FAILED;
6e03b44c
AK
317 }
318
72b2cb61 319 alloc = arg_uint_value(cmd, alloc_ARG, lv->alloc);
7f0dc9c4 320
241913fe
AK
321 if (lp->size) {
322 if (lp->size % vg->extent_size) {
323 if (lp->sign == SIGN_MINUS)
324 lp->size -= lp->size % vg->extent_size;
03a8a07d 325 else
241913fe
AK
326 lp->size += vg->extent_size -
327 (lp->size % vg->extent_size);
03a8a07d
AK
328
329 log_print("Rounding up size to full physical extent %s",
72b2cb61 330 display_size(cmd, (uint64_t) lp->size));
03a8a07d
AK
331 }
332
241913fe 333 lp->extents = lp->size / vg->extent_size;
03a8a07d
AK
334 }
335
dfef7f69
DW
336 if (!(pvh = lp->argc ? create_pv_list(cmd->mem, vg, lp->argc,
337 lp->argv, 1) : &vg->pvs)) {
338 stack;
339 return ECMD_FAILED;
340 }
341
34fadac4
AK
342 switch(lp->percent) {
343 case PERCENT_VG:
344 lp->extents = lp->extents * vg->extent_count / 100;
345 break;
346 case PERCENT_FREE:
347 lp->extents = lp->extents * vg->free_count / 100;
348 break;
349 case PERCENT_LV:
350 lp->extents = lp->extents * lv->le_count / 100;
351 break;
dfef7f69
DW
352 case PERCENT_PVS:
353 pv_extent_count = pv_list_extents_free(pvh);
354 lp->extents = lp->extents * pv_extent_count / 100;
355 break;
34fadac4
AK
356 case PERCENT_NONE:
357 break;
358 }
359
241913fe
AK
360 if (lp->sign == SIGN_PLUS)
361 lp->extents += lv->le_count;
03a8a07d 362
241913fe
AK
363 if (lp->sign == SIGN_MINUS) {
364 if (lp->extents >= lv->le_count) {
03a8a07d 365 log_error("Unable to reduce %s below 1 extent",
241913fe 366 lp->lv_name);
7f0dc9c4 367 return EINVALID_CMD_LINE;
03a8a07d
AK
368 }
369
241913fe 370 lp->extents = lv->le_count - lp->extents;
03a8a07d
AK
371 }
372
241913fe 373 if (!lp->extents) {
03a8a07d 374 log_error("New size of 0 not permitted");
7f0dc9c4 375 return EINVALID_CMD_LINE;
03a8a07d
AK
376 }
377
241913fe 378 if (lp->extents == lv->le_count) {
03a8a07d 379 log_error("New size (%d extents) matches existing size "
241913fe 380 "(%d extents)", lp->extents, lv->le_count);
7f0dc9c4 381 return EINVALID_CMD_LINE;
03a8a07d
AK
382 }
383
241913fe 384 seg_size = lp->extents - lv->le_count;
25b73380 385
1832f310
AK
386 /* Use segment type of last segment */
387 list_iterate_items(seg, &lv->segments) {
241913fe 388 lp->segtype = seg->segtype;
1832f310
AK
389 }
390
391 /* FIXME Support LVs with mixed segment types */
72b2cb61 392 if (lp->segtype != arg_ptr_value(cmd, type_ARG, lp->segtype)) {
241913fe 393 log_error("VolumeType does not match (%s)", lp->segtype->name);
7f0dc9c4 394 return EINVALID_CMD_LINE;
1832f310
AK
395 }
396
52dc2139 397 /* If extending, find stripes, stripesize & size of last segment */
241913fe
AK
398 if ((lp->extents > lv->le_count) &&
399 !(lp->stripes == 1 || (lp->stripes > 1 && lp->stripe_size))) {
f2b7349e 400 list_iterate_items(seg, &lv->segments) {
32469fb2 401 if (!seg_is_striped(seg))
1832f310 402 continue;
b8c919b4 403
579944d3 404 sz = seg->stripe_size;
b8c919b4 405 str = seg->area_count;
52dc2139 406
da4e57f2 407 if ((seg_stripesize && seg_stripesize != sz
241913fe
AK
408 && !lp->stripe_size) ||
409 (seg_stripes && seg_stripes != str && !lp->stripes)) {
52dc2139
AK
410 log_error("Please specify number of "
411 "stripes (-i) and stripesize (-I)");
7f0dc9c4 412 return EINVALID_CMD_LINE;
52dc2139
AK
413 }
414
415 seg_stripesize = sz;
416 seg_stripes = str;
417 }
418
241913fe
AK
419 if (!lp->stripes)
420 lp->stripes = seg_stripes;
52dc2139 421
241913fe 422 if (!lp->stripe_size && lp->stripes > 1) {
25b73380 423 if (seg_stripesize) {
12de747d 424 log_print("Using stripesize of last segment %s",
72b2cb61 425 display_size(cmd, (uint64_t) seg_stripesize));
241913fe 426 lp->stripe_size = seg_stripesize;
25b73380 427 } else {
7f0dc9c4 428 lp->stripe_size =
2293567c 429 find_config_tree_int(cmd,
8ef2b021 430 "metadata/stripesize",
8ef2b021 431 DEFAULT_STRIPESIZE) * 2;
12de747d 432 log_print("Using default stripesize %s",
72b2cb61 433 display_size(cmd, (uint64_t) lp->stripe_size));
25b73380
AK
434 }
435 }
52dc2139
AK
436 }
437
ffb0e538
AK
438 /* If extending, find mirrors of last segment */
439 if ((lp->extents > lv->le_count)) {
440 list_iterate_back_items(seg, &lv->segments) {
441 if (seg_is_mirrored(seg))
442 seg_mirrors = seg->area_count;
443 else
444 seg_mirrors = 0;
445 break;
446 }
447 if (!arg_count(cmd, mirrors_ARG) && seg_mirrors) {
448 log_print("Extending %" PRIu32 " mirror images.",
449 seg_mirrors);
450 lp->mirrors = seg_mirrors;
451 }
452 if ((arg_count(cmd, mirrors_ARG) || seg_mirrors) &&
453 (lp->mirrors != seg_mirrors)) {
454 log_error("Cannot vary number of mirrors in LV yet.");
455 return EINVALID_CMD_LINE;
456 }
457 }
458
52dc2139 459 /* If reducing, find stripes, stripesize & size of last segment */
241913fe 460 if (lp->extents < lv->le_count) {
25b73380 461 extents_used = 0;
52dc2139 462
ffb0e538
AK
463 if (lp->stripes || lp->stripe_size || lp->mirrors)
464 log_error("Ignoring stripes, stripesize and mirrors "
465 "arguments when reducing");
52dc2139 466
f2b7349e 467 list_iterate_items(seg, &lv->segments) {
579944d3
AK
468 seg_extents = seg->len;
469
32469fb2 470 if (seg_is_striped(seg)) {
b8c919b4
AK
471 seg_stripesize = seg->stripe_size;
472 seg_stripes = seg->area_count;
473 }
52dc2139 474
ffb0e538
AK
475 if (seg_is_mirrored(seg))
476 seg_mirrors = seg->area_count;
477 else
478 seg_mirrors = 0;
479
241913fe 480 if (lp->extents <= extents_used + seg_extents)
52dc2139
AK
481 break;
482
483 extents_used += seg_extents;
484 }
485
241913fe
AK
486 seg_size = lp->extents - extents_used;
487 lp->stripe_size = seg_stripesize;
488 lp->stripes = seg_stripes;
ffb0e538 489 lp->mirrors = seg_mirrors;
52dc2139
AK
490 }
491
241913fe 492 if (lp->stripes > 1 && !lp->stripe_size) {
25b73380 493 log_error("Stripesize for striped segment should not be 0!");
7f0dc9c4 494 return EINVALID_CMD_LINE;
d4e5f63e 495 }
5a52dca9 496
241913fe
AK
497 if ((lp->stripes > 1)) {
498 if (!(stripesize_extents = lp->stripe_size / vg->extent_size))
d4e5f63e
AK
499 stripesize_extents = 1;
500
241913fe 501 if ((size_rest = seg_size % (lp->stripes * stripesize_extents))) {
d4e5f63e
AK
502 log_print("Rounding size (%d extents) down to stripe "
503 "boundary size for segment (%d extents)",
241913fe
AK
504 lp->extents, lp->extents - size_rest);
505 lp->extents = lp->extents - size_rest;
d4e5f63e 506 }
12de747d
AK
507
508 if (lp->stripe_size < STRIPE_SIZE_MIN) {
509 log_error("Invalid stripe size %s",
72b2cb61 510 display_size(cmd, (uint64_t) lp->stripe_size));
406a9754 511 return EINVALID_CMD_LINE;
12de747d 512 }
da4e57f2 513 }
52dc2139 514
241913fe 515 if (lp->extents == lv->le_count) {
52dc2139 516 log_error("New size (%d extents) matches existing size "
241913fe 517 "(%d extents)", lp->extents, lv->le_count);
7f0dc9c4 518 return EINVALID_CMD_LINE;
52dc2139
AK
519 }
520
241913fe
AK
521 if (lp->extents < lv->le_count) {
522 if (lp->resize == LV_EXTEND) {
03a8a07d
AK
523 log_error("New size given (%d extents) not larger "
524 "than existing size (%d extents)",
241913fe 525 lp->extents, lv->le_count);
7f0dc9c4 526 return EINVALID_CMD_LINE;
03a8a07d 527 } else
241913fe 528 lp->resize = LV_REDUCE;
03a8a07d
AK
529 }
530
241913fe
AK
531 if (lp->extents > lv->le_count) {
532 if (lp->resize == LV_REDUCE) {
03a8a07d 533 log_error("New size given (%d extents) not less than "
241913fe 534 "existing size (%d extents)", lp->extents,
03a8a07d 535 lv->le_count);
7f0dc9c4 536 return EINVALID_CMD_LINE;
03a8a07d 537 } else
241913fe 538 lp->resize = LV_EXTEND;
03a8a07d
AK
539 }
540
f5190ed4 541 if (lp->mirrors && activation() &&
f894b4b1 542 lv_info(cmd, lv, &info, 0) && info.exists) {
f5190ed4
AK
543 log_error("Mirrors cannot be resized while active yet.");
544 return ECMD_FAILED;
545 }
864de9ce
AK
546
547 if (lv_is_origin(lv)) {
548 if (lp->resize == LV_REDUCE) {
549 log_error("Snapshot origin volumes cannot be reduced "
550 "in size yet.");
551 return ECMD_FAILED;
552 }
553
554 memset(&info, 0, sizeof(info));
555
f894b4b1 556 if (lv_info(cmd, lv, &info, 0) && info.exists) {
864de9ce
AK
557 log_error("Snapshot origin volumes can be resized "
558 "only while inactive: try lvchange -an");
559 return ECMD_FAILED;
560 }
561 }
562
241913fe
AK
563 if (lp->resize == LV_REDUCE) {
564 if (lp->argc)
e7ddf416 565 log_warn("Ignoring PVs on command line when reducing");
1a9ea74d 566 }
03a8a07d 567
1a9ea74d 568 if (lp->resize == LV_REDUCE || lp->resizefs) {
406a9754 569 if (!confirm_resizefs_reduce(cmd, vg, lv, lp))
1a9ea74d 570 return ECMD_FAILED;
1a9ea74d 571 }
03a8a07d 572
1a9ea74d 573 if (lp->resizefs) {
1102c4ee 574 if (!do_resizefs_reduce(cmd, vg, lp))
406a9754 575 return ECMD_FAILED;
1a9ea74d 576 }
614a4508 577
1a9ea74d
AK
578 if (!archive(vg)) {
579 stack;
580 return ECMD_FAILED;
581 }
03a8a07d 582
1a9ea74d
AK
583 log_print("%sing logical volume %s to %s",
584 (lp->resize == LV_REDUCE) ? "Reduc" : "Extend",
585 lp->lv_name,
72b2cb61 586 display_size(cmd, (uint64_t) lp->extents * vg->extent_size));
1a9ea74d
AK
587
588 if (lp->resize == LV_REDUCE) {
32469fb2 589 if (!lv_reduce(lv, lv->le_count - lp->extents)) {
7f0dc9c4
AK
590 stack;
591 return ECMD_FAILED;
241913fe 592 }
32469fb2 593 } else if (!lv_extend(lv, lp->segtype, lp->stripes,
ffb0e538 594 lp->stripe_size, lp->mirrors,
32469fb2
AK
595 lp->extents - lv->le_count,
596 NULL, 0u, 0u, pvh, alloc)) {
1a9ea74d
AK
597 stack;
598 return ECMD_FAILED;
03a8a07d
AK
599 }
600
03a8a07d 601 /* store vg on disk(s) */
25b73380 602 if (!vg_write(vg)) {
914c9723 603 stack;
7f0dc9c4 604 return ECMD_FAILED;
cc8b2cd7 605 }
03a8a07d 606
6fda126d 607 backup(vg);
cc219483 608
914c9723 609 /* If snapshot, must suspend all associated devices */
2cd0aa72
AK
610 if (lv_is_cow(lv))
611 lock_lv = origin_from_cow(lv);
914c9723 612 else
0fb173aa 613 lock_lv = lv;
914c9723 614
0fb173aa 615 if (!suspend_lv(cmd, lock_lv)) {
241913fe 616 log_error("Failed to suspend %s", lp->lv_name);
914c9723 617 vg_revert(vg);
7f0dc9c4 618 return ECMD_FAILED;
914c9723
AK
619 }
620
621 if (!vg_commit(vg)) {
622 stack;
0fb173aa 623 resume_lv(cmd, lock_lv);
7f0dc9c4 624 return ECMD_FAILED;
914c9723
AK
625 }
626
0fb173aa 627 if (!resume_lv(cmd, lock_lv)) {
241913fe 628 log_error("Problem reactivating %s", lp->lv_name);
7f0dc9c4 629 return ECMD_FAILED;
cc8b2cd7 630 }
03a8a07d 631
241913fe 632 log_print("Logical volume %s successfully resized", lp->lv_name);
03a8a07d 633
1a9ea74d
AK
634 if (lp->resizefs && (lp->resize == LV_EXTEND)) {
635 if (!exec_cmd("fsadm", "resize", lv_path, size_buf)) {
636 stack;
637 return ECMD_FAILED;
638 }
639 }
640
cfb7bfc7 641 return ECMD_PROCESSED;
03a8a07d 642}
241913fe
AK
643
644int lvresize(struct cmd_context *cmd, int argc, char **argv)
645{
646 struct lvresize_params lp;
647 int r;
648
649 memset(&lp, 0, sizeof(lp));
650
8a2fc586 651 if (!_lvresize_params(cmd, argc, argv, &lp))
241913fe
AK
652 return EINVALID_CMD_LINE;
653
654 log_verbose("Finding volume group %s", lp.vg_name);
655 if (!lock_vol(cmd, lp.vg_name, LCK_VG_WRITE)) {
656 log_error("Can't get lock for %s", lp.vg_name);
657 return ECMD_FAILED;
658 }
659
660 if (!(r = _lvresize(cmd, &lp)))
661 stack;
662
663 unlock_vg(cmd, lp.vg_name);
664
665 return r;
666}
This page took 0.135065 seconds and 5 git commands to generate.