How to Reduce or Shrink Physical Volume in Linux

|
Last Updated:
|
|

How do I shrink physical volume in Linux? In this tutorial, you will learn how to reduce or shrink Physical Volume in Linux. In a Linux system, a hard drive, or a partition or a group of them can be allocated a Physical Volume for storing data. Sometimes, you may assign a system more disk space than is required. If you are managing such storage devices via LVM, it would really be easy for you to reduce or shrink physical volume in Linux.

Reduce or Shrink Physical Volume in Linux

So, how do you reduce physical volume? Well, this is a not a walk in the park procedure especially if you are dealing with a physical volume that is mounted and it is containing root filesystem.

In order to reduce or shrink physical volume in Linux;

Backup your System

Attempting to shrink a physical volume at least with root filesystem might make you loose your data. As a precaution, if there is a really a need for you to reduce or shrink physical volume, then at least backup the system.

You can backup by cloning the system, or taking snapshot to allow you restore in case the procedure doesn’t work out.

Attach and Boot your System from Live CD

On-line resize of root filesystem not possible. Thus, to ensure that we can work on the partition seamlessly, download and attach Live CD into your Linux system.

As already mentioned, we are using Ubuntu 22.04 headless server as an example in this guide;

Current disk usage;

df -hT

Sample output;


Filesystem                        Type   Size  Used Avail Use% Mounted on
tmpfs                             tmpfs  198M  1.2M  197M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4    37G  7.3G   28G  21% /
tmpfs                             tmpfs  988M     0  988M   0% /dev/shm
tmpfs                             tmpfs  5.0M     0  5.0M   0% /run/lock
/dev/sda2                         ext4   1.8G  245M  1.4G  15% /boot
tmpfs                             tmpfs  198M  4.0K  198M   1% /run/user/1000

As you can see, we only have 7.3G disk space used out of 37G. We will need to shrink this to at least 15G total disk space.

Thus;

  • power off your virtual machine
  • attach a live CD and configure the machine to boot via Live CD
How to Reduce or Shrink Physical Volume in Linux

Reduce or Shrink Logical Volume

To reduce or shrink Physical Volume in Linux, begin by shrinking logical volume and the underlying filesystem.

Logical volume details can be displayed using lvs or lvdisplay command;

lvdisplay

Sample output;


  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu-lv
  LV Name                ubuntu-lv
  VG Name                ubuntu-vg
  LV UUID                nmfKLT-IsWR-FfLX-CspJ-yaGz-CwR7-cGKSk2
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2022-04-29 05:05:06 +0000
  LV Status              available
  # open                 0
  LV Size                <37.29 GiB
  Current LE             9545
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

If the volumes are not activated on system boot, activate using the command below;

vgchange -ay

To shrink the logical volume and the underlying filesystem, use lvreduce --resizefs command as follows;

