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.
fdiskandmkfswork 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, orp1for NVMe (/dev/nvme0n1p1). In the examples below,/dev/sdbis only an example of a new, empty disk.
Steps
Step 1: Check disk usage
Check disk free by typing:
df -hYou 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:
fdisk -lNow 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):
lsblkblkidlsblkshows 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.blkidshows 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
fdisk /dev/your-new-diskFor example fdisk /dev/sdb. Use the whole disk name here, not a partition (/dev/sdb, not /dev/sdb1).
Inside fdisk:
- Enter
mfor help. - Enter
nto create a new partition. - Choose
efor extended partition orpfor primary partition. - Enter the partition number between
1-4: if it is the first time then1, else choose accordingly. - Press Enter to accept the default first and last sectors, which uses the whole disk (or give a size such as
+20G). - Enter
wto write the changes to disk and exit. This step was missing from my original notes. Until you pressw, nothing is changed;qquits 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):
mnp1(Enter)(Enter)wStep 4: Create the file system
My original command was:
mkfs.ext4 /dev/your-new-diskCorrection: 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:
mkfs.ext4 /dev/sdb1Run lsblk first to confirm the partition name. Check that it shows as ext4 afterwards with:
blkid /dev/sdb1Step 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):
mkdir /dataThen edit the file system table:
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 2defaults: the standard mount options.0: do not back up with the olddumptool.2: check this file system with fsck at boot, after the root file system (which uses1).0means never check.
Step 6: Mount everything in fstab
mount -aThis 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:
df -h /dataNotes for today
- A mistake in
/etc/fstabcan stop the system booting (it may drop to an emergency shell). Always runmount -aafter editing and fix any errors before rebooting. Adding thenofailoption (defaults,nofail) lets the system carry on booting if this disk is missing. - After running
mount -aon a systemd system you may see a hint to runsystemctl 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, pressgto create a new empty GPT table beforen. Recent versions offdisksupport GPT; on older systems such as CentOS 6,partedorgdiskwere used for GPT. - Red Hat-family systems (CentOS 7 and later, Rocky Linux, AlmaLinux) use XFS as the default file system;
mkfs.xfs /dev/sdb1creates one, withxfsas the type in/etc/fstab. ext4 still works fine. - To unmount a disk, use
umount /data(note the spelling:umount, notunmount).