How to Regenerate LVM UUIDs for Cloned Virtual Machine Disks


2 views

When cloning virtual machines that use LVM (Logical Volume Manager), you'll inevitably encounter duplicate UUIDs across your systems. This happens because:

  • The original LVM metadata gets copied verbatim during cloning
  • Both PV (Physical Volume) and VG (Volume Group) UUIDs remain identical
  • LVM tools can get confused when detecting multiple volumes with same identifiers

First verify the duplicate UUIDs exist:

# List physical volumes
sudo pvdisplay

# List volume groups
sudo vgdisplay

# Compare outputs across cloned systems

For physical volumes:

# Uniquify a physical volume (destructive operation!)
sudo pvchange -u /dev/sdX

# Alternative non-destructive method:
sudo pvcreate --uuid NEW_UUID --restorefile /etc/lvm/backup/vg_backup /dev/sdX

Example with generated UUID:

sudo pvchange -u /dev/sdb1 --uuid "kK91j1-5m9m-8G0z-3S4H-qd8X-l8A5-abc123"

For volume groups (requires vgchange):

# First export the VG if active
sudo vgchange -an vg_name

# Generate new UUID
sudo vgchange -u vg_name

When working with VM templates, consider these automation approaches:

#!/bin/bash
# Script to regenerate LVM UUIDs during VM provisioning

for PV in $(pvs --noheadings -o pv_name); do
  pvchange -u "$PV"
done

for VG in $(vgs --noheadings -o vg_name); do
  vgchange -u "$VG"
done

After changing UUIDs:

# Update initramfs (critical for bootable volumes)
sudo update-initramfs -u

# Refresh udev rules
sudo udevadm trigger

# Verify changes
sudo pvscan
sudo vgscan
  • Always have backups before modifying UUIDs
  • Some distributions may require additional steps in /etc/fstab or /etc/crypttab
  • Bootloader configurations might need updating
  • LVM filters in /etc/lvm/lvm.conf may need adjustment

For modern LVM versions (2.02.158+):

# Single command to handle all UUID regeneration
sudo vgimportclone --basevgname vg_original -i /dev/sdX

This automatically:

  • Creates new random UUIDs for all components
  • Handles both PV and VG UUIDs
  • Maintains all volume relationships

When working with cloned virtual machines that use LVM, you'll inevitably encounter UUID conflicts. Since LVM uses UUIDs to uniquely identify physical volumes (PVs), volume groups (VGs), and logical volumes (LVs), having duplicate UUIDs across systems can cause serious management issues.

First, verify the duplicates with these commands:


# List all physical volumes
sudo pvs -o +pv_uuid

# List all volume groups
sudo vgs -o +vg_uuid

# List all logical volumes
sudo lvs -o +lv_uuid

For physical volumes, use pvchange with the --uuid flag:


# Generate new UUID for a PV
sudo pvchange --uuid /dev/sdX

# Alternative method using pvcreate
sudo pvcreate --uuid NEW_UUID --restorefile /etc/lvm/backup/vg_backup /dev/sdX

Volume groups require a different approach since they reference PV UUIDs:


# First deactivate the VG
sudo vgchange -an vg_name

# Then change the UUID
sudo vgchange --uuid vg_name

Logical volumes can have their UUIDs changed while active:


sudo lvchange --uuid /dev/vg_name/lv_name

For multiple cloned systems, consider this script:


#!/bin/bash

# Change all PV UUIDs
for PV in $(pvs --noheadings -o pv_name); do
  pvchange --uuid $PV
done

# Change all VG UUIDs
for VG in $(vgs --noheadings -o vg_name); do
  vgchange --uuid $VG
done

# Change all LV UUIDs
for LV in $(lvs --noheadings -o lv_path); do
  lvchange --uuid $LV
done
  • Always backup your LVM metadata before making changes
  • Update /etc/fstab if referencing LVs by UUID
  • Check initramfs and bootloader configurations
  • Verify all services using the volumes after changes