lvreduce [-r|--resizefs] [-L|--size [-]Size[m|UNIT] <path/to/LV>

e.g;

lvreduce -r -L 15G /dev/ubuntu-vg/ubuntu-lv

Sample output;


fsck from util-linux 2.37.2
/dev/mapper/ubuntu--vg-ubuntu--lv: 164766/2449408 files (0.1% non-contiguous), 2093538/9774080 blocks
resize2fs 1.46.5 (30-Dec-2021)
Resizing the filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv to 3932160 (4k) blocks.
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 3932160 (4k) blocks long.

  Size of logical volume ubuntu-vg/ubuntu-lv changed from <37.29 GiB (9545 extents) to 15.00 GiB (3840 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.

Confirm the shrinkage;

lvdisplay

  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu-lv
  LV Name                ubuntu-lv
  VG Name                ubuntu-vg
  LV UUID                nmfKLT-IsWR-FfLX-CspJ-yaGz-CwR7-cGKSk2
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2022-04-29 05:05:06 +0000
  LV Status              available
  # open                 0
  LV Size                15.00 GiB
  Current LE             3840
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

If all is good, then proceed!

Reduce or Shrink Physical Volume

You can now proceed to reduce or shrink Physical volume.

At the moment, our physical volume, /dev/sda3, is ~37.29g. We will shrink it as well to 15G.

You can show the details about Physical volume using pvs or pvdisplay command.

pvdisplay

  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               ubuntu-vg
  PV Size               <37.29 GiB / not usable 2.98 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              9545
  Free PE               5705
  Allocated PE          3840
  PV UUID               2l4lAf-vXju-afXg-LfeQ-jcl2-Q3mR-99iXpg

In our example, we need to resize our physical volume to 15G.

As you can see from the example above, we have Allocated PE of 3840 each of size 4.00 MiB. This totals to 15360 MiB (15 GiB) of space.

To ensure that the last sector is counted while resizing, add 1 Extent (4 MiB).

You can get the information about the start and length of each physical extents;

pvs -v --segments /dev/sda3

Sample output;

  PV         VG        Fmt  Attr PSize  PFree Start SSize LV        Start Type   PE Ranges       
  /dev/sda3  ubuntu-vg lvm2 a--  15.00g    0      0  3840 ubuntu-lv     0 linear /dev/sda3:0-3839

The total allocated extents are 3840. When you add 1 extent, it becomes 3841 extents which is equivalent to (3841x4) = 15364 MiB.

Since pvresize command doesn't take extents an option, we can use the total volume size in MiB. Hence, to reduce or shrink our volume size to 15G, we can use the command;

pvresize /dev/sda3 --setphysicalvolumesize 15364

Sample output;

/dev/sda3: Requested size 15.00 GiB is less than real size <37.29 GiB. Proceed?  [y/n]: y
  WARNING: /dev/sda3: Pretending size is 31465472 not 78200799 sectors.
  Physical volume "/dev/sda3" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized

You may get an output that says cannot resize to XXXX extents as YYYY are allocated. In this case, you need to ensure that your segments are aligned properly.

Confirm new Physical volume size;

pvdisplay

  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               ubuntu-vg
  PV Size               15.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              3840
  Free PE               0
  Allocated PE          3840
  PV UUID               2l4lAf-vXju-afXg-LfeQ-jcl2-Q3mR-99iXpg

Reduce or Shrink the Physical Volume Partition

Disks with Primary Partitions Only

You can now shrink the physical volume partition. You can use fdisk command;

fdisk /dev/sda

List the partitions by pressing p.


Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Command (m for help): p

Sample output;


Disk /dev/sda: 39.06 GiB, 41943040000 bytes, 81920000 sectors
Disk model: VBOX HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 55B779AD-FE40-4978-9911-787F868D006B

Device       Start      End  Sectors  Size Type
/dev/sda1     2048     4095     2048    1M BIOS boot
/dev/sda2     4096  3719167  3715072  1.8G Linux filesystem
/dev/sda3  3719168 81919966 78200799 37.3G Linux filesystem

We need to delete root partition in this case, /dev/sda3;

Hence, press d and select partition 3;

Command (m for help): d
Partition number (1-3, default 3): 

Partition 3 has been deleted.

Command (m for help):

Create a new partition and allocate 15364M (Note that we set the PV to 15364 MiB) and when prompted to remove the signature, select No;


Command (m for help): n
Partition number (3-128, default 3): 
First sector (3719168-81919966, default 3719168): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (3719168-81919966, default 81919966): +15364M

Created a new partition 3 of type 'Linux filesystem' and of size 15 GiB.
Partition #3 contains a LVM2_member signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help):

Print the partition tables again to confirm the size;


Command (m for help): p

Disk /dev/sda: 39.06 GiB, 41943040000 bytes, 81920000 sectors
Disk model: VBOX HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 55B779AD-FE40-4978-9911-787F868D006B

Device       Start      End  Sectors  Size Type
/dev/sda1     2048     4095     2048    1M BIOS boot
/dev/sda2     4096  3719167  3715072  1.8G Linux filesystem
/dev/sda3  3719168 35176447 31457280   15G Linux filesystem

Command (m for help):

Write changes and exit fdisk;

Command (m for help): w
The partition table has been altered.
Syncing disks.

Disks with Primary and Extended/Logical Partitions

What if you have a disk with Extended and logical partitions? And the first logical partition is is the one with the disk physical volume?

This is an example;

fdisk -l /dev/sda

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: QEMU HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x75b85259

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048   999423   997376  487M 83 Linux
/dev/sda2       1001470 41940991 40939522 19.5G  5 Extended
/dev/sda5       1001472 41940991 40939520 19.5G 8e Linux LVM

You need to delete the logical and extended partition and recreate them as shown below;

fdisk /dev/sda

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Command (m for help):

List the partitions by typing p and press ENTER;


Command (m for help): p

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: QEMU HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x75b85259

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048   999423   997376  487M 83 Linux
/dev/sda2       1001470 41940991 40939522 19.5G  5 Extended
/dev/sda5       1001472 41940991 40939520 19.5G 8e Linux LVM

Command (m for help):

So, we need to delete the Logical partition (/dev/sda5) and Extended partition (/dev/sda2) using d option;


Command (m for help): d
Partition number (1,2,5, default 5): 

Partition 5 has been deleted.

Command (m for help): d
Partition number (1,2, default 2): 

Partition 2 has been deleted.

Command (m for help):

Next, create the Extended partition. The size should be exactly same as the physical volume size. Consider this physical size;

pvdisplay

  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               bookworm-vg
  PV Size               10.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              2560
  Free PE               0
  Allocated PE          2560
  PV UUID               JK72C1-ginB-1kph-t96A-NV4c-8H3e-6wxgpc

So, in this case, we would set the size to (2560+1)x4=10244MiB. Hence, in this case, this should be our Extended partition size.

Hence, type n and press enter to create new Extended partition;

Partition type is Extended (e)


Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): e
Partition number (2-4, default 2): 
First sector (999424-41943039, default 999424): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (999424-41943039, default 41943039): +10244M

Created a new partition 2 of type 'Extended' and of size 10 GiB.

Next, create another parition, which is the logical partition. It should be same size as the extended partition above. While creating the logical partition, you will be prompted whether to remove the signature. PLEASE DO NOT. Enter N for No!

Partition type is Logical (l);


Command (m for help): n
Partition type
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l

Adding logical partition 5
First sector (1001472-21979135, default 1001472): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1001472-21979135, default 21979135): +10244M

Created a new partition 5 of type 'Linux' and of size 10 GiB.
Partition #5 contains a LVM2_member signature.

Do you want to remove the signature? [Y]es/[N]o: N

Write the changes to the disk by typing w.

Detach Live CD from the System

Shutdown the system and remove live CD iso.

After that, boot the system.

Confirm Physical Volume Resize/Shrink

Check if all is good.

pvdisplay

  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               ubuntu-vg
  PV Size               15.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              3840
  Free PE               0
  Allocated PE          3840
  PV UUID               2l4lAf-vXju-afXg-LfeQ-jcl2-Q3mR-99iXpg

Check disk usage;

df -hT

Filesystem                        Type   Size  Used Avail Use% Mounted on
tmpfs                             tmpfs  198M  1.2M  197M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4    15G  7.3G  6.8G  53% /
tmpfs                             tmpfs  988M     0  988M   0% /dev/shm
tmpfs                             tmpfs  5.0M     0  5.0M   0% /run/lock
/dev/sda2                         ext4   1.8G  245M  1.4G  15% /boot
tmpfs                             tmpfs  198M  4.0K  198M   1% /run/user/1000

And there you go! You have learnt how to successfully reduce or shrink physical volume in Linux (Ubuntu 22.04) using live CD!

That marks the end of our tutorial on reducing or shrinking Physical Volume in Linux.

Other Tutorials

Easy Way to Extend KVM Virtual Machine Disk Size

Change Windows Boot Device from IDE to Virtio on KVM

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
Kifarunix
Linux Certified Engineer, with a passion for open-source technology and a strong understanding of Linux systems. With experience in system administration, troubleshooting, and automation, I am skilled in maintaining and optimizing Linux infrastructure.

Leave a Comment