]> sourceware.org Git - lvm2.git/blame - tools/lvresize.c
Fix code that performs RAID device replacement while under snapshot.
[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
241913fe
AK
457 if (lp->size) {
458 if (lp->size % vg->extent_size) {
459 if (lp->sign == SIGN_MINUS)
460 lp->size -= lp->size % vg->extent_size;
03a8a07d 461 else
241913fe
AK
462 lp->size += vg->extent_size -
463 (lp->size % vg->extent_size);
03a8a07d
AK
464
465 log_print("Rounding up size to full physical extent %s",
aeaec150 466 display_size(cmd, lp->size));
03a8a07d
AK
467 }
468
241913fe 469 lp->extents = lp->size / vg->extent_size;
03a8a07d
AK
470 }
471
dfef7f69
DW
472 if (!(pvh = lp->argc ? create_pv_list(cmd->mem, vg, lp->argc,
473 lp->argv, 1) : &vg->pvs)) {
474 stack;
475 return ECMD_FAILED;
476 }
477
34fadac4
AK
478 switch(lp->percent) {
479 case PERCENT_VG:
4fbde014
ZK
480 lp->extents = percent_of_extents(lp->extents, vg->extent_count,
481 (lp->sign != SIGN_MINUS));
34fadac4
AK
482 break;
483 case PERCENT_FREE:
4fbde014
ZK
484 lp->extents = percent_of_extents(lp->extents, vg->free_count,
485 (lp->sign != SIGN_MINUS));
34fadac4
AK
486 break;
487 case PERCENT_LV:
4fbde014
ZK
488 lp->extents = percent_of_extents(lp->extents, lv->le_count,
489 (lp->sign != SIGN_MINUS));
34fadac4 490 break;
dfef7f69 491 case PERCENT_PVS:
ba3851fd
MB
492 if (lp->argc) {
493 pv_extent_count = pv_list_extents_free(pvh);
4fbde014
ZK
494 lp->extents = percent_of_extents(lp->extents, pv_extent_count,
495 (lp->sign != SIGN_MINUS));
ba3851fd 496 } else
4fbde014
ZK
497 lp->extents = percent_of_extents(lp->extents, vg->extent_count,
498 (lp->sign != SIGN_MINUS));
dfef7f69 499 break;
5bc2af26
MS
500 case PERCENT_ORIGIN:
501 if (!lv_is_cow(lv)) {
502 log_error("Specified LV does not have an origin LV.");
503 return EINVALID_CMD_LINE;
504 }
4fbde014
ZK
505 lp->extents = percent_of_extents(lp->extents, origin_from_cow(lv)->le_count,
506 (lp->sign != SIGN_MINUS));
5bc2af26 507 break;
34fadac4
AK
508 case PERCENT_NONE:
509 break;
510 }
511
4079a8f2
ZK
512 if (lp->sign == SIGN_PLUS) {
513 if (lp->extents >= (MAX_EXTENT_COUNT - lv->le_count)) {
514 log_error("Unable to extend %s by %u extents, exceeds limit (%u).",
515 lp->lv_name, lv->le_count, MAX_EXTENT_COUNT);
516 return EINVALID_CMD_LINE;
517 }
241913fe 518 lp->extents += lv->le_count;
4079a8f2 519 }
03a8a07d 520
241913fe
AK
521 if (lp->sign == SIGN_MINUS) {
522 if (lp->extents >= lv->le_count) {
03a8a07d 523 log_error("Unable to reduce %s below 1 extent",
241913fe 524 lp->lv_name);
7f0dc9c4 525 return EINVALID_CMD_LINE;
03a8a07d
AK
526 }
527
241913fe 528 lp->extents = lv->le_count - lp->extents;
03a8a07d
AK
529 }
530
241913fe 531 if (!lp->extents) {
03a8a07d 532 log_error("New size of 0 not permitted");
7f0dc9c4 533 return EINVALID_CMD_LINE;
03a8a07d
AK
534 }
535
241913fe 536 if (lp->extents == lv->le_count) {
a341cab7
PR
537 if (use_policy)
538 return ECMD_PROCESSED; /* Nothing to do. */
c8669f6b
ZK
539 if (!lp->resizefs) {
540 log_error("New size (%d extents) matches existing size "
541 "(%d extents)", lp->extents, lv->le_count);
542 return EINVALID_CMD_LINE;
543 }
544 lp->resize = LV_EXTEND; /* lets pretend zero size extension */
03a8a07d
AK
545 }
546
241913fe 547 seg_size = lp->extents - lv->le_count;
25b73380 548
1832f310 549 /* Use segment type of last segment */
2c44337b 550 dm_list_iterate_items(seg, &lv->segments) {
241913fe 551 lp->segtype = seg->segtype;
1832f310
AK
552 }
553
554 /* FIXME Support LVs with mixed segment types */
54d7741a
AK
555 if (lp->segtype != get_segtype_from_string(cmd, arg_str_value(cmd, type_ARG,
556 lp->segtype->name))) {
241913fe 557 log_error("VolumeType does not match (%s)", lp->segtype->name);
7f0dc9c4 558 return EINVALID_CMD_LINE;
1832f310
AK
559 }
560
1485ce69
AK
561 /* If extending, find mirrors of last segment */
562 if ((lp->extents > lv->le_count)) {
a80192b6
JEB
563 /*
564 * Has the user specified that they would like the additional
565 * extents of a mirror not to have an initial sync?
566 */
567 if (seg_is_mirrored(first_seg(lv)) && arg_count(cmd, nosync_ARG))
568 lv->status |= LV_NOTSYNCED;
569
1485ce69
AK
570 dm_list_iterate_back_items(mirr_seg, &lv->segments) {
571 if (seg_is_mirrored(mirr_seg))
572 seg_mirrors = lv_mirror_count(mirr_seg->lv);
573 else
574 seg_mirrors = 0;
575 break;
576 }
577 if (!arg_count(cmd, mirrors_ARG) && seg_mirrors) {
578 log_print("Extending %" PRIu32 " mirror images.",
579 seg_mirrors);
580 lp->mirrors = seg_mirrors;
581 }
582 if ((arg_count(cmd, mirrors_ARG) || seg_mirrors) &&
583 (lp->mirrors != seg_mirrors)) {
584 log_error("Cannot vary number of mirrors in LV yet.");
585 return EINVALID_CMD_LINE;
586 }
587 }
588
52dc2139 589 /* If extending, find stripes, stripesize & size of last segment */
241913fe
AK
590 if ((lp->extents > lv->le_count) &&
591 !(lp->stripes == 1 || (lp->stripes > 1 && lp->stripe_size))) {
1485ce69 592 /* FIXME Don't assume mirror seg will always be AREA_LV */
5dc27b75
ZK
593 /* FIXME We will need to support resize for metadata LV as well,
594 * and data LV could be any type (i.e. mirror)) */
595 dm_list_iterate_items(seg, seg_mirrors ? &seg_lv(mirr_seg, 0)->segments :
596 lv_is_thin_pool(lv) ? &seg_lv(first_seg(lv), 0)->segments : &lv->segments) {
32469fb2 597 if (!seg_is_striped(seg))
1832f310 598 continue;
b8c919b4 599
579944d3 600 sz = seg->stripe_size;
b8c919b4 601 str = seg->area_count;
52dc2139 602
bc175d28 603 if ((seg_stripesize && seg_stripesize != sz &&
1485ce69 604 sz && !lp->stripe_size) ||
241913fe 605 (seg_stripes && seg_stripes != str && !lp->stripes)) {
52dc2139
AK
606 log_error("Please specify number of "
607 "stripes (-i) and stripesize (-I)");
7f0dc9c4 608 return EINVALID_CMD_LINE;
52dc2139
AK
609 }
610
611 seg_stripesize = sz;
612 seg_stripes = str;
613 }
614
241913fe
AK
615 if (!lp->stripes)
616 lp->stripes = seg_stripes;
52dc2139 617
241913fe 618 if (!lp->stripe_size && lp->stripes > 1) {
25b73380 619 if (seg_stripesize) {
12de747d 620 log_print("Using stripesize of last segment %s",
72b2cb61 621 display_size(cmd, (uint64_t) seg_stripesize));
241913fe 622 lp->stripe_size = seg_stripesize;
25b73380 623 } else {
7f0dc9c4 624 lp->stripe_size =
2293567c 625 find_config_tree_int(cmd,
8ef2b021 626 "metadata/stripesize",
8ef2b021 627 DEFAULT_STRIPESIZE) * 2;
12de747d 628 log_print("Using default stripesize %s",
72b2cb61 629 display_size(cmd, (uint64_t) lp->stripe_size));
25b73380
AK
630 }
631 }
52dc2139
AK
632 }
633
634 /* If reducing, find stripes, stripesize & size of last segment */
241913fe 635 if (lp->extents < lv->le_count) {
25b73380 636 extents_used = 0;
52dc2139 637
ffb0e538
AK
638 if (lp->stripes || lp->stripe_size || lp->mirrors)
639 log_error("Ignoring stripes, stripesize and mirrors "
640 "arguments when reducing");
52dc2139 641
2c44337b 642 dm_list_iterate_items(seg, &lv->segments) {
579944d3
AK
643 seg_extents = seg->len;
644
f4ba9c5d
MB
645 /* Check for underlying stripe sizes */
646 seg_stripes = lvseg_get_stripes(seg, &seg_stripesize);
52dc2139 647
ffb0e538 648 if (seg_is_mirrored(seg))
31e9db26 649 seg_mirrors = lv_mirror_count(seg->lv);
ffb0e538
AK
650 else
651 seg_mirrors = 0;
652
241913fe 653 if (lp->extents <= extents_used + seg_extents)
52dc2139
AK
654 break;
655
656 extents_used += seg_extents;
657 }
658
241913fe
AK
659 seg_size = lp->extents - extents_used;
660 lp->stripe_size = seg_stripesize;
661 lp->stripes = seg_stripes;
ffb0e538 662 lp->mirrors = seg_mirrors;
52dc2139
AK
663 }
664
241913fe 665 if (lp->stripes > 1 && !lp->stripe_size) {
25b73380 666 log_error("Stripesize for striped segment should not be 0!");
7f0dc9c4 667 return EINVALID_CMD_LINE;
d4e5f63e 668 }
5a52dca9 669
851b1a96 670 if (lp->stripes > 1) {
9bdff1ee
ZK
671 if (lp->stripe_size < STRIPE_SIZE_MIN) {
672 log_error("Invalid stripe size %s",
673 display_size(cmd, (uint64_t) lp->stripe_size));
674 return EINVALID_CMD_LINE;
675 }
676
241913fe 677 if (!(stripesize_extents = lp->stripe_size / vg->extent_size))
d4e5f63e
AK
678 stripesize_extents = 1;
679
851b1a96 680 size_rest = seg_size % (lp->stripes * stripesize_extents);
44dfb51f 681 /* Round toward the original size. */
6fc1f948
ZK
682 if (size_rest &&
683 ((lp->extents < lv->le_count) ||
684 !lp->percent ||
685 (vg->free_count >= (lp->extents - lv->le_count - size_rest +
686 (lp->stripes * stripesize_extents))))) {
851b1a96
MB
687 log_print("Rounding size (%d extents) up to stripe "
688 "boundary size for segment (%d extents)",
6f14cd22
MB
689 lp->extents, lp->extents - size_rest +
690 (lp->stripes * stripesize_extents));
691 lp->extents = lp->extents - size_rest +
692 (lp->stripes * stripesize_extents);
851b1a96 693 } else if (size_rest) {
d4e5f63e
AK
694 log_print("Rounding size (%d extents) down to stripe "
695 "boundary size for segment (%d extents)",
241913fe
AK
696 lp->extents, lp->extents - size_rest);
697 lp->extents = lp->extents - size_rest;
d4e5f63e 698 }
da4e57f2 699 }
52dc2139 700
241913fe
AK
701 if (lp->extents < lv->le_count) {
702 if (lp->resize == LV_EXTEND) {
03a8a07d
AK
703 log_error("New size given (%d extents) not larger "
704 "than existing size (%d extents)",
241913fe 705 lp->extents, lv->le_count);
7f0dc9c4 706 return EINVALID_CMD_LINE;
c8669f6b
ZK
707 }
708 lp->resize = LV_REDUCE;
709 } else if (lp->extents > lv->le_count) {
241913fe 710 if (lp->resize == LV_REDUCE) {
03a8a07d 711 log_error("New size given (%d extents) not less than "
241913fe 712 "existing size (%d extents)", lp->extents,
03a8a07d 713 lv->le_count);
7f0dc9c4 714 return EINVALID_CMD_LINE;
c8669f6b
ZK
715 }
716 lp->resize = LV_EXTEND;
03a8a07d
AK
717 }
718
864de9ce
AK
719 if (lv_is_origin(lv)) {
720 if (lp->resize == LV_REDUCE) {
721 log_error("Snapshot origin volumes cannot be reduced "
722 "in size yet.");
723 return ECMD_FAILED;
724 }
725
726 memset(&info, 0, sizeof(info));
727
2d6fcbf6 728 if (lv_info(cmd, lv, 0, &info, 0, 0) && info.exists) {
864de9ce
AK
729 log_error("Snapshot origin volumes can be resized "
730 "only while inactive: try lvchange -an");
731 return ECMD_FAILED;
732 }
733 }
734
daa10ad0
ZK
735 if (lv_is_thin_pool(lv)) {
736 if (lp->resize == LV_REDUCE) {
737 log_error("Thin pool volumes cannot be reduced in size yet.");
738 return ECMD_FAILED;
739 }
740
741 if (lp->resizefs) {
742 log_warn("Thin pool volumes do not have filesystem.");
743 lp->resizefs = 0;
744 }
daa10ad0
ZK
745 }
746
bc175d28
AK
747 if ((lp->resize == LV_REDUCE) && lp->argc)
748 log_warn("Ignoring PVs on command line when reducing");
03a8a07d 749
bc175d28
AK
750 /* Request confirmation before operations that are often mistakes. */
751 if ((lp->resizefs || (lp->resize == LV_REDUCE)) &&
752 !_request_confirmation(cmd, vg, lv, lp)) {
753 stack;
2b238642 754 return ECMD_FAILED;
bc175d28 755 }
03a8a07d 756
1a9ea74d 757 if (lp->resizefs) {
bc175d28 758 if (!lp->nofsck &&
2955b913
ZK
759 !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_CHECK, &status)) {
760 if (status != FSADM_CHECK_FAILS_FOR_MOUNTED) {
da1350d4 761 log_error("Filesystem check failed.");
2955b913
ZK
762 return ECMD_FAILED;
763 }
da1350d4 764 /* some filesystems supports online resize */
c8669f6b
ZK
765 }
766
bc175d28 767 if ((lp->resize == LV_REDUCE) &&
2955b913 768 !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_RESIZE, NULL)) {
da1350d4 769 log_error("Filesystem resize failed.");
c8669f6b
ZK
770 return ECMD_FAILED;
771 }
1a9ea74d 772 }
614a4508 773
1a9ea74d
AK
774 if (!archive(vg)) {
775 stack;
776 return ECMD_FAILED;
777 }
03a8a07d 778
1a9ea74d
AK
779 log_print("%sing logical volume %s to %s",
780 (lp->resize == LV_REDUCE) ? "Reduc" : "Extend",
781 lp->lv_name,
72b2cb61 782 display_size(cmd, (uint64_t) lp->extents * vg->extent_size));
1a9ea74d
AK
783
784 if (lp->resize == LV_REDUCE) {
32469fb2 785 if (!lv_reduce(lv, lv->le_count - lp->extents)) {
7f0dc9c4
AK
786 stack;
787 return ECMD_FAILED;
241913fe 788 }
bc175d28 789 } else if ((lp->extents > lv->le_count) && /* Ensure we extend */
fe93c99a
JEB
790 !lv_extend(lv, lp->segtype,
791 lp->stripes, lp->stripe_size,
792 lp->mirrors, first_seg(lv)->region_size,
9ac61d2b 793 lp->extents - lv->le_count, NULL,
fe93c99a 794 pvh, alloc)) {
1a9ea74d
AK
795 stack;
796 return ECMD_FAILED;
03a8a07d
AK
797 }
798
03a8a07d 799 /* store vg on disk(s) */
25b73380 800 if (!vg_write(vg)) {
914c9723 801 stack;
7f0dc9c4 802 return ECMD_FAILED;
cc8b2cd7 803 }
03a8a07d 804
914c9723 805 /* If snapshot, must suspend all associated devices */
2cd0aa72
AK
806 if (lv_is_cow(lv))
807 lock_lv = origin_from_cow(lv);
914c9723 808 else
0fb173aa 809 lock_lv = lv;
914c9723 810
0fb173aa 811 if (!suspend_lv(cmd, lock_lv)) {
241913fe 812 log_error("Failed to suspend %s", lp->lv_name);
914c9723 813 vg_revert(vg);
8f3fd69f 814 backup(vg);
7f0dc9c4 815 return ECMD_FAILED;
914c9723
AK
816 }
817
818 if (!vg_commit(vg)) {
819 stack;
df13cf08
MS
820 if (!resume_lv(cmd, lock_lv))
821 stack;
8f3fd69f 822 backup(vg);
7f0dc9c4 823 return ECMD_FAILED;
914c9723
AK
824 }
825
0fb173aa 826 if (!resume_lv(cmd, lock_lv)) {
241913fe 827 log_error("Problem reactivating %s", lp->lv_name);
8f3fd69f 828 backup(vg);
7f0dc9c4 829 return ECMD_FAILED;
cc8b2cd7 830 }
03a8a07d 831
8f3fd69f
MB
832 backup(vg);
833
6b58c796
ZK
834 /*
835 * Update lvm pool metadata (drop messages) if the pool has been
836 * resumed and do a pool active/deactivate in other case.
837 *
838 * Note: Active thin pool can be waiting for resize.
839 *
840 * FIXME: Activate only when thin volume is active
841 */
842 if (lv_is_thin_pool(lv) &&
843 !update_pool_lv(lv, !lv_is_active(lv))) {
844 stack;
845 return ECMD_FAILED;
846 }
847
241913fe 848 log_print("Logical volume %s successfully resized", lp->lv_name);
03a8a07d 849
bc175d28 850 if (lp->resizefs && (lp->resize == LV_EXTEND) &&
2955b913 851 !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_RESIZE, NULL)) {
c8669f6b
ZK
852 stack;
853 return ECMD_FAILED;
1a9ea74d
AK
854 }
855
cfb7bfc7 856 return ECMD_PROCESSED;
03a8a07d 857}
241913fe
AK
858
859int lvresize(struct cmd_context *cmd, int argc, char **argv)
860{
861 struct lvresize_params lp;
e5f7352b 862 struct volume_group *vg;
241913fe
AK
863 int r;
864
865 memset(&lp, 0, sizeof(lp));
866
8a2fc586 867 if (!_lvresize_params(cmd, argc, argv, &lp))
241913fe
AK
868 return EINVALID_CMD_LINE;
869
870 log_verbose("Finding volume group %s", lp.vg_name);
b8b3508c
DW
871 vg = vg_read_for_update(cmd, lp.vg_name, NULL, 0);
872 if (vg_read_error(vg)) {
077a6755 873 release_vg(vg);
d0bf2f3f 874 stack;
241913fe 875 return ECMD_FAILED;
d0bf2f3f 876 }
241913fe 877
e5f7352b 878 if (!(r = _lvresize(cmd, vg, &lp)))
241913fe
AK
879 stack;
880
077a6755 881 unlock_and_release_vg(cmd, vg, lp.vg_name);
241913fe
AK
882
883 return r;
884}
This page took 0.230976 seconds and 5 git commands to generate.