Friday, April 26, 2013

LVM : How to extend an active logical volume?

Important: This is not a definite guide. Use LVM tools with caution and always have a backup before you modify anything. Improper use of those tools may destroy all your data and I do not take any responsibility of that. You were warned.  

In this case we will add new PV (physical volume) to the existing VG (volume group) to extend an existing LV (logical volume) containing user home directories and mounted under /home. Both, the existing and a new PV are LUKS encrypted partitions. See my previous post LUKS : How to encrypt a partition? if you need to prepare the LUKS encrypted device first.

Check the current LVM setup

[root@cmstorm home]# lvs
  LV   VG      Attr     LSize   Pool Origin Data%  Move Log Copy%  Convert
  home vg_home -wi-ao-- 232.88g                                          
  root vg_root -wi-ao-- 347.62g                                          
  swap vg_root -wi-ao--  17.62g 


[root@cmstorm home]# lvdisplay vg_home
  --- Logical volume ---
  LV Path                /dev/vg_home/home
  LV Name                home
  VG Name                vg_home
  LV UUID                hd4N00-VKbv-47Hh-pTQ3-VH1m-6dEf-uukfDp
  LV Write Access        read/write
  LV Creation host, time cmstorm.localdomain, 2012-10-06 04:22:45 +0100
  LV Status              available
  # open                 1
  LV Size                232.88 GiB
  Current LE             7452
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3


[root@cmstorm home]# vgdisplay vg_home
  --- Volume group ---
  VG Name               vg_home
  System ID            
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               232.88 GiB
  PE Size               32.00 MiB
  Total PE              7452
  Alloc PE / Size       7452 / 232.88 GiB
  Free  PE / Size       0 / 0  
  VG UUID               IuoMBo-r2OD-D7Sg-U9dl-TbS6-3rFj-pcX5P7



[root@cmstorm home]# pvs
  PV                                                    VG      Fmt  Attr PSize   PFree
  /dev/mapper/luks-e78b92c8-26a5-415b-9f95-c56aa9e511ca vg_home lvm2 a--  232.88g    0
  /dev/sdb5                                             vg_root lvm2 a--  365.25g    0
 

[root@cmstorm home]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb5
  VG Name               vg_root
  PV Size               365.27 GiB / not usable 22.00 MiB
  Allocatable           yes (but full)
  PE Size               32.00 MiB
  Total PE              11688
  Free PE               0
  Allocated PE          11688
  PV UUID               OUOFIT-KRPR-qnOK-yeKC-Me2y-NGn4-WeWqyR
  
  --- Physical volume ---
  PV Name               /dev/mapper/luks-e78b92c8-26a5-415b-9f95-c56aa9e511ca
  VG Name               vg_home
  PV Size               232.88 GiB / not usable 8.00 MiB
  Allocatable           yes (but full)
  PE Size               32.00 MiB
  Total PE              7452
  Free PE               0
  Allocated PE          7452
  PV UUID               Er1gKd-L20a-320j-EdGy-lbcl-szwz-hpVvDe


[root@cmstorm home]# pvdisplay /dev/mapper/luks-e78b92c8-26a5-415b-9f95-c56aa9e511ca
  --- Physical volume ---
  PV Name               /dev/mapper/luks-e78b92c8-26a5-415b-9f95-c56aa9e511ca
  VG Name               vg_home
  PV Size               232.88 GiB / not usable 8.00 MiB
  Allocatable           yes (but full)
  PE Size               32.00 MiB
  Total PE              7452
  Free PE               0
  Allocated PE          7452
  PV UUID               Er1gKd-L20a-320j-EdGy-lbcl-szwz-hpVvDe
  
[root@cmstorm home]# ls /dev/mapper/luks-cba6febb-91cb-456e-afcf-0a16e89e4cae 
luks-e78b92c8-26a5-415b-9f95-c56aa9e511ca


Create a new physical volume (PV) from the LUKS encrypted device

[root@cmstorm home]# pvcreate /dev/mapper/luks-cba6febb-91cb-456e-afcf-0a16e89e4cae
  Writing physical volume data to disk "/dev/mapper/luks-cba6febb-91cb-456e-afcf-0a16e89e4cae"
  Physical volume "/dev/mapper/luks-cba6febb-91cb-456e-afcf-0a16e89e4cae" successfully created


[root@cmstorm home]# pvs
  PV                                                    VG      Fmt  Attr PSize   PFree 
  /dev/mapper/luks-cba6febb-91cb-456e-afcf-0a16e89e4cae         lvm2 a--  931.51g 931.51g
  /dev/mapper/luks-e78b92c8-26a5-415b-9f95-c56aa9e511ca vg_home lvm2 a--  232.88g      0
  /dev/sdb5                                             vg_root lvm2 a--  365.25g      0
 

