Sorry, I just saw your question now, and I will try my best to help you.
You know,
Principle-A: Size(vm_disk) == Size(primary/main partition) + Size(extended partition);
Principle-B: Size(extended partition) == All of the Size(logical partition);
Principle-C: Size(logical partiton) <= Size(extended parition);
As your GParted show, the primary/main partition is /dev/sda1; the extended partition is /dev/sda2; and logical partition is /dev/sda5.
If you want to expand logical partition/dev/sda5, firstly, you should expand extended parition /dev/sda2 because priciple-C, and if you just use sudo cfdisk -l to check the usage of all your partition on yout VM's terminal, I guess your VM's terminal show below:
Disk /dev/sda: 40 GiB, 42212254720 bytes, 82914560 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: 0x50c8fe86
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 1 512M 1102880 512M c W95 FAT32 (LBA)
/dev/sda2 512M 20G 39902880 19.5G 5 Extended
/dev/sda5 512M 20G 39902880 19.5G 82 Linux swap / Solaris
free space 20G 40G 41162880 20G
Also, I guess you want to get below output via sudo cfdisk after you expand the logical partition /dev/sda5:
Disk /dev/sda: 40 GiB, 42212254720 bytes, 82914560 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: 0x50c8fe86
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 1 512M 1102880 512M c W95 FAT32 (LBA)
/dev/sda2 512M 40G 81066760 39.5G 5 Extended
/dev/sda5 512M 40G 81066760 39.5G 82 Linux swap / Solaris
So the solution is clear. I do highly recommend you use cfdisk instead of ISO APPGParted. Also, just use cfdisk for an example.
Solution:
1.please log in your VM via terminal;
2.use sudo cfdisk to enter the permission of cfdisk;
Just use up and down direction button to choose the target partition, use right and left direction button to choose the cfdisk operation, use enter button to make sure.
3.use the Resize button of cfdisk to expand the extended partition /dev/sda2, clean the default disk size and input 39.5GB, then ok;
4.use the Resize button of cfdisk to expand the logical partition /dev/sda5, also, clean the default disk size and input 39.5GB, then ok;
5.use the Write button of cfdisk to write the operation to the Disk, then use Quit button to quit cfdisk;
6.Set all of the expanded logical partition /dev/sda5 into ext4 via sudo resize2fs /dev/sda5;
7.Restart or reboot your virtual machine: sudo reboot.
Have fun!!!
OVER!