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