How to resize any VMs Hard disk size

Pre-Requiste

First Confirm if your Linux machine was created using LVM or not, execute the following commands

pvdisplay
vgdisplay
lvdisplay

if these commands show some out then it means that VM was created through LVM and you can resize it. 

now install GRUB in /dev/sda/ so that when you resize the HDD, BIOS can boot the machine.

/sbin/grub-install /dev/sda/

Commands to be Executed on Host OS (Windows 10)

Now stop your VM and open Powershell in host OS (Windows 10) as administrator

cd 'C:\Program Files\Oracle\VirtualBox\'

Our Virtua Box VM name is Homer10

.\VBoxManage.exe list hdds | Select-String Homer10

copy the file path

.\VBoxManage.exe modifyhd 'D:\VirtualBox VMs\Homer10\Homer10.vmdk' --resize 30720

Caution

Above mentioned command will resize the disk to 30GB

Some Sample commands

.\VBoxManage.exe -?
.\VBoxManage.exe list vms
.\VBoxManage.exe list hdds
.\VBoxManage.exe list runningvms
.\VBoxManage.exe showvminfo Homer10
.\VBoxManage.exe guestcontrol Homer10 list all

Commands to be Executed on Guest OS

Now start Guest OS (Debian) again first confirm is lvm2 installed or not

ls -lhtr /etc/lvm/

take back of the configuration

vgcfgbackup 

in case we have to revert the changes execute the following command

vgcfgrestore 

Caution

Execute above mentioned command if you want to revert the changes.

Concept

basic-lvm-volume

We will First Create

  1. Create new Partition from the extended Disk
  2. Create Physical Volume with Extended Disk
  3. Then We will Extend the Volume Group with the new Created Phsical Volume
  4. at the end We will Increase/extend the Logical Volume

Step 1

List Partitions - fdisk -l

root@debian12-lvm:/home/hrhashmi# fdisk -l
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Disk model: VBOX 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: 0x58a53dff

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048   999423   997376  487M 83 Linux
/dev/sda2        1001470 31455231 30453762 14.5G  5 Extended
/dev/sda3       31455232 41940991 10485760    5G 83 Linux
/dev/sda5        1001472 31455231 30453760 14.5G 8e Linux LVM

Partition table entries are not in disk order.


Disk /dev/mapper/debian12--lvm--vg-root: 3.11 GiB, 3338665984 bytes, 6520832 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 /dev/mapper/debian12--lvm--vg-var: 6.34 GiB, 6811549696 bytes, 13303808 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 /dev/mapper/debian12--lvm--vg-swap_1: 976 MiB, 1023410176 bytes, 1998848 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 /dev/mapper/debian12--lvm--vg-tmp: 312 MiB, 327155712 bytes, 638976 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 /dev/mapper/debian12--lvm--vg-home: 8.8 GiB, 9453961216 bytes, 18464768 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

Create partition /dev/sda4 using the command fdisk /dev/sda

root@debian12-lvm:/home/hrhashmi# fdisk /dev/sda

Welcome to fdisk (util-linux 2.38.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Command (m for help): n
Partition type
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): p

Selected partition 4
First sector (41940992-62914559, default 41940992):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (41940992-62914559, default 62914559):

Created a new partition 4 of type 'Linux' and of size 10 GiB.

Command (m for help): w
The partition table has been altered.
Syncing disks.

List Partitions again - fdisk -l

root@debian12-lvm:/home/hrhashmi# fdisk -l
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Disk model: VBOX 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: 0x58a53dff

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048   999423   997376  487M 83 Linux
/dev/sda2        1001470 31455231 30453762 14.5G  5 Extended
/dev/sda3       31455232 41940991 10485760    5G 83 Linux
/dev/sda4       41940992 62914559 20973568   10G 83 Linux
/dev/sda5        1001472 31455231 30453760 14.5G 8e Linux LVM

Partition table entries are not in disk order.


Disk /dev/mapper/debian12--lvm--vg-root: 3.11 GiB, 3338665984 bytes, 6520832 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 /dev/mapper/debian12--lvm--vg-var: 6.34 GiB, 6811549696 bytes, 13303808 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 /dev/mapper/debian12--lvm--vg-swap_1: 976 MiB, 1023410176 bytes, 1998848 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 /dev/mapper/debian12--lvm--vg-tmp: 312 MiB, 327155712 bytes, 638976 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 /dev/mapper/debian12--lvm--vg-home: 8.8 GiB, 9453961216 bytes, 18464768 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

Step 2

List Physical Volume - pvdisplay

root@debian12-lvm:/home/hrhashmi# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               debian12-lvm-vg
  PV Size               14.52 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              3717
  Free PE               0
  Allocated PE          3717
  PV UUID               1isik1-ln1a-jTlc-N01a-AlIq-Mvqp-wbyVhX

  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               debian12-lvm-vg
  PV Size               5.00 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              1279
  Free PE               0
  Allocated PE          1279
  PV UUID               372Vwo-P9e3-N5CJ-8wyR-W33i-mwsF-tPwTFs

