Skip to main content

2) Linux VM (No LVM)

Resize the Disk in PVE

Before you can resize any partitions in the VM, you need to expand the disk size in PVE first: https://notes.benheater.com/books/proxmox/page/resize-the-vm-disk-in-pve 

Disk size was formerly 32 GiB and has been extended 16 GiB to total 48 GiB

This example Linux host has basic partitioning, no LVM, and the entire file system under one partition.

The Partition Table

sudo su

Elevate to root user

image.png

The disk /dev/sda reflects the expanded size of 48G and the partition /dev/sda1 is the target partition, as lsblk shows the file system root -- identified as / -- is mounted here.

fdisk /dev/sda

image.png

fdisk print command shows us the current partition table

image.png

fdisk F command shows the blocks of the unallocated 16G on the disk

Because the unallocated space is not contiguous with the blocks comprising /dev/sda1, we need to shuffle some things around in order to grow the partition.

Command (m for help): quit

Exit fdisk

Shifting Partitions

Keep in mind as you're following along that disk names and partition IDs may vary. In these examples, I'm targeting /dev/sda with a primary partition of /dev/sda1. Shift your inputs according to your environment.

Unmount Swap

swapoff /dev/sda5

Identify Partition Sectors

The output from fdisk F indicates that the end of the disk is at sector 100663295

Effectively, what we are aiming to do is:

  1. Delete the Swap partition
  2. Delete the extended partition
  3. Recreate them, so they are shifted to the end of the disk
  4. Grow partition 1 to the new starting block of the Extended partition

Rebuild the Partitions

fdisk /dev/sda
Command (m for help): d

Delete a partition, when prompted enter 5

Command (m for help): d

Delete a partition, when prompted enter 2

Move the Extended Partition

Command (m for help): n

Create a new partition, enter e for extended, and 2 for the partition ID

From here, we'll take the end of the disk -- 100663295 -- and subtract the size of the original EXTENDED partition 1996802. In other words, 100663295 - 1996802 = 98666493

  • First Sector: 98666493
  • Last Sector: Press ENTER key to use the default value

Add the Swap Partition

Command (m for help): n

Create a new partition, enter l for logical, and 5 for the partition ID

  • First Sector: Press ENTER key to use the default value
  • Last Sector: Press ENTER key to use the default value

Set the Swap Partition Type

Command (m for help): t

Enter 5 for partition 5, enter 82 for Swap

Recreate the Primary Partition

image.png

Print the new partition table entries from memory

image.png

Examining the free space, we can see it now runs contiguous to sda1

Command (m for help): d

Delete a partition, when prompted enter 1

Command (m for help): n

Create a new partition, enter p for Primary, enter 1 for the ID

  • First Sector: Press ENTER key to accept the default value
  • Last Sector: Press ENTER key to accept the default value
  • Do you want to remove the signature? No

Write the Partition Table to the Kernel

Command (m for help): w

This will exit fdisk as well

Resize the Filesystem

resize2fs /dev/sda1

image.png

image.png

Output showing the file system is now resized

Reconfigure Swap

mkswap /dev/sda5
swapon /dev/sda5
blkid /dev/sda5

image.png

Replace the UUID in the current configuration with that of the output from blkid. Backs up current /etc/fstab to /etc/fstab.bak

# Assumes Swap partition is /dev/sda5
sed -i.bak -E "/\s*swap\s*/ s/UUID=[0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12}/UUID=$(blkid /dev/sda5 | cut -d '"' -f 2)/g" /etc/fstab
update-initramfs -u

You should now be safe to reboot and make sure that the system is functioning normally. If anything breaks or you're experiencing data loss, revert to your snapshot and try again.