]>
Commit | Line | Data |
---|---|---|
03a8a07d | 1 | /* |
1832f310 | 2 | * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved. |
6606c3ae | 3 | * Copyright (C) 2004 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 | |
9 | * of the GNU General Public License v.2. | |
03a8a07d AK |
10 | * |
11 | * You should have received a copy of the GNU 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 AK |
27 | |
28 | struct segment_type *segtype; | |
29 | ||
241913fe AK |
30 | /* size */ |
31 | uint32_t extents; | |
32 | uint64_t size; | |
33 | sign_t sign; | |
03a8a07d AK |
34 | |
35 | enum { | |
36 | LV_ANY = 0, | |
37 | LV_REDUCE = 1, | |
38 | LV_EXTEND = 2 | |
241913fe AK |
39 | } resize; |
40 | ||
1a9ea74d AK |
41 | int resizefs; |
42 | int nofsck; | |
43 | ||
241913fe AK |
44 | int argc; |
45 | char **argv; | |
46 | }; | |
47 | ||
48 | static int _read_params(struct cmd_context *cmd, int argc, char **argv, | |
49 | struct lvresize_params *lp) | |
50 | { | |
51 | const char *cmd_name; | |
52 | char *st; | |
53 | ||
54 | lp->sign = SIGN_NONE; | |
55 | lp->resize = LV_ANY; | |
03a8a07d | 56 | |
60274aba | 57 | cmd_name = command_name(cmd); |
03a8a07d | 58 | if (!strcmp(cmd_name, "lvreduce")) |
241913fe | 59 | lp->resize = LV_REDUCE; |
03a8a07d | 60 | if (!strcmp(cmd_name, "lvextend")) |
241913fe | 61 | lp->resize = LV_EXTEND; |
03a8a07d | 62 | |
6fda126d | 63 | if (arg_count(cmd, extents_ARG) + arg_count(cmd, size_ARG) != 1) { |
03a8a07d | 64 | log_error("Please specify either size or extents (not both)"); |
241913fe | 65 | return 0; |
03a8a07d AK |
66 | } |
67 | ||
6fda126d | 68 | if (arg_count(cmd, extents_ARG)) { |
241913fe AK |
69 | lp->extents = arg_uint_value(cmd, extents_ARG, 0); |
70 | lp->sign = arg_sign_value(cmd, extents_ARG, SIGN_NONE); | |
03a8a07d AK |
71 | } |
72 | ||
1832f310 | 73 | /* Size returned in kilobyte units; held in sectors */ |
6fda126d | 74 | if (arg_count(cmd, size_ARG)) { |
241913fe AK |
75 | lp->size = arg_uint64_value(cmd, size_ARG, UINT64_C(0)) * 2; |
76 | lp->sign = arg_sign_value(cmd, size_ARG, SIGN_NONE); | |
03a8a07d AK |
77 | } |
78 | ||
241913fe | 79 | if (lp->resize == LV_EXTEND && lp->sign == SIGN_MINUS) { |
03a8a07d | 80 | log_error("Negative argument not permitted - use lvreduce"); |
241913fe | 81 | return 0; |
03a8a07d AK |
82 | } |
83 | ||
241913fe | 84 | if (lp->resize == LV_REDUCE && lp->sign == SIGN_PLUS) { |
03a8a07d | 85 | log_error("Positive sign not permitted - use lvextend"); |
241913fe | 86 | return 0; |
03a8a07d AK |
87 | } |
88 | ||
1a9ea74d AK |
89 | lp->resizefs = arg_count(cmd, resizefs_ARG) ? 1 : 0; |
90 | lp->nofsck = arg_count(cmd, nofsck_ARG) ? 1 : 0; | |
91 | ||
03a8a07d AK |
92 | if (!argc) { |
93 | log_error("Please provide the logical volume name"); | |
241913fe | 94 | return 0; |
03a8a07d AK |
95 | } |
96 | ||
241913fe | 97 | lp->lv_name = argv[0]; |
03a8a07d AK |
98 | argv++; |
99 | argc--; | |
100 | ||
241913fe | 101 | if (!(lp->vg_name = extract_vgname(cmd, lp->lv_name))) { |
03a8a07d | 102 | log_error("Please provide a volume group name"); |
241913fe | 103 | return 0; |
03a8a07d AK |
104 | } |
105 | ||
241913fe AK |
106 | if ((st = strrchr(lp->lv_name, '/'))) |
107 | lp->lv_name = st + 1; | |
03a8a07d | 108 | |
241913fe AK |
109 | lp->argc = argc; |
110 | lp->argv = argv; | |
111 | ||
112 | return 1; | |
113 | } | |
7d0e6e80 | 114 | |
241913fe AK |
115 | static int _lvresize(struct cmd_context *cmd, struct lvresize_params *lp) |
116 | { | |
117 | struct volume_group *vg; | |
118 | struct logical_volume *lv; | |
072893aa | 119 | struct lv_segment *snap_seg; |
241913fe AK |
120 | struct lvinfo info; |
121 | uint32_t stripesize_extents = 0; | |
122 | uint32_t seg_stripes = 0, seg_stripesize = 0, seg_size = 0; | |
ffb0e538 | 123 | uint32_t seg_mirrors = 0; |
241913fe AK |
124 | uint32_t extents_used = 0; |
125 | uint32_t size_rest; | |
a0a23eff | 126 | alloc_policy_t alloc; |
0fb173aa | 127 | struct logical_volume *lock_lv; |
241913fe AK |
128 | struct lv_list *lvl; |
129 | int consistent = 1; | |
130 | struct lv_segment *seg; | |
131 | uint32_t seg_extents; | |
132 | uint32_t sz, str; | |
7f0dc9c4 | 133 | struct list *pvh = NULL; |
1a9ea74d AK |
134 | char size_buf[SIZE_BUF]; |
135 | char lv_path[PATH_MAX]; | |
241913fe AK |
136 | |
137 | if (!(vg = vg_read(cmd, lp->vg_name, &consistent))) { | |
138 | log_error("Volume group %s doesn't exist", lp->vg_name); | |
7f0dc9c4 | 139 | return ECMD_FAILED; |
03a8a07d AK |
140 | } |
141 | ||
6fda126d AK |
142 | if (vg->status & EXPORTED_VG) { |
143 | log_error("Volume group %s is exported", vg->name); | |
7f0dc9c4 | 144 | return ECMD_FAILED; |
6fda126d | 145 | } |
f53c6aa6 | 146 | |
6fda126d | 147 | if (!(vg->status & LVM_WRITE)) { |
241913fe | 148 | log_error("Volume group %s is read-only", lp->vg_name); |
7f0dc9c4 | 149 | return ECMD_FAILED; |
6fda126d | 150 | } |
f53c6aa6 | 151 | |
03a8a07d | 152 | /* does LV exist? */ |
241913fe | 153 | if (!(lvl = find_lv_in_vg(vg, lp->lv_name))) { |
03a8a07d | 154 | log_error("Logical volume %s not found in volume group %s", |
241913fe | 155 | lp->lv_name, lp->vg_name); |
7f0dc9c4 | 156 | return ECMD_FAILED; |
03a8a07d AK |
157 | } |
158 | ||
25b73380 AK |
159 | if (arg_count(cmd, stripes_ARG)) { |
160 | if (vg->fid->fmt->features & FMT_SEGMENTS) | |
241913fe | 161 | lp->stripes = arg_uint_value(cmd, stripes_ARG, 1); |
25b73380 AK |
162 | else |
163 | log_print("Varied striping not supported. Ignoring."); | |
164 | } | |
165 | ||
ffb0e538 AK |
166 | if (arg_count(cmd, mirrors_ARG)) { |
167 | if (vg->fid->fmt->features & FMT_SEGMENTS) | |
168 | lp->mirrors = arg_uint_value(cmd, mirrors_ARG, 1) + 1; | |
169 | else | |
170 | log_print("Mirrors not supported. Ignoring."); | |
7424de5b AK |
171 | if (arg_sign_value(cmd, mirrors_ARG, 0) == SIGN_MINUS) { |
172 | log_error("Mirrors argument may not be negative"); | |
173 | return 0; | |
174 | } | |
ffb0e538 AK |
175 | } |
176 | ||
25b73380 | 177 | if (arg_count(cmd, stripesize_ARG)) { |
fdd4f3c0 AK |
178 | if (arg_sign_value(cmd, stripesize_ARG, 0) == SIGN_MINUS) { |
179 | log_error("Stripesize may not be negative."); | |
7f0dc9c4 | 180 | return ECMD_FAILED; |
fdd4f3c0 | 181 | } |
25b73380 | 182 | if (vg->fid->fmt->features & FMT_SEGMENTS) |
241913fe AK |
183 | lp->stripe_size = 2 * arg_uint_value(cmd, |
184 | stripesize_ARG, 0); | |
25b73380 AK |
185 | else |
186 | log_print("Varied stripesize not supported. Ignoring."); | |
ffb0e538 AK |
187 | if (lp->mirrors) { |
188 | log_error("Mirrors and striping cannot be combined yet."); | |
189 | return ECMD_FAILED; | |
190 | } | |
25b73380 AK |
191 | } |
192 | ||
f868d635 | 193 | lv = lvl->lv; |
03a8a07d | 194 | |
6e03b44c AK |
195 | if (lv->status & LOCKED) { |
196 | log_error("Can't resize locked LV %s", lv->name); | |
7f0dc9c4 | 197 | return ECMD_FAILED; |
6e03b44c AK |
198 | } |
199 | ||
a0a23eff | 200 | alloc = (alloc_policy_t) arg_uint_value(cmd, alloc_ARG, lv->alloc); |
7f0dc9c4 | 201 | |
241913fe AK |
202 | if (lp->size) { |
203 | if (lp->size % vg->extent_size) { | |
204 | if (lp->sign == SIGN_MINUS) | |
205 | lp->size -= lp->size % vg->extent_size; | |
03a8a07d | 206 | else |
241913fe AK |
207 | lp->size += vg->extent_size - |
208 | (lp->size % vg->extent_size); | |
03a8a07d AK |
209 | |
210 | log_print("Rounding up size to full physical extent %s", | |
30bab85b | 211 | display_size(cmd, (uint64_t) lp->size, |
8ef2b021 | 212 | SIZE_SHORT)); |
03a8a07d AK |
213 | } |
214 | ||
241913fe | 215 | lp->extents = lp->size / vg->extent_size; |
03a8a07d AK |
216 | } |
217 | ||
241913fe AK |
218 | if (lp->sign == SIGN_PLUS) |
219 | lp->extents += lv->le_count; | |
03a8a07d | 220 | |
241913fe AK |
221 | if (lp->sign == SIGN_MINUS) { |
222 | if (lp->extents >= lv->le_count) { | |
03a8a07d | 223 | log_error("Unable to reduce %s below 1 extent", |
241913fe | 224 | lp->lv_name); |
7f0dc9c4 | 225 | return EINVALID_CMD_LINE; |
03a8a07d AK |
226 | } |
227 | ||
241913fe | 228 | lp->extents = lv->le_count - lp->extents; |
03a8a07d AK |
229 | } |
230 | ||
241913fe | 231 | if (!lp->extents) { |
03a8a07d | 232 | log_error("New size of 0 not permitted"); |
7f0dc9c4 | 233 | return EINVALID_CMD_LINE; |
03a8a07d AK |
234 | } |
235 | ||
241913fe | 236 | if (lp->extents == lv->le_count) { |
03a8a07d | 237 | log_error("New size (%d extents) matches existing size " |
241913fe | 238 | "(%d extents)", lp->extents, lv->le_count); |
7f0dc9c4 | 239 | return EINVALID_CMD_LINE; |
03a8a07d AK |
240 | } |
241 | ||
241913fe | 242 | seg_size = lp->extents - lv->le_count; |
25b73380 | 243 | |
1832f310 AK |
244 | /* Use segment type of last segment */ |
245 | list_iterate_items(seg, &lv->segments) { | |
241913fe | 246 | lp->segtype = seg->segtype; |
1832f310 AK |
247 | } |
248 | ||
249 | /* FIXME Support LVs with mixed segment types */ | |
241913fe AK |
250 | if (lp->segtype != (struct segment_type *) arg_ptr_value(cmd, type_ARG, |
251 | lp->segtype)) { | |
252 | log_error("VolumeType does not match (%s)", lp->segtype->name); | |
7f0dc9c4 | 253 | return EINVALID_CMD_LINE; |
1832f310 AK |
254 | } |
255 | ||
52dc2139 | 256 | /* If extending, find stripes, stripesize & size of last segment */ |
241913fe AK |
257 | if ((lp->extents > lv->le_count) && |
258 | !(lp->stripes == 1 || (lp->stripes > 1 && lp->stripe_size))) { | |
f2b7349e | 259 | list_iterate_items(seg, &lv->segments) { |
32469fb2 | 260 | if (!seg_is_striped(seg)) |
1832f310 | 261 | continue; |
b8c919b4 | 262 | |
579944d3 | 263 | sz = seg->stripe_size; |
b8c919b4 | 264 | str = seg->area_count; |
52dc2139 | 265 | |
da4e57f2 | 266 | if ((seg_stripesize && seg_stripesize != sz |
241913fe AK |
267 | && !lp->stripe_size) || |
268 | (seg_stripes && seg_stripes != str && !lp->stripes)) { | |
52dc2139 AK |
269 | log_error("Please specify number of " |
270 | "stripes (-i) and stripesize (-I)"); | |
7f0dc9c4 | 271 | return EINVALID_CMD_LINE; |
52dc2139 AK |
272 | } |
273 | ||
274 | seg_stripesize = sz; | |
275 | seg_stripes = str; | |
276 | } | |
277 | ||
241913fe AK |
278 | if (!lp->stripes) |
279 | lp->stripes = seg_stripes; | |
52dc2139 | 280 | |
241913fe | 281 | if (!lp->stripe_size && lp->stripes > 1) { |
25b73380 AK |
282 | if (seg_stripesize) { |
283 | log_print("Using stripesize of last segment " | |
284 | "%dKB", seg_stripesize / 2); | |
241913fe | 285 | lp->stripe_size = seg_stripesize; |
25b73380 | 286 | } else { |
7f0dc9c4 AK |
287 | lp->stripe_size = |
288 | find_config_int(cmd->cft->root, | |
8ef2b021 | 289 | "metadata/stripesize", |
8ef2b021 | 290 | DEFAULT_STRIPESIZE) * 2; |
25b73380 | 291 | log_print("Using default stripesize %dKB", |
241913fe | 292 | lp->stripe_size / 2); |
25b73380 AK |
293 | } |
294 | } | |
52dc2139 AK |
295 | } |
296 | ||
ffb0e538 AK |
297 | /* If extending, find mirrors of last segment */ |
298 | if ((lp->extents > lv->le_count)) { | |
299 | list_iterate_back_items(seg, &lv->segments) { | |
300 | if (seg_is_mirrored(seg)) | |
301 | seg_mirrors = seg->area_count; | |
302 | else | |
303 | seg_mirrors = 0; | |
304 | break; | |
305 | } | |
306 | if (!arg_count(cmd, mirrors_ARG) && seg_mirrors) { | |
307 | log_print("Extending %" PRIu32 " mirror images.", | |
308 | seg_mirrors); | |
309 | lp->mirrors = seg_mirrors; | |
310 | } | |
311 | if ((arg_count(cmd, mirrors_ARG) || seg_mirrors) && | |
312 | (lp->mirrors != seg_mirrors)) { | |
313 | log_error("Cannot vary number of mirrors in LV yet."); | |
314 | return EINVALID_CMD_LINE; | |
315 | } | |
316 | } | |
317 | ||
52dc2139 | 318 | /* If reducing, find stripes, stripesize & size of last segment */ |
241913fe | 319 | if (lp->extents < lv->le_count) { |
25b73380 | 320 | extents_used = 0; |
52dc2139 | 321 | |
ffb0e538 AK |
322 | if (lp->stripes || lp->stripe_size || lp->mirrors) |
323 | log_error("Ignoring stripes, stripesize and mirrors " | |
324 | "arguments when reducing"); | |
52dc2139 | 325 | |
f2b7349e | 326 | list_iterate_items(seg, &lv->segments) { |
579944d3 AK |
327 | seg_extents = seg->len; |
328 | ||
32469fb2 | 329 | if (seg_is_striped(seg)) { |
b8c919b4 AK |
330 | seg_stripesize = seg->stripe_size; |
331 | seg_stripes = seg->area_count; | |
332 | } | |
52dc2139 | 333 | |
ffb0e538 AK |
334 | if (seg_is_mirrored(seg)) |
335 | seg_mirrors = seg->area_count; | |
336 | else | |
337 | seg_mirrors = 0; | |
338 | ||
241913fe | 339 | if (lp->extents <= extents_used + seg_extents) |
52dc2139 AK |
340 | break; |
341 | ||
342 | extents_used += seg_extents; | |
343 | } | |
344 | ||
241913fe AK |
345 | seg_size = lp->extents - extents_used; |
346 | lp->stripe_size = seg_stripesize; | |
347 | lp->stripes = seg_stripes; | |
ffb0e538 | 348 | lp->mirrors = seg_mirrors; |
52dc2139 AK |
349 | } |
350 | ||
241913fe | 351 | if (lp->stripes > 1 && !lp->stripe_size) { |
25b73380 | 352 | log_error("Stripesize for striped segment should not be 0!"); |
7f0dc9c4 | 353 | return EINVALID_CMD_LINE; |
d4e5f63e | 354 | } |
5a52dca9 | 355 | |
241913fe AK |
356 | if ((lp->stripes > 1)) { |
357 | if (!(stripesize_extents = lp->stripe_size / vg->extent_size)) | |
d4e5f63e AK |
358 | stripesize_extents = 1; |
359 | ||
241913fe | 360 | if ((size_rest = seg_size % (lp->stripes * stripesize_extents))) { |
d4e5f63e AK |
361 | log_print("Rounding size (%d extents) down to stripe " |
362 | "boundary size for segment (%d extents)", | |
241913fe AK |
363 | lp->extents, lp->extents - size_rest); |
364 | lp->extents = lp->extents - size_rest; | |
d4e5f63e | 365 | } |
da4e57f2 | 366 | } |
52dc2139 | 367 | |
241913fe | 368 | if (lp->extents == lv->le_count) { |
52dc2139 | 369 | log_error("New size (%d extents) matches existing size " |
241913fe | 370 | "(%d extents)", lp->extents, lv->le_count); |
7f0dc9c4 | 371 | return EINVALID_CMD_LINE; |
52dc2139 AK |
372 | } |
373 | ||
241913fe AK |
374 | if (lp->extents < lv->le_count) { |
375 | if (lp->resize == LV_EXTEND) { | |
03a8a07d AK |
376 | log_error("New size given (%d extents) not larger " |
377 | "than existing size (%d extents)", | |
241913fe | 378 | lp->extents, lv->le_count); |
7f0dc9c4 | 379 | return EINVALID_CMD_LINE; |
03a8a07d | 380 | } else |
241913fe | 381 | lp->resize = LV_REDUCE; |
03a8a07d AK |
382 | } |
383 | ||
241913fe AK |
384 | if (lp->extents > lv->le_count) { |
385 | if (lp->resize == LV_REDUCE) { | |
03a8a07d | 386 | log_error("New size given (%d extents) not less than " |
241913fe | 387 | "existing size (%d extents)", lp->extents, |
03a8a07d | 388 | lv->le_count); |
7f0dc9c4 | 389 | return EINVALID_CMD_LINE; |
03a8a07d | 390 | } else |
241913fe | 391 | lp->resize = LV_EXTEND; |
03a8a07d AK |
392 | } |
393 | ||
f5190ed4 | 394 | if (lp->mirrors && activation() && |
f894b4b1 | 395 | lv_info(cmd, lv, &info, 0) && info.exists) { |
f5190ed4 AK |
396 | log_error("Mirrors cannot be resized while active yet."); |
397 | return ECMD_FAILED; | |
398 | } | |
864de9ce AK |
399 | |
400 | if (lv_is_origin(lv)) { | |
401 | if (lp->resize == LV_REDUCE) { | |
402 | log_error("Snapshot origin volumes cannot be reduced " | |
403 | "in size yet."); | |
404 | return ECMD_FAILED; | |
405 | } | |
406 | ||
407 | memset(&info, 0, sizeof(info)); | |
408 | ||
f894b4b1 | 409 | if (lv_info(cmd, lv, &info, 0) && info.exists) { |
864de9ce AK |
410 | log_error("Snapshot origin volumes can be resized " |
411 | "only while inactive: try lvchange -an"); | |
412 | return ECMD_FAILED; | |
413 | } | |
414 | } | |
415 | ||
241913fe AK |
416 | if (lp->resize == LV_REDUCE) { |
417 | if (lp->argc) | |
03a8a07d | 418 | log_print("Ignoring PVs on command line when reducing"); |
1a9ea74d | 419 | } else if (!(pvh = lp->argc ? create_pv_list(cmd->mem, vg, lp->argc, |
eabaa339 | 420 | lp->argv, 1) : &vg->pvs)) { |
1a9ea74d AK |
421 | stack; |
422 | return ECMD_FAILED; | |
423 | } | |
03a8a07d | 424 | |
1a9ea74d | 425 | if (lp->resize == LV_REDUCE || lp->resizefs) { |
25b73380 AK |
426 | memset(&info, 0, sizeof(info)); |
427 | ||
f894b4b1 | 428 | if (!lv_info(cmd, lv, &info, 1) && driver_version(NULL, 0)) { |
25b73380 | 429 | log_error("lv_info failed: aborting"); |
7f0dc9c4 | 430 | return ECMD_FAILED; |
41967a02 AK |
431 | } |
432 | ||
1a9ea74d AK |
433 | if (lp->resizefs && !info.exists) { |
434 | log_error("Logical volume %s must be activated " | |
435 | "before resizing filesystem", lp->lv_name); | |
436 | return ECMD_FAILED; | |
437 | } | |
438 | ||
439 | if (info.exists && !lp->resizefs && (lp->resize == LV_REDUCE)) { | |
03a8a07d | 440 | log_print("WARNING: Reducing active%s logical volume " |
41967a02 | 441 | "to %s", info.open_count ? " and open" : "", |
241913fe | 442 | display_size(cmd, (uint64_t) lp->extents * |
30bab85b | 443 | vg->extent_size, |
4c64ed4c | 444 | SIZE_SHORT)); |
03a8a07d AK |
445 | |
446 | log_print("THIS MAY DESTROY YOUR DATA " | |
447 | "(filesystem etc.)"); | |
03a8a07d | 448 | |
1a9ea74d AK |
449 | if (!arg_count(cmd, force_ARG)) { |
450 | if (yes_no_prompt("Do you really want to " | |
451 | "reduce %s? [y/n]: ", | |
452 | lp->lv_name) == 'n') { | |
453 | log_print("Logical volume %s NOT " | |
454 | "reduced", lp->lv_name); | |
455 | return ECMD_FAILED; | |
456 | } | |
03a8a07d AK |
457 | } |
458 | } | |
1a9ea74d | 459 | } |
03a8a07d | 460 | |
1a9ea74d AK |
461 | if (lp->resizefs) { |
462 | if (lvm_snprintf(lv_path, PATH_MAX, "%s%s/%s", cmd->dev_dir, | |
463 | lp->vg_name, lp->lv_name) < 0) { | |
464 | log_error("Couldn't create LV path for %s", | |
465 | lp->lv_name); | |
7f0dc9c4 | 466 | return ECMD_FAILED; |
241913fe | 467 | } |
614a4508 | 468 | |
1a9ea74d AK |
469 | if (lvm_snprintf(size_buf, SIZE_BUF, "%" PRIu64, |
470 | (uint64_t) lp->extents * vg->extent_size / 2) | |
471 | < 0) { | |
472 | log_error("Couldn't generate new LV size string"); | |
7f0dc9c4 | 473 | return ECMD_FAILED; |
1a9ea74d | 474 | } |
03a8a07d | 475 | |
1a9ea74d AK |
476 | if (!lp->nofsck) { |
477 | if (!exec_cmd("fsadm", "check", lv_path, NULL)) { | |
478 | stack; | |
479 | return ECMD_FAILED; | |
480 | } | |
8ef2b021 AK |
481 | } |
482 | ||
1a9ea74d AK |
483 | if (lp->resize == LV_REDUCE) { |
484 | if (!exec_cmd("fsadm", "resize", lv_path, size_buf)) { | |
485 | stack; | |
486 | return ECMD_FAILED; | |
487 | } | |
241913fe | 488 | } |
1a9ea74d | 489 | } |
614a4508 | 490 | |
1a9ea74d AK |
491 | if (!archive(vg)) { |
492 | stack; | |
493 | return ECMD_FAILED; | |
494 | } | |
03a8a07d | 495 | |
1a9ea74d AK |
496 | log_print("%sing logical volume %s to %s", |
497 | (lp->resize == LV_REDUCE) ? "Reduc" : "Extend", | |
498 | lp->lv_name, | |
499 | display_size(cmd, (uint64_t) lp->extents * vg->extent_size, | |
500 | SIZE_SHORT)); | |
501 | ||
502 | if (lp->resize == LV_REDUCE) { | |
32469fb2 | 503 | if (!lv_reduce(lv, lv->le_count - lp->extents)) { |
7f0dc9c4 AK |
504 | stack; |
505 | return ECMD_FAILED; | |
241913fe | 506 | } |
32469fb2 | 507 | } else if (!lv_extend(lv, lp->segtype, lp->stripes, |
ffb0e538 | 508 | lp->stripe_size, lp->mirrors, |
32469fb2 AK |
509 | lp->extents - lv->le_count, |
510 | NULL, 0u, 0u, pvh, alloc)) { | |
1a9ea74d AK |
511 | stack; |
512 | return ECMD_FAILED; | |
03a8a07d AK |
513 | } |
514 | ||
03a8a07d | 515 | /* store vg on disk(s) */ |
25b73380 | 516 | if (!vg_write(vg)) { |
914c9723 | 517 | stack; |
7f0dc9c4 | 518 | return ECMD_FAILED; |
cc8b2cd7 | 519 | } |
03a8a07d | 520 | |
6fda126d | 521 | backup(vg); |
cc219483 | 522 | |
914c9723 | 523 | /* If snapshot, must suspend all associated devices */ |
072893aa | 524 | if ((snap_seg = find_cow(lv))) |
0fb173aa | 525 | lock_lv = snap_seg->origin; |
914c9723 | 526 | else |
0fb173aa | 527 | lock_lv = lv; |
914c9723 | 528 | |
0fb173aa | 529 | if (!suspend_lv(cmd, lock_lv)) { |
241913fe | 530 | log_error("Failed to suspend %s", lp->lv_name); |
914c9723 | 531 | vg_revert(vg); |
7f0dc9c4 | 532 | return ECMD_FAILED; |
914c9723 AK |
533 | } |
534 | ||
535 | if (!vg_commit(vg)) { | |
536 | stack; | |
0fb173aa | 537 | resume_lv(cmd, lock_lv); |
7f0dc9c4 | 538 | return ECMD_FAILED; |
914c9723 AK |
539 | } |
540 | ||
0fb173aa | 541 | if (!resume_lv(cmd, lock_lv)) { |
241913fe | 542 | log_error("Problem reactivating %s", lp->lv_name); |
7f0dc9c4 | 543 | return ECMD_FAILED; |
cc8b2cd7 | 544 | } |
03a8a07d | 545 | |
241913fe | 546 | log_print("Logical volume %s successfully resized", lp->lv_name); |
03a8a07d | 547 | |
1a9ea74d AK |
548 | if (lp->resizefs && (lp->resize == LV_EXTEND)) { |
549 | if (!exec_cmd("fsadm", "resize", lv_path, size_buf)) { | |
550 | stack; | |
551 | return ECMD_FAILED; | |
552 | } | |
553 | } | |
554 | ||
cfb7bfc7 | 555 | return ECMD_PROCESSED; |
03a8a07d | 556 | } |
241913fe AK |
557 | |
558 | int lvresize(struct cmd_context *cmd, int argc, char **argv) | |
559 | { | |
560 | struct lvresize_params lp; | |
561 | int r; | |
562 | ||
563 | memset(&lp, 0, sizeof(lp)); | |
564 | ||
565 | if (!_read_params(cmd, argc, argv, &lp)) | |
566 | return EINVALID_CMD_LINE; | |
567 | ||
568 | log_verbose("Finding volume group %s", lp.vg_name); | |
569 | if (!lock_vol(cmd, lp.vg_name, LCK_VG_WRITE)) { | |
570 | log_error("Can't get lock for %s", lp.vg_name); | |
571 | return ECMD_FAILED; | |
572 | } | |
573 | ||
574 | if (!(r = _lvresize(cmd, &lp))) | |
575 | stack; | |
576 | ||
577 | unlock_vg(cmd, lp.vg_name); | |
578 | ||
579 | return r; | |
580 | } |