Proxmox resize linux guest partition

I sometimes come across that my partitions are too small in a VM Guest. And I can never remember the exact steps involved when resizing them. This post explains the steps needed to resize the root partition (can be any partition) of a VM.

The machines I am working with are CentOS 7 machines and they are using LVM.

I am going to be using fdisk to resize the disk instead of gparted or similar.

This is what we are starting with (df -h)

Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  7.5G  2.2G  5.3G  30% /

Since I don’t need 100% uptime I can shutdown the VM to be resized (can probably be done live see https://pve.proxmox.com/wiki/Resize_disks)

Resize the disk in proxmox:

  1. Locate the VM to resize in the Proxmox web interface
  2. Select Hardware
  3. Find the Hard Disk to resize in the list of hardware
  4. Select Resize disk and enter the increment of disk size (in this example I selected 1 GB)

Note: Any snapshots on the machine needs to be removed before resize is allowed
Note 2: If the disk image is located on a nfs share, it will probably not work

Now we can boot up the machine again…

If we try the above df -h we can see that nothing has changed, this is because the partitions are the same, lets change that…

Using fdisk -l I get a list of all disks in the machine, here I can see that /dev/vda contains the LVM partition (/dev/vda2) that I want to resize

Disk /dev/vda: 11.8 GB, 11811160064 bytes, 23068672 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b205b

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    20930559     9952256   8e  Linux LVM

So lets edit the partition map (here is the part that I find scary since we are going to delete the partition…)

# fdisk /dev/vda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p

Disk /dev/vda: 11.8 GB, 11811160064 bytes, 23068672 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b205b

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    20930559     9952256   8e  Linux LVM

Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p):
Using default response p
Partition number (2-4, default 2):
First sector (1026048-23068671, default 1026048):
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-23068671, default 23068671):
Using default value 23068671
Partition 2 of type Linux and of size 10.5 GiB is set

Command (m for help): t
Partition number (1,2, default 2):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/vda: 11.8 GB, 11811160064 bytes, 23068672 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b205b

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    23068671    11021312   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

So what did we do:

  1. Entering the command fdisk /dev/vda on the command line allows us to edit /dev/vda
  2. Entering the p command lists the current partition table
  3. Entering d and then 2 allows us to delete the partition /dev/vda2
  4. Now we can create the new partition by entering n and using default values by pressing ENTER
  5. Then we change the type of the partition from Linux to Linux LVM by entering command t and the code 8e
  6. After that we print the new partition table using p
  7. And finally we write the partition table to disk using w

Now in order for the system to notice the change there are probably several ways to do this, but the easiest is to reboot. So let’s do that now…

Next it is time to resize the lvm, for reference, this is how it looks before.

Physical volume

# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               centos
  PV Size               9.49 GiB / not usable 3.00 MiB
  ...
  Free PE               1
  Allocated PE          2428

Volume Group

# vgdisplay
  --- Volume group ---
  VG Name               centos
  ...
  VG Size               9.49 GiB
  ...
  Total PE              2429
  Alloc PE / Size       2428 / 9.48 GiB
  Free  PE / Size       1 / 4.00 MiB

Logical volume

# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  ...
  LV Size                7.49 GiB

  --- Logical volume ---
  LV Path                /dev/centos/swap
  ...

So start by resizing the Physical volume:

# pvresize /dev/vda2
  Physical volume "/dev/vda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

Running a pvdisplay show that the physical volume has changed from

PV Size               9.49 GiB / not usable 3.00 MiB
...
Free PE               1

To

PV Size               10.51 GiB / not usable 2.00 MiB
...
Free PE               262

We can also see that the volume group (vgdisplay) now has increased FreePE.

Let’s extend the Logical volume

# lvresize --extents +100%FREE --resizefs /dev/centos/root
  Size of logical volume centos/root changed from 7.49 GiB (1917 extents) to 8.49 GiB (2173 extents).
  Logical volume centos/root successfully resized.

Here I entered the path to the logical volume /dev/centos/root which can be found in the output of the lvdisplay command on LV Path.

Now the resize is complete and the new output from the previous commands are

Physical volume

# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               centos
  PV Size               10.51 GiB / not usable 2.00 MiB
  ...
  Free PE               0
  Allocated PE          2690

Volume Group

# vgdisplay
  --- Volume group ---
  VG Name               centos
  ...
  VG Size               10.51 GiB
  ...
  Total PE              2690
  Alloc PE / Size       2690 / 10.51 GiB
  Free  PE / Size       0 / 0   

Logical volume

# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  ...
  LV Size                8.51 GiB

  --- Logical volume ---
  LV Path                /dev/centos/swap
  ...

Disk space usage

# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  8.6G  2.2G  6.4G  26% /
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s