In today’s post we will go over how to partition a disk in Linux using the ‘fdisk’ utility and how to format a disk using the ‘mkfs’ utility. If you are still wondering how to get a disk to be recognized by the OS you can refer to yesterdays article here. After the disk has been installed and you have verified that the disk is being seen by the operating system the next step is to partition the disk.
Partitions (or in some operating systems referred to as “slices”) allow you to divide a disk into separate logical sections that you will then format with a file-system. In most Linux distributions you will have a ‘boot’ partition that contains the needed information to boot into the OS; a ‘system’ partition where your main operating system file structure is stored; and a ‘swap’ partition which is an area of the disk the OS can use as memory for operations. On most modern day Linux distros (and Windows for that matter), the partitions will be created and formatted for you automatically. In this article we will be explaining how to create a single partition that spans the entire disk. Of course you can create more than one partition on a physical hard drive but creating one large partition is useful when adding a new disk that will be used as a storage drive.
To partition a disk we will be using the ‘fdisk’ utility. This utility has been around for many years and is getting a little outdated in that it can only handle partitions up to 2TB in size. So if you are working with a disk that is larger than 2TB and you want to create a single, all encompassing partition you would be better off using a utility such as ‘parted’ (more on that in another post). The main reason I am using the ‘fdisk’ utility in this example is that it is easy to use and works very well.
To begin formatting the disk open a command prompt and type the following command:
(Note: ‘sudo’ is required if you are using a distro such as Ubuntu. All instructions are written assuming that you are using Ubuntu as this is the most popular desktop distribution at the time of this writing.)
sudo fdisk /dev/<hard drive>
<hard drive> is the name of the drive you are working with (e.g. /dev/sda). You should now be prompted with a prompt that looks like this:
Command (m for help):
From here you can press the ‘m’ key, then enter for a list of commands. Before we partition the disk, lets make sure that there are no existing partitions on the drive by pressing ‘p’ then enter. This will present you with a list of partitions that are on the disk. The list should be empty, meaning that there are no partitions on the disk. If there are partitions on the disk you can remove them by pressing the ‘d’ key then enter. You will be prompted on which partition you would like to remove.
Keep in mind that all commands that you place using the ‘fdisk’ utility are not “committed” to the disk (that is they are not written to the drive) until you issue the ‘w’ command. Even with that in mind, you should proceed with caution as removing or editing partitions on a disk will render existing data unreadable!
To create a partition on a disk you use the ‘n’ command followed by enter. You will be presented with the option of creating a “primary” or “extended” partition. In this scenario we will be creating a “primary” partition that spans the entire disk. To create the primary partition type “p” then enter. Next, you will presented with options on how large you want the partition to be. In this case, since we are creating a single partition that spans the entire disk we can simply press enter to accept the default values.
Next we will want to define the type of partition that we are going to be creating. You can define the partition type by issuing the ‘t’ command followed by enter. You will then be prompted to select the partition you would like to define – in this case we will just press enter as we only have one partition on the disk. If you where working with multiple partitions you would select the desired partition number.
After selecting the partition to define a type to, you will be prompted with a rather scary looking message asking for the “Hex code”. At this prompt you are going to want to press the “L” key to display a list of codes and their associated partition types. In this example we are going to define a “Linux” type partition which uses hex code ’83’.
After defining the partition parameters as explained above we are ready to write the partition to the disk. This is the point of no return so to speak and if you venture past this point of the article you will be making changes to your drive that could potentially cause you to loose information. Of course if this is a brand new drive that you are using for storage (as we are assuming in this article) then you will have no existing data on the disk.
To write the partition to the disk, issue the ‘w’ command and press enter. This will begin the processes of writing the partition tables to the disk. Depending on the size of your disk and other factors this can take a minute to complete. Once you are presented with your regular command prompt the processes has been completed and you now have a disk with a partition that is ready for formatting.
In tomorrows post, I will go over the processes of formatting the partition using the ‘mkfs’ utility included in most Linux distributions.
As always, if you have any questions leave me a comment!