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
0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
how to list all hard disks in linux from command line
Viewed 438 times since Mon, Jan 28, 2019
Linux - How to monitor CPU usage
Viewed 518 times since Fri, Jun 8, 2018
RHEL: Change system’s hostname
Viewed 992 times since Sun, May 27, 2018
CONFIGURE OCFS2
Viewed 1749 times since Sat, Jun 2, 2018
SSH Essentials: Working with SSH Servers, Clients, and Keys
Viewed 1144 times since Wed, Jun 27, 2018
Jak wygenerować silne hasła jednorazowe w Linuksie?
Viewed 845 times since Thu, May 24, 2018
systemd Auto-restart a crashed service in systemd
Viewed 311 times since Fri, Jan 17, 2020
RHEL: What is "SysRq key" and how to use it
Viewed 1266 times since Sat, Jun 2, 2018
Fix rpmdb: Thread died in Berkeley DB library
Viewed 312 times since Fri, Feb 14, 2020
INSTALACJA MIB SNMP W SYSTEMIE CENTOS/RHEL 6
Viewed 578 times since Fri, Nov 30, 2018