Resizing VM disk in Proxmox

I have been using VM templates for a while to instantly create a VM and these VM templates has a fixed size vDisk. I wanted to create a smaller vDisk template let’s say 5GB of vDisk. When I do a full clone, I would simply resize the vDisk to a desire vDisk size.

I am using Debian 10 as my VM OS and I installed Debian to use all the disk space with LVM. The LVM part will play a big role here. This post assumption is that there is a Linux VM that uses LVM template that already exist. It does not matter which distro as long as it is Linux and it uses LVM. Check the Proxmox wiki on how to create a template. Once you have a template, create a full clone and start the VM.

There are three steps,

  1. Resize the hard disk of the VM via the Proxmox web UI
  2. Enlarge the partition within the VM
  3. Enlarge the filesystem within the VM

Resizing the vDisk

I can’t really say about in Windows, but in Linux you can resize the disk while the VM is running. To resize the vDisk, click on the VM and select the following as shown in Figure 1.

  1. Select the Hardware option
  2. Select the Hard Disk
  3. Enter the size you want to add to the existing hard disk
  4. Click on Resize
Figure 1

We have to access the VM’s shell either via SSH or VM the Console. In Figure 2, the output of the lsblk command shows that the /dev/sda is now 10GB; however, the LVM does not have this information, and the extra 5GB we added is currently in Unpartition space as shown in Figure 2. To fix this, we need to extend the partition using fdisk and add the unused space to the LVM.

Figure 2

Extending the root partition with fdisk

Enter the following command – fdisk /dev/sda. This will allow us to make some modification to the existing partition. The option p will print the current partition table. The one we are going to work on is the /dev/sda2. If you look at the Type, it says Extended.

[email protected]:~# fdisk /dev/sda

Welcome to fdisk (util-linux 2.33.1).
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/sda: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: QEMU HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x8f60f197

Device     Boot   Start      End Sectors  Size Id Type
/dev/sda1  *       2048   999423  997376  487M 83 Linux
/dev/sda2       1001470 10483711 9482242  4.5G  5 Extended
/dev/sda5       1001472 10483711 9482240  4.5G 8e Linux LVM
  1. We need to delete the /dev/sda2 partition and recreate it. To do so, we need to use the option d to delete and enter 2 to select the partition #2
  2. Enter the option n for the new partition
    1. For the Partition type, enter e for extended
    2. Keep the default partition number which is # 2
    3. Select the default value for the First sector
    4. Select default value for the Last sector

Should see the partition 2 is now 9.5 GB.

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

Partition 2 has been deleted.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): e
Partition number (2-4, default 2): 
First sector (999424-20971519, default 999424): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (999424-20971519, default 20971519): 

Created a new partition 2 of type 'Extended' and of size 9.5 GiB.

We need to create a new partition for the LVM. To do so.

  1. Enter n for new partition and it will create a logical partition 5
    1. Select the default First sector
    2. Select the default Last sector
    3. Enter No to keep the signature
  2. Enter p to print the new partition table and verify the partition size

It should show now that the /dev/sda2 and /dev/sda5 are both 9.5G.

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (1001472-20971519, default 1001472): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1001472-20971519, default 20971519): 

Created a new partition 5 of type 'Linux' and of size 9.5 GiB.
Partition #5 contains a LVM2_member signature.

Do you want to remove the signature? [Y]es/[N]o: No

Command (m for help): p

Disk /dev/sda: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: QEMU HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x8f60f197

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048   999423   997376  487M 83 Linux
/dev/sda2        999424 20971519 19972096  9.5G  5 Extended
/dev/sda5       1001472 20971519 19970048  9.5G 83 Linux

Command (m for help):  

We are almost done with fdisk. We need to change the /dev/sda5 Type from Linux to LVM.

  1. Enter t to change the partition type
    1. Select 5 if it is not the default
    2. Enter 8e for Linux LVM type – enter L to see the list of partition types options
    3. Enter p to verify the partition type for /dev/sda5
  2. If everything looks good, enter w to save the changes
Command (m for help): t
Partition number (1,2,5, default 5): 
Hex code (type L to list all codes): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): p
Disk /dev/sda: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: QEMU HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x8f60f197

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048   999423   997376  487M 83 Linux
/dev/sda2        999424 20971519 19972096  9.5G  5 Extended
/dev/sda5       1001472 20971519 19970048  9.5G 8e Linux LVM

Command (m for help): w
The partition table has been altered.
Failed to add partition 2 to system: Device or resource busy

The kernel still uses the old partitions. The new table will be used at the next reboot. 
Syncing disks.

Reboot the VM for the changes to take effect. After the reboot, the df -h command will still show the / is still 5GB instead of 10GB as shown in the snippet below.

[email protected]:~# df -h
Filesystem                   Size  Used Avail Use% Mounted on
udev                         982M     0  982M   0% /dev
tmpfs                        200M  5.4M  195M   3% /run
/dev/mapper/debian--vg-root  3.5G  1.3G  2.1G  38% /
tmpfs                        998M     0  998M   0% /dev/shm
tmpfs                        5.0M     0  5.0M   0% /run/lock
tmpfs                        998M     0  998M   0% /sys/fs/cgroup
/dev/sda1                    472M   85M  364M  19% /boot
tmpfs                        200M     0  200M   0% /run/user/1000

Extending LVM

We need to extend the physical volume and logical volume. First, we need to start with the physical volume. We need to use the extra 5GB we added from the beginning of this post.

[email protected]:~# pvresize /dev/sda5
  Physical volume "/dev/sda5" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized

We need to extend the logical volume to take the remaining unused space. The command below:

  • -r option is for the resize2fs
  • -l option for extend
  • +100%FREE is to use all the remaining free space
  • /dev/mapper/debian–vg-root is the target filesystem
[email protected]:~# lvextend -r -l +100%FREE /dev/mapper/debian--vg-root
  Size of logical volume debian-vg/root changed from 3.56 GiB (912 extents) to 8.56 GiB (2192 extents).
  Logical volume debian-vg/root successfully resized.
resize2fs 1.44.5 (15-Dec-2018)
Filesystem at /dev/mapper/debian--vg-root is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/mapper/debian--vg-root is now 2244608 (4k) blocks long.

Here is the output of the df -h command.

[email protected]:~# df -h
Filesystem                   Size  Used Avail Use% Mounted on
udev                         982M     0  982M   0% /dev
tmpfs                        200M  5.4M  195M   3% /run
/dev/mapper/debian--vg-root  8.4G  1.3G  6.8G  16% /
tmpfs                        998M     0  998M   0% /dev/shm
tmpfs                        5.0M     0  5.0M   0% /run/lock
tmpfs                        998M     0  998M   0% /sys/fs/cgroup
/dev/sda1                    472M   85M  364M  19% /boot
tmpfs                        200M     0  200M   0% /run/user/1000

As you can see, it is showing 8.4G now. The post looks very long, but it is a ~5 minute job. This is it. Hope you’ll find this use full.

Cheers!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
WordPress Appliance - Powered by TurnKey Linux