1. Home
  2. »
  3. Linux
  4. »
  5. How to Format, Partition, and Auto-Mount a Disk in Linux: A Comprehensive Guide

How to Format, Partition, and Auto-Mount a Disk in Linux: A Comprehensive Guide

By

🕑ERT : 4 min
Facebook
Twitter
LinkedIn
Contents
Estimated Read Time : 4 min

How to Format, Partition, and Auto-Mount a Disk in Linux: A Comprehensive Guide

As your Linux system grows, so do your storage needs. Whether you’re adding an additional external or internal drive to expand your storage, it’s essential to format, partition, and ensure that the disk automatically mounts on boot. In this blog, we’ll walk through the steps of how to format, partition, and configure a new disk to auto-mount on boot in Linux. We will work with an example drive, /dev/sdb, showing it as a data partition, and ensure it becomes available automatically every time the system boots.

Why Auto-Mounting Is Important

Mounting a disk manually each time the system reboots is time-consuming and prone to errors. By configuring the system to auto-mount, you guarantee that the disk is always available after boot without any manual intervention.


Step-by-Step Guide

Step 1: Check Existing Disks and Partitions

Before proceeding, identify the available disks and partitions on your system. Run the following command to list all block devices:

				
					lsblk 
				
			

This command provides a visual layout of all the attached disks and their partition schemes. In our example, we will be working with /dev/sdb as the new disk. Ensure this disk doesn’t contain any critical data before proceeding with the next steps.


Step 2: Check for Errors

If the disk has errors or an unrecognized label, it’s important to fix it before formatting. Check for any unrecognized disk labels by running:

				
					sudo parted -l | grep Error
				
			

If the output contains an error like Error: /dev/sdb: unrecognized disk label, the disk needs to be formatted and properly labeled, which we will handle in the next steps.


Step 3: Format the Disk and Create a Partition Table

Now, format the disk and create a partition table. There are two popular partitioning schemes:

  • GPT (GUID Partition Table) for modern systems
  • MBR (Master Boot Record) for legacy systems

To format the disk with GPT, use:

				
					sudo parted /dev/sdb mklabel gpt
				
			

For MBR, use:

				
					sudo parted /dev/sdb mklabel msdos

				
			

Step 4: Create a Partition

Next, create a partition that spans the entire disk. Here, we’ll create a primary partition of type ext4, which is a widely-used Linux file system.

				
					sudo parted -a opt /dev/sdb mkpart primary ext4 0% 100%

				
			

This command creates an ext4 partition that starts at the beginning (0%) and occupies 100% of the disk space.


Step 5: Format the Partition with ext4

Once the partition is created, format it with the ext4 file system. You can label the partition for easy identification. For example, let’s label it datapartition:

				
					sudo mkfs.ext4 -L datapartition /dev/sdb1
				
			

This step creates the ext4 file system and labels the partition as datapartition. Replace datapartition with the desired label for the partition.


Step 6: (Optional) Change Partition Label

If you want to change the partition label at a later time, you can do so with the following command:

				
					sudo e2label /dev/sdb1 newlabel
				
			

Replace newlabel with the desired label for the partition.


Step 7: Create a Mount Point

Now, we need to create a directory where the partition will be mounted. This can be any directory, but for this guide, we will create a mount point under /mnt/data.

				
					sudo mkdir -p /mnt/data

				
			

This creates the /mnt/data directory where the partition will be mounted.


Step 8: Mount the Partition

After creating the mount point, manually mount the partition to verify that everything works as expected:

				
					sudo mount -o defaults /dev/sdb1 /mnt/data

				
			

Now, the partition should be mounted to /mnt/data.


Step 9: Configure Auto-Mount at Boot

To ensure that the partition automatically mounts on system boot, we need to add an entry in the /etc/fstab file.

Open /etc/fstab in your preferred text editor, such as nano:

				
					sudo nano /etc/fstab
				
			

Add the following line to mount the partition based on its label

				
					LABEL=datapartition /mnt/data ext4 defaults 0 0

				
			

This entry tells the system to mount the partition labeled datapartition to /mnt/data using the ext4 file system with default options on boot.


Step 10: Test the Configuration

To ensure the configuration works without rebooting, run:

				
					sudo mount -a
				
			

This command reads the /etc/fstab file and mounts all entries specified in it.


Step 11: Verify the Mount

Finally, verify that the partition is mounted by running:

				
					df -h -x tmpfs
				
			

Popular Content

Comments & Ratings

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x

we churn out 100+ successful projects in a year

lets solve your business problems

Please enable JavaScript in your browser to complete this form.
Service for enquiry