This Blog is to share our knowledge and expertise on Linux System Administration and VMware Administration

Wednesday, September 13, 2017

How to Clone the LVM2 Volume Groups?

These instructions describe the steps required to clone an LVM2 volume
group by creating a duplicate copy of the physical storage (PVs). This
requires the VG be deactivated while the clone is created and
re-named.

The volume group being cloned, CloneVG consists of two PVs originally present
on /dev/testpv0 and /dev/testpv1. A new volume group named CloneVG-clone will
be created on devices /dev/testpv2 and /dev/testpv3.

1. Deactivate the VG

       # vgchange -an CloneVG

2. Create the cloned PV(s)

       E.g., dd, clone LUNs on storage, break mirror etc.

       # dd if=/dev/testpv0 of=/dev/testpv2
       # dd if=/dev/testpv1 of=/dev/testpv3

3. For each original PV, create a filter entry in /etc/lvm/lvm.conf to
temporarily mask the PV from the LVM tools.

Preserve a copy of the original filtering rules so that it can be
restored at the end of the process, for example:

       # cp /etc/lvm/lvm.conf /etc/lvm/lvm.conf.orig

To exclude the original loopback devices /dev/testpv0 and /dev/testpv1, the
filter line could look like this:

       filter = [ "r|/dev/testpv0|", "r|/dev/testpv1|", "a|.*|" ]

Or, using a regex to match both devices with a single rule:

       filter = [ "r|/dev/loop[01]|", "a|.*|" ]

Once the filters are set up, remove the LVM persistent cache:

       # rm -f /etc/lvm/.cache [versions before 2.02.23]
OR
       # rm -f /etc/lvm/cache/.cache [version 2.02.23 or later]

Verify that the filtering is correct by running pvscan:

       # pvscan
         PV /dev/testpv2   VG CloneVG         lvm2 [60.00 MB / 40.00 MB free]
         PV /dev/testpv3   VG CloneVG         lvm2 [60.00 MB / 40.00 MB free]
         Total: 2 [120.00 MB] / in use: 2 [120.00 MB] / in no VG: 0 [0   ]

Only the cloned PVs should be displayed. If the original PVs appear,
check the syntax of the filtering rule and clear the persistent cache
again.

4. Modify the cloned volume group name, ID and physical volume IDs to
avoid name and UUID clashes between the original and cloned devices:

For each cloned physical volume, run:

       # pvchange --uuid /path/to/physical/volume

This will generate a new random UUID for the specified physical volume
and update the volume group metadata to reflect the changed identity.

For example:

       # pvchange --uuid /dev/testpv2
         Physical volume "/dev/testpv2" changed
         1 physical volume changed / 0 physical volumes not changed
       # pvchange --uuid /dev/testpv3
         Physical volume "/dev/testpv3" changed
         1 physical volume changed / 0 physical volumes not changed

Generate a new UUID for the entire volume group using vgchange:

       # vgchange --uuid CloneVG
         Volume group "CloneVG" successfully changed

Finally, rename the cloned VG:

       # vgrename CloneVG CloneVG-clone

5. Remove filtering rules & verify both VGs co-exist correctly

Restore the original filtering configuration and wipe the persistent cache:

       # cp /etc/lvm/lvm.conf.orig /etc/lvm/lvm.conf
       cp: overwrite `/etc/lvm/lvm.conf'? y
       # rm -f /etc/lvm/.cache

Run pvscan to verify the new and old VGs are correctly displayed:

       # pvscan
         PV /dev/testpv0   VG CloneVG         lvm2 [60.00 MB / 40.00 MB free]
         PV /dev/testpv1   VG CloneVG         lvm2 [60.00 MB / 40.00 MB free]

         PV /dev/testpv2   VG CloneVG-clone   lvm2 [60.00 MB / 40.00 MB free]
         PV /dev/testpv3   VG CloneVG-clone   lvm2 [60.00 MB / 40.00 MB free]
         Total: 4 [240.00 MB] / in use: 4 [240.00 MB] / in no VG: 0 [0  ]

6. Activate volume groups

Both the original and cloned VGs can now be activated simultaneously:

       # vgchange -ay CloneVG
         1 logical volume(s) in volume group "CloneVG" now active

       # vgchange -ay CloneVG-clone
         1 logical volume(s) in volume group "CloneVG-clone" now active

No comments:

Post a Comment