As of the LVM in Debian stretch (9.0), namely 2.02.168-2, it's
possible to do a copy of a logical volume across volume groups using a combination of vgmerge
, lvconvert
, and vgsplit
. Since a move is a combination of a copy and a delete, this will also work for a move.
Alternatively, you can use pvmove
to move the physical extents instead. To quote U&L: Purpose of Physical Extents:
A single physical extent is the smallest unit of disk space that can be individually managed by LVM.
A complete self-contained example session using loop devices and
lvconvert
follows.
Summary: we create volume group vg1
with logical volume lv1
, and vg2
with lv2
, and make a copy of lv1
in vg2
.
Create files.
truncate pv1 --size 100MB
truncate pv2 --size 100MB
Set up loop devices on files.
losetup /dev/loop1 pv1
losetup /dev/loop2 pv2
Create physical volumes on loop devices (initialize loop devices for use by LVM).
pvcreate /dev/loop1 /dev/loop2
Create volume groups vg1 and vg2 on /dev/loop1 and /dev/loop2
respectively.
vgcreate vg1 /dev/loop1
vgcreate vg2 /dev/loop2
Create logical volumes lv1 and lv2 on vg1 and vg2 respectively.
lvcreate -L 10M -n lv1 vg1
lvcreate -L 10M -n lv2 vg2
Create ext4 filesystems on lv1 and lv2.
mkfs.ext4 -j /dev/vg1/lv1
mkfs.ext4 -j /dev/vg2/lv2
Optionally, write something on lv1 so you can later check the copy was correctly created. Make vg1 inactive.
vgchange -a n vg1
Run merge command in test mode. This merges vg1 into vg2.
# vgmerge -A y -l -t -v <<destination-vg>> <<source-vg>>
vgmerge -A y -l -t -v vg2 vg1
And then for real.
vgmerge -A y -l -v vg2 vg1
Then create a RAID 1 mirror pair from lv1
using lvconvert
. The
dest-pv
argument tells lvconvert
to make the mirror copy
on /dev/loop2
.
# lvconvert --type raid1 --mirrors 1 <<source-lv>> <<dest-pv>>
lvconvert --type raid1 --mirrors 1 /dev/vg2/lv1 /dev/loop2
Then split the mirror. The new LV is now lv1_copy
.
# lvconvert --splitmirrors 1 --name <<source-lv-copy>> <<source-lv>>
lvconvert --splitmirrors 1 --name lv1_copy /dev/vg2/lv1
Make vg2/lv1
inactive.
lvchange -a n /dev/vg2/lv1
Then (testing mode)
# vgsplit -t -v <<source-vg>> <<destination-vg>> <<moved-to-pv>>
vgsplit -t -v /dev/vg2 /dev/vg1 /dev/loop1
For real
vgsplit -v /dev/vg2 /dev/vg1 /dev/loop1
Resulting output:
lvs
[...]
lv1 vg1 -wi-a----- 12.00m
lv1_copy vg2 -wi-a----- 12.00m
lv2 vg2 -wi-a----- 12.00m
NOTES:
Most of these commands will need to be run as root. The #
in front of some of the commands indicates that it is a comment. It does not represent the root prompt.
If there is any duplication of the names of the logical volumes in the two volume groups, vgmerge
will refuse to proceed.
On merge, logical volumes in vg1
must be inactive. And on split, logical volumes in vg2
belonging to vg1
must be inactive. In our case, this is lv1
.