]>
Commit | Line | Data |
---|---|---|
03a8a07d | 1 | /* |
1832f310 | 2 | * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved. |
be684599 | 3 | * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved. |
03a8a07d | 4 | * |
6606c3ae | 5 | * This file is part of LVM2. |
03a8a07d | 6 | * |
6606c3ae AK |
7 | * This copyrighted material is made available to anyone wishing to use, |
8 | * modify, copy, or redistribute it subject to the terms and conditions | |
be684599 | 9 | * of the GNU Lesser General Public License v.2.1. |
03a8a07d | 10 | * |
be684599 | 11 | * You should have received a copy of the GNU Lesser General Public License |
6606c3ae AK |
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 | |
03a8a07d AK |
14 | */ |
15 | ||
16 | #include "tools.h" | |
17 | ||
1a9ea74d AK |
18 | #define SIZE_BUF 128 |
19 | ||
241913fe | 20 | struct lvresize_params { |
8ef2b021 | 21 | const char *vg_name; |
241913fe AK |
22 | const char *lv_name; |
23 | ||
24 | uint32_t stripes; | |
25 | uint32_t stripe_size; | |
ffb0e538 | 26 | uint32_t mirrors; |
241913fe | 27 | |
72b2cb61 | 28 | const struct segment_type *segtype; |
241913fe | 29 | |
241913fe AK |
30 | /* size */ |
31 | uint32_t extents; | |
32 | uint64_t size; | |
33 | sign_t sign; | |
34fadac4 | 34 | percent_t percent; |
03a8a07d AK |
35 | |
36 | enum { | |
37 | LV_ANY = 0, | |
38 | LV_REDUCE = 1, | |
39 | LV_EXTEND = 2 | |
241913fe AK |
40 | } resize; |
41 | ||
1a9ea74d AK |
42 | int resizefs; |
43 | int nofsck; | |
44 | ||
241913fe AK |
45 | int argc; |
46 | char **argv; | |
47 | }; | |
48 | ||
406a9754 | 49 | static int validate_stripesize(struct cmd_context *cmd, |
bf4f5b21 | 50 | const struct volume_group *vg, |
406a9754 DW |
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 | ||
204a12e5 | 58 | if (arg_uint_value(cmd, stripesize_ARG, 0) > STRIPE_SIZE_LIMIT * 2) { |
406a9754 DW |
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."); | |
204a12e5 | 66 | else if (arg_uint_value(cmd, stripesize_ARG, 0) > vg->extent_size * 2) { |
406a9754 DW |
67 | log_error("Reducing stripe size %s to maximum, " |
68 | "physical extent size %s", | |
69 | display_size(cmd, | |
204a12e5 | 70 | (uint64_t) arg_uint_value(cmd, stripesize_ARG, 0)), |
406a9754 DW |
71 | display_size(cmd, (uint64_t) vg->extent_size)); |
72 | lp->stripe_size = vg->extent_size; | |
73 | } else | |
204a12e5 | 74 | lp->stripe_size = arg_uint_value(cmd, stripesize_ARG, 0); |
406a9754 DW |
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, | |
bf4f5b21 DW |
89 | const struct volume_group *vg, |
90 | const struct logical_volume *lv, | |
91 | const struct lvresize_params *lp) | |
406a9754 DW |
92 | { |
93 | struct lvinfo info; | |
94 | ||
95 | memset(&info, 0, sizeof(info)); | |
96 | ||
a6b22cf3 | 97 | if (!lv_info(cmd, lv, &info, 1, 0) && driver_version(NULL, 0)) { |
406a9754 DW |
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 | ||
bf4f5b21 DW |
133 | static int do_resizefs_reduce(const struct cmd_context *cmd, |
134 | const struct volume_group *vg, | |
135 | const struct lvresize_params *lp) | |
406a9754 DW |
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 | ||
8a2fc586 AK |
170 | static int _lvresize_params(struct cmd_context *cmd, int argc, char **argv, |
171 | struct lvresize_params *lp) | |
241913fe AK |
172 | { |
173 | const char *cmd_name; | |
174 | char *st; | |
175 | ||
176 | lp->sign = SIGN_NONE; | |
177 | lp->resize = LV_ANY; | |
03a8a07d | 178 | |
60274aba | 179 | cmd_name = command_name(cmd); |
03a8a07d | 180 | if (!strcmp(cmd_name, "lvreduce")) |
241913fe | 181 | lp->resize = LV_REDUCE; |
03a8a07d | 182 | if (!strcmp(cmd_name, "lvextend")) |
241913fe | 183 | lp->resize = LV_EXTEND; |
03a8a07d | 184 | |
dfef7f69 DW |
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."); | |
241913fe | 199 | return 0; |
03a8a07d AK |
200 | } |
201 | ||
6fda126d | 202 | if (arg_count(cmd, extents_ARG)) { |
241913fe AK |
203 | lp->extents = arg_uint_value(cmd, extents_ARG, 0); |
204 | lp->sign = arg_sign_value(cmd, extents_ARG, SIGN_NONE); | |
34fadac4 | 205 | lp->percent = arg_percent_value(cmd, extents_ARG, PERCENT_NONE); |
03a8a07d AK |
206 | } |
207 | ||
1832f310 | 208 | /* Size returned in kilobyte units; held in sectors */ |
6fda126d | 209 | if (arg_count(cmd, size_ARG)) { |
204a12e5 | 210 | lp->size = arg_uint64_value(cmd, size_ARG, UINT64_C(0)); |
241913fe | 211 | lp->sign = arg_sign_value(cmd, size_ARG, SIGN_NONE); |
34fadac4 | 212 | lp->percent = PERCENT_NONE; |
03a8a07d AK |
213 | } |
214 | ||
241913fe | 215 | if (lp->resize == LV_EXTEND && lp->sign == SIGN_MINUS) { |
03a8a07d | 216 | log_error("Negative argument not permitted - use lvreduce"); |
241913fe | 217 | return 0; |
03a8a07d AK |
218 | } |
219 | ||
241913fe | 220 | if (lp->resize == LV_REDUCE && lp->sign == SIGN_PLUS) { |
03a8a07d | 221 | log_error("Positive sign not permitted - use lvextend"); |
241913fe | 222 | return 0; |
03a8a07d AK |
223 | } |
224 | ||
1a9ea74d AK |
225 | lp->resizefs = arg_count(cmd, resizefs_ARG) ? 1 : 0; |
226 | lp->nofsck = arg_count(cmd, nofsck_ARG) ? 1 : 0; | |
227 | ||
03a8a07d AK |
228 | if (!argc) { |
229 | log_error("Please provide the logical volume name"); | |
241913fe | 230 | return 0; |
03a8a07d AK |
231 | } |
232 | ||
241913fe | 233 | lp->lv_name = argv[0]; |
03a8a07d AK |
234 | argv++; |
235 | argc--; | |
236 | ||
241913fe | 237 | if (!(lp->vg_name = extract_vgname(cmd, lp->lv_name))) { |
03a8a07d | 238 | log_error("Please provide a volume group name"); |
241913fe | 239 | return 0; |
03a8a07d | 240 | } |
d38bf361 AK |
241 | |
242 | if (!validate_name(lp->vg_name)) { | |
243 | log_error("Volume group name %s has invalid characters", | |
244 | lp->vg_name); | |
6c3dc203 | 245 | return 0; |
d38bf361 | 246 | } |
03a8a07d | 247 | |
241913fe AK |
248 | if ((st = strrchr(lp->lv_name, '/'))) |
249 | lp->lv_name = st + 1; | |
03a8a07d | 250 | |
241913fe AK |
251 | lp->argc = argc; |
252 | lp->argv = argv; | |
253 | ||
254 | return 1; | |
255 | } | |
7d0e6e80 | 256 | |
e5f7352b AK |
257 | static int _lvresize(struct cmd_context *cmd, struct volume_group *vg, |
258 | struct lvresize_params *lp) | |
241913fe | 259 | { |
241913fe | 260 | struct logical_volume *lv; |
241913fe AK |
261 | struct lvinfo info; |
262 | uint32_t stripesize_extents = 0; | |
263 | uint32_t seg_stripes = 0, seg_stripesize = 0, seg_size = 0; | |
ffb0e538 | 264 | uint32_t seg_mirrors = 0; |
241913fe AK |
265 | uint32_t extents_used = 0; |
266 | uint32_t size_rest; | |
dfef7f69 | 267 | uint32_t pv_extent_count = 0; |
a0a23eff | 268 | alloc_policy_t alloc; |
0fb173aa | 269 | struct logical_volume *lock_lv; |
241913fe | 270 | struct lv_list *lvl; |
241913fe AK |
271 | struct lv_segment *seg; |
272 | uint32_t seg_extents; | |
273 | uint32_t sz, str; | |
7f0dc9c4 | 274 | struct list *pvh = NULL; |
1a9ea74d AK |
275 | char size_buf[SIZE_BUF]; |
276 | char lv_path[PATH_MAX]; | |
241913fe | 277 | |
03a8a07d | 278 | /* does LV exist? */ |
241913fe | 279 | if (!(lvl = find_lv_in_vg(vg, lp->lv_name))) { |
03a8a07d | 280 | log_error("Logical volume %s not found in volume group %s", |
241913fe | 281 | lp->lv_name, lp->vg_name); |
7f0dc9c4 | 282 | return ECMD_FAILED; |
03a8a07d AK |
283 | } |
284 | ||
25b73380 AK |
285 | if (arg_count(cmd, stripes_ARG)) { |
286 | if (vg->fid->fmt->features & FMT_SEGMENTS) | |
241913fe | 287 | lp->stripes = arg_uint_value(cmd, stripes_ARG, 1); |
25b73380 | 288 | else |
e7ddf416 | 289 | log_warn("Varied striping not supported. Ignoring."); |
25b73380 AK |
290 | } |
291 | ||
ffb0e538 AK |
292 | if (arg_count(cmd, mirrors_ARG)) { |
293 | if (vg->fid->fmt->features & FMT_SEGMENTS) | |
294 | lp->mirrors = arg_uint_value(cmd, mirrors_ARG, 1) + 1; | |
295 | else | |
e7ddf416 | 296 | log_warn("Mirrors not supported. Ignoring."); |
7424de5b AK |
297 | if (arg_sign_value(cmd, mirrors_ARG, 0) == SIGN_MINUS) { |
298 | log_error("Mirrors argument may not be negative"); | |
406a9754 | 299 | return EINVALID_CMD_LINE; |
7424de5b | 300 | } |
ffb0e538 AK |
301 | } |
302 | ||
25b73380 | 303 | if (arg_count(cmd, stripesize_ARG)) { |
406a9754 DW |
304 | if (!validate_stripesize(cmd, vg, lp)) |
305 | return EINVALID_CMD_LINE; | |
25b73380 AK |
306 | } |
307 | ||
f868d635 | 308 | lv = lvl->lv; |
03a8a07d | 309 | |
6e03b44c AK |
310 | if (lv->status & LOCKED) { |
311 | log_error("Can't resize locked LV %s", lv->name); | |
7f0dc9c4 | 312 | return ECMD_FAILED; |
6e03b44c AK |
313 | } |
314 | ||
72b2cb61 | 315 | alloc = arg_uint_value(cmd, alloc_ARG, lv->alloc); |
7f0dc9c4 | 316 | |
241913fe AK |
317 | if (lp->size) { |
318 | if (lp->size % vg->extent_size) { | |
319 | if (lp->sign == SIGN_MINUS) | |
320 | lp->size -= lp->size % vg->extent_size; | |
03a8a07d | 321 | else |
241913fe AK |
322 | lp->size += vg->extent_size - |
323 | (lp->size % vg->extent_size); | |
03a8a07d AK |
324 | |
325 | log_print("Rounding up size to full physical extent %s", | |
72b2cb61 | 326 | display_size(cmd, (uint64_t) lp->size)); |
03a8a07d AK |
327 | } |
328 | ||
241913fe | 329 | lp->extents = lp->size / vg->extent_size; |
03a8a07d AK |
330 | } |
331 | ||
dfef7f69 DW |
332 | if (!(pvh = lp->argc ? create_pv_list(cmd->mem, vg, lp->argc, |
333 | lp->argv, 1) : &vg->pvs)) { | |
334 | stack; | |
335 | return ECMD_FAILED; | |
336 | } | |
337 | ||
34fadac4 AK |
338 | switch(lp->percent) { |
339 | case PERCENT_VG: | |
340 | lp->extents = lp->extents * vg->extent_count / 100; | |
341 | break; | |
342 | case PERCENT_FREE: | |
343 | lp->extents = lp->extents * vg->free_count / 100; | |
344 | break; | |
345 | case PERCENT_LV: | |
346 | lp->extents = lp->extents * lv->le_count / 100; | |
347 | break; | |
dfef7f69 DW |
348 | case PERCENT_PVS: |
349 | pv_extent_count = pv_list_extents_free(pvh); | |
350 | lp->extents = lp->extents * pv_extent_count / 100; | |
351 | break; | |
34fadac4 AK |
352 | case PERCENT_NONE: |
353 | break; | |
354 | } | |
355 | ||
241913fe AK |
356 | if (lp->sign == SIGN_PLUS) |
357 | lp->extents += lv->le_count; | |
03a8a07d | 358 | |
241913fe AK |
359 | if (lp->sign == SIGN_MINUS) { |
360 | if (lp->extents >= lv->le_count) { | |
03a8a07d | 361 | log_error("Unable to reduce %s below 1 extent", |
241913fe | 362 | lp->lv_name); |
7f0dc9c4 | 363 | return EINVALID_CMD_LINE; |
03a8a07d AK |
364 | } |
365 | ||
241913fe | 366 | lp->extents = lv->le_count - lp->extents; |
03a8a07d AK |
367 | } |
368 | ||
241913fe | 369 | if (!lp->extents) { |
03a8a07d | 370 | log_error("New size of 0 not permitted"); |
7f0dc9c4 | 371 | return EINVALID_CMD_LINE; |
03a8a07d AK |
372 | } |
373 | ||
241913fe | 374 | if (lp->extents == lv->le_count) { |
03a8a07d | 375 | log_error("New size (%d extents) matches existing size " |
241913fe | 376 | "(%d extents)", lp->extents, lv->le_count); |
7f0dc9c4 | 377 | return EINVALID_CMD_LINE; |
03a8a07d AK |
378 | } |
379 | ||
241913fe | 380 | seg_size = lp->extents - lv->le_count; |
25b73380 | 381 | |
1832f310 AK |
382 | /* Use segment type of last segment */ |
383 | list_iterate_items(seg, &lv->segments) { | |
241913fe | 384 | lp->segtype = seg->segtype; |
1832f310 AK |
385 | } |
386 | ||
387 | /* FIXME Support LVs with mixed segment types */ | |
72b2cb61 | 388 | if (lp->segtype != arg_ptr_value(cmd, type_ARG, lp->segtype)) { |
241913fe | 389 | log_error("VolumeType does not match (%s)", lp->segtype->name); |
7f0dc9c4 | 390 | return EINVALID_CMD_LINE; |
1832f310 AK |
391 | } |
392 | ||
52dc2139 | 393 | /* If extending, find stripes, stripesize & size of last segment */ |
241913fe AK |
394 | if ((lp->extents > lv->le_count) && |
395 | !(lp->stripes == 1 || (lp->stripes > 1 && lp->stripe_size))) { | |
f2b7349e | 396 | list_iterate_items(seg, &lv->segments) { |
32469fb2 | 397 | if (!seg_is_striped(seg)) |
1832f310 | 398 | continue; |
b8c919b4 | 399 | |
579944d3 | 400 | sz = seg->stripe_size; |
b8c919b4 | 401 | str = seg->area_count; |
52dc2139 | 402 | |
da4e57f2 | 403 | if ((seg_stripesize && seg_stripesize != sz |
241913fe AK |
404 | && !lp->stripe_size) || |
405 | (seg_stripes && seg_stripes != str && !lp->stripes)) { | |
52dc2139 AK |
406 | log_error("Please specify number of " |
407 | "stripes (-i) and stripesize (-I)"); | |
7f0dc9c4 | 408 | return EINVALID_CMD_LINE; |
52dc2139 AK |
409 | } |
410 | ||
411 | seg_stripesize = sz; | |
412 | seg_stripes = str; | |
413 | } | |
414 | ||
241913fe AK |
415 | if (!lp->stripes) |
416 | lp->stripes = seg_stripes; | |
52dc2139 | 417 | |
241913fe | 418 | if (!lp->stripe_size && lp->stripes > 1) { |
25b73380 | 419 | if (seg_stripesize) { |
12de747d | 420 | log_print("Using stripesize of last segment %s", |
72b2cb61 | 421 | display_size(cmd, (uint64_t) seg_stripesize)); |
241913fe | 422 | lp->stripe_size = seg_stripesize; |
25b73380 | 423 | } else { |
7f0dc9c4 | 424 | lp->stripe_size = |
2293567c | 425 | find_config_tree_int(cmd, |
8ef2b021 | 426 | "metadata/stripesize", |
8ef2b021 | 427 | DEFAULT_STRIPESIZE) * 2; |
12de747d | 428 | log_print("Using default stripesize %s", |
72b2cb61 | 429 | display_size(cmd, (uint64_t) lp->stripe_size)); |
25b73380 AK |
430 | } |
431 | } | |
52dc2139 AK |
432 | } |
433 | ||
ffb0e538 AK |
434 | /* If extending, find mirrors of last segment */ |
435 | if ((lp->extents > lv->le_count)) { | |
436 | list_iterate_back_items(seg, &lv->segments) { | |
437 | if (seg_is_mirrored(seg)) | |
438 | seg_mirrors = seg->area_count; | |
439 | else | |
440 | seg_mirrors = 0; | |
441 | break; | |
442 | } | |
443 | if (!arg_count(cmd, mirrors_ARG) && seg_mirrors) { | |
444 | log_print("Extending %" PRIu32 " mirror images.", | |
445 | seg_mirrors); | |
446 | lp->mirrors = seg_mirrors; | |
447 | } | |
448 | if ((arg_count(cmd, mirrors_ARG) || seg_mirrors) && | |
449 | (lp->mirrors != seg_mirrors)) { | |
450 | log_error("Cannot vary number of mirrors in LV yet."); | |
451 | return EINVALID_CMD_LINE; | |
452 | } | |
453 | } | |
454 | ||
52dc2139 | 455 | /* If reducing, find stripes, stripesize & size of last segment */ |
241913fe | 456 | if (lp->extents < lv->le_count) { |
25b73380 | 457 | extents_used = 0; |
52dc2139 | 458 | |
ffb0e538 AK |
459 | if (lp->stripes || lp->stripe_size || lp->mirrors) |
460 | log_error("Ignoring stripes, stripesize and mirrors " | |
461 | "arguments when reducing"); | |
52dc2139 | 462 | |
f2b7349e | 463 | list_iterate_items(seg, &lv->segments) { |
579944d3 AK |
464 | seg_extents = seg->len; |
465 | ||
32469fb2 | 466 | if (seg_is_striped(seg)) { |
b8c919b4 AK |
467 | seg_stripesize = seg->stripe_size; |
468 | seg_stripes = seg->area_count; | |
469 | } | |
52dc2139 | 470 | |
ffb0e538 AK |
471 | if (seg_is_mirrored(seg)) |
472 | seg_mirrors = seg->area_count; | |
473 | else | |
474 | seg_mirrors = 0; | |
475 | ||
241913fe | 476 | if (lp->extents <= extents_used + seg_extents) |
52dc2139 AK |
477 | break; |
478 | ||
479 | extents_used += seg_extents; | |
480 | } | |
481 | ||
241913fe AK |
482 | seg_size = lp->extents - extents_used; |
483 | lp->stripe_size = seg_stripesize; | |
484 | lp->stripes = seg_stripes; | |
ffb0e538 | 485 | lp->mirrors = seg_mirrors; |
52dc2139 AK |
486 | } |
487 | ||
241913fe | 488 | if (lp->stripes > 1 && !lp->stripe_size) { |
25b73380 | 489 | log_error("Stripesize for striped segment should not be 0!"); |
7f0dc9c4 | 490 | return EINVALID_CMD_LINE; |
d4e5f63e | 491 | } |
5a52dca9 | 492 | |
241913fe AK |
493 | if ((lp->stripes > 1)) { |
494 | if (!(stripesize_extents = lp->stripe_size / vg->extent_size)) | |
d4e5f63e AK |
495 | stripesize_extents = 1; |
496 | ||
241913fe | 497 | if ((size_rest = seg_size % (lp->stripes * stripesize_extents))) { |
d4e5f63e AK |
498 | log_print("Rounding size (%d extents) down to stripe " |
499 | "boundary size for segment (%d extents)", | |
241913fe AK |
500 | lp->extents, lp->extents - size_rest); |
501 | lp->extents = lp->extents - size_rest; | |
d4e5f63e | 502 | } |
12de747d AK |
503 | |
504 | if (lp->stripe_size < STRIPE_SIZE_MIN) { | |
505 | log_error("Invalid stripe size %s", | |
72b2cb61 | 506 | display_size(cmd, (uint64_t) lp->stripe_size)); |
406a9754 | 507 | return EINVALID_CMD_LINE; |
12de747d | 508 | } |
da4e57f2 | 509 | } |
52dc2139 | 510 | |
241913fe | 511 | if (lp->extents == lv->le_count) { |
52dc2139 | 512 | log_error("New size (%d extents) matches existing size " |
241913fe | 513 | "(%d extents)", lp->extents, lv->le_count); |
7f0dc9c4 | 514 | return EINVALID_CMD_LINE; |
52dc2139 AK |
515 | } |
516 | ||
241913fe AK |
517 | if (lp->extents < lv->le_count) { |
518 | if (lp->resize == LV_EXTEND) { | |
03a8a07d AK |
519 | log_error("New size given (%d extents) not larger " |
520 | "than existing size (%d extents)", | |
241913fe | 521 | lp->extents, lv->le_count); |
7f0dc9c4 | 522 | return EINVALID_CMD_LINE; |
03a8a07d | 523 | } else |
241913fe | 524 | lp->resize = LV_REDUCE; |
03a8a07d AK |
525 | } |
526 | ||
241913fe AK |
527 | if (lp->extents > lv->le_count) { |
528 | if (lp->resize == LV_REDUCE) { | |
03a8a07d | 529 | log_error("New size given (%d extents) not less than " |
241913fe | 530 | "existing size (%d extents)", lp->extents, |
03a8a07d | 531 | lv->le_count); |
7f0dc9c4 | 532 | return EINVALID_CMD_LINE; |
03a8a07d | 533 | } else |
241913fe | 534 | lp->resize = LV_EXTEND; |
03a8a07d AK |
535 | } |
536 | ||
f5190ed4 | 537 | if (lp->mirrors && activation() && |
a6b22cf3 | 538 | lv_info(cmd, lv, &info, 0, 0) && info.exists) { |
f5190ed4 AK |
539 | log_error("Mirrors cannot be resized while active yet."); |
540 | return ECMD_FAILED; | |
541 | } | |
864de9ce AK |
542 | |
543 | if (lv_is_origin(lv)) { | |
544 | if (lp->resize == LV_REDUCE) { | |
545 | log_error("Snapshot origin volumes cannot be reduced " | |
546 | "in size yet."); | |
547 | return ECMD_FAILED; | |
548 | } | |
549 | ||
550 | memset(&info, 0, sizeof(info)); | |
551 | ||
a6b22cf3 | 552 | if (lv_info(cmd, lv, &info, 0, 0) && info.exists) { |
864de9ce AK |
553 | log_error("Snapshot origin volumes can be resized " |
554 | "only while inactive: try lvchange -an"); | |
555 | return ECMD_FAILED; | |
556 | } | |
557 | } | |
558 | ||
241913fe AK |
559 | if (lp->resize == LV_REDUCE) { |
560 | if (lp->argc) | |
e7ddf416 | 561 | log_warn("Ignoring PVs on command line when reducing"); |
1a9ea74d | 562 | } |
03a8a07d | 563 | |
1a9ea74d | 564 | if (lp->resize == LV_REDUCE || lp->resizefs) { |
406a9754 | 565 | if (!confirm_resizefs_reduce(cmd, vg, lv, lp)) |
1a9ea74d | 566 | return ECMD_FAILED; |
1a9ea74d | 567 | } |
03a8a07d | 568 | |
1a9ea74d | 569 | if (lp->resizefs) { |
1102c4ee | 570 | if (!do_resizefs_reduce(cmd, vg, lp)) |
406a9754 | 571 | return ECMD_FAILED; |
1a9ea74d | 572 | } |
614a4508 | 573 | |
1a9ea74d AK |
574 | if (!archive(vg)) { |
575 | stack; | |
576 | return ECMD_FAILED; | |
577 | } | |
03a8a07d | 578 | |
1a9ea74d AK |
579 | log_print("%sing logical volume %s to %s", |
580 | (lp->resize == LV_REDUCE) ? "Reduc" : "Extend", | |
581 | lp->lv_name, | |
72b2cb61 | 582 | display_size(cmd, (uint64_t) lp->extents * vg->extent_size)); |
1a9ea74d AK |
583 | |
584 | if (lp->resize == LV_REDUCE) { | |
32469fb2 | 585 | if (!lv_reduce(lv, lv->le_count - lp->extents)) { |
7f0dc9c4 AK |
586 | stack; |
587 | return ECMD_FAILED; | |
241913fe | 588 | } |
32469fb2 | 589 | } else if (!lv_extend(lv, lp->segtype, lp->stripes, |
ffb0e538 | 590 | lp->stripe_size, lp->mirrors, |
32469fb2 AK |
591 | lp->extents - lv->le_count, |
592 | NULL, 0u, 0u, pvh, alloc)) { | |
1a9ea74d AK |
593 | stack; |
594 | return ECMD_FAILED; | |
03a8a07d AK |
595 | } |
596 | ||
03a8a07d | 597 | /* store vg on disk(s) */ |
25b73380 | 598 | if (!vg_write(vg)) { |
914c9723 | 599 | stack; |
7f0dc9c4 | 600 | return ECMD_FAILED; |
cc8b2cd7 | 601 | } |
03a8a07d | 602 | |
6fda126d | 603 | backup(vg); |
cc219483 | 604 | |
914c9723 | 605 | /* If snapshot, must suspend all associated devices */ |
2cd0aa72 AK |
606 | if (lv_is_cow(lv)) |
607 | lock_lv = origin_from_cow(lv); | |
914c9723 | 608 | else |
0fb173aa | 609 | lock_lv = lv; |
914c9723 | 610 | |
0fb173aa | 611 | if (!suspend_lv(cmd, lock_lv)) { |
241913fe | 612 | log_error("Failed to suspend %s", lp->lv_name); |
914c9723 | 613 | vg_revert(vg); |
7f0dc9c4 | 614 | return ECMD_FAILED; |
914c9723 AK |
615 | } |
616 | ||
617 | if (!vg_commit(vg)) { | |
618 | stack; | |
0fb173aa | 619 | resume_lv(cmd, lock_lv); |
7f0dc9c4 | 620 | return ECMD_FAILED; |
914c9723 AK |
621 | } |
622 | ||
0fb173aa | 623 | if (!resume_lv(cmd, lock_lv)) { |
241913fe | 624 | log_error("Problem reactivating %s", lp->lv_name); |
7f0dc9c4 | 625 | return ECMD_FAILED; |
cc8b2cd7 | 626 | } |
03a8a07d | 627 | |
241913fe | 628 | log_print("Logical volume %s successfully resized", lp->lv_name); |
03a8a07d | 629 | |
1a9ea74d AK |
630 | if (lp->resizefs && (lp->resize == LV_EXTEND)) { |
631 | if (!exec_cmd("fsadm", "resize", lv_path, size_buf)) { | |
632 | stack; | |
633 | return ECMD_FAILED; | |
634 | } | |
635 | } | |
636 | ||
cfb7bfc7 | 637 | return ECMD_PROCESSED; |
03a8a07d | 638 | } |
241913fe AK |
639 | |
640 | int lvresize(struct cmd_context *cmd, int argc, char **argv) | |
641 | { | |
642 | struct lvresize_params lp; | |
e5f7352b | 643 | struct volume_group *vg; |
241913fe AK |
644 | int r; |
645 | ||
646 | memset(&lp, 0, sizeof(lp)); | |
647 | ||
8a2fc586 | 648 | if (!_lvresize_params(cmd, argc, argv, &lp)) |
241913fe AK |
649 | return EINVALID_CMD_LINE; |
650 | ||
651 | log_verbose("Finding volume group %s", lp.vg_name); | |
e5f7352b AK |
652 | if (!(vg = vg_lock_and_read(cmd, lp.vg_name, NULL, LCK_VG_WRITE, |
653 | CLUSTERED | EXPORTED_VG | LVM_WRITE, | |
654 | CORRECT_INCONSISTENT))) { | |
655 | log_error("Volume group %s doesn't exist", lp.vg_name); | |
241913fe AK |
656 | return ECMD_FAILED; |
657 | } | |
658 | ||
e5f7352b | 659 | if (!(r = _lvresize(cmd, vg, &lp))) |
241913fe AK |
660 | stack; |
661 | ||
662 | unlock_vg(cmd, lp.vg_name); | |
663 | ||
664 | return r; | |
665 | } |