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