3

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?

  • Exactly what structure do you want to end up with? And why aren't you just using kickstart? – Michael Hampton Jul 2 '15 at 0:58
  • I'm not familiar with kickstart. Moreover the machine that I want to clone is already configured to serve as a compute node in a cluster. It has torque job manager installed and several scientific software. User data is on the head node and mounted with NFS. The only important partitions are / and /boot. – Manuel Jul 2 '15 at 1:42
  • Did you miss that file anaconda-ks.cfg that was left in the /root directory after installation? That's a kickstart file. Feed it to the installer and it will install an identical system. And you can of course customize it to do whatever configuration you wish. – Michael Hampton Jul 2 '15 at 1:51
  • The problem is that I would have to compile a lot of additional software on each node. It is already compiled in the node that I want to clone. – Manuel Jul 2 '15 at 1:56
  • In that case you should be packaging it and running an internal repository. – Michael Hampton Jul 2 '15 at 1:57
3

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.

  • For backup and restore of lvm layouts, take a look at vgcfgbackup tool – Ali Nadalizadeh Oct 14 '17 at 9:05
0

You can do almost the same thing that VMware p2v software does. What it does is create the new filesystems on the new system exactly as you want them to be, then do a tar of the filesystems across to the other server. This way you get everything exactly the same, and you are only copying files and space that are currently being used. Then you have to do the grub stuff to make sure its bootable.

-2

LVM does a lot of things much more complicated than just a simple disk with a simple partition... and because of that... cloning is a much more complicated process.

In fact, you'd be better off (re?)creating the lvm volume manually (or scripted) and then simply using the sfdisk/partclone process to clone the actual data to the new LVM.

This would also give you the added benefit of being able to clone a RAID-1 system to a RAID-5 setup across more disk... as the partitions would be unaffected.

  • I googled about using partclone with LVM partitions but I did not find anything specific. Would the following work? partclone.ext4 -c -s /dev/mapper/vg_node07-lv_root -o - | partclone.ext4 -r -s - -o /dev/mapper/vg_node08-lv_root – Manuel Jul 1 '15 at 23:41
  • I can't say 100% yes... but it appears to be fine... except for the fact that the volume-group name appears to be changed. In the fstab (and possibly in grub as well) you might need to make changes to accommodate this... or to a vgrename to rename it to what it should be. – TheCompWiz Jul 2 '15 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.