1. Home
  2. How To
  3. Dedicated Server
  4. Linux Cloud Server
  5. Shrink / Reduce LVM Partition on CentOS

Shrink / Reduce LVM Partition on CentOS

Objective

To be able to reduce a client’s CentOS VM disk partition should they wish to downgrade the VM’s specifications.

Prerequisites

  1. CentOS 7 / CentOS 8
  2. Basic knowledge on using gparted ISO (https://gparted.org/download.php)
  3. Proxmox VE

Steps

In a typical CentOS installation, the disk is partitioned into root, boot, swap and the remainder into the /home directory. The lsblk command can used to list the current block devices.

In this article, the partition size of /home directory will be adjusted.

Step 1: Backup the Home Directory

Prepare a backup of the contents in the /home directory in prior to the exercise.

tar -czvf /root/home.tgz -C /home .

Verify the backup by running the following command: –

tar -tvf /root/home.tgz

Step 2: Reduce the Partition Size of Home Directory

Remove the /home directory as the mounted point for /centos-home.

umount /dev/mapper/centos-home

Next, remove the logical volume.

lvremove /dev/mapper/centos-home

Recreate a new logical volume with your desired size for /home directory

lvcreate -L 100GB -n home centos

After that, ensure it is properly formatted and mount the logical volume to the /home directory

mkfs.xfs /dev/centos/home
mount /dev/mapper/centos-home

Restore the contents of the /home directory with the following command: –

tar -xzvf /root/home.tgz -C /home

Step 3: Boot Up the Gparted ISO

Since the logical volume has been recreated with a smaller size. The remaining capacity will be extracted and marked as non-allocated storage with the help of a boot-able partitioning tool, GParted (Refer to the Prerequisites for the download link).

On Proxmox VE, navigate to the VM’s Options and modify the boot order by setting CD/DVD to be booted up first.

Once the ISO is booted up, simply go through the initial setup process by tapping Enter. You should be directed to the desktop screen.

Do note that simply resizing the partition may result an message similar to the following: –

dev/sdax: cannot resize to xxxxx extents as later ones are allocated.

To address this, it is essential to relocate the unallocated space at the end of the LVM after the root and swap partitions. Run the pvs command in the terminal to view the current volume arrangement

pvs -v --segments /dev/sdaX

Here is an example of output after running the pvs command: –

/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

Note that the free / unallocated space resides between the root and swap partition.

Run the pvmove command to remove the external fragmentation

sudo pvmove --alloc anywhere /dev/sda5:yyyy-end

Run the pvs command to view the changes.

Next, simply use GParted to shrink the LVM.

Step 4: Shrink Disk Size at Proxmox VE

To reduce the disk size of the actual VM, shut down the VM and run the following command depending on the disk extension: –

# If LV:
lvreduce -L 5G /dev/pve/disk-name (reduce to 5G)
lvreduce -L -5G /dev/pve/disk-name (reduce by 5G)

# If qcow2/raw:
qemu-img resize --shrink <vmfile.qcow2> [+-] or size

# If ZFS:
zfs set volsize=<new size>G rpool/data/vm-<vm id>-disk-<disk number>

Next, edit the VM config file located at /etc/pve/local/qemu-server/<vmid>.conf by manually entering the new disk size.

Make any changes towards the VM’s hardware settings such as editing the disk’s cache settings to view the updated config via the Proxmox GUI.

Boot up the VM and verify the changes by running the df or lsblk command.

Updated on December 15, 2022

Was this article helpful?

Related Articles

Need Help?
Submit a ticket to us and let our professional team assists you

Support Billing Sales
Support
Billing
Sales