ag@linux:~$

history 1 of 36

Partition, Format and Mount a New Disk with fdisk

How I partition a new disk with fdisk, format it as ext4 and mount it via /etc/fstab, with corrected steps, UUIDs and a warning on picking the right disk.

Written

From my Linux study notes, 2012. Some commands and versions here are old, so please reuse them carefully. Anything marked "added later", and the notes at the end, explain what has changed since.

In this post, I am writing how I do disk partitioning and mounting in Linux.

Before you start

  • Warning: picking the wrong disk destroys data. fdisk and mkfs work on whatever device you name. Partitioning or formatting your system disk, or a disk with data on it, wipes it. Check the device name two or three times before writing changes, and have a backup of anything important.
  • Run these commands as root or with sudo.
  • Disk names vary: /dev/sda, /dev/sdb (SATA/SCSI/USB), /dev/vda (many virtual machines), /dev/nvme0n1 (NVMe). Partitions add a number: /dev/sdb1, or p1 for NVMe (/dev/nvme0n1p1). In the examples below, /dev/sdb is only an example of a new, empty disk.

Steps

Step 1: Check disk usage

Check disk free by typing:

Terminal window
df -h

You will see the mounted disks and how much space is used. -h shows sizes in human-readable units (G, M).

Step 2: Find the new disk

If you want to add a new disk:

Terminal window
fdisk -l

Now you will see every disk attached to your system, with its size and any partitions.

Two other commands make it easier to be sure which disk is the new one (added later):

Terminal window
lsblk
blkid
  • lsblk shows every disk and partition as a tree, with sizes and where each is mounted. A new disk has no partitions under it and no mount point.
  • blkid shows the file system type and UUID of each partition that has a file system. A brand-new disk will not appear here until it is formatted.

Match the size you expect (for example a 100G disk you just attached) before going further.

Step 3: Create a partition

Terminal window
fdisk /dev/your-new-disk

For example fdisk /dev/sdb. Use the whole disk name here, not a partition (/dev/sdb, not /dev/sdb1).

Inside fdisk:

  1. Enter m for help.
  2. Enter n to create a new partition.
  3. Choose e for extended partition or p for primary partition.
  4. Enter the partition number between 1-4: if it is the first time then 1, else choose accordingly.
  5. Press Enter to accept the default first and last sectors, which uses the whole disk (or give a size such as +20G).
  6. Enter w to write the changes to disk and exit. This step was missing from my original notes. Until you press w, nothing is changed; q quits without saving, which is a safe way out if you are unsure.

About primary and extended: on a disk with an MBR (DOS) partition table you can have up to four primary partitions. If you need more, one of the four can be an extended partition that holds logical partitions. For a single data partition, choose p. If the disk has a GPT partition table, fdisk does not ask this question.

The interactive session looks like this (keys you type, one per prompt):

m
n
p
1
(Enter)
(Enter)
w

Step 4: Create the file system

My original command was:

mkfs.ext4 /dev/your-new-disk

Correction: format the partition you just created, not the whole disk. Running mkfs on the whole disk device (/dev/sdb) overwrites the partition table you created in Step 3. Use the partition name, which has a number at the end:

Terminal window
mkfs.ext4 /dev/sdb1

Run lsblk first to confirm the partition name. Check that it shows as ext4 afterwards with:

Terminal window
blkid /dev/sdb1

Step 5: Add the disk to /etc/fstab

Create a directory to mount the disk on (added; my original notes skipped this, but the mount point must exist):

Terminal window
mkdir /data

Then edit the file system table:

Terminal window
vi /etc/fstab

/etc/fstab lists the file systems to mount at boot. Each line has six fields: device, mount point, file system type, options, dump flag and fsck order. It is safest to identify the disk by its UUID rather than /dev/sdb1, because device names can change between boots (for example when another disk is added), but the UUID stays the same. Get it with blkid /dev/sdb1, then add a line like this, using your own UUID:

UUID=your-uuid-from-blkid /data ext4 defaults 0 2
  • defaults: the standard mount options.
  • 0: do not back up with the old dump tool.
  • 2: check this file system with fsck at boot, after the root file system (which uses 1). 0 means never check.

Step 6: Mount everything in fstab

Terminal window
mount -a

This mounts every entry in /etc/fstab that is not already mounted. It is also a good test: if it shows an error, fix the line before rebooting. Confirm the disk is mounted with:

Terminal window
df -h /data

Notes for today

  • A mistake in /etc/fstab can stop the system booting (it may drop to an emergency shell). Always run mount -a after editing and fix any errors before rebooting. Adding the nofail option (defaults,nofail) lets the system carry on booting if this disk is missing.
  • After running mount -a on a systemd system you may see a hint to run systemctl daemon-reload, because systemd builds mount units from /etc/fstab. Running it is harmless.
  • MBR vs GPT: MBR partition tables cannot use more than 2 TiB of a disk (with the usual 512-byte sectors). For larger disks use GPT: inside fdisk, press g to create a new empty GPT table before n. Recent versions of fdisk support GPT; on older systems such as CentOS 6, parted or gdisk were used for GPT.
  • Red Hat-family systems (CentOS 7 and later, Rocky Linux, AlmaLinux) use XFS as the default file system; mkfs.xfs /dev/sdb1 creates one, with xfs as the type in /etc/fstab. ext4 still works fine.
  • To unmount a disk, use umount /data (note the spelling: umount, not unmount).