How to reduce Volume Group size in LVM?
These are the steps required to resize an LVM or LVM2 partition:
sudo lvresize --verbose --resizefs -L -150G /dev/ubuntu/root
sudo pvresize --setphysicalvolumesize {any size here} /dev/sda5
The last command, pvresize
, may yield the error
/dev/sda5: cannot resize to xxxxx extents as later ones are allocated.
You have to rearrange the unallocated space at the end of the LVM. That means after root and swap_1 partition. You can see the current arrangement of space with this command
pvs -v --segments /dev/sda5
pvs
will show output like this
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g 0 xxx+1 root 0 linear /dev/sda:0-xxx
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g xxx+1 iii 0 free
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g yyyy jjj swap 0 linear /dev/sda5:yyyy-end
Now use pvmove
to remove external fragmentation:
sudo pvmove --alloc anywhere /dev/sda5:yyyy-end
Now let's see if moving the swap volume succeeded.
pvs -v --segments /dev/sda5
should show the new order of volumes:
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g 0 xxx+1 root 0 linear /dev/sda:0-xxx
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g xxx+1 iii swap 0 linear /dev/sda5:xxx+1-yyyy
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g yyyy+1 end 0 free
After that, use GParted and resize the LVM to the maximum used area. The rest will be in unallocated space.
You can use pvmove
to move those extents to the beginning of the device or another device:
sudo pvmove --alloc anywhere /dev/device:60000-76182
Then pvmove
chooses where to move the extents to, or you can specify where to move them.
See pvs -v --segments /dev/device
to see what extents are currently allocated.