How do you clone an LVM partition?

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.

Posted in Linux by Andy Botting at October 17th, 2008.

12 Responses to “How do you clone an LVM partition?”

  1. Leon says:

    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

  2. Andy says:

    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

  3. Allen says:

    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.

  4. marc says:

    great nifty hint. works like a charm. thanx.

  5. foo says:

    @Allen: when logically copying files using cp, hardlinks are not preserved. otoh, tar and cpio can.

  6. BlaY0 says:

    More convenient way nowadays would be to make a mirror of LV and splitting it afterwards:

    lvconvert -m 1 –alloc /dev/VG/LV
    lvconvert –splitmirrors 1 –name CLONE /dev/VG/LV

  7. Digitalinux says:

    Thanks for sharing..!! I love your post .. I am looking more post like this one on
    this blog .. this post proves really helpful for me .

    http://www.digitalinux.com/2010/11/lvm-logical-volume-manager.html

  8. ikke says:

    Thanksk BLaYO for the tip, but that required a bit more in my case. I have RAID5 + lvm, so pv is md1 only. Lvconvert doesn’t allow mirroring in such case where there is only one PV, it seems. I had to add “–alloc cling” to force it to allow mirroring on the same pv. I found the allocation policy explained from “man lvm”.

  9. vicky says:

    Hi, this is a nice tutorial.

    I just wanted to point out that a snapshot must exist in the same VG (AFAIK).

    Am referring to this part:

    =============================================
    # lvdisplay

    — Logical volume —
    LV Name /dev/vg/host-disk
    VG Name vg
    ….
    ….

    — Logical volume —
    LV Name /dev/vg/host-disk-snap
    VG Name server1 <<<<<<<<<<<<
    …..
    …..
    =============================================

    Cheers,
    Vicky

  10. felipe says:

    what would be the equivalent `tar` command (instead of `cpio`)?

  11. Joachim Wagner says:

    A warning on the size of the snapshot: “we’re only mirroring the real volume” doesn’t mean that 10-20% is enough. How much is needed depends on how the source volume is used during the lifetime of the snapshot. In extreme cases, 100% may be needed.

    @felipe: See for example http://www.linuxdevcenter.com/pub/a/linux/lpt/18_16.html “Copying Directory Trees with (tar | tar)”. Also consider “rsync” and read https://blog.mudy.info/2010/07/linux-file-copy-benchmark-cp-vs-cpio-vs-tar-vs-rsync/

Leave a Reply


× 3 = twenty four