Disk Partitioning in Linux Using parted or fdisk (Without LVM)

Featured Image - Disk Partitioning in Linux (Without LVM)

Disk Partitioning is the process of dividing a disk into one or more logical areas, known as partitions.

Before we can create any files, a filesystem must exist. And for creating a filesystem we must create a partition on our disk.

In this tutorial, we’ll discuss the tools and the process of partitioning disks in Linux. First, we will add a new disk in our server and then create a new partition in it. We will create a partition with both the parted and fdisk commands.

How a Partitioned Disk is Store in the System

First let’s understand how the information of a partitioned disk is stored in the system.

This is called partition table and its format is generally known as disk type.

There exist two most commonly used partition tables, MBR (Master Boot Record) and GPT (GUID Partition Table).

The MBR standard supports creating a disk partition up to 2TB.

If we have a disk of 2TB or larger, we have to use GPT.

There are generally 3 types of partitions:

  • Primary
  • Extended
  • Logical

MBR has a limit of 4 primary partitions.

If we need more partitions, one of the primary partitions has to be set as an extended partition.

Inside that extended partition, we will create our new partitions and these additions are called logical partitions.

With GPT, we can have up to 128 partitions. GPT doesn’t support extended or logical partitions.

Two tools can be used for partitioning.

  • For partitioning the MBR disk, fdisk is used (fdisk can also understand GPT format).
  • For partitioning both MBR and GPT, parted is used. It is newer and considered a better option than fdisk.

In this tutorial we will use both the tools and create partitions with them.

Disk Partitioning with parted

Adding A New Device

First, we will add a new device on our server. We will first check our current disk and filesystem using lsblk command.

word image 89

Here we can see I have only one disk added, nvme0n1 which is a nvme disk and has three partitions within it (nvme0n1p1, nvme0n1p2, nvme0n1p3).

From this point on we’ll assume you’re adding a new disk to your system.

Note: I’m using RedHat Enterprise in VMware so I’ll demonstrate how to add a SATA disk to my virtual machine, but your setup may differ.

Adding a New Disk to a VMware Virtual Machine

Now we will add a SATA disk of about 10G.

From the workstation, right clicking on the vm server, we click the settings menu on the last of dropdown.

word image 90

After that we will select Add on the bottom left to add new components.

word image 91

Then we will select the hard disk and press next.

word image 92

After that, we will select “SATA” to add a SATA disk and press next.

word image 93

We will select Create a new virtual disk and press next.

word image 94

Then we will set the size as 10G.

word image 95

Then we will finish and our disk will be added.

word image 96

Then we will see our newly added disk here.

word image 97

We can also check it from the command line using lsblk.

Here we can find our added sata disk. As it is the first SATA disk, so the name is already set as sda.

word image 98

Now we will start with partitioning and need a root or sudo user for executing commands.

Always remember that these tools can be dangerous and we should use them with extreme caution as there is a possibility of data loss. So, if partitions contain data, it is good practice to take a backup and then partition it.

Using parted to list out storage device information

We will first check the list storage devices and partitions using command

parted -l

word image 99

Here we are provided with information on storage devices and partitions. Parted command will print out below information:

  • Model – Model of the storage device.
  • Disk – Name and size of the disk.
  • Sector size – Logical and physical size of the memory
  • Partition Table – Partition table type (msdos, gpt, aix, amiga, bsd, dvh, mac etc)
  • Disk Flags – Partitions with information on size, type, file system, and flags.

Opening the Storage Disk

Here we will first open the disk. Our disk path is /dev/sda. We will open it using the command

sudo parted /dev/sda

word image 100

We can also use parted, and then choose our disk, if we had multiple.

So again, we will first enter into the parted command menu using parted command:

parted

And then select our desired disk to create a partition.

(parted) select /dev/sda
Using the select /dev/sda in the parted menu
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) select /dev/sda
Using /dev/sda
(parted)

Create Partition Table

Now we have to create a partitioning table. To do this we run the following command in the parted menu:

(parted) mklabel <partition table type>

Here we will create a gpt partition table. Run the following command:

(parted) mklabel gpt

word image 102

Enter yes at the prompt.

Check Table

Now we will check our latest created partition table. To do this run print inside the parted menu:

(parted) print

word image 103

Partition Creation

Now it’s time to create a partition. The command used for this is mkpart and format is

(parted) mkpart <partition type> <filesystem> <start size> <end size>

Let’s make a new 2048MB partition using the ext4 file system. The assigned disk start shall be 1MB and the disk end is at 2048MB. Command will be

(parted) mkpart primary ext4 1MB 2048MB

word image 104

Now again we can check it using the print command.

(print) command

word image 105

Here we can see our created partition under the Disk Flags section.

Finally, we will enter the quit command. Changes are saved automatically with this command.

(parted) quit

word image 106

There will be a text with information that we need to update /etc/fstab. We will understand that after a while.

Now we will again check our filesystem and blocks using lsblk.

word image 107

Here we can see our 2GB partition named as /dev/sda1 under /sda.

Activating the partitions

Now that the partition has been created, the next step is to format the partitions and mount them to the system’s directory tree.

To format the partition, we use the mkfs command, format is

mkfs. <filesystem type> <partition name>

Here, we are formatting the partition as an ext4 filesystem. So, our command will be

mkfs.ext4 /dev/sda1

word image 108

Our partition is formatted and activated to use.

Mount point creation and mounting the partition

Now we will create a mount point to interact with the new partition we created. We will create a new mount point named partedpartition. The command for this is:

