how to shrink a linux partition used by LVM
To shrink a linux partition, any common tool like gparted
can be used. In case the partition is currently being used by LVM, some previous steps should be performed.
Assuming there’s a 60Gb partition on /dev/sda2 and we want to shrink it 6Gb:
1. Determine the available space (if any) and the name of the LVM components
// get the physical volume name and available space # pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg_main lvm2 a-- 60.00g 0.00g // get logical volumes names # lvs LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert lv_root vg_main -wi-ao-- 56.03g lv_swap vg_main -wi-ao-- 3.97g
2. Resize any logical volumes from the partition
// reduce 6G from lv_root # lvresize --verbose --resizefs -L -6G /dev/vg_main/lv_root
4. Move the contents of the logical volumes together
The previous command will leave a gap of free space between lv_root
and lv_swap
:
# pvs -v --segments Scanning for physical volume names PV VG Fmt Attr PSize PFree Start SSize LV Start Type PE Ranges /dev/sda2 vg_main lvm2 a-- 60.00g 6.03g 0 12800 lv_root 0 linear /dev/sda2:0-12799 /dev/sda2 vg_main lvm2 a-- 60.00g 6.03g 12800 1543 0 free /dev/sda2 vg_main lvm2 a-- 60.00g 6.03g 14343 1016 lv_swap 0 linear /dev/sda2:14343-15358
To remove that gap, execute:
// the range 14343-15358 is obtained from the previous command # pvmove --alloc anywhere /dev/sda2:14343-15358
3. Resize the physical volume
# pvresize --setphysicalvolumesize 50 /dev/sda2
If the above command return and error like this:
/dev/sda5: cannot resize to xxxxx extents as later ones are allocated.
then it means that there’s a gap of free space between logical volumes (check step 4.) or the new size is too small for the current logical volumes (specify a larger size or further reduce the logical volumes)
4. Resize the partition
Now the partition is ready to be resized using gparted
or similar
Leave a Reply