[root@cmstorm home]# pvdisplay /dev/mapper/luks-cba6febb-91cb-456e-afcf-0a16e89e4cae
  "/dev/mapper/luks-cba6febb-91cb-456e-afcf-0a16e89e4cae" is a new physical volume of "931.51 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/mapper/luks-cba6febb-91cb-456e-afcf-0a16e89e4cae
  VG Name              
  PV Size               931.51 GiB
  Allocatable           NO
  PE Size               0  
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               8VRooy-YVJ1-KDqU-QFhk-GnvF-VbJt-uEbhRS


Add the new physical volume (PV) to the volume group (VG)

[root@cmstorm home]# vgextend vg_home /dev/mapper/luks-cba6febb-91cb-456e-afcf-0a16e89e4cae
  Volume group "vg_home" successfully extended


[root@cmstorm home]# pvs
  PV                                                    VG      Fmt  Attr PSize   PFree 
  /dev/mapper/luks-cba6febb-91cb-456e-afcf-0a16e89e4cae vg_home lvm2 a--  931.50g 931.50g
  /dev/mapper/luks-e78b92c8-26a5-415b-9f95-c56aa9e511ca vg_home lvm2 a--  232.88g      0
  /dev/sdb5                                             vg_root lvm2 a--  365.25g      0


[root@cmstorm home]# pvdisplay /dev/mapper/luks-cba6febb-91cb-456e-afcf-0a16e89e4cae
  --- Physical volume ---
  PV Name               /dev/mapper/luks-cba6febb-91cb-456e-afcf-0a16e89e4cae
  VG Name               vg_home
  PV Size               931.51 GiB / not usable 6.00 MiB
  Allocatable           yes
  PE Size               32.00 MiB
  Total PE              29808
  Free PE               29808
  Allocated PE          0
  PV UUID               8VRooy-YVJ1-KDqU-QFhk-GnvF-VbJt-uEbhRS


Extend the logical volume (LV)

[root@cmstorm home]# lvextend -l +100%FREE /dev/vg_home/home
  Extending logical volume home to 1.14 TiB
  Logical volume home successfully resized
[root@cmstorm home]#



[root@cmstorm home]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb5
  VG Name               vg_root
  PV Size               365.27 GiB / not usable 22.00 MiB
  Allocatable           yes (but full)
  PE Size               32.00 MiB
  Total PE              11688
  Free PE               0
  Allocated PE          11688
  PV UUID               OUOFIT-KRPR-qnOK-yeKC-Me2y-NGn4-WeWqyR
  
  --- Physical volume ---
  PV Name               /dev/mapper/luks-e78b92c8-26a5-415b-9f95-c56aa9e511ca
  VG Name               vg_home
  PV Size               232.88 GiB / not usable 8.00 MiB
  Allocatable           yes (but full)
  PE Size               32.00 MiB
  Total PE              7452
  Free PE               0
  Allocated PE          7452
  PV UUID               Er1gKd-L20a-320j-EdGy-lbcl-szwz-hpVvDe
  
  --- Physical volume ---
  PV Name               /dev/mapper/luks-cba6febb-91cb-456e-afcf-0a16e89e4cae
  VG Name               vg_home
  PV Size               931.51 GiB / not usable 6.00 MiB
  Allocatable           yes (but full)
  PE Size               32.00 MiB
  Total PE              29808
  Free PE               0
  Allocated PE          29808
  PV UUID               8VRooy-YVJ1-KDqU-QFhk-GnvF-VbJt-uEbhRS


[root@cmstorm home]# lvdisplay vg_home
  --- Logical volume ---
  LV Path                /dev/vg_home/home
  LV Name                home
  VG Name                vg_home
  LV UUID                hd4N00-VKbv-47Hh-pTQ3-VH1m-6dEf-uukfDp
  LV Write Access        read/write
  LV Creation host, time cmstorm.localdomain, 2012-10-06 04:22:45 +0100
  LV Status              available
  # open                 1
  LV Size                1.14 TiB
  Current LE             37260
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3


[root@cmstorm home]# df -h
Filesystem                Size  Used Avail Use% Mounted on
devtmpfs                  7.9G     0  7.9G   0% /dev
tmpfs                     7.9G  448K  7.9G   1% /dev/shm
tmpfs                     7.9G  2.0M  7.9G   1% /run
/dev/mapper/vg_root-root  343G   12G  314G   4% /
tmpfs                     7.9G     0  7.9G   0% /sys/fs/cgroup
tmpfs                     7.9G     0  7.9G   0% /media
/dev/sdd1                 459G  244G  192G  57% /mnt/WD10EAVS-EXT4
/dev/sdd2                 466G  114G  352G  25% /mnt/WD10EAVS-NTFS
/dev/sdb3                 477M   93M  359M  21% /boot
/dev/mapper/vg_home-home  230G   95G  123G  44% /home


The logical volume size is now 1.14TiB, but the /dev/mapper/vg_home-home filesystem is still 230G, so now you need to resize the file system to start using the new space.

 [root@cmstorm home]# resize2fs /dev/vg_home/home