18
18
static int _move_pv(struct volume_group *vg_from, struct volume_group *vg_to,
21
struct physical_volume *pv;
21
22
struct pv_list *pvl;
22
struct physical_volume *pv;
24
/* FIXME: handle tags */
24
25
if (!(pvl = find_pv_in_vg(vg_from, pv_name))) {
25
26
log_error("Physical volume %s not in volume group %s",
26
27
pv_name, vg_from->name);
31
list_add(&vg_to->pvs, &pvl->list);
31
list_move(&vg_to->pvs, &pvl->list);
33
33
vg_from->pv_count--;
36
pv = list_item(pvl, struct pv_list)->pv;
38
vg_from->extent_count -= get_pv_pe_count(pv);
39
vg_to->extent_count += get_pv_pe_count(pv);
41
vg_from->free_count -= get_pv_pe_count(pv) - get_pv_pe_alloc_count(pv);
42
vg_to->free_count += get_pv_pe_count(pv) - get_pv_pe_alloc_count(pv);
38
vg_from->extent_count -= pv_pe_count(pv);
39
vg_to->extent_count += pv_pe_count(pv);
41
vg_from->free_count -= pv_pe_count(pv) - pv_pe_alloc_count(pv);
42
vg_to->free_count += pv_pe_count(pv) - pv_pe_alloc_count(pv);
47
static int _move_pvs_used_by_lv(struct volume_group *vg_from,
48
struct volume_group *vg_to,
51
struct lv_segment *lvseg;
54
struct logical_volume *lv;
56
/* FIXME: handle tags */
57
if (!(lvl = find_lv_in_vg(vg_from, lv_name))) {
58
log_error("Logical volume %s not in volume group %s",
59
lv_name, vg_from->name);
63
list_iterate_items(lvseg, &lvl->lv->segments) {
65
if (!_move_pvs_used_by_lv(vg_from, vg_to,
68
for (s = 0; s < lvseg->area_count; s++) {
69
if (seg_type(lvseg, s) == AREA_PV) {
70
if (!_move_pv(vg_from, vg_to,
71
pv_dev_name(seg_pv(lvseg, s))))
73
} else if (seg_type(lvseg, s) == AREA_LV) {
74
lv = seg_lv(lvseg, s);
75
if (!_move_pvs_used_by_lv(vg_from, vg_to,
201
264
if (seg_in == seg->area_count && log_in) {
203
list_add(&vg_to->lvs, lvh);
265
if (!_move_one_lv(vg_from, vg_to, lvh))
274
* Has the user given an option related to a new vg as the split destination?
276
static int new_vg_option_specified(struct cmd_context *cmd)
278
return(arg_count(cmd, clustered_ARG) ||
279
arg_count(cmd, alloc_ARG) ||
280
arg_count(cmd, maxphysicalvolumes_ARG) ||
281
arg_count(cmd, maxlogicalvolumes_ARG));
213
284
int vgsplit(struct cmd_context *cmd, int argc, char **argv)
286
struct vgcreate_params vp_new;
287
struct vgcreate_params vp_def;
215
288
char *vg_name_from, *vg_name_to;
216
289
struct volume_group *vg_to, *vg_from;
222
log_error("Existing VG, new VG and physical volumes required.");
295
if ((arg_count(cmd, name_ARG) + argc) < 3) {
296
log_error("Existing VG, new VG and either physical volumes "
297
"or logical volume required.");
223
298
return EINVALID_CMD_LINE;
301
if (arg_count(cmd, name_ARG) && (argc > 2)) {
302
log_error("A logical volume name cannot be given with "
303
"physical volumes.");
307
if (arg_count(cmd, name_ARG))
308
lv_name = arg_value(cmd, name_ARG);
226
312
vg_name_from = skip_dev_dir(cmd, argv[0], NULL);
227
313
vg_name_to = skip_dev_dir(cmd, argv[1], NULL);
231
if (!validate_name(vg_name_from)) {
232
log_error("Volume group name \"%s\" is invalid",
237
317
if (!strcmp(vg_name_to, vg_name_from)) {
238
318
log_error("Duplicate volume group name \"%s\"", vg_name_from);
239
319
return ECMD_FAILED;
242
322
log_verbose("Checking for volume group \"%s\"", vg_name_from);
243
if (!lock_vol(cmd, vg_name_from, LCK_VG_WRITE)) {
244
log_error("Can't get lock for %s", vg_name_from);
248
if (!(vg_from = vg_read(cmd, vg_name_from, NULL, &consistent)) || !consistent) {
249
log_error("Volume group \"%s\" doesn't exist", vg_name_from);
250
unlock_vg(cmd, vg_name_from);
254
if (!vg_check_status(vg_from, CLUSTERED | EXPORTED_VG |
255
RESIZEABLE_VG | LVM_WRITE)) {
256
unlock_vg(cmd, vg_name_from);
260
log_verbose("Checking for volume group \"%s\"", vg_name_to);
323
if (!(vg_from = vg_lock_and_read(cmd, vg_name_from, NULL, LCK_VG_WRITE,
324
CLUSTERED | EXPORTED_VG |
325
RESIZEABLE_VG | LVM_WRITE,
326
CORRECT_INCONSISTENT | FAIL_INCONSISTENT)))
329
log_verbose("Checking for new volume group \"%s\"", vg_name_to);
261
330
if (!lock_vol(cmd, vg_name_to, LCK_VG_WRITE | LCK_NONBLOCK)) {
262
331
log_error("Can't get lock for %s", vg_name_to);
263
332
unlock_vg(cmd, vg_name_from);
268
337
if ((vg_to = vg_read(cmd, vg_name_to, NULL, &consistent))) {
269
/* FIXME Remove this restriction */
270
log_error("Volume group \"%s\" already exists", vg_name_to);
274
if (!validate_vg_name(cmd, vg_name_to)) {
275
log_error("New volume group name \"%s\" is invalid",
280
if ((active = lvs_in_vg_activated(vg_from))) {
281
/* FIXME Remove this restriction */
282
log_error("Logical volumes in \"%s\" must be inactive",
287
/* Set metadata format of original VG */
288
/* FIXME: need some common logic */
289
cmd->fmt = vg_from->fid->fmt;
291
/* Create new VG structure */
292
if (!(vg_to = vg_create(cmd, vg_name_to, vg_from->extent_size,
293
vg_from->max_pv, vg_from->max_lv,
294
vg_from->alloc, 0, NULL)))
297
if (vg_from->status & CLUSTERED)
298
vg_to->status |= CLUSTERED;
339
if (new_vg_option_specified(cmd)) {
340
log_error("Volume group \"%s\" exists, but new VG "
341
"option specified", vg_name_to);
344
if (!vgs_are_compatible(cmd, vg_from,vg_to))
349
/* Set metadata format of original VG */
350
/* FIXME: need some common logic */
351
cmd->fmt = vg_from->fid->fmt;
353
vp_def.vg_name = NULL;
354
vp_def.extent_size = vg_from->extent_size;
355
vp_def.max_pv = vg_from->max_pv;
356
vp_def.max_lv = vg_from->max_lv;
357
vp_def.alloc = vg_from->alloc;
358
vp_def.clustered = 0;
360
if (fill_vg_create_params(cmd, vg_name_to, &vp_new, &vp_def)) {
361
unlock_vg(cmd, vg_name_from);
362
unlock_vg(cmd, vg_name_to);
363
return EINVALID_CMD_LINE;
366
if (validate_vg_create_params(cmd, &vp_new)) {
367
unlock_vg(cmd, vg_name_from);
368
unlock_vg(cmd, vg_name_to);
369
return EINVALID_CMD_LINE;
372
if (!(vg_to = vg_create(cmd, vg_name_to, vp_new.extent_size,
373
vp_new.max_pv, vp_new.max_lv,
374
vp_new.alloc, 0, NULL)))
377
if (vg_is_clustered(vg_from))
378
vg_to->status |= CLUSTERED;
300
381
/* Archive vg_from before changing it */
301
382
if (!archive(vg_from))
304
385
/* Move PVs across to new structure */
305
386
for (opt = 0; opt < argc; opt++) {
306
387
if (!_move_pv(vg_from, vg_to, argv[opt]))
391
/* If an LV given on the cmdline, move used_by PVs */
392
if (lv_name && !_move_pvs_used_by_lv(vg_from, vg_to, lv_name))
310
395
/* Move required LVs across, checking consistency */
311
396
if (!(_move_lvs(vg_from, vg_to)))
314
399
/* Move required snapshots across */
315
400
if (!(_move_snapshots(vg_from, vg_to)))
318
403
/* Move required mirrors across */
319
404
if (!(_move_mirrors(vg_from, vg_to)))
322
407
/* Split metadata areas and check if both vgs have at least one area */
323
if (!(vg_split_mdas(cmd, vg_from, vg_to))) {
408
if (!(vg_split_mdas(cmd, vg_from, vg_to)) && vg_from->pv_count) {
324
409
log_error("Cannot split: Nowhere to store metadata for new Volume Group");
328
413
/* Set proper name for all PVs in new VG */
329
414
if (!vg_rename(cmd, vg_to, vg_name_to))
332
417
/* store it on disks */
333
418
log_verbose("Writing out updated volume groups");
335
/* Write out new VG as EXPORTED */
421
* First, write out the new VG as EXPORTED. We do this first in case
422
* there is a crash - we will still have the new VG information, in an
423
* exported state. Recovery after this point would be removal of the
424
* new VG and redoing the vgsplit.
425
* FIXME: recover automatically or instruct the user?
336
427
vg_to->status |= EXPORTED_VG;
338
429
if (!archive(vg_to))
341
432
if (!vg_write(vg_to) || !vg_commit(vg_to))
346
/* Write out updated old VG */
347
if (!vg_write(vg_from) || !vg_commit(vg_from))
352
/* Remove EXPORTED flag from new VG */
438
* Next, write out the updated old VG. If we crash after this point,
439
* recovery is a vgimport on the new VG.
440
* FIXME: recover automatically or instruct the user?
442
if (vg_from->pv_count) {
443
if (!vg_write(vg_from) || !vg_commit(vg_from))
450
* Finally, remove the EXPORTED flag from the new VG and write it out.
354
if (!(vg_to = vg_read(cmd, vg_name_to, NULL, &consistent)) || !consistent) {
454
(!(vg_to = vg_read(cmd, vg_name_to, NULL, &consistent)) ||
355
456
log_error("Volume group \"%s\" became inconsistent: please "
356
457
"fix manually", vg_name_to);
360
461
vg_to->status &= ~EXPORTED_VG;
362
463
if (!vg_write(vg_to) || !vg_commit(vg_to))
367
468
unlock_vg(cmd, vg_name_from);
368
469
unlock_vg(cmd, vg_name_to);
370
log_print("Volume group \"%s\" successfully split from \"%s\"",
471
log_print("%s volume group \"%s\" successfully split from \"%s\"",
472
existing_vg ? "Existing" : "New",
371
473
vg_to->name, vg_from->name);
372
474
return ECMD_PROCESSED;
375
477
unlock_vg(cmd, vg_name_from);
376
478
unlock_vg(cmd, vg_name_to);
377
479
return ECMD_FAILED;