Creating Partitions and Filesystems - fdisk and mkfs
What You Will Achieve
- Explain the difference between MBR and GPT and how to choose
- Create partitions with
fdisk/gdisk/parted - Create ext4 / xfs / vfat filesystems with the
mkfsfamily - Build a swap area with
mkswapandswapon - Inspect disk state accurately with
lsblk/blkid/partprobe
This is the core of LPIC-1 objective 104.1 "Create partitions and filesystems". It covers the full flow of dividing a disk and making it usable.
What Do You Decide First?
The first decision is "MBR or GPT", because it changes which partition-editing tool you use. Choose GPT for disks larger than 2TB or when you need more than four primary partitions.
To make a disk usable, you normally go through three stages. Skipping the order means you cannot mount.
| Stage | Operation | Typical commands |
|---|---|---|
| 1. Partition | Divide the disk into regions | fdisk / gdisk / parted |
| 2. Create filesystem | Format each region | mkfs.ext4 / mkfs.xfs / mkswap |
| 3. Mount | Attach a region to a directory | mount (out of scope here) |
A partition alone cannot store data yet. If you forget to create a filesystem with mkfs, you get a wrong fs type error at the mount stage.
How Do MBR and GPT Differ?
MBR is the older scheme with a limit of four primary partitions. GPT is the newer scheme that supports large disks and many partitions.
| Item | MBR (msdos) | GPT |
|---|---|---|
| Max disk size | Up to 2TB | Beyond 2TB |
| Primary partitions | Up to 4 | 128 in practice |
| Extended partition | Required (3 primary + 1 extended) | Not needed |
| Editing tool | fdisk |
gdisk |
| Common tool | parted (both) |
parted (both) |
When MBR needs five or more regions, you create 3 primary partitions plus 1 extended partition, and place logical partitions inside the extended one. GPT removes this constraint and treats all partitions equally.
Steps
Step 1: Check disk layout with lsblk
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sda 8:0 0 50G 0 disk ├─sda1 8:1 0 49G 0 part / └─sda2 8:2 0 1G 0 part [SWAP] sdb 8:16 0 20G 0 disk
lsblk shows block devices and partitions hierarchically. Here sdb is still unpartitioned (no sdb1 child). Mistaking the target device name destroys existing data, so always check first.
Step 2: Create an MBR partition with fdisk
fdisk /dev/sdb
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (default 41943039): +10G
Created a new partition 1 of type 'Linux' and of size 10 GiB.
Command (m for help): w
The partition table has been altered.
The main fdisk subcommands are n (new), p (print table), d (delete), t (change type), w (write and quit), and q (quit without saving). Use n to specify type, number, and size, then commit with w.
Nothing is written to disk until you run w. Conversely, pressing w overwrites the existing partition table. If you opened the wrong device, quit without saving with q.
Step 3: Use gdisk for GPT, parted for general use
gdisk /dev/sdb parted /dev/sdb mklabel gpt parted /dev/sdb mkpart primary ext4 1MiB 10GiB
Command (? for help): n Partition number (1-128, default 1): 1 First sector (...): Last sector (...): +10G Current type is 8300 (Linux filesystem) Command (? for help): w
GPT disks are edited interactively with gdisk. Its operation set resembles fdisk, using n (create), d (delete), and w (write). parted supports both MBR and GPT and can run mklabel (create the table type) and mkpart (create a partition) on one line, making it well suited to scripting.
Step 4: Reload the table into the kernel with partprobe
partprobe /dev/sdb lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sdb 8:16 0 20G 0 disk └─sdb1 8:17 0 10G 0 part
Even after rewriting the partition table, a kernel may still hold the old information for a disk in use. partprobe asks the kernel to reread the partition table. Confirm with lsblk that the new sdb1 is recognized.
Step 5: Create a filesystem with mkfs
mkfs.ext4 /dev/sdb1 blkid /dev/sdb1
mke2fs 1.46.5 (30-Dec-2021) Creating filesystem with 2621440 4k blocks and 655360 inodes Filesystem UUID: 3f29c0a1-...-9b2e4d Writing superblocks and filesystem accounting information: done /dev/sdb1: UUID="3f29c0a1-...-9b2e4d" TYPE="ext4"
mkfs.ext4 creates an ext4 filesystem. mkfs.xfs (XFS) and mkfs.vfat (FAT, -F 32 for FAT32) work the same way. mkfs.ext4 internally calls mke2fs. After creation, check the UUID and filesystem type with blkid. The UUID is used for persistent mounts in /etc/fstab.
Step 6: Create a swap area with mkswap
mkswap /dev/sdb2 swapon /dev/sdb2 swapon --show
Setting up swapspace version 1, size = 2 GiB no label, UUID=8a1f...e07c NAME TYPE SIZE USED PRIO /dev/sdb2 partition 2G 0B -2
A swap partition is initialized with mkswap, not mkfs. Enable it with swapon and disable it with swapoff. Confirm active swap with swapon --show or free -h. Note that a swap area also fails on swapon unless you run mkswap first.
How Do You Choose ext / xfs / vfat / btrfs?
For a general-purpose Linux filesystem, ext4 is a safe default. Use xfs for large capacity and high concurrency, and vfat for Windows sharing or the EFI system partition.
| Type | Create command | Main use |
|---|---|---|
| ext2 | mkfs.ext2 |
No journaling. Small volumes, boot partitions |
| ext3 | mkfs.ext3 |
ext2 plus journaling |
| ext4 | mkfs.ext4 |
Standard for Linux. Large and reliable |
| xfs | mkfs.xfs |
Large capacity, high throughput |
| vfat | mkfs.vfat |
FAT. Windows sharing, EFI system partition |
| btrfs | mkfs.btrfs |
Newer FS with snapshots and subvolumes |
ext2/3/4 are compatible by lineage, with mke2fs as the common implementation. Note operationally that xfs cannot be shrunk once created (grow only). btrfs is a relatively new filesystem with built-in snapshots and RAID features; for LPIC-1, an overview-level understanding is enough.
Troubleshooting
Symptom: mount reports wrong fs type
Cause: You created the partition but did not create a filesystem with mkfs
Check:
blkid /dev/sdb1
Fix: If blkid shows no TYPE=, it is unformatted. Create a filesystem with mkfs.ext4 /dev/sdb1 or similar.
Symptom: fdisk cannot create a fifth primary partition
Cause: MBR is limited to four primary partitions
Check:
fdisk -l /dev/sdb
Fix: Create an extended partition and place logical partitions inside it. To avoid the limit entirely, switch to GPT with parted ... mklabel gpt (existing data is lost).
Symptom: A partition does not appear in lsblk
Cause: The kernel holds the old partition table
Check:
lsblk /dev/sdb
Fix: Run partprobe /dev/sdb to make the kernel reread the table. If it still does not appear, reboot.
Symptom: swapon fails with Invalid argument
Cause: You did not run mkswap on the target partition
Check:
blkid /dev/sdb2
Fix: Initialize it as swap with mkswap /dev/sdb2, then run swapon /dev/sdb2.
Completion Checklist
- [ ] Confirmed with
lsblkthat you targeted the right device - [ ] Created the partition with
fdisk/gdisk/partedand wrote it withw - [ ] Reloaded the table into the kernel with
partprobe - [ ] Formatted with
mkfs.ext4(or the mkfs for your purpose) - [ ] Enabled swap with
mkswapthenswapon - [ ] Checked UUID and filesystem type with
blkid
Summary
| Scenario | Command | Purpose |
|---|---|---|
| Inspect | lsblk / fdisk -l |
Understand devices and partitions |
| Edit MBR | fdisk /dev/sdb |
Interactive editing with n/p/d/t/w/q |
| Edit GPT | gdisk /dev/sdb |
Create GPT partitions |
| General edit | parted ... mklabel/mkpart |
Scriptable, both formats |
| Apply | partprobe /dev/sdb |
Reload the table |
| Format | mkfs.ext4 /dev/sdb1 |
Create a filesystem |
| Swap | mkswap / swapon |
Build a swap area |
| Verify | blkid /dev/sdb1 |
Check UUID and FS type |
Creating partitions and filesystems is the entry point to making a disk usable. Next, proceed to mounting that filesystem and making it persistent in /etc/fstab to complete your understanding.