LVM: Reduce root PV/VG


LVM: Reduce root PV/VG

# Tested on RHEL 6 & 7

# *** I recommend to backup all important data before carrying out an operation like the
# one described here.

# I carried out my RHEL installation without being careful about disk partitioning. Now I
# don't have any space to create an additional Volume Group on system disk (I don' have
# any other available disks) and, the worst, there's a lot of wasted GBs as my system disk
# is about 136GB and I'm only using some 18GB for the OS filesystems:

pvs
     PV         VG     Fmt  Attr PSize   PFree
     /dev/sda2  rootvg lvm2 a--  135.72g 117.72g

lvs
     LV      VG     Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert
     lv_home rootvg -wi-ao---- 2.00g
     lv_opt  rootvg -wi-ao---- 2.00g
     lv_root rootvg -wi-ao---- 1.00g
     lv_swap rootvg -wi-ao---- 4.00g
     lv_tmp  rootvg -wi-ao---- 2.00g
     lv_usr  rootvg -wi-ao---- 4.00g
     lv_var  rootvg -wi-ao---- 3.00g


# I decided to reduce root Volume Group down to 32GB in order to free up some disk space


# This is the current partitioning

  /boot (ext4)                          rootvg
|--------------|-----------------------------------------------------------|
 <-- 256 MB --> <---- rest of the disk, some 136 GB ---------------------->



# And what I'd like to have

  /boot (ext4)       rootvg                      free space
|--------------|-----------------|-----------------------------------------|
 <-- 256 MB --> <---- 32 GB ----> <--------------- ~100 GB --------------->


# First of all, I check that all the existing volumes are in contiguous disk areas; at
# least, that there is enough free space at the end of the physical volume as to reduce
# it to the wished size. In my case everything seems good to do the required operations.
# Otherwise we should have done some "move" operations on the logical volumes.

lvm pvdisplay --maps
     --- Physical volume ---
     PV Name               /dev/sda2
     VG Name               rootvg
     PV Size               135.72 GiB / not usable 3.00 MiB
     Allocatable           yes
     PE Size               32.00 MiB
     Total PE              4343
     Free PE               3767
     Allocated PE          576
     PV UUID               tNLp0a-Hucs-Ic53-9PXH-6C1z-YRGX-t5eqy6

     --- Physical Segments ---
     Physical extent 0 to 31:
       Logical volume      /dev/rootvg/lv_root
       Logical extents     0 to 31
     Physical extent 32 to 159:
       Logical volume      /dev/rootvg/lv_usr
       Logical extents     0 to 127
     Physical extent 160 to 223:
       Logical volume      /dev/rootvg/lv_opt
       Logical extents     0 to 63
     Physical extent 224 to 287:
       Logical volume      /dev/rootvg/lv_home
       Logical extents     0 to 63
     Physical extent 288 to 383:
       Logical volume      /dev/rootvg/lv_var
       Logical extents     0 to 95
     Physical extent 384 to 447:
       Logical volume      /dev/rootvg/lv_tmp
       Logical extents     0 to 63
     Physical extent 448 to 575:
       Logical volume      /dev/rootvg/lv_swap
       Logical extents     0 to 127
     Physical extent 576 to 4342:                         <-----
       FREE


# Let's reduce the physical volume

pvresize --setphysicalvolumesize 32G /dev/sda2
     Physical volume "/dev/sda2" changed
     1 physical volume(s) resized / 0 physical volume(s) not resized

pvs
     PV         VG     Fmt  Attr PSize  PFree
     /dev/sda2  rootvg lvm2 a--  31.97g 13.97g

# and, then, the partition that contains the physical volume (regarding cylinders
# boundaries, I did my calculations before)

fdisk /dev/sda

   WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
            switch off the mode (command 'c') and change display units to
            sectors (command 'u').

   Command (m for help): p

   Disk /dev/sda: 146.0 GB, 145999527936 bytes
   255 heads, 63 sectors/track, 17750 cylinders
   Units = cylinders of 16065 * 512 = 8225280 bytes
   Sector size (logical/physical): 512 bytes / 512 bytes
   I/O size (minimum/optimal): 512 bytes / 512 bytes
   Disk identifier: 0x0007a6a0

      Device Boot      Start         End      Blocks   Id  System
   /dev/sda1   *           1          33      262144   83  Linux
   Partition 1 does not end on cylinder boundary.
   /dev/sda2              33       17751   142314496   8e  Linux LVM

   Command (m for help): d
   Partition number (1-4): 2

   Command (m for help): n
   Command action
      e   extended
      p   primary partition (1-4)
   p
   Partition number (1-4): 2
   First cylinder (33-17750, default 33):
   Using default value 33
   Last cylinder, +cylinders or +size{K,M,G} (33-17750, default 17750): 4211

   Command (m for help): p

   Disk /dev/sda: 146.0 GB, 145999527936 bytes
   255 heads, 63 sectors/track, 17750 cylinders
   Units = cylinders of 16065 * 512 = 8225280 bytes
   Sector size (logical/physical): 512 bytes / 512 bytes
   I/O size (minimum/optimal): 512 bytes / 512 bytes
   Disk identifier: 0x0007a6a0

      Device Boot      Start         End      Blocks   Id  System
   /dev/sda1   *           1          33      262144   83  Linux
   Partition 1 does not end on cylinder boundary.
   /dev/sda2              33        4211    33561689+  83  Linux

   Command (m for help): w
   The partition table has been altered!

   Calling ioctl() to re-read partition table.



# Reboot, to load the new partition table

shutdown -r now

# And, finally, resize physical volume to match partition size

pvresize /dev/sda2
     Physical volume "/dev/sda2" changed
     1 physical volume(s) resized / 0 physical volume(s) not resized

pvs
     PV         VG     Fmt  Attr PSize  PFree
     /dev/sda2  rootvg lvm2 a--  32.00g 14.00g


Article Number: 169
Posted: Sat, Jun 2, 2018 10:24 PM
Last Updated: Sat, Jun 2, 2018 10:24 PM

Online URL: http://kb.ictbanking.net/article.php?id=169