Creating MBR partitions in Linux

Creating MBR partitions in Linux

In this article, we’ll learn about creating MBR partitions in Linux, delve into the procedure using the fdisk and parted tool, and provide clear command line examples to illustrate its utility.

Table of Contents

Introduction

Efficiently managing storage is a critical aspect of IT infrastructure. Whether you’re setting up servers, workstations, or virtual machines, understanding how to create partitions is essential. This article provides a comprehensive guide on creating Master Boot Record (MBR) partitions on systems running Red Hat Enterprise Linux (RHEL), CentOS, Ubuntu, or any other flavor of Linux. We’ll explore the significance of MBR partitions, delve into the procedure using the fdisk and parted tool, and provide clear command line examples to illustrate its utility.

Creating MBR partitions in Linux - Hard Disk Image

Photo by Pixabay from Pexels

Brief History

The concept of partitioning dates back to the early days of computing, when storage capacity was limited, and optimizing data organization was crucial. The Master Boot Record (MBR) partitioning scheme emerged as a standard for organizing data on hard drives. It allows you to divide a single physical drive into multiple logical sections, each of which can be formatted and utilized independently. MBR partitions are still relevant today, especially in scenarios where compatibility with older systems is a concern.

Creating MBR Partitions: Using fdisk

Accessing the Command Line

Before we dive into partition creation, ensure that you have access to the command line interface. On any Linux distribution, you can launch the terminal by searching for “Terminal” in the search bar or application menu.

Identify the Disk

To start, identify the disk you want to partition. You can use the lsblk command to list all available disks and their partitions. For instance:

				
					# lsblk
NAME          MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
vda           252:0    0  50G  0 disk 
├─vda1        252:1    0   2G  0 part /boot
└─vda2        252:2    0  48G  0 part 
  ├─vg00-root 253:0    0  15G  0 lvm  /
  ├─vg00-swap 253:1    0   4G  0 lvm  [SWAP]
  └─vg00-home 253:2    0  10G  0 lvm  /home
vdb           252:16   0  25G  0 disk 
vdc           252:32   0  25G  0 disk 

				
			

As you can see (above), this command will display a list of disks along with their sizes and partitions.

Partitioning the Disk

Let’s assume you want to partition /dev/vdb. To begin, use the fdisk command followed by the disk name:

				
					# fdisk /dev/vdb

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


Command (m for help): 

				
			

This will launch the interactive fdisk utility. As you can see in the example above (launched on RHEL9).

Creating Partitions

Inside the fdisk utility, you can create partitions as follows:

  1. Type n to create a new partition.
  2. Choose between primary (p) and extended (e) partition types. For MBR partitions, you can create up to four primary partitions, or three primary partitions and one extended partition.
  3. Specify the starting and ending sector of the partition. You can often press Enter to accept default values for these, effectively utilizing the entire disk space.
  4. Type t to change the partition’s system ID (partition type).
  5. Select the partition number you want to modify, then input the hexadecimal code for the desired partition type. For example, 83 for Linux partitions.

Repeat these steps for each partition you want to create.

Saving Changes

After creating partitions, press w to write the changes to the disk. This step is crucial, as changes made within fdisk are only applied when you save them.

Formatting Partitions

Once you’ve created the partitions, you need to format them using filesystems. For instance, to format a partition as ext4:

				
					# mkfs.ext4 /dev/vdb1

				
			

In this instance, /dev/vdb1 is the appropriate partition name.

Creating MBR Partitions: Using parted

Partitioning the Disk

In the Creating MBR partitions using fdisk section, we reviewed how to identify a disk. Partitioning a disk using parted is similar: 

				
					# parted /dev/vdb
GNU Parted 3.5
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)    
				
			

In the example (above), we replaced fdisk with parted.

Creating Partitions

Inside the parted utility, follow these steps to create partitions:

  1. Use the mklabel msdos command to create an MBR partition table on the disk.
  2. Create a partition using the mkpart command. Specify the partition type (primary) and the file system type (ext4, XFS, etc.).
  3. Set the partition’s start and end points using percentages or sizes.
  4. Repeat the process for additional partitions.

Here’s an example to create a primary ext4 partition using 50% of the disk:

				
					# parted /dev/vdb mklabel msdos
Warning: The existing disk label on /dev/vdb will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? y                                                                 
Information: You may need to update /etc/fstab.

# parted /dev/vdb mkpart primary ext4 0% 50%
Information: You may need to update /etc/fstab.

# lsblk
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
vda           252:0    0   50G  0 disk 
├─vda1        252:1    0    2G  0 part /boot
└─vda2        252:2    0   48G  0 part 
  ├─vg00-root 253:0    0   15G  0 lvm  /
  ├─vg00-swap 253:1    0    4G  0 lvm  [SWAP]
  └─vg00-home 253:2    0   10G  0 lvm  /home
vdb           252:16   0   25G  0 disk 
└─vdb1        252:17   0 12.5G  0 part 
vdc           252:32   0   25G  0 disk 
				
			

The /dev/vdb1 partition (above) is now using half of the available size.

Here’s an example to create a primary ext4 partition of a 5GiB in size:

				
					# parted /dev/vdb mklabel msdos
Warning: The existing disk label on /dev/vdb will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? y       
Information: You may need to update /etc/fstab.

# parted /dev/vdb mkpart primary ext4 2048s 5GiB
Information: You may need to update /etc/fstab.

# lsblk
NAME          MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
vda           252:0    0  50G  0 disk 
├─vda1        252:1    0   2G  0 part /boot
└─vda2        252:2    0  48G  0 part 
  ├─vg00-root 253:0    0  15G  0 lvm  /
  ├─vg00-swap 253:1    0   4G  0 lvm  [SWAP]
  └─vg00-home 253:2    0  10G  0 lvm  /home
vdb           252:16   0  25G  0 disk 
└─vdb1        252:17   0   5G  0 part 
vdc           252:32   0  25G  0 disk 

				
			

The /dev/vdb1 is now using 5GiB of the available size. To ensure proper alignment, set the starting sector to 2048.

Quitting Parted and formatting

After creating partitions, exit the parted utility:

				
					(parted) quit
				
			

You can exit by entering quit or q. Also, to format the partition use the same mkfs.ext4 command following the previous example: 

				
					# mkfs.ext4 /dev/vdb1
				
			

Mounting Partitions

Finally, mount the formatted partitions to directory paths of your choice. Create directories if needed:

				
					# mkdir /mnt/mydata
# mount /dev/vdb1 /mnt/mydata
				
			

To ensure these partitions are mounted automatically after system reboots, add entries to the /etc/fstab file.

Conclusion

In this guide, you’ve learned how to create MBR partitions using the fdisk and parted tool in Linux. Partitioning remains a fundamental skill in managing storage for various IT scenarios. By following the outlined steps and examples, you’re better equipped to organize and optimize your system’s storage. Stay informed about evolving technologies, but also recognize the continued relevance of established methods like MBR partitions.

Related Posts

Leave a Reply

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