Resize VM partition without erasing data

Solution 1:

It doesn't look like you actually have the empty space available - the "extended" partition usually covers all remaining disk space. It's possible that you do ; particularly if you created the disk image at 10GB and then it was resized to 20GB.

So, presuming you do have the empty space, you need to :

  • Make sure you have backups!
  • Resize the extended partition to fill the new upper sector limit
    • Use fdisk for this
    • Be careful! fdisk can wreck your partition table
    • A method for doing that is here : resize/grow partition without losing data
    • The method can be summarized as
      • Remove the existing partitions (2 and 5)
      • Recreate them with exactly the same starting sector numbers
      • For partition 2, allow it to consume the whole disk
      • For partition 5, use the exact starting sector and size it has now
  • Enrol a new LVM partition in the root volume group
    • Create a new Linux LVM partition in the extended space, allow it to consume remaining disk space
    • Make this a "physical volume" with pvcreate /dev/sdaX where X is the new partition
    • List the volume groups
      • vgdisplay
    • Extend the server volume group (replace name with name from previous step as required)
      • vgextend server-vg /dev/sdaX
    • Show the logical volumes
      • lvdisplay
    • Extend the root fs volume
      • lvextend /dev/server-vg/root /dev/sdaX
    • Resize the root FS to fit the new space
      • Depends on the FS you're using... e.g. for ext based FS
      • resize2fs /dev/server-vg/root

References :

https://www.rootusers.com/how-to-increase-the-size-of-a-linux-lvm-by-expanding-the-virtual-machine-disk/

http://litwol.com/content/fdisk-resizegrow-physical-partition-without-losing-data-linodecom

Solution 2:

I did it in a VM with Ubuntu Server 18.04 and without LVM.

Initial state after increase the HDD in VirtualBox.

ivanx@ivanx:~$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   40G  0 disk
├─sda1   8:1    0    1M  0 part
└─sda2   8:2    0   20G  0 part /

Next step is to delete the partition and resize with fdisk.

ivanx@ivanx:~$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.31.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
Device     Start      End  Sectors Size Type
/dev/sda1   2048     4095     2048   1M BIOS boot
/dev/sda2   4096 41940991 41936896  20G Linux filesystem

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

Partition 2 has been deleted.

Command (m for help): p
Device     Start   End Sectors Size Type
/dev/sda1   2048  4095    2048   1M BIOS boot

Command (m for help): n
Partition number (2-128, default 2): 2
First sector (4096-83886046, default 4096): 4096
Last sector, +sectors or +size{K,M,G,T,P} (4096-83886046, default **83886046**): 

Created a new partition 2 of type 'Linux filesystem' and of size 40 GiB.
Partition #2 contains a ext4 signature.

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

Command (m for help): p
Device     Start      End  Sectors Size Type
/dev/sda1   2048     4095     2048   1M BIOS boot
/dev/sda2   4096 83886046 83881951  40G Linux filesystem

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

Check results:

ivanx@ivanx:~$ df -h
/dev/sda2        20G   19G  466M  98% /

ivanx@ivanx:~$ sudo resize2fs /dev/sda2
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 5
The filesystem on /dev/sda2 is now 10485243 (4k) blocks long.

ivanx@ivanx:~$ df -h
/dev/sda2        40G   19G   20G  49% /