]> sourceware.org Git - lvm2.git/blob - tools/lvresize.c
Don't allow resizing of internal logical volumes.
[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_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_uint_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->mirrors) {
77 log_error("Mirrors and striping cannot be combined yet.");
78 return 0;
79 }
80 if (lp->stripe_size & (lp->stripe_size - 1)) {
81 log_error("Stripe size must be power of 2");
82 return 0;
83 }
84
85 return 1;
86 }
87
88 static int _request_confirmation(struct cmd_context *cmd,
89 const struct volume_group *vg,
90 const struct logical_volume *lv,
91 const struct lvresize_params *lp)
92 {
93 struct lvinfo info;
94
95 memset(&info, 0, sizeof(info));
96
97 if (!lv_info(cmd, lv, &info, 1, 0) && driver_version(NULL, 0)) {
98 log_error("lv_info failed: aborting");
99 return 0;
100 }
101
102 if (lp->resizefs) {
103 if (!info.exists) {
104 log_error("Logical volume %s must be activated "
105 "before resizing filesystem", lp->lv_name);
106 return 0;
107 }
108 return 1;
109 }
110
111 if (!info.exists)
112 return 1;
113
114 log_warn("WARNING: Reducing active%s logical volume to %s",
115 info.open_count ? " and open" : "",
116 display_size(cmd, (uint64_t) lp->extents * vg->extent_size));
117
118 log_warn("THIS MAY DESTROY YOUR DATA (filesystem etc.)");
119
120 if (!arg_count(cmd, force_ARG)) {
121 if (yes_no_prompt("Do you really want to reduce %s? [y/n]: ",
122 lp->lv_name) == 'n') {
123 log_error("Logical volume %s NOT reduced", lp->lv_name);
124 return 0;
125 }
126 if (sigint_caught())
127 return 0;
128 }
129
130 return 1;
131 }
132
133 enum fsadm_cmd_e { FSADM_CMD_CHECK, FSADM_CMD_RESIZE };
134 #define FSADM_CMD "fsadm"
135 #define FSADM_CMD_MAX_ARGS 6
136
137 /*
138 * FSADM_CMD --dry-run --verbose --force check lv_path
139 * FSADM_CMD --dry-run --verbose --force resize lv_path size
140 */
141 static int _fsadm_cmd(struct cmd_context *cmd,
142 const struct volume_group *vg,
143 const struct lvresize_params *lp,
144 enum fsadm_cmd_e fcmd)
145 {
146 char lv_path[PATH_MAX];
147 char size_buf[SIZE_BUF];
148 const char *argv[FSADM_CMD_MAX_ARGS + 2];
149 unsigned i = 0;
150
151 argv[i++] = FSADM_CMD;
152
153 if (test_mode())
154 argv[i++] = "--dry-run";
155
156 if (verbose_level() >= _LOG_NOTICE)
157 argv[i++] = "--verbose";
158
159 if (arg_count(cmd, force_ARG))
160 argv[i++] = "--force";
161
162 argv[i++] = (fcmd == FSADM_CMD_RESIZE) ? "resize" : "check";
163
164 if (dm_snprintf(lv_path, PATH_MAX, "%s%s/%s", cmd->dev_dir, lp->vg_name,
165 lp->lv_name) < 0) {
166 log_error("Couldn't create LV path for %s", lp->lv_name);
167 return 0;
168 }
169
170 argv[i++] = lv_path;
171
172 if (fcmd == FSADM_CMD_RESIZE) {
173 if (dm_snprintf(size_buf, SIZE_BUF, "%" PRIu64 "K",
174 (uint64_t) lp->extents * vg->extent_size / 2) < 0) {
175 log_error("Couldn't generate new LV size string");
176 return 0;
177 }
178
179 argv[i++] = size_buf;
180 }
181
182 argv[i] = NULL;
183
184 return exec_cmd(cmd, argv);
185 }
186
187 static int _lvresize_params(struct cmd_context *cmd, int argc, char **argv,
188 struct lvresize_params *lp)
189 {
190 const char *cmd_name;
191 char *st;
192 unsigned dev_dir_found = 0;
193
194 lp->sign = SIGN_NONE;
195 lp->resize = LV_ANY;
196
197 cmd_name = command_name(cmd);
198 if (!strcmp(cmd_name, "lvreduce"))
199 lp->resize = LV_REDUCE;
200 if (!strcmp(cmd_name, "lvextend"))
201 lp->resize = LV_EXTEND;
202
203 /*
204 * Allow omission of extents and size if the user has given us
205 * one or more PVs. Most likely, the intent was "resize this
206 * LV the best you can with these PVs"
207 */
208 if ((arg_count(cmd, extents_ARG) + arg_count(cmd, size_ARG) == 0) &&
209 (argc >= 2)) {
210 lp->extents = 100;
211 lp->percent = PERCENT_PVS;
212 lp->sign = SIGN_PLUS;
213 } else if ((arg_count(cmd, extents_ARG) +
214 arg_count(cmd, size_ARG) != 1)) {
215 log_error("Please specify either size or extents but not "
216 "both.");
217 return 0;
218 }
219
220 if (arg_count(cmd, extents_ARG)) {
221 lp->extents = arg_uint_value(cmd, extents_ARG, 0);
222 lp->sign = arg_sign_value(cmd, extents_ARG, SIGN_NONE);
223 lp->percent = arg_percent_value(cmd, extents_ARG, PERCENT_NONE);
224 }
225
226 /* Size returned in kilobyte units; held in sectors */
227 if (arg_count(cmd, size_ARG)) {
228 lp->size = arg_uint64_value(cmd, size_ARG, UINT64_C(0));
229 lp->sign = arg_sign_value(cmd, size_ARG, SIGN_NONE);
230 lp->percent = PERCENT_NONE;
231 }
232
233 if (lp->resize == LV_EXTEND && lp->sign == SIGN_MINUS) {
234 log_error("Negative argument not permitted - use lvreduce");
235 return 0;
236 }
237
238 if (lp->resize == LV_REDUCE && lp->sign == SIGN_PLUS) {
239 log_error("Positive sign not permitted - use lvextend");
240 return 0;
241 }
242
243 lp->resizefs = arg_is_set(cmd, resizefs_ARG);
244 lp->nofsck = arg_is_set(cmd, nofsck_ARG);
245
246 if (!argc) {
247 log_error("Please provide the logical volume name");
248 return 0;
249 }
250
251 lp->lv_name = argv[0];
252 argv++;
253 argc--;
254
255 if (!(lp->lv_name = skip_dev_dir(cmd, lp->lv_name, &dev_dir_found)) ||
256 !(lp->vg_name = extract_vgname(cmd, lp->lv_name))) {
257 log_error("Please provide a volume group name");
258 return 0;
259 }
260
261 if (!validate_name(lp->vg_name)) {
262 log_error("Volume group name %s has invalid characters",
263 lp->vg_name);
264 return 0;
265 }
266
267 if ((st = strrchr(lp->lv_name, '/')))
268 lp->lv_name = st + 1;
269
270 lp->argc = argc;
271 lp->argv = argv;
272
273 return 1;
274 }
275
276 static int _lvresize(struct cmd_context *cmd, struct volume_group *vg,
277 struct lvresize_params *lp)
278 {
279 struct logical_volume *lv;
280 struct lvinfo info;
281 uint32_t stripesize_extents = 0;
282 uint32_t seg_stripes = 0, seg_stripesize = 0, seg_size = 0;
283 uint32_t seg_mirrors = 0;
284 uint32_t extents_used = 0;
285 uint32_t size_rest;
286 uint32_t pv_extent_count = 0;
287 alloc_policy_t alloc;
288 struct logical_volume *lock_lv;
289 struct lv_list *lvl;
290 struct lv_segment *seg;
291 uint32_t seg_extents;
292 uint32_t sz, str;
293 struct dm_list *pvh = NULL;
294
295 /* does LV exist? */
296 if (!(lvl = find_lv_in_vg(vg, lp->lv_name))) {
297 log_error("Logical volume %s not found in volume group %s",
298 lp->lv_name, lp->vg_name);
299 return ECMD_FAILED;
300 }
301
302 if (arg_count(cmd, stripes_ARG)) {
303 if (vg->fid->fmt->features & FMT_SEGMENTS)
304 lp->stripes = arg_uint_value(cmd, stripes_ARG, 1);
305 else
306 log_warn("Varied striping not supported. Ignoring.");
307 }
308
309 if (arg_count(cmd, mirrors_ARG)) {
310 if (vg->fid->fmt->features & FMT_SEGMENTS)
311 lp->mirrors = arg_uint_value(cmd, mirrors_ARG, 1) + 1;
312 else
313 log_warn("Mirrors not supported. Ignoring.");
314 if (arg_sign_value(cmd, mirrors_ARG, 0) == SIGN_MINUS) {
315 log_error("Mirrors argument may not be negative");
316 return EINVALID_CMD_LINE;
317 }
318 }
319
320 if (arg_count(cmd, stripesize_ARG) &&
321 !_validate_stripesize(cmd, vg, lp))
322 return EINVALID_CMD_LINE;
323
324 lv = lvl->lv;
325
326 if (!lv_is_visible(lv)) {
327 log_error("Can't resize internal logical volume %s", lv->name);
328 return ECMD_FAILED;
329 }
330
331 if (lv->status & LOCKED) {
332 log_error("Can't resize locked LV %s", lv->name);
333 return ECMD_FAILED;
334 }
335
336 if (lv->status & CONVERTING) {
337 log_error("Can't resize %s while lvconvert in progress", lv->name);
338 return ECMD_FAILED;
339 }
340
341 alloc = arg_uint_value(cmd, alloc_ARG, lv->alloc);
342
343 if (lp->size) {
344 if (lp->size % vg->extent_size) {
345 if (lp->sign == SIGN_MINUS)
346 lp->size -= lp->size % vg->extent_size;
347 else
348 lp->size += vg->extent_size -
349 (lp->size % vg->extent_size);
350
351 log_print("Rounding up size to full physical extent %s",
352 display_size(cmd, (uint64_t) lp->size));
353 }
354
355 lp->extents = lp->size / vg->extent_size;
356 }
357
358 if (!(pvh = lp->argc ? create_pv_list(cmd->mem, vg, lp->argc,
359 lp->argv, 1) : &vg->pvs)) {
360 stack;
361 return ECMD_FAILED;
362 }
363
364 switch(lp->percent) {
365 case PERCENT_VG:
366 lp->extents = lp->extents * vg->extent_count / 100;
367 break;
368 case PERCENT_FREE:
369 lp->extents = lp->extents * vg->free_count / 100;
370 break;
371 case PERCENT_LV:
372 lp->extents = lp->extents * lv->le_count / 100;
373 break;
374 case PERCENT_PVS:
375 if (lp->argc) {
376 pv_extent_count = pv_list_extents_free(pvh);
377 lp->extents = lp->extents * pv_extent_count / 100;
378 } else
379 lp->extents = lp->extents * vg->extent_count / 100;
380 break;
381 case PERCENT_ORIGIN:
382 if (!lv_is_cow(lv)) {
383 log_error("Specified LV does not have an origin LV.");
384 return EINVALID_CMD_LINE;
385 }
386 lp->extents = lp->extents * origin_from_cow(lv)->le_count / 100;
387 break;
388 case PERCENT_NONE:
389 break;
390 }
391
392 if (lp->sign == SIGN_PLUS)
393 lp->extents += lv->le_count;
394
395 if (lp->sign == SIGN_MINUS) {
396 if (lp->extents >= lv->le_count) {
397 log_error("Unable to reduce %s below 1 extent",
398 lp->lv_name);
399 return EINVALID_CMD_LINE;
400 }
401
402 lp->extents = lv->le_count - lp->extents;
403 }
404
405 if (!lp->extents) {
406 log_error("New size of 0 not permitted");
407 return EINVALID_CMD_LINE;
408 }
409
410 if (lp->extents == lv->le_count) {
411 if (!lp->resizefs) {
412 log_error("New size (%d extents) matches existing size "
413 "(%d extents)", lp->extents, lv->le_count);
414 return EINVALID_CMD_LINE;
415 }
416 lp->resize = LV_EXTEND; /* lets pretend zero size extension */
417 }
418
419 seg_size = lp->extents - lv->le_count;
420
421 /* Use segment type of last segment */
422 dm_list_iterate_items(seg, &lv->segments) {
423 lp->segtype = seg->segtype;
424 }
425
426 /* FIXME Support LVs with mixed segment types */
427 if (lp->segtype != arg_ptr_value(cmd, type_ARG, lp->segtype)) {
428 log_error("VolumeType does not match (%s)", lp->segtype->name);
429 return EINVALID_CMD_LINE;
430 }
431
432 /* If extending, find stripes, stripesize & size of last segment */
433 if ((lp->extents > lv->le_count) &&
434 !(lp->stripes == 1 || (lp->stripes > 1 && lp->stripe_size))) {
435 dm_list_iterate_items(seg, &lv->segments) {
436 if (!seg_is_striped(seg))
437 continue;
438
439 sz = seg->stripe_size;
440 str = seg->area_count;
441
442 if ((seg_stripesize && seg_stripesize != sz &&
443 !lp->stripe_size) ||
444 (seg_stripes && seg_stripes != str && !lp->stripes)) {
445 log_error("Please specify number of "
446 "stripes (-i) and stripesize (-I)");
447 return EINVALID_CMD_LINE;
448 }
449
450 seg_stripesize = sz;
451 seg_stripes = str;
452 }
453
454 if (!lp->stripes)
455 lp->stripes = seg_stripes;
456
457 if (!lp->stripe_size && lp->stripes > 1) {
458 if (seg_stripesize) {
459 log_print("Using stripesize of last segment %s",
460 display_size(cmd, (uint64_t) seg_stripesize));
461 lp->stripe_size = seg_stripesize;
462 } else {
463 lp->stripe_size =
464 find_config_tree_int(cmd,
465 "metadata/stripesize",
466 DEFAULT_STRIPESIZE) * 2;
467 log_print("Using default stripesize %s",
468 display_size(cmd, (uint64_t) lp->stripe_size));
469 }
470 }
471 }
472
473 /* If extending, find mirrors of last segment */
474 if ((lp->extents > lv->le_count)) {
475 dm_list_iterate_back_items(seg, &lv->segments) {
476 if (seg_is_mirrored(seg))
477 seg_mirrors = lv_mirror_count(seg->lv);
478 else
479 seg_mirrors = 0;
480 break;
481 }
482 if (!arg_count(cmd, mirrors_ARG) && seg_mirrors) {
483 log_print("Extending %" PRIu32 " mirror images.",
484 seg_mirrors);
485 lp->mirrors = seg_mirrors;
486 }
487 if ((arg_count(cmd, mirrors_ARG) || seg_mirrors) &&
488 (lp->mirrors != seg_mirrors)) {
489 log_error("Cannot vary number of mirrors in LV yet.");
490 return EINVALID_CMD_LINE;
491 }
492 }
493
494 /* If reducing, find stripes, stripesize & size of last segment */
495 if (lp->extents < lv->le_count) {
496 extents_used = 0;
497
498 if (lp->stripes || lp->stripe_size || lp->mirrors)
499 log_error("Ignoring stripes, stripesize and mirrors "
500 "arguments when reducing");
501
502 dm_list_iterate_items(seg, &lv->segments) {
503 seg_extents = seg->len;
504
505 if (seg_is_striped(seg)) {
506 seg_stripesize = seg->stripe_size;
507 seg_stripes = seg->area_count;
508 }
509
510 if (seg_is_mirrored(seg))
511 seg_mirrors = lv_mirror_count(seg->lv);
512 else
513 seg_mirrors = 0;
514
515 if (lp->extents <= extents_used + seg_extents)
516 break;
517
518 extents_used += seg_extents;
519 }
520
521 seg_size = lp->extents - extents_used;
522 lp->stripe_size = seg_stripesize;
523 lp->stripes = seg_stripes;
524 lp->mirrors = seg_mirrors;
525 }
526
527 if (lp->stripes > 1 && !lp->stripe_size) {
528 log_error("Stripesize for striped segment should not be 0!");
529 return EINVALID_CMD_LINE;
530 }
531
532 if ((lp->stripes > 1)) {
533 if (!(stripesize_extents = lp->stripe_size / vg->extent_size))
534 stripesize_extents = 1;
535
536 if ((size_rest = seg_size % (lp->stripes * stripesize_extents))) {
537 log_print("Rounding size (%d extents) down to stripe "
538 "boundary size for segment (%d extents)",
539 lp->extents, lp->extents - size_rest);
540 lp->extents = lp->extents - size_rest;
541 }
542
543 if (lp->stripe_size < STRIPE_SIZE_MIN) {
544 log_error("Invalid stripe size %s",
545 display_size(cmd, (uint64_t) lp->stripe_size));
546 return EINVALID_CMD_LINE;
547 }
548 }
549
550 if (lp->extents < lv->le_count) {
551 if (lp->resize == LV_EXTEND) {
552 log_error("New size given (%d extents) not larger "
553 "than existing size (%d extents)",
554 lp->extents, lv->le_count);
555 return EINVALID_CMD_LINE;
556 }
557 lp->resize = LV_REDUCE;
558 } else if (lp->extents > lv->le_count) {
559 if (lp->resize == LV_REDUCE) {
560 log_error("New size given (%d extents) not less than "
561 "existing size (%d extents)", lp->extents,
562 lv->le_count);
563 return EINVALID_CMD_LINE;
564 }
565 lp->resize = LV_EXTEND;
566 }
567
568 if (lv_is_origin(lv)) {
569 if (lp->resize == LV_REDUCE) {
570 log_error("Snapshot origin volumes cannot be reduced "
571 "in size yet.");
572 return ECMD_FAILED;
573 }
574
575 memset(&info, 0, sizeof(info));
576
577 if (lv_info(cmd, lv, &info, 0, 0) && info.exists) {
578 log_error("Snapshot origin volumes can be resized "
579 "only while inactive: try lvchange -an");
580 return ECMD_FAILED;
581 }
582 }
583
584 if ((lp->resize == LV_REDUCE) && lp->argc)
585 log_warn("Ignoring PVs on command line when reducing");
586
587 /* Request confirmation before operations that are often mistakes. */
588 if ((lp->resizefs || (lp->resize == LV_REDUCE)) &&
589 !_request_confirmation(cmd, vg, lv, lp)) {
590 stack;
591 return ECMD_FAILED;
592 }
593
594 if (lp->resizefs) {
595 if (!lp->nofsck &&
596 !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_CHECK)) {
597 stack;
598 return ECMD_FAILED;
599 }
600
601 if ((lp->resize == LV_REDUCE) &&
602 !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_RESIZE)) {
603 stack;
604 return ECMD_FAILED;
605 }
606 }
607
608 if (!archive(vg)) {
609 stack;
610 return ECMD_FAILED;
611 }
612
613 log_print("%sing logical volume %s to %s",
614 (lp->resize == LV_REDUCE) ? "Reduc" : "Extend",
615 lp->lv_name,
616 display_size(cmd, (uint64_t) lp->extents * vg->extent_size));
617
618 if (lp->resize == LV_REDUCE) {
619 if (!lv_reduce(lv, lv->le_count - lp->extents)) {
620 stack;
621 return ECMD_FAILED;
622 }
623 } else if ((lp->extents > lv->le_count) && /* Ensure we extend */
624 !lv_extend(lv, lp->segtype, lp->stripes,
625 lp->stripe_size, lp->mirrors,
626 lp->extents - lv->le_count,
627 NULL, 0u, 0u, pvh, alloc)) {
628 stack;
629 return ECMD_FAILED;
630 }
631
632 /* store vg on disk(s) */
633 if (!vg_write(vg)) {
634 stack;
635 return ECMD_FAILED;
636 }
637
638 /* If snapshot, must suspend all associated devices */
639 if (lv_is_cow(lv))
640 lock_lv = origin_from_cow(lv);
641 else
642 lock_lv = lv;
643
644 if (!suspend_lv(cmd, lock_lv)) {
645 log_error("Failed to suspend %s", lp->lv_name);
646 vg_revert(vg);
647 backup(vg);
648 return ECMD_FAILED;
649 }
650
651 if (!vg_commit(vg)) {
652 stack;
653 if (!resume_lv(cmd, lock_lv))
654 stack;
655 backup(vg);
656 return ECMD_FAILED;
657 }
658
659 if (!resume_lv(cmd, lock_lv)) {
660 log_error("Problem reactivating %s", lp->lv_name);
661 backup(vg);
662 return ECMD_FAILED;
663 }
664
665 backup(vg);
666
667 log_print("Logical volume %s successfully resized", lp->lv_name);
668
669 if (lp->resizefs && (lp->resize == LV_EXTEND) &&
670 !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_RESIZE)) {
671 stack;
672 return ECMD_FAILED;
673 }
674
675 return ECMD_PROCESSED;
676 }
677
678 int lvresize(struct cmd_context *cmd, int argc, char **argv)
679 {
680 struct lvresize_params lp;
681 struct volume_group *vg;
682 int r;
683
684 memset(&lp, 0, sizeof(lp));
685
686 if (!_lvresize_params(cmd, argc, argv, &lp))
687 return EINVALID_CMD_LINE;
688
689 log_verbose("Finding volume group %s", lp.vg_name);
690 vg = vg_read_for_update(cmd, lp.vg_name, NULL, 0);
691 if (vg_read_error(vg)) {
692 vg_release(vg);
693 stack;
694 return ECMD_FAILED;
695 }
696
697 if (!(r = _lvresize(cmd, vg, &lp)))
698 stack;
699
700 unlock_and_release_vg(cmd, vg, lp.vg_name);
701
702 return r;
703 }
This page took 0.067518 seconds and 6 git commands to generate.