fdisk and fstab image

So, you bought that multi-terabyte hard drive and want to add it to your server, but you’re not quite sure where to start. Don’t worry—I’ve got you covered with an easy-to-follow guide.

Setting up storage on Linux can be intimidating, especially when you’re dealing with tools like fdisk and fstab. But it’s not as complicated as it seems. By the end of this guide, you’ll know how to use fdisk to create partitions, format them, and set them up to automatically mount at boot using fstab. Let’s get started!


The Power of Partitions (and Why They Matter)

Think of your hard drive as a giant, empty filing cabinet. Without partitions, your system doesn’t know how to organize or access all that space. Partitions break your drive into smaller sections, each with a specific purpose—whether it’s for storing data, running programs, or setting up backups, partitions keep everything running smoothly.

With fdisk, you can easily create and manage these partitions. It’s a powerful tool that comes with almost every Linux distribution. In this guide, I’ll walk you through each step, whether you’re setting up a brand-new storage drive or reorganizing an existing one.


Step-by-Step Guide to Using fdisk, Formatting Partitions, and Configuring fstab

Step 1: Identify the Target Disk

Before creating partitions, you need to identify which drive you’re working with. Use the lsblk command to list all available disks and partitions:

lsblk

This will display something like:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   500G  0 disk
├─sda1   8:1    0   1M    0 part
├─sda2   8:2    0   2G    0 part /boot
└─sda3   8:3    0   498G  0 part /
sdb      8:16   0   12.7T 0 disk

Here, sda is your main disk, and sdb is an empty 14TB drive. For this tutorial, we’ll work with sdb.

⚠️ Caution: Double-check the disk name to avoid accidentally overwriting important data!


Step 2: Launch fdisk

Run the following command to open fdisk for your target disk:

sudo fdisk /dev/sdb

You’ll see a prompt like this:

Command (m for help):

Type m to view the available commands.


Step 3: Create a New Partition

1. Delete Old Partitions (Optional)

If the disk has existing partitions you want to remove, type d and follow the prompts to delete them.

2. Create a New Partition

To create a new partition, type n and press Enter. fdisk will ask:

  • Partition type: Choose p for primary or e for extended (choose p for most use cases).

  • Partition number: Press Enter to accept the default (usually 1).

  • First sector: Press Enter to accept the default (start of the disk).

  • Last sector: Press Enter to use the entire disk or specify a size (e.g., +100G for 100GB).

After completing this, you’ll see the new partition listed.

3. Write Changes to Disk

Type w to save the changes and exit fdisk. This writes the partition table to the disk.


Step 4: Format the Partition

Now that you’ve created the partition, it’s time to format it with a filesystem. A common choice is ext4. However, for a media server that hosts several large files, I recommend XFS.

sudo mkfs.xfs /dev/sdb1

This command formats the first partition (/dev/sdb1) on the sdb disk with the XFS filesystem. Replace xfs with your preferred filesystem (e.g., ext4 or btrfs) if needed.

Running lsblk again should look simmalar to this if everything worked:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   500G  0 disk
├─sda1   8:1    0   1M    0 part
├─sda2   8:2    0   2G    0 part /boot
└─sda3   8:3    0   498G  0 part /
sdb      8:16   0   12.7T 0 disk
└─sda1   8:17   0   12.7T 0 part /

Step 5: Create a Mount Point

A mount point is where your system will access the partition. For example, if you want to use it for storing media files, you might create a directory like this:

sudo mkdir -p /mnt/media

The -p flag ensures the command creates the directory, even if parent directories don’t exist.


Step 6: Mount the Partition (Temporary Test)

Before configuring fstab, test mounting the partition:

sudo mount /dev/sdb1 /mnt/media

Verify the mount by running:

df -h

You should see /dev/sdb1 listed with /mnt/media as its mount point.


Step 7: Add the Partition to fstab

To ensure the partition mounts automatically at boot, add it to the /etc/fstab file. Start by retrieving the partition’s UUID:

sudo blkid /dev/sdb1

You’ll see output like this:

/dev/sdb1: UUID="1234-5678-90AB-CDEF" TYPE="xfs"

Copy the UUID and open the fstab file for editing:

sudo nano /etc/fstab

Add the following line at the end of the file:

UUID=1234-5678-90AB-CDEF /mnt/media xfs defaults 0 2

Here’s what the options mean:

  • UUID=1234-5678-90AB-CDEF: Unique identifier for the partition.

  • /mnt/media: Mount point.

  • xfs: Filesystem type.

  • defaults: Standard mount options.

  • 0 2: Dump and fsck options (usually safe to leave as-is).

Save the file and exit.


Step 8: Test the fstab Configuration

To verify your changes, unmount the partition and remount all entries in fstab:

sudo umount /mnt/media
sudo mount -a

If no errors appear, your configuration is correct.


Wrapping It All Up Congrats! You’ve successfully created a partition, formatted it, and set it up to mount automatically using fstab. Whether you’re adding a new drive to your home media server or managing existing storage, these steps will be your go-to guide.

Just remember—mistakes happen. Always double-check your disk names and back up important data before making any changes. With a bit of practice, managing partitions and mount points will become second nature.

Ready to level up? Put these skills to use by setting up dedicated partitions for media storage, backups, or even Docker containers. The possibilities are endless.