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.