mkdir /partedpartition

word image 109

Then we have to mount the partition with the created mount point. To do this, we run:

mount /dev/sda1 /partedpartition

word image 110

We can check our recently added mount point using the following command:

df -hT

word image 111

Yes, we have now created a partition and mounted it into a mount point to access it.

Permanent Mount

But this mount is temporary. If you restart your machine or unmount the mount point, then this mounting will be gone.

For making this mount point permanent we have to add an entry of it on the /etc/fstab file.

Understating the fstab

fstab stands for File System Table, which is a system configuration file.

The fstab entry table consists of 6 columns where each column designates a specific parameter and must be set up in the correct order. The columns of the table are as follows from left to right:

  1. Device: usually the given name or UUID of the mounted device (sda1/sda2/etc).
  2. Mount Point: the directory where the device will be mounted.
  3. File System Type: the type of filesystem in use.
  4. Options: lists any active mount options, set defaults in general, other options can be added using commas
  5. Backup Operation: (the first digit) this is a binary system where 1 = dump utility backup of a partition. 0 = no backup. 0 is default and generally used
  6. File System Check Order: (second digit) Here we can see three possible outcomes. 0 means that fsck will not check the filesystem. Numbers higher than this represent the check order. By default, we will use 0.

The general entry format is:

<device name> <mount point> <filesystem type> defaults 0 0

Editing the fstab

Now open the /etc/fstab file using vi or vim or nano and add an entry. And then we will add information about our partition. Here I will open it using vi.

The command for this is:

vi /etc/fstab

 

Now we will add our partition entry on the file. Press i for insert into the /etc/fstab file.

So, our entry format will be

/dev/sda1 /partedpartition ext4 defaults 0 0

word image 113

We will save and exit the editor by first pressing esc.

Then :wq to save and exit from the editor.

Now our partition is permanently mounted. We can use lsblk or df -hT to check:

word image 114

You can check it after restarting the server.

Disk Partitioning with fdisk

Another option for partitioning is to use the fdisk tool and its command. This is widely used among the Linux systems and very easy to use.

Adding a new hard disk

First, we will add another hard disk about 10G. We’ve already learnt how to do it, so I’ve just added it and now checking using lsblk.

word image 115

We can see our newly added hard disk here, named sdb. And in this example, we’ll work on /dev/sdb to create a partition using fdisk.

List partitions

The fdisk command is almost similar to parted. We will first list our partitions using command:

fdisk -l /dev/sdb

word image 116

Here we can see the information about our disk device containing

  • Disk location
  • Units
  • Sector Size
  • I/O Size

Get Started with fdisk

First, we have to select the disk to create a partition using fdisk. Then we will be prompted into the fdisk command line tool. Command format is

fdisk <device_name>

As our device is sdb, we will use the command as below:

fdisk /dev/sdb

word image 117

Here we can see that the command prompt is prompting for entering the fdisk command (just like parted).

We can type m for help, to see all the options available for fdisk.

word image 118

We can see many options here for our use. If we forget any, we can type m and check the desired command.

No need of partition table (if MBR)

We will partition the disk on MBR. As fdisk will by default configure the disk as MBR, we don’t need to create a partition table now.

If we want to use GPT, we will create a new gpt partition table first using g and then add a new partition.

Create New Partiton

We will now add a new partition using the command n as shown in help.

word image 119

Now first we have to select the partition type.

As shown in the screen, we have 4 free primary partitions, we will choose p for creating a new primary partition. Or by default, it will choose primary.

word image 120

Then we will be prompted to choose the partition number. By default, it will be the first one available. Here as we are creating the first one, we will go with 1.

word image 121

Then we will be prompted to enter the first sector and last sector.

For the first sector we will always select default and press enter. (as it is not possible for us to mark the first block sector in storage device)

Then for the end sector, we will enter our partition size.

Here I want to create a partition with 3GB. So, I will add this 3GB to the end. And in the command prompt we will write +3G and press Enter.

word image 122

Congratulations! Our partition is created. We have to write the configuration and exit using w.

word image 123

Using partprobe

Then we will use the command:

partprobe

partprobe is a program that informs the operating system kernel of partition table changes, by requesting that the operating system re-read the partition table. It is necessary to use this command.

word image 124

Checking the newly created partition

Finally, we can check using lsblk command.

word image 125

Mounting the partition

Now for mounting, the process is the same as it was for parted. We have to

  1. Format the partition with desired filesystem using mkfs
  2. Create new mount point
  3. Mount the mount point with newly formatted partition
  4. Make it permanent by adding an entry on /etc/fstab

Formatting the partition to activate

We will format our partition on the xfs filesystem. To do this run:

mkfs.xfs /dev/sdb1

word image 126

Mount Point creation and addition

Then we will create a new mount point named /fdiskpartiton and mount our partition on it.

word image 127

/etc/fstab entry

Finally, we edit the /etc/fstab file and enter the partition as shown in below format. To do this run:

vi /etc/fstab

Then add entry on below format:

/dev/sdb1 /fdiskpartition xfs defaults 0 0

word image 129

Finally exiting the fstab using :wq.

We can check our mounted system using lsblk and df -hT command.

word image 130

Conclusion

We have now learned 2 command line tools to create partitions in Linux without LVM. We can get more information about them on their man pages. You can type man parted or man fdisk to learn more. If you have any feedback or questions feel free to let us know in the comments and we’ll get back to you as soon as we can.

11 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)
0 Comments
Inline Feedbacks
View all comments
You May Also Like