How To Find Unallocated Space On Linux

When managing a server, you can need to operate on data storage. To do it better, you can need to know how to manage unallocated disk space. Unallocated space is the unused space on the Hard disk which has not been partitioned into a Volume or Drive. It is not a partition, just dead space. It can be made into a Partition and formatted. Or it can be used to extend a current partition next to it.

What is unallocated space?

Don't confuse free space and unallocated space. Unallocated space means that the operating system knows that there's physical space on the hard drive, but it hasn't been assigned (allocated) to a particular partition whereas free space means that the operating system knows you haven't used up all of the allocated space on a particular drive or partition.

1. Display disk cylinders

With fdisk command, the start and end columns in your fdisk -l output are the start and end cylinders. From the header of your fdisk -l output, you can also see how many cylinders the disk has and how many bytes a cylinder represents. Normally, cylinder's values follow each other when all disk space is allocated. If there are some values which are missing, it means that we have unallocated disk space.

Consider the example below

# fdisk -l /dev/sdf

Disk /dev/sdf: 12.9 GB, 12884901888 bytes, 25165824 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 label type: dos
Disk identifier: 0x7e5db80f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdf1            2048     9766911     4882432   83  Linux
/dev/sdf2        18032550    25165823     3566637   83  Linux

You can see that /dev/sdf1 starts on sector 9766911 but /dev/sdf2 doesn't start directly on the next value but on sector 18032550. It means that the range 9766912-18032549 is missing so there is unallocated space on the disk. To calculate the unallocated space, you can do as below:

  • missing sectors: 18032549 - 9766912 = 82656378265637
  • calculate the size in bytes: 82656378265637 * 512 = 4232006144 bytes ~ 4.2GB

Notice that /dev/sdf1 starts on sector 2048 which is normal on some case for the first default partition. It also means that before, there are few unallocated space.

2. Show numbering of on-disk partitions

It is possible to use partx command which asks the kernel to probe a given device and re-read the partition table. Given a device or disk-image, partx tries to parse the partition table and list its contents. You first need to take disk size information with fdisk -l command

# fdisk -l /dev/sdf

Disk /dev/sdf: 12.9 GB, 12884901888 bytes, 25165824 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 label type: dos
Disk identifier: 0x7e5db80f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdf1            2048     9766911     4882432   83  Linux
/dev/sdf2        18032550    25165823     3566637   83  Linux

Total space 12.9 GB . You can use partx -l command to list the partitions in order to  find the used space and can subtract from total space. You shall notice that but all numbers are in 512-byte sectors.

[root@centos7-srv admin]# partx -l /dev/sdf
# 1: 2048-      9766911 ( 9764864 sectors, 4999 MB)
# 2: 18032550- 25165823 ( 7133274 sectors, 3652 MB)

#1 and #2 are the number of partitions. Remember that in Linux, when a disk space is ready to be initialized by the system, it is numbered as you can see on fdisk -l output. So my server has around 4.2 GB unallocated space.

Instead of -l option, you can use -s option to have more information detailed and size readable by human.

# partx -s /dev/sdf
NR START         END SECTORS SIZE NAME UUID
 1 2048      9766911 9764864 4.7G 
 2 18032550 25165823 7133274 3.4G

3. Use the partition manipulation program

Another option we have is to use parted command. You can use free parameter to get unallocated space and also disk partition.

# parted /dev/sdf print free
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdf: 12.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  5001MB  5000MB  primary  ext2
        5001MB  9233MB  4232MB           Free Space
 2      9233MB  12.9GB  3652MB  primary

The keyword Free Space there doesn't refer to the free space explained on the introduction but exactly to unallocated space which is the subject of this topic. You can see the first Free Space which refers to the unallocated space that I explained before for the first default partition. Notice that unallocated space have no numbering.

In my below example, there is no partition on the hard disk and so it displays only unallocated space (free space). See that there is no numbering

# parted /dev/sdg print free
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdg: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number Start   End     Size   Type  File system   Flags
       32.3kB  10.7GB  10.7GB       Free Space

When you execute parted command without any argument, by default it selects the first hard disk drive that is available on your system.

4.  Display disk partition table

Another command we can use is cfdisk.  It is a curses-based program for partitioning any hard disk drive. The partitions section always displays the current partition table. The command line is the place where commands and text are entered. The format of the partition table in the partitions section is, from left to right: Name, Flags, Partition Type, Filesystem Type and Size.

# cfdisk /dev/sdf

You can see the first Free Space above. For more detail, you can choose Print, then Sectors  and validate to have the result below

You have more details about sector range we calculate earlier for unallocated space (refered by Free Space not free space)

Conclusion

It is possible to view the unallocated space on the hard disk through terminal which is different from free space information that you can have with some other commands as df, du... You can operate on unallocated space to create new disk space or extend some existing partitions.

About Bobbin Zachariah

Founder of LinOxide, passionate lover of Linux and technology writer. Started his career in Linux / Opensource from 2000. Love traveling, blogging and listening music. Reach Bobbin Zachariah about me page and google plus page.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.

2 Comments