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