Linux Disk Management: Partition, Format, Mount and Check Disks
lsblk -f # find the new disk, e.g. /dev/sdb
sudo parted /dev/sdb --script mklabel gpt mkpart data ext4 0% 100%
sudo mkfs.ext4 -L data /dev/sdb1
sudo mkdir -p /data
echo "UUID=$(sudo blkid -s UUID -o value /dev/sdb1) /data ext4 defaults,nofail 0 2" | sudo tee -a /etc/fstab
sudo systemctl daemon-reload
sudo mount -a && df -h /data
Double-check the device name first: partitioning and mkfs erase everything on it.
1. See your disks and partitions
# tree of disks, partitions, filesystems and mount points
lsblk -f
# sizes and models
lsblk -o NAME,SIZE,TYPE,MODEL,SERIAL,TRAN
# UUIDs and filesystem types
sudo blkid
# usage of mounted filesystems
df -hT
# partition tables in detail
sudo fdisk -l
Example output of lsblk -f:
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS
sda
└─sda1 ext4 1.0 data 3f6c2a1e-9b0d-4e5f-8a7b-1c2d3e4f5a6b 850.2G 4% /data
nvme0n1
├─nvme0n1p1 vfat FAT32 A1B2-C3D4 1G 2% /boot/efi
└─nvme0n1p2 ext4 1.0 8d7e6f5a-4b3c-2d1e-0f9a-8b7c6d5e4f3a 120.5G 48% /
Naming: SATA/USB disks are /dev/sda, /dev/sdb… with partitions sda1; NVMe disks are /dev/nvme0n1 with partitions nvme0n1p1. Names can change between boots, which is why fstab should use UUIDs. To find what's filling a disk, use the find largest files script or sudo du -xh / --max-depth=1 | sort -h.
2. Partition a disk
Creating a new partition table erases every partition on that disk. Confirm the device with lsblk (size, model, no mount points) before running any of these, and back up anything you need.
Use GPT for every new disk unless an old BIOS machine needs to boot from it with MBR. Three tools:
fdisk(interactive, GPT and MBR):sudo fdisk /dev/sdb, then g new GPT table, n new partition, t type, p print, w write. Nothing changes until w.parted(scriptable, good for automation):sudo parted /dev/sdb --script \ mklabel gpt \ mkpart backup ext4 0% 50% \ mkpart media ext4 50% 100% sudo parted /dev/sdb printcfdiskor GNOME Disks / KDE Partition Manager / GParted for a visual interface.
Using percentages or MiB boundaries in parted keeps partitions aligned for SSD performance. After changing a disk that is in use, run sudo partprobe /dev/sdb so the kernel rereads the table.
3. Create a filesystem (format)
| Filesystem | Command | Use for |
|---|---|---|
| ext4 | sudo mkfs.ext4 -L data /dev/sdb1 | Default choice: robust, fast, can grow and shrink. |
| XFS | sudo mkfs.xfs -L data /dev/sdb1 | Large files and servers (RHEL/Fedora Server default). Cannot shrink. |
| Btrfs | sudo mkfs.btrfs -L data /dev/sdb1 | Snapshots, checksums, compression (Fedora and openSUSE default). |
| exFAT | sudo mkfs.exfat -n USB /dev/sdb1 | USB sticks and SD cards shared with Windows/macOS. |
| FAT32 | sudo mkfs.fat -F 32 /dev/sdb1 | EFI system partitions, very old devices. |
| swap | sudo mkswap /dev/sdb2 | Swap partition (a swap file is usually easier). |
Packages: e2fsprogs (ext4), xfsprogs, btrfs-progs, exfatprogs, dosfstools. Install with apt, dnf, pacman -S or zypper install as usual.
4. Mount and add to /etc/fstab
# temporary mount
sudo mkdir -p /mnt/usb
sudo mount /dev/sdb1 /mnt/usb
# unmount
sudo umount /mnt/usb
If umount says the target is busy, a shell or program is using it; see umount: target is busy.
For a permanent mount, add a line to /etc/fstab. Format: device mountpoint type options dump pass.
# /etc/fstab examples
UUID=3f6c2a1e-9b0d-4e5f-8a7b-1c2d3e4f5a6b /data ext4 defaults,nofail 0 2
UUID=5e4d3c2b-1a09-4f8e-9d7c-6b5a4f3e2d1c /srv/xfs xfs defaults,nofail 0 0
UUID=A1B2-C3D4 /mnt/share exfat defaults,nofail,uid=1000,gid=1000 0 0
/swapfile none swap sw 0 0
nofaillets the system boot if the disk is missing, instead of dropping to emergency mode. Use it for every non-essential disk.- The last field (pass) is
1for/,2for other ext4 filesystems to be checked at boot,0for XFS, Btrfs, FAT and network shares. - FAT and exFAT have no Unix permissions, so
uid=/gid=set who owns the files.
Always test before rebooting:
sudo findmnt --verify
sudo systemctl daemon-reload
sudo mount -a
findmnt /data
findmnt --verify catches typos and unknown UUIDs; mount -a mounts everything in fstab and errors out on bad lines. A bad fstab line is the most common cause of a system that boots to emergency mode.
5. Resize partitions and filesystems
Resizing is safe when done in the right order, but a mistake (for example shrinking a partition smaller than its filesystem) loses data. Have a backup.
Grow (most common: a VM disk was enlarged)
# 1. grow the partition to fill the disk (package: cloud-guest-utils / cloud-utils-growpart)
sudo growpart /dev/sda 2
# 2. grow the filesystem, online
sudo resize2fs /dev/sda2 # ext4
sudo xfs_growfs / # XFS: takes the mount point
sudo btrfs filesystem resize max / # Btrfs
Shrink ext4
Shrink the filesystem first, then the partition. It must be unmounted, so for / boot a live USB.
sudo umount /dev/sdb1
sudo e2fsck -f /dev/sdb1
sudo resize2fs /dev/sdb1 200G
# then shrink the partition to slightly more than 200G in parted/GParted, and run:
sudo resize2fs /dev/sdb1
GParted from a live USB does all of this in one step and is the safer choice for desktop users.
LVM (Ubuntu Server, Fedora Server, RHEL defaults)
# overview
sudo pvs; sudo vgs; sudo lvs
# use free space in the volume group and grow the filesystem in one step
sudo lvextend -r -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
# add a new disk to the volume group first if it has no free space
sudo pvcreate /dev/sdc
sudo vgextend ubuntu-vg /dev/sdc
Ubuntu Server's installer often leaves half the volume group unallocated, which is why a new server shows a small /. The lvextend -r command above fixes that and is the usual answer to No space left on device on a fresh server.
6. Check disk health with SMART
sudo apt install smartmontools # dnf install smartmontools / pacman -S smartmontools / zypper install smartmontools
# overall verdict
sudo smartctl -H /dev/sda
# full attributes
sudo smartctl -a /dev/sda
sudo smartctl -a /dev/nvme0
# start a short self-test (2 minutes), then read the result
sudo smartctl -t short /dev/sda
sudo smartctl -l selftest /dev/sda
What to look at:
| Attribute | Bad sign |
|---|---|
Reallocated_Sector_Ct (HDD/SATA SSD) | Above 0 and increasing. |
Current_Pending_Sector, Offline_Uncorrectable | Any non-zero value: unreadable sectors. |
UDMA_CRC_Error_Count | Increasing: usually a bad cable, not the disk. |
NVMe Percentage Used | Near or above 100% means rated endurance is used up. |
NVMe Media and Data Integrity Errors | Any non-zero value. |
The NVMe-specific tool gives the same data: sudo nvme smart-log /dev/nvme0 (package nvme-cli). Enable continuous monitoring with sudo systemctl enable --now smartd (the service may be called smartmontools on Debian/Ubuntu).
A filesystem that suddenly turns read-only is often the first symptom of a failing disk; see Read-only file system, and check sudo dmesg | grep -i -E 'error|i/o'.
7. Check and repair filesystems
# ext4: must be unmounted (use a live USB for /)
sudo umount /dev/sdb1
sudo fsck -f /dev/sdb1
# XFS
sudo xfs_repair /dev/sdb1
# Btrfs: scrub runs online and verifies checksums
sudo btrfs scrub start -B /
sudo btrfs device stats /
Never run fsck on a mounted filesystem; it can corrupt it. For the root filesystem on ext4, you can also schedule a check at next boot by adding fsck.mode=force to the kernel command line once (see kernel parameters).
Swap files
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
swapon --show
On Btrfs, create swap files with sudo btrfs filesystem mkswapfile --size 4G /swap/swapfile instead, because swap files there need copy-on-write disabled. Fedora uses zram (compressed RAM swap) by default, so a swap file is usually unnecessary there. The Arch Wiki's fstab and partitioning pages are good deeper references for any distro.
Frequently asked questions
How do I mount a disk permanently on Linux?
Find its UUID with lsblk -f, create a mount point, add a line like UUID=... /data ext4 defaults,nofail 0 2 to /etc/fstab, then test with sudo mount -a before rebooting.
Which filesystem should I use?
ext4 for general use and maximum compatibility, XFS for large servers, Btrfs if you want snapshots and checksums, and exFAT for USB drives shared with Windows and macOS.
How do I check if a disk is failing?
Install smartmontools and run sudo smartctl -H -A /dev/sdX (or /dev/nvme0). Rising reallocated or pending sector counts, or NVMe media errors, mean back up now and replace the disk.
Can I resize a mounted partition?
You can grow ext4, XFS and Btrfs while mounted. Shrinking ext4 requires unmounting it first, and XFS cannot be shrunk at all.
Spotted a mistake or a command that no longer works? Send a correction — pages are re-checked when reports come in.