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.
How to 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
In this tutorial, we will be learning how to reduce or shrink physical volume in Linux (Ubuntu system) with root filesystem. This means that online resize is 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
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 logical volumes are 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;
lvresize [-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
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.
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 how to reduce or shrink Physical Volume in Linux.