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