Create Physical Volume - pvcreate

root@debian12-lvm:/home/hrhashmi# pvcreate /dev/sda4
  Physical volume "/dev/sda4" successfully created.

List Physical Volume - pvdisplay

root@debian12-lvm:/home/hrhashmi# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               debian12-lvm-vg
  PV Size               14.52 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              3717
  Free PE               0
  Allocated PE          3717
  PV UUID               1isik1-ln1a-jTlc-N01a-AlIq-Mvqp-wbyVhX

  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               debian12-lvm-vg
  PV Size               5.00 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              1279
  Free PE               0
  Allocated PE          1279
  PV UUID               372Vwo-P9e3-N5CJ-8wyR-W33i-mwsF-tPwTFs

  "/dev/sda4" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda4
  VG Name
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               5uNwTs-frxH-mdif-4c6r-7rFa-bs3p-ezoD0k

Step 3

List Volume Group - vgdisplay

root@debian12-lvm:/home/hrhashmi# vgdisplay
  --- Volume group ---
  VG Name               debian12-lvm-vg
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  8
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                5
  Open LV               5
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               <19.52 GiB
  PE Size               4.00 MiB
  Total PE              4996
  Alloc PE / Size       4996 / <19.52 GiB
  Free  PE / Size       0 / 0
  VG UUID               mWs90Z-F70k-sBbn-FTsM-fcNX-VYAR-AxdIOx

Extend Volume Group - vgextend

root@debian12-lvm:/home/hrhashmi# vgextend debian12-lvm-vg /dev/sda4
  Volume group "debian12-lvm-vg" successfully extended

List Volume Groups - vgdisplay

root@debian12-lvm:/home/hrhashmi# vgdisplay
  --- Volume group ---
  VG Name               debian12-lvm-vg
  System ID
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  9
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                5
  Open LV               5
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <29.52 GiB
  PE Size               4.00 MiB
  Total PE              7556
  Alloc PE / Size       4996 / <19.52 GiB
  Free  PE / Size       2560 / 10.00 GiB
  VG UUID               mWs90Z-F70k-sBbn-FTsM-fcNX-VYAR-AxdIOx

Step 4

list Logical Volume - lvdisplay

root@debian12-lvm:/home/hrhashmi# lvdisplay
  --- Logical volume ---
  LV Path                /dev/debian12-lvm-vg/root
  LV Name                root
  VG Name                debian12-lvm-vg
  LV UUID                C6NF4v-55K3-HfmX-lgwh-g6TY-Y9Nx-RgpTb5
  LV Write Access        read/write
  LV Creation host, time debian12-lvm, 2024-05-03 08:25:46 -0500
  LV Status              available
  # open                 1
  LV Size                <3.11 GiB
  Current LE             796
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0

  --- Logical volume ---
  LV Path                /dev/debian12-lvm-vg/var
  LV Name                var
  VG Name                debian12-lvm-vg
  LV UUID                t3nBlr-UUwq-NHmi-hpGw-10S2-wKz3-6qT764
  LV Write Access        read/write
  LV Creation host, time debian12-lvm, 2024-05-03 08:25:46 -0500
  LV Status              available
  # open                 1
  LV Size                6.34 GiB
  Current LE             1624
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:1

  --- Logical volume ---
  LV Path                /dev/debian12-lvm-vg/swap_1
  LV Name                swap_1
  VG Name                debian12-lvm-vg
  LV UUID                x2KiSn-172o-13nK-Jxlh-OZUa-uOOz-7Uswam
  LV Write Access        read/write
  LV Creation host, time debian12-lvm, 2024-05-03 08:25:46 -0500
  LV Status              available
  # open                 2
  LV Size                976.00 MiB
  Current LE             244
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:2

  --- Logical volume ---
  LV Path                /dev/debian12-lvm-vg/tmp
  LV Name                tmp
  VG Name                debian12-lvm-vg
  LV UUID                4s7qlW-CtjS-gzG6-DdMS-Q0Jw-f7h7-QcqNmu
  LV Write Access        read/write
  LV Creation host, time debian12-lvm, 2024-05-03 08:25:47 -0500
  LV Status              available
  # open                 1
  LV Size                312.00 MiB
  Current LE             78
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:3

  --- Logical volume ---
  LV Path                /dev/debian12-lvm-vg/home
  LV Name                home
  VG Name                debian12-lvm-vg
  LV UUID                GS6fh1-6Q8a-fWRr-kO6y-KfUb-6Eot-wNBMHn
  LV Write Access        read/write
  LV Creation host, time debian12-lvm, 2024-05-03 08:25:47 -0500
  LV Status              available
  # open                 1
  LV Size                8.80 GiB
  Current LE             2254
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:4

Extend Logical Volume - lvextend

root@debian12-lvm:/home/hrhashmi# lvextend -l +100%FREE /dev/debian12-lvm-vg/root
  Size of logical volume debian12-lvm-vg/root changed from <3.11 GiB (796 extents) to <13.11 GiB (3356 extents).
  Logical volume debian12-lvm-vg/root successfully resized.

List Logical Volumes - lvdisplay

root@debian12-lvm:/home/hrhashmi# lvdisplay
  --- Logical volume ---
  LV Path                /dev/debian12-lvm-vg/root
  LV Name                root
  VG Name                debian12-lvm-vg
  LV UUID                C6NF4v-55K3-HfmX-lgwh-g6TY-Y9Nx-RgpTb5
  LV Write Access        read/write
  LV Creation host, time debian12-lvm, 2024-05-03 08:25:46 -0500
  LV Status              available
  # open                 1
  LV Size                <13.11 GiB
  Current LE             3356
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0

  --- Logical volume ---
  LV Path                /dev/debian12-lvm-vg/var
  LV Name                var
  VG Name                debian12-lvm-vg
  LV UUID                t3nBlr-UUwq-NHmi-hpGw-10S2-wKz3-6qT764
  LV Write Access        read/write
  LV Creation host, time debian12-lvm, 2024-05-03 08:25:46 -0500
  LV Status              available
  # open                 1
  LV Size                6.34 GiB
  Current LE             1624
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:1

  --- Logical volume ---
  LV Path                /dev/debian12-lvm-vg/swap_1
  LV Name                swap_1
  VG Name                debian12-lvm-vg
  LV UUID                x2KiSn-172o-13nK-Jxlh-OZUa-uOOz-7Uswam
  LV Write Access        read/write
  LV Creation host, time debian12-lvm, 2024-05-03 08:25:46 -0500
  LV Status              available
  # open                 2
  LV Size                976.00 MiB
  Current LE             244
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:2

  --- Logical volume ---
  LV Path                /dev/debian12-lvm-vg/tmp
  LV Name                tmp
  VG Name                debian12-lvm-vg
  LV UUID                4s7qlW-CtjS-gzG6-DdMS-Q0Jw-f7h7-QcqNmu
  LV Write Access        read/write
  LV Creation host, time debian12-lvm, 2024-05-03 08:25:47 -0500
  LV Status              available
  # open                 1
  LV Size                312.00 MiB
  Current LE             78
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:3

  --- Logical volume ---
  LV Path                /dev/debian12-lvm-vg/home
  LV Name                home
  VG Name                debian12-lvm-vg
  LV UUID                GS6fh1-6Q8a-fWRr-kO6y-KfUb-6Eot-wNBMHn
  LV Write Access        read/write
  LV Creation host, time debian12-lvm, 2024-05-03 08:25:47 -0500
  LV Status              available
  # open                 1
  LV Size                8.80 GiB
  Current LE             2254
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:4

Final Step

Check disk file

root@debian12-lvm:/home/hrhashmi# df -h
Filesystem                          Size  Used Avail Use% Mounted on
udev                                1.5G     0  1.5G   0% /dev
tmpfs                               300M  620K  299M   1% /run
/dev/mapper/debian12--lvm--vg-root  3.0G  1.4G  1.5G  49% /
tmpfs                               1.5G     0  1.5G   0% /dev/shm
tmpfs                               5.0M     0  5.0M   0% /run/lock
/dev/sda1                           455M   98M  333M  23% /boot
/dev/mapper/debian12--lvm--vg-tmp   283M   10K  263M   1% /tmp
/dev/mapper/debian12--lvm--vg-home  8.6G   40K  8.2G   1% /home
/dev/mapper/debian12--lvm--vg-var   6.3G  265M  5.7G   5% /var
tmpfs                               300M     0  300M   0% /run/user/0
tmpfs                               300M     0  300M   0% /run/user/1000

Resize Logical Volume

root@debian12-lvm:/home/hrhashmi# resize2fs /dev/debian12-lvm-vg/root
resize2fs 1.47.0 (5-Feb-2023)
Filesystem at /dev/debian12-lvm-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/debian12-lvm-vg/root is now 3436544 (4k) blocks long.

Check disk file again

root@debian12-lvm:/home/hrhashmi# df -h
Filesystem                          Size  Used Avail Use% Mounted on
udev                                1.5G     0  1.5G   0% /dev
tmpfs                               300M  620K  299M   1% /run
/dev/mapper/debian12--lvm--vg-root   13G  1.4G   11G  12% /
tmpfs                               1.5G     0  1.5G   0% /dev/shm
tmpfs                               5.0M     0  5.0M   0% /run/lock
/dev/sda1                           455M   98M  333M  23% /boot
/dev/mapper/debian12--lvm--vg-tmp   283M   10K  263M   1% /tmp
/dev/mapper/debian12--lvm--vg-home  8.6G   40K  8.2G   1% /home
/dev/mapper/debian12--lvm--vg-var   6.3G  265M  5.7G   5% /var
tmpfs                               300M     0  300M   0% /run/user/0
tmpfs                               300M     0  300M   0% /run/user/1000

Enjoy 😉

How to resize any VMs Hard disk size

Pre-Requiste First Confirm if your Linux machine was created using LVM or not, execute the following commands pvdisplay vgdisplay lvdisplay ...