]>
Commit | Line | Data |
---|---|---|
03a8a07d | 1 | /* |
1832f310 | 2 | * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved. |
daa10ad0 | 3 | * Copyright (C) 2004-2011 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; | |
8191fe4f | 34 | percent_type_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 | ||
301c2b88 | 58 | if (arg_uint64_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 | ||
2d6fcbf6 | 93 | if (!lv_info(cmd, lv, 0, &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 | |
2955b913 | 132 | #define FSADM_CHECK_FAILS_FOR_MOUNTED 3 /* shell exist status code */ |
c8669f6b | 133 | |
bc175d28 AK |
134 | /* |
135 | * FSADM_CMD --dry-run --verbose --force check lv_path | |
136 | * FSADM_CMD --dry-run --verbose --force resize lv_path size | |
137 | */ | |
ed82bfd2 | 138 | static int _fsadm_cmd(struct cmd_context *cmd, |
bc175d28 AK |
139 | const struct volume_group *vg, |
140 | const struct lvresize_params *lp, | |
2955b913 ZK |
141 | enum fsadm_cmd_e fcmd, |
142 | int *status) | |
406a9754 DW |
143 | { |
144 | char lv_path[PATH_MAX]; | |
145 | char size_buf[SIZE_BUF]; | |
bc175d28 AK |
146 | const char *argv[FSADM_CMD_MAX_ARGS + 2]; |
147 | unsigned i = 0; | |
c8669f6b | 148 | |
bc175d28 | 149 | argv[i++] = FSADM_CMD; |
c8669f6b ZK |
150 | |
151 | if (test_mode()) | |
152 | argv[i++] = "--dry-run"; | |
153 | ||
bc175d28 | 154 | if (verbose_level() >= _LOG_NOTICE) |
c8669f6b ZK |
155 | argv[i++] = "--verbose"; |
156 | ||
157 | if (arg_count(cmd, force_ARG)) | |
158 | argv[i++] = "--force"; | |
159 | ||
160 | argv[i++] = (fcmd == FSADM_CMD_RESIZE) ? "resize" : "check"; | |
406a9754 | 161 | |
bc175d28 AK |
162 | if (dm_snprintf(lv_path, PATH_MAX, "%s%s/%s", cmd->dev_dir, lp->vg_name, |
163 | lp->lv_name) < 0) { | |
164 | log_error("Couldn't create LV path for %s", lp->lv_name); | |
406a9754 DW |
165 | return 0; |
166 | } | |
167 | ||
c8669f6b | 168 | argv[i++] = lv_path; |
406a9754 | 169 | |
c8669f6b ZK |
170 | if (fcmd == FSADM_CMD_RESIZE) { |
171 | if (dm_snprintf(size_buf, SIZE_BUF, "%" PRIu64 "K", | |
172 | (uint64_t) lp->extents * vg->extent_size / 2) < 0) { | |
173 | log_error("Couldn't generate new LV size string"); | |
174 | return 0; | |
175 | } | |
406a9754 | 176 | |
c8669f6b ZK |
177 | argv[i++] = size_buf; |
178 | } | |
406a9754 | 179 | |
c8669f6b ZK |
180 | argv[i] = NULL; |
181 | ||
b1b38215 | 182 | return exec_cmd(cmd, argv, status, 1); |
406a9754 DW |
183 | } |
184 | ||
8a2fc586 AK |
185 | static int _lvresize_params(struct cmd_context *cmd, int argc, char **argv, |
186 | struct lvresize_params *lp) | |
241913fe AK |
187 | { |
188 | const char *cmd_name; | |
189 | char *st; | |
17dd04ca | 190 | unsigned dev_dir_found = 0; |
a341cab7 | 191 | int use_policy = arg_count(cmd, use_policies_ARG); |
241913fe AK |
192 | |
193 | lp->sign = SIGN_NONE; | |
194 | lp->resize = LV_ANY; | |
03a8a07d | 195 | |
60274aba | 196 | cmd_name = command_name(cmd); |
03a8a07d | 197 | if (!strcmp(cmd_name, "lvreduce")) |
241913fe | 198 | lp->resize = LV_REDUCE; |
03a8a07d | 199 | if (!strcmp(cmd_name, "lvextend")) |
241913fe | 200 | lp->resize = LV_EXTEND; |
03a8a07d | 201 | |
a341cab7 PR |
202 | if (use_policy) { |
203 | /* do nothing; _lvresize will handle --use-policies itself */ | |
204 | lp->extents = 0; | |
dfef7f69 | 205 | lp->sign = SIGN_PLUS; |
a341cab7 PR |
206 | lp->percent = PERCENT_LV; |
207 | } else { | |
208 | /* | |
209 | * Allow omission of extents and size if the user has given us | |
210 | * one or more PVs. Most likely, the intent was "resize this | |
211 | * LV the best you can with these PVs" | |
212 | */ | |
213 | if ((arg_count(cmd, extents_ARG) + arg_count(cmd, size_ARG) == 0) && | |
214 | (argc >= 2)) { | |
215 | lp->extents = 100; | |
216 | lp->percent = PERCENT_PVS; | |
217 | lp->sign = SIGN_PLUS; | |
218 | } else if ((arg_count(cmd, extents_ARG) + | |
219 | arg_count(cmd, size_ARG) != 1)) { | |
220 | log_error("Please specify either size or extents but not " | |
221 | "both."); | |
222 | return 0; | |
223 | } | |
03a8a07d | 224 | |
a341cab7 PR |
225 | if (arg_count(cmd, extents_ARG)) { |
226 | lp->extents = arg_uint_value(cmd, extents_ARG, 0); | |
227 | lp->sign = arg_sign_value(cmd, extents_ARG, SIGN_NONE); | |
228 | lp->percent = arg_percent_value(cmd, extents_ARG, PERCENT_NONE); | |
229 | } | |
03a8a07d | 230 | |
a341cab7 PR |
231 | /* Size returned in kilobyte units; held in sectors */ |
232 | if (arg_count(cmd, size_ARG)) { | |
233 | lp->size = arg_uint64_value(cmd, size_ARG, 0); | |
234 | lp->sign = arg_sign_value(cmd, size_ARG, SIGN_NONE); | |
235 | lp->percent = PERCENT_NONE; | |
236 | } | |
03a8a07d AK |
237 | } |
238 | ||
241913fe | 239 | if (lp->resize == LV_EXTEND && lp->sign == SIGN_MINUS) { |
03a8a07d | 240 | log_error("Negative argument not permitted - use lvreduce"); |
241913fe | 241 | return 0; |
03a8a07d AK |
242 | } |
243 | ||
241913fe | 244 | if (lp->resize == LV_REDUCE && lp->sign == SIGN_PLUS) { |
03a8a07d | 245 | log_error("Positive sign not permitted - use lvextend"); |
241913fe | 246 | return 0; |
03a8a07d AK |
247 | } |
248 | ||
a8fb89ad AK |
249 | lp->resizefs = arg_is_set(cmd, resizefs_ARG); |
250 | lp->nofsck = arg_is_set(cmd, nofsck_ARG); | |
1a9ea74d | 251 | |
03a8a07d AK |
252 | if (!argc) { |
253 | log_error("Please provide the logical volume name"); | |
241913fe | 254 | return 0; |
03a8a07d AK |
255 | } |
256 | ||
241913fe | 257 | lp->lv_name = argv[0]; |
03a8a07d AK |
258 | argv++; |
259 | argc--; | |
260 | ||
3a370b73 AK |
261 | if (!(lp->lv_name = skip_dev_dir(cmd, lp->lv_name, &dev_dir_found)) || |
262 | !(lp->vg_name = extract_vgname(cmd, lp->lv_name))) { | |
03a8a07d | 263 | log_error("Please provide a volume group name"); |
241913fe | 264 | return 0; |
03a8a07d | 265 | } |
17dd04ca | 266 | |
d38bf361 AK |
267 | if (!validate_name(lp->vg_name)) { |
268 | log_error("Volume group name %s has invalid characters", | |
269 | lp->vg_name); | |
6c3dc203 | 270 | return 0; |
d38bf361 | 271 | } |
03a8a07d | 272 | |
241913fe AK |
273 | if ((st = strrchr(lp->lv_name, '/'))) |
274 | lp->lv_name = st + 1; | |
03a8a07d | 275 | |
241913fe AK |
276 | lp->argc = argc; |
277 | lp->argv = argv; | |
278 | ||
279 | return 1; | |
280 | } | |
7d0e6e80 | 281 | |
a341cab7 PR |
282 | static int _adjust_policy_params(struct cmd_context *cmd, |
283 | struct logical_volume *lv, struct lvresize_params *lp) | |
284 | { | |
8191fe4f | 285 | percent_t percent; |
a341cab7 PR |
286 | int policy_threshold, policy_amount; |
287 | ||
288 | policy_threshold = | |
289 | find_config_tree_int(cmd, "activation/snapshot_autoextend_threshold", | |
8191fe4f | 290 | DEFAULT_SNAPSHOT_AUTOEXTEND_THRESHOLD) * PERCENT_1; |
a341cab7 PR |
291 | policy_amount = |
292 | find_config_tree_int(cmd, "activation/snapshot_autoextend_percent", | |
293 | DEFAULT_SNAPSHOT_AUTOEXTEND_PERCENT); | |
294 | ||
8191fe4f | 295 | if (policy_threshold >= PERCENT_100) |
a341cab7 PR |
296 | return 1; /* nothing to do */ |
297 | ||
8191fe4f | 298 | if (!lv_snapshot_percent(lv, &percent)) |
a341cab7 PR |
299 | return_0; |
300 | ||
8191fe4f | 301 | if (!(PERCENT_0 < percent && percent < PERCENT_100) || percent <= policy_threshold) |
a341cab7 PR |
302 | return 1; /* nothing to do */ |
303 | ||
304 | lp->extents = policy_amount; | |
305 | return 1; | |
306 | } | |
307 | ||
f4ba9c5d MB |
308 | static uint32_t lvseg_get_stripes(struct lv_segment *seg, uint32_t *stripesize) |
309 | { | |
310 | uint32_t s; | |
311 | struct lv_segment *seg_mirr; | |
312 | ||
313 | /* If segment mirrored, check if images are striped */ | |
314 | if (seg_is_mirrored(seg)) | |
315 | for (s = 0; s < seg->area_count; s++) { | |
316 | if (seg_type(seg, s) != AREA_LV) | |
317 | continue; | |
318 | seg_mirr = first_seg(seg_lv(seg, s)); | |
319 | ||
320 | if (seg_is_striped(seg_mirr)) { | |
321 | seg = seg_mirr; | |
322 | break; | |
323 | } | |
324 | } | |
325 | ||
326 | ||
327 | if (seg_is_striped(seg)) { | |
328 | *stripesize = seg->stripe_size; | |
329 | return seg->area_count; | |
330 | } | |
331 | ||
332 | *stripesize = 0; | |
333 | return 0; | |
334 | } | |
335 | ||
e5f7352b AK |
336 | static int _lvresize(struct cmd_context *cmd, struct volume_group *vg, |
337 | struct lvresize_params *lp) | |
241913fe | 338 | { |
241913fe | 339 | struct logical_volume *lv; |
241913fe AK |
340 | struct lvinfo info; |
341 | uint32_t stripesize_extents = 0; | |
342 | uint32_t seg_stripes = 0, seg_stripesize = 0, seg_size = 0; | |
ffb0e538 | 343 | uint32_t seg_mirrors = 0; |
241913fe AK |
344 | uint32_t extents_used = 0; |
345 | uint32_t size_rest; | |
dfef7f69 | 346 | uint32_t pv_extent_count = 0; |
a0a23eff | 347 | alloc_policy_t alloc; |
0fb173aa | 348 | struct logical_volume *lock_lv; |
241913fe | 349 | struct lv_list *lvl; |
1485ce69 | 350 | struct lv_segment *seg, *uninitialized_var(mirr_seg); |
241913fe AK |
351 | uint32_t seg_extents; |
352 | uint32_t sz, str; | |
2955b913 | 353 | int status; |
2c44337b | 354 | struct dm_list *pvh = NULL; |
a341cab7 | 355 | int use_policy = arg_count(cmd, use_policies_ARG); |
241913fe | 356 | |
03a8a07d | 357 | /* does LV exist? */ |
241913fe | 358 | if (!(lvl = find_lv_in_vg(vg, lp->lv_name))) { |
03a8a07d | 359 | log_error("Logical volume %s not found in volume group %s", |
241913fe | 360 | lp->lv_name, lp->vg_name); |
7f0dc9c4 | 361 | return ECMD_FAILED; |
03a8a07d AK |
362 | } |
363 | ||
d34991ed JEB |
364 | if (lvl->lv->status & (RAID_IMAGE | RAID_META)) { |
365 | log_error("Cannot resize a RAID %s directly", | |
366 | (lvl->lv->status & RAID_IMAGE) ? "image" : | |
367 | "metadata area"); | |
368 | return ECMD_FAILED; | |
369 | } | |
370 | ||
371 | if (lv_is_raid_with_tracking(lvl->lv)) { | |
372 | log_error("Cannot resize %s while it is tracking a split image", | |
373 | lvl->lv->name); | |
374 | return ECMD_FAILED; | |
375 | } | |
376 | ||
25b73380 AK |
377 | if (arg_count(cmd, stripes_ARG)) { |
378 | if (vg->fid->fmt->features & FMT_SEGMENTS) | |
241913fe | 379 | lp->stripes = arg_uint_value(cmd, stripes_ARG, 1); |
25b73380 | 380 | else |
e7ddf416 | 381 | log_warn("Varied striping not supported. Ignoring."); |
25b73380 AK |
382 | } |
383 | ||
ffb0e538 AK |
384 | if (arg_count(cmd, mirrors_ARG)) { |
385 | if (vg->fid->fmt->features & FMT_SEGMENTS) | |
386 | lp->mirrors = arg_uint_value(cmd, mirrors_ARG, 1) + 1; | |
387 | else | |
e7ddf416 | 388 | log_warn("Mirrors not supported. Ignoring."); |
7424de5b AK |
389 | if (arg_sign_value(cmd, mirrors_ARG, 0) == SIGN_MINUS) { |
390 | log_error("Mirrors argument may not be negative"); | |
406a9754 | 391 | return EINVALID_CMD_LINE; |
7424de5b | 392 | } |
ffb0e538 AK |
393 | } |
394 | ||
bc175d28 AK |
395 | if (arg_count(cmd, stripesize_ARG) && |
396 | !_validate_stripesize(cmd, vg, lp)) | |
397 | return EINVALID_CMD_LINE; | |
25b73380 | 398 | |
f868d635 | 399 | lv = lvl->lv; |
03a8a07d | 400 | |
a341cab7 PR |
401 | if (use_policy) { |
402 | if (!lv_is_cow(lv)) { | |
403 | log_error("Can't use policy-based resize for non-snapshot volumes."); | |
404 | return ECMD_FAILED; | |
405 | } | |
406 | _adjust_policy_params(cmd, lv, lp); | |
407 | } | |
408 | ||
fd817ff3 MS |
409 | if (!lv_is_visible(lv)) { |
410 | log_error("Can't resize internal logical volume %s", lv->name); | |
411 | return ECMD_FAILED; | |
412 | } | |
413 | ||
6e03b44c AK |
414 | if (lv->status & LOCKED) { |
415 | log_error("Can't resize locked LV %s", lv->name); | |
7f0dc9c4 | 416 | return ECMD_FAILED; |
6e03b44c AK |
417 | } |
418 | ||
08b481bb AK |
419 | if (lv->status & CONVERTING) { |
420 | log_error("Can't resize %s while lvconvert in progress", lv->name); | |
421 | return ECMD_FAILED; | |
422 | } | |
423 | ||
72b2cb61 | 424 | alloc = arg_uint_value(cmd, alloc_ARG, lv->alloc); |
7f0dc9c4 | 425 | |
241913fe AK |
426 | if (lp->size) { |
427 | if (lp->size % vg->extent_size) { | |
428 | if (lp->sign == SIGN_MINUS) | |
429 | lp->size -= lp->size % vg->extent_size; | |
03a8a07d | 430 | else |
241913fe AK |
431 | lp->size += vg->extent_size - |
432 | (lp->size % vg->extent_size); | |
03a8a07d AK |
433 | |
434 | log_print("Rounding up size to full physical extent %s", | |
72b2cb61 | 435 | display_size(cmd, (uint64_t) lp->size)); |
03a8a07d AK |
436 | } |
437 | ||
241913fe | 438 | lp->extents = lp->size / vg->extent_size; |
03a8a07d AK |
439 | } |
440 | ||
dfef7f69 DW |
441 | if (!(pvh = lp->argc ? create_pv_list(cmd->mem, vg, lp->argc, |
442 | lp->argv, 1) : &vg->pvs)) { | |
443 | stack; | |
444 | return ECMD_FAILED; | |
445 | } | |
446 | ||
34fadac4 AK |
447 | switch(lp->percent) { |
448 | case PERCENT_VG: | |
b18e1fd5 | 449 | lp->extents = percent_of_extents(lp->extents, vg->extent_count); |
34fadac4 AK |
450 | break; |
451 | case PERCENT_FREE: | |
b18e1fd5 | 452 | lp->extents = percent_of_extents(lp->extents, vg->free_count); |
34fadac4 AK |
453 | break; |
454 | case PERCENT_LV: | |
b18e1fd5 | 455 | lp->extents = percent_of_extents(lp->extents, lv->le_count); |
34fadac4 | 456 | break; |
dfef7f69 | 457 | case PERCENT_PVS: |
ba3851fd MB |
458 | if (lp->argc) { |
459 | pv_extent_count = pv_list_extents_free(pvh); | |
b18e1fd5 | 460 | lp->extents = percent_of_extents(lp->extents, pv_extent_count); |
ba3851fd | 461 | } else |
b18e1fd5 | 462 | lp->extents = percent_of_extents(lp->extents, vg->extent_count); |
dfef7f69 | 463 | break; |
5bc2af26 MS |
464 | case PERCENT_ORIGIN: |
465 | if (!lv_is_cow(lv)) { | |
466 | log_error("Specified LV does not have an origin LV."); | |
467 | return EINVALID_CMD_LINE; | |
468 | } | |
b18e1fd5 | 469 | lp->extents = percent_of_extents(lp->extents, origin_from_cow(lv)->le_count); |
5bc2af26 | 470 | break; |
34fadac4 AK |
471 | case PERCENT_NONE: |
472 | break; | |
473 | } | |
474 | ||
4079a8f2 ZK |
475 | if (lp->sign == SIGN_PLUS) { |
476 | if (lp->extents >= (MAX_EXTENT_COUNT - lv->le_count)) { | |
477 | log_error("Unable to extend %s by %u extents, exceeds limit (%u).", | |
478 | lp->lv_name, lv->le_count, MAX_EXTENT_COUNT); | |
479 | return EINVALID_CMD_LINE; | |
480 | } | |
241913fe | 481 | lp->extents += lv->le_count; |
4079a8f2 | 482 | } |
03a8a07d | 483 | |
241913fe AK |
484 | if (lp->sign == SIGN_MINUS) { |
485 | if (lp->extents >= lv->le_count) { | |
03a8a07d | 486 | log_error("Unable to reduce %s below 1 extent", |
241913fe | 487 | lp->lv_name); |
7f0dc9c4 | 488 | return EINVALID_CMD_LINE; |
03a8a07d AK |
489 | } |
490 | ||
241913fe | 491 | lp->extents = lv->le_count - lp->extents; |
03a8a07d AK |
492 | } |
493 | ||
241913fe | 494 | if (!lp->extents) { |
03a8a07d | 495 | log_error("New size of 0 not permitted"); |
7f0dc9c4 | 496 | return EINVALID_CMD_LINE; |
03a8a07d AK |
497 | } |
498 | ||
241913fe | 499 | if (lp->extents == lv->le_count) { |
a341cab7 PR |
500 | if (use_policy) |
501 | return ECMD_PROCESSED; /* Nothing to do. */ | |
c8669f6b ZK |
502 | if (!lp->resizefs) { |
503 | log_error("New size (%d extents) matches existing size " | |
504 | "(%d extents)", lp->extents, lv->le_count); | |
505 | return EINVALID_CMD_LINE; | |
506 | } | |
507 | lp->resize = LV_EXTEND; /* lets pretend zero size extension */ | |
03a8a07d AK |
508 | } |
509 | ||
241913fe | 510 | seg_size = lp->extents - lv->le_count; |
25b73380 | 511 | |
1832f310 | 512 | /* Use segment type of last segment */ |
2c44337b | 513 | dm_list_iterate_items(seg, &lv->segments) { |
241913fe | 514 | lp->segtype = seg->segtype; |
1832f310 AK |
515 | } |
516 | ||
517 | /* FIXME Support LVs with mixed segment types */ | |
54d7741a AK |
518 | if (lp->segtype != get_segtype_from_string(cmd, arg_str_value(cmd, type_ARG, |
519 | lp->segtype->name))) { | |
241913fe | 520 | log_error("VolumeType does not match (%s)", lp->segtype->name); |
7f0dc9c4 | 521 | return EINVALID_CMD_LINE; |
1832f310 AK |
522 | } |
523 | ||
1485ce69 AK |
524 | /* If extending, find mirrors of last segment */ |
525 | if ((lp->extents > lv->le_count)) { | |
a80192b6 JEB |
526 | /* |
527 | * Has the user specified that they would like the additional | |
528 | * extents of a mirror not to have an initial sync? | |
529 | */ | |
530 | if (seg_is_mirrored(first_seg(lv)) && arg_count(cmd, nosync_ARG)) | |
531 | lv->status |= LV_NOTSYNCED; | |
532 | ||
1485ce69 AK |
533 | dm_list_iterate_back_items(mirr_seg, &lv->segments) { |
534 | if (seg_is_mirrored(mirr_seg)) | |
535 | seg_mirrors = lv_mirror_count(mirr_seg->lv); | |
536 | else | |
537 | seg_mirrors = 0; | |
538 | break; | |
539 | } | |
540 | if (!arg_count(cmd, mirrors_ARG) && seg_mirrors) { | |
541 | log_print("Extending %" PRIu32 " mirror images.", | |
542 | seg_mirrors); | |
543 | lp->mirrors = seg_mirrors; | |
544 | } | |
545 | if ((arg_count(cmd, mirrors_ARG) || seg_mirrors) && | |
546 | (lp->mirrors != seg_mirrors)) { | |
547 | log_error("Cannot vary number of mirrors in LV yet."); | |
548 | return EINVALID_CMD_LINE; | |
549 | } | |
550 | } | |
551 | ||
52dc2139 | 552 | /* If extending, find stripes, stripesize & size of last segment */ |
241913fe AK |
553 | if ((lp->extents > lv->le_count) && |
554 | !(lp->stripes == 1 || (lp->stripes > 1 && lp->stripe_size))) { | |
1485ce69 AK |
555 | /* FIXME Don't assume mirror seg will always be AREA_LV */ |
556 | dm_list_iterate_items(seg, seg_mirrors ? &seg_lv(mirr_seg, 0)->segments : &lv->segments) { | |
32469fb2 | 557 | if (!seg_is_striped(seg)) |
1832f310 | 558 | continue; |
b8c919b4 | 559 | |
579944d3 | 560 | sz = seg->stripe_size; |
b8c919b4 | 561 | str = seg->area_count; |
52dc2139 | 562 | |
bc175d28 | 563 | if ((seg_stripesize && seg_stripesize != sz && |
1485ce69 | 564 | sz && !lp->stripe_size) || |
241913fe | 565 | (seg_stripes && seg_stripes != str && !lp->stripes)) { |
52dc2139 AK |
566 | log_error("Please specify number of " |
567 | "stripes (-i) and stripesize (-I)"); | |
7f0dc9c4 | 568 | return EINVALID_CMD_LINE; |
52dc2139 AK |
569 | } |
570 | ||
571 | seg_stripesize = sz; | |
572 | seg_stripes = str; | |
573 | } | |
574 | ||
241913fe AK |
575 | if (!lp->stripes) |
576 | lp->stripes = seg_stripes; | |
52dc2139 | 577 | |
241913fe | 578 | if (!lp->stripe_size && lp->stripes > 1) { |
25b73380 | 579 | if (seg_stripesize) { |
12de747d | 580 | log_print("Using stripesize of last segment %s", |
72b2cb61 | 581 | display_size(cmd, (uint64_t) seg_stripesize)); |
241913fe | 582 | lp->stripe_size = seg_stripesize; |
25b73380 | 583 | } else { |
7f0dc9c4 | 584 | lp->stripe_size = |
2293567c | 585 | find_config_tree_int(cmd, |
8ef2b021 | 586 | "metadata/stripesize", |
8ef2b021 | 587 | DEFAULT_STRIPESIZE) * 2; |
12de747d | 588 | log_print("Using default stripesize %s", |
72b2cb61 | 589 | display_size(cmd, (uint64_t) lp->stripe_size)); |
25b73380 AK |
590 | } |
591 | } | |
52dc2139 AK |
592 | } |
593 | ||
594 | /* If reducing, find stripes, stripesize & size of last segment */ | |
241913fe | 595 | if (lp->extents < lv->le_count) { |
25b73380 | 596 | extents_used = 0; |
52dc2139 | 597 | |
ffb0e538 AK |
598 | if (lp->stripes || lp->stripe_size || lp->mirrors) |
599 | log_error("Ignoring stripes, stripesize and mirrors " | |
600 | "arguments when reducing"); | |
52dc2139 | 601 | |
2c44337b | 602 | dm_list_iterate_items(seg, &lv->segments) { |
579944d3 AK |
603 | seg_extents = seg->len; |
604 | ||
f4ba9c5d MB |
605 | /* Check for underlying stripe sizes */ |
606 | seg_stripes = lvseg_get_stripes(seg, &seg_stripesize); | |
52dc2139 | 607 | |
ffb0e538 | 608 | if (seg_is_mirrored(seg)) |
31e9db26 | 609 | seg_mirrors = lv_mirror_count(seg->lv); |
ffb0e538 AK |
610 | else |
611 | seg_mirrors = 0; | |
612 | ||
241913fe | 613 | if (lp->extents <= extents_used + seg_extents) |
52dc2139 AK |
614 | break; |
615 | ||
616 | extents_used += seg_extents; | |
617 | } | |
618 | ||
241913fe AK |
619 | seg_size = lp->extents - extents_used; |
620 | lp->stripe_size = seg_stripesize; | |
621 | lp->stripes = seg_stripes; | |
ffb0e538 | 622 | lp->mirrors = seg_mirrors; |
52dc2139 AK |
623 | } |
624 | ||
241913fe | 625 | if (lp->stripes > 1 && !lp->stripe_size) { |
25b73380 | 626 | log_error("Stripesize for striped segment should not be 0!"); |
7f0dc9c4 | 627 | return EINVALID_CMD_LINE; |
d4e5f63e | 628 | } |
5a52dca9 | 629 | |
851b1a96 | 630 | if (lp->stripes > 1) { |
241913fe | 631 | if (!(stripesize_extents = lp->stripe_size / vg->extent_size)) |
d4e5f63e AK |
632 | stripesize_extents = 1; |
633 | ||
851b1a96 | 634 | size_rest = seg_size % (lp->stripes * stripesize_extents); |
44dfb51f PR |
635 | /* Round toward the original size. */ |
636 | if (size_rest && lp->extents < lv->le_count) { | |
851b1a96 MB |
637 | log_print("Rounding size (%d extents) up to stripe " |
638 | "boundary size for segment (%d extents)", | |
6f14cd22 MB |
639 | lp->extents, lp->extents - size_rest + |
640 | (lp->stripes * stripesize_extents)); | |
641 | lp->extents = lp->extents - size_rest + | |
642 | (lp->stripes * stripesize_extents); | |
851b1a96 | 643 | } else if (size_rest) { |
d4e5f63e AK |
644 | log_print("Rounding size (%d extents) down to stripe " |
645 | "boundary size for segment (%d extents)", | |
241913fe AK |
646 | lp->extents, lp->extents - size_rest); |
647 | lp->extents = lp->extents - size_rest; | |
d4e5f63e | 648 | } |
12de747d AK |
649 | |
650 | if (lp->stripe_size < STRIPE_SIZE_MIN) { | |
651 | log_error("Invalid stripe size %s", | |
72b2cb61 | 652 | display_size(cmd, (uint64_t) lp->stripe_size)); |
406a9754 | 653 | return EINVALID_CMD_LINE; |
12de747d | 654 | } |
da4e57f2 | 655 | } |
52dc2139 | 656 | |
241913fe AK |
657 | if (lp->extents < lv->le_count) { |
658 | if (lp->resize == LV_EXTEND) { | |
03a8a07d AK |
659 | log_error("New size given (%d extents) not larger " |
660 | "than existing size (%d extents)", | |
241913fe | 661 | lp->extents, lv->le_count); |
7f0dc9c4 | 662 | return EINVALID_CMD_LINE; |
c8669f6b ZK |
663 | } |
664 | lp->resize = LV_REDUCE; | |
665 | } else if (lp->extents > lv->le_count) { | |
241913fe | 666 | if (lp->resize == LV_REDUCE) { |
03a8a07d | 667 | log_error("New size given (%d extents) not less than " |
241913fe | 668 | "existing size (%d extents)", lp->extents, |
03a8a07d | 669 | lv->le_count); |
7f0dc9c4 | 670 | return EINVALID_CMD_LINE; |
c8669f6b ZK |
671 | } |
672 | lp->resize = LV_EXTEND; | |
03a8a07d AK |
673 | } |
674 | ||
864de9ce AK |
675 | if (lv_is_origin(lv)) { |
676 | if (lp->resize == LV_REDUCE) { | |
677 | log_error("Snapshot origin volumes cannot be reduced " | |
678 | "in size yet."); | |
679 | return ECMD_FAILED; | |
680 | } | |
681 | ||
682 | memset(&info, 0, sizeof(info)); | |
683 | ||
2d6fcbf6 | 684 | if (lv_info(cmd, lv, 0, &info, 0, 0) && info.exists) { |
864de9ce AK |
685 | log_error("Snapshot origin volumes can be resized " |
686 | "only while inactive: try lvchange -an"); | |
687 | return ECMD_FAILED; | |
688 | } | |
689 | } | |
690 | ||
daa10ad0 ZK |
691 | if (lv_is_thin_pool(lv)) { |
692 | if (lp->resize == LV_REDUCE) { | |
693 | log_error("Thin pool volumes cannot be reduced in size yet."); | |
694 | return ECMD_FAILED; | |
695 | } | |
696 | ||
697 | if (lp->resizefs) { | |
698 | log_warn("Thin pool volumes do not have filesystem."); | |
699 | lp->resizefs = 0; | |
700 | } | |
701 | ||
702 | if (!lp->stripes) { | |
703 | /* Try to use the same strip settings for underlying pool data LV */ | |
704 | lp->stripes = last_seg(seg_lv(first_seg(lv), 0))->area_count; | |
705 | if (!lp->stripe_size) | |
706 | lp->stripe_size = last_seg(seg_lv(first_seg(lv), 0))->stripe_size; | |
707 | } | |
708 | } | |
709 | ||
bc175d28 AK |
710 | if ((lp->resize == LV_REDUCE) && lp->argc) |
711 | log_warn("Ignoring PVs on command line when reducing"); | |
03a8a07d | 712 | |
bc175d28 AK |
713 | /* Request confirmation before operations that are often mistakes. */ |
714 | if ((lp->resizefs || (lp->resize == LV_REDUCE)) && | |
715 | !_request_confirmation(cmd, vg, lv, lp)) { | |
716 | stack; | |
2b238642 | 717 | return ECMD_FAILED; |
bc175d28 | 718 | } |
03a8a07d | 719 | |
1a9ea74d | 720 | if (lp->resizefs) { |
bc175d28 | 721 | if (!lp->nofsck && |
2955b913 ZK |
722 | !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_CHECK, &status)) { |
723 | if (status != FSADM_CHECK_FAILS_FOR_MOUNTED) { | |
da1350d4 | 724 | log_error("Filesystem check failed."); |
2955b913 ZK |
725 | return ECMD_FAILED; |
726 | } | |
da1350d4 | 727 | /* some filesystems supports online resize */ |
c8669f6b ZK |
728 | } |
729 | ||
bc175d28 | 730 | if ((lp->resize == LV_REDUCE) && |
2955b913 | 731 | !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_RESIZE, NULL)) { |
da1350d4 | 732 | log_error("Filesystem resize failed."); |
c8669f6b ZK |
733 | return ECMD_FAILED; |
734 | } | |
1a9ea74d | 735 | } |
614a4508 | 736 | |
1a9ea74d AK |
737 | if (!archive(vg)) { |
738 | stack; | |
739 | return ECMD_FAILED; | |
740 | } | |
03a8a07d | 741 | |
1a9ea74d AK |
742 | log_print("%sing logical volume %s to %s", |
743 | (lp->resize == LV_REDUCE) ? "Reduc" : "Extend", | |
744 | lp->lv_name, | |
72b2cb61 | 745 | display_size(cmd, (uint64_t) lp->extents * vg->extent_size)); |
1a9ea74d AK |
746 | |
747 | if (lp->resize == LV_REDUCE) { | |
32469fb2 | 748 | if (!lv_reduce(lv, lv->le_count - lp->extents)) { |
7f0dc9c4 AK |
749 | stack; |
750 | return ECMD_FAILED; | |
241913fe | 751 | } |
bc175d28 | 752 | } else if ((lp->extents > lv->le_count) && /* Ensure we extend */ |
fe93c99a JEB |
753 | !lv_extend(lv, lp->segtype, |
754 | lp->stripes, lp->stripe_size, | |
755 | lp->mirrors, first_seg(lv)->region_size, | |
9ac61d2b | 756 | lp->extents - lv->le_count, NULL, |
fe93c99a | 757 | pvh, alloc)) { |
1a9ea74d AK |
758 | stack; |
759 | return ECMD_FAILED; | |
03a8a07d AK |
760 | } |
761 | ||
03a8a07d | 762 | /* store vg on disk(s) */ |
25b73380 | 763 | if (!vg_write(vg)) { |
914c9723 | 764 | stack; |
7f0dc9c4 | 765 | return ECMD_FAILED; |
cc8b2cd7 | 766 | } |
03a8a07d | 767 | |
914c9723 | 768 | /* If snapshot, must suspend all associated devices */ |
2cd0aa72 AK |
769 | if (lv_is_cow(lv)) |
770 | lock_lv = origin_from_cow(lv); | |
914c9723 | 771 | else |
0fb173aa | 772 | lock_lv = lv; |
914c9723 | 773 | |
0fb173aa | 774 | if (!suspend_lv(cmd, lock_lv)) { |
241913fe | 775 | log_error("Failed to suspend %s", lp->lv_name); |
914c9723 | 776 | vg_revert(vg); |
8f3fd69f | 777 | backup(vg); |
7f0dc9c4 | 778 | return ECMD_FAILED; |
914c9723 AK |
779 | } |
780 | ||
781 | if (!vg_commit(vg)) { | |
782 | stack; | |
df13cf08 MS |
783 | if (!resume_lv(cmd, lock_lv)) |
784 | stack; | |
8f3fd69f | 785 | backup(vg); |
7f0dc9c4 | 786 | return ECMD_FAILED; |
914c9723 AK |
787 | } |
788 | ||
0fb173aa | 789 | if (!resume_lv(cmd, lock_lv)) { |
241913fe | 790 | log_error("Problem reactivating %s", lp->lv_name); |
8f3fd69f | 791 | backup(vg); |
7f0dc9c4 | 792 | return ECMD_FAILED; |
cc8b2cd7 | 793 | } |
03a8a07d | 794 | |
8f3fd69f MB |
795 | backup(vg); |
796 | ||
6b58c796 ZK |
797 | /* |
798 | * Update lvm pool metadata (drop messages) if the pool has been | |
799 | * resumed and do a pool active/deactivate in other case. | |
800 | * | |
801 | * Note: Active thin pool can be waiting for resize. | |
802 | * | |
803 | * FIXME: Activate only when thin volume is active | |
804 | */ | |
805 | if (lv_is_thin_pool(lv) && | |
806 | !update_pool_lv(lv, !lv_is_active(lv))) { | |
807 | stack; | |
808 | return ECMD_FAILED; | |
809 | } | |
810 | ||
241913fe | 811 | log_print("Logical volume %s successfully resized", lp->lv_name); |
03a8a07d | 812 | |
bc175d28 | 813 | if (lp->resizefs && (lp->resize == LV_EXTEND) && |
2955b913 | 814 | !_fsadm_cmd(cmd, vg, lp, FSADM_CMD_RESIZE, NULL)) { |
c8669f6b ZK |
815 | stack; |
816 | return ECMD_FAILED; | |
1a9ea74d AK |
817 | } |
818 | ||
cfb7bfc7 | 819 | return ECMD_PROCESSED; |
03a8a07d | 820 | } |
241913fe AK |
821 | |
822 | int lvresize(struct cmd_context *cmd, int argc, char **argv) | |
823 | { | |
824 | struct lvresize_params lp; | |
e5f7352b | 825 | struct volume_group *vg; |
241913fe AK |
826 | int r; |
827 | ||
828 | memset(&lp, 0, sizeof(lp)); | |
829 | ||
8a2fc586 | 830 | if (!_lvresize_params(cmd, argc, argv, &lp)) |
241913fe AK |
831 | return EINVALID_CMD_LINE; |
832 | ||
833 | log_verbose("Finding volume group %s", lp.vg_name); | |
b8b3508c DW |
834 | vg = vg_read_for_update(cmd, lp.vg_name, NULL, 0); |
835 | if (vg_read_error(vg)) { | |
077a6755 | 836 | release_vg(vg); |
d0bf2f3f | 837 | stack; |
241913fe | 838 | return ECMD_FAILED; |
d0bf2f3f | 839 | } |
241913fe | 840 | |
e5f7352b | 841 | if (!(r = _lvresize(cmd, vg, &lp))) |
241913fe AK |
842 | stack; |
843 | ||
077a6755 | 844 | unlock_and_release_vg(cmd, vg, lp.vg_name); |
241913fe AK |
845 | |
846 | return r; | |
847 | } |