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