Adding free unpartitioned space to the main Ubuntu partition

In continuation to Used VBoxManage modifyhd --resize, actual size did not change

I have resized my .vmdk file, giving more space to the my virtual machine.

Using fdisk (command p) I get the following

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048   999423   997376  487M 83 Linux
/dev/sda2       1001470 83884031 82882562 39.5G  5 Extended
/dev/sda5       1001472 83884031 82882560 39.5G 8e Linux LVM

(Is it normal for two partitions to share their sectors this way?)

Meanwhile the space I added is unpartitioned, as can be seeing by using fdisk (command F) :

Unpartitioned space /dev/sda: 69.8 GiB, 74911318016 bytes, 146311168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes

   Start       End   Sectors  Size
83884032 230195199 146311168 69.8G

How can I add this space to my existing partition? Should I add it to sda2 or sda5?

I should add that I do not have access to GParted as I only have command-line based interface, no GUI.


Partitions should not overlap... but what you actually have here is an "Extended Partition". With extended partitions, you create a "parent" partition which then contains others, allowing you to have more than the 4 partitions supported by an MBR.


To make use of your extra space, you need to carefully delete and re-create the partitions...

My example uses a 100MB file that I have partitioned, put a filesystem on.

$ fdisk disk.dd <<<"p"

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


Command (m for help): Disk disk.dd: 100 MiB, 104857600 bytes, 204800 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
Disklabel type: dos
Disk identifier: 0xcff1f22d

Device     Boot Start    End Sectors Size Id Type
disk.dd1         2048  43007   40960  20M 83 Linux
disk.dd2        43008 204799  161792  79M  5 Extended
disk.dd5        45056 204799  159744  78M 83 Linux

I then extend the file (this has a very similar effect to what you've done):

$ truncate -s 200M disk.dd

Now, we must delete all partitions contained by the extended partition, and then the extended partition itself:

$ fdisk disk.dd

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

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

Partition 5 has been deleted.

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

Partition 2 has been deleted.

Command (m for help): p
Disk disk.dd: 200 MiB, 209715200 bytes, 409600 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
Disklabel type: dos
Disk identifier: 0xcff1f22d

Device     Boot Start   End Sectors Size Id Type
disk.dd1         2048 43007   40960  20M 83 Linux

Then, we must re-create the partitions, making sure that the Start sectors match the originals exactly (this uses my numbers, not yours). Then write the changes.

Here I am using the defaults (just pressing return maked partitions start early, and makes them as big as possible)... You can give values if you'd like to keep some space for other partitions.

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 (43008-409599, default 43008): 43008
Last sector, +sectors or +size{K,M,G,T,P} (43008-409599, default 409599):

Created a new partition 2 of type 'Extended' and of size 179 MiB.

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

Created a new partition 5 of type 'Linux' and of size 178 MiB.

Command (m for help): p
Disk disk.dd: 200 MiB, 209715200 bytes, 409600 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
Disklabel type: dos
Disk identifier: 0xcff1f22d

Device     Boot Start    End Sectors  Size Id Type
disk.dd1         2048  43007   40960   20M 83 Linux
disk.dd2        43008 409599  366592  179M  5 Extended
disk.dd5        45056 409599  364544  178M 83 Linux

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

Don't forget to maintain the partition's type. In the example case the default (83) is fine, but you'll want to use t to set the type back to 8e.

Now, you may need to run partprobe, giving your device (e.g: /dev/sda). This will get the system to reload the partition table. Use lsblk to check that the partition sizes look correct.

Finally, resize the filesystem. This can be done while it is online (mounted). If it is not mounted, then you will probably be promted to perform a full "check and fix" first:

$ e2fsck -f /dev/loop1
e2fsck 1.42.13 (17-May-2015)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/loop1: 12/20000 files (0.0% non-contiguous), 8180/79872 blocks
$ resize2fs /dev/loop1
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/loop1 to 182272 (1k) blocks.
The filesystem on /dev/loop1 is now 182272 (1k) blocks long.

Now, once the filesystem is remounted, you shoud see the extra space you're after:

$ mount /dev/loop1 ./mnt
$ df -h mnt
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop1      169M  1.6M  155M   1% /home/attie/box/so/mnt
$ cat mnt/test
hello world

You pointed out (in a comment) that you were unable to place the start of partition 5 where it should be - fdisk was giving an incompatible range.

To fix this up, you will need to create the partition 5 as per normal... but use the incorrect default start value that is a few sectors larger than your desired value. Once the partition has been created, you can then enter expert mode (x) and place the start more manually. Then return to normal mode (r) and re-print the table... if the numbers look good, then write it (w).

In my example below, I have created the partition, and used the default value that fdisk provided (45056)... I then use expert mode to set the "correct" value (43010) which would otherwise be out of range.

Command (m for help): p
Disk disk.dd: 200 MiB, 209715200 bytes, 409600 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
Disklabel type: dos
Disk identifier: 0xa11ae080

Device     Boot Start    End Sectors  Size Id Type
disk.dd1         2048  43007   40960   20M 83 Linux
disk.dd2        43008 409599  366592  179M  5 Extended
disk.dd5        45056 409599  364544  178M 83 Linux

Command (m for help): x

Expert command (m for help): b
Partition number (1,2,5, default 5): 5
New beginning of data (43009-409599, default 45056): 43010

Expert command (m for help): r

Command (m for help): p

Disk disk.dd: 200 MiB, 209715200 bytes, 409600 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
Disklabel type: dos
Disk identifier: 0xa11ae080

Device     Boot Start    End Sectors  Size Id Type
disk.dd1         2048  43007   40960   20M 83 Linux
disk.dd2        43008 409599  366592  179M  5 Extended
disk.dd5        43010 409599  366590  179M 83 Linux

I had a bit of a rummage in the fdisk sources, but couldn't clearly see why it was adding this ~1MB buffer... As your disk is already setup without it, things should be fine.