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