1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
|
/*
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
* Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
*
* This file is part of LVM2.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v.2.1.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "tools.h"
/* FIXME Why not (lv->vg == vg) ? */
static int _lv_is_in_vg(struct volume_group *vg, struct logical_volume *lv)
{
struct lv_list *lvl;
dm_list_iterate_items(lvl, &vg->lvs)
if (lv == lvl->lv)
return 1;
return 0;
}
static int _move_one_lv(struct volume_group *vg_from,
struct volume_group *vg_to,
struct dm_list *lvh)
{
struct logical_volume *lv = dm_list_item(lvh, struct lv_list)->lv;
dm_list_move(&vg_to->lvs, lvh);
lv->vg = vg_to;
if (lv_is_active(lv)) {
log_error("Logical volume \"%s\" must be inactive", lv->name);
return 0;
}
return 1;
}
static int _move_lvs(struct volume_group *vg_from, struct volume_group *vg_to)
{
struct dm_list *lvh, *lvht;
struct logical_volume *lv;
struct lv_segment *seg;
struct physical_volume *pv;
struct volume_group *vg_with;
unsigned s;
dm_list_iterate_safe(lvh, lvht, &vg_from->lvs) {
lv = dm_list_item(lvh, struct lv_list)->lv;
if ((lv->status & SNAPSHOT))
continue;
if ((lv->status & MIRRORED))
continue;
/* Ensure all the PVs used by this LV remain in the same */
/* VG as each other */
vg_with = NULL;
dm_list_iterate_items(seg, &lv->segments) {
for (s = 0; s < seg->area_count; s++) {
/* FIXME Check AREA_LV too */
if (seg_type(seg, s) != AREA_PV)
continue;
pv = seg_pv(seg, s);
if (vg_with) {
if (!pv_is_in_vg(vg_with, pv)) {
log_error("Can't split Logical "
"Volume %s between "
"two Volume Groups",
lv->name);
return 0;
}
continue;
}
if (pv_is_in_vg(vg_from, pv)) {
vg_with = vg_from;
continue;
}
if (pv_is_in_vg(vg_to, pv)) {
vg_with = vg_to;
continue;
}
log_error("Physical Volume %s not found",
pv_dev_name(pv));
return 0;
}
}
if (vg_with == vg_from)
continue;
/* Move this LV */
if (!_move_one_lv(vg_from, vg_to, lvh))
return_0;
}
/* FIXME Ensure no LVs contain segs pointing at LVs in the other VG */
return 1;
}
/*
* Move the hidden / internal "snapshotN" LVs.from 'vg_from' to 'vg_to'.
*/
static int _move_snapshots(struct volume_group *vg_from,
struct volume_group *vg_to)
{
struct dm_list *lvh, *lvht;
struct logical_volume *lv;
struct lv_segment *seg;
int cow_from = 0;
int origin_from = 0;
dm_list_iterate_safe(lvh, lvht, &vg_from->lvs) {
lv = dm_list_item(lvh, struct lv_list)->lv;
if (!(lv->status & SNAPSHOT))
continue;
dm_list_iterate_items(seg, &lv->segments) {
cow_from = _lv_is_in_vg(vg_from, seg->cow);
origin_from = _lv_is_in_vg(vg_from, seg->origin);
if (cow_from && origin_from)
continue;
if ((!cow_from && origin_from) ||
(cow_from && !origin_from)) {
log_error("Can't split snapshot %s between"
" two Volume Groups", seg->cow->name);
return 0;
}
/*
* At this point, the cow and origin should already be
* in vg_to.
*/
if (_lv_is_in_vg(vg_to, seg->cow) &&
_lv_is_in_vg(vg_to, seg->origin)) {
if (!_move_one_lv(vg_from, vg_to, lvh))
return_0;
}
}
}
return 1;
}
static int _move_mirrors(struct volume_group *vg_from,
struct volume_group *vg_to)
{
struct dm_list *lvh, *lvht;
struct logical_volume *lv;
struct lv_segment *seg, *log_seg;
unsigned s, seg_in, log_in;
dm_list_iterate_safe(lvh, lvht, &vg_from->lvs) {
lv = dm_list_item(lvh, struct lv_list)->lv;
if (!(lv->status & MIRRORED))
continue;
seg = first_seg(lv);
seg_in = 0;
for (s = 0; s < seg->area_count; s++)
if (_lv_is_in_vg(vg_to, seg_lv(seg, s)))
seg_in++;
log_in = !seg->log_lv;
if (seg->log_lv) {
log_seg = first_seg(seg->log_lv);
if (seg_is_mirrored(log_seg)) {
log_in = 1;
/* Ensure each log dev is in vg_to */
for (s = 0; s < log_seg->area_count; s++)
log_in = log_in &&
_lv_is_in_vg(vg_to,
seg_lv(log_seg, s));
} else
log_in = _lv_is_in_vg(vg_to, seg->log_lv);
}
if ((seg_in && seg_in < seg->area_count) ||
(seg_in && seg->log_lv && !log_in) ||
(!seg_in && seg->log_lv && log_in)) {
log_error("Can't split mirror %s between "
"two Volume Groups", lv->name);
return 0;
}
if (seg_in == seg->area_count && log_in) {
if (!_move_one_lv(vg_from, vg_to, lvh))
return_0;
}
}
return 1;
}
/*
* Create or open the destination of the vgsplit operation.
* Returns
* - non-NULL: VG handle w/VG lock held
* - NULL: no VG lock held
*/
static struct volume_group *_vgsplit_to(struct cmd_context *cmd,
const char *vg_name_to,
int *existing_vg)
{
struct volume_group *vg_to = NULL;
log_verbose("Checking for new volume group \"%s\"", vg_name_to);
/*
* First try to create a new VG. If we cannot create it,
* and we get FAILED_EXIST (we will not be holding a lock),
* a VG must already exist with this name. We then try to
* read the existing VG - the vgsplit will be into an existing VG.
*
* Otherwise, if the lock was successful, it must be the case that
* we obtained a WRITE lock and could not find the vgname in the
* system. Thus, the split will be into a new VG.
*/
vg_to = vg_create(cmd, vg_name_to);
if (vg_read_error(vg_to) == FAILED_LOCKING) {
log_error("Can't get lock for %s", vg_name_to);
release_vg(vg_to);
return NULL;
}
if (vg_read_error(vg_to) == FAILED_EXIST) {
*existing_vg = 1;
release_vg(vg_to);
vg_to = vg_read_for_update(cmd, vg_name_to, NULL, 0);
if (vg_read_error(vg_to)) {
release_vg(vg_to);
stack;
return NULL;
}
} else if (vg_read_error(vg_to) == SUCCESS) {
*existing_vg = 0;
}
return vg_to;
}
/*
* Open the source of the vgsplit operation.
* Returns
* - non-NULL: VG handle w/VG lock held
* - NULL: no VG lock held
*/
static struct volume_group *_vgsplit_from(struct cmd_context *cmd,
const char *vg_name_from)
{
struct volume_group *vg_from;
log_verbose("Checking for volume group \"%s\"", vg_name_from);
vg_from = vg_read_for_update(cmd, vg_name_from, NULL, 0);
if (vg_read_error(vg_from)) {
release_vg(vg_from);
return NULL;
}
return vg_from;
}
/*
* Has the user given an option related to a new vg as the split destination?
*/
static int new_vg_option_specified(struct cmd_context *cmd)
{
return(arg_count(cmd, clustered_ARG) ||
arg_count(cmd, alloc_ARG) ||
arg_count(cmd, maxphysicalvolumes_ARG) ||
arg_count(cmd, maxlogicalvolumes_ARG) ||
arg_count(cmd, vgmetadatacopies_ARG));
}
int vgsplit(struct cmd_context *cmd, int argc, char **argv)
{
struct vgcreate_params vp_new;
struct vgcreate_params vp_def;
const char *vg_name_from, *vg_name_to;
struct volume_group *vg_to = NULL, *vg_from = NULL;
int opt;
int existing_vg = 0;
int r = ECMD_FAILED;
const char *lv_name;
int lock_vg_from_first = 1;
if ((arg_count(cmd, name_ARG) + argc) < 3) {
log_error("Existing VG, new VG and either physical volumes "
"or logical volume required.");
return EINVALID_CMD_LINE;
}
if (arg_count(cmd, name_ARG) && (argc > 2)) {
log_error("A logical volume name cannot be given with "
"physical volumes.");
return ECMD_FAILED;
}
if (arg_count(cmd, name_ARG))
lv_name = arg_value(cmd, name_ARG);
else
lv_name = NULL;
vg_name_from = skip_dev_dir(cmd, argv[0], NULL);
vg_name_to = skip_dev_dir(cmd, argv[1], NULL);
argc -= 2;
argv += 2;
if (!strcmp(vg_name_to, vg_name_from)) {
log_error("Duplicate volume group name \"%s\"", vg_name_from);
return ECMD_FAILED;
}
if (strcmp(vg_name_to, vg_name_from) < 0)
lock_vg_from_first = 0;
if (lock_vg_from_first) {
vg_from = _vgsplit_from(cmd, vg_name_from);
if (!vg_from) {
stack;
return ECMD_FAILED;
}
/*
* Set metadata format of original VG.
* NOTE: We must set the format before calling vg_create()
* since vg_create() calls the per-format constructor.
*/
cmd->fmt = vg_from->fid->fmt;
vg_to = _vgsplit_to(cmd, vg_name_to, &existing_vg);
if (!vg_to) {
unlock_and_release_vg(cmd, vg_from, vg_name_from);
stack;
return ECMD_FAILED;
}
} else {
vg_to = _vgsplit_to(cmd, vg_name_to, &existing_vg);
if (!vg_to) {
stack;
return ECMD_FAILED;
}
vg_from = _vgsplit_from(cmd, vg_name_from);
if (!vg_from) {
unlock_and_release_vg(cmd, vg_to, vg_name_to);
stack;
return ECMD_FAILED;
}
if (cmd->fmt != vg_from->fid->fmt) {
/* In this case we don't know the vg_from->fid->fmt */
log_error("Unable to set new VG metadata type based on "
"source VG format - use -M option.");
goto bad;
}
}
if (existing_vg) {
if (new_vg_option_specified(cmd)) {
log_error("Volume group \"%s\" exists, but new VG "
"option specified", vg_name_to);
goto bad;
}
if (!vgs_are_compatible(cmd, vg_from,vg_to))
goto_bad;
} else {
vgcreate_params_set_defaults(&vp_def, vg_from);
vp_def.vg_name = vg_name_to;
if (vgcreate_params_set_from_args(cmd, &vp_new, &vp_def)) {
r = EINVALID_CMD_LINE;
goto_bad;
}
if (vgcreate_params_validate(cmd, &vp_new)) {
r = EINVALID_CMD_LINE;
goto_bad;
}
if (!vg_set_extent_size(vg_to, vp_new.extent_size) ||
!vg_set_max_lv(vg_to, vp_new.max_lv) ||
!vg_set_max_pv(vg_to, vp_new.max_pv) ||
!vg_set_alloc_policy(vg_to, vp_new.alloc) ||
!vg_set_clustered(vg_to, vp_new.clustered) ||
!vg_set_mda_copies(vg_to, vp_new.vgmetadatacopies))
goto_bad;
}
/* Archive vg_from before changing it */
if (!archive(vg_from))
goto_bad;
/* Move PVs across to new structure */
for (opt = 0; opt < argc; opt++) {
dm_unescape_colons_and_at_signs(argv[opt], NULL, NULL);
if (!move_pv(vg_from, vg_to, argv[opt]))
goto_bad;
}
/* If an LV given on the cmdline, move used_by PVs */
if (lv_name && !move_pvs_used_by_lv(vg_from, vg_to, lv_name))
goto_bad;
/* Move required LVs across, checking consistency */
if (!(_move_lvs(vg_from, vg_to)))
goto_bad;
/* FIXME Separate the 'move' from the 'validation' to fix dev stacks */
/* Move required mirrors across */
if (!(_move_mirrors(vg_from, vg_to)))
goto_bad;
/* Move required snapshots across */
if (!(_move_snapshots(vg_from, vg_to)))
goto_bad;
/* Split metadata areas and check if both vgs have at least one area */
if (!(vg_split_mdas(cmd, vg_from, vg_to)) && vg_from->pv_count) {
log_error("Cannot split: Nowhere to store metadata for new Volume Group");
goto bad;
}
/* Set proper name for all PVs in new VG */
if (!vg_rename(cmd, vg_to, vg_name_to))
goto_bad;
/* store it on disks */
log_verbose("Writing out updated volume groups");
/*
* First, write out the new VG as EXPORTED. We do this first in case
* there is a crash - we will still have the new VG information, in an
* exported state. Recovery after this point would be removal of the
* new VG and redoing the vgsplit.
* FIXME: recover automatically or instruct the user?
*/
vg_to->status |= EXPORTED_VG;
if (!archive(vg_to))
goto_bad;
if (!vg_write(vg_to) || !vg_commit(vg_to))
goto_bad;
backup(vg_to);
/*
* Next, write out the updated old VG. If we crash after this point,
* recovery is a vgimport on the new VG.
* FIXME: recover automatically or instruct the user?
*/
if (vg_from->pv_count) {
if (!vg_write(vg_from) || !vg_commit(vg_from))
goto_bad;
backup(vg_from);
}
/*
* Finally, remove the EXPORTED flag from the new VG and write it out.
*/
if (!test_mode()) {
release_vg(vg_to);
vg_to = vg_read_for_update(cmd, vg_name_to, NULL,
READ_ALLOW_EXPORTED);
if (vg_read_error(vg_to)) {
log_error("Volume group \"%s\" became inconsistent: "
"please fix manually", vg_name_to);
goto bad;
}
}
vg_to->status &= ~EXPORTED_VG;
if (!vg_write(vg_to) || !vg_commit(vg_to))
goto_bad;
backup(vg_to);
log_print_unless_silent("%s volume group \"%s\" successfully split from \"%s\"",
existing_vg ? "Existing" : "New",
vg_to->name, vg_from->name);
r = ECMD_PROCESSED;
bad:
/*
* vg_to references elements moved from vg_from
* so vg_to has to be freed first.
*/
unlock_and_release_vg(cmd, vg_to, vg_name_to);
unlock_and_release_vg(cmd, vg_from, vg_name_from);
return r;
}
|