4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 static struct volume_group
*_vgmerge_vg_read(struct cmd_context
*cmd
,
23 struct volume_group
*vg
;
24 log_verbose("Checking for volume group \"%s\"", vg_name
);
25 vg
= vg_read_for_update(cmd
, vg_name
, NULL
, 0);
26 if (vg_read_error(vg
)) {
33 static int _vgmerge_single(struct cmd_context
*cmd
, const char *vg_name_to
,
34 const char *vg_name_from
)
36 struct volume_group
*vg_to
, *vg_from
;
37 struct lv_list
*lvl1
, *lvl2
;
39 int lock_vg_from_first
= 0;
41 if (!strcmp(vg_name_to
, vg_name_from
)) {
42 log_error("Duplicate volume group name \"%s\"", vg_name_from
);
46 if (strcmp(vg_name_to
, vg_name_from
) > 0)
47 lock_vg_from_first
= 1;
49 if (lock_vg_from_first
) {
50 vg_from
= _vgmerge_vg_read(cmd
, vg_name_from
);
55 vg_to
= _vgmerge_vg_read(cmd
, vg_name_to
);
58 unlock_and_release_vg(cmd
, vg_from
, vg_name_from
);
62 vg_to
= _vgmerge_vg_read(cmd
, vg_name_to
);
68 vg_from
= _vgmerge_vg_read(cmd
, vg_name_from
);
71 unlock_and_release_vg(cmd
, vg_to
, vg_name_to
);
76 if (!vgs_are_compatible(cmd
, vg_from
, vg_to
))
79 /* FIXME List arg: vg_show_with_pv_and_lv(vg_to); */
81 if (!archive(vg_from
) || !archive(vg_to
))
84 drop_cached_metadata(vg_from
);
86 /* Merge volume groups */
87 while (!dm_list_empty(&vg_from
->pvs
)) {
88 struct dm_list
*pvh
= vg_from
->pvs
.n
;
89 struct physical_volume
*pv
;
91 dm_list_move(&vg_to
->pvs
, pvh
);
93 pv
= dm_list_item(pvh
, struct pv_list
)->pv
;
94 pv
->vg_name
= dm_pool_strdup(cmd
->mem
, vg_to
->name
);
96 vg_to
->pv_count
+= vg_from
->pv_count
;
99 dm_list_iterate_items(lvl1
, &vg_to
->lvs
) {
100 union lvid
*lvid1
= &lvl1
->lv
->lvid
;
101 char uuid
[64] __attribute((aligned(8)));
103 dm_list_iterate_items(lvl2
, &vg_from
->lvs
) {
104 union lvid
*lvid2
= &lvl2
->lv
->lvid
;
106 if (id_equal(&lvid1
->id
[1], &lvid2
->id
[1])) {
107 if (!id_create(&lvid2
->id
[1])) {
108 log_error("Failed to generate new "
109 "random LVID for %s",
113 if (!id_write_format(&lvid2
->id
[1], uuid
,
117 log_verbose("Changed LVID for %s to %s",
118 lvl2
->lv
->name
, uuid
);
123 while (!dm_list_empty(&vg_from
->lvs
)) {
124 struct dm_list
*lvh
= vg_from
->lvs
.n
;
126 dm_list_move(&vg_to
->lvs
, lvh
);
129 while (!dm_list_empty(&vg_from
->fid
->metadata_areas
)) {
130 struct dm_list
*mdah
= vg_from
->fid
->metadata_areas
.n
;
132 dm_list_move(&vg_to
->fid
->metadata_areas
, mdah
);
135 vg_to
->extent_count
+= vg_from
->extent_count
;
136 vg_to
->free_count
+= vg_from
->free_count
;
138 /* store it on disks */
139 log_verbose("Writing out updated volume group");
140 if (!vg_write(vg_to
) || !vg_commit(vg_to
))
143 /* FIXME Remove /dev/vgfrom */
146 log_print("Volume group \"%s\" successfully merged into \"%s\"",
147 vg_from
->name
, vg_to
->name
);
150 if (lock_vg_from_first
) {
151 unlock_and_release_vg(cmd
, vg_to
, vg_name_to
);
152 unlock_and_release_vg(cmd
, vg_from
, vg_name_from
);
154 unlock_and_release_vg(cmd
, vg_from
, vg_name_from
);
155 unlock_and_release_vg(cmd
, vg_to
, vg_name_to
);
160 int vgmerge(struct cmd_context
*cmd
, int argc
, char **argv
)
162 char *vg_name_to
, *vg_name_from
;
164 int ret
= 0, ret_max
= 0;
167 log_error("Please enter 2 or more volume groups to merge");
168 return EINVALID_CMD_LINE
;
171 vg_name_to
= skip_dev_dir(cmd
, argv
[0], NULL
);
175 for (; opt
< argc
; opt
++) {
176 vg_name_from
= skip_dev_dir(cmd
, argv
[opt
], NULL
);
178 ret
= _vgmerge_single(cmd
, vg_name_to
, vg_name_from
);