TransWikia.com

LVM: Decrease/shrink size of a physical volume by deallocating free space from removed logical volume

Unix & Linux Asked by SaveMeTenMinutes on December 7, 2020

I have the following LVM partition:

# df -h

  Filesystem                Size  Used Avail Use% Mounted on
  /dev/mapper/data-root     5.3G  4.2G  864M  84% /
  ...
  /dev/mapper/data-install  6.8G  5.7G  783M  89% /install

# vgs
  VG   #PV #LV #SN Attr   VSize   VFree
  data   1   2   0 wz--n- <12.50g    0

# vgdisplay
  --- Volume group ---
  VG Name               data
  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               <12.50 GiB
  PE Size               4.00 MiB
  Total PE              3199
  Alloc PE / Size       3199 / <12.50 GiB
  Free  PE / Size       0 / 0
  VG UUID               jSACsC-wGwy-ki0w-nSCn-kxMy-bLOV-dVN68E


# pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sda2  data lvm2 a--  <12.50g    0

# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               data
  PV Size               <12.50 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              3199
  Free PE               0
  Allocated PE          3199
  PV UUID               CPggVR-sUXw-4gfw-lb2v-cq0j-M5ey-Ct7td3

# lvs
  LV      VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  install data -wi-ao----  7.00g
  root    data -wi-ao---- <5.50g


# lvdisplay
  --- Logical volume ---
  LV Path                /dev/data/install
  LV Name                install
  VG Name                data
  LV UUID                ZXQmp5-L17b-eQyS-g3An-msk7-Jqso-MTaTIw
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-09-28 14:42:43 +0000
  LV Status              available
  open                 1
  LV Size                7.00 GiB
  Current LE             1792
  Segments               1
  Allocation             inherit
  Read ahead sectors     aut
  - currently set to     8192
  Block device           253:1



  --- Logical volume ---
  LV Path                /dev/data/root
  LV Name                root
  VG Name                data
  LV UUID                i3eMbC-rcm1-H2Gm-JJeF-jRxO-nL9h-toHSHQ
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-09-28 14:42:55 +0000
  LV Status              available
  # open                 1
  LV Size                <5.50 GiB
  Current LE             1407
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

I am removing one of the logical volumes:

# umount /install
# lvremove /dev/data/install
# sed -i 'N;//dev/mapper/data-install/{s#/dev/mapper/data-install.*n##}' /etc/fstab

After the removal:

# df -h
  Filesystem             Size  Used Avail Use% Mounted on
  /dev/mapper/data-root  5.3G  4.2G  867M  84% /
  ...


# vgs
  VG   #PV #LV #SN Attr   VSize   VFre
  data   1   1   0 wz--n- <12.50g 7.00g


# vgdisplay
  --- Volume group ---
  VG Name               data
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  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               <12.50 GiB
  PE Size               4.00 MiB
  Total PE              3199
  Alloc PE / Size       1407 / <5.50 GiB
  Free  PE / Size       1792 / 7.00 GiB
  VG UUID               jSACsC-wGwy-ki0w-nSCn-kxMy-bLOV-dVN68E


# pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sda2  data lvm2 a--  <12.50g 7.00g


# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               data
  PV Size               <12.50 GiB / not usable 3.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              3199
  Free PE               1792
  Allocated PE          1407
  PV UUID               CPggVR-sUXw-4gfw-lb2v-cq0j-M5ey-Ct7td3


# lvs
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root data -wi-ao---- <5.50g


# lvdisplay
  --- Logical volume ---
  LV Path                /dev/data/root
  LV Name                root
  VG Name                data
  LV UUID                i3eMbC-rcm1-H2Gm-JJeF-jRxO-nL9h-toHSHQ
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-09-28 14:42:55 +0000
  LV Status              available
  # open                 1
  LV Size                <5.50 GiB
  Current LE             1407
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

How can I shrink the physical volume, volume group and finally the partition altogether by what amounts to the free space after removing the logical volume? Preferably that should be done specifying relative values (to pvresize?) or something similar to lvextend -l 100%FREE <lvolume>. Also I would like to accomplish this using the command line alone (no GUI). It is also worth noting that the root/system logical volume is active and in use at all times.

One Answer

Well, pvresize doesn't have an option for relative values like lvextend does, so you need to specify the exact size you want. Here's what you can do:

  1. Shrink physical volume: pvresize --setphysicalvolumesize 5.5G /dev/sda2
  2. Shrink the partition with the tool of your choice. If you want to script it you can use parted. Make the partition a little bit bigger than 5.5G to account for any differences in how the size in bytes is calculated, or ensure you're using the same unit in both tools.
  3. If the units differed between the tools, expand the physical volume to consume the entire partition: pvresize /dev/sda2

Answered by Emmanuel Rosa on December 7, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP