I need to clone a CentOS installation from a 1TB disk partitioned with LVM, to several identical machines. The disk is mostly empty since only the operating system and some software are installed and configured.

Without LVM I would copy the entire partition table, and then I would clone the partitions one by one using partclone:

sfdisk -d /dev/sda | sed -e 's/sda/sdb/' | sfdisk /dev/sdb
partclone.ext4 -c -s /dev/sda# -o - | partclone.ext4 -r -s - -o /dev/sdb#

However I think it will not work with LVM.

Of course I could just use dd to clone the whole disk:

dd if=/dev/sda of=/dev/sdb

but it takes too much time compared to partclone.

Is there a way to clone the LVM partitions faster? I think one possible solution is to clone the LVM partitions to regular partitions in another disk using dd, and then clone the new disk to the other machines using partclone. But I do not know if something like this will work:

dd if=/dev/mapper/vg_node07-lv_root of=/dev/sdb1

Can it work? Can you tell me other solutions?


Solution 1:

Yes, you can use dd just as described.

What I would do is create the source image using the smallest possible partitions, clone them, then enlarge the logical volume and filesystem on the target. Your cloning procedure becomes something like:

# <attach target for cloning, say, /dev/sdc>
# CURRENT_LE=2000  (get exact "Current LE" value from lvdisplay)
# NEW_SIZE="20G"
# parted -a optimal /dev/sdc mklabel gpt mkpart p1 ext4 0% 100%
# pvcreate /dev/sdc1
# vgcreate nodexx /dev/sdc1
# lvcreate -n lv_root -l $CURRENT_LE nodexx
# dd if=/dev/node07/lv_root of=/dev/nodexx/lv_root bs=4M
# lvresize /dev/vg_nodexx/lv_root -L $NEW_SIZE
# fsck.ext4 -f -y /dev/vg_nodexx/lv_root
# resize2fs /dev/vg_nodexx/lv_root

You'll want to book up on LVM and the file system tools, but this is a great candidate for shell scripting.