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