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