Cloning LVM partitions

26,302

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.

Share:
26,302

Related videos on Youtube

Manuel
Author by

Manuel

Updated on September 18, 2022

Comments

  • Manuel
    Manuel almost 2 years

    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?

    • Michael Hampton
      Michael Hampton almost 9 years
      Exactly what structure do you want to end up with? And why aren't you just using kickstart?
    • Manuel
      Manuel almost 9 years
      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.
    • Michael Hampton
      Michael Hampton almost 9 years
      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.
    • Manuel
      Manuel almost 9 years
      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.
    • Michael Hampton
      Michael Hampton almost 9 years
      In that case you should be packaging it and running an internal repository.
    • Manuel
      Manuel almost 9 years
      That would be ideal, but this is a one-time job, so I was looking for a simpler solution.
    • Michael Hampton
      Michael Hampton almost 9 years
      Keep in mind that it's only a one-time job until you have to do it again. And if you had to do it once, you will have to do it again sooner or later. Plus, what happens when that custom software needs to be updated? Do you really want to go compile it again on 100 compute nodes? Or just once, and then drop it in the repo and let all 100 nodes update?
    • Manuel
      Manuel almost 9 years
    • ddm-j
      ddm-j over 2 years
      Note: when you clone a system like that, you still need to roll out a few changes to each. With sysvinit, it still somewhat worked, although all machines would have the same hostname, and logs would be collected by IP address, but systemd has a unique system ID that is used when merging logs, so this needs to be changed at least when you want central log file analysis. There is a reason why identical machines are usually set up by running the installer in non-interactive mode.
  • Manuel
    Manuel almost 9 years
    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
  • TheCompWiz
    TheCompWiz almost 9 years
    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.
  • Ali Nadalizadeh
    Ali Nadalizadeh over 6 years
    For backup and restore of lvm layouts, take a look at vgcfgbackup tool