How do you clone an LVM partition?
| Posted in Linux | Posted on 17-10-2008
6
It’s actually more difficult than you might think. From the bit of googling that I did, it seems that you can’t just ‘clone’ and LVM logical volume, while it’s running.
One method I found was to use the ‘snapshot’ feature of LVM to create a ‘frozen image’ copy of the logical volume, which is then suitable for copying to a new logical volume, while leaving the original intact.
Here’s our original logical volume that we want to clone.
# lvdisplay
--- Logical volume ---
LV Name /dev/vg/host-disk
VG Name vg
LV UUID UK1rjH-LS3l-f7aO-240S-EwGw-0Uws-5ldhlW
LV Write Access read/write
LV Status available
# open 1
LV Size 9.30 GB
Current LE 2382
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 254:0
Let’s now create our snapshot logical volume. For the size, it should only need 10 – 20% of the original size, as we’re only mirroring the real volume.
# lvcreate --size 2G --snapshot --name host-disk-snap /dev/vg/host-disk
Let’s take a look at our new volume
# lvdisplay
--- Logical volume ---
LV Name /dev/vg/host-disk
VG Name vg
LV UUID UK1rjH-LS3l-f7aO-240S-EwGw-0Uws-5ldhlW
LV Write Access read/write
LV snapshot status source of /dev/vg/host-disk-snap [active]
LV Status available
# open 1
LV Size 9.30 GB
Current LE 2382
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 254:0
--- Logical volume ---
LV Name /dev/vg/host-disk-snap
VG Name server1
LV UUID 9zR5X5-OhM5-xUI0-OolP-vLjG-pexO-nk36oz
LV Write Access read/write
LV snapshot status active destination for /dev/vg/host-disk
LV Status available
# open 1
LV Size 9.30 GB
Current LE 2382
COW-table size 10.00 GB
COW-table LE 2560
Allocated to snapshot 0.01%
Snapshot chunk size 8.00 KB
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 254:5
From the output, you should be able to see that we’ve now got some snapshot fields shown in our output. We’ll create another logical volume, which will be our final target for our new virtual machine.
# lvcreate --size 10G --name newhost-disk vg
With our source and target partitions ready to go, we need to begin copying the data. You have some choices here, depending on your setup.
If you’re using the same size partitions you could use dd, or even xfs_copy if you’re using XFS.
If you’re like me, I wanted the new target partition to be a smaller size than the original. Also, if you wanted to use a different filesystem, the only real way to do it is to copy the files.
We’ll need to make the new filesystem on our target partiton
# mkfs.xfs /dev/vg/newhost-disk
and mount our filesystems
# mkdir /mnt/host-disk-snap
# mount -o ro /dev/vg/host-disk-snap /mnt/host-disk-snap
# mkdir /mnt/newhost-disk
# mount /dev/vg/newhost-disk /mnt/newhost-disk
I wasn’t sure about how changes to the filesystem would affect the original, so I thought I’d stick to the safe side, and mount it as read-only.
Change into the source filesystem
# cd /mnt/host-disk-snap
Using a mix of find and cpio, copy the files
# find . -mount -print | cpio -pdm /mnt/newhost-disk
Wait a few minutes, depending on your filesystem size, and you’re done.
When you’re satisfied, you can just use lvremove to remove your snapshot volume.
# umount /mnt/host-disk-snap
# lvremove /dev/vg/host-disk-snap
After all that, you should finally have a cloned filesystem to use. I’m sure there’s an easier way, but this worked for me.


Hi Andy,
Great blog!
What about cloning lvm same size partitions? Is there a way to skip the dd step? (It just takes so long).
Right now I am messing around with VM’s with xen. I am using lvms and I would like to be able to clone a complete lvm partition without changing sizes.
What I do is:
1. Create a snapshot
2. copy from the snapshot to a new lvm partiton with dd
3. erase snapshot
4. reconfig the new lvm as a new VM.
Thanks again for your great blog and I hope we can find a better way of cloning this lvms.
Leon
Instead of using dd, you could use cpio like what I did in this blog post. I found cpio (for a small root partition) only took a minute or two. The install was a fairly minimal debian install
For those who have SElinux I think that using dd or ‘cp -a’ which preserve file contexts and other SElinux magic would be preferable.
Using ‘cp -a’ has the benefit of being able to copy to a smaller disk and it does not need a pipe.
great nifty hint. works like a charm. thanx.
Thxs buddy…
Try this link, very easy to understand
http://www.redhatlinux.info/2010/11/lvm-logical-volume-manager.html
@Allen: when logically copying files using cp, hardlinks are not preserved. otoh, tar and cpio can.