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