Categories
SysOps

How to deal with overlapping LVM names

Today, I will connect the secondary encrypted hard disk from the previous Ubuntu installation to copy personal data, so there will be a problem with overlapping LVM names.

Connect an additional encrypted hard disk.

$ sudo lsblk
NAME                  MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda                     8:0    0 931,5G  0 disk  
├─sda1                  8:1    0     1M  0 part  
├─sda2                  8:2    0   513M  0 part  
├─sda3                  8:3    0   732M  0 part  
└─sda4                  8:4    0 930,3G  0 part  
sdb                     8:16   0 447,1G  0 disk  
├─sdb1                  8:17   0     1M  0 part  
├─sdb2                  8:18   0   513M  0 part  /boot/efi
├─sdb3                  8:19   0   732M  0 part  /boot
└─sdb4                  8:20   0 445,9G  0 part  
  └─sda4_crypt        253:0    0 445,9G  0 crypt 
    ├─vgubuntu-root   253:1    0 444,9G  0 lvm   /
    └─vgubuntu-swap_1 253:2    0   980M  0 lvm   [SWAP]

Inspect initial LVM logical volumes.

$ sudo lvs
  LV     VG       Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root   vgubuntu -wi-ao---- <444,94g                                                    
  swap_1 vgubuntu -wi-ao----  980,00m 

Open the LUKS device.

$ sudo cryptsetup luksOpen /dev/sda4 crypt_dev
Enter passphrase for /dev/sda4: ****************

You will get overlapping LVM volume groups, which is a problem here.

$ sudo lvs
  LV     VG       Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root   vgubuntu -wi-------  929,32g                                                    
  root   vgubuntu -wi-ao---- <444,94g                                                    
  swap_1 vgubuntu -wi-------  976,00m                                                    
  swap_1 vgubuntu -wi-ao----  980,00m   
$ sudo vgs
  VG       #PV #LV #SN Attr   VSize    VFree
  vgubuntu   1   2   0 wz--n- <930,28g    0 
  vgubuntu   1   2   0 wz--n-  445,89g    0 
$ sudo pvs
  PV                     VG       Fmt  Attr PSize    PFree
  /dev/mapper/crypt_dev  vgubuntu lvm2 a--  <930,28g    0 
  /dev/mapper/sda4_crypt vgubuntu lvm2 a--   445,89g    0 

Display volume group information to get unique VG UUID.

$ sudo vgdisplay
  --- Volume group ---
  VG Name               vgubuntu
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <930,28 GiB
  PE Size               4,00 MiB
  Total PE              238151
  Alloc PE / Size       238151 / <930,28 GiB
  Free  PE / Size       0 / 0   
  VG UUID               pPnX9c-Jolo-fwOF-7aYS-ZgoV-IFf1-Lx9dle
   
  --- Volume group ---
  VG Name               vgubuntu
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               445,89 GiB
  PE Size               4,00 MiB
  Total PE              114149
  Alloc PE / Size       114149 / 445,89 GiB
  Free  PE / Size       0 / 0   
  VG UUID               UZH27y-dsKG-owhU-zXdj-5FCH-30d0-KvLdIO

Now you can rename a specific volume group using its UUID.

$ sudo vgrename pPnX9c-Jolo-fwOF-7aYS-ZgoV-IFf1-Lx9dle additional_vg
  Processing VG vgubuntu because of matching UUID pPnX9c-Jolo-fwOF-7aYS-ZgoV-IFf1-Lx9dle
  Volume group "pPnX9c-Jolo-fwOF-7aYS-ZgoV-IFf1-Lx9dle" successfully renamed to "additional_vg"

Search for volume groups.

$ sudo vgscan
  Found volume group "additional_vg" using metadata type lvm2
  Found volume group "vgubuntu" using metadata type lvm2

Search for logical volumes, notice that new logical volumes are inactive after rename operation.

$ sudo lvscan 
  inactive          '/dev/additional_vg/root' [929,32 GiB] inherit
  inactive          '/dev/additional_vg/swap_1' [976,00 MiB] inherit
  ACTIVE            '/dev/vgubuntu/root' [<444,94 GiB] inherit
  ACTIVE            '/dev/vgubuntu/swap_1' [980,00 MiB] inherit

Activate the logical volume that will be mounted.

$ sudo lvchange -ay /dev/additional_vg/root

Inspect logical volumes.

$ sudo lvs
  LV     VG            Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root   additional_vg -wi-ao----  929,32g                                                    
  swap_1 additional_vg -wi-------  976,00m                                                    
  root   vgubuntu      -wi-ao---- <444,94g                                                    
  swap_1 vgubuntu      -wi-ao----  980,00m

Mount specific logical volume.

$ sudo mount /dev/mapper/additional_vg-root crypt_dir/

Copy data or perform other operations on the mounted file system.

Unmount specific logical volume.

$ sudo umount crypt_dir

Deactivate logical volume.

$ sudo lvchange -an /dev/additional_vg/root

Close the LUKS device.

$ sudo cryptsetup luksClose crypt_dev

You do not need to activate logical volume next time, as it will be done automatically.