Linux Admin

Linux Disk & Filesystem: lsblk, mount, fstab, fsck

Part of pathway: Linux Mastery: 300 Commands

Disks, Partitions, Filesystems, Mount Points

Linux storage has four layers, and confusing them is the #1 cause of “why isn’t my disk showing up” questions. Block devices (the raw disk: /dev/sda) hold partitions (/dev/sda1, /dev/sda2) which contain filesystems (ext4, xfs, btrfs) which get mounted at directories in your tree (/, /home, /mnt/data).

This article is a working reference for Linux disk and filesystem commands — viewing usage, partitioning, formatting, mounting (manual and persistent via fstab), and the diagnostic flow when something runs out of space.

What Storage Do I Have? — lsblk and blkid

lsblk                                    # tree of block devices
lsblk -f                                 # with filesystem info and UUIDs
blkid                                    # show UUID and TYPE for all
blkid /dev/sda1                          # for a specific device

lsblk is the first command for orientation. Output:

NAME    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda       8:0    0   500G  0 disk
├─sda1    8:1    0   100G  0 part /
├─sda2    8:2    0   200G  0 part /home
├─sda3    8:3    0   100G  0 part /var
└─sda4    8:4    0   100G  0 part [SWAP]
sdb       8:16   0     1T  0 disk
└─sdb1    8:17   0     1T  0 part /mnt/data

Disk Usage — df

df -h                                    # human-readable sizes
df -i                                    # show inode usage instead of bytes
df -T                                    # include filesystem TYPE
df -h /var                               # specific mountpoint

df -i matters more than people realize. A filesystem can be 30% full in bytes but 100% full in inodes if you have millions of tiny files. The error message is the same as out-of-space: No space left on device. Always check both.

Directory Sizes — du

du -sh /var/log                          # summary, human-readable
du -h --max-depth=1 /var                 # one level deep
du -sh /var/* | sort -h                  # sizes sorted ascending
du -ah | sort -h | tail -20              # 20 largest items
ncdu /                                   # interactive (install separately)

ncdu is dramatically more useful than du for “where did all my disk go?” questions. Install it.

Partitioning — fdisk, parted, cfdisk

fdisk -l                                 # list all partition tables
fdisk /dev/sdb                           # interactive partition editor
parted /dev/sdb print                    # parted's read-only view
parted /dev/sdb mklabel gpt              # create GPT partition table
parted /dev/sdb mkpart primary ext4 0% 100%
cfdisk /dev/sdb                          # ncurses-style partition editor

fdisk only handles MBR by default on older versions. Use parted or modern fdisk for GPT (drives larger than 2 TB).

Formatting — mkfs

mkfs.ext4 /dev/sdb1                      # ext4 filesystem
mkfs.xfs /dev/sdb1                       # XFS (preferred for large data)
mkfs.btrfs /dev/sdb1                     # btrfs (snapshots, COW)
mkfs.ext4 -L data /dev/sdb1              # with a label
mkfs.fat -F32 /dev/sdb1                  # FAT32 (USB sticks)
mkswap /dev/sdb2                         # swap partition

Picking a filesystem:

  • ext4 — safe default. Mature, fast for most workloads.
  • XFS — preferred for large filesystems (>100GB), high I/O databases. Default on RHEL.
  • btrfs — copy-on-write, snapshots, RAID. More features, more complexity.
  • ZFS — not in mainline kernel, requires DKMS. Worth it for large storage pools.

Mounting — Manual

mount /dev/sdb1 /mnt/data                # mount once
mount -o ro /dev/sdb1 /mnt/data          # read-only
mount -o remount,rw /                    # remount root as read-write
umount /mnt/data                         # unmount
umount -l /mnt/data                      # lazy unmount (busy filesystem)
mount                                    # show all current mounts
findmnt                                  # nicer mount listing

If umount fails with “target is busy,” find what’s using it: lsof +D /mnt/data or fuser -vm /mnt/data.

Persistent Mounts — /etc/fstab

UUID=abc-123-def /mnt/data ext4 defaults,noatime 0 2

Six fields: device, mount point, filesystem, options, dump (always 0), fsck order (0=skip, 1=root, 2=others).

Always use UUIDs, not /dev/sdb1. Device names can shift between reboots if you add or remove disks; UUIDs are tied to the filesystem and persist. Get UUID with blkid /dev/sdb1.

After editing /etc/fstab:

mount -a                                 # mount everything in fstab not yet mounted
findmnt --verify                         # check for syntax issues

Test fstab BEFORE rebooting. A bad /etc/fstab entry can drop you into emergency mode at boot. mount -a will throw the error live, while you still have a working system.

Common Mount Options

  • defaults — rw, suid, dev, exec, auto, nouser, async
  • noatime — don’t update access time on read (recommended for SSDs)
  • nodiratime — same but for directories
  • ro — mount read-only
  • nosuid — ignore setuid bits (security)
  • nodev — no device files (security)
  • noexec — no executables (security; common on /tmp)

Filesystem Repair — fsck

fsck /dev/sdb1                           # check (filesystem must be unmounted!)
fsck -y /dev/sdb1                        # answer yes to all
fsck -f /dev/sdb1                        # force check even if clean
xfs_repair /dev/sdb1                     # for XFS (won't use fsck)

Never run fsck on a mounted filesystem. It can corrupt the filesystem irreversibly. Boot from rescue media or unmount first.

Swap

swapon -s                                # show active swap
swapoff /swapfile                        # disable swap (e.g., before resizing)
swapon /swapfile                         # enable
free -h                                  # see swap in memory summary

# Adding a swap file:
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

Common Pitfalls

  • Out of inodes, not out of bytes. Always check both df -h and df -i.
  • Bad fstab entry blocks boot. Use nofail in options for non-critical mounts so a missing disk doesn’t kill the boot.
  • /dev/sdX name shifting. Use UUIDs in fstab.
  • fsck on mounted filesystem. Never. Boot from rescue media.
  • Forgot chmod 600 on swap file. The kernel may refuse, or worse, expose memory to other users.
  • noatime not set. On SSDs, every read becomes a write to the inode atime. noatime is free performance.

Conclusion

Five habits:

  1. lsblk first whenever you’re working with storage. Orient before acting.
  2. UUIDs in fstab, not device names.
  3. Test mount -a before reboot.
  4. Check df -h AND df -i when something says “out of space.”
  5. ncdu for visual disk-hog hunting.

Related Linux Admin troubleshooting

For common errors and fixes related to this topic, see:

Leave a Reply