]> sourceware.org Git - lvm2.git/blame - tools/lvresize.c
Remove 'up' from rounding message that sometimes rounds down.
[lvm2.git] / tools / lvresize.c
CommitLineData
03a8a07d 1/*
1832f310 2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
63368983 3 * Copyright (C) 2004-2012 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;
8191fe4f 34 percent_type_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
bc175d28
AK
49static int _validate_stripesize(struct cmd_context *cmd,
50 const struct volume_group *vg,
51 struct lvresize_params *lp)
406a9754 52{
fbf6b89a 53 if (arg_sign_value(cmd, stripesize_ARG, SIGN_NONE) == SIGN_MINUS) {
406a9754
DW
54 log_error("Stripesize may not be negative.");
55 return 0;
56 }
57
301c2b88 58 if (arg_uint64_value(cmd, stripesize_ARG, 0) > STRIPE_SIZE_LIMIT * 2) {
406a9754
DW
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.");
aeaec150 66 else if (arg_uint_value(cmd, stripesize_ARG, 0) > (uint64_t) vg->extent_size * 2) {
406a9754
DW
67 log_error("Reducing stripe size %s to maximum, "
68 "physical extent size %s",
69 display_size(cmd,
204a12e5 70 (uint64_t) arg_uint_value(cmd, stripesize_ARG, 0)),
406a9754
DW
71 display_size(cmd, (uint64_t) vg->extent_size));
72 lp->stripe_size = vg->extent_size;
73 } else
204a12e5 74 lp->stripe_size = arg_uint_value(cmd, stripesize_ARG, 0);
406a9754 75
406a9754
DW
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
bc175d28
AK
84static 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)
406a9754
DW
88{
89 struct lvinfo info;
90
91 memset(&info, 0, sizeof(info));
92
2d6fcbf6 93 if (!lv_info(cmd, lv, 0, &info, 1, 0) && driver_version(NULL, 0)) {
406a9754
DW
94 log_error("lv_info failed: aborting");
95 return 0;
96 }
97
bc175d28
AK
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;
406a9754
DW
105 }
106
bc175d28
AK
107 if (!info.exists)
108 return 1;
406a9754 109
bc175d28
AK
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));
406a9754 113
bc175d28
AK
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') {
fec4de95 119 log_error("Logical volume %s NOT reduced", lp->lv_name);
bc175d28 120 return 0;
406a9754 121 }
bc175d28
AK
122 if (sigint_caught())
123 return 0;
406a9754
DW
124 }
125
126 return 1;
127}
128
c8669f6b 129enum fsadm_cmd_e { FSADM_CMD_CHECK, FSADM_CMD_RESIZE };
bc175d28
AK
130#define FSADM_CMD "fsadm"
131#define FSADM_CMD_MAX_ARGS 6
2955b913 132#define FSADM_CHECK_FAILS_FOR_MOUNTED 3 /* shell exist status code */
c8669f6b 133
bc175d28
AK
134/*
135 * FSADM_CMD --dry-run --verbose --force check lv_path
136 * FSADM_CMD --dry-run --verbose --force resize lv_path size
137 */
ed82bfd2 138static int _fsadm_cmd(struct cmd_context *cmd,
bc175d28
AK
139 const struct volume_group *vg,
140 const struct lvresize_params *lp,
2955b913
ZK
141 enum fsadm_cmd_e fcmd,
142 int *status)
406a9754
DW
143{
144 char lv_path[PATH_MAX];
145 char size_buf[SIZE_BUF];
bc175d28
AK
146 const char *argv[FSADM_CMD_MAX_ARGS + 2];
147 unsigned i = 0;
c8669f6b 148
bc175d28 149 argv[i++] = FSADM_CMD;
c8669f6b
ZK
150
151 if (test_mode())
152 argv[i++] = "--dry-run";
153
bc175d28 154 if (verbose_level() >= _LOG_NOTICE)
c8669f6b
ZK
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";
406a9754 161
cd4c26a2
ZK
162 if (status)
163 *status = -1;
164
bc175d28
AK
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);
406a9754
DW
168 return 0;
169 }
170
c8669f6b 171 argv[i++] = lv_path;
406a9754 172
c8669f6b
ZK
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 }
406a9754 179
c8669f6b
ZK
180 argv[i++] = size_buf;
181 }
406a9754 182
c8669f6b
ZK
183 argv[i] = NULL;
184
b1b38215 185 return exec_cmd(cmd, argv, status, 1);
406a9754
DW
186}
187
8a2fc586
AK
188static int _lvresize_params(struct cmd_context *cmd, int argc, char **argv,
189 struct lvresize_params *lp)
241913fe
AK
190{
191 const char *cmd_name;
192 char *st;
17dd04ca 193 unsigned dev_dir_found = 0;
a341cab7 194 int use_policy = arg_count(cmd, use_policies_ARG);
241913fe
AK
195
196 lp->sign = SIGN_NONE;
197 lp->resize = LV_ANY;
03a8a07d 198
60274aba 199 cmd_name = command_name(cmd);
03a8a07d 200 if (!strcmp(cmd_name, "lvreduce"))
241913fe 201 lp->resize = LV_REDUCE;
03a8a07d 202 if (!strcmp(cmd_name, "lvextend"))
241913fe 203 lp->resize = LV_EXTEND;
03a8a07d 204
a341cab7
PR
205 if (use_policy) {
206 /* do nothing; _lvresize will handle --use-policies itself */
207 lp->extents = 0;
dfef7f69 208 lp->sign = SIGN_PLUS;
a341cab7
PR
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 }
03a8a07d 227
a341cab7
PR
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 }
03a8a07d 233
a341cab7
PR
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 }
03a8a07d
AK
240 }
241
241913fe 242 if (lp->resize == LV_EXTEND && lp->sign == SIGN_MINUS) {
03a8a07d 243 log_error("Negative argument not permitted - use lvreduce");
241913fe 244 return 0;
03a8a07d
AK
245 }
246
241913fe 247 if (lp->resize == LV_REDUCE && lp->sign == SIGN_PLUS) {
03a8a07d 248 log_error("Positive sign not permitted - use lvextend");
241913fe 249 return 0;
03a8a07d
AK
250 }
251
a8fb89ad
AK
252 lp->resizefs = arg_is_set(cmd, resizefs_ARG);
253 lp->nofsck = arg_is_set(cmd, nofsck_ARG);
1a9ea74d 254
03a8a07d
AK
255 if (!argc) {
256 log_error("Please provide the logical volume name");
241913fe 257 return 0;
03a8a07d
AK
258 }
259
241913fe 260 lp->lv_name = argv[0];
03a8a07d
AK
261 argv++;
262 argc--;
263
3a370b73
AK
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))) {
03a8a07d 266 log_error("Please provide a volume group name");
241913fe 267 return 0;
03a8a07d 268 }
17dd04ca 269
d38bf361
AK
270 if (!validate_name(lp->vg_name)) {
271 log_error("Volume group name %s has invalid characters",
272 lp->vg_name);
6c3dc203 273 return 0;
d38bf361 274 }
03a8a07d 275
241913fe
AK
276 if ((st = strrchr(lp->lv_name, '/')))
277 lp->lv_name = st + 1;
03a8a07d 278
241913fe
AK
279 lp->argc = argc;
280 lp->argv = argv;
281
282 return 1;
283}
7d0e6e80 284
a341cab7
PR
285static int _adjust_policy_params(struct cmd_context *cmd,
286 struct logical_volume *lv, struct lvresize_params *lp)
287{
8191fe4f 288 percent_t percent;
a341cab7
PR
289 int policy_threshold, policy_amount;
290
0e0f706f
ZK
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 }
a341cab7 306
8191fe4f 307 if (policy_threshold >= PERCENT_100)
a341cab7
PR
308 return 1; /* nothing to do */
309
0e0f706f 310 if (lv_is_thin_pool(lv)) {
63368983
ZK
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))
0e0f706f
ZK
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 }
a341cab7
PR
331
332 lp->extents = policy_amount;
0e0f706f 333
a341cab7
PR
334 return 1;
335}
336
f4ba9c5d
MB
337static 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
e5f7352b
AK
365static int _lvresize(struct cmd_context *cmd, struct volume_group *vg,
366 struct lvresize_params *lp)
241913fe 367{
241913fe 368 struct logical_volume *lv;
241913fe 369 struct lvinfo info;
9bdff1ee
ZK
370 uint32_t stripesize_extents;
371 uint32_t seg_stripes = 0, seg_stripesize = 0, seg_size;
ffb0e538 372 uint32_t seg_mirrors = 0;
9bdff1ee 373 uint32_t extents_used;
241913fe 374 uint32_t size_rest;
9bdff1ee 375 uint32_t pv_extent_count;
a0a23eff 376 alloc_policy_t alloc;
0fb173aa 377 struct logical_volume *lock_lv;
241913fe 378 struct lv_list *lvl;
1485ce69 379 struct lv_segment *seg, *uninitialized_var(mirr_seg);
241913fe
AK
380 uint32_t seg_extents;
381 uint32_t sz, str;
2955b913 382 int status;
2c44337b 383 struct dm_list *pvh = NULL;
a341cab7 384 int use_policy = arg_count(cmd, use_policies_ARG);
241913fe 385
03a8a07d 386 /* does LV exist? */
241913fe 387 if (!(lvl = find_lv_in_vg(vg, lp->lv_name))) {
03a8a07d 388 log_error("Logical volume %s not found in volume group %s",
241913fe 389 lp->lv_name, lp->vg_name);
7f0dc9c4 390 return ECMD_FAILED;
03a8a07d
AK
391 }
392
d34991ed
JEB
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
25b73380
AK
406 if (arg_count(cmd, stripes_ARG)) {
407 if (vg->fid->fmt->features & FMT_SEGMENTS)
241913fe 408 lp->stripes = arg_uint_value(cmd, stripes_ARG, 1);
25b73380 409 else
e7ddf416 410 log_warn("Varied striping not supported. Ignoring.");
25b73380
AK
411 }
412
ffb0e538
AK
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
e7ddf416 417 log_warn("Mirrors not supported. Ignoring.");
fbf6b89a 418 if (arg_sign_value(cmd, mirrors_ARG, SIGN_NONE) == SIGN_MINUS) {
7424de5b 419 log_error("Mirrors argument may not be negative");
406a9754 420 return EINVALID_CMD_LINE;
7424de5b 421 }
ffb0e538
AK
422 }
423
bc175d28
AK
424 if (arg_count(cmd, stripesize_ARG) &&
425 !_validate_stripesize(cmd, vg, lp))
426 return EINVALID_CMD_LINE;
25b73380 427
f868d635 428 lv = lvl->lv;
03a8a07d 429
a341cab7 430 if (use_policy) {
0e0f706f
ZK
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.");
a341cab7
PR
434 return ECMD_FAILED;
435 }
7afa7b07
ZK
436 if (!_adjust_policy_params(cmd, lv, lp))
437 return ECMD_FAILED;
a341cab7
PR
438 }
439
fd817ff3
MS
440 if (!lv_is_visible(lv)) {
441 log_error("Can't resize internal logical volume %s", lv->name);
442 return ECMD_FAILED;
443 }
444
6e03b44c
AK
445 if (lv->status & LOCKED) {
446 log_error("Can't resize locked LV %s", lv->name);
7f0dc9c4 447 return ECMD_FAILED;
6e03b44c
AK
448 }
449
08b481bb
AK
450 if (lv->status & CONVERTING) {
451 log_error("Can't resize %s while lvconvert in progress", lv->name);
452 return ECMD_FAILED;
453 }
454
fbf6b89a 455 alloc = (alloc_policy_t) arg_uint_value(cmd, alloc_ARG, lv->alloc);
7f0dc9c4 456
e38e9e58
AK
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 */
241913fe
AK
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;
03a8a07d 467 else
241913fe
AK
468 lp->size += vg->extent_size -
469 (lp->size % vg->extent_size);
03a8a07d 470
e38e9e58 471 log_print("Rounding size to boundary between physical extents: %s",
aeaec150 472 display_size(cmd, lp->size));
03a8a07d
AK
473 }
474
241913fe 475 lp->extents = lp->size / vg->extent_size;
03a8a07d
AK
476 }
477
dfef7f69
DW
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
34fadac4
AK
484 switch(lp->percent) {
485 case PERCENT_VG:
4fbde014
ZK
486 lp->extents = percent_of_extents(lp->extents, vg->extent_count,
487 (lp->sign != SIGN_MINUS));
34fadac4
AK
488 break;
489 case PERCENT_FREE:
4fbde014
ZK
490 lp->extents = percent_of_extents(lp->extents, vg->free_count,
491 (lp->sign != SIGN_MINUS));
34fadac4
AK
492 break;
493 case PERCENT_LV:
4fbde014
ZK
494 lp->extents = percent_of_extents(lp->extents, lv->le_count,
495 (lp->sign != SIGN_MINUS));
34fadac4 496 break;
dfef7f69 497 case PERCENT_PVS:
ba3851fd
MB
498 if (lp->argc) {
499 pv_extent_count = pv_list_extents_free(pvh);
4fbde014
ZK
500 lp->extents = percent_of_extents(lp->extents, pv_extent_count,
501 (lp->sign != SIGN_MINUS));
ba3851fd 502 } else
4fbde014
ZK
503 lp->extents = percent_of_extents(lp->extents, vg->extent_count,
504 (lp->sign != SIGN_MINUS));
dfef7f69 505 break;
5bc2af26
MS
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 }
4fbde014
ZK
511 lp->extents = percent_of_extents(lp->extents, origin_from_cow(lv)->le_count,
512 (lp->sign != SIGN_MINUS));
5bc2af26 513 break;
34fadac4
AK
514 case PERCENT_NONE:
515 break;
516 }
517
4079a8f2
ZK
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 }
241913fe 524 lp->extents += lv->le_count;
4079a8f2 525 }
03a8a07d 526
241913fe
AK
527 if (lp->sign == SIGN_MINUS) {
528 if (lp->extents >= lv->le_count) {
03a8a07d 529 log_error("Unable to reduce %s below 1 extent",
241913fe 530 lp->lv_name);
7f0dc9c4 531 return EINVALID_CMD_LINE;
03a8a07d
AK
532 }
533
241913fe 534 lp->extents = lv->le_count - lp->extents;
03a8a07d
AK
535 }
536
241913fe 537 if (!lp->extents) {
03a8a07d 538 log_error("New size of 0 not permitted");
7f0dc9c4 539 return EINVALID_CMD_LINE;
03a8a07d
AK
540 }
541
241913fe 542 if (lp->extents == lv->le_count) {
a341cab7
PR
543 if (use_policy)
544 return ECMD_PROCESSED; /* Nothing to do. */
c8669f6b
ZK
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 */
03a8a07d
AK
551 }
552
241913fe 553 seg_size = lp->extents - lv->le_count;
25b73380 554
1832f310 555 /* Use segment type of last segment */
2c44337b 556 dm_list_iterate_items(seg, &lv->segments) {
241913fe 557 lp->segtype = seg->segtype;
1832f310
AK
558 }
559
560 /* FIXME Support LVs with mixed segment types */
54d7741a
AK
561 if (lp->segtype != get_segtype_from_string(cmd, arg_str_value(cmd, type_ARG,
562 lp->segtype->name))) {
241913fe 563 log_error("VolumeType does not match (%s)", lp->segtype->name);
7f0dc9c4 564 return EINVALID_CMD_LINE;
1832f310
AK
565 }
566
1485ce69
AK
567 /* If extending, find mirrors of last segment */
568 if ((lp->extents > lv->le_count)) {
a80192b6
JEB
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
1485ce69
AK
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
52dc2139 595 /* If extending, find stripes, stripesize & size of last segment */
241913fe
AK
596 if ((lp->extents > lv->le_count) &&
597 !(lp->stripes == 1 || (lp->stripes > 1 && lp->stripe_size))) {
1485ce69 598 /* FIXME Don't assume mirror seg will always be AREA_LV */
5dc27b75
ZK
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) {
32469fb2 603 if (!seg_is_striped(seg))
1832f310 604 continue;
b8c919b4 605
579944d3 606 sz = seg->stripe_size;
b8c919b4 607 str = seg->area_count;
52dc2139 608
bc175d28 609 if ((seg_stripesize && seg_stripesize != sz &&
1485ce69 610 sz && !lp->stripe_size) ||
241913fe 611 (seg_stripes && seg_stripes != str && !lp->stripes)) {
52dc2139
AK
612 log_error("Please specify number of "
613 "stripes (-i) and stripesize (-I)");
7f0dc9c4 614 return EINVALID_CMD_LINE;
52dc2139
AK
615 }
616
617 seg_stripesize = sz;
618 seg_stripes = str;
619 }
620
241913fe
AK
621 if (!lp->stripes)
622 lp->stripes = seg_stripes;
52dc2139 623
241913fe 624 if (!lp->stripe_size && lp->stripes > 1) {
25b73380 625 if (seg_stripesize) {
12de747d 626 log_print("Using stripesize of last segment %s",
72b2cb61 627 display_size(cmd, (uint64_t) seg_stripesize));
241913fe 628 lp->stripe_size = seg_stripesize;
25b73380 629 } else {
7f0dc9c4 630 lp->stripe_size =
2293567c 631 find_config_tree_int(cmd,
8ef2b021 632 "metadata/stripesize",
8ef2b021 633 DEFAULT_STRIPESIZE) * 2;
12de747d 634 log_print("Using default stripesize %s",
72b2cb61 635 display_size(cmd, (uint64_t) lp->stripe_size));
25b73380
AK
636 }
637 }
52dc2139
AK
638 }
639
640 /* If reducing, find stripes, stripesize & size of last segment */
241913fe 641 if (lp->extents < lv->le_count) {
25b73380 642 extents_used = 0;
52dc2139 643
ffb0e538
AK
644 if (lp->stripes || lp->stripe_size || lp->mirrors)
645 log_error("Ignoring stripes, stripesize and mirrors "
646 "arguments when reducing");
52dc2139 647
2c44337b 648 dm_list_iterate_items(seg, &lv->segments) {
579944d3
AK
649 seg_extents = seg->len;
650
f4ba9c5d
MB
651 /* Check for underlying stripe sizes */
652 seg_stripes = lvseg_get_stripes(seg, &seg_stripesize);
52dc2139 653
ffb0e538 654 if (seg_is_mirrored(seg))
31e9db26 655 seg_mirrors = lv_mirror_count(seg->lv);
ffb0e538
AK
656 else
657 seg_mirrors = 0;
658
241913fe 659 if (lp->extents <= extents_used + seg_extents)
52dc2139
AK
660 break;
661
662 extents_used += seg_extents;
663 }
664
241913fe
AK
665 seg_size = lp->extents - extents_used;
666 lp->stripe_size = seg_stripesize;
667 lp->stripes = seg_stripes;
ffb0e538 668 lp->mirrors = seg_mirrors;
52dc2139
AK
669 }
670
241913fe 671 if (lp->stripes > 1 && !lp->stripe_size) {
25b73380 672 log_error("Stripesize for striped segment should not be 0!");
7f0dc9c4 673 return EINVALID_CMD_LINE;
d4e5f63e 674 }
5a52dca9 675
851b1a96 676 if (lp->stripes > 1) {
9bdff1ee
ZK
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
241913fe 683 if (!(stripesize_extents = lp->stripe_size / vg->extent_size))
d4e5f63e
AK
684 stripesize_extents = 1;
685
851b1a96 686 size_rest = seg_size % (lp->stripes * stripesize_extents);
44dfb51f 687 /* Round toward the original size. */
6fc1f948
ZK
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))))) {
851b1a96
MB
693 log_print("Rounding size (%d extents) up to stripe "
694 "boundary size for segment (%d extents)",
6f14cd22
MB
695 lp->extents, lp->extents - size_rest +
696 (lp->stripes * stripesize_extents));
697 lp->extents = lp->extents - size_rest +
698 (lp->stripes * stripesize_extents);
851b1a96 699 } else if (size_rest) {
d4e5f63e
AK
700 log_print("Rounding size (%d extents) down to stripe "
701 "boundary size for segment (%d extents)",
241913fe
AK
702 lp->extents, lp->extents - size_rest);
703 lp->extents = lp->extents - size_rest;
d4e5f63e 704 }
da4e57f2 705 }
52dc2139 706
241913fe
AK
707 if (lp->extents < lv->le_count) {
708 if (lp->resize == LV_EXTEND) {
03a8a07d
AK
709 log_error("New size given (%d extents) not larger "
710 "than existing size (%d extents)",
241913fe 711 lp->extents, lv->le_count);
7f0dc9c4 712 return EINVALID_CMD_LINE;
c8669f6b
ZK
713 }
714 lp->resize = LV_REDUCE;
715 } else if (lp->extents > lv->le_count) {
241913fe 716 if (lp->resize == LV_REDUCE) {
03a8a07d 717 log_error("New size given (%d extents) not less than "
241913fe 718 "existing size (%d extents)", lp->extents,
03a8a07d 719 lv->le_count);
7f0dc9c4 720 return EINVALID_CMD_LINE;
c8669f6b
ZK
721 }
722 lp->resize = LV_EXTEND;
e38e9e58
AK
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;
03a8a07d
AK
732 }
733
864de9ce
AK
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
2d6fcbf6 743 if (lv_info(cmd, lv, 0, &info, 0, 0) && info.exists) {
864de9ce
AK
744 log_error("Snapshot origin volumes can be resized "
745 "only while inactive: try lvchange -an");
746 return ECMD_FAILED;
747 }
748 }
749
daa10ad0
ZK
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 }
daa10ad0
ZK
760 }
761
bc175d28
AK
762 if ((lp->resize == LV_REDUCE) && lp->argc)
763 log_warn("Ignoring PVs on command line when reducing");
03a8a07d 764
bc175d28
AK
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;
2b238642 769 return ECMD_FAILED;
bc175d28 770 }
03a8a07d 771
1a9ea74d 772 if (lp->resizefs) {
bc175d28 773 if (!lp->nofsck &&
2955b913
ZK
774 !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_CHECK, &status)) {
775 if (status != FSADM_CHECK_FAILS_FOR_MOUNTED) {
da1350d4 776 log_error("Filesystem check failed.");
2955b913
ZK
777 return ECMD_FAILED;
778 }
da1350d4 779 /* some filesystems supports online resize */
c8669f6b
ZK
780 }
781
bc175d28 782 if ((lp->resize == LV_REDUCE) &&
2955b913 783 !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_RESIZE, NULL)) {
da1350d4 784 log_error("Filesystem resize failed.");
c8669f6b
ZK
785 return ECMD_FAILED;
786 }
1a9ea74d 787 }
614a4508 788
1a9ea74d
AK
789 if (!archive(vg)) {
790 stack;
791 return ECMD_FAILED;
792 }
03a8a07d 793
1a9ea74d
AK
794 log_print("%sing logical volume %s to %s",
795 (lp->resize == LV_REDUCE) ? "Reduc" : "Extend",
796 lp->lv_name,
72b2cb61 797 display_size(cmd, (uint64_t) lp->extents * vg->extent_size));
1a9ea74d
AK
798
799 if (lp->resize == LV_REDUCE) {
32469fb2 800 if (!lv_reduce(lv, lv->le_count - lp->extents)) {
7f0dc9c4
AK
801 stack;
802 return ECMD_FAILED;
241913fe 803 }
bc175d28 804 } else if ((lp->extents > lv->le_count) && /* Ensure we extend */
fe93c99a
JEB
805 !lv_extend(lv, lp->segtype,
806 lp->stripes, lp->stripe_size,
807 lp->mirrors, first_seg(lv)->region_size,
9ac61d2b 808 lp->extents - lv->le_count, NULL,
fe93c99a 809 pvh, alloc)) {
1a9ea74d
AK
810 stack;
811 return ECMD_FAILED;
03a8a07d
AK
812 }
813
03a8a07d 814 /* store vg on disk(s) */
25b73380 815 if (!vg_write(vg)) {
914c9723 816 stack;
7f0dc9c4 817 return ECMD_FAILED;
cc8b2cd7 818 }
03a8a07d 819
914c9723 820 /* If snapshot, must suspend all associated devices */
2cd0aa72
AK
821 if (lv_is_cow(lv))
822 lock_lv = origin_from_cow(lv);
914c9723 823 else
0fb173aa 824 lock_lv = lv;
914c9723 825
0fb173aa 826 if (!suspend_lv(cmd, lock_lv)) {
241913fe 827 log_error("Failed to suspend %s", lp->lv_name);
914c9723 828 vg_revert(vg);
8f3fd69f 829 backup(vg);
7f0dc9c4 830 return ECMD_FAILED;
914c9723
AK
831 }
832
833 if (!vg_commit(vg)) {
834 stack;
df13cf08
MS
835 if (!resume_lv(cmd, lock_lv))
836 stack;
8f3fd69f 837 backup(vg);
7f0dc9c4 838 return ECMD_FAILED;
914c9723
AK
839 }
840
0fb173aa 841 if (!resume_lv(cmd, lock_lv)) {
241913fe 842 log_error("Problem reactivating %s", lp->lv_name);
8f3fd69f 843 backup(vg);
7f0dc9c4 844 return ECMD_FAILED;
cc8b2cd7 845 }
03a8a07d 846
8f3fd69f
MB
847 backup(vg);
848
6b58c796
ZK
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
241913fe 863 log_print("Logical volume %s successfully resized", lp->lv_name);
03a8a07d 864
bc175d28 865 if (lp->resizefs && (lp->resize == LV_EXTEND) &&
2955b913 866 !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_RESIZE, NULL)) {
c8669f6b
ZK
867 stack;
868 return ECMD_FAILED;
1a9ea74d
AK
869 }
870
cfb7bfc7 871 return ECMD_PROCESSED;
03a8a07d 872}
241913fe
AK
873
874int lvresize(struct cmd_context *cmd, int argc, char **argv)
875{
876 struct lvresize_params lp;
e5f7352b 877 struct volume_group *vg;
241913fe
AK
878 int r;
879
880 memset(&lp, 0, sizeof(lp));
881
8a2fc586 882 if (!_lvresize_params(cmd, argc, argv, &lp))
241913fe
AK
883 return EINVALID_CMD_LINE;
884
885 log_verbose("Finding volume group %s", lp.vg_name);
b8b3508c
DW
886 vg = vg_read_for_update(cmd, lp.vg_name, NULL, 0);
887 if (vg_read_error(vg)) {
077a6755 888 release_vg(vg);
d0bf2f3f 889 stack;
241913fe 890 return ECMD_FAILED;
d0bf2f3f 891 }
241913fe 892
e5f7352b 893 if (!(r = _lvresize(cmd, vg, &lp)))
241913fe
AK
894 stack;
895
077a6755 896 unlock_and_release_vg(cmd, vg, lp.vg_name);
241913fe
AK
897
898 return r;
899}
This page took 0.221943 seconds and 5 git commands